From 80b80d1cbffe1c230d39cad1e2b7ac452da197cd 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/oracle/traits.hxx | 258 +++++++++++++++++++++++++++++++++++++++++++++----- odb/oracle/traits.txx | 62 ++++++++++++ 2 files changed, 295 insertions(+), 25 deletions(-) (limited to 'odb') diff --git a/odb/oracle/traits.hxx b/odb/oracle/traits.hxx index 367e79d..0f7c635 100644 --- a/odb/oracle/traits.hxx +++ b/odb/oracle/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 @@ -583,24 +589,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 { }; @@ -686,7 +692,7 @@ namespace odb } }; - // char[n] (buffer) specialization for RAW. + // char[N] (buffer) specialization for RAW. // template struct default_value_traits @@ -717,7 +723,7 @@ namespace odb } }; - // unsigned char[n] (buffer) specialization for RAW. + // unsigned char[N] (buffer) specialization for RAW. // template struct default_value_traits @@ -751,6 +757,72 @@ namespace odb } }; +#ifdef ODB_CXX11 + // std::array (buffer) specialization for RAW. + // + template + struct default_value_traits, id_raw> + { + public: + typedef std::array value_type; + typedef value_type query_type; + typedef char* image_type; + + static void + set_value (value_type& v, const char* b, std::size_t n, bool is_null) + { + if (!is_null) + std::memcpy (v.data (), b, (n < N ? n : N)); + else + std::memset (v.data (), 0, N); + } + + static void + set_image (char* b, + std::size_t c, + std::size_t& n, + bool& is_null, + const value_type& v) + { + is_null = false; + n = N < c ? N : c; + std::memcpy (b, v.data (), n); + } + }; + + // std::array (buffer) specialization for RAW. + // + template + struct default_value_traits, id_raw> + { + public: + typedef std::array value_type; + typedef value_type query_type; + typedef char* image_type; + + static void + set_value (value_type& v, const char* b, std::size_t n, bool is_null) + { + if (!is_null) + std::memcpy (v.data (), b, (n < N ? n : N)); + else + std::memset (v.data (), 0, N); + } + + static void + set_image (char* b, + std::size_t c, + std::size_t& n, + bool& is_null, + const value_type& v) + { + is_null = false; + n = N < c ? N : c; + std::memcpy (b, v.data (), n); + } + }; +#endif + // std::string specialization for LOBs. // class LIBODB_ORACLE_EXPORT string_lob_value_traits @@ -861,26 +933,26 @@ namespace odb { }; - template - struct default_value_traits: + template + struct default_value_traits: c_string_lob_value_traits { }; - template - struct default_value_traits: + template + struct default_value_traits: c_string_lob_value_traits { }; - template - struct default_value_traits: + template + struct default_value_traits: c_string_lob_value_traits { }; - template - struct default_value_traits: + template + struct default_value_traits: c_string_lob_value_traits { }; @@ -993,7 +1065,7 @@ namespace odb ub4 capacity); }; - // char[n] (buffer) specialization for BLOBs. + // char[N] (buffer) specialization for BLOBs. // template struct default_value_traits @@ -1046,7 +1118,7 @@ namespace odb ub4 capacity); }; - // unsigned char[n] (buffer) specialization for BLOBs. + // unsigned char[N] (buffer) specialization for BLOBs. // template struct default_value_traits @@ -1099,6 +1171,114 @@ namespace odb ub4 capacity); }; +#ifdef ODB_CXX11 + // std::array (buffer) specialization for BLOBS. + // + template + struct default_value_traits, id_blob> + { + public: + typedef std::array value_type; + typedef value_type query_type; + typedef lob_callback image_type; + + static void + set_value (value_type& v, + result_callback_type& cb, + void*& context, + bool is_null) + { + if (!is_null) + { + cb = &result_callback; + context = v.data (); + } + else + std::memset (v.data (), 0, N); + } + + static void + set_image (param_callback_type& cb, + const void*& context, + bool& is_null, + const value_type& v) + { + is_null = false; + cb = ¶m_callback; + context = v.data (); + } + + static bool + result_callback (void* context, + ub4* position_context, + void* buffer, + ub4 size, + chunk_position); + + static bool + param_callback (const void* context, + ub4* position_context, + const void** buffer, + ub4* size, + chunk_position*, + void* temp_buffer, + ub4 capacity); + }; + + // std::array (buffer) specialization for BLOBS. + // + template + struct default_value_traits, id_blob> + { + public: + typedef std::array value_type; + typedef value_type query_type; + typedef lob_callback image_type; + + static void + set_value (value_type& v, + result_callback_type& cb, + void*& context, + bool is_null) + { + if (!is_null) + { + cb = &result_callback; + context = v.data (); + } + else + std::memset (v.data (), 0, N); + } + + static void + set_image (param_callback_type& cb, + const void*& context, + bool& is_null, + const value_type& v) + { + is_null = false; + cb = ¶m_callback; + context = v.data (); + } + + static bool + result_callback (void* context, + ub4* position_context, + void* buffer, + ub4 size, + chunk_position); + + static bool + param_callback (const void* context, + ub4* position_context, + const void** buffer, + ub4* size, + chunk_position*, + void* temp_buffer, + ub4 capacity); + }; +#endif + // // type_traits // @@ -1193,7 +1373,7 @@ namespace odb static const database_type_id db_type_id = id_double; }; - // String type. + // String types. // template <> struct default_type_traits @@ -1207,17 +1387,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. Assume BLOB. + // + template <> + struct default_type_traits > + { + static const database_type_id db_type_id = id_blob; + }; + + template <> + struct default_type_traits > + { + static const database_type_id db_type_id = id_blob; + }; + +#ifdef ODB_CXX11 + template + struct default_type_traits > + { + static const database_type_id db_type_id = id_blob; + }; + + template + struct default_type_traits > + { + static const database_type_id db_type_id = id_blob; + }; +#endif } } diff --git a/odb/oracle/traits.txx b/odb/oracle/traits.txx index 7916ef7..f620e18 100644 --- a/odb/oracle/traits.txx +++ b/odb/oracle/traits.txx @@ -65,5 +65,67 @@ namespace odb *b = c; return true; } + +#ifdef ODB_CXX11 + // + // default_value_traits, id_blob> + // + + template + bool default_value_traits, id_blob>:: + result_callback (void* c, ub4* position, void* b, ub4 s, chunk_position) + { + ub4 n (*position + s < N ? s : N - *position); + std::memcpy (static_cast (c) + *position, b, n); + *position += n; + return true; + } + + template + bool default_value_traits, id_blob>:: + param_callback (const void* c, + ub4*, + const void** b, + ub4* s, + chunk_position* p, + void*, + ub4) + { + *p = chunk_one; + *s = static_cast (N); + *b = c; + return true; + } + + // + // default_value_traits, id_blob> + // + + template + bool default_value_traits, id_blob>:: + result_callback (void* c, ub4* position, void* b, ub4 s, chunk_position) + { + ub4 n (*position + s < N ? s : N - *position); + std::memcpy (static_cast (c) + *position, b, n); + *position += n; + return true; + } + + template + bool default_value_traits, id_blob>:: + param_callback (const void* c, + ub4*, + const void** b, + ub4* s, + chunk_position* p, + void*, + ub4) + { + *p = chunk_one; + *s = static_cast (N); + *b = c; + return true; + } +#endif } } -- cgit v1.1