AnyDAC supports correct appending of the new records with auto-incrementing fields and automatic refreshing of the database assigned values.
AnyDAC allows to insert a new record with an auto-incrementing column and get back a new value of this column. That works as for immediate updates, as for cached updates. Depending on a DBMS, the auto-incrementing fields may be implemented either using a special IDENTITY (or similar) column data type, either using a generator (or a sequence) and a table trigger:
|
DBMS |
Auto-incrementing column implementation |
Recognizing |
|
Advantage Database |
AUTOINC data type |
Automatic as auto-incrementing data type. |
|
Firebird / Interbase |
|
Automatic and manual specifying. See below. |
|
IBM DB2 |
GENERATED AS IDENTITY data type |
Automatic as auto-incrementing data type. |
|
Informix |
SERIAL data type |
Automatic as auto-incrementing data type. |
|
MS Access |
COUNTER data type |
Automatic as auto-incrementing data type. |
|
MS SQL Server |
IDENTITY data type |
Automatic as auto-incrementing data type. |
|
MySQL |
AUTO_INCREMENT data type |
Automatic as auto-incrementing data type. |
|
Oracle |
Sequence and BEFORE INSERT FOR EACH ROW trigger |
Manual specifying. See below. |
|
PostgreSQL |
SERIAL data type |
Automatic as auto-incrementing data type. |
|
SQL Anywhere |
IDENTITY data type |
Automatic as auto-incrementing data type. |
|
SQLite |
INTEGER PRIMARY KEY AUTOINCREMENT data type |
Automatic as auto-incrementing data type. |
|
Sybase Adaptive Server Enterprise |
IDENTITY data type |
Automatic as auto-incrementing data type. |
AnyDAC automatically recognizes a column of an auto-incrementing data type, and defines it as dtIntXxx, [caAutoInc, caReadOnly, caAllowNull]. This leads to TField setup:
AnyDAC automatically recognizes limited set of the Firebird auto-incrementing columns, and defines them as dtIntXxx, [caAutoInc, caAllowNull] when:
For Oracle, other Firebird / Interbase cases, and other columns the auto-incrementing mode may be specified by programmer, using one of the options:
All these methods at end leads to TField.AutoGenerateValue = arAutoInc.
By default AnyDAC uses the client side auto-incrementing for the auto-incrementing columns. After calling dataset Insert / Append method, the auto-incrementing column will get -1 value. With each next method call, the value will be incremented with the -1 (negative) step. That was made to distinguish the client assigned values and the DBMS assigned values.
When UpdateOptions.RefreshMode <> rmManual, then after posting a new record the auto-incrementing column will get an actual positive value.
The TADAutoIncField properties allows to adjust the client side auto-incrementing:
When a DBMS supports sequences or generators (here they are synonyms), then AnyDAC may fill an auto-incrementing column from a sequence on a client. This is alternative to client-side auto-incrementing.
The auto-incrementing column must have pfInUpdate in TField.ProviderFlags and one of the UpdateOptions.FetchGeneratorsPoint values:
Set the UpdateOptions.GeneratorName or TADAutoIncField.GeneratorName to a generator name, which will be used to get next sequence value.
The following auto-incrementing column setup will work with any DBMS, including supporting native auto-incremental columns, and using sequences and triggers:
For Oracle, Firebird and Interbase is required to manually specify auto-incremental columns. For databases supporting native auto-incremental data types, no additional setup is required. But specifying will work in both cases.
When master and detail datasets are linked by an auto-incrementing master column and cached updates is enabled, then consider to use Centralized Cached Updates mode with propagation of changes from master to detail datasets.
|
What do you think about this topic? Send feedback!
|