diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2010-09-21 19:55:14 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2010-09-21 19:55:14 +0200 |
commit | 35a280c5f8fa20e54bd56efd64fcdb9bd2f10dc4 (patch) | |
tree | 436c43a15fd99b85ff034b6c2106b6f2922a66fd | |
parent | 348efda0db31ca3a3ea0f7bf2eaa69985d3984b8 (diff) |
Redesign value_traits
-rw-r--r-- | odb/mysql/query.hxx | 42 | ||||
-rw-r--r-- | odb/mysql/traits.cxx | 48 | ||||
-rw-r--r-- | 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<T> v) { - append<T, value_traits<T>::image_id> (v); + append<T, image_traits<T>::image_id> (v); } template <typename T> explicit query (ref_bind<T> r) { - append<T, value_traits<T>::image_id> (r); + append<T, image_traits<T>::image_id> (r); } template <image_id_type ID> @@ -157,7 +157,7 @@ namespace odb query& operator+= (val_bind<T> v) { - append<T, value_traits<T>::image_id> (v); + append<T, image_traits<T>::image_id> (v); return *this; } @@ -165,7 +165,7 @@ namespace odb query& operator+= (ref_bind<T> r) { - append<T, value_traits<T>::image_id> (r); + append<T, image_traits<T>::image_id> (r); return *this; } @@ -1024,7 +1024,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T>::set_image (image_, dummy, v); + value_traits<T, signed char>::set_image (image_, dummy, v); } private: @@ -1056,7 +1056,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T>::set_image (image_, dummy, v); + value_traits<T, unsigned char>::set_image (image_, dummy, v); } private: @@ -1090,7 +1090,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T>::set_image (image_, dummy, v); + value_traits<T, short>::set_image (image_, dummy, v); } private: @@ -1122,7 +1122,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T>::set_image (image_, dummy, v); + value_traits<T, unsigned short>::set_image (image_, dummy, v); } private: @@ -1156,7 +1156,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T>::set_image (image_, dummy, v); + value_traits<T, int>::set_image (image_, dummy, v); } private: @@ -1188,7 +1188,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T>::set_image (image_, dummy, v); + value_traits<T, unsigned int>::set_image (image_, dummy, v); } private: @@ -1222,7 +1222,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T>::set_image (image_, dummy, v); + value_traits<T, long long>::set_image (image_, dummy, v); } private: @@ -1254,7 +1254,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T>::set_image (image_, dummy, v); + value_traits<T, unsigned long long>::set_image (image_, dummy, v); } private: @@ -1288,7 +1288,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T>::set_image (image_, dummy, v); + value_traits<T, float>::set_image (image_, dummy, v); } private: @@ -1322,7 +1322,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T>::set_image (image_, dummy, v); + value_traits<T, double>::set_image (image_, dummy, v); } private: @@ -1355,7 +1355,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T>::set_image (image_, dummy, v); + value_traits<T, MYSQL_TIME>::set_image (image_, dummy, v); } private: @@ -1388,7 +1388,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T>::set_image (image_, dummy, v); + value_traits<T, MYSQL_TIME>::set_image (image_, dummy, v); } private: @@ -1421,7 +1421,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T>::set_image (image_, dummy, v); + value_traits<T, MYSQL_TIME>::set_image (image_, dummy, v); } private: @@ -1454,7 +1454,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T>::set_image (image_, dummy, v); + value_traits<T, MYSQL_TIME>::set_image (image_, dummy, v); } private: @@ -1488,7 +1488,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T>::set_image (image_, dummy, v); + value_traits<T, short>::set_image (image_, dummy, v); } private: @@ -1524,7 +1524,7 @@ namespace odb { bool dummy; std::size_t size; - value_traits<T>::set_image (buffer_, size, dummy, v); + value_traits<T, details::buffer>::set_image (buffer_, size, dummy, v); size_ = static_cast<unsigned long> (size); } @@ -1562,7 +1562,7 @@ namespace odb { bool dummy; std::size_t size; - value_traits<T>::set_image (buffer_, size, dummy, v); + value_traits<T, details::buffer>::set_image (buffer_, size, dummy, v); size_ = static_cast<unsigned long> (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<string> + // value_traits<string, buffer> // - void value_traits<string>:: - 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<string>:: - set_image (details::buffer& b, + void value_traits<string, buffer>:: + set_image (buffer& b, size_t& n, bool& is_null, const string& v) @@ -51,28 +36,11 @@ namespace odb } // - // value_traits<const char*> + // value_traits<const char*, buffer> // - void value_traits<const char*>:: - 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<const char*>:: - set_image (details::buffer& b, + void value_traits<const char*, buffer>:: + 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 <typename T> - class generic_value_traits + template <typename T, typename I> + class value_traits { public: - typedef T type; typedef T value_type; + typedef T query_type; + typedef I image_type; - template <typename I> static void set_value (T& v, I i, bool is_null) { @@ -65,7 +65,6 @@ namespace odb v = T (); } - template <typename I> 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<std::string, details::buffer> + { + 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<const char*, details::buffer> + { + 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 <typename T> - class value_traits: public generic_value_traits<T> + struct default_image_id; + + template <typename T> + class image_traits { + public: + static const image_id_type image_id = default_image_id<T>::image_id; }; // Integral types. // template <> - class value_traits<bool>: public generic_value_traits<bool> + struct default_image_id<bool> { - public: static const image_id_type image_id = id_tiny; }; template <> - class value_traits<signed char>: public generic_value_traits<signed char> + struct default_image_id<signed char> { - public: static const image_id_type image_id = id_tiny; }; template <> - class value_traits<unsigned char>: public generic_value_traits<unsigned char> + struct default_image_id<unsigned char> { - public: static const image_id_type image_id = id_utiny; }; template <> - class value_traits<short>: public generic_value_traits<short> + struct default_image_id<short> { - public: static const image_id_type image_id = id_short; }; template <> - class value_traits<unsigned short>: public generic_value_traits<unsigned short> + struct default_image_id<unsigned short> { - public: static const image_id_type image_id = id_ushort; }; template <> - class value_traits<int>: public generic_value_traits<int> + struct default_image_id<int> { - public: static const image_id_type image_id = id_long; }; template <> - class value_traits<unsigned int>: public generic_value_traits<unsigned int> + struct default_image_id<unsigned int> { - public: static const image_id_type image_id = id_ulong; }; template <> - class value_traits<long>: public generic_value_traits<long> + struct default_image_id<long> { - public: static const image_id_type image_id = id_longlong; }; template <> - class value_traits<unsigned long>: public generic_value_traits<unsigned long> + struct default_image_id<unsigned long> { - public: static const image_id_type image_id = id_ulonglong; }; template <> - class value_traits<long long>: public generic_value_traits<long long> + struct default_image_id<long long> { - public: static const image_id_type image_id = id_longlong; }; template <> - class value_traits<unsigned long long>: public generic_value_traits<unsigned long long> + struct default_image_id<unsigned long long> { - public: static const image_id_type image_id = id_ulonglong; }; // Float types. // template <> - class value_traits<float>: public generic_value_traits<float> + struct default_image_id<float> { - public: static const image_id_type image_id = id_float; }; template <> - class value_traits<double>: public generic_value_traits<double> + struct default_image_id<double> { - public: static const image_id_type image_id = id_double; }; // String type. // template <> - class LIBODB_MYSQL_EXPORT value_traits<std::string> + struct default_image_id<std::string> { - 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<const char*> + struct default_image_id<const char*> { - 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*); }; } } |