Retrieving Existing Objects
Objects that have been stored to the database can be retrieved into memory by using any of the following techniques: If the Id of the object is known the Retrieve constructor can be used.An optional connector through which the object shall be retrieved can be specified. If no connector is specified, the default connector is used.If the object already exists in memory, the existing object is returned.If the object is not found, the constructor returns nil. See example 1.
If the Id of the object is not known or multiple objects meeting a certain criteria need to be retrieved, a Selector or an InstantQuery can be used. See example 2 and example 3.
Example 1
pascal
var
Person: TPerson;
begin
Person := TPerson.Retrieve('12345678', MyConnector);
try
ShowMessage(Person.Name);
finally
Person.Free;
end;
end;
Example 2
pascal
var
I: Integer;
Person: TPerson;
begin
with TInstantSelector.Create(nil) do
try
Connector := MyConnector;
Command.Text := 'SELECT * FROM TPerson WHERE Company.Name = "Happy Donuts"';
Open;
for I := 0 to Pred(ObjectCount) do
begin
Person := Objects[I] as TPerson;
Person.Salary := Person.Salary * 1.05;
Person.Store;
end;
finally
Free;
end;
end;
Example 3
pascal
var
I: Integer;
Person: TPerson;
begin
with MyConnector.CreateQuery do
try
Command := 'SELECT * FROM TPerson WHERE Company.Name = "Happy Donuts"';
Open;
for I := 0 to Pred(ObjectCount) do
begin
Person := Objects[I] as TPerson;
Person.Salary := Person.Salary * 1.05;
Person.Store;
end;
finally
Free;
end;
end;