AnyDAC allows to selectively override the automatically generated update SQL commands by using TADUpdateSQL or completely override the posting algorithm by using OnUpdateRecord event handler.
Although AnyDAC is able to automatically generate updating SQL commands, in some cases this can not be done correctly:
|
Original SQL command |
Generated SQL command possible issues |
Possible actions |
|
SELECT with JOIN's |
Includes columns from the joined tables. |
|
|
SELECT with grouping operations |
Updates more than one record. |
|
|
SELECT with common table expressions |
Fails to get updating table name. |
|
|
SELECT with aliased columns and expressions |
Includes incorrect column names. |
|
|
Stored procedure call |
All above SELECT issues. |
|
|
Cursor expressions and variable |
All above SELECT issues. |
|
|
Other non-SELECT commands |
All above SELECT issues. |
|
Note, the above is not a must, instead check first, what really happens.
AnyDAC can detect a base table and column names for an aliased or joined columns:
|
DBMS |
Setup |
|
Advantage Database |
Is not supported. |
|
Firebird / Interbase |
Required ExtendedMetadata=True connection definition parameter. |
|
IBM DB2 |
Automatically. |
|
MS Access |
Automatically. |
|
MS SQL Server |
Required ExtendedMetadata=True connection definition parameter. |
|
MySQL |
Automatically. |
|
Oracle |
Is not supported. |
|
PostgreSQL |
Required ExtendedMetadata=True connection definition parameter. |
|
SQL Anywhere |
Automatically. |
|
SQLite |
Automatically. |
In some cases application needs to specify alternative DB table name, to which the updates will be posted. To do that set UpdateOptions.UpdateTableName to a required table name.
In some cases application needs to exclude columns from an updating SQL command. To do that exclude pfInUpdate from TField.ProviderFlags. And set TField.ReadOnly to prohibit field value modification.
To specify alternative DB column name, set TField.Origin to a required value.
The TADUpdateSQL component allows to:
In general, the TADUpdateSQL is a collection of SQL update commands, each of them handles specific task, like a inserting a new record into database. The TADUpdateSQL may be setup at design-time and / or at run-time.
To setup at design-time, drop TADUpdateSQL on a form. Set dataset UpdateObject property to point to this component. Then double click on TADUpdateSQL to invoke the AnyDAC Update SQL Editor:
The editor will automatically get the updating table name from the associated dataset and setup columns according to the dataset columns TField.ProviderFlags and TField.AutoGenerateValue.
To change setup, use:
The "Describe From DB" button provides setup for the specified table, by retrieving its metadata from the database. The "Revert To Defaults" button provides setup using the dataset field properties. The additional options may be specified on the "Options" page.
When setup is finished press the "Generate SQL" button to generate full set of the updating SQL's. Also, the editor may be used to manually edit update SQL commands on the "SQL Commands" page.
Press OK to store changes in the TADUpdateSQL.
To specify the TADUpdateSQL SQL commands at run-time, the application should use XxxxSQL properties. To reference in a SQL to a specific column value use the parameter markers:
These parameter values will be assigned automatically by AnyDAC. Do not assign a value to them, because their values will be overridden. AnyDAC will ignore parameters with other names.
The command parameters and command macros may be setup only at run-time. For that you have to use TADUpdateSQL.Commands property, which returns references to the TADCommand objects. For example:
ADUpdateSQL1.InsertSQL.Text := 'insert into &tab (id, name) values (:new_id, :new_name)'; ADUpdateSQL1.Commands[arInsert].Macros[0].AsRaw := 'Orders';
This event allows to completely override posting updates from a dataset. Also, you can combine TADUpdateSQL's and OnUpdateRecord approaches, to enable semi-automatic updates posting to different tables or databases.
For details see OnUpdateRecord event description and the AnyDAC demos:
|
What do you think about this topic? Send feedback!
|