// file : odb/mysql/traits.hxx // author : Boris Kolpackov // copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file #ifndef ODB_MYSQL_TRAITS_HXX #define ODB_MYSQL_TRAITS_HXX #include #include #include // std::size_t #include #include #include #include #include namespace odb { namespace mysql { enum database_type_id { id_tiny, id_utiny, id_short, id_ushort, id_long, id_ulong, id_longlong, id_ulonglong, id_float, id_double, id_decimal, id_date, id_time, id_datetime, id_timestamp, id_year, id_string, id_blob, id_bit, id_enum, id_set }; // // image_traits // template struct image_traits; template <> struct image_traits {typedef signed 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 unsigned short image_type;}; template <> struct image_traits {typedef int image_type;}; template <> struct image_traits {typedef unsigned int image_type;}; template <> struct image_traits {typedef long long image_type;}; template <> struct image_traits {typedef unsigned long long image_type;}; template <> struct image_traits {typedef float image_type;}; template <> struct image_traits {typedef double image_type;}; template <> struct image_traits {typedef details::buffer image_type;}; template <> struct image_traits {typedef MYSQL_TIME image_type;}; template <> struct image_traits {typedef MYSQL_TIME image_type;}; template <> struct image_traits {typedef MYSQL_TIME image_type;}; template <> struct image_traits {typedef MYSQL_TIME image_type;}; template <> struct image_traits {typedef short image_type;}; template <> struct image_traits {typedef details::buffer image_type;}; template <> struct image_traits {typedef details::buffer image_type;}; template <> struct image_traits {typedef unsigned char* image_type;}; // Note: default mapping is to integer. Alternative mapping is to // string. // template <> struct image_traits {typedef unsigned short image_type;}; template <> struct image_traits {typedef details::buffer image_type;}; // // value_traits // template struct default_value_traits; template class value_traits: public default_value_traits { }; 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); } }; // Specialization for numeric enum representations (C++ enum, integer // types, etc). In particular, this specialization works only for C++ // enum type as long as its numeric value space starts with 0, is // ascending and contiguous (i.e., the default enumerator assignment). // template struct default_value_traits { typedef T value_type; typedef T query_type; typedef unsigned short image_type; static void set_value (T& v, unsigned short i, bool is_null) { // In MySQL first enumerator has index 1. // if (!is_null) v = static_cast (i - 1); else v = T (); } static void set_image (unsigned short& i, bool& is_null, const T& v) { is_null = false; i = static_cast (v) + 1; } }; // std::string specialization. // class LIBODB_MYSQL_EXPORT string_value_traits { public: typedef std::string value_type; typedef std::string query_type; typedef details::buffer image_type; static void set_value (std::string& v, const details::buffer& b, std::size_t n, bool is_null) { if (!is_null) v.assign (b.data (), n); else v.erase (); } static void set_image (details::buffer&, std::size_t& n, bool& is_null, const std::string&); }; template <> struct LIBODB_MYSQL_EXPORT default_value_traits: string_value_traits { }; template <> struct LIBODB_MYSQL_EXPORT default_value_traits: string_value_traits { }; template <> struct LIBODB_MYSQL_EXPORT default_value_traits: string_value_traits { }; template <> struct LIBODB_MYSQL_EXPORT default_value_traits: string_value_traits { }; // const char* specialization // // Specialization for const char* which only supports initialization // of an image from the value but not the other way around. This way // we can pass such values to the queries. // class LIBODB_MYSQL_EXPORT c_string_value_traits { public: typedef const char* value_type; typedef const char* query_type; typedef details::buffer image_type; static void set_image (details::buffer&, std::size_t& n, bool& is_null, const char*); }; template <> struct LIBODB_MYSQL_EXPORT default_value_traits: c_string_value_traits { }; template <> struct LIBODB_MYSQL_EXPORT default_value_traits: c_string_value_traits { }; template <> struct LIBODB_MYSQL_EXPORT default_value_traits: c_string_value_traits { }; template <> struct LIBODB_MYSQL_EXPORT default_value_traits: c_string_value_traits { }; // // 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_tiny; }; template <> struct default_type_traits { static const database_type_id db_type_id = id_tiny; }; template <> struct default_type_traits { static const database_type_id db_type_id = id_utiny; }; template <> struct default_type_traits { static const database_type_id db_type_id = id_short; }; template <> struct default_type_traits { static const database_type_id db_type_id = id_ushort; }; template <> struct default_type_traits { static const database_type_id db_type_id = id_long; }; template <> struct default_type_traits { static const database_type_id db_type_id = id_ulong; }; template <> struct default_type_traits { static const database_type_id db_type_id = id_longlong; }; template <> struct default_type_traits { static const database_type_id db_type_id = id_ulonglong; }; template <> struct default_type_traits { static const database_type_id db_type_id = id_longlong; }; template <> struct default_type_traits { static const database_type_id db_type_id = id_ulonglong; }; // Float types. // template <> struct default_type_traits { static const database_type_id db_type_id = id_float; }; template <> struct default_type_traits { static const database_type_id db_type_id = id_double; }; // String type. // template <> struct default_type_traits { static const database_type_id db_type_id = id_string; }; template <> struct default_type_traits { static const database_type_id db_type_id = id_string; }; } } #include #endif // ODB_MYSQL_TRAITS_HXX