FireDAC
ContentsIndexHome
PreviousUpNext
Data Type Mapping

FireDAC offers flexible adjustable data type mapping system, allowing to simplify migration to FireDAC or optimize data representation.

Group
Links
General

The data type mapping allows to map:

  • result set column data types returned by an FireDAC driver, to the data types preferred by application;
  • command parameters data types defined by an application, to the driver supported data types.

The data type mapping is useful for:

  • creation of the data type schema compatible with other data access components, when migrating application from these components to the FireDAC;
  • creation of the data type schema compatible across few supported DBMS, when developing a cross-DBMS application;
  • mapping the data types not supported by an application into the supported ones;
  • mapping the generalized data types supported by an driver into the more specialized / convenient ones;
  • optimizing the memory consumption, by specifying the more optimal data types.

Lets consider SELECT of numeric column from an Oracle table and how it may be mapped:

DDL 
Driver data type 
Preferred data type 
NUMBER(2,0) 
dtBcd, Precision=2, Scale=0 
dtSByte 
NUMBER(4,0) 
dtBcd, Precision=4, Scale=0 
dtInt16 
NUMBER(8,0) 
dtBcd, Precision=8, Scale=0 
dtInt32 
NUMBER(18,4) 
dtBcd, Precision=18, Scale=4 
dtCurrency 

There you see, that Oracle driver returns some unified data type (dtBcd / dtFmtBCD) for all possible NUMBER(X,Y) database types. But an application may prefer to use a more specialized / convenient data type, like a dtInt32. 

 

Defining

FireDAC applies the mapping rules at a command preparation. After the command is prepared, the rule changes will have no effect. If data type conforms to few rules, then only first one will be used. MaxStringSize, MaxBcdPrecision, MaxBcdScale properties are applied to source data type before mapping rules. 

To define the data type mapping an application must set FormatOptions.OwnMapRules to True and fill MapRules collection. Each item in collection is of TADMapRule class and represents a single mapping rule. In case of a result set column, each rule defines a transformation of a source data type, returned by a driver, into a target one, preferred by an application. In case of a command parameter, rule defines a transformation of a target data type, specified by an application, into a source data type, supported by a driver. All rules, excluding the name based ones work bidirectionally for both cases. 

Each rule is defined by the TADMapRule properties:

Properties 
Description 
Defines the range of source data type numeric precision. 
Defines the range of source data type numeric scale. 
Defines the range of source data type string length. 
Source data type. 
Target data type. 
Column name mask. 

If a precision, scale or size is not used by the rule, then its value must be -1 (default value). If a source data type conforms to some rule, then a column data type will be defined using corresponding TargetDataType.

To define mapping rules for sample above, use code:

with ADConnection1.FormatOptions do begin
  OwnMapRules := True;
  with MapRules.Add do begin
    ScaleMin := 0;
    ScaleMax := 0;
    PrecMin := 0;
    PrecMax := 2;
    SourceDataType := dtBcd;
    TargetDataType := dtSByte;
  end;
  with MapRules.Add do begin
    ScaleMin := 0;
    ScaleMax := 0;
    PrecMin := 3;
    PrecMax := 4;
    SourceDataType := dtBcd;
    TargetDataType := dtInt16;
  end;
  with MapRules.Add do begin
    ScaleMin := 0;
    ScaleMax := 0;
    PrecMin := 5;
    PrecMax := 8;
    SourceDataType := dtBcd;
    TargetDataType := dtInt32;
  end;
  with MapRules.Add do begin
    ScaleMin := 4;
    ScaleMax := 4;
    PrecMin := 18;
    PrecMax := 18;
    SourceDataType := dtBcd;
    TargetDataType := dtCurrency;
  end;
end;
What do you think about this topic? Send feedback!