Articles Re-introduction to FireDAC data access (5) by Embarcadero Team Japan

emailx45

Местный
Регистрация
5 Май 2008
Сообщения
3,571
Реакции
2,438
Credits
573
Re-introduction to FireDAC data access (5)
Embarcadero Team Japan - 07/Jul/2020
[SHOWTOGROUPS=4,20,22]
Utilization of in-memory dataset
In this blog, we will explain the basic usage of FireDAC for those who have used Delphi / C++Builder to some extent.

The fifth theme
  • In-memory dataset overview
  • Precautions when using TClientDataSet in an older version project
  • Application creation exercise for migrating TClientDataSet data to TFDMemTable
By the 4th session , I think you have learned the basic usage of FireDAC. This time, let's move on to some more advanced content. The theme this time is "In-memory data set".

An in-memory dataset is a database-independent dataset that is expanded in memory. The FireDAC dataset was introduced in Part 3 . Datasets such as TFDQuery and TFDTable were supposed to handle the data associated with the database information connected by TFDConnection, but in-memory datasets are held in memory without depending on the database.

In-memory datasets can be used to efficiently implement cached data retention and temporary data retention that does not require persistence. This is one of the powerful features of FireDAC.

In this article, I will explain the outline of in-memory datasets and the points to be noted when migrating a project that uses TClientDataSet with an older version of Delphi / C++Builder.

In-memory dataset overview
The in-memory data set is a data set that is expanded on the memory of the client PC and is provided as a component called "TFDMemTable" in FireDAC, and has the following features.

Retain the data in the memory
data of TFDMemTable is capable of high-speed data operation because it can all handle on the memory. However, since all the data is retained in memory, it will be lost when closed, unlike a normal data set. In addition, TFDMemTable has an import/export function for XML/BIN/JSON format files, and data can be stored and read in these formats.

Database-independent
datasets such as TFDQuery and TFDTable always required a database connection, but TFDMemTable does not require a database connection. It can be treated as a local dataset that is database independent.

Similar functionality to TClientDataSet
Delphi/C++Builder provided an in-memory dataset named "TClientDataSet" from a very old version. TFDMemTable has similar functionality to this TClientDataSet, so it can be used in a similar way.

The advantage of using TFDMemTable is that it behaves like a local DB.

Depending on your business, you may not be able to interrupt the operation of inputting/outputting data to/from the system even if you cannot connect to the network. In such a case, it is necessary to prepare a mechanism that allows the operation to continue in some way.

For example, if you get the necessary data from a remote data center with a network connection, you can temporarily store it in a local dataset (TFDMemTable) for caching purposes. Even if the network connection is lost, if you have a mechanism that synchronizes with the data on the server later, you will be able to carry out the work without stopping the operation.

Since FireDAC also supports data access to local DB such as SQLite and IBLite, there is an option to cache to local DB instead of local dataset.

While not suitable for systems that require real-time data synchronization, leveraging local datasets can reduce network traffic by avoiding unnecessary remote DB access. In addition, since the data is manipulated on the memory of the client, it has the advantage of improving the responsiveness of processing.

Precautions when using TClientDataSet in an older version project
Next, let's explain the points to be noted when migrating an existing project that uses TClientDataSet in an older version of Delphi/C++Builder.

For example, when replacing a DB component such as BDE or dbExpress with FireDAC, it has been reported that the behavior may be different from the conventional behavior in the case of continuing to use the combination of FireDAC data set and TClientDataSet-TDataSetProvider.

When migrating to FireDAC, you should use the alternative, TFDMemTable, built on the same new architecture, rather than the old architecture, TClientDataSet and TDataSetProvider.


Below are some guidelines for replacing FireDAC components.

FireDAC doesn't require
TClientDataSet and TDataSetProvider DB components like dbGO and dbExpress only support unidirectional datasets, so I had to use TCientDataSet and TDataSetProvider together to display the data in a grid control like TDBGrid. .. FireDAC datasets are not needed as they can be displayed in TDBGrid without using TClientDataSet and TDataSetProvider. This is also the case when using BDE.

Use of
TFDMemTable As TFDMemTable has the similar function to TClientDataSet as described above, it can be used like TClientDataSet. If you want to use it for local data set, please replace it with TFDMemTable.

[/SHOWTOGROUPS]
 

emailx45

Местный
Регистрация
5 Май 2008
Сообщения
3,571
Реакции
2,438
Credits
573
[SHOWTOGROUPS=4,20,22]
Application creation exercise for migrating TClientDataSet data to TFDMemTable
In this section, we will practice the application creation to actually migrate the data of TClientDataSet to TFDMemTable.

The practice procedure is as follows.
  1. Load sample data (XML file) with TClientDataSet
  2. Copy the data of TClientDataSet to TFDMemTable and save it as a JSON file
  3. Read the JSON file saved in 2. with TFDMemTable and display the data
For this exercise, we will not use a data module to keep the procedure simpler.

(1) Creating a VCL Form Application project

From the Delphi/C++Builder menu, select [File]-[New]-[Windows VCL Forms Application].

(2) Save the project

Select [File]-[Save All] from the menu to save all files. The project can be saved in any folder.

(3) Place components on the form

From the Data Access category of the tool palette
・TClientDataSet

From the [FireDAC] category of the tool palette
・TFDMemTable

From the [FireDAC Links] category of the tool palette
・TFDStanStorageJSONLink

WARNING:

-TFDStanStorageXXXLink is a component that supports streaming with FireDAC.
It can handle three types of data formats: binary format/JSON format/XML format, and XXX of TFDStanStorageXXXLink contains Bin/JSON/XML according to the selected data format.
This time, we will handle JSON format files, so please place the TFDStanStorageJSONLink component.

From the Data Access category of the tool palette

・TDataSource
From the Data Controls category of the tool palette

・TDBGrid
From the [Standard] category of the tool palette

・TButton
Two

Place it at any position on the form (see the placement example in the figure below).

7701.image2.png



(4) Change each property

Change the following properties of each component from the screen of the Object Inspector.

DBGrid1
Property namevalue
DataSourceDataSource1
Button1
Property namevalue
CaptionSave ClientDataSet data
Button2
Property namevalue
CaptionRead data in FDMemTable


(5) Implement the process of saving ClientDataSet data

In this exercise, you will use the sample data file to load the ClientDataSet data.

The sample data file is located at the following path.

C:\Users\Public\Documents\Embarcadero\Studio\xxx\Samples\Data

The value corresponding to the version of Delphi/C++Builder is entered in xxx.

(For example, 20.3 for 10.3 and 21.0 for 10.4)

Please note that if you do not install the sample code when you choose to install Delphi/C++Builder, the sample data file will not be placed either.


When you double-click Button1 on the design surface, the OnClick event handler for Button1 is generated. Add a process to open the dataset in that event handler.

Delphi:
procedure TForm1.Button1Click(Sender: TObject);
const
// 10.4 sample data path
Data='C:\Users\Public\Documents\Embarcadero\Studio\21.0\Samples\Data\';
begin
// this time as an example
Read employee.xml in Data folder ClientDataSet1.LoadFromFile(Data+'employee.xml');

// Copy
ClientDataSet data to FDMemTable FDMemTable1.CopyDataSet(ClientDataSet1,[coStructure,coRestart, coAppend]);

//FDMemTable data As a JSON file
FDMemtable1.SaveToFile(Data+'employee.json',sfJSON);
end;

C++Builder:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
/ 10.4 sample data path
const UnicodeString
Data=L"C:\\Users\\Public\\Documents\\Embarcadero\\Studio\\21.0\\Samples\ \Data\\";

// This time, as an example, load employee.xml in the Data folder
ClientDataSet1->LoadFromFile(Data+L"employee.xml");

// Copy the data of ClientDataSet to
FDMemTable FDMemTable1->CopyDataSet( ClientDataSet1,TFDCopyDataSetOptions() << coStructure << coRestart << coAppend);

// Save
FDMemTable data as a JSON file FDMemTable1->SaveToFile(Data+L"employee.json",TFDStorageFormat::sfJSON);
}

Use the LoadFromFile and SaveToFile methods to import/export the dataset.
The file output by ClientDataSet cannot be directly read by FDMemTable. The reverse is also true.
ClientDataSet and FDMemTable are not compatible with each other's internal data format.


As with the data format of the file, the Dataset class is not compatible, so you cannot directly assign the Data property as shown below.
ClientDataSet1.Data := FDMemTable1.Data;

Use the CopyDataSet method to copy the ClientDataSet data to FDMemTable.

[/SHOWTOGROUPS]
 

emailx45

Местный
Регистрация
5 Май 2008
Сообщения
3,571
Реакции
2,438
Credits
573
[SHOWTOGROUPS=4,20,22]

(6) Implement the process of reading data with FDMemTable

When you double-click Button2 on the design surface, the OnClick event handler for Button2 is generated. Add a process to open the dataset in that event handler.

Delphi:
procedure TForm1.Button2Click(Sender: TObject);
const
// 10.4 sample data path
Data='C:\Users\Public\Documents\Embarcadero\Studio\21.0\Samples\Data\';
begin
DataSource1.DataSet:=FDMemTable1 ;

// Load the JSON file data into FDMemTable
FDMemTable1.LoadFromFile(Data+'employee.json',sfJSON);

end;

C++Builder:
void __fastcall TForm1::Button2Click(TObject *Sender)
{
/ 10.4 sample data path
const UnicodeString Data=L"C:\\Users\\Public\\Documents\\Embarcadero\\Studio\\21.0\\Samples\ \Data\\";

DataSource1->DataSet=FDMemTable1;

// Load JSON file data into FDMemTable
FDMemTable1->LoadFromFile(Data+L"employee.json",TFDStorageFormat::sfJSON);

}

(7) Save the project

Select [File]-[Save All] from the menu to save all files.

(8) Run the application

2335.image11.png



Press the execute button on the toolbar (above figure) or the [F9] button on the keyboard.

(9) Verify application operation

Run the application and press the [Save Data in ClientDataSet] button. If no error occurs, the file employee.json will be created in the Data folder of the sample program.

If the file has been created, press the [Load data in FDMemTable] button. When the json file data is read normally, the data is displayed in DBGrid as shown below.

1588.image3.png



In this exercise, data was input and output via files to make it as simple as possible, but you can also connect to an actual database and acquire or update data. If you are using TClientDataSet in a project created in an older version, please consider replacing it with TFDMemTable.

Next time, I'll explain FireDAC performance tuning tips.


[/SHOWTOGROUPS]