From b5d76bf8ab11e73b260cbc4343c1f947c0da2699 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 25 Mar 2011 13:04:55 +0200 Subject: Query support --- odb/sqlite/query.hxx | 1228 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1228 insertions(+) create mode 100644 odb/sqlite/query.hxx (limited to 'odb/sqlite/query.hxx') diff --git a/odb/sqlite/query.hxx b/odb/sqlite/query.hxx new file mode 100644 index 0000000..517fd2f --- /dev/null +++ b/odb/sqlite/query.hxx @@ -0,0 +1,1228 @@ +// file : odb/sqlite/query.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_SQLITE_QUERY_HXX +#define ODB_SQLITE_QUERY_HXX + +#include + +#include +#include +#include // std::size_t + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace odb +{ + namespace sqlite + { + template + class val_bind + { + public: + explicit + val_bind (const T& v): val (v) {} + + const T& val; + }; + + template + class ref_bind + { + public: + explicit + ref_bind (const T& r): ref (r) {} + + const T& ref; + }; + + struct LIBODB_SQLITE_EXPORT query_param: details::shared_base + { + virtual + ~query_param (); + + bool + reference () const + { + return value_ != 0; + } + + virtual bool + init () = 0; + + virtual void + bind (sqlite::bind*) = 0; + + protected: + query_param (const void* value) + : value_ (value) + { + } + + protected: + const void* value_; + }; + + class query; + + struct LIBODB_SQLITE_EXPORT query_params: details::shared_base + { + typedef sqlite::binding binding_type; + + binding_type& + binding (); + + private: + friend class query; + + query_params (): binding_ (0, 0) {} + query_params (const query_params&); + + query_params& + operator= (const query_params&); + + private: + typedef std::vector > params; + + params params_; + std::vector bind_; + binding_type binding_; + }; + + // + // + template + struct query_column; + + class LIBODB_SQLITE_EXPORT query + { + public: + query () + : parameters_ (new (details::shared) query_params) + { + } + + explicit + query (const std::string& q) + : clause_ (q), parameters_ (new (details::shared) query_params) + { + } + + template + explicit + query (val_bind v) + : parameters_ (new (details::shared) query_params) + { + append::db_type_id> (v); + } + + template + explicit + query (ref_bind r) + : parameters_ (new (details::shared) query_params) + { + append::db_type_id> (r); + } + + template + query (const query_column&); + + query (const query&); + + query& + operator= (const query&); + + public: + std::string + clause () const; + + binding& + parameters_binding () const; + + details::shared_ptr + parameters () const; + + public: + template + static val_bind + _val (const T& x) + { + return val_bind (x); + } + + template + static ref_bind + _ref (const T& x) + { + return ref_bind (x); + } + + public: + query& + operator+= (const query&); + + query& + operator+= (const std::string& q) + { + 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 v) + { + append::db_type_id> (v); + return *this; + } + + template + query& + operator+= (ref_bind r) + { + append::db_type_id> (r); + return *this; + } + + public: + template + void + append (val_bind); + + template + void + append (ref_bind); + + private: + void + add (details::shared_ptr); + + private: + std::string clause_; + details::shared_ptr parameters_; + }; + + inline query + operator+ (const query& x, const query& y) + { + query r (x); + r += y; + return r; + } + + template + inline query + operator+ (const query& q, val_bind b) + { + query r (q); + r += b; + return r; + } + + template + inline query + operator+ (const query& q, ref_bind b) + { + query r (q); + r += b; + return r; + } + + template + inline query + operator+ (val_bind b, const query& q) + { + query r; + r += b; + r += q; + return r; + } + + template + inline query + operator+ (ref_bind b, const query& q) + { + query r; + r += b; + r += q; + return r; + } + + inline query + operator+ (const query& q, const std::string& s) + { + query r (q); + r += s; + return r; + } + + inline query + operator+ (const std::string& s, const query& q) + { + query r (s); + r += q; + return r; + } + + template + inline query + operator+ (const std::string& s, val_bind b) + { + query r (s); + r += b; + return r; + } + + template + inline query + operator+ (const std::string& s, ref_bind b) + { + query r (s); + r += b; + return r; + } + + template + inline query + operator+ (val_bind b, const std::string& s) + { + query r; + r += b; + r += s; + return r; + } + + template + inline query + operator+ (ref_bind b, const std::string& s) + { + query r; + r += b; + r += s; + 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 ("NOT ("); + 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 + struct query_param_impl; + + // INTEGER + // + 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 bool + init () + { + init (*static_cast (value_)); + return false; + } + + virtual void + bind (sqlite::bind* b) + { + b->type = sqlite::bind::integer; + b->buffer = &image_; + } + + private: + void + init (const T& v) + { + bool dummy; + value_traits::set_image (image_, dummy, v); + } + + private: + long long image_; + }; + + // REAL + // + 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 bool + init () + { + init (*static_cast (value_)); + return false; + } + + virtual void + bind (sqlite::bind* b) + { + b->type = sqlite::bind::real; + b->buffer = &image_; + } + + private: + void + init (const T& v) + { + bool dummy; + value_traits::set_image (image_, dummy, v); + } + + private: + double image_; + }; + + // TEXT + // + 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 bool + init () + { + return init (*static_cast (value_)); + } + + virtual void + bind (sqlite::bind* b) + { + b->type = sqlite::bind::text; + b->buffer = buffer_.data (); + b->size = &size_; + } + + private: + bool + init (const T& v) + { + bool dummy; + std::size_t cap (buffer_.capacity ()); + value_traits::set_image ( + buffer_, size_, dummy, v); + return cap != buffer_.capacity (); + } + + private: + details::buffer buffer_; + std::size_t size_; + }; + + // BLOB + // + 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 bool + init () + { + return init (*static_cast (value_)); + } + + virtual void + bind (sqlite::bind* b) + { + b->type = sqlite::bind::blob; + b->buffer = buffer_.data (); + b->size = &size_; + } + + private: + bool + init (const T& v) + { + bool dummy; + std::size_t cap (buffer_.capacity ()); + value_traits::set_image ( + buffer_, size_, dummy, v); + return cap != buffer_.capacity (); + } + + private: + details::buffer buffer_; + std::size_t size_; + }; + } +} + +// odb::query specialization for SQLite. +// +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 (sqlite::val_bind v) + : object_traits::query_type (sqlite::query (v)) + { + } + + template + explicit + query (sqlite::ref_bind r) + : object_traits::query_type (sqlite::query (r)) + { + } + + query (const sqlite::query& q) + : object_traits::query_type (q) + { + } + + template + query (const sqlite::query_column& qc) + : object_traits::query_type (qc) + { + } + }; +} + +#include +#include + +#include + +#endif // ODB_SQLITE_QUERY_HXX -- cgit v1.1