#1 2015-10-09 07:08:41

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

How to access TSQLDBConnectionProperties in TSQLRest objects?

I initialize mORMot like below

  FConnProps := TSQLDBFireDACConnectionProperties.Create(GetServer, GetDatabase, GetUsername, GetPassword);

  FSQLModel := TSQLModel.Create([TSQLCustomer]);

  VirtualTableExternalRegister(FSQLModel, [TSQLCustomer], FConnProps, 'CUSTOMER');

  FRestServerDB := TSQLRestServerDB.Create(FSQLModel, false);
  FRestServerDB.CreateMissingTables();

  FRestClientDB := TSQLRestClientDB.Create(FRestServerDB);

I have also the client code like

  Customers := TSQLCustomer.CreateAndFillPrepare(FRestClientDB, 'SPECIFIC_CUSTOMER_DATABASE', 'SPECIFIC_CUSTOMER_TABLE', 'Company = "mORMot"')
  try
    while Customers.FillOne do
    begin
      // ...
    end; 
  finally
    Customers.Free;
  end;

In the class TSQLCustomer I have the following constructor and would like to have the access to the external db connection properties. Can you built it in?

  TSQLCustomer = class(TSQLRecord);

  TSQLCustomer.Create(aClient: TSQLRest; const aDatabase, aTable: string; const aSQLWhere: RawUTF8); overload; virtual;
  begin
    ConProps := TSQLRestClientDB(aClient).Server.ConnectionDefinition;
    // OR better
    ConProps := TSQLRestClientDB(aClient).Server.ConnectionProperties;
  end;

In the current version of mORMot if I call the code below the returned TSynConnectionDefinition object is empty :-(

  TSQLCustomer.Create(aClient: TSQLRest; const aDatabase, aTable: string; const aSQLWhere: RawUTF8); overload; virtual;
  var
    ConnDef: TSynConnectionDefinition;
  begin
    ConnDef := TSynConnectionDefinition.Create;
    try
      TSQLRestClientDB(aClient).Server.DefinitionTo(ConnDef);
      // ConnDef --> DOESN'T HAVE ANYTHING
    finally
      ConnDef.Free;
    end;
  end;  

Could you fix it? Or what am I doing wrong?


I hope, this improvement will be helpful for the mORMot framework in general! Thank you, AB, very much for you job! The mORMot is awesome! :-)

Last edited by cypriotcalm (2015-10-09 07:29:15)

Offline

#2 2015-10-09 09:33:43

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

Re: How to access TSQLDBConnectionProperties in TSQLRest objects?

You are mixing here the stored values (i.e. TSQLRecord inherited class), and the mean of storage (TSQLRest).

IMHO you should not define such an overloaded constructor to TSQLCustomer, but create a factory function in your own TSQLRestServer instance.

Offline

#3 2015-10-09 09:50:23

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

Re: How to access TSQLDBConnectionProperties in TSQLRest objects?

ab wrote:

You are mixing here the stored values (i.e. TSQLRecord inherited class), and the mean of storage (TSQLRest).

IMHO you should not define such an overloaded constructor to TSQLCustomer, but create a factory function in your own TSQLRestServer instance.

The problem is that I don't know if a database and a table already exist at the time I want to work with them. Their dynamic names are only known at runtime. So, the mORMot should create them if they not exists. How can I achiche this with mORMot?

Offline

#4 2015-10-09 09:57:00

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

Re: How to access TSQLDBConnectionProperties in TSQLRest objects?

This process should be done at the TSQLRest level, not at TSQLCustomer level.

Offline

#5 2015-10-09 10:09:13

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

Re: How to access TSQLDBConnectionProperties in TSQLRest objects?

ab wrote:

This process should be done at the TSQLRest level, not at TSQLCustomer level.

i.e. should create my own TSQLRestServerDB inherited from the TSQLRestServerDB and call in it VirtualTableExternalRegister and .ExternalDB.MapField?

Offline

#6 2015-10-09 10:26:33

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

Re: How to access TSQLDBConnectionProperties in TSQLRest objects?

Or just define a factory method for your TSQLCustomer class, depending on your needs.

A factory method is a method which returns a new class instance, depending on the current context.

Such a method could be implemented where all the needed context is available, may be TSQLRest inherited class, or whatever class you defined in your own code, which knows how to do the table creation if needed.

Certainly not at TSQLCustomer level IMHO.
This would break the http://synopse.info/files/html/Synopse% … #TITLE_330

Offline

#7 2015-10-12 13:11:51

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

Re: How to access TSQLDBConnectionProperties in TSQLRest objects?

Thank you for your advices! I will do it on the proper and right way! :-)

Offline

Board footer

Powered by FluxBB