FireDAC.AutoincFields Sample
This sample demonstrates how to implement auto-incremental fields using FireDAC.
Contents
Location
You can find the AutoincFields sample project at:
- Start | Programs | Embarcadero RAD Studio Rio | Samples and then navigate to:
Object Pascal\Database\FireDAC\Samples\DApt Layer\Autoinc fields
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
Description
The AutoincFields sample shows you how to work with auto-incremental fields using FireDAC. To this end, the sample uses the TFDDatSColumn class to control the client-side and the server-side auto-incrementing fields. Concretely, the sample uses the TFDDatSColumn.ServerAutoIncrement property of TFDDatSColumn to specify when the DBMS generates an auto-incrementing value for the new record column.
How to Use the Sample
- Navigate to the location given above and open
AutoInc.dproj
. - Press F9 or choose Run > Run.
- Click on the Use Connection Definition combo box and select an option.
Files
File in Delphi | Contains |
---|---|
|
The project itself. |
|
The main form. |
Implementation
The sample implements the following features.
Create schema adapter
To this end, the sample uses FDCreateInterface. See the following code:
var
oSchAdapt: IFDDAptSchemaAdapter;
// ...
begin
// create schema adapter
FDCreateInterface(IFDDAptSchemaAdapter, oSchAdapt);
Add master table adapter
The sample adds the master table adapter and sets some configuration properties, such as the ServerAutoIncrement property. See the following code:
var
oMastAdapt: IFDDAptTableAdapter;
// ...
begin
oMastAdapt := oSchAdapt.TableAdapters.Add(EncodeName('FDQA_master_autoinc'), 'master');
with oMastAdapt do begin
// ...
with DatSTable.Columns[0] do
ServerAutoIncrement := True;
// ...
end
// ...
- Note: setting the ServerAutoIncrement property to
True
automatically assigns the AutoIncrementSeed and AutoIncrementStep properties to-1
.
Add detail table adapter
The sample adds a detail table adapter and sets up some features. To this end, the sample adds the following code:
var
oDetAdapt: IFDDAptTableAdapter;
// ...
begin
// ...
oDetAdapt := oSchAdapt.TableAdapters.Add(EncodeName('FDQA_details_autoinc'), 'details');
with oDetAdapt do begin
// ...
with DatSTable.Columns[0] do
ServerAutoIncrement := True;
// ...
end
// ...
Add constraints to the DatSManager
To this end, the sample uses the Constraints property of TFDDatSTable. See the following code:
with oSchAdapt.DatSManager.Tables.ItemsS['master'] do
Constraints.AddUK('master_pk', 'parent_id', True);
with oSchAdapt.DatSManager.Tables.ItemsS['details'] do begin
Constraints.AddUK('details_pk', 'child_id', True);
with Constraints.AddFK('details_fk_master', 'master', 'parent_id', 'fk_parent_id') do begin
UpdateRule := crCascade;
DeleteRule := crCascade;
AcceptRejectRule := arCascade;
end;
end;
Add new row to the master table
oMastRow := oMastAdapt.DatSTable.Rows.Add([Unassigned, 'Master ' + IntToStr(Random(1000000000))]); // Set Unassigned for identity fields
Post changes to RDBMS
Finally, the sample updates the changes with the Update method. Then, the sample uses the Reconcile method to obtain the error information for each record that could not be applied.
oSchAdapt.Update;
oSchAdapt.Reconcile;
Uses
- FDCreateInterface
- TFDAutoIncField
- IFDPhysCommand
- The sample also uses some properties such as Constraints of the following unit:
- FireDAC.DatS
- The sample also uses some methods such as Update or Reconcile and some properties such as ColumnMapping of the following units:
- FireDAC.DApt
- FireDAC.DApt.Intf
See Also
Samples
- FireDAC DAptLayer GettingStarted sample
- FireDAC DApt Layer Commands sample
- FireDAC Mapping Columns sample