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/makefile | 2 + odb/sqlite/query.cxx | 162 +++++++ odb/sqlite/query.hxx | 1228 +++++++++++++++++++++++++++++++++++++++++++++++++ odb/sqlite/query.ixx | 40 ++ odb/sqlite/query.txx | 111 +++++ odb/sqlite/result.cxx | 18 + odb/sqlite/result.hxx | 89 ++++ odb/sqlite/result.txx | 120 +++++ 8 files changed, 1770 insertions(+) create mode 100644 odb/sqlite/query.cxx create mode 100644 odb/sqlite/query.hxx create mode 100644 odb/sqlite/query.ixx create mode 100644 odb/sqlite/query.txx create mode 100644 odb/sqlite/result.cxx create mode 100644 odb/sqlite/result.hxx create mode 100644 odb/sqlite/result.txx 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 +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include // std::size_t +#include // std::memset + +#include + +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 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 +// 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 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 +// 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:: + parameters () const + { + return parameters_; + } + + template + inline void query:: + append (val_bind v) + { + add ( + details::shared_ptr ( + new (details::shared) query_param_impl (v))); + } + + template + inline void query:: + append (ref_bind r) + { + add ( + details::shared_ptr ( + new (details::shared) query_param_impl (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 +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace sqlite + { + // query + // + + template + query:: + query (const query_column& 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 (val_bind (true)); + } + + // query_column + // + template + query query_column:: + in (const T& v1, const T& v2) const + { + query q (name_); + q += "IN ("; + q.append (val_bind (v1)); + q += ","; + q.append (val_bind (v2)); + q += ")"; + return q; + } + + template + query query_column:: + in (const T& v1, const T& v2, const T& v3) const + { + 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; + } + } +} 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 +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include + +namespace odb +{ + namespace sqlite + { + result_impl_base:: + result_impl_base (const query& q, details::shared_ptr 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 +// 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 + +#include // std::size_t + +#include + +#include +#include +#include +#include + +#include + +namespace odb +{ + namespace sqlite + { + class result_impl_base + { + public: + result_impl_base (const query&, details::shared_ptr); + + protected: + // We need to hold on to the query parameters because SQLite uses + // the parameter buffers to find each next row. + // + details::shared_ptr params_; + details::shared_ptr statement_; + }; + + template + class result_impl: public odb::result_impl, public result_impl_base + { + public: + typedef typename odb::result_impl::pointer_type pointer_type; + typedef typename odb::result_impl::pointer_traits pointer_traits; + + typedef typename odb::result_impl::object_type object_type; + typedef typename odb::result_impl::id_type id_type; + typedef typename odb::result_impl::object_traits object_traits; + + + virtual + ~result_impl (); + + result_impl (const query&, + details::shared_ptr, + object_statements&); + + 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::current; + + private: + void + load_image (); + + private: + object_statements& statements_; + }; + } +} + +#include + +#include + +#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 +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include + +namespace odb +{ + namespace sqlite + { + template + result_impl:: + ~result_impl () + { + } + + template + result_impl:: + result_impl (const query& q, + details::shared_ptr st, + object_statements& sts) + : odb::result_impl (sts.connection ().database ()), + result_impl_base (q, st), + statements_ (sts) + { + } + + template + void result_impl:: + load (object_type& obj) + { + load_image (); + + // This is a top-level call so the statements cannot be locked. + // + assert (!statements_.locked ()); + typename object_statements::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 result_impl::id_type result_impl:: + load_id () + { + load_image (); + return object_traits::id (statements_.image ()); + } + + template + void result_impl:: + next () + { + this->current (pointer_type ()); + + if (!statement_->next ()) + this->end_ = true; + } + + template + void result_impl:: + 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 + void result_impl:: + cache () + { + } + + template + std::size_t result_impl:: + size () + { + // @@ Should we rather throw unsupported_operation or some such? + // + throw result_not_cached (); + } + } +} -- cgit v1.1