diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-09-11 13:55:47 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-09-11 13:55:47 +0200 |
commit | 80b80d1cbffe1c230d39cad1e2b7ac452da197cd (patch) | |
tree | 16cdcc875d2f7a6577a4a59ea14ffe00b8bee2a6 | |
parent | 2d725c335afc7f051e3f461f498b6f1bdbdc5a4d (diff) |
Add support for mapping std::array to BLOB and char[16] to UUID types
-rw-r--r-- | odb/oracle/traits.hxx | 258 | ||||
-rw-r--r-- | odb/oracle/traits.txx | 62 |
2 files changed, 295 insertions, 25 deletions
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 <odb/pre.hxx> +#include <odb/details/config.hxx> // ODB_CXX11 + #include <string> #include <vector> #include <cstddef> // std::size_t #include <cstring> // std::memcpy, std::memset, std::strlen +#ifdef ODB_CXX11 +# include <array> +#endif + #include <odb/traits.hxx> #include <odb/wrapper-traits.hxx> @@ -583,24 +589,24 @@ namespace odb { }; - template <std::size_t n> - struct default_value_traits<char[n], id_string>: c_string_value_traits + template <std::size_t N> + struct default_value_traits<char[N], id_string>: c_string_value_traits { }; - template <std::size_t n> - struct default_value_traits<const char[n], id_string>: + template <std::size_t N> + struct default_value_traits<const char[N], id_string>: c_string_value_traits { }; - template <std::size_t n> - struct default_value_traits<char[n], id_nstring>: c_string_value_traits + template <std::size_t N> + struct default_value_traits<char[N], id_nstring>: c_string_value_traits { }; - template <std::size_t n> - struct default_value_traits<const char[n], id_nstring>: + template <std::size_t N> + struct default_value_traits<const char[N], id_nstring>: c_string_value_traits { }; @@ -686,7 +692,7 @@ namespace odb } }; - // char[n] (buffer) specialization for RAW. + // char[N] (buffer) specialization for RAW. // template <std::size_t N> struct default_value_traits<char[N], id_raw> @@ -717,7 +723,7 @@ namespace odb } }; - // unsigned char[n] (buffer) specialization for RAW. + // unsigned char[N] (buffer) specialization for RAW. // template <std::size_t N> struct default_value_traits<unsigned char[N], id_raw> @@ -751,6 +757,72 @@ namespace odb } }; +#ifdef ODB_CXX11 + // std::array<char, N> (buffer) specialization for RAW. + // + template <std::size_t N> + struct default_value_traits<std::array<char, N>, id_raw> + { + public: + typedef std::array<char, N> 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<unsigned char, N> (buffer) specialization for RAW. + // + template <std::size_t N> + struct default_value_traits<std::array<unsigned char, N>, id_raw> + { + public: + typedef std::array<unsigned char, N> 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 <std::size_t n> - struct default_value_traits<char[n], id_clob>: + template <std::size_t N> + struct default_value_traits<char[N], id_clob>: c_string_lob_value_traits { }; - template <std::size_t n> - struct default_value_traits<const char[n], id_clob>: + template <std::size_t N> + struct default_value_traits<const char[N], id_clob>: c_string_lob_value_traits { }; - template <std::size_t n> - struct default_value_traits<char[n], id_nclob>: + template <std::size_t N> + struct default_value_traits<char[N], id_nclob>: c_string_lob_value_traits { }; - template <std::size_t n> - struct default_value_traits<const char[n], id_nclob>: + template <std::size_t N> + struct default_value_traits<const char[N], id_nclob>: 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 <std::size_t N> struct default_value_traits<char[N], id_blob> @@ -1046,7 +1118,7 @@ namespace odb ub4 capacity); }; - // unsigned char[n] (buffer) specialization for BLOBs. + // unsigned char[N] (buffer) specialization for BLOBs. // template <std::size_t N> struct default_value_traits<unsigned char[N], id_blob> @@ -1099,6 +1171,114 @@ namespace odb ub4 capacity); }; +#ifdef ODB_CXX11 + // std::array<char, N> (buffer) specialization for BLOBS. + // + template <std::size_t N> + struct default_value_traits<std::array<char, N>, id_blob> + { + public: + typedef std::array<char, N> 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<unsigned char, N> (buffer) specialization for BLOBS. + // + template <std::size_t N> + struct default_value_traits<std::array<unsigned char, N>, id_blob> + { + public: + typedef std::array<unsigned char, N> 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<std::string> @@ -1207,17 +1387,45 @@ namespace odb static const database_type_id db_type_id = id_string; }; - template <std::size_t n> - struct default_type_traits<char[n]> + template <std::size_t N> + struct default_type_traits<char[N]> { static const database_type_id db_type_id = id_string; }; - template <std::size_t n> - struct default_type_traits<const char[n]> + template <std::size_t N> + struct default_type_traits<const char[N]> { static const database_type_id db_type_id = id_string; }; + + // Binary types. Assume BLOB. + // + template <> + struct default_type_traits<std::vector<char> > + { + static const database_type_id db_type_id = id_blob; + }; + + template <> + struct default_type_traits<std::vector<unsigned char> > + { + static const database_type_id db_type_id = id_blob; + }; + +#ifdef ODB_CXX11 + template <std::size_t N> + struct default_type_traits<std::array<char, N> > + { + static const database_type_id db_type_id = id_blob; + }; + + template <std::size_t N> + struct default_type_traits<std::array<unsigned char, N> > + { + 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<std::array<char, N>, id_blob> + // + + template <std::size_t N> + bool default_value_traits<std::array<char, N>, 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<char*> (c) + *position, b, n); + *position += n; + return true; + } + + template <std::size_t N> + bool default_value_traits<std::array<char, N>, id_blob>:: + param_callback (const void* c, + ub4*, + const void** b, + ub4* s, + chunk_position* p, + void*, + ub4) + { + *p = chunk_one; + *s = static_cast<ub4> (N); + *b = c; + return true; + } + + // + // default_value_traits<std::array<unsigned char, N>, id_blob> + // + + template <std::size_t N> + bool default_value_traits<std::array<unsigned char, N>, 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<unsigned char*> (c) + *position, b, n); + *position += n; + return true; + } + + template <std::size_t N> + bool default_value_traits<std::array<unsigned char, N>, id_blob>:: + param_callback (const void* c, + ub4*, + const void** b, + ub4* s, + chunk_position* p, + void*, + ub4) + { + *p = chunk_one; + *s = static_cast<ub4> (N); + *b = c; + return true; + } +#endif } } |