From a1638d19a9088a3488b80163afe414a5355debd9 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 22 Sep 2010 18:40:58 +0200 Subject: Add database type id as value_traits template argument --- odb/mysql/query.hxx | 243 ++++++++++++++++++++++++++++++++++++++++++--------- odb/mysql/query.ixx | 4 +- odb/mysql/query.txx | 12 +-- odb/mysql/traits.cxx | 8 +- odb/mysql/traits.hxx | 134 +++++++++++++++++++--------- 5 files changed, 308 insertions(+), 93 deletions(-) diff --git a/odb/mysql/query.hxx b/odb/mysql/query.hxx index 5fd89ec..e0a6884 100644 --- a/odb/mysql/query.hxx +++ b/odb/mysql/query.hxx @@ -77,7 +77,7 @@ namespace odb // // - template + template struct query_column; class LIBODB_MYSQL_EXPORT query @@ -97,17 +97,17 @@ namespace odb explicit query (val_bind v) { - append::image_id> (v); + append::db_type_id> (v); } template explicit query (ref_bind r) { - append::image_id> (r); + append::db_type_id> (r); } - template + template query (const query_column&); query (const query&); @@ -157,7 +157,7 @@ namespace odb query& operator+= (val_bind v) { - append::image_id> (v); + append::db_type_id> (v); return *this; } @@ -165,16 +165,16 @@ namespace odb query& operator+= (ref_bind r) { - append::image_id> (r); + append::db_type_id> (r); return *this; } public: - template + template void append (val_bind); - template + template void append (ref_bind); @@ -338,7 +338,7 @@ namespace odb const T& type_instance (); - template + template struct query_column { explicit @@ -904,9 +904,9 @@ namespace odb // Column comparison. // public: - template + template query - operator== (const query_column& c) const + operator== (const query_column& c) const { // We can compare columns only if we can compare their C++ types. // @@ -918,9 +918,9 @@ namespace odb return q; } - template + template query - operator!= (const query_column& c) const + operator!= (const query_column& c) const { // We can compare columns only if we can compare their C++ types. // @@ -932,9 +932,9 @@ namespace odb return q; } - template + template query - operator< (const query_column& c) const + operator< (const query_column& c) const { // We can compare columns only if we can compare their C++ types. // @@ -946,9 +946,9 @@ namespace odb return q; } - template + template query - operator> (const query_column& c) const + operator> (const query_column& c) const { // We can compare columns only if we can compare their C++ types. // @@ -960,9 +960,9 @@ namespace odb return q; } - template + template query - operator<= (const query_column& c) const + operator<= (const query_column& c) const { // We can compare columns only if we can compare their C++ types. // @@ -974,9 +974,9 @@ namespace odb return q; } - template + template query - operator>= (const query_column& c) const + operator>= (const query_column& c) const { // We can compare columns only if we can compare their C++ types. // @@ -994,7 +994,7 @@ namespace odb // // - template + template struct query_param_impl; // TINY @@ -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,8 @@ 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 +1157,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 +1189,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 +1223,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 +1255,8 @@ 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 +1290,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,13 +1324,52 @@ namespace odb init (const T& v) { bool dummy; - value_traits::set_image (image_, dummy, v); + value_traits::set_image (image_, dummy, v); } private: double image_; }; + // DECIMAL + // + template + struct query_param_impl: query_param + { + query_param_impl (ref_bind r) : query_param (&r.ref) {} + query_param_impl (val_bind v) : query_param (0) {init (v.val);} + + virtual void + init () + { + init (*static_cast (value_)); + } + + virtual void + bind (MYSQL_BIND* b) + { + b->buffer_type = MYSQL_TYPE_NEWDECIMAL; + b->buffer = buffer_.data (); + b->buffer_length = static_cast (buffer_.capacity ()); + b->length = &size_; + } + + private: + void + init (const T& v) + { + bool dummy; + std::size_t size; + value_traits::set_image ( + buffer_, size, dummy, v); + size_ = static_cast (size); + } + + private: + details::buffer buffer_; + unsigned long size_; + }; + // DATE // template @@ -1355,7 +1396,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 +1429,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 +1462,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 +1495,8 @@ 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 +1530,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 +1566,8 @@ 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 +1605,127 @@ 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); + } + + private: + details::buffer buffer_; + unsigned long size_; + }; + + // BIT + // + template + struct query_param_impl: query_param + { + query_param_impl (ref_bind r) : query_param (&r.ref) {} + query_param_impl (val_bind v) : query_param (0) {init (v.val);} + + virtual void + init () + { + init (*static_cast (value_)); + } + + virtual void + bind (MYSQL_BIND* b) + { + b->buffer_type = MYSQL_TYPE_BLOB; + b->buffer = buffer_; + b->buffer_length = static_cast (sizeof (buffer_)); + b->length = &size_; + } + + private: + void + init (const T& v) + { + bool dummy; + std::size_t size; + value_traits::set_image ( + buffer_, sizeof (buffer_), size, dummy, v); + size_ = static_cast (size); + } + + private: + // Max 64 bit. + // + unsigned char buffer_[8]; + unsigned long size_; + }; + + // ENUM + // + template + struct query_param_impl: query_param + { + query_param_impl (ref_bind r) : query_param (&r.ref) {} + query_param_impl (val_bind v) : query_param (0) {init (v.val);} + + virtual void + init () + { + init (*static_cast (value_)); + } + + virtual void + bind (MYSQL_BIND* b) + { + b->buffer_type = MYSQL_TYPE_STRING; + b->buffer = buffer_.data (); + b->buffer_length = static_cast (buffer_.capacity ()); + b->length = &size_; + } + + private: + void + init (const T& v) + { + bool dummy; + std::size_t size; + value_traits::set_image ( + buffer_, size, dummy, v); + size_ = static_cast (size); + } + + private: + details::buffer buffer_; + unsigned long size_; + }; + + // SET + // + template + struct query_param_impl: query_param + { + query_param_impl (ref_bind r) : query_param (&r.ref) {} + query_param_impl (val_bind v) : query_param (0) {init (v.val);} + + virtual void + init () + { + init (*static_cast (value_)); + } + + virtual void + bind (MYSQL_BIND* b) + { + b->buffer_type = MYSQL_TYPE_STRING; + b->buffer = buffer_.data (); + b->buffer_length = static_cast (buffer_.capacity ()); + b->length = &size_; + } + + private: + void + init (const T& v) + { + bool dummy; + std::size_t size; + value_traits::set_image ( + buffer_, size, dummy, v); size_ = static_cast (size); } @@ -1614,7 +1777,7 @@ namespace odb { } - template + template query (const mysql::query_column& qc) : object_traits::query_type (qc) { diff --git a/odb/mysql/query.ixx b/odb/mysql/query.ixx index ca4c60c..44233a9 100644 --- a/odb/mysql/query.ixx +++ b/odb/mysql/query.ixx @@ -7,7 +7,7 @@ namespace odb { namespace mysql { - template + template inline void query:: append (val_bind v) { @@ -16,7 +16,7 @@ namespace odb new (details::shared) query_param_impl (v))); } - template + template inline void query:: append (ref_bind r) { diff --git a/odb/mysql/query.txx b/odb/mysql/query.txx index dda0e72..5063082 100644 --- a/odb/mysql/query.txx +++ b/odb/mysql/query.txx @@ -10,7 +10,7 @@ namespace odb // query // - template + template query:: query (const query_column& c) : clause_ (c.name ()) @@ -20,7 +20,7 @@ namespace odb // query_column // - template + template query query_column:: in (const T& v1, const T& v2) const { @@ -33,7 +33,7 @@ namespace odb return q; } - template + template query query_column:: in (const T& v1, const T& v2, const T& v3) const { @@ -48,7 +48,7 @@ namespace odb return q; } - template + template query query_column:: in (const T& v1, const T& v2, const T& v3, const T& v4) const { @@ -65,7 +65,7 @@ namespace odb return q; } - template + template query query_column:: in (const T& v1, const T& v2, const T& v3, const T& v4, const T& v5) const { @@ -84,7 +84,7 @@ namespace odb return q; } - template + template template query query_column:: in_range (I begin, I end) const diff --git a/odb/mysql/traits.cxx b/odb/mysql/traits.cxx index f701978..d4eb4b3 100644 --- a/odb/mysql/traits.cxx +++ b/odb/mysql/traits.cxx @@ -16,10 +16,10 @@ namespace odb using details::buffer; // - // value_traits + // string_value_traits // - void value_traits:: + void string_value_traits:: set_image (buffer& b, size_t& n, bool& is_null, @@ -36,10 +36,10 @@ namespace odb } // - // value_traits + // c_string_value_traits // - void value_traits:: + void c_string_value_traits:: set_image (buffer& b, size_t& n, bool& is_null, diff --git a/odb/mysql/traits.hxx b/odb/mysql/traits.hxx index c708726..e823eda 100644 --- a/odb/mysql/traits.hxx +++ b/odb/mysql/traits.hxx @@ -24,7 +24,7 @@ namespace odb { namespace mysql { - enum image_id_type + enum database_type_id { id_tiny, id_utiny, @@ -37,6 +37,7 @@ namespace odb id_float, id_double, + id_decimal, id_date, id_time, @@ -45,10 +46,14 @@ namespace odb id_year, id_string, - id_blob + id_blob, + + id_bit, + id_enum, + id_set }; - template + template class value_traits { public: @@ -75,8 +80,7 @@ namespace odb // std::string specialization. // - template <> - class LIBODB_MYSQL_EXPORT value_traits + class LIBODB_MYSQL_EXPORT string_value_traits { public: typedef std::string value_type; @@ -102,14 +106,37 @@ namespace odb const std::string&); }; + template <> + class LIBODB_MYSQL_EXPORT value_traits< + std::string, details::buffer, id_string>: public string_value_traits + { + }; + + template <> + class LIBODB_MYSQL_EXPORT value_traits< + std::string, details::buffer, id_decimal>: public string_value_traits + { + }; + + template <> + class LIBODB_MYSQL_EXPORT value_traits< + std::string, details::buffer, id_enum>: public string_value_traits + { + }; + + template <> + class LIBODB_MYSQL_EXPORT value_traits< + std::string, details::buffer, id_set>: public string_value_traits + { + }; + // 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 + class LIBODB_MYSQL_EXPORT c_string_value_traits { public: typedef const char* value_type; @@ -123,114 +150,139 @@ namespace odb const char*); }; + template <> + class LIBODB_MYSQL_EXPORT value_traits< + const char*, details::buffer, id_string>: public c_string_value_traits + { + }; + + template <> + class LIBODB_MYSQL_EXPORT value_traits< + const char*, details::buffer, id_decimal>: public c_string_value_traits + { + }; + + template <> + class LIBODB_MYSQL_EXPORT value_traits< + const char*, details::buffer, id_enum>: public c_string_value_traits + { + }; + + template <> + class LIBODB_MYSQL_EXPORT value_traits< + const char*, details::buffer, id_set>: public c_string_value_traits + { + }; + // - // image_traits + // type_traits // template - struct default_image_id; + struct default_type_traits; template - class image_traits + class type_traits { public: - static const image_id_type image_id = default_image_id::image_id; + static const database_type_id db_type_id = + default_type_traits::db_type_id; }; // Integral types. // template <> - struct default_image_id + struct default_type_traits { - static const image_id_type image_id = id_tiny; + static const database_type_id db_type_id = id_tiny; }; template <> - struct default_image_id + struct default_type_traits { - static const image_id_type image_id = id_tiny; + static const database_type_id db_type_id = id_tiny; }; template <> - struct default_image_id + struct default_type_traits { - static const image_id_type image_id = id_utiny; + static const database_type_id db_type_id = id_utiny; }; template <> - struct default_image_id + struct default_type_traits { - static const image_id_type image_id = id_short; + static const database_type_id db_type_id = id_short; }; template <> - struct default_image_id + struct default_type_traits { - static const image_id_type image_id = id_ushort; + static const database_type_id db_type_id = id_ushort; }; template <> - struct default_image_id + struct default_type_traits { - static const image_id_type image_id = id_long; + static const database_type_id db_type_id = id_long; }; template <> - struct default_image_id + struct default_type_traits { - static const image_id_type image_id = id_ulong; + static const database_type_id db_type_id = id_ulong; }; template <> - struct default_image_id + struct default_type_traits { - static const image_id_type image_id = id_longlong; + static const database_type_id db_type_id = id_longlong; }; template <> - struct default_image_id + struct default_type_traits { - static const image_id_type image_id = id_ulonglong; + static const database_type_id db_type_id = id_ulonglong; }; template <> - struct default_image_id + struct default_type_traits { - static const image_id_type image_id = id_longlong; + static const database_type_id db_type_id = id_longlong; }; template <> - struct default_image_id + struct default_type_traits { - static const image_id_type image_id = id_ulonglong; + static const database_type_id db_type_id = id_ulonglong; }; // Float types. // template <> - struct default_image_id + struct default_type_traits { - static const image_id_type image_id = id_float; + static const database_type_id db_type_id = id_float; }; template <> - struct default_image_id + struct default_type_traits { - static const image_id_type image_id = id_double; + static const database_type_id db_type_id = id_double; }; // String type. // template <> - struct default_image_id + struct default_type_traits { - static const image_id_type image_id = id_string; + static const database_type_id db_type_id = id_string; }; template <> - struct default_image_id + struct default_type_traits { - static const image_id_type image_id = id_string; + static const database_type_id db_type_id = id_string; }; } } -- cgit v1.1