#1 2011-10-31 20:20:39

Sir Rufo
Member
Registered: 2011-10-14
Posts: 24

TSQLVirtualTable with TPersistent/TCollection

While checking out the TSQLVirtualTable i got some strange behavior with them

At first i have a virtual table:

MyModel.VirtualTableRegister( TSQLMyData1Temp, TSQLVirtualTableJSON );

Sending and retrieving data works fine ... but there is some data missing.
The Record contains a collection and the collection data is missing in the file ... where has it gone?
Switching the table into a normal TSQLRecord everything is fine - also the collection data exists in the sqlite file.

Last edited by Sir Rufo (2011-10-31 20:21:45)

Offline

#2 2011-11-01 09:06:50

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

Re: TSQLVirtualTable with TPersistent/TCollection

The record MUST be a TSQLRecord  descendant.

Online

#3 2011-11-01 17:49:05

Sir Rufo
Member
Registered: 2011-10-14
Posts: 24

Re: TSQLVirtualTable with TPersistent/TCollection

ab wrote:

The record MUST be a TSQLRecord  descendant.

it is ... not really TSQLRecord, but TSQLRecordVirtual, cause otherwise I can't register this as a virtual table

TMyData1Temp = class( TSQLRecordVirtual )
published
  property MyCollection : TCollection read GetMyCollection;
end;

...

MyModel.VirtualTableRegister( TMyData1Temp, TSQLVirtualTableJSON );

so if i had to use a TSQLRecord descendant, how do I register as a virtual table?

Offline

#4 2011-11-02 08:24:56

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

Re: TSQLVirtualTable with TPersistent/TCollection

As stated by the documentation (see e.g. the definition of TSQLVirtualTableJSON), you shall inherit your class from TSQLRecordVirtualTableAutoID instead of TSQLRecordVirtual, which is an abstract class common to all virtual tables:

  {{ A TSQLRestServerStaticInMemory-based virtual table using JSON storage
   - for ORM access, you should use TSQLModel.VirtualTableRegister method to
     associated this virtual table module to a TSQLRecordVirtualTableAutoID class
   - transactions are not handled by this module
   - by default, no data is written on disk: you will need to call explicitly
     aServer.StaticVirtualTable[aClass].UpdateToFile for file creation or refresh
   - file extension is set to '.json' }
  TSQLVirtualTableJSON = class(TSQLVirtualTable)
  ....

Online

#5 2011-11-02 09:10:47

Sir Rufo
Member
Registered: 2011-10-14
Posts: 24

Re: TSQLVirtualTable with TPersistent/TCollection

upps ... you are totally right

after building a test case, everything works fine ... smile

Offline

#6 2011-11-02 09:43:44

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

Re: TSQLVirtualTable with TPersistent/TCollection

Nice!

I'm happy for you. smile

Online

#7 2011-11-02 15:26:14

Sir Rufo
Member
Registered: 2011-10-14
Posts: 24

Re: TSQLVirtualTable with TPersistent/TCollection

But there is still a difference, when using VirtualTables

here my record

type
  TSQLSomeDataReal = class( TSQLRecord )
  private
    fStrData :     SynUnicode;
    FStrDataColl : TSomeDataCollection;
  public
    constructor Create; override;
    destructor Destroy; override;
  published
    property StrData :     SynUnicode read fStrData write fStrData;
    property StrDataColl : TSomeDataCollection read FStrDataColl;
  end;

  TSQLSomeDataVirtual = class( TSQLRecordVirtualTableAutoID )
  private
    fStrData :     SynUnicode;
    FStrDataColl : TSomeDataCollection;
  public
    constructor Create; override;
    destructor Destroy; override;
  published
    property StrData :     SynUnicode read fStrData write fStrData;
    property StrDataColl : TSomeDataCollection read FStrDataColl;
  end;

and the creation of the client (and the database)

begin
  Model := TSQLModel.Create( [TSQLSomeDataReal, TSQLSomeDataVirtual] );
  Model.VirtualTableRegister( TSQLSomeDataVirtual, TSQLVirtualTableJSON );
  fClient := TSQLRestClientDB.Create( Model, nil, TSQLDatabase.Create( ChangeFileExt( ParamStr( 0 ), '.db' ) ),
    TSQLRestServerDB );
  ( fClient as TSQLRestClientDB ).Server.CreateMissingTables( 0 );
  ( fClient as TSQLRestClientDB ).Server.CreateSQLIndex( TSQLSomeDataReal, 'StrData', True );
end;

but there will be no UNIQUE INDEX in the database

without the VirtualTable

begin
  Model := TSQLModel.Create( [TSQLSomeDataReal] );
  fClient := TSQLRestClientDB.Create( Model, nil, TSQLDatabase.Create( ChangeFileExt( ParamStr( 0 ), '.db' ) ),
    TSQLRestServerDB );
  ( fClient as TSQLRestClientDB ).Server.CreateMissingTables( 0 );
  ( fClient as TSQLRestClientDB ).Server.CreateSQLIndex( TSQLSomeDataReal, 'StrData', True );
end;

the UNIQUE INDEX works as expected

Last edited by Sir Rufo (2011-11-02 15:27:17)

Offline

Board footer

Powered by FluxBB