From 213740c161499a5961fa426d1b77c7d68763c5df Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 13 Dec 2011 09:52:44 +0200 Subject: Initial value traits implementations --- odb/mssql/makefile | 1 + odb/mssql/mssql-fwd.hxx | 60 ++++++ odb/mssql/mssql-types.hxx | 11 +- odb/mssql/statement.cxx | 74 ++++++- odb/mssql/traits.cxx | 15 ++ odb/mssql/traits.hxx | 498 ++++++++++++++++++++++++++++++++++++++++++++++ odb/mssql/traits.txx | 11 + 7 files changed, 661 insertions(+), 9 deletions(-) create mode 100644 odb/mssql/traits.cxx create mode 100644 odb/mssql/traits.hxx create mode 100644 odb/mssql/traits.txx diff --git a/odb/mssql/makefile b/odb/mssql/makefile index acdeb76..6e054d5 100644 --- a/odb/mssql/makefile +++ b/odb/mssql/makefile @@ -17,6 +17,7 @@ object-statements.cxx \ statement.cxx \ statements-base.cxx \ tracer.cxx \ +traits.cxx \ transaction.cxx \ transaction-impl.cxx diff --git a/odb/mssql/mssql-fwd.hxx b/odb/mssql/mssql-fwd.hxx index 3579403..20acab3 100644 --- a/odb/mssql/mssql-fwd.hxx +++ b/odb/mssql/mssql-fwd.hxx @@ -93,6 +93,66 @@ typedef SQLHANDLE SQLHDESC; # define SQL_HANDLE_DESC 4 #endif +// The following types are our own equivalents of ODBC and Native Client +// ODBC driver types. They are all PODs and should be layout-compatible +// with the original types, which means they can be used interchangeably. +// +namespace odb +{ + namespace mssql + { + // UCS-2 character type (SQLWCHAR). + // +#ifdef _WIN32 + typedef wchar_t ucs2_char; +#else + typedef unsigned short ucs2_char; +#endif + + // SQL_NUMERIC_STRUCT + // +#ifndef SQL_MAX_NUMERIC_LEN +#define SQL_MAX_NUMERIC_LEN 16 +#else +# if SQL_MAX_NUMERIC_LEN != 16 +# error unexpected SQL_NUMERIC_STRUCT value +# endif +#endif + + struct decimal + { + unsigned char precision; + signed char scale; + unsigned char sign; // 1 - positive, 0 - negative + unsigned char val[SQL_MAX_NUMERIC_LEN]; + }; + + // DBMONEY + // + struct money + { + // 8-byte signed integer containing value * 10,000. + // + int high; + unsigned int low; + }; + + // DBMONEY4 + // + struct smallmoney + { + int value; // 4-byte signed integer containing value * 10,000. + }; + + //@@ TODO + // + struct date {}; + struct time {}; + struct datetime {}; + struct datetimeoffset {}; + } +} + #include #endif // ODB_MSSQL_MSSQL_FWD_HXX diff --git a/odb/mssql/mssql-types.hxx b/odb/mssql/mssql-types.hxx index 2ce7940..caedda3 100644 --- a/odb/mssql/mssql-types.hxx +++ b/odb/mssql/mssql-types.hxx @@ -87,12 +87,10 @@ namespace odb int_, // Buffer is a 4-byte integer. bigint, // Buffer is an 8-byte integer. - /* - numeric, // Buffer is an SQL_NUMERIC_STRUCT. + decimal, // Buffer is a decimal struct (SQL_NUMERIC_STRUCT). - smallmoney, // Buffer is a 4-byte integer (*10,000 value). - money, // Buffer is an 8-byte integer (*10,000 value). - */ + smallmoney, // Buffer is a smallmoney struct (DBMONEY4). + money, // Buffer is a money struct (DBMONEY). float4, // Buffer is a float. float8, // Buffer is a double. @@ -124,6 +122,9 @@ namespace odb SQLLEN* size_ind; // Pointer to the size/inidicator variable. SQLLEN capacity; // Buffer capacity. For string/binary parameters // this value is also used as maximum column size. + // For decimal parameters it contains precision (p) + // and scale (s) encoded as (p * 100 + s). For float4 + // and float8 it contains precision. }; } } diff --git a/odb/mssql/statement.cxx b/odb/mssql/statement.cxx index 8e61d82..0f05e95 100644 --- a/odb/mssql/statement.cxx +++ b/odb/mssql/statement.cxx @@ -31,8 +31,12 @@ namespace odb SQL_INTEGER, // bind::integer SQL_BIGINT, // bind::bigint - SQL_REAL, // bind::float4 - SQL_DOUBLE, // bind::float8 + SQL_DECIMAL, // bind::decimal + SQL_DECIMAL, // bind::smallmoney + SQL_DECIMAL, // bind::money + + SQL_FLOAT, // bind::float4 + SQL_FLOAT, // bind::float8 SQL_VARCHAR, // bind::string SQL_VARCHAR, // bind::long_string @@ -54,6 +58,10 @@ namespace odb SQL_C_SLONG, // bind::integer SQL_C_SBIGINT, // bind::bigint + SQL_C_NUMERIC, // bind::decimal + SQL_C_BINARY, // bind::smallmoney + SQL_C_BINARY, // bind::money + SQL_C_FLOAT, // bind::float4 SQL_C_DOUBLE, // bind::float8 @@ -170,10 +178,39 @@ namespace odb for (size_t i (1); i < n; ++i, ++b) { SQLULEN col_size; + SQLSMALLINT digits (0); SQLPOINTER buf; switch (b->type) { + case bind::decimal: + { + buf = (SQLPOINTER) b->buffer; + col_size = (SQLULEN) (b->capacity / 100); // precision + digits = (SQLSMALLINT) (b->capacity % 100); // scale + break; + } + case bind::smallmoney: + { + buf = (SQLPOINTER) b->buffer; + col_size = 10; + digits = 4; + break; + } + case bind::money: + { + buf = (SQLPOINTER) b->buffer; + col_size = 19; + digits = 4; + break; + } + case bind::float4: + case bind::float8: + { + buf = (SQLPOINTER) b->buffer; + col_size = (SQLULEN) b->capacity; // precision + break; + } case bind::long_string: case bind::long_binary: { @@ -207,6 +244,7 @@ namespace odb default: { buf = (SQLPOINTER) b->buffer; + col_size = 0; break; } } @@ -218,7 +256,7 @@ namespace odb c_type_lookup[b->type], sql_type_lookup[b->type], col_size, - 0, // decimal digits + digits, buf, 0, // buffer capacity (shouldn't be needed for input parameters) b->size_ind); @@ -236,8 +274,35 @@ namespace odb size_t i (0); for (bind* end (b + n); b != end; ++b) { + SQLLEN cap; + switch (b->type) { + case bind::decimal: + { + cap = (SQLLEN) sizeof (decimal); + break; + } + case bind::smallmoney: + { + cap = (SQLLEN) sizeof (smallmoney); + break; + } + case bind::money: + { + cap = (SQLLEN) sizeof (money); + break; + } + case bind::float4: + { + cap = (SQLLEN) sizeof (float); + break; + } + case bind::float8: + { + cap = (SQLLEN) sizeof (double); + break; + } case bind::long_string: case bind::long_nstring: case bind::long_binary: @@ -248,6 +313,7 @@ namespace odb } default: { + cap = b->capacity; break; } } @@ -256,7 +322,7 @@ namespace odb (SQLUSMALLINT) (i + 1), // Results are counted from 1. c_type_lookup[b->type], (SQLPOINTER) b->buffer, - b->capacity, + cap, b->size_ind); if (!SQL_SUCCEEDED (r)) diff --git a/odb/mssql/traits.cxx b/odb/mssql/traits.cxx new file mode 100644 index 0000000..6c625c6 --- /dev/null +++ b/odb/mssql/traits.cxx @@ -0,0 +1,15 @@ +// file : odb/mssql/traits.cxx +// author : Constantin Michael +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#include + +using namespace std; + +namespace odb +{ + namespace mssql + { + } +} diff --git a/odb/mssql/traits.hxx b/odb/mssql/traits.hxx new file mode 100644 index 0000000..28188cb --- /dev/null +++ b/odb/mssql/traits.hxx @@ -0,0 +1,498 @@ +// file : odb/mssql/traits.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#ifndef ODB_MSSQL_TRAITS_HXX +#define ODB_MSSQL_TRAITS_HXX + +#include + +#include +#include +#include // std::size_t +//@@ #include // std::memcpy, std::memset, std::strlen +//@@ #include + +#include +#include + +#include +#include + +#include +#include + +#include + +namespace odb +{ + namespace mssql + { + enum database_type_id + { + id_bit, + id_tinyint, + id_smallint, + id_int, + id_bigint, + + id_decimal, // DECIMAL; NUMERIC + + id_smallmoney, + id_money, + + id_float4, // REAL; FLOAT(n) with n <= 24 + id_float8, // FLOAT(n) with n > 24 + + id_string, // CHAR(n), VARCHAR(n) with n <= N + id_long_string, // CHAR(n), VARCHAR(n) with n > N; TEXT + + id_nstring, // NCHAR(n), NVARCHAR(n) with 2*n <= N + id_long_nstring, // NCHAR(n), NVARCHAR(n) with 2*n > N; NTEXT + + id_binary, // BINARY(n), VARBINARY(n) with n <= N + id_long_binary, // BINARY(n), VARBINARY(n) with n > N; IMAGE + + id_date, // DATE + id_time, // TIME + id_datetime, // DATETIME; DATETIME2; SMALLDATETIME + id_datetimeoffset, // DATETIMEOFFSET + + id_uuid, // UNIQUEIDENTIFIER + id_rowversion // ROWVERSION; TIMESTAMP + }; + + // + // image_traits + // + + template + struct image_traits; + + template <> + struct image_traits {typedef unsigned char image_type;}; + + template <> + struct image_traits {typedef unsigned char image_type;}; + + template <> + struct image_traits {typedef short image_type;}; + + template <> + struct image_traits {typedef int image_type;}; + + template <> + struct image_traits {typedef long long image_type;}; + + template <> + struct image_traits {typedef decimal image_type;}; + + template <> + struct image_traits {typedef smallmoney image_type;}; + + template <> + struct image_traits {typedef money image_type;}; + + template <> + struct image_traits {typedef float image_type;}; + + template <> + struct image_traits {typedef double image_type;}; + + template <> + struct image_traits {typedef char* image_type;}; + + template <> + struct image_traits {typedef long_callback image_type;}; + + template <> + struct image_traits {typedef ucs2_char* image_type;}; + + template <> + struct image_traits {typedef long_callback image_type;}; + + template <> + struct image_traits {typedef char* image_type;}; + + template <> + struct image_traits {typedef long_callback image_type;}; + + template <> + struct image_traits {typedef date image_type;}; + + template <> + struct image_traits {typedef time image_type;}; + + template <> + struct image_traits {typedef datetime image_type;}; + + template <> + struct image_traits {typedef datetimeoffset image_type;}; + + // Image is a 16-byte sequence. + // + template <> + struct image_traits {typedef unsigned char* image_type;}; + + // Image is an 8-byte sequence. + // + template <> + struct image_traits {typedef unsigned char* image_type;}; + + // + // value_traits + // + + template + struct wrapped_value_traits; + + template + struct default_value_traits; + + template ::r> + struct select_traits; + + template + struct select_traits + { + typedef default_value_traits type; + }; + + template + struct select_traits + { + typedef + wrapped_value_traits::null_handler> + type; + }; + + template + class value_traits: public select_traits::type + { + }; + + // The wrapped_value_traits specializations should be able to handle + // any value type which means we have to have every possible signature + // of the set_value() and set_image() functions. + // + template + struct wrapped_value_traits + { + typedef wrapper_traits wtraits; + typedef typename wtraits::unrestricted_wrapped_type wrapped_type; + + typedef W value_type; + typedef wrapped_type query_type; + typedef typename image_traits::image_type image_type; + + typedef value_traits vtraits; + + static void + set_value (W& v, const image_type& i, bool is_null) + { + vtraits::set_value (wtraits::set_ref (v), i, is_null); + } + + static void + set_image (image_type& i, bool& is_null, const W& v) + { + vtraits::set_image (i, is_null, wtraits::get_ref (v)); + } + + /* + @@ TODO + + // big_int, big_float, string, nstring, raw. + // + static void + set_value (W& v, const char* i, std::size_t n, bool is_null) + { + vtraits::set_value (wtraits::set_ref (v), i, n, is_null); + } + + // string, nstring, raw. + // + static void + set_image (char* i, + std::size_t c, + std::size_t& n, + bool& is_null, + const W& v) + { + vtraits::set_image (i, c, n, is_null, wtraits::get_ref (v)); + } + + // big_int, big_float. + // + static void + set_image (char* i, std::size_t& n, bool& is_null, const W& v) + { + vtraits::set_image (i, n, is_null, wtraits::get_ref (v)); + } + + // blob, clob, nclob. + // + static void + set_value (W& v, result_callback_type& cb, void*& context, bool is_null) + { + vtraits::set_value (wtraits::set_ref (v), cb, context, is_null); + } + + static void + set_image (param_callback_type& cb, + const void*& context, + bool& is_null, + const W& v) + { + vtraits::set_image (cb, context, is_null, wtraits::get_ref (v)); + } + */ + }; + + template + struct wrapped_value_traits + { + typedef wrapper_traits wtraits; + typedef typename wtraits::unrestricted_wrapped_type wrapped_type; + + typedef W value_type; + typedef wrapped_type query_type; + typedef typename image_traits::image_type image_type; + + typedef value_traits vtraits; + + static void + set_value (W& v, const image_type& i, bool is_null) + { + if (is_null) + wtraits::set_null (v); + else + vtraits::set_value (wtraits::set_ref (v), i, is_null); + } + + static void + set_image (image_type& i, bool& is_null, const W& v) + { + is_null = wtraits::get_null (v); + + if (!is_null) + vtraits::set_image (i, is_null, wtraits::get_ref (v)); + } + + /* + @@ TODO + + // big_int, big_float, string, nstring, raw. + // + static void + set_value (W& v, const char* i, std::size_t n, bool is_null) + { + if (is_null) + wtraits::set_null (v); + else + vtraits::set_value (wtraits::set_ref (v), i, n, is_null); + } + + // string, nstring, raw. + // + static void + set_image (char* i, + std::size_t c, + std::size_t& n, + bool& is_null, + const W& v) + { + is_null = wtraits::get_null (v); + + if (!is_null) + vtraits::set_image (i, c, n, is_null, wtraits::get_ref (v)); + } + + // big_int, big_float + // + static void + set_image (char* i, std::size_t& n, bool& is_null, const W& v) + { + is_null = wtraits::get_null (v); + + if (!is_null) + vtraits::set_image (i, n, is_null, wtraits::get_ref (v)); + } + + // blob, clob, nclob. + // + static void + set_value (W& v, result_callback_type& cb, void*& context, bool is_null) + { + if (is_null) + wtraits::set_null (v); + else + vtraits::set_value (wtraits::set_ref (v), cb, context, is_null); + } + + static void + set_image (param_callback_type& cb, + const void*& context, + bool& is_null, + const W& v) + { + is_null = wtraits::get_null (v); + + if (!is_null) + vtraits::set_image (cb, context, is_null, wtraits::get_ref (v)); + } + */ + }; + + template + struct default_value_traits + { + typedef T value_type; + typedef T query_type; + typedef typename image_traits::image_type image_type; + + static void + set_value (T& v, const image_type& i, bool is_null) + { + if (!is_null) + v = T (i); + else + v = T (); + } + + static void + set_image (image_type& i, bool& is_null, T v) + { + is_null = false; + i = image_type (v); + } + }; + + // + // type_traits + // + + template + struct default_type_traits; + + template + class type_traits: public default_type_traits + { + }; + + // Integral types. + // + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_bit; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_tinyint; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_tinyint; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_smallint; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_smallint; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_int; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_int; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_bigint; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_bigint; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_bigint; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_bigint; + }; + + // Float types. + // + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_float4; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_float8; + }; + + // String type. + // + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_long_string; + }; + + template <> + struct default_type_traits + { + static const database_type_id db_type_id = id_long_string; + }; + + template + struct default_type_traits + { + static const database_type_id db_type_id = id_long_string; + }; + + template + struct default_type_traits + { + static const database_type_id db_type_id = id_long_string; + }; + } +} + +#include + +#include + +#endif // ODB_MSSQL_TRAITS_HXX diff --git a/odb/mssql/traits.txx b/odb/mssql/traits.txx new file mode 100644 index 0000000..b3bc442 --- /dev/null +++ b/odb/mssql/traits.txx @@ -0,0 +1,11 @@ +// file : odb/mssql/traits.txx +// author : Constantin Michael +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +namespace odb +{ + namespace mssql + { + } +} -- cgit v1.1