Data Mappings

Contents

Introduction
Standard Data Mappings
Unsigned Values

Introduction

Strictly speaking this document covers the DatabaseInterface and DatabaseMaintenance and has been included here mostly as a technical reference. Mapping data types is quite complex. Data can exist in 4 domains and its type in each is declared as follows:- It can be transported between these domains:- In an attempt to simplify the mapping yet another representation is introduced: the DbiFieldType which is used to map between the other types.

Conversions are handled DbiFieldType which can be created from:-

1)  DbiFieldType(Int_t type = Dbi::kInt);

    Where:-

        type corresponds to ROOT's types (Dbi:: DataTypes)
  
2)  DbiFieldType(Int_t type,
                 Int_t size,
                 const char* typeName);

    Where:-

       type           value from TSQLColumnInfo::GetSQLType()
       size           value from TSQLColumnInfo::GetLength()
       dbType         value from TSQLColumnInfo::GetTypeName()

3)  DbiFieldType(const string& sql precision);


    Where:-

        sql          A MySQL type as a string.

It can be used to generate:-

1)  string AsString() const;

    The C++ name, but with first letter capitalised and extended to
    include TString and Date

2)  string AsSQLString(Dbi::DbTypes dbType = Dbi::kMySQL) const;

    The MySQL type as a string.
The various mapping are list below.

Standard Mappings

---- DbiFieldType  -----        ----  Root   ----    TSQL    ----  MySQL  ----  
Type    Concept      Size       Type     DataTypes   type    type    len        

Bool      Bool          1       Bool          1       

Tiny      Int           1       Tiny          4       3      TINYINT     4       
UTiny     Int           1       UTiny         5       
Short     Int           2       Short         6       3      SMALLINT    6        
UShort    UInt          2       UShort        7       
Int       Int           4       Int           8       3      INT        11        
UInt      UInt          4       UInt          9       
Long      Int           8       Long         10       3      BIGINT     20        
ULong     UInt          8       ULong        11         
Float     Float         4       Float        12       4      FLOAT      12        
Double    Float         8       Double       13       5      DOUBLE     22        

Char      Char          1       Char          2       1      CHAR        1        
UChar     UChar         1       UChar         3       
String    String      255                             2      TINTTEXT   -1        
String    String    65535       String       14       2      TEXT       -1        
TString   String    65535       TString      15       

Date      Date         19       Date         16       8      DATETIME   19        
Unknown   Unknown       0       Unknown       0      -1

Char      Char          i                             1      CHAR(i)     i        
String    String        i                             2      VARCHAR(i)  i       

Notes

Unsigned Values

Having unsigned integer data is a good coding practice when the value in question cannot have negative values and becomes essential if the full capacity of the variable is to be used. MySQL supports unsigned values but, in a failed attempt to use ORACLE and an obsolete back-end (RDBC) that did not support them, we decided to ban its use in the database. Conversion between unsigned in the application and signed in the database is handled as follows.
  1. Reading table row from the Database
    For unsigned integers the DbiResultSet operator >> methods store the number first in a signed value, to ensure that reading for GetString does not fail (using istringstream to write a character representation of a negative number to an unsigned integer is an error) and then, if necessary, mask off any leading sign bit extension (for example when reading from a TINYINT into a short).

  2. Writing table row to the the Database
    All data is first stored as SQL commands in a DbiSqlValPacket which is then executed directly by the client . So the data must be written as signed.

    To achieve this the DbiOutRowStream operator << methods for unsigned integers first converts the value to signed (using a cast to preserve the bits) and then tests to see if the sign bit (whose position is determined by the capacity of the receiving column is set, and if so extends the sign bit.

    Historical note: the code still has partial support for unsigned database types, which explains why it still tests to see that it is signed before doing the conversion.

  3. Transmitting between Databases
    This time the DbiSqlValPacket is built by the exporting database directly from TSQLStatement::GetString data so will always be returned as signed integers. After transmission to the importing database the DbiSqlValPacket is reconstructed from the text file resulting exactly the same SQL as if writing directly from the application (see the previous section).

Return to the top-level Database Distribution document
Last Modified: $Date: 2007/05/01 06:47:05 $