diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-11-03 08:16:50 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-11-03 08:16:50 +0200 |
commit | 85654d7ca5f8b92466465c1ec48975ca343fd331 (patch) | |
tree | 409016e8c0c6aaf2100b575f8c2e3b7234675fa6 | |
parent | a82b67271a23ca42575bda276dc6a0c03b2156ed (diff) |
Add support for mapping std::vector<unsigned char> to BLOB types
-rw-r--r-- | odb/sqlite/traits.cxx | 22 | ||||
-rw-r--r-- | odb/sqlite/traits.hxx | 37 |
2 files changed, 58 insertions, 1 deletions
diff --git a/odb/sqlite/traits.cxx b/odb/sqlite/traits.cxx index 8a9f310..d6b6572 100644 --- a/odb/sqlite/traits.cxx +++ b/odb/sqlite/traits.cxx @@ -70,5 +70,27 @@ namespace odb if (n != 0) memcpy (b.data (), &v.front (), n); } + + // + // default_value_traits<vector<unsigned char>, id_blob> + // + + void default_value_traits<vector<unsigned char>, id_blob>:: + set_image (details::buffer& b, + size_t& n, + bool& is_null, + const value_type& v) + { + is_null = false; + n = v.size (); + + if (n > b.capacity ()) + b.capacity (n); + + // std::vector::data() may not be available in older compilers. + // + if (n != 0) + memcpy (b.data (), &v.front (), n); + } } } diff --git a/odb/sqlite/traits.hxx b/odb/sqlite/traits.hxx index 5d88365..b53d3ab 100644 --- a/odb/sqlite/traits.hxx +++ b/odb/sqlite/traits.hxx @@ -268,7 +268,8 @@ namespace odb // std::vector<char> (buffer) specialization. // template <> - struct LIBODB_SQLITE_EXPORT default_value_traits<std::vector<char>, id_blob> + struct LIBODB_SQLITE_EXPORT default_value_traits< + std::vector<char>, id_blob> { public: typedef std::vector<char> value_type; @@ -294,6 +295,40 @@ namespace odb const value_type&); }; + // std::vector<unsigned char> (buffer) specialization. + // + template <> + struct LIBODB_SQLITE_EXPORT default_value_traits< + std::vector<unsigned char>, id_blob> + { + public: + typedef std::vector<unsigned char> value_type; + typedef std::vector<unsigned char> 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<const unsigned char*> (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&); + }; + // // type_traits // |