#1 2021-10-04 20:03:10

wxinix
Member
Registered: 2020-09-07
Posts: 121

Using mORMot2 framework for PostgreSQL partitioned tables

Following this query "How to get a string copy of CreateSQLMultiIndex",  @ab implemented two extra really cool callbacks to facilitate PostgreSQL partitioned tables in mormot.orm.sql.pas :

- TRestStorageExternal.OnEngineCreate, for customize-creating the parent partitioned table
- TRestStorageExternal.OnEngineCreateMultiIndex for customize-creating index for the child partitions

However, some problems on using them - in the following code,  (FServer.OrmInstance as TRestOrmServer).StaticDataServer[LTable] as TRestStorageExternal would return nil (the nil is set at line 594, mormot.orm.server, where fStaticData = nil, causing the result set to be nil).

Any advice or insights?

  VirtualTableExternalRegister(FSQLModel, ATables, FSQLDBConnectionProps);
  FServer := TRestServerDB.Create(FSQLModel, SQLITE_MEMORY_DATABASE_NAME, False, '');

  if FSQLDBConnectionProps.Dbms = dPostgreSQL then
  begin
    for var LTable in ATables do
    begin
      var LStorExt := (FServer.OrmInstance as TRestOrmServer).StaticDataServer[LTable] as TRestStorageExternal; // <=<= This returns LStorExt = nil
      LStorExt.OnEngineCreate := AOnEngineCreate;
      LStorExt.OnEngineCreateMultiIndex := AOnEngineCreateMultiIndex;
    end;
  end;

  FServer.CreateMissingTables;

Last edited by wxinix (2021-10-05 12:40:47)

Offline

#2 2021-10-05 09:37:56

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

Re: Using mORMot2 framework for PostgreSQL partitioned tables

Ah you are right: it is too soon.
The external storage is initialized during CreateMissingTables call.

I will look into it.
Sorry for the issue.

Offline

#3 2021-10-06 14:59:00

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

Re: Using mORMot2 framework for PostgreSQL partitioned tables

I have moved external table/field/index creation customization callbacks to TSqlDBConnectionProperties.

Now you should be able to customize the process as you expect.

Offline

#4 2021-10-06 16:41:37

wxinix
Member
Registered: 2020-09-07
Posts: 121

Re: Using mORMot2 framework for PostgreSQL partitioned tables

ab wrote:

I have moved external table/field/index creation customization callbacks to TSqlDBConnectionProperties.

Now you should be able to customize the process as you expect.

Thank you ab!

With these callbacks affiliated with TSqlDBConnectionProperties,  it is more logical (than with TRestStorageExternal).  The entire workflow is much better organized.   

Everything works like a CHARM, clean and beautiful.

Thank you again for this wonderful work.

Last edited by wxinix (2021-10-06 16:44:13)

Offline

#5 2021-10-06 20:38:50

wxinix
Member
Registered: 2020-09-07
Posts: 121

Re: Using mORMot2 framework for PostgreSQL partitioned tables

In the OnTableCreate handler,  what is the best way to determine the Table Creation result (True / False)?

TRestStorageExternal has a method ExecuteDirect which would return a boolean, but it is not accessible from TSqlDBConnectionProperties.

function TMormotOrmModel.OnTableCreate(ASender: TSqlDBConnectionProperties; const ATableName: RawUtf8; const
  AColumns: TSqlDBColumnCreateDynArray; const ASQL: RawUtf8): Boolean;
begin
   ASender.ExecuteNoResult(ASQL, []); // <<--- how to determine if this table-creation Query is successful or not?   
end;

Edit:

Is it possible to changed the callback parameter const ATableName: RawUtf8 to const ATable: TOrmClass?

Last edited by wxinix (2021-10-06 21:37:26)

Offline

#6 2021-10-07 07:03:20

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

Re: Using mORMot2 framework for PostgreSQL partitioned tables

It raises an exception on issue.

Offline

#7 2021-10-08 08:24:21

wxinix
Member
Registered: 2020-09-07
Posts: 121

Re: Using mORMot2 framework for PostgreSQL partitioned tables

@ab Is there a way otherwise elegant to obtain the TOrmClass info from inside the callbacks?

Offline

#8 2021-10-08 10:36:24

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

Re: Using mORMot2 framework for PostgreSQL partitioned tables

The easiest is to define your callback in the context of the TRestServer instance, so that it can access the TModel and therefore find the TOrmClass from the table name.

Offline

#9 2021-10-08 12:41:52

wxinix
Member
Registered: 2020-09-07
Posts: 121

Re: Using mORMot2 framework for PostgreSQL partitioned tables

ab wrote:

The easiest is to define your callback in the context of the TRestServer instance, so that it can access the TModel and therefore find the TOrmClass from the table name.

That is what I thought, but I just was just asking in case there might be more elegant way from mORMot2 framework side.

Offline

Board footer

Powered by FluxBB