![firedacimagenotitle02 | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/06/firedac-image-no-title02.jpg?resize=1140%2C680&ssl=1)
FireDAC allows the insertion of a new record with an auto-incrementing column and getting back a new value of this column. That works for immediate updates, as well as for cached updates. Depending on the DBMS, the auto-incrementing fields may be implemented either using a special IDENTITY (or similar) column data type, or using a generator (or a sequence) and a table trigger
Table of Contents
Location
You can find the AutoincFields sample project at:
- Start | Programs | Embarcadero RAD Studio Sydney | 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 |
---|---|
AutoInc.dproj AutoInc.dpr | The project itself. |
fAutoInc.pas fAutoInc.fmx | The main form. |
Implementation
The sample implements the following features.
Create schema adapter
To this end, the sample uses FDCreateInterface. See the following code:
1 2 3 4 |
<strong>var</strong> oSchAdapt: IFDDAptSchemaAdapter; <strong>begin</strong> 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:
1 2 3 4 5 6 7 8 |
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:
1 2 3 4 5 6 7 8 |
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:
1 2 3 4 5 6 7 8 9 10 |
with oSchAdapt.DatSManager.Tables.Items['master'] do Constraints.AddUK('master_pk', 'parent_id', True); with oSchAdapt.DatSManager.Tables.Items['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
1 |
oMastRow := oMastAdapt.DatSTable.Rows.Add([Unassigned, 'Master ' + IntToStr(Random(1000000000))]); <em>// Set Unassigned for identity fields</em> |
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.
1 2 |
oSchAdapt.Update; oSchAdapt.Reconcile; |
To see the original post, please refer to the link below:
http://docwiki.embarcadero.com/CodeExamples/Sydney/en/FireDAC.AutoincFields_Sample
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition