Migrating from DBExpress to FireDAC


In my last blog I explained how to migrate from IBExpress to FireDAC. Here I will show you how to migrate a client-server application that using DBExpress data access components, such as TSqlConnectionTSqlTable, TSqlQuery, TSqlStoredProc to the FireDAC. It shows the basic principles of replacing the common components, properties and code, preserving the developers working time and avoiding the common migration pitfalls.

So lets start in steps.

1. Units Changed
We need to replace used DBExpress units to FireDAC units.
Find 
FMTBcd, SqlExpr
Replace
          FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf,
FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Phys, FireDAC.Stan.Pool,
FireDAC.Stan.Async, FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf,
FireDAC.DApt, FireDAC.Comp.Client, FireDAC.Comp.DataSet

2. DBExpress components classes compatible with FireDAC
Search DBEXPRESS classes and replace with FireDAC classes

 DBEXPRESS
 FireDAC
TSQLConnection
TFDConnection
TSQLTable
TFDTable
TSQLQuery
TFDQuery
TSQLStoredProc
TFDStoredproc
TSQLDataset
TFDCommand
TSimpleDataset
TFDMemTable

3. Other Types classes changed
Search DBEXPRESS classes and replace with FireDAC classes

 DBEXPRESS
 FireDAC
TParams
TFDParams
TBlobStream
TFDBlobStream
TBDEDataSet
TFDRDBMSDataSet

4. TSQLConnection -> TFDConnection Properties
Search DBEXPRESS classes and replace with FireDAC classes

 DBEXPRESS
 FireDAC
Driver
DriverName
KeepConnection
ResourceOptions.KeepConnection
TableScope
Not Supported so remove.

Note* - OnLogin Event
TSQLConnection.OnLogin event is different than TFDConnection.OnLogin. So we should remove Tdatabase event and re-write TFDConnection Event.

5. TSQLTable -> TFDTable Properties
Search DBEXPRESS classes and replace with FireDAC classes

 DBEXPRESS
 FireDAC
SQLConnection
Connection
GetMetadata
Not Supported so remove.
MaxBlobSize
Not Supported so remove.
NumericMapping
Not Supported so remove.

6. TSQLQuery -> TFDQuery Properties
Search DBEXPRESS classes and replace with FireDAC classes

 DBEXPRESS
 FireDAC
SQLConnection
Connection
Datasource
MasterSource

Note. Please check MasterFields

GetMetadata
Not Supported so remove.
MaxBlobSize
Not Supported so remove.
NumericMapping
Not Supported so remove.
Datasource
MasterSource

Note. Please check MasterFields

ParamCheck
ResourceOptions.Paramcreate
SchemaName
Not Supported so remove.

7. TSQLStoredProc -> TFDStoredProc     Properties
Search DBEXPRESS classes and replace with FireDAC classes

 DBEXPRESS
 FireDAC
SQLConnection
Connection
GetMetadata
Not Supported so remove.
MaxBlobSize
Not Supported so remove.
NumericMapping
Not Supported so remove.
Datasource
MasterSource

Note. Please check MasterFields

ParamCheck
ResourceOptions.ParamCreate
SchemaName
Not Supported so remove.


8. TSQLDataset -> TFDCommand   Properties
Search DBEXPRESS classes and replace with FireDAC classes

 DBEXPRESS
 FireDAC
SQLConnection
Connection
CommandType
CommandKind
Datasource
Not supported so remove
DBXCommandtype
Not supported so remove
MaxBlobSize
Not Supported so remove.
NumericMapping
Not Supported so remove.
ParamCheck
ResourceOptions.Paramcreate
SortFieldNames
Not Supported so remove.

9. TSimpleDataset -> TFDMemTable        Properties
Search DBEXPRESS classes and replace with FireDAC classes

 DBEXPRESS
 FireDAC
Connection
Adapter
Dataset
Not supported so remove
FileName
Not supported so remove
Params
Not supported so remove
Readonly
UpdateOptions.Readonly

10. Sorting
FireDAC components like TFDQuery, TFDTable does not have Sort property. So we have to use IndexFieldNames property for sorting. So where ever we have used Sort for sorting in our project we need to replace with IndexFieldNames like follow.

ADQuery1.IndexFieldNames := 'ORDERID';
or
ADQuery1.IndexFieldNames := 'OrderDate:D;Price'; //D for descending//

11. TBookMark Changes for Dataset components
When we save a bookmark for future use we must use pointer for

var
BookMark : Pointer;
CDS : TClientDataset
……
// Required so your assignment doesn't attempt to free a non-existent bookmark //
 Bookmark := nil;
 TBookmark(Bookmark) := CDS.GetBookmark;

12. Data Property

If we use TFDQuery and ClientDataset together then Data property is mostly used to assign saved data to any other dataset component directly instead of using any loop. Here TFDQuery have also Data property but type is different. Earlier in TADOQuery or other Query component, Data property was OleVariant type which was same type like ClientDataset but now it is IFDDataSetReference. So we cannot just assign directly like.

Clientdataset1.Data := FDQuery.Data; //will result an error //

Above code will compile successfully but will result “Invalid data packet” error at runtime. In this scenario we have to use TDatasetProvider as mediator to transfer data from once dataset to another. We can use DatasetProvider.Data property like follow.


DataSetProvider1.Dataset := FDQuery1;
FdQuery1.Open;
Clientdataset1.data := DatasetProvider1.data;

14. Extra points to check
Need to change some numeric fields to TBCDFields when it will show error and set Size=2 when in Database field type is like number(15,2)

Comments

Popular posts from this blog

ShellExecute in Delphi

How to send Email in Delphi?

Drawing Shapes in Delphi