From 68f64ea9543f44b71f580f165bff52286e005c79 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 3 Nov 2011 08:16:50 +0200 Subject: Add support for mapping std::vector to BLOB types --- odb/mysql/traits.cxx | 22 ++++++++++++++++++++++ odb/mysql/traits.hxx | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/odb/mysql/traits.cxx b/odb/mysql/traits.cxx index 7b901c5..0727861 100644 --- a/odb/mysql/traits.cxx +++ b/odb/mysql/traits.cxx @@ -76,5 +76,27 @@ namespace odb if (n != 0) memcpy (b.data (), &v.front (), 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.front (), n); + } } } diff --git a/odb/mysql/traits.hxx b/odb/mysql/traits.hxx index c508624..7994d59 100644 --- a/odb/mysql/traits.hxx +++ b/odb/mysql/traits.hxx @@ -519,6 +519,40 @@ namespace odb const value_type&); }; + // std::vector (buffer) specialization. + // + template <> + struct LIBODB_MYSQL_EXPORT default_value_traits< + std::vector, 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) + { + const unsigned char* d ( + reinterpret_cast (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 // -- cgit v1.1