#1 2023-05-22 12:01:22

damiand
Member
From: Greece
Registered: 2018-09-21
Posts: 94

Calculated fields are actually created as table fields in the DB?

Hi

I've noticed that calculated fields in ORM classes created as read-only published properties, are actually created as DB tables. For example in this class:

TPerson = class(TOrm)
  private
    fFirstName, fSurName: RawUTF8;
  protected
    function GetFullName: RawUTF8;
  published
    property FirstName: RawUTF8 index 50 read fFirstName write fFirstName;
    property SurName: RawUTF8 index 50 read fSurName write fSurName;
    property FullName: RawUTF8 read GetFullName;
end;

registered as a virtual MS-SQL Server table, ORM actually creates a field named FullName in the database table. Is there any purpose on this? In case I don't want a calculated field to be actually created as a db table field, do I have to use a public calculation function instead (e.g. GetFullName), and not a published property?

Offline

#2 2023-05-22 12:10:54

tbo
Member
Registered: 2015-04-20
Posts: 349

Re: Calculated fields are actually created as table fields in the DB?

damiand wrote:

In case I don't want a calculated field to be actually created as a db table field, do I have to use a public calculation function instead (e.g. GetFullName), and not a published property?

Yes, you can write it like this:

TPerson = class(TOrm)
private
  ...
  function GetFullName: RawUTF8;
public
  property FullName: RawUTF8 read GetFullName;
published
  property FirstName: RawUTF8 index 50 read fFirstName write fFirstName;
  property SurName: RawUTF8 index 50 read fSurName write fSurName;
end;

But for clarity, I would write it this way:

TCustomPerson = class(TOrmBaseRecord)
private
  fFirstName, fSurName: RawUTF8;
published
  property FirstName: RawUTF8 index 50 read fFirstName write fFirstName;
  property SurName: RawUTF8 index 50 read fSurName write fSurName;
end;

TPerson = class(TCustomPerson)
private
  function GetFullName: RawUTF8;
public
  property FullName: RawUTF8 read GetFullName;
end;

Then, if necessary, you can derive the ORM object TOrmBaseRecord on the server or client from different base objects using a compiler switch. On the server derived from the class TOrm and on the client from the class TObjectWithID. All TCustom... classes could be shared between server and client. The specializations are done only in the final classes. I have already described the technique here in the forum.

With best regards
Thomas

Last edited by tbo (2023-05-22 12:32:47)

Offline

#3 2023-05-22 14:07:23

damiand
Member
From: Greece
Registered: 2018-09-21
Posts: 94

Re: Calculated fields are actually created as table fields in the DB?

Thanks Thomas! smile

Offline

Board footer

Powered by FluxBB