diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2010-09-22 18:40:58 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2010-09-22 18:40:58 +0200 |
commit | a1638d19a9088a3488b80163afe414a5355debd9 (patch) | |
tree | 4496e3308ed4983ecd8b64ccef8b9473193952f9 | |
parent | 35a280c5f8fa20e54bd56efd64fcdb9bd2f10dc4 (diff) |
Add database type id as value_traits template argument
-rw-r--r-- | odb/mysql/query.hxx | 243 | ||||
-rw-r--r-- | odb/mysql/query.ixx | 4 | ||||
-rw-r--r-- | odb/mysql/query.txx | 12 | ||||
-rw-r--r-- | odb/mysql/traits.cxx | 8 | ||||
-rw-r--r-- | 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 <typename T, image_id_type ID> + template <typename T, database_type_id ID> struct query_column; class LIBODB_MYSQL_EXPORT query @@ -97,17 +97,17 @@ namespace odb explicit query (val_bind<T> v) { - append<T, image_traits<T>::image_id> (v); + append<T, type_traits<T>::db_type_id> (v); } template <typename T> explicit query (ref_bind<T> r) { - append<T, image_traits<T>::image_id> (r); + append<T, type_traits<T>::db_type_id> (r); } - template <image_id_type ID> + template <database_type_id ID> query (const query_column<bool, ID>&); query (const query&); @@ -157,7 +157,7 @@ namespace odb query& operator+= (val_bind<T> v) { - append<T, image_traits<T>::image_id> (v); + append<T, type_traits<T>::db_type_id> (v); return *this; } @@ -165,16 +165,16 @@ namespace odb query& operator+= (ref_bind<T> r) { - append<T, image_traits<T>::image_id> (r); + append<T, type_traits<T>::db_type_id> (r); return *this; } public: - template <typename T, image_id_type ID> + template <typename T, database_type_id ID> void append (val_bind<T>); - template <typename T, image_id_type ID> + template <typename T, database_type_id ID> void append (ref_bind<T>); @@ -338,7 +338,7 @@ namespace odb const T& type_instance (); - template <typename T, image_id_type ID> + template <typename T, database_type_id ID> struct query_column { explicit @@ -904,9 +904,9 @@ namespace odb // Column comparison. // public: - template <typename T2, image_id_type I2> + template <typename T2, database_type_id ID2> query - operator== (const query_column<T2, I2>& c) const + operator== (const query_column<T2, ID2>& c) const { // We can compare columns only if we can compare their C++ types. // @@ -918,9 +918,9 @@ namespace odb return q; } - template <typename T2, image_id_type I2> + template <typename T2, database_type_id ID2> query - operator!= (const query_column<T2, I2>& c) const + operator!= (const query_column<T2, ID2>& c) const { // We can compare columns only if we can compare their C++ types. // @@ -932,9 +932,9 @@ namespace odb return q; } - template <typename T2, image_id_type I2> + template <typename T2, database_type_id ID2> query - operator< (const query_column<T2, I2>& c) const + operator< (const query_column<T2, ID2>& c) const { // We can compare columns only if we can compare their C++ types. // @@ -946,9 +946,9 @@ namespace odb return q; } - template <typename T2, image_id_type I2> + template <typename T2, database_type_id ID2> query - operator> (const query_column<T2, I2>& c) const + operator> (const query_column<T2, ID2>& c) const { // We can compare columns only if we can compare their C++ types. // @@ -960,9 +960,9 @@ namespace odb return q; } - template <typename T2, image_id_type I2> + template <typename T2, database_type_id ID2> query - operator<= (const query_column<T2, I2>& c) const + operator<= (const query_column<T2, ID2>& c) const { // We can compare columns only if we can compare their C++ types. // @@ -974,9 +974,9 @@ namespace odb return q; } - template <typename T2, image_id_type I2> + template <typename T2, database_type_id ID2> query - operator>= (const query_column<T2, I2>& c) const + operator>= (const query_column<T2, ID2>& c) const { // We can compare columns only if we can compare their C++ types. // @@ -994,7 +994,7 @@ namespace odb // // - template <typename T, image_id_type> + template <typename T, database_type_id> struct query_param_impl; // TINY @@ -1024,7 +1024,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T, signed char>::set_image (image_, dummy, v); + value_traits<T, signed char, id_tiny>::set_image (image_, dummy, v); } private: @@ -1056,7 +1056,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T, unsigned char>::set_image (image_, dummy, v); + value_traits<T, unsigned char, id_utiny>::set_image (image_, dummy, v); } private: @@ -1090,7 +1090,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T, short>::set_image (image_, dummy, v); + value_traits<T, short, id_short>::set_image (image_, dummy, v); } private: @@ -1122,7 +1122,8 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T, unsigned short>::set_image (image_, dummy, v); + value_traits<T, unsigned short, id_ushort>::set_image ( + image_, dummy, v); } private: @@ -1156,7 +1157,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T, int>::set_image (image_, dummy, v); + value_traits<T, int, id_long>::set_image (image_, dummy, v); } private: @@ -1188,7 +1189,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T, unsigned int>::set_image (image_, dummy, v); + value_traits<T, unsigned int, id_ulong>::set_image (image_, dummy, v); } private: @@ -1222,7 +1223,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T, long long>::set_image (image_, dummy, v); + value_traits<T, long long, id_longlong>::set_image (image_, dummy, v); } private: @@ -1254,7 +1255,8 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T, unsigned long long>::set_image (image_, dummy, v); + value_traits<T, unsigned long long, id_ulonglong>::set_image ( + image_, dummy, v); } private: @@ -1288,7 +1290,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T, float>::set_image (image_, dummy, v); + value_traits<T, float, id_float>::set_image (image_, dummy, v); } private: @@ -1322,13 +1324,52 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T, double>::set_image (image_, dummy, v); + value_traits<T, double, id_double>::set_image (image_, dummy, v); } private: double image_; }; + // DECIMAL + // + template <typename T> + struct query_param_impl<T, id_decimal>: query_param + { + query_param_impl (ref_bind<T> r) : query_param (&r.ref) {} + query_param_impl (val_bind<T> v) : query_param (0) {init (v.val);} + + virtual void + init () + { + init (*static_cast<const T*> (value_)); + } + + virtual void + bind (MYSQL_BIND* b) + { + b->buffer_type = MYSQL_TYPE_NEWDECIMAL; + b->buffer = buffer_.data (); + b->buffer_length = static_cast<unsigned long> (buffer_.capacity ()); + b->length = &size_; + } + + private: + void + init (const T& v) + { + bool dummy; + std::size_t size; + value_traits<T, details::buffer, id_decimal>::set_image ( + buffer_, size, dummy, v); + size_ = static_cast<unsigned long> (size); + } + + private: + details::buffer buffer_; + unsigned long size_; + }; + // DATE // template <typename T> @@ -1355,7 +1396,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T, MYSQL_TIME>::set_image (image_, dummy, v); + value_traits<T, MYSQL_TIME, id_date>::set_image (image_, dummy, v); } private: @@ -1388,7 +1429,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T, MYSQL_TIME>::set_image (image_, dummy, v); + value_traits<T, MYSQL_TIME, id_time>::set_image (image_, dummy, v); } private: @@ -1421,7 +1462,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T, MYSQL_TIME>::set_image (image_, dummy, v); + value_traits<T, MYSQL_TIME, id_datetime>::set_image (image_, dummy, v); } private: @@ -1454,7 +1495,8 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T, MYSQL_TIME>::set_image (image_, dummy, v); + value_traits<T, MYSQL_TIME, id_timestamp>::set_image ( + image_, dummy, v); } private: @@ -1488,7 +1530,7 @@ namespace odb init (const T& v) { bool dummy; - value_traits<T, short>::set_image (image_, dummy, v); + value_traits<T, short, id_year>::set_image (image_, dummy, v); } private: @@ -1524,7 +1566,8 @@ namespace odb { bool dummy; std::size_t size; - value_traits<T, details::buffer>::set_image (buffer_, size, dummy, v); + value_traits<T, details::buffer, id_string>::set_image ( + buffer_, size, dummy, v); size_ = static_cast<unsigned long> (size); } @@ -1562,7 +1605,127 @@ namespace odb { bool dummy; std::size_t size; - value_traits<T, details::buffer>::set_image (buffer_, size, dummy, v); + value_traits<T, details::buffer, id_blob>::set_image ( + buffer_, size, dummy, v); + size_ = static_cast<unsigned long> (size); + } + + private: + details::buffer buffer_; + unsigned long size_; + }; + + // BIT + // + template <typename T> + struct query_param_impl<T, id_bit>: query_param + { + query_param_impl (ref_bind<T> r) : query_param (&r.ref) {} + query_param_impl (val_bind<T> v) : query_param (0) {init (v.val);} + + virtual void + init () + { + init (*static_cast<const T*> (value_)); + } + + virtual void + bind (MYSQL_BIND* b) + { + b->buffer_type = MYSQL_TYPE_BLOB; + b->buffer = buffer_; + b->buffer_length = static_cast<unsigned long> (sizeof (buffer_)); + b->length = &size_; + } + + private: + void + init (const T& v) + { + bool dummy; + std::size_t size; + value_traits<T, details::buffer, id_bit>::set_image ( + buffer_, sizeof (buffer_), size, dummy, v); + size_ = static_cast<unsigned long> (size); + } + + private: + // Max 64 bit. + // + unsigned char buffer_[8]; + unsigned long size_; + }; + + // ENUM + // + template <typename T> + struct query_param_impl<T, id_enum>: query_param + { + query_param_impl (ref_bind<T> r) : query_param (&r.ref) {} + query_param_impl (val_bind<T> v) : query_param (0) {init (v.val);} + + virtual void + init () + { + init (*static_cast<const T*> (value_)); + } + + virtual void + bind (MYSQL_BIND* b) + { + b->buffer_type = MYSQL_TYPE_STRING; + b->buffer = buffer_.data (); + b->buffer_length = static_cast<unsigned long> (buffer_.capacity ()); + b->length = &size_; + } + + private: + void + init (const T& v) + { + bool dummy; + std::size_t size; + value_traits<T, details::buffer, id_enum>::set_image ( + buffer_, size, dummy, v); + size_ = static_cast<unsigned long> (size); + } + + private: + details::buffer buffer_; + unsigned long size_; + }; + + // SET + // + template <typename T> + struct query_param_impl<T, id_set>: query_param + { + query_param_impl (ref_bind<T> r) : query_param (&r.ref) {} + query_param_impl (val_bind<T> v) : query_param (0) {init (v.val);} + + virtual void + init () + { + init (*static_cast<const T*> (value_)); + } + + virtual void + bind (MYSQL_BIND* b) + { + b->buffer_type = MYSQL_TYPE_STRING; + b->buffer = buffer_.data (); + b->buffer_length = static_cast<unsigned long> (buffer_.capacity ()); + b->length = &size_; + } + + private: + void + init (const T& v) + { + bool dummy; + std::size_t size; + value_traits<T, details::buffer, id_set>::set_image ( + buffer_, size, dummy, v); size_ = static_cast<unsigned long> (size); } @@ -1614,7 +1777,7 @@ namespace odb { } - template <mysql::image_id_type ID> + template <mysql::database_type_id ID> query (const mysql::query_column<bool, ID>& qc) : object_traits<T>::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 <typename T, image_id_type ID> + template <typename T, database_type_id ID> inline void query:: append (val_bind<T> v) { @@ -16,7 +16,7 @@ namespace odb new (details::shared) query_param_impl<T, ID> (v))); } - template <typename T, image_id_type ID> + template <typename T, database_type_id ID> inline void query:: append (ref_bind<T> 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 <image_id_type ID> + template <database_type_id ID> query:: query (const query_column<bool, ID>& c) : clause_ (c.name ()) @@ -20,7 +20,7 @@ namespace odb // query_column // - template <typename T, image_id_type ID> + template <typename T, database_type_id ID> query query_column<T, ID>:: in (const T& v1, const T& v2) const { @@ -33,7 +33,7 @@ namespace odb return q; } - template <typename T, image_id_type ID> + template <typename T, database_type_id ID> query query_column<T, ID>:: in (const T& v1, const T& v2, const T& v3) const { @@ -48,7 +48,7 @@ namespace odb return q; } - template <typename T, image_id_type ID> + template <typename T, database_type_id ID> query query_column<T, ID>:: in (const T& v1, const T& v2, const T& v3, const T& v4) const { @@ -65,7 +65,7 @@ namespace odb return q; } - template <typename T, image_id_type ID> + template <typename T, database_type_id ID> query query_column<T, ID>:: 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 <typename T, image_id_type ID> + template <typename T, database_type_id ID> template <typename I> query query_column<T, ID>:: 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, buffer> + // string_value_traits // - void value_traits<string, buffer>:: + void string_value_traits:: set_image (buffer& b, size_t& n, bool& is_null, @@ -36,10 +36,10 @@ namespace odb } // - // value_traits<const char*, buffer> + // c_string_value_traits // - void value_traits<const char*, buffer>:: + 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 <typename T, typename I> + template <typename T, typename I, database_type_id> class value_traits { public: @@ -75,8 +80,7 @@ namespace odb // std::string specialization. // - template <> - class LIBODB_MYSQL_EXPORT value_traits<std::string, details::buffer> + 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<const char*, details::buffer> + 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 <typename T> - struct default_image_id; + struct default_type_traits; template <typename T> - class image_traits + class type_traits { public: - static const image_id_type image_id = default_image_id<T>::image_id; + static const database_type_id db_type_id = + default_type_traits<T>::db_type_id; }; // Integral types. // template <> - struct default_image_id<bool> + struct default_type_traits<bool> { - static const image_id_type image_id = id_tiny; + static const database_type_id db_type_id = id_tiny; }; template <> - struct default_image_id<signed char> + struct default_type_traits<signed char> { - static const image_id_type image_id = id_tiny; + static const database_type_id db_type_id = id_tiny; }; template <> - struct default_image_id<unsigned char> + struct default_type_traits<unsigned char> { - static const image_id_type image_id = id_utiny; + static const database_type_id db_type_id = id_utiny; }; template <> - struct default_image_id<short> + struct default_type_traits<short> { - static const image_id_type image_id = id_short; + static const database_type_id db_type_id = id_short; }; template <> - struct default_image_id<unsigned short> + struct default_type_traits<unsigned short> { - static const image_id_type image_id = id_ushort; + static const database_type_id db_type_id = id_ushort; }; template <> - struct default_image_id<int> + struct default_type_traits<int> { - static const image_id_type image_id = id_long; + static const database_type_id db_type_id = id_long; }; template <> - struct default_image_id<unsigned int> + struct default_type_traits<unsigned int> { - static const image_id_type image_id = id_ulong; + static const database_type_id db_type_id = id_ulong; }; template <> - struct default_image_id<long> + struct default_type_traits<long> { - static const image_id_type image_id = id_longlong; + static const database_type_id db_type_id = id_longlong; }; template <> - struct default_image_id<unsigned long> + struct default_type_traits<unsigned long> { - static const image_id_type image_id = id_ulonglong; + static const database_type_id db_type_id = id_ulonglong; }; template <> - struct default_image_id<long long> + struct default_type_traits<long long> { - static const image_id_type image_id = id_longlong; + static const database_type_id db_type_id = id_longlong; }; template <> - struct default_image_id<unsigned long long> + struct default_type_traits<unsigned long long> { - 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<float> + struct default_type_traits<float> { - static const image_id_type image_id = id_float; + static const database_type_id db_type_id = id_float; }; template <> - struct default_image_id<double> + struct default_type_traits<double> { - 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<std::string> + struct default_type_traits<std::string> { - static const image_id_type image_id = id_string; + static const database_type_id db_type_id = id_string; }; template <> - struct default_image_id<const char*> + struct default_type_traits<const char*> { - static const image_id_type image_id = id_string; + static const database_type_id db_type_id = id_string; }; } } |