BDE Application Migration (FireDAC)
Go Up to Migrating BDE Applications to FireDAC
This example shows you step by step how the classic Embarcadero Delphi demo application - MastApp - will be migrated to FireDAC and InterBase DBMS.
Step 1
Go to the C:\Users\Public\Documents\Embarcadero\Studio\23.0\Samples\Object Pascal\Database\FireDAC\Tool\reFind\BDE2FDMigration\Demo
folder. It contains the copy of the MastApp application sources. Create a new subdirectory, FireDAC_MastApp. Then copy the MastApp source files to the newly created one to preserve the original sources.
Step 2
Open a cmd window and change to the FireDAC_MastApp directory, or use your preferred directory manager, for example, FAR. Run the RAD Studio tool reFind in order to replace the BDE terms with their FireDAC counterparts:
C:\Program Files (x86)\Embarcadero\Studio\23.0\bin\reFind.exe *.pas *.dfm /X:C:\Users\Public\Documents\Embarcadero\Studio\23.0\Samples\Object Pascal\Database\FireDAC\Tool\reFind\BDE2FDMigration\FireDAC_Migrate_BDE.txt.
Step 3
Create a FireDAC connection definition using the FireDAC Explorer. Then setup the connection definition parameters. This is the simplest connection definition for MastApp:
[MASTSQL]
DriverID=IB
Protocol=TCPIP
Server=127.0.0.1
DataBase=C:\MastApp.GDB
User_Name=sysdba
SQLDialect=3
CharacterSet=UTF8
ExtendedMetadata=True
Note: This implies that you have created an InterBase C:\MastApp.GDB database and loaded a DBDEMOS database (Paradox) into the C:\MastApp.GDB database. For that you can use the Clever Components InterBase DataPump.
Step 4
Add a DBMS-specific FireDAC driver to the application. Add one of the FireDAC.Phys.[DBMS driver ID] units to your project. For example, if you use InterBase, add FireDAC.Phys.IB to the project. Then add the FireDAC.VCLUI.Wait unit to your project:
program Mastapp;
uses
Forms,
FireDAC.Phys.IB,
FireDAC.VCLUI.Wait,
MAIN in 'MAIN.PAS' {MainForm},
// …
Step 5
Now open the DataMod.dfm file using a text editor. Set up the TFDConnection component in the data module. Set ConnectionDefName to an InterBase connection definition. Also change the user name and password to the correct ones. The result will be something like this:
Params.Strings = (
'ConnectionDef=MASTSQL'
'User_Name=sysdba'
'Password=masterkey')
Step 6
The InterBase SQLLink and FireDAC InterBase driver have different data type mappings, so we need to adjust that to make FireDAC type mapping compatible with BDE. Otherwise, we will need to recreate all the persistent fields. To set up data mapping, we should fill up the collection property TFDConnection.FormatOptions.MapRules. Lets do it in DataMod.DFM:
object Database: TFDConnection
// …
FormatOptions.OwnMapRules = True
FormatOptions.MapRules = <
item
PrecMax = 10
PrecMin = 0
ScaleMax = 0
ScaleMin = 0
SourceDataType = dtFmtBCD
TargetDataType = dtInt32
end
item
SourceDataType = dtFmtBCD
TargetDataType = dtDouble
end
item
SourceDataType = dtDateTimeStamp
TargetDataType = dtDateTime
end>
// …
end
Additionally, set the FormatOptions.StrsTrim property to False, as the default value for this property is incompatible with BDE.
Step 7
FireDAC does not support desktop DBs, like Paradox or Dbase. So, we have to remove all desktop DB (Paradox, Dbase) code from the application:
- The TMastData.UseLocalData method in DataMod.pas.
- The TMainForm.ViewLocalClick method in Main.pas.
Step 8
Now we should adjust the application source code to InterBase:
- In the TMainForm.ViewRemoteClick method in Main.pas, replace string ' (Local Interbase)' with ' (InterBase)'.
- Remove the TMainForm.ViewMenuClick handler in Main.pas.
- Remove the TMastData.DataDirectory method in DataMod.pas.
- The application creates a DB connection definition on the fly if it does not exist. So, change the TMastData.UseRemoteData in the DataMod.pas to:
procedure TMastData.UseRemoteData;
var
Params: TStringList;
begin
{ See if the ConnectionDef exists. If not, add it. }
if not FDManager.IsConnectionDef('MASTSQL') then
begin
Params := TStringList.create;
try
Params.Values['Protocol'] := 'TCPIP';
Params.Values['Server'] := '127.0.0.1';
Params.Values['DataBase'] := 'C:\MastApp.GDB';
Params.Values['User_Name'] := 'sysdba';
Params.Values['SQLDialect'] := '3';
Params.Values['CharacterSet'] := 'UTF8';
Params.Values['ExtendedMetadata'] := 'True';
FDManager.AddConnectionDef('MASTSQL', 'IB', Params);
finally
Params.Free;
end;
end;
SetDatabaseConnectionDef('MASTSQL');
end;
- We have to adjust the SQL commands used by the applications. In DataMod, the CustByLastInvQuery query has DESCENDING as key word, but InterBase uses DESC.