Skip to content

Id Data Type and Size ​

By default the Id fields of the tables of InstantObjects are strings of 32 characters, filled with a GUID. The properties IdDataType and IdSize let you use a different data type, for example an integer OID, mainly for storage size and efficiency reasons.

How to choose a different data type ​

The connector (and the connection definition managed by the Connection Manager) has two properties:

pascal
property IdDataType: TInstantDataType default dtString;
property IdSize: Integer default InstantDefaultFieldSize;

Set a data type, a size where applicable, and build the database. Only dtString and dtInteger have been tested; data types such as dtBlob and dtBoolean make no sense for an Id.

WARNING

Use the same settings when you connect to an existing database.

Generating the Ids ​

If you use a data type different from string[32] you may also need to replace the default key generation (otherwise InstantObjects generates 32-character GUIDs that might not fit in your key fields). To do it, handle the event OnGenerateId of the connector:

pascal
type
  TInstantGenerateIdEvent = procedure(Sender: TObject; var Id: string) of object;

property OnGenerateId: TInstantGenerateIdEvent;

Here is a sample handler that generates integer Ids by means of a Firebird generator (sequence), with FireDAC:

pascal
procedure TDmMain.FireDACConnectorGenerateId(Sender: TObject; var Id: string);
begin
  Id := VarToStr(FDConnection.ExecSQLScalar('select GEN_ID(G_ID, 1) from RDB$DATABASE'));
end;

where G_ID is a generator defined in the Firebird database.

Remarks ​

  • The property TInstantObject.Id is always of type string, whatever data type you choose for the mapping.

Limitations ​

  • You cannot choose the Id data type for a single class or table: all the tables of a database share the same settings.
  • The mechanism is rather simple: there are no predefined, extensible Id generators to use instead of the event OnGenerateId, and no more flexible mapping strategies (to map existing databases).

See also the properties IdDataType and IdSize of the scheme in the InstantMetadata reference.


Originally written by Nando Dessena, 2005. The example used the IBX broker, now deprecated.

Released under Mozilla License, Version 2.0.