AnyDAC
ContentsIndexHome
PreviousUpNext
Overriding Posting Updates

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.

Group
Links
General

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. 
  • Exclude non-base table column from update commands, by excluding pfInUpdate from TField.ProviderFlags and setting TField.ReadOnly to True;
  • Set UpdateOptions.UpdateNonBaseFields to False;
 
SELECT with grouping operations 
Updates more than one record. 
  • Redirect updates to a correct table by setting UpdateOptions.UpdateTableName;
  • Disable updated record count checking, by setting UpdateOptions.CountUpdatedRecords to False;
  • Use custom SQL commands.
 
SELECT with common table expressions 
Fails to get updating table name. 
  • Redirect updates to a correct table by setting UpdateOptions.UpdateTableName;
  • Use custom SQL commands.
 
SELECT with aliased columns and expressions 
Includes incorrect column names. 
  • Optionally, exclude non-base table column from update commands, by excluding pfInUpdate from TField.ProviderFlags and setting TField.ReadOnly to True.
  • Optionally, specify base column name, by specifying TField.Origin.
 
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. 

 

Specifying update table name

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. 

 

Specifying update column names and modes

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. 

 

Using TADUpdateSQL

The TADUpdateSQL component allows to:

  • selectively override the update SQL commands generated by AnyDAC;
  • enable updates posting, when AnyDAC cannot generate the update commands, like a complex SELECT's or stored procedure calls.

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. 

 

Setting at design 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 "Table Name" combo to specify updating table name;
  • the "Key Fields" list box to specify unique identifying columns (corresponds to pfInKey in ProviderFlags);
  • the "Updating Fields" list box to specify columns to include into update (corresponds to pfInUpdate in ProviderFlags);
  • the "Refreshing Fields" list box to specify columns those values must be refreshed after posting update (corresponds to AutoGenerateValue <> arNone).

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

 

Settings at run time

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:

  • new column value - ':NEW_<column name>';
  • old column value - ':OLD_<column name>';
  • current column value - ':<column name>'.

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';

 

Using OnUpdateRecord

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:

  • AnyDAC\Samples\Comp Layer\TADQuery\CachedUpdates\OnUpdateRecord
  • AnyDAC\Samples\Comp Layer\TADUpdateSQL\Main
What do you think about this topic? Send feedback!