From 37f5dcb1d34205aa4a395c1e21e853e363fd7f12 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 28 Jul 2011 09:45:36 +0200 Subject: Add value_traits specializations for std::vector This allows using it as a buffer for BLOB mapping. --- odb/sqlite/traits.cxx | 26 ++++++++++++++++++++++++++ odb/sqlite/traits.hxx | 30 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/odb/sqlite/traits.cxx b/odb/sqlite/traits.cxx index e73462a..96e201d 100644 --- a/odb/sqlite/traits.cxx +++ b/odb/sqlite/traits.cxx @@ -15,8 +15,10 @@ namespace odb { using details::buffer; + // // default_value_traits // + void default_value_traits:: set_image (buffer& b, size_t& n, @@ -33,8 +35,10 @@ namespace odb memcpy (b.data (), v.c_str (), n); } + // // default_value_traits // + void default_value_traits:: set_image (buffer& b, size_t& n, @@ -50,5 +54,27 @@ namespace odb if (n != 0) memcpy (b.data (), v, n); } + + // + // default_value_traits, id_blob> + // + + void default_value_traits, 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[0], n); + } } } diff --git a/odb/sqlite/traits.hxx b/odb/sqlite/traits.hxx index 2fdac04..8863fb1 100644 --- a/odb/sqlite/traits.hxx +++ b/odb/sqlite/traits.hxx @@ -9,6 +9,7 @@ #include #include +#include #include // std::size_t #include @@ -132,6 +133,35 @@ namespace odb const char*); }; + // std::vector (buffer) specialization. + // + template <> + struct LIBODB_SQLITE_EXPORT default_value_traits, id_blob> + { + 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&); + }; + // // type_traits // -- cgit v1.1