Additional
AnyDAC for Delphi and Centralized Cached Updates
Posted by Support [Jul 16, 2012]
AnyDAC for Delphi v 6.0.1 introduces Centralized Cached Updates feature, enabling few datasets to track and apply the changes in chronological order accross all datasets, sharing the same changes journal. That gives new unique possibilities for master-detail handling.

Introduction

The classical task, when 2 datasets are in master-detail relation and working in cached updates mode. And the detail dataset is linked to an auto-increment column of the master dataset. Eg, "Order Details" table is linked with "Orders" table using "OrderID" column. And two scenarious:

 

  1. Insert a record into the master dataset. Insert few records into the detail dataset. When you apply inserted master record, it will get new OrderID column value. Now will be nice to propogate OrderID value to the new inserted detail dataset records, then apply detail records.
  2. Delete detail records, then delete master record. Note, you cannot just delete master record, if DB does not have a FOREIGN KEY with CASCADE DELETE option or a TRIGGER deleting corresponding detail records. Now you need first apply detail dataset changes, then master changes. And this scenario requires different ApplyUpdates orders from first one.

 

So, although it was technically possible to handle both scenarious, it was not simple with AnyDAC pre-6.0 version. With v 6.0 Centralized Cached Updates feature this is simple and automatic.

Centralized Cached Updates

AnyDAC TADSchemaAdapter AnyDAC had TADSchemaAdapter component right from v 1.0, but it was never documented and supported. Finally it is. TADSchemaAdapter is a centralized change log, which may be shared by few datasets. This component has ApplyUpdates method, which will apply updates of few datasets in the chronological order.

At first this feature is useful for the master-detail relations, as I showed above. For that range based M/D relation must be configured. I will show that on example "Orders" and "Order Details" tables:

  • Drop TADQuery on a form. Name it qOrders. Setup it - lets assign the SQL:

SELECT * FROM {id Orders}

  • Drop TDataSource on a form. Name it dsOrders. Set it DataSet to qOrders.
  • Drop TADQuery on a form. Name it qOrderDetails. Setup it - lets assign the SQL:

SELECT * FROM {id Order Details}

  • Then set MasterFields to ORDERID, IndexFieldNames to ORDERID and MasterSource to dsOrders. The base setup is finished.

 

To enable Centralized Cached Updates:

 

  • Drop TADSchemaAdapter on the form.
  • Set qOrders.SchemaAdapter and qOrderDetails.SchemaAdapter properties to the ADSchemaAdapter1.

 

Then to enable master field value propogation to the detail records and cascading deletion of the detail records:

 

  • Set qOrderDetails.FetchOptions.DetailCascade option to True.

 

Finally to apply changes you should use ADSchemaAdapter1.ApplyUpdates method.

Summary

Centralized Cached Updates feature was awaited by many AnyDAC customers. And this actually rounds our offer of the advanced cached updates mode and master-detail support.

 

More to read and see:

 

Add Comments