From 35a280c5f8fa20e54bd56efd64fcdb9bd2f10dc4 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 21 Sep 2010 19:55:14 +0200 Subject: Redesign value_traits --- odb/mysql/query.hxx | 42 +++++++------- odb/mysql/traits.cxx | 48 +++------------- odb/mysql/traits.hxx | 158 +++++++++++++++++++++++++-------------------------- 3 files changed, 108 insertions(+), 140 deletions(-) diff --git a/odb/mysql/query.hxx b/odb/mysql/query.hxx index 99d9d38..5fd89ec 100644 --- a/odb/mysql/query.hxx +++ b/odb/mysql/query.hxx @@ -97,14 +97,14 @@ namespace odb explicit query (val_bind v) { - append::image_id> (v); + append::image_id> (v); } template explicit query (ref_bind r) { - append::image_id> (r); + append::image_id> (r); } template @@ -157,7 +157,7 @@ namespace odb query& operator+= (val_bind v) { - append::image_id> (v); + append::image_id> (v); return *this; } @@ -165,7 +165,7 @@ namespace odb query& operator+= (ref_bind r) { - append::image_id> (r); + append::image_id> (r); return *this; } @@ -1024,7 +1024,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: @@ -1056,7 +1056,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: @@ -1090,7 +1090,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: @@ -1122,7 +1122,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: @@ -1156,7 +1156,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: @@ -1188,7 +1188,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: @@ -1222,7 +1222,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: @@ -1254,7 +1254,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: @@ -1288,7 +1288,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: @@ -1322,7 +1322,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: @@ -1355,7 +1355,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: @@ -1388,7 +1388,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: @@ -1421,7 +1421,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: @@ -1454,7 +1454,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: @@ -1488,7 +1488,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: @@ -1524,7 +1524,7 @@ namespace odb { bool dummy; std::size_t size; - value_traits::set_image (buffer_, size, dummy, v); + value_traits::set_image (buffer_, size, dummy, v); size_ = static_cast (size); } @@ -1562,7 +1562,7 @@ namespace odb { bool dummy; std::size_t size; - value_traits::set_image (buffer_, size, dummy, v); + value_traits::set_image (buffer_, size, dummy, v); size_ = static_cast (size); } diff --git a/odb/mysql/traits.cxx b/odb/mysql/traits.cxx index f90cc0b..f701978 100644 --- a/odb/mysql/traits.cxx +++ b/odb/mysql/traits.cxx @@ -13,29 +13,14 @@ namespace odb { namespace mysql { + using details::buffer; + // - // value_traits + // value_traits // - void value_traits:: - set_image (char* s, - size_t c, - size_t& n, - bool& is_null, - const string& v) - { - is_null = false; - n = v.size (); - - if (n > c) - n = c; - - if (n != 0) - memcpy (s, v.c_str (), n); - } - - void value_traits:: - set_image (details::buffer& b, + void value_traits:: + set_image (buffer& b, size_t& n, bool& is_null, const string& v) @@ -51,28 +36,11 @@ namespace odb } // - // value_traits + // value_traits // - void value_traits:: - set_image (char* s, - size_t c, - size_t& n, - bool& is_null, - const char* v) - { - is_null = false; - n = strlen (v); - - if (n > c) - n = c; - - if (n != 0) - memcpy (s, v, n); - } - - void value_traits:: - set_image (details::buffer& b, + void value_traits:: + set_image (buffer& b, size_t& n, bool& is_null, const char* v) diff --git a/odb/mysql/traits.hxx b/odb/mysql/traits.hxx index f05e04e..c708726 100644 --- a/odb/mysql/traits.hxx +++ b/odb/mysql/traits.hxx @@ -48,14 +48,14 @@ namespace odb id_blob }; - template - class generic_value_traits + template + class value_traits { public: - typedef T type; typedef T value_type; + typedef T query_type; + typedef I image_type; - template static void set_value (T& v, I i, bool is_null) { @@ -65,7 +65,6 @@ namespace odb v = T (); } - template static void set_image (I& i, bool& is_null, T v) { @@ -74,163 +73,164 @@ namespace odb } }; + // std::string specialization. + // + template <> + class LIBODB_MYSQL_EXPORT value_traits + { + public: + typedef std::string value_type; + typedef std::string query_type; + typedef details::buffer image_type; + + static void + set_value (std::string& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + if (!is_null) + v.assign (b.data (), n); + else + v.erase (); + } + + static void + set_image (details::buffer&, + std::size_t& n, + bool& is_null, + const std::string&); + }; + + // const char* specialization + // + // Specialization for const char* which only supports initialization + // of an image from the value but not the other way around. This way + // we can pass such values to the queries. + // + template <> + class LIBODB_MYSQL_EXPORT value_traits + { + public: + typedef const char* value_type; + typedef const char* query_type; + typedef details::buffer image_type; + + static void + set_image (details::buffer&, + std::size_t& n, + bool& is_null, + const char*); + }; + + // + // image_traits + // + template - class value_traits: public generic_value_traits + struct default_image_id; + + template + class image_traits { + public: + static const image_id_type image_id = default_image_id::image_id; }; // Integral types. // template <> - class value_traits: public generic_value_traits + struct default_image_id { - public: static const image_id_type image_id = id_tiny; }; template <> - class value_traits: public generic_value_traits + struct default_image_id { - public: static const image_id_type image_id = id_tiny; }; template <> - class value_traits: public generic_value_traits + struct default_image_id { - public: static const image_id_type image_id = id_utiny; }; template <> - class value_traits: public generic_value_traits + struct default_image_id { - public: static const image_id_type image_id = id_short; }; template <> - class value_traits: public generic_value_traits + struct default_image_id { - public: static const image_id_type image_id = id_ushort; }; template <> - class value_traits: public generic_value_traits + struct default_image_id { - public: static const image_id_type image_id = id_long; }; template <> - class value_traits: public generic_value_traits + struct default_image_id { - public: static const image_id_type image_id = id_ulong; }; template <> - class value_traits: public generic_value_traits + struct default_image_id { - public: static const image_id_type image_id = id_longlong; }; template <> - class value_traits: public generic_value_traits + struct default_image_id { - public: static const image_id_type image_id = id_ulonglong; }; template <> - class value_traits: public generic_value_traits + struct default_image_id { - public: static const image_id_type image_id = id_longlong; }; template <> - class value_traits: public generic_value_traits + struct default_image_id { - public: static const image_id_type image_id = id_ulonglong; }; // Float types. // template <> - class value_traits: public generic_value_traits + struct default_image_id { - public: static const image_id_type image_id = id_float; }; template <> - class value_traits: public generic_value_traits + struct default_image_id { - public: static const image_id_type image_id = id_double; }; // String type. // template <> - class LIBODB_MYSQL_EXPORT value_traits + struct default_image_id { - public: - typedef std::string type; - typedef std::string value_type; static const image_id_type image_id = id_string; - - static void - set_value (std::string& v, const char* s, std::size_t n, bool is_null) - { - if (!is_null) - v.assign (s, n); - else - v.erase (); - } - - static void - set_image (char*, - std::size_t c, - std::size_t& n, - bool& is_null, - const std::string&); - - static void - set_image (details::buffer&, - std::size_t& n, - bool& is_null, - const std::string&); }; - // Specialization for const char* which only supports initialization - // of an image from the value but not the other way around. This way - // we can pass such values to the queries. - // template <> - class LIBODB_MYSQL_EXPORT value_traits + struct default_image_id { - public: - typedef const char* type; - typedef const char* value_type; static const image_id_type image_id = id_string; - - static void - set_image (char*, - std::size_t c, - std::size_t& n, - bool& is_null, - const char*); - - static void - set_image (details::buffer&, - std::size_t& n, - bool& is_null, - const char*); }; } } -- cgit v1.1