// 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