// file : odb/pgsql/traits.hxx // copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file #ifndef ODB_PGSQL_TRAITS_HXX #define ODB_PGSQL_TRAITS_HXX #include #include // ODB_CXX11 #include #include #include // std::size_t #include // std::memcpy, std::memset, std::strlen #ifdef ODB_CXX11 # include #endif #include #include #include #include #include #include #include #include namespace odb { namespace pgsql { enum database_type_id { id_boolean, id_smallint, id_integer, id_bigint, id_numeric, id_real, id_double, id_date, id_time, id_timestamp, id_string, id_bytea, id_bit, id_varbit, id_uuid }; // // image_traits // template struct image_traits; template <> struct image_traits {typedef bool 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 details::buffer image_type;}; template <> struct image_traits {typedef float image_type;}; template <> struct image_traits {typedef double image_type;}; template <> struct image_traits {typedef int image_type;}; template <> struct image_traits {typedef long long image_type;}; template <> struct image_traits {typedef long long 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;}; template <> struct image_traits {typedef details::ubuffer image_type;}; // UUID image is a 16-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)); } // String, BYTEA, and NUMERIC. // static void set_value (W& v, const details::buffer& b, std::size_t n, bool is_null) { vtraits::set_value (wtraits::set_ref (v), b, n, is_null); } static void set_image (details::buffer& b, std::size_t& n, bool& is_null, const W& v) { vtraits::set_image (b, n, is_null, wtraits::get_ref (v)); } // BIT. // static void set_value (W& v, const unsigned char* i, std::size_t n, bool is_null) { vtraits::set_value (wtraits::set_ref (v), i, n, is_null); } static void set_image (unsigned 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)); } // VARBIT. // static void set_value (W& v, const details::ubuffer& b, std::size_t n, bool is_null) { vtraits::set_value (wtraits::set_ref (v), b, n, is_null); } static void set_image (details::ubuffer& b, std::size_t& n, bool& is_null, const W& v) { vtraits::set_image (b, n, is_null, wtraits::get_ref (v)); } // UUID. // static void set_value (W& v, const unsigned char* i, bool is_null) { vtraits::set_value (wtraits::set_ref (v), i, is_null); } static void set_image (unsigned char* i, bool& is_null, const W& v) { vtraits::set_image (i, 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)); } // String, BYTEA, and NUMERIC. // static void set_value (W& v, const details::buffer& b, std::size_t n, bool is_null) { if (is_null) wtraits::set_null (v); else vtraits::set_value (wtraits::set_ref (v), b, n, is_null); } static void set_image (details::buffer& b, std::size_t& n, bool& is_null, const W& v) { is_null = wtraits::get_null (v); if (!is_null) vtraits::set_image (b, n, is_null, wtraits::get_ref (v)); } // BIT. // static void set_value (W& v, const unsigned 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); } static void set_image (unsigned 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)); } // VARBIT. // static void set_value (W& v, const details::ubuffer& b, std::size_t n, bool is_null) { if (is_null) wtraits::set_null (v); else vtraits::set_value (wtraits::set_ref (v), b, n, is_null); } static void set_image (details::ubuffer& b, std::size_t& n, bool& is_null, const W& v) { is_null = wtraits::get_null (v); if (!is_null) vtraits::set_image (b, n, is_null, wtraits::get_ref (v)); } // UUID. // static void set_value (W& v, const unsigned char* 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 (unsigned char* 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)); } }; 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 (details::endian_traits::ntoh (i)); else v = T (); } static void set_image (image_type& i, bool& is_null, T v) { is_null = false; i = details::endian_traits::hton (image_type (v)); } }; // std::string specialization. // class LIBODB_PGSQL_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_PGSQL_EXPORT default_value_traits: string_value_traits { }; template <> struct LIBODB_PGSQL_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_PGSQL_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_PGSQL_EXPORT default_value_traits: c_string_value_traits { }; template <> struct LIBODB_PGSQL_EXPORT default_value_traits: c_string_value_traits { }; template struct default_value_traits: c_string_value_traits { }; template struct default_value_traits: c_string_value_traits { }; template struct default_value_traits: c_string_value_traits { }; template struct default_value_traits: c_string_value_traits { }; // std::vector (buffer) specialization. // template <> struct LIBODB_PGSQL_EXPORT default_value_traits< std::vector, id_bytea> { public: typedef std::vector value_type; typedef std::vector query_type; typedef details::buffer image_type; static void set_value (value_type& v, const details::buffer& b, std::size_t n, bool is_null) { if (!is_null) v.assign (b.data (), b.data () + n); else v.clear (); } static void set_image (details::buffer&, std::size_t& n, bool& is_null, const value_type&); }; // std::vector (buffer) specialization. // template <> struct LIBODB_PGSQL_EXPORT default_value_traits< std::vector, id_bytea> { public: typedef std::vector value_type; typedef std::vector query_type; typedef details::buffer image_type; static void set_value (value_type& v, const details::buffer& b, std::size_t n, bool is_null) { if (!is_null) { const unsigned char* d ( reinterpret_cast (b.data ())); v.assign (d, d + n); } else v.clear (); } static void set_image (details::buffer&, std::size_t& n, bool& is_null, const value_type&); }; // char[N] (buffer) specialization. // template struct default_value_traits { public: typedef char* value_type; typedef const char* query_type; typedef details::buffer image_type; static void set_value (char* const& v, const details::buffer& b, std::size_t n, bool is_null) { if (!is_null) std::memcpy (v, b.data (), (n < N ? n : N)); else std::memset (v, 0, N); } static void set_image (details::buffer& b, std::size_t& n, bool& is_null, const char* v) { is_null = false; n = N; if (n > b.capacity ()) b.capacity (n); std::memcpy (b.data (), v, n); } }; // unsigned char[N] (buffer) specialization. // template struct default_value_traits { public: typedef unsigned char* value_type; typedef const unsigned char* query_type; typedef details::buffer image_type; static void set_value (unsigned char* const& v, const details::buffer& b, std::size_t n, bool is_null) { if (!is_null) std::memcpy (v, b.data (), (n < N ? n : N)); else std::memset (v, 0, N); } static void set_image (details::buffer& b, std::size_t& n, bool& is_null, const unsigned char* v) { is_null = false; n = N; if (n > b.capacity ()) b.capacity (n); std::memcpy (b.data (), v, n); } }; #ifdef ODB_CXX11 // std::array (buffer) specialization. // template struct default_value_traits, id_bytea> { public: typedef std::array value_type; typedef value_type query_type; typedef details::buffer image_type; static void set_value (value_type& v, const details::buffer& b, std::size_t n, bool is_null) { if (!is_null) std::memcpy (v.data (), b.data (), (n < N ? n : N)); else std::memset (v.data (), 0, N); } static void set_image (details::buffer& b, std::size_t& n, bool& is_null, const value_type& v) { is_null = false; n = N; if (n > b.capacity ()) b.capacity (n); std::memcpy (b.data (), v.data (), n); } }; // std::array (buffer) specialization. // template struct default_value_traits, id_bytea> { public: typedef std::array value_type; typedef value_type query_type; typedef details::buffer image_type; static void set_value (value_type& v, const details::buffer& b, std::size_t n, bool is_null) { if (!is_null) std::memcpy (v.data (), b.data (), (n < N ? n : N)); else std::memset (v.data (), 0, N); } static void set_image (details::buffer& b, std::size_t& n, bool& is_null, const value_type& v) { is_null = false; n = N; if (n > b.capacity ()) b.capacity (n); std::memcpy (b.data (), v.data (), n); } }; #endif // char[16] specialization for uuid. // template <> struct LIBODB_PGSQL_EXPORT default_value_traits { typedef char* value_type; typedef const char* query_type; typedef unsigned char* image_type; static void set_value (char* const& v, const unsigned char* i, bool is_null) { if (!is_null) std::memcpy (v, i, 16); else std::memset (v, 0, 16); } static void set_image (unsigned char* i, bool& is_null, const char* v) { is_null = false; std::memcpy (i, v, 16); } }; // // 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_boolean; }; 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_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_integer; }; template <> struct default_type_traits { static const database_type_id db_type_id = id_integer; }; 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_real; }; template <> struct default_type_traits { static const database_type_id db_type_id = id_double; }; // String types. // 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; }; 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; }; // Binary types. // template <> struct default_type_traits > { static const database_type_id db_type_id = id_bytea; }; template <> struct default_type_traits > { static const database_type_id db_type_id = id_bytea; }; #ifdef ODB_CXX11 template struct default_type_traits > { static const database_type_id db_type_id = id_bytea; }; template struct default_type_traits > { static const database_type_id db_type_id = id_bytea; }; #endif } } #include #endif // ODB_PGSQL_TRAITS_HXX