From bad675ea0e17b6eb18d75ddc403b81ff5f76ad25 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 13 Aug 2010 13:39:57 +0200 Subject: Add support for language-embedded query --- odb/mysql/query.cxx | 23 ++ odb/mysql/query.hxx | 811 +++++++++++++++++++++++++++++++++++++++++++++++++++- odb/mysql/query.ixx | 28 ++ odb/mysql/query.txx | 102 ++++++- 4 files changed, 944 insertions(+), 20 deletions(-) create mode 100644 odb/mysql/query.ixx diff --git a/odb/mysql/query.cxx b/odb/mysql/query.cxx index 1a2003a..0caa214 100644 --- a/odb/mysql/query.cxx +++ b/odb/mysql/query.cxx @@ -42,9 +42,32 @@ namespace odb return *this; } + query& query:: + operator+= (const query& q) + { + size_t n (clause_.size ()); + + if (n != 0 && clause_[n - 1] != ' ' && + !q.clause_.empty () && q.clause_[0] != ' ') + clause_ += ' '; + + clause_ += q.clause_; + + parameters_.insert ( + parameters_.end (), q.parameters_.begin (), q.parameters_.end ()); + + binding_.insert ( + binding_.end (), q.binding_.begin (), q.binding_.end ()); + } + void query:: add (shared_ptr p) { + size_t n (clause_.size ()); + + if (n != 0 && clause_[n - 1] != ' ') + clause_ += ' '; + clause_ += '?'; parameters_.push_back (p); diff --git a/odb/mysql/query.hxx b/odb/mysql/query.hxx index c322c3c..fe53bd5 100644 --- a/odb/mysql/query.hxx +++ b/odb/mysql/query.hxx @@ -12,6 +12,7 @@ #include #include // std::size_t +#include #include #include @@ -26,6 +27,7 @@ namespace odb class val_bind { public: + explicit val_bind (const T& v): val (v) {} const T& val; @@ -35,6 +37,7 @@ namespace odb class ref_bind { public: + explicit ref_bind (const T& r): ref (r) {} const T& ref; @@ -67,6 +70,11 @@ namespace odb const void* value_; }; + // + // + template + struct query_column; + class query { public: @@ -80,6 +88,23 @@ namespace odb { } + template + explicit + query (val_bind v) + { + append::image_id> (v); + } + + template + explicit + query (ref_bind r) + { + append::image_id> (r); + } + + template + query (const query_column&); + query (const query&); query& @@ -109,26 +134,44 @@ namespace odb public: query& - operator+= (const query& q) - { - clause_ += ' '; - clause_ += q.clause_; - } + operator+= (const query&); query& operator+= (const std::string& q) { - clause_ += ' '; + size_t n (clause_.size ()); + + if (n != 0 && clause_[n - 1] != ' ' && !q.empty () && q[0] != ' ') + clause_ += ' '; + clause_ += q; + return *this; } template query& - operator+= (val_bind); + operator+= (val_bind v) + { + append::image_id> (v); + return *this; + } template query& - operator+= (ref_bind); + operator+= (ref_bind r) + { + append::image_id> (r); + return *this; + } + + public: + template + void + append (val_bind); + + template + void + append (ref_bind); private: void @@ -242,6 +285,708 @@ namespace odb return r; } + inline query + operator&& (const query& x, const query& y) + { + query r ("("); + r += x; + r += ") AND ("; + r += y; + r += ")"; + return r; + } + + inline query + operator|| (const query& x, const query& y) + { + query r ("("); + r += x; + r += ") OR ("; + r += y; + r += ")"; + return r; + } + + inline query + operator! (const query& x) + { + query r ("!("); + r += x; + r += ")"; + return r; + } + + // query_column + // + + template + class copy_bind: public val_bind + { + public: + explicit + copy_bind (const T2& v): val_bind (val), val (v) {} + + const T val; + }; + + template + const T& + type_instance (); + + template + struct query_column + { + explicit + query_column (const char* name) + : name_ (name) + { + } + + const char* + name () const + { + return name_; + } + + // is_null, is_not_null + // + public: + query + is_null () const + { + query q (name_); + q += "IS NULL"; + return q; + } + + query + is_not_null () const + { + query q (name_); + q += "IS NOT NULL"; + return q; + } + + // in + // + public: + query + in (const T&, const T&) const; + + query + in (const T&, const T&, const T&) const; + + query + in (const T&, const T&, const T&, const T&) const; + + query + in (const T&, const T&, const T&, const T&, const T&) const; + + template + query + in_range (I begin, I end) const; + + // = + // + public: + query + equal (const T& v) const + { + return equal (val_bind (v)); + } + + query + equal (val_bind v) const + { + query q (name_); + q += "="; + q.append (v); + return q; + } + + template + query + equal (val_bind v) const + { + copy_bind c (v.val); + return equal (c); + } + + query + equal (ref_bind r) const + { + query q (name_); + q += "="; + q.append (r); + return q; + } + + friend query + operator== (const query_column& c, const T& v) + { + return c.equal (v); + } + + friend query + operator== (const T& v, const query_column& c) + { + return c.equal (v); + } + + friend query + operator== (const query_column& c, val_bind v) + { + return c.equal (v); + } + + friend query + operator== (val_bind v, const query_column& c) + { + return c.equal (v); + } + + template + friend query + operator== (const query_column& c, val_bind v) + { + return c.equal (v); + } + + template + friend query + operator== (val_bind v, const query_column& c) + { + return c.equal (v); + } + + friend query + operator== (const query_column& c, ref_bind r) + { + return c.equal (r); + } + + friend query + operator== (ref_bind r, const query_column& c) + { + return c.equal (r); + } + + // != + // + public: + query + unequal (const T& v) const + { + return unequal (val_bind (v)); + } + + query + unequal (val_bind v) const + { + query q (name_); + q += "!="; + q.append (v); + return q; + } + + template + query + unequal (val_bind v) const + { + copy_bind c (v.val); + return unequal (c); + } + + query + unequal (ref_bind r) const + { + query q (name_); + q += "!="; + q.append (r); + return q; + } + + friend query + operator!= (const query_column& c, const T& v) + { + return c.unequal (v); + } + + friend query + operator!= (const T& v, const query_column& c) + { + return c.unequal (v); + } + + friend query + operator!= (const query_column& c, val_bind v) + { + return c.unequal (v); + } + + friend query + operator!= (val_bind v, const query_column& c) + { + return c.unequal (v); + } + + template + friend query + operator!= (const query_column& c, val_bind v) + { + return c.unequal (v); + } + + template + friend query + operator!= (val_bind v, const query_column& c) + { + return c.unequal (v); + } + + friend query + operator!= (const query_column& c, ref_bind r) + { + return c.unequal (r); + } + + friend query + operator!= (ref_bind r, const query_column& c) + { + return c.unequal (r); + } + + // < + // + public: + query + less (const T& v) const + { + return less (val_bind (v)); + } + + query + less (val_bind v) const + { + query q (name_); + q += "<"; + q.append (v); + return q; + } + + template + query + less (val_bind v) const + { + copy_bind c (v.val); + return less (c); + } + + query + less (ref_bind r) const + { + query q (name_); + q += "<"; + q.append (r); + return q; + } + + friend query + operator< (const query_column& c, const T& v) + { + return c.less (v); + } + + friend query + operator< (const T& v, const query_column& c) + { + return c.greater (v); + } + + friend query + operator< (const query_column& c, val_bind v) + { + return c.less (v); + } + + friend query + operator< (val_bind v, const query_column& c) + { + return c.greater (v); + } + + template + friend query + operator< (const query_column& c, val_bind v) + { + return c.less (v); + } + + template + friend query + operator< (val_bind v, const query_column& c) + { + return c.greater (v); + } + + friend query + operator< (const query_column& c, ref_bind r) + { + return c.less (r); + } + + friend query + operator< (ref_bind r, const query_column& c) + { + return c.greater (r); + } + + // > + // + public: + query + greater (const T& v) const + { + return greater (val_bind (v)); + } + + query + greater (val_bind v) const + { + query q (name_); + q += ">"; + q.append (v); + return q; + } + + template + query + greater (val_bind v) const + { + copy_bind c (v.val); + return greater (c); + } + + query + greater (ref_bind r) const + { + query q (name_); + q += ">"; + q.append (r); + return q; + } + + friend query + operator> (const query_column& c, const T& v) + { + return c.greater (v); + } + + friend query + operator> (const T& v, const query_column& c) + { + return c.less (v); + } + + friend query + operator> (const query_column& c, val_bind v) + { + return c.greater (v); + } + + friend query + operator> (val_bind v, const query_column& c) + { + return c.less (v); + } + + template + friend query + operator> (const query_column& c, val_bind v) + { + return c.greater (v); + } + + template + friend query + operator> (val_bind v, const query_column& c) + { + return c.less (v); + } + + friend query + operator> (const query_column& c, ref_bind r) + { + return c.greater (r); + } + + friend query + operator> (ref_bind r, const query_column& c) + { + return c.less (r); + } + + // <= + // + public: + query + less_equal (const T& v) const + { + return less_equal (val_bind (v)); + } + + query + less_equal (val_bind v) const + { + query q (name_); + q += "<="; + q.append (v); + return q; + } + + template + query + less_equal (val_bind v) const + { + copy_bind c (v.val); + return less_equal (c); + } + + query + less_equal (ref_bind r) const + { + query q (name_); + q += "<="; + q.append (r); + return q; + } + + friend query + operator<= (const query_column& c, const T& v) + { + return c.less_equal (v); + } + + friend query + operator<= (const T& v, const query_column& c) + { + return c.greater_equal (v); + } + + friend query + operator<= (const query_column& c, val_bind v) + { + return c.less_equal (v); + } + + friend query + operator<= (val_bind v, const query_column& c) + { + return c.greater_equal (v); + } + + template + friend query + operator<= (const query_column& c, val_bind v) + { + return c.less_equal (v); + } + + template + friend query + operator<= (val_bind v, const query_column& c) + { + return c.greater_equal (v); + } + + friend query + operator<= (const query_column& c, ref_bind r) + { + return c.less_equal (r); + } + + friend query + operator<= (ref_bind r, const query_column& c) + { + return c.greater_equal (r); + } + + // >= + // + public: + query + greater_equal (const T& v) const + { + return greater_equal (val_bind (v)); + } + + query + greater_equal (val_bind v) const + { + query q (name_); + q += ">="; + q.append (v); + return q; + } + + template + query + greater_equal (val_bind v) const + { + copy_bind c (v.val); + return greater_equal (c); + } + + query + greater_equal (ref_bind r) const + { + query q (name_); + q += ">="; + q.append (r); + return q; + } + + friend query + operator>= (const query_column& c, const T& v) + { + return c.greater_equal (v); + } + + friend query + operator>= (const T& v, const query_column& c) + { + return c.less_equal (v); + } + + friend query + operator>= (const query_column& c, val_bind v) + { + return c.greater_equal (v); + } + + friend query + operator>= (val_bind v, const query_column& c) + { + return c.less_equal (v); + } + + template + friend query + operator>= (const query_column& c, val_bind v) + { + return c.greater_equal (v); + } + + template + friend query + operator>= (val_bind v, const query_column& c) + { + return c.less_equal (v); + } + + friend query + operator>= (const query_column& c, ref_bind r) + { + return c.greater_equal (r); + } + + friend query + operator>= (ref_bind r, const query_column& c) + { + return c.less_equal (r); + } + + // Column comparison. + // + public: + template + query + operator== (const query_column& c) const + { + // We can compare columns only if we can compare their C++ types. + // + (void) (sizeof (type_instance () == type_instance ())); + + query q (name_); + q += "="; + q += c.name (); + return q; + } + + template + query + operator!= (const query_column& c) const + { + // We can compare columns only if we can compare their C++ types. + // + (void) (sizeof (type_instance () != type_instance ())); + + query q (name_); + q += "!="; + q += c.name (); + return q; + } + + template + query + operator< (const query_column& c) const + { + // We can compare columns only if we can compare their C++ types. + // + (void) (sizeof (type_instance () < type_instance ())); + + query q (name_); + q += "<"; + q += c.name (); + return q; + } + + template + query + operator> (const query_column& c) const + { + // We can compare columns only if we can compare their C++ types. + // + (void) (sizeof (type_instance () > type_instance ())); + + query q (name_); + q += ">"; + q += c.name (); + return q; + } + + template + query + operator<= (const query_column& c) const + { + // We can compare columns only if we can compare their C++ types. + // + (void) (sizeof (type_instance () <= type_instance ())); + + query q (name_); + q += "<="; + q += c.name (); + return q; + } + + template + query + operator>= (const query_column& c) const + { + // We can compare columns only if we can compare their C++ types. + // + (void) (sizeof (type_instance () >= type_instance ())); + + query q (name_); + q += ">="; + q += c.name (); + return q; + } + + private: + const char* name_; + }; + // // template @@ -823,6 +1568,56 @@ namespace odb } } +// odb::query specialization for MySQL. +// +namespace odb +{ + template + class query: public object_traits::query_type + { + public: + // We don't define any typedefs here since they may clash with + // column names defined by our base type. + // + + query () + { + } + + explicit + query (const std::string& q) + : object_traits::query_type (q) + { + } + + template + explicit + query (mysql::val_bind v) + : object_traits::query_type (mysql::query (v)) + { + } + + template + explicit + query (mysql::ref_bind r) + : object_traits::query_type (mysql::query (r)) + { + } + + query (const mysql::query& q) + : object_traits::query_type (q) + { + } + + template + query (const mysql::query_column& qc) + : object_traits::query_type (qc) + { + } + }; +} + +#include #include #endif // ODB_MYSQL_QUERY_HXX diff --git a/odb/mysql/query.ixx b/odb/mysql/query.ixx new file mode 100644 index 0000000..9562bd1 --- /dev/null +++ b/odb/mysql/query.ixx @@ -0,0 +1,28 @@ +// file : odb/mysql/query.ixx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace mysql + { + template + inline void query:: + append (val_bind v) + { + add ( + shared_ptr ( + new (shared) query_param_impl (v))); + } + + template + inline void query:: + append (ref_bind r) + { + add ( + shared_ptr ( + new (shared) query_param_impl (r))); + } + } +} diff --git a/odb/mysql/query.txx b/odb/mysql/query.txx index 23d4ba7..dda0e72 100644 --- a/odb/mysql/query.txx +++ b/odb/mysql/query.txx @@ -7,22 +7,100 @@ namespace odb { namespace mysql { - template - query& query:: - operator+= (val_bind v) + // query + // + + template + query:: + query (const query_column& c) + : clause_ (c.name ()) + { + clause_ += " IS TRUE"; + } + + // query_column + // + template + query query_column:: + in (const T& v1, const T& v2) const { - add ( - shared_ptr ( - new (shared) query_param_impl::image_id> (v))); + query q (name_); + q += "IN ("; + q.append (val_bind (v1)); + q += ","; + q.append (val_bind (v2)); + q += ")"; + return q; } - template - query& query:: - operator+= (ref_bind r) + template + query query_column:: + in (const T& v1, const T& v2, const T& v3) const { - add ( - shared_ptr ( - new (shared) query_param_impl::image_id> (r))); + query q (name_); + q += "IN ("; + q.append (val_bind (v1)); + q += ","; + q.append (val_bind (v2)); + q += ","; + q.append (val_bind (v3)); + q += ")"; + return q; + } + + template + query query_column:: + in (const T& v1, const T& v2, const T& v3, const T& v4) const + { + query q (name_); + q += "IN ("; + q.append (val_bind (v1)); + q += ","; + q.append (val_bind (v2)); + q += ","; + q.append (val_bind (v3)); + q += ","; + q.append (val_bind (v4)); + q += ")"; + return q; + } + + template + query query_column:: + in (const T& v1, const T& v2, const T& v3, const T& v4, const T& v5) const + { + query q (name_); + q += "IN ("; + q.append (val_bind (v1)); + q += ","; + q.append (val_bind (v2)); + q += ","; + q.append (val_bind (v3)); + q += ","; + q.append (val_bind (v4)); + q += ","; + q.append (val_bind (v5)); + q += ")"; + return q; + } + + template + template + query query_column:: + in_range (I begin, I end) const + { + query q (name_); + q += "IN ("; + + for (I i (begin); i != end; ++i) + { + if (i != begin) + q += ","; + + q.append (val_bind (*i)); + } + q += ")"; + return q; } } } -- cgit v1.1