#1 2014-11-10 13:27:20

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 516

InitializeTable and inheritance

Hi Arnoud i have the following Problem with InitializeTable:

suppose you have 2 TSQLRecord Classes :

class TBaseClass : TSQLRecord
..
  fReferenceID : RawUtf8;
..
  public
    class procedure InitializeTable(Server: TSQLRestServer; const FieldName: RawUTF8; Options: TSQLInitializeTableOptions); override;
...

and one derived Class:

class TDerivedClass : TBaseClass
..
  fIndexfield1 : RawUtf8;
..
  public
    class procedure InitializeTable(Server: TSQLRestServer; const FieldName: RawUTF8; Options: TSQLInitializeTableOptions); override;
...


class procedure TBaseClass.InitializeTable(Server: TSQLRestServer; const FieldName: RawUTF8; Options: TSQLInitializeTableOptions);
begin
  inherited;
  if (FieldName = '') or (FieldName = 'ReferenceID') then
    Server.CreateSQLMultiIndex(TBaseClass, ['ReferenceID'], false);
end;

class procedure TDerivedClass.InitializeTable(Server: TSQLRestServer; const FieldName: RawUTF8; Options: TSQLInitializeTableOptions);
begin
  inherited;
  if (FieldName = '') or (FieldName = 'fIndexfield1') then
    Server.CreateSQLMultiIndex(TDerivedClass, ['fIndexfield1'], false);
end;


If you call CreateMissingTables(0) an error occurs in TBaseClass.InitializeTable cause
    Server.CreateSQLMultiIndex(TBaseClass, ['ReferenceID'], false); is called with TBaseClass and not with TDerivedClass.

I think that it has to be changed to pass TDerivedclass to the TBaseClass InitializeTable !

Last edited by itSDS (2014-11-10 13:29:07)


Rad Studio 12.1 Santorini

Offline

#2 2014-11-10 13:39:59

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 516

Re: InitializeTable and inheritance

I have 2 Ideas: the best would be it there exists something like "Self" Paramater for the ClassType (don't know if this exists in Delphi) an
pass it to
    Server.CreateSQLMultiIndex("Self - Classtype", ['fIndexfield1'], false);

the second is more Complicated and enhance InitializeTable with the Parameter (Table : TSQLRecordClass)

  class procedure InitializeTable(Server: TSQLRestServer; const FieldName: RawUTF8; Options: TSQLInitializeTableOptions; Table: TSQLRecordClass = nil);

In CreateMissingTables you call the default Paramater (Table = nil)

In custom classes we hav to get rid of this Param

in the derived InitializeTable we have to add

class procedure TDerivedClass.InitializeTable(Server: TSQLRestServer; const FieldName: RawUTF8; Options: TSQLInitializeTableOptions; Table: TSQLRecordClass);
begin
  inherited InitializeTable(Server, FieldName, Options: TSQLInitializeTableOptions, TDerivedClass);
  if (FieldName = '') or (FieldName = 'fIndexfield1') then
    Server.CreateSQLMultiIndex(TDerivedClass, ['fIndexfield1'], false);
end;

and in Baseclass:

class procedure TBaseClass.InitializeTable(Server: TSQLRestServer; const FieldName: RawUTF8; Options: TSQLInitializeTableOptions; Table: TSQLRecordClass);
begin
  if assigned(Table) then begin
    inherited (Server, FieldName, Options: TSQLInitializeTableOptions, Table);
    if (FieldName = '') or (FieldName = 'ReferenceID') then
      Server.CreateSQLMultiIndex(Table, ['ReferenceID'], false);
  end else begin
    inherited;
    if (FieldName = '') or (FieldName = 'ReferenceID') then
      Server.CreateSQLMultiIndex(TBaseClass, ['ReferenceID'], false);
  end;
end;


Rad Studio 12.1 Santorini

Offline

#3 2014-11-10 13:44:40

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: InitializeTable and inheritance

Why not just use self as class type in the inittable method?

Offline

#4 2014-11-10 13:52:54

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 516

Re: InitializeTable and inheritance

Solved by mySelf !

Found in the Embarcadero Doku (http://docwiki.embarcadero.com/RADStudi … e/Methoden) that in class Functions the Self is defined as class of MyClass

so i pass Self to every CreateIndex and it works fine


Rad Studio 12.1 Santorini

Offline

Board footer

Powered by FluxBB