Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
CodeDelphiRAD Studio

Build Powerful Master-Detail Relationships With Centralized Cached Updates In Delphi

First, the CentralizedCachedUpdates sample sets up range-based master-detail relationships between datasets using two TFDQuery objects. Then, the sample shows you how to use a TFDSchemaAdapter object to enable the Centralized Cached Updates mode. To this end, the schema adapter object is used as a central change log shared by both the master and the detail datasets. Therefore, if you modify the records of both the master and the detail datasets in run time, you can apply changes for all records in the centralized change log to the database. To apply the changes, the sample implements the Apply updates button, which uses the ApplyUpdates method.

Location

You can find the CentralizedCachedUpdates sample project at:

  • Start | Programs | Embarcadero RAD Studio Sydney | Samples and then navigate to:
    • Object PascalDatabaseFireDACSamplesComp LayerTFDQueryCachedUpdatesCentralized
  • Subversion Repository:
    • You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
schemaadapter 2

How to Use the Sample

  1. Navigate to the location given above and open Qry_SchemaAdapter.dproj.
  2. Press F9 or choose Run > Run.
  3. Click on the Use Connection Definition combo box and select an option.
    • Modify the records of the master or the detailed datasets.
    • Click ont the Apply updates button.

Files

File in DelphiContains
Qry_SchemaAdapter.dproj
Qry_SchemaAdapter.dpr
The project itself.
fSchemaAdapter.pas
fSchemaAdapter.fmx
The main form.

Implementation

To set up a range-based master-detail relationship and a central change log, the following components have to be configured at design time using the Object Inspector as follows:

  • Two TFDQuery objects named qMaster and qDetail. These components are used to implement a dataset capable of executing SQL queries. The following setup is needed:
  • Configure the Connection property of both components to specify the FireDAC connection object that is used to connect to a DBMS.

In order to set up a master-detail relationship, the SQL properties of both components have to be set up as follows:

  • The SQL property of qMaster is set to select * from {id FDQA_master_autoinc} in order to select the master records.
  • The SQL property of qDetail is set to select * from {id FDQA_details_autoinc} in order to select the detail records.

Finally, both objects are linked in a range-based master-detail relationship, where qMaster is the master dataset and qDetail is the detail dataset. To this end, you have to set up the following properties of qDetail:

  • MasterFields is set to id1.
  • IndexFieldNames is set to fk_id1.
  • MasterSource is set to dsMaster.
  • Two TDataSource objects named dsMaster and dsDetail. These components provide an interface between a dataset component and data-aware controls on a form. In this sample, it is used to provide communication between the datasets and the grids where the datasets are displayed. To this end, the following properties are set up at design time using the Object Inspector:
  • The DataSet property of dsMaster is set to qMaster and the DataSource property of DBGrid1 is set to dsMaster.
  • The DataSet property of dsDetail is set to qDetail and the DataSource property of DBGrid2 is set to dsDetail.
  • TFDSchemaAdapter object named FDSchemaAdapter1. This object enables the Centralized Cached Updates mode. The schema adapter object is used as a central change log shared by both the master and the detail datasets. To enable propagation of master dataset changes to a detail dataset, the FetchOptions.DetailCascade property of the detail dataset is set to True. Moreover, both master and detail datasets must point to the same TFDSchemaAdapter object. Therefore, the sample sets the SchemaAdapter property of qMaster and qDetail to FDSchemaAdapter1.

For more details you can visit the original post of Embarcadero:

http://docwiki.embarcadero.com/CodeExamples/Sydney/en/FireDAC.TFDQuery.CentralizedCachedUpdates_Sample

Check out the full source code for the cached updates sample over on Embarcadero’s GitHub.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES