From e8162bdac61ac49ae30cdfeae9f2bd3d230df809 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/mysql/traits.cxx | 22 ++++++++++++++++++++++ odb/mysql/traits.hxx | 30 ++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/odb/mysql/traits.cxx b/odb/mysql/traits.cxx index c0686b5..38cffa8 100644 --- a/odb/mysql/traits.cxx +++ b/odb/mysql/traits.cxx @@ -54,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/mysql/traits.hxx b/odb/mysql/traits.hxx index 7f96c0b..b5c9830 100644 --- a/odb/mysql/traits.hxx +++ b/odb/mysql/traits.hxx @@ -9,6 +9,7 @@ #include #include +#include #include // std::size_t #include @@ -289,6 +290,35 @@ namespace odb { }; + // std::vector (buffer) specialization. + // + template <> + struct LIBODB_MYSQL_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