Type Conversions

The bulk of programming with IBPP to interact with a Firebird database is to exchange values with the database, reading columns values and inserting or updating columns values. SQL has a number of basic types and C++ has its own set of primitive types. This article discusses relationships between both worlds, in the context of IBPP.

Mappings

CHAR and VARCHAR

Reading or writing a CHAR or VARCHAR column is usually done using a std::string variable. There are no numeric to string or string to numeric implicit conversions.

There is an exception to this rule, regarding the C++ bool. You can store and read a bool to/from a CHAR or VARCHAR. Here is the mapping that will happen in such case.

  • Writing a bool to a CHAR : a leading 'T' or 'F' (true or false) will be written, other characters set to ' ' (space)
  • Writing a bool to a VARCHAR : a leading 'T' or 'F' will be written and the VARCHAR content length set to 1 character
  • Reading a bool from a CHAR : if the leading character is any of 't', 'T', 'y', 'Y', or '1', store a true, else a false
  • Reading a bool from a VARCHAR : if the leading character is any of 't', 'T', 'y', 'Y', or '1', store a true, else a false

SMALLINT, INTEGER, BIGINT

Any of these 3 integer sizes (16, 32 and 64 bits) can be mixed with any integer size on the C++ side. IBPP will make the appropriate conversions, and will only throw an exception if an overflow occurs. For instance, storing a 64 bits column to a int16_t is valid, as long as at runtime the value transferred does not overflow the capacity of the int16_t.

C++ bool can also be used with integer SQL columns on the basis that a 0 means false and anything else means true (when storing, IBPP always writes 1 for a true, but it is relaxed on reading).

NUMERIC, DECIMAL

Those exact precision SQL types are actually stored in integer quantities, taking a scaling factor into account, which is declared in the metadata. IBPP permits to handle these SQL types with C++ float or double, or integer types. When exchanging these values with integer types, one must understand that the actual scaled integer value is exchanged. For instance, NUMERIC(9,2) stores values as integers by multiplying them by a scaling factor of 100 (FB stores 1234 when you intend 12.34).

When exchanging such a column with a float or a double, IBPP applies the scaling factor and returns or expects 12.34 in the float or double variable.

When exchanging with an integer type, IBPP returns or expects the value as is, that is the integer value 1234 in this example.

FLOAT

This floating point SQL type requires a C++ float variable.

DOUBLE

This floating point SQL type requires a C++ double variable.

TIMESTAMP

Maps to an IBPP::Timestamp type.

DATE

Maps to an IBPP::Date type.

TIME

Maps to an IBPP::Time type.

BLOB

Maps to an IBPP::Blob type.

ARRAY

Maps to an IBPP::Array type.

reference/conversions.txt · Last modified: 2006/03/30 18:12 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki