From 03c5ec6cf01b58b629b96fe12202ebbabb4f7910 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 11 Sep 2012 13:55:47 +0200 Subject: Add support for mapping std::array to BLOB and char[16] to UUID types --- odb/pgsql/traits.hxx | 168 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 153 insertions(+), 15 deletions(-) diff --git a/odb/pgsql/traits.hxx b/odb/pgsql/traits.hxx index e03287e..8b330e5 100644 --- a/odb/pgsql/traits.hxx +++ b/odb/pgsql/traits.hxx @@ -7,11 +7,17 @@ #include +#include // ODB_CXX11 + #include #include #include // std::size_t #include // std::memcpy, std::memset, std::strlen +#ifdef ODB_CXX11 +# include +#endif + #include #include @@ -441,24 +447,24 @@ namespace odb { }; - template - struct default_value_traits: c_string_value_traits + template + struct default_value_traits: c_string_value_traits { }; - template - struct default_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: + template + struct default_value_traits: c_string_value_traits { }; @@ -527,7 +533,7 @@ namespace odb const value_type&); }; - // char[n] (buffer) specialization. + // char[N] (buffer) specialization. // template struct default_value_traits @@ -565,7 +571,7 @@ namespace odb } }; - // unsigned char[n] (buffer) specialization. + // unsigned char[N] (buffer) specialization. // template struct default_value_traits @@ -603,6 +609,110 @@ namespace odb } }; +#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 // @@ -697,7 +807,7 @@ namespace odb static const database_type_id db_type_id = id_double; }; - // String type. + // String types. // template <> struct default_type_traits @@ -711,17 +821,45 @@ namespace odb static const database_type_id db_type_id = id_string; }; - template - struct default_type_traits + template + struct default_type_traits { static const database_type_id db_type_id = id_string; }; - template - struct default_type_traits + 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 } } -- cgit v1.1