Skip to content

Virtual Containers ​

A Virtual Container is a particular References container of a master object that manages its detail objects. It works only with SQL brokers.

To obtain a master-detail relationship in a SQL environment, the detail object holds a reference to the master object. A virtual container lets the master object see its details as a normal container, using that reference.

What does it do ​

A normal References container stores a blob field in the master record, with the list of the Ids of the detail records.

With a Virtual Container the list of Ids is loaded directly by a query on the detail table, using the field that references the master. This makes details easier to manage:

  • it is never necessary to update the master object after deleting a detail object;
  • you are not responsible for reordering the details in the container: you can order them by a particular attribute.

In the master table the field of the virtual container does not exist.

Defining a Virtual Container ​

In the Attribute Editor (Definition page) use these parameters:

ParameterValue
TypeReferences
Object ClassTxxxx (the class of the details)
Storage KindVirtual
External Storage Namethe name of the detail table and the names of the fields of the detail table that reference the master, separated by ;

In the source code the model definition (IOMETADATA) looks like this:

pascal
Details: References(TISSampleDetail) virtual 'SAMPLEDETAIL;PARENTOBJECTCLASS;PARENTOBJECTID';

The External Storage Name is not used to define a new table for the relationship (as it is for external storage): it carries two pieces of information about the existing detail table:

  1. SAMPLEDETAIL is the name of the detail table;
  2. PARENTOBJECTCLASS;PARENTOBJECTID are the fields of the detail table that reference the master record.

The order of the detail objects ​

You can work with a virtual container as with a normal container, but you cannot change the order of the details with Move(), because that order is never stored in a field.

The default order of the details of a virtual container is the InstantUpdateCountFieldName attribute.

To change the order, override the method GetDetailsStatementValues of the master class:

pascal
procedure TISSampleClass.GetDetailsStatementValues(
  var FromClause, SequenceNoFieldName, OrderByClause: string);
begin
  inherited;
  if SameText(FromClause, 'SAMPLEDETAIL') then
  begin
    SequenceNoFieldName := 'CompanyDescription';
    OrderByClause := 'COMPANY.DX DESC';
  end;
end;

Originally written by Carlo Barazzetta, 2014.

Released under Mozilla License, Version 2.0.