diff options
-rw-r--r-- | odb/sqlite/makefile | 2 | ||||
-rw-r--r-- | odb/sqlite/query.cxx | 162 | ||||
-rw-r--r-- | odb/sqlite/query.hxx | 1228 | ||||
-rw-r--r-- | odb/sqlite/query.ixx | 40 | ||||
-rw-r--r-- | odb/sqlite/query.txx | 111 | ||||
-rw-r--r-- | odb/sqlite/result.cxx | 18 | ||||
-rw-r--r-- | odb/sqlite/result.hxx | 89 | ||||
-rw-r--r-- | odb/sqlite/result.txx | 120 |
8 files changed, 1770 insertions, 0 deletions
diff --git a/odb/sqlite/makefile b/odb/sqlite/makefile index 3cc9d3c..2d51d7d 100644 --- a/odb/sqlite/makefile +++ b/odb/sqlite/makefile @@ -12,6 +12,8 @@ database.cxx \ error.cxx \ exceptions.cxx \ object-statements.cxx \ +query.cxx \ +result.cxx \ statement-cache.cxx \ statement.cxx \ traits.cxx \ diff --git a/odb/sqlite/query.cxx b/odb/sqlite/query.cxx new file mode 100644 index 0000000..a1c5b00 --- /dev/null +++ b/odb/sqlite/query.cxx @@ -0,0 +1,162 @@ +// file : odb/sqlite/query.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <cstddef> // std::size_t +#include <cstring> // std::memset + +#include <odb/sqlite/query.hxx> + +using namespace std; + +namespace odb +{ + namespace sqlite + { + // query_param + // + + query_param:: + ~query_param () + { + } + + // query_params + // + + query_params:: + query_params (const query_params& x) + : details::shared_base (x), + params_ (x.params_), bind_ (x.bind_), binding_ (0, 0) + { + } + + query_params& query_params:: + operator= (const query_params& x) + { + if (this != &x) + { + params_ = x.params_; + bind_ = x.bind_; + } + + return *this; + } + + query_params::binding_type& query_params:: + binding () + { + size_t n (params_.size ()); + binding_type& r (binding_); + + if (n == 0) + return r; // r.bind and r.count should be 0. + + sqlite::bind* b (&bind_[0]); + + bool inc_ver (false); + + if (r.bind != b || r.count != n) + { + r.bind = b; + r.count = n; + inc_ver = true; + } + + for (size_t i (0); i < n; ++i) + { + query_param& p (*params_[i]); + + if (p.reference ()) + { + if (p.init ()) + { + p.bind (b + i); + inc_ver = true; + } + } + } + + if (inc_ver) + r.version++; + + return r; + } + + // query + // + + query:: + query (const query& q) + : clause_ (q.clause_), + parameters_ (new (details::shared) query_params (*q.parameters_)) + { + } + + query& query:: + operator= (const query& q) + { + if (this != &q) + { + clause_ = q.clause_; + *parameters_ = *q.parameters_; + } + + 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_; + + query_params& p (*parameters_); + query_params& qp (*q.parameters_); + + p.params_.insert ( + p.params_.end (), qp.params_.begin (), qp.params_.end ()); + + p.bind_.insert (p.bind_.end (), qp.bind_.begin (), qp.bind_.end ()); + + return *this; + } + + void query:: + add (details::shared_ptr<query_param> p) + { + size_t n (clause_.size ()); + + if (n != 0 && clause_[n - 1] != ' ') + clause_ += ' '; + + clause_ += '?'; + + parameters_->params_.push_back (p); + parameters_->bind_.push_back (sqlite::bind ()); + sqlite::bind* b (¶meters_->bind_.back ()); + memset (b, 0, sizeof (sqlite::bind)); + + p->bind (b); + } + + std::string query:: + clause () const + { + if (clause_.empty () || + clause_.compare (0, 6, "WHERE ") == 0 || + clause_.compare (0, 9, "ORDER BY ") == 0 || + clause_.compare (0, 9, "GROUP BY ") == 0 || + clause_.compare (0, 7, "HAVING ") == 0) + return clause_; + else + return "WHERE " + clause_; + } + } +} 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 <boris@codesynthesis.com> +// 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 <odb/pre.hxx> + +#include <string> +#include <vector> +#include <cstddef> // std::size_t + +#include <odb/query.hxx> +#include <odb/details/buffer.hxx> +#include <odb/details/shared-ptr.hxx> + +#include <odb/sqlite/version.hxx> +#include <odb/sqlite/forward.hxx> +#include <odb/sqlite/traits.hxx> +#include <odb/sqlite/sqlite-types.hxx> +#include <odb/sqlite/binding.hxx> +#include <odb/sqlite/details/export.hxx> + +namespace odb +{ + namespace sqlite + { + template <typename T> + class val_bind + { + public: + explicit + val_bind (const T& v): val (v) {} + + const T& val; + }; + + template <typename T> + 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<details::shared_ptr<query_param> > params; + + params params_; + std::vector<sqlite::bind> bind_; + binding_type binding_; + }; + + // + // + template <typename T, database_type_id ID> + 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 <typename T> + explicit + query (val_bind<T> v) + : parameters_ (new (details::shared) query_params) + { + append<T, type_traits<T>::db_type_id> (v); + } + + template <typename T> + explicit + query (ref_bind<T> r) + : parameters_ (new (details::shared) query_params) + { + append<T, type_traits<T>::db_type_id> (r); + } + + template <database_type_id ID> + query (const query_column<bool, ID>&); + + query (const query&); + + query& + operator= (const query&); + + public: + std::string + clause () const; + + binding& + parameters_binding () const; + + details::shared_ptr<query_params> + parameters () const; + + public: + template <typename T> + static val_bind<T> + _val (const T& x) + { + return val_bind<T> (x); + } + + template <typename T> + static ref_bind<T> + _ref (const T& x) + { + return ref_bind<T> (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 <typename T> + query& + operator+= (val_bind<T> v) + { + append<T, type_traits<T>::db_type_id> (v); + return *this; + } + + template <typename T> + query& + operator+= (ref_bind<T> r) + { + append<T, type_traits<T>::db_type_id> (r); + return *this; + } + + public: + template <typename T, database_type_id ID> + void + append (val_bind<T>); + + template <typename T, database_type_id ID> + void + append (ref_bind<T>); + + private: + void + add (details::shared_ptr<query_param>); + + private: + std::string clause_; + details::shared_ptr<query_params> parameters_; + }; + + inline query + operator+ (const query& x, const query& y) + { + query r (x); + r += y; + return r; + } + + template <typename T> + inline query + operator+ (const query& q, val_bind<T> b) + { + query r (q); + r += b; + return r; + } + + template <typename T> + inline query + operator+ (const query& q, ref_bind<T> b) + { + query r (q); + r += b; + return r; + } + + template <typename T> + inline query + operator+ (val_bind<T> b, const query& q) + { + query r; + r += b; + r += q; + return r; + } + + template <typename T> + inline query + operator+ (ref_bind<T> 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 <typename T> + inline query + operator+ (const std::string& s, val_bind<T> b) + { + query r (s); + r += b; + return r; + } + + template <typename T> + inline query + operator+ (const std::string& s, ref_bind<T> b) + { + query r (s); + r += b; + return r; + } + + template <typename T> + inline query + operator+ (val_bind<T> b, const std::string& s) + { + query r; + r += b; + r += s; + return r; + } + + template <typename T> + inline query + operator+ (ref_bind<T> 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 <typename T, typename T2> + class copy_bind: public val_bind<T> + { + public: + explicit + copy_bind (const T2& v): val_bind<T> (val), val (v) {} + + const T val; + }; + + template <typename T> + const T& + type_instance (); + + template <typename T, database_type_id ID> + 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 <typename I> + query + in_range (I begin, I end) const; + + // = + // + public: + query + equal (const T& v) const + { + return equal (val_bind<T> (v)); + } + + query + equal (val_bind<T> v) const + { + query q (name_); + q += "="; + q.append<T, ID> (v); + return q; + } + + template <typename T2> + query + equal (val_bind<T2> v) const + { + copy_bind<T, T2> c (v.val); + return equal (c); + } + + query + equal (ref_bind<T> r) const + { + query q (name_); + q += "="; + q.append<T, ID> (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<T> v) + { + return c.equal (v); + } + + friend query + operator== (val_bind<T> v, const query_column& c) + { + return c.equal (v); + } + + template <typename T2> + friend query + operator== (const query_column& c, val_bind<T2> v) + { + return c.equal (v); + } + + template <typename T2> + friend query + operator== (val_bind<T2> v, const query_column& c) + { + return c.equal (v); + } + + friend query + operator== (const query_column& c, ref_bind<T> r) + { + return c.equal (r); + } + + friend query + operator== (ref_bind<T> r, const query_column& c) + { + return c.equal (r); + } + + // != + // + public: + query + unequal (const T& v) const + { + return unequal (val_bind<T> (v)); + } + + query + unequal (val_bind<T> v) const + { + query q (name_); + q += "!="; + q.append<T, ID> (v); + return q; + } + + template <typename T2> + query + unequal (val_bind<T2> v) const + { + copy_bind<T, T2> c (v.val); + return unequal (c); + } + + query + unequal (ref_bind<T> r) const + { + query q (name_); + q += "!="; + q.append<T, ID> (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<T> v) + { + return c.unequal (v); + } + + friend query + operator!= (val_bind<T> v, const query_column& c) + { + return c.unequal (v); + } + + template <typename T2> + friend query + operator!= (const query_column& c, val_bind<T2> v) + { + return c.unequal (v); + } + + template <typename T2> + friend query + operator!= (val_bind<T2> v, const query_column& c) + { + return c.unequal (v); + } + + friend query + operator!= (const query_column& c, ref_bind<T> r) + { + return c.unequal (r); + } + + friend query + operator!= (ref_bind<T> r, const query_column& c) + { + return c.unequal (r); + } + + // < + // + public: + query + less (const T& v) const + { + return less (val_bind<T> (v)); + } + + query + less (val_bind<T> v) const + { + query q (name_); + q += "<"; + q.append<T, ID> (v); + return q; + } + + template <typename T2> + query + less (val_bind<T2> v) const + { + copy_bind<T, T2> c (v.val); + return less (c); + } + + query + less (ref_bind<T> r) const + { + query q (name_); + q += "<"; + q.append<T, ID> (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<T> v) + { + return c.less (v); + } + + friend query + operator< (val_bind<T> v, const query_column& c) + { + return c.greater (v); + } + + template <typename T2> + friend query + operator< (const query_column& c, val_bind<T2> v) + { + return c.less (v); + } + + template <typename T2> + friend query + operator< (val_bind<T2> v, const query_column& c) + { + return c.greater (v); + } + + friend query + operator< (const query_column& c, ref_bind<T> r) + { + return c.less (r); + } + + friend query + operator< (ref_bind<T> r, const query_column& c) + { + return c.greater (r); + } + + // > + // + public: + query + greater (const T& v) const + { + return greater (val_bind<T> (v)); + } + + query + greater (val_bind<T> v) const + { + query q (name_); + q += ">"; + q.append<T, ID> (v); + return q; + } + + template <typename T2> + query + greater (val_bind<T2> v) const + { + copy_bind<T, T2> c (v.val); + return greater (c); + } + + query + greater (ref_bind<T> r) const + { + query q (name_); + q += ">"; + q.append<T, ID> (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<T> v) + { + return c.greater (v); + } + + friend query + operator> (val_bind<T> v, const query_column& c) + { + return c.less (v); + } + + template <typename T2> + friend query + operator> (const query_column& c, val_bind<T2> v) + { + return c.greater (v); + } + + template <typename T2> + friend query + operator> (val_bind<T2> v, const query_column& c) + { + return c.less (v); + } + + friend query + operator> (const query_column& c, ref_bind<T> r) + { + return c.greater (r); + } + + friend query + operator> (ref_bind<T> r, const query_column& c) + { + return c.less (r); + } + + // <= + // + public: + query + less_equal (const T& v) const + { + return less_equal (val_bind<T> (v)); + } + + query + less_equal (val_bind<T> v) const + { + query q (name_); + q += "<="; + q.append<T, ID> (v); + return q; + } + + template <typename T2> + query + less_equal (val_bind<T2> v) const + { + copy_bind<T, T2> c (v.val); + return less_equal (c); + } + + query + less_equal (ref_bind<T> r) const + { + query q (name_); + q += "<="; + q.append<T, ID> (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<T> v) + { + return c.less_equal (v); + } + + friend query + operator<= (val_bind<T> v, const query_column& c) + { + return c.greater_equal (v); + } + + template <typename T2> + friend query + operator<= (const query_column& c, val_bind<T2> v) + { + return c.less_equal (v); + } + + template <typename T2> + friend query + operator<= (val_bind<T2> v, const query_column& c) + { + return c.greater_equal (v); + } + + friend query + operator<= (const query_column& c, ref_bind<T> r) + { + return c.less_equal (r); + } + + friend query + operator<= (ref_bind<T> r, const query_column& c) + { + return c.greater_equal (r); + } + + // >= + // + public: + query + greater_equal (const T& v) const + { + return greater_equal (val_bind<T> (v)); + } + + query + greater_equal (val_bind<T> v) const + { + query q (name_); + q += ">="; + q.append<T, ID> (v); + return q; + } + + template <typename T2> + query + greater_equal (val_bind<T2> v) const + { + copy_bind<T, T2> c (v.val); + return greater_equal (c); + } + + query + greater_equal (ref_bind<T> r) const + { + query q (name_); + q += ">="; + q.append<T, ID> (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<T> v) + { + return c.greater_equal (v); + } + + friend query + operator>= (val_bind<T> v, const query_column& c) + { + return c.less_equal (v); + } + + template <typename T2> + friend query + operator>= (const query_column& c, val_bind<T2> v) + { + return c.greater_equal (v); + } + + template <typename T2> + friend query + operator>= (val_bind<T2> v, const query_column& c) + { + return c.less_equal (v); + } + + friend query + operator>= (const query_column& c, ref_bind<T> r) + { + return c.greater_equal (r); + } + + friend query + operator>= (ref_bind<T> r, const query_column& c) + { + return c.less_equal (r); + } + + // Column comparison. + // + public: + template <typename T2, database_type_id ID2> + query + operator== (const query_column<T2, ID2>& c) const + { + // We can compare columns only if we can compare their C++ types. + // + (void) (sizeof (type_instance<T> () == type_instance<T2> ())); + + query q (name_); + q += "="; + q += c.name (); + return q; + } + + template <typename T2, database_type_id ID2> + query + operator!= (const query_column<T2, ID2>& c) const + { + // We can compare columns only if we can compare their C++ types. + // + (void) (sizeof (type_instance<T> () != type_instance<T2> ())); + + query q (name_); + q += "!="; + q += c.name (); + return q; + } + + template <typename T2, database_type_id ID2> + query + operator< (const query_column<T2, ID2>& c) const + { + // We can compare columns only if we can compare their C++ types. + // + (void) (sizeof (type_instance<T> () < type_instance<T2> ())); + + query q (name_); + q += "<"; + q += c.name (); + return q; + } + + template <typename T2, database_type_id ID2> + query + operator> (const query_column<T2, ID2>& c) const + { + // We can compare columns only if we can compare their C++ types. + // + (void) (sizeof (type_instance<T> () > type_instance<T2> ())); + + query q (name_); + q += ">"; + q += c.name (); + return q; + } + + template <typename T2, database_type_id ID2> + query + operator<= (const query_column<T2, ID2>& c) const + { + // We can compare columns only if we can compare their C++ types. + // + (void) (sizeof (type_instance<T> () <= type_instance<T2> ())); + + query q (name_); + q += "<="; + q += c.name (); + return q; + } + + template <typename T2, database_type_id ID2> + query + operator>= (const query_column<T2, ID2>& c) const + { + // We can compare columns only if we can compare their C++ types. + // + (void) (sizeof (type_instance<T> () >= type_instance<T2> ())); + + query q (name_); + q += ">="; + q += c.name (); + return q; + } + + private: + const char* name_; + }; + + // + // + template <typename T, database_type_id> + struct query_param_impl; + + // INTEGER + // + template <typename T> + struct query_param_impl<T, id_integer>: 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 bool + init () + { + init (*static_cast<const T*> (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<T, long long, id_integer>::set_image (image_, dummy, v); + } + + private: + long long image_; + }; + + // REAL + // + template <typename T> + struct query_param_impl<T, id_real>: 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 bool + init () + { + init (*static_cast<const T*> (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<T, double, id_real>::set_image (image_, dummy, v); + } + + private: + double image_; + }; + + // TEXT + // + template <typename T> + struct query_param_impl<T, id_text>: 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 bool + init () + { + return init (*static_cast<const T*> (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<T, details::buffer, id_text>::set_image ( + buffer_, size_, dummy, v); + return cap != buffer_.capacity (); + } + + private: + details::buffer buffer_; + std::size_t size_; + }; + + // BLOB + // + template <typename T> + struct query_param_impl<T, id_blob>: 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 bool + init () + { + return init (*static_cast<const T*> (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<T, details::buffer, id_blob>::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 <typename T> + class query<T, sqlite::query>: public object_traits<T>::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<T>::query_type (q) + { + } + + template <typename T2> + explicit + query (sqlite::val_bind<T2> v) + : object_traits<T>::query_type (sqlite::query (v)) + { + } + + template <typename T2> + explicit + query (sqlite::ref_bind<T2> r) + : object_traits<T>::query_type (sqlite::query (r)) + { + } + + query (const sqlite::query& q) + : object_traits<T>::query_type (q) + { + } + + template <sqlite::database_type_id ID> + query (const sqlite::query_column<bool, ID>& qc) + : object_traits<T>::query_type (qc) + { + } + }; +} + +#include <odb/sqlite/query.ixx> +#include <odb/sqlite/query.txx> + +#include <odb/post.hxx> + +#endif // ODB_SQLITE_QUERY_HXX diff --git a/odb/sqlite/query.ixx b/odb/sqlite/query.ixx new file mode 100644 index 0000000..f51da5a --- /dev/null +++ b/odb/sqlite/query.ixx @@ -0,0 +1,40 @@ +// file : odb/sqlite/query.ixx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace sqlite + { + inline binding& query:: + parameters_binding () const + { + return parameters_->binding (); + } + + inline details::shared_ptr<query_params> query:: + parameters () const + { + return parameters_; + } + + template <typename T, database_type_id ID> + inline void query:: + append (val_bind<T> v) + { + add ( + details::shared_ptr<query_param> ( + new (details::shared) query_param_impl<T, ID> (v))); + } + + template <typename T, database_type_id ID> + inline void query:: + append (ref_bind<T> r) + { + add ( + details::shared_ptr<query_param> ( + new (details::shared) query_param_impl<T, ID> (r))); + } + } +} diff --git a/odb/sqlite/query.txx b/odb/sqlite/query.txx new file mode 100644 index 0000000..3144348 --- /dev/null +++ b/odb/sqlite/query.txx @@ -0,0 +1,111 @@ +// file : odb/sqlite/query.txx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace sqlite + { + // query + // + + template <database_type_id ID> + query:: + query (const query_column<bool, ID>& c) + : clause_ (c.name ()), + parameters_ (new (details::shared) query_params) + { + // Cannot use IS TRUE here since database type can be a non- + // integral type. + // + clause_ += " = "; + append<bool, ID> (val_bind<bool> (true)); + } + + // query_column + // + template <typename T, database_type_id ID> + query query_column<T, ID>:: + in (const T& v1, const T& v2) const + { + query q (name_); + q += "IN ("; + q.append<T, ID> (val_bind<T> (v1)); + q += ","; + q.append<T, ID> (val_bind<T> (v2)); + q += ")"; + return q; + } + + template <typename T, database_type_id ID> + query query_column<T, ID>:: + in (const T& v1, const T& v2, const T& v3) const + { + query q (name_); + q += "IN ("; + q.append<T, ID> (val_bind<T> (v1)); + q += ","; + q.append<T, ID> (val_bind<T> (v2)); + q += ","; + q.append<T, ID> (val_bind<T> (v3)); + q += ")"; + return q; + } + + 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 + { + query q (name_); + q += "IN ("; + q.append<T, ID> (val_bind<T> (v1)); + q += ","; + q.append<T, ID> (val_bind<T> (v2)); + q += ","; + q.append<T, ID> (val_bind<T> (v3)); + q += ","; + q.append<T, ID> (val_bind<T> (v4)); + q += ")"; + return q; + } + + 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 + { + query q (name_); + q += "IN ("; + q.append<T, ID> (val_bind<T> (v1)); + q += ","; + q.append<T, ID> (val_bind<T> (v2)); + q += ","; + q.append<T, ID> (val_bind<T> (v3)); + q += ","; + q.append<T, ID> (val_bind<T> (v4)); + q += ","; + q.append<T, ID> (val_bind<T> (v5)); + q += ")"; + return q; + } + + template <typename T, database_type_id ID> + template <typename I> + query query_column<T, ID>:: + 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<T, ID> (val_bind<T> (*i)); + } + q += ")"; + return q; + } + } +} diff --git a/odb/sqlite/result.cxx b/odb/sqlite/result.cxx new file mode 100644 index 0000000..7e4116f --- /dev/null +++ b/odb/sqlite/result.cxx @@ -0,0 +1,18 @@ +// file : odb/sqlite/result.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <odb/sqlite/result.hxx> + +namespace odb +{ + namespace sqlite + { + result_impl_base:: + result_impl_base (const query& q, details::shared_ptr<select_statement> s) + : params_ (q.parameters ()), statement_ (s) + { + } + } +} diff --git a/odb/sqlite/result.hxx b/odb/sqlite/result.hxx new file mode 100644 index 0000000..187589e --- /dev/null +++ b/odb/sqlite/result.hxx @@ -0,0 +1,89 @@ +// file : odb/sqlite/result.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_SQLITE_RESULT_HXX +#define ODB_SQLITE_RESULT_HXX + +#include <odb/pre.hxx> + +#include <cstddef> // std::size_t + +#include <odb/result.hxx> + +#include <odb/sqlite/version.hxx> +#include <odb/sqlite/forward.hxx> +#include <odb/sqlite/query.hxx> +#include <odb/sqlite/statement.hxx> + +#include <odb/details/shared-ptr.hxx> + +namespace odb +{ + namespace sqlite + { + class result_impl_base + { + public: + result_impl_base (const query&, details::shared_ptr<select_statement>); + + protected: + // We need to hold on to the query parameters because SQLite uses + // the parameter buffers to find each next row. + // + details::shared_ptr<query_params> params_; + details::shared_ptr<select_statement> statement_; + }; + + template <typename T> + class result_impl: public odb::result_impl<T>, public result_impl_base + { + public: + typedef typename odb::result_impl<T>::pointer_type pointer_type; + typedef typename odb::result_impl<T>::pointer_traits pointer_traits; + + typedef typename odb::result_impl<T>::object_type object_type; + typedef typename odb::result_impl<T>::id_type id_type; + typedef typename odb::result_impl<T>::object_traits object_traits; + + + virtual + ~result_impl (); + + result_impl (const query&, + details::shared_ptr<select_statement>, + object_statements<object_type>&); + + virtual void + load (object_type&); + + virtual id_type + load_id (); + + virtual void + next (); + + virtual void + cache (); + + virtual std::size_t + size (); + + using odb::result_impl<T>::current; + + private: + void + load_image (); + + private: + object_statements<object_type>& statements_; + }; + } +} + +#include <odb/sqlite/result.txx> + +#include <odb/post.hxx> + +#endif // ODB_SQLITE_RESULT_HXX diff --git a/odb/sqlite/result.txx b/odb/sqlite/result.txx new file mode 100644 index 0000000..a7f00e0 --- /dev/null +++ b/odb/sqlite/result.txx @@ -0,0 +1,120 @@ +// file : odb/sqlite/result.txx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <odb/exceptions.hxx> + +namespace odb +{ + namespace sqlite + { + template <typename T> + result_impl<T>:: + ~result_impl () + { + } + + template <typename T> + result_impl<T>:: + result_impl (const query& q, + details::shared_ptr<select_statement> st, + object_statements<object_type>& sts) + : odb::result_impl<T> (sts.connection ().database ()), + result_impl_base (q, st), + statements_ (sts) + { + } + + template <typename T> + void result_impl<T>:: + load (object_type& obj) + { + load_image (); + + // This is a top-level call so the statements cannot be locked. + // + assert (!statements_.locked ()); + typename object_statements<object_type>::auto_lock l (statements_); + + typename object_traits::image_type& im (statements_.image ()); + object_traits::init (obj, im, this->database ()); + + // Initialize the id image and load the rest of the object + // (containers, etc). + // + object_traits::init (statements_.id_image (), object_traits::id (im)); + object_traits::load_ (statements_, obj); + + statements_.load_delayed (); + l.unlock (); + } + + template <typename T> + typename result_impl<T>::id_type result_impl<T>:: + load_id () + { + load_image (); + return object_traits::id (statements_.image ()); + } + + template <typename T> + void result_impl<T>:: + next () + { + this->current (pointer_type ()); + + if (!statement_->next ()) + this->end_ = true; + } + + template <typename T> + void result_impl<T>:: + load_image () + { + // The image can grow between calls to load() as a result of other + // statements execution. + // + typename object_traits::image_type& im (statements_.image ()); + + if (im.version != statements_.out_image_version ()) + { + binding& b (statements_.out_image_binding ()); + object_traits::bind (b.bind, im, true); + statements_.out_image_version (im.version); + b.version++; + } + + select_statement::result r (statement_->load ()); + + if (r == select_statement::truncated) + { + object_traits::grow (im, statements_.out_image_truncated ()); + + if (im.version != statements_.out_image_version ()) + { + binding& b (statements_.out_image_binding ()); + object_traits::bind (b.bind, im, true); + statements_.out_image_version (im.version); + b.version++; + statement_->reload (); + } + } + } + + template <typename T> + void result_impl<T>:: + cache () + { + } + + template <typename T> + std::size_t result_impl<T>:: + size () + { + // @@ Should we rather throw unsupported_operation or some such? + // + throw result_not_cached (); + } + } +} |