From 4a23d7554de82ba24d10bfc6d165157d113fa447 Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Tue, 25 Oct 2011 07:20:24 +0200 Subject: Add query and result implementations --- odb/oracle/makefile | 2 + odb/oracle/object-result.hxx | 73 ++ odb/oracle/object-result.txx | 109 +++ odb/oracle/query-const-expr.cxx | 16 + odb/oracle/query.cxx | 345 ++++++++ odb/oracle/query.hxx | 1724 +++++++++++++++++++++++++++++++++++++++ odb/oracle/query.ixx | 28 + odb/oracle/query.txx | 111 +++ odb/oracle/result.hxx | 35 + odb/oracle/view-result.hxx | 68 ++ odb/oracle/view-result.txx | 77 ++ 11 files changed, 2588 insertions(+) create mode 100644 odb/oracle/object-result.hxx create mode 100644 odb/oracle/object-result.txx create mode 100644 odb/oracle/query-const-expr.cxx create mode 100644 odb/oracle/query.cxx create mode 100644 odb/oracle/query.hxx create mode 100644 odb/oracle/query.ixx create mode 100644 odb/oracle/query.txx create mode 100644 odb/oracle/result.hxx create mode 100644 odb/oracle/view-result.hxx create mode 100644 odb/oracle/view-result.txx diff --git a/odb/oracle/makefile b/odb/oracle/makefile index bd8d8cb..98cd7c8 100644 --- a/odb/oracle/makefile +++ b/odb/oracle/makefile @@ -14,6 +14,8 @@ database.cxx \ error.cxx \ exceptions.cxx \ object-statements.cxx \ +query.cxx \ +query-const-expr.cxx \ statement.cxx \ statements-base.cxx \ traits.cxx \ diff --git a/odb/oracle/object-result.hxx b/odb/oracle/object-result.hxx new file mode 100644 index 0000000..aa20615 --- /dev/null +++ b/odb/oracle/object-result.hxx @@ -0,0 +1,73 @@ +// file : odb/oracle/object-result.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#ifndef ODB_ORACLE_OBJECT_RESULT_HXX +#define ODB_ORACLE_OBJECT_RESULT_HXX + +#include + +#include // std::size_t + +#include + +#include +#include // query, object_statements +#include +#include + +namespace odb +{ + namespace oracle + { + template + class result_impl: + public odb::result_impl + { + public: + typedef odb::result_impl base_type; + + typedef typename base_type::object_type object_type; + typedef typename base_type::object_traits object_traits; + typedef typename base_type::id_type id_type; + + typedef typename base_type::pointer_type pointer_type; + typedef typename base_type::pointer_traits pointer_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 base_type::current; + + private: + details::shared_ptr statement_; + object_statements& statements_; + }; + } +} + +#include + +#include + +#endif // ODB_ORACLE_OBJECT_RESULT_HXX diff --git a/odb/oracle/object-result.txx b/odb/oracle/object-result.txx new file mode 100644 index 0000000..01a8a6f --- /dev/null +++ b/odb/oracle/object-result.txx @@ -0,0 +1,109 @@ +// file : odb/oracle/object-result.txx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#include +#include + +#include + +namespace odb +{ + namespace oracle + { + template + result_impl:: + ~result_impl () + { + } + + template + result_impl:: + result_impl (const query&, + details::shared_ptr statement, + object_statements& statements) + : base_type (statements.connection ().database ()), + statement_ (statement), + statements_ (statements) + { + } + + template + void result_impl:: + load (object_type& obj) + { + // This is a top-level call so the statements cannot be locked. + // + assert (!statements_.locked ()); + typename object_statements::auto_lock l (statements_); + + odb::database& db (this->database ()); + object_traits::callback (db, obj, callback_event::pre_load); + + typename object_traits::image_type& i (statements_.image ()); + object_traits::init (obj, i, db); + + // Initialize the id image and binding and load the rest of the object + // (containers, etc). + // + typename object_traits::id_image_type& idi (statements_.id_image ()); + object_traits::init (idi, object_traits::id (i)); + statement_->stream_result (); + + binding& idb (statements_.id_image_binding ()); + if (idi.version != statements_.id_image_version () || idb.version == 0) + { + object_traits::bind (idb.bind, idi); + statements_.id_image_version (idi.version); + idb.version++; + } + + object_traits::load_ (statements_, obj); + statements_.load_delayed (); + l.unlock (); + object_traits::callback (db, obj, callback_event::post_load); + } + + template + typename result_impl::id_type + result_impl:: + load_id () + { + return object_traits::id (statements_.image ()); + } + + template + void result_impl:: + next () + { + this->current (pointer_type ()); + + typename object_traits::image_type& im (statements_.image ()); + + if (im.version != statements_.select_image_version ()) + { + binding& b (statements_.select_image_binding ()); + object_traits::bind (b.bind, im, statement_select); + statements_.select_image_version (im.version); + b.version++; + } + + this->end_ = this->end_ || + (statement_->fetch () == select_statement::success ? false : true); + } + + template + void result_impl:: + cache () + { + } + + template + std::size_t result_impl:: + size () + { + throw result_not_cached (); + } + } +} diff --git a/odb/oracle/query-const-expr.cxx b/odb/oracle/query-const-expr.cxx new file mode 100644 index 0000000..e5fa2d0 --- /dev/null +++ b/odb/oracle/query-const-expr.cxx @@ -0,0 +1,16 @@ +// file : odb/oracle/query-const-expr.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#include + +namespace odb +{ + namespace oracle + { + // Sun CC cannot handle this in query.cxx. + // + const query query::true_expr (true); + } +} diff --git a/odb/oracle/query.cxx b/odb/oracle/query.cxx new file mode 100644 index 0000000..4a6642b --- /dev/null +++ b/odb/oracle/query.cxx @@ -0,0 +1,345 @@ +// file : odb/oracle/query.cxx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#include // std::size_t +#include // std::memset +#include // std::ostringstream + +#include + +using namespace std; + +namespace odb +{ + namespace oracle + { + // query_param + // + query_param:: + ~query_param () + { + } + + // query + // + query:: + query (const query& q) + : clause_ (q.clause_), + parameters_ (q.parameters_), + bind_ (q.bind_), + binding_ (0, 0) + { + // Here and below we want to maintain up to date binding info so + // that the call to parameters_binding() below is an immutable + // operation, provided the query does not have any by-reference + // parameters. This way a by-value-only query can be shared + // between multiple threads without the need for synchronization. + // + if (size_t n = bind_.size ()) + { + binding_.bind = &bind_[0]; + binding_.count = n; + binding_.version++; + } + } + + query& query:: + operator= (const query& q) + { + if (this != &q) + { + clause_ = q.clause_; + parameters_ = q.parameters_; + bind_ = q.bind_; + + size_t n (bind_.size ()); + binding_.bind = n != 0 ? &bind_[0] : 0; + binding_.count = n; + binding_.version++; + } + + return *this; + } + + query& query:: + operator+= (const query& q) + { + clause_.insert (clause_.end (), q.clause_.begin (), q.clause_.end ()); + + size_t n (bind_.size ()); + + parameters_.insert ( + parameters_.end (), q.parameters_.begin (), q.parameters_.end ()); + + bind_.insert ( + bind_.end (), q.bind_.begin (), q.bind_.end ()); + + if (n != bind_.size ()) + { + binding_.bind = &bind_[0]; + binding_.count = bind_.size (); + binding_.version++; + } + + return *this; + } + + void query:: + append (const string& q) + { + if (!clause_.empty () && clause_.back ().kind == clause_part::native) + { + string& s (clause_.back ().part); + + char first (!q.empty () ? q[0] : ' '); + char last (!s.empty () ? s[s.size () - 1] : ' '); + + // We don't want extra spaces after '(' as well as before ',' + // and ')'. + // + if (last != ' ' && last != '(' && + first != ' ' && first != ',' && first != ')') + s += ' '; + + s += q; + } + else + clause_.push_back (clause_part (clause_part::native, q)); + } + + void query:: + append (const char* table, const char* column) + { + string s ("\""); + s += table; + s += "\".\""; + s += column; + s += '\"'; + + clause_.push_back (clause_part (clause_part::column, s)); + } + + void query:: + add (details::shared_ptr p) + { + clause_.push_back (clause_part (clause_part::param)); + + parameters_.push_back (p); + bind_.push_back (bind ()); + binding_.bind = &bind_[0]; + binding_.count = bind_.size (); + binding_.version++; + + bind* b (&bind_.back ()); + memset (b, 0, sizeof (bind)); + p->bind (b); + } + + binding& query:: + parameters_binding () const + { + size_t n (parameters_.size ()); + binding& r (binding_); + + if (n == 0) + return r; + + bool inc_ver (false); + bind* b (&bind_[0]); + + for (size_t i (0); i < n; ++i) + { + query_param& p (*parameters_[i]); + + if (p.reference ()) + { + if (p.init ()) + { + p.bind (b + i); + inc_ver = true; + } + } + } + + if (inc_ver) + r.version++; + + return r; + } + + static bool + check_prefix (const string& s) + { + string::size_type n; + + // It is easier to compare to upper and lower-case versions + // rather than getting involved with the portable case- + // insensitive string comparison mess. + // + if (s.compare (0, (n = 5), "WHERE") == 0 || + s.compare (0, (n = 5), "where") == 0 || + s.compare (0, (n = 6), "SELECT") == 0 || + s.compare (0, (n = 6), "select") == 0 || + s.compare (0, (n = 8), "ORDER BY") == 0 || + s.compare (0, (n = 8), "order by") == 0 || + s.compare (0, (n = 8), "GROUP BY") == 0 || + s.compare (0, (n = 8), "group by") == 0 || + s.compare (0, (n = 6), "HAVING") == 0 || + s.compare (0, (n = 6), "having") == 0) + { + // It either has to be an exact match, or there should be + // a whitespace following the keyword. + // + if (s.size () == n || s[n] == ' ' || s[n] =='\t') + return true; + } + + return false; + } + + void query:: + optimize () + { + // Remove a single TRUE literal or one that is followe by one of + // the other clauses. This avoids usless WHERE clauses like + // + // WHERE TRUE GROUP BY foo + // + clause_type::iterator i (clause_.begin ()), e (clause_.end ()); + + if (i != e && i->kind == clause_part::boolean && i->bool_part) + { + clause_type::iterator j (i + 1); + + if (j == e || + (j->kind == clause_part::native && check_prefix (j->part))) + clause_.erase (i); + } + } + + const char* query:: + clause_prefix () const + { + if (!clause_.empty ()) + { + const clause_part& p (clause_.front ()); + + if (p.kind == clause_part::native && check_prefix (p.part)) + return ""; + + return "WHERE "; + } + + return ""; + } + + string query:: + clause () const + { + string r; + size_t param (1); + + for (clause_type::const_iterator i (clause_.begin ()), + end (clause_.end ()); + i != end; + ++i) + { + char last (!r.empty () ? r[r.size () - 1] : ' '); + + switch (i->kind) + { + case clause_part::column: + { + if (last != ' ' && last != '(') + r += ' '; + + r += i->part; + break; + } + case clause_part::param: + { + if (last != ' ' && last != '(') + r += ' '; + + ostringstream os; + os << param++; + r += ':'; + r += os.str (); + break; + } + case clause_part::native: + { + // We don't want extra spaces after '(' as well as before ',' + // and ')'. + // + const string& p (i->part); + char first (!p.empty () ? p[0] : ' '); + + if (last != ' ' && last != '(' && + first != ' ' && first != ',' && first != ')') + r += ' '; + + r += p; + break; + } + case clause_part::boolean: + { + if (last != ' ' && last != '(') + r += ' '; + + r += i->bool_part ? "1 = 1" : "1 = 0"; + break; + } + } + } + + return clause_prefix () + r; + } + + query + operator&& (const query& x, const query& y) + { + // Optimize cases where one or both sides are constant truth. + // + bool xt (x.const_true ()), yt (y.const_true ()); + + if (xt && yt) + return x; + + if (xt) + return y; + + if (yt) + return x; + + query r ("("); + r += x; + r += ") AND ("; + r += y; + r += ")"; + return r; + } + + query + operator|| (const query& x, const query& y) + { + query r ("("); + r += x; + r += ") OR ("; + r += y; + r += ")"; + return r; + } + + query + operator! (const query& x) + { + query r ("NOT ("); + r += x; + r += ")"; + return r; + } + } +} diff --git a/odb/oracle/query.hxx b/odb/oracle/query.hxx new file mode 100644 index 0000000..d491cdf --- /dev/null +++ b/odb/oracle/query.hxx @@ -0,0 +1,1724 @@ +// file : odb/oracle/query.hxx +// author : Contantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#ifndef ODB_ORACLE_QUERY_HXX +#define ODB_ORACLE_QUERY_HXX + +#include + +#include +#include +#include // std::size_t + +#include + +#include +#include +#include +#include + +#include +#include + +#include + +namespace odb +{ + namespace oracle + { + 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_ORACLE_EXPORT query_param: details::shared_base + { + typedef oracle::bind bind_type; + + virtual + ~query_param (); + + bool + reference () const + { + return value_ != 0; + } + + virtual bool + init () = 0; + + virtual void + bind (bind_type*) = 0; + + protected: + query_param (const void* value) + : value_ (value) + { + } + + protected: + const void* value_; + }; + + // + // + template + struct query_column; + + class LIBODB_ORACLE_EXPORT query + { + public: + struct clause_part + { + enum kind_type + { + column, + param, + native, + boolean + }; + + clause_part (kind_type k): kind (k) {} + clause_part (kind_type k, const std::string& p): kind (k), part (p) {} + clause_part (bool p): kind (boolean), bool_part (p) {} + + kind_type kind; + std::string part; + bool bool_part; + }; + + query () + : binding_ (0, 0) + { + } + + // True or false literal. + // + explicit + query (bool v) + : binding_ (0, 0) + { + clause_.push_back (clause_part (v)); + } + + explicit + query (const char* native) + : binding_ (0, 0) + { + clause_.push_back (clause_part (clause_part::native, native)); + } + + explicit + query (const std::string& native) + : binding_ (0, 0) + { + clause_.push_back (clause_part (clause_part::native, native)); + } + + query (const char* table, const char* column) + : binding_ (0, 0) + { + append (table, column); + } + + template + explicit + query (val_bind v) + : binding_ (0, 0) + { + append::db_type_id> (v); + } + + template + explicit + query (ref_bind r) + : binding_ (0, 0) + { + append::db_type_id> (r); + } + + template + query (const query_column&); + + query (const query&); + + query& + operator= (const query&); + + public: + std::string + clause () const; + + const char* + clause_prefix () const; + + binding& + parameters_binding () const; + + public: + bool + empty () const + { + return clause_.empty (); + } + + static const query true_expr; + + bool + const_true () const + { + return clause_.size () == 1 && + clause_.front ().kind == clause_part::boolean && + clause_.front ().bool_part; + } + + void + optimize (); + + 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) + { + append (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); + + void + append (const std::string& native); + + void + append (const char* table, const char* column); + + private: + void + add (details::shared_ptr); + + private: + typedef std::vector clause_type; + typedef std::vector > parameters_type; + + clause_type clause_; + parameters_type parameters_; + mutable std::vector bind_; + mutable binding binding_; + }; + + 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; + } + + LIBODB_ORACLE_EXPORT query + operator&& (const query& x, const query& y); + + LIBODB_ORACLE_EXPORT query + operator|| (const query& x, const query& y); + + LIBODB_ORACLE_EXPORT query + operator! (const query& x); + + // 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 + { + // Note that we keep shalow copies of the table and column names. + // + query_column (const char* table, const char* column) + : table_ (table), column_ (column) + { + } + + const char* + table () const + { + return table_; + } + + const char* + column () const + { + return column_; + } + + // is_null, is_not_null + // + public: + query + is_null () const + { + query q (table_, column_); + q += "IS NULL"; + return q; + } + + query + is_not_null () const + { + query q (table_, column_); + 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 (table_, column_); + 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 (table_, column_); + 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 (table_, column_); + 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 (table_, column_); + 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 (table_, column_); + 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 (table_, column_); + 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 (table_, column_); + 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 (table_, column_); + 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 (table_, column_); + 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 (table_, column_); + 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 (table_, column_); + 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 (table_, column_); + 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 (table_, column_); + q += "="; + q.append (c.table (), c.column ()); + 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 (table_, column_); + q += "!="; + q.append (c.table (), c.column ()); + 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 (table_, column_); + q += "<"; + q.append (c.table (), c.column ()); + 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 (table_, column_); + q += ">"; + q.append (c.table (), c.column ()); + 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 (table_, column_); + q += "<="; + q.append (c.table (), c.column ()); + 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 (table_, column_); + q += ">="; + q.append (c.table (), c.column ()); + return q; + } + + private: + const char* table_; + const char* column_; + }; + + // + // Oracle does not support comparison operations between LOB columns. + // query_column therefore only supports the IS NULL and IS NOT NULL + // predicates for these types. + // + + template + struct query_column + { + // Note that we keep shalow copies of the table and column names. + // + query_column (const char* table, const char* column) + : table_ (table), column_ (column) + { + } + + const char* + table () const + { + return table_; + } + + const char* + column () const + { + return column_; + } + + // is_null, is_not_null + // + public: + query + is_null () const + { + query q (table_, column_); + q += "IS NULL"; + return q; + } + + query + is_not_null () const + { + query q (table_, column_); + q += "IS NOT NULL"; + return q; + } + + private: + const char* table_; + const char* column_; + }; + + template + struct query_column + { + // Note that we keep shalow copies of the table and column names. + // + query_column (const char* table, const char* column) + : table_ (table), column_ (column) + { + } + + const char* + table () const + { + return table_; + } + + const char* + column () const + { + return column_; + } + + // is_null, is_not_null + // + public: + query + is_null () const + { + query q (table_, column_); + q += "IS NULL"; + return q; + } + + query + is_not_null () const + { + query q (table_, column_); + q += "IS NOT NULL"; + return q; + } + + private: + const char* table_; + const char* column_; + }; + + template + struct query_column + { + // Note that we keep shalow copies of the table and column names. + // + query_column (const char* table, const char* column) + : table_ (table), column_ (column) + { + } + + const char* + table () const + { + return table_; + } + + const char* + column () const + { + return column_; + } + + // is_null, is_not_null + // + public: + query + is_null () const + { + query q (table_, column_); + q += "IS NULL"; + return q; + } + + query + is_not_null () const + { + query q (table_, column_); + q += "IS NOT NULL"; + return q; + } + + private: + const char* table_; + const char* column_; + }; + + // Provide operator+() for using columns to construct native + // query fragments (e.g., ORDER BY). + // + template + inline query + operator+ (const query_column& c, const std::string& s) + { + query q (c.table (), c.column ()); + q += s; + return q; + } + + template + inline query + operator+ (const std::string& s, const query_column& c) + { + query q (s); + q.append (c.table (), c.column ()); + return q; + } + + template + inline query + operator+ (const query_column& c, const query& q) + { + query r (c.table (), c.column ()); + r += q; + return r; + } + + template + inline query + operator+ (const query& q, const query_column& c) + { + query r (q); + r.append (c.table (), c.column ()); + return r; + } + + // + // + template + struct query_param_impl; + + // id_int32. + // + 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 (bind_type* b) + { + b->type = bind_type::integer; + b->buffer = &image_; + b->capacity = sizeof (int); + b->size = 0; + } + + private: + void + init (const T& v) + { + bool dummy; + value_traits::set_image (image_, dummy, v); + } + + private: + typename image_traits::image_type image_; + }; + + // id_int64. + // + 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 (bind_type* b) + { + b->type = bind_type::integer; + b->buffer = &image_; + b->capacity = sizeof (long long); + b->size = 0; + } + + private: + void + init (const T& v) + { + bool dummy; + value_traits::set_image (image_, dummy, v); + } + + private: + typename image_traits::image_type image_; + }; + + // id_big_int. + // + 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 (bind_type* b) + { + b->type = bind_type::number; + b->buffer = &image_; + b->capacity = sizeof (image_); + b->size = &size_; + } + + private: + void + init (const T& v) + { + bool dummy; + std::size_t size (0); + value_traits::set_image (image_, size, dummy, v); + size_ = static_cast (size); + } + + private: + char image_[21]; + ub2 size_; + }; + + // id_float. + // + 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 (bind_type* b) + { + b->type = bind_type::binary_float; + b->buffer = &image_; + b->capacity = sizeof (float); + b->size = 0; + } + + private: + void + init (const T& v) + { + bool dummy; + value_traits::set_image (image_, dummy, v); + } + + private: + float image_; + }; + + // id_double. + // + 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 (bind_type* b) + { + b->type = bind_type::binary_double; + b->buffer = &image_; + b->capacity = sizeof (double); + b->size = 0; + } + + private: + void + init (const T& v) + { + bool dummy; + value_traits::set_image (image_, dummy, v); + } + + private: + double image_; + }; + + // id_big_float. + // + 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 (bind_type* b) + { + b->type = bind_type::number; + b->buffer = &image_; + b->capacity = sizeof (image_); + b->size = &size_; + } + + private: + void + init (const T& v) + { + bool dummy; + std::size_t size (0); + value_traits::set_image (image_, size, dummy, v); + size_ = static_cast (size); + } + + private: + char image_[21]; + ub2 size_; + }; + + // id_date. + // + 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 (bind_type* b) + { + b->type = bind_type::date; + b->buffer = &image_; + b->capacity = sizeof (image_); + b->size = 0; + } + + private: + void + init (const T& v) + { + bool dummy; + value_traits::set_image (image_, dummy, v); + } + + private: + char image_[7]; + }; + + // id_timestamp + // + 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 (bind_type* b) + { + b->type = bind_type::timestamp; + b->buffer = &image_; + b->capacity = sizeof (image_); + b->size = &size_; + } + + private: + void + init (const T& v) + { + bool dummy; + std::size_t size (0), cap (11); + value_traits::set_image (image_, size, cap, dummy, v); + size_ = static_cast (size); + } + + private: + char image_[11]; + ub2 size_; + }; + + // id_string. + // + 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 (bind_type* b) + { + b->type = bind_type::string; + b->buffer = image_; + b->capacity = 4000; + b->size = &size_; + } + + private: + void + init (const T& v) + { + bool dummy; + std::size_t size (0); + value_traits::set_image (image_, 4000, size, dummy, v); + size_ = static_cast (size); + } + + private: + char image_[4000]; + ub2 size_; + }; + + // id_nstring + // + 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 (bind_type* b) + { + b->type = bind_type::string; + b->buffer = image_; + b->capacity = 4000; + b->size = &size_; + } + + private: + void + init (const T& v) + { + bool dummy; + std::size_t size (0); + value_traits::set_image (image_, 4000, size, dummy, v); + size_ = static_cast (size); + } + + private: + char image_[4000]; + ub2 size_; + }; + + // id_raw + // + 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 (bind_type* b) + { + b->type = bind_type::string; + b->buffer = image_; + b->capacity = 4000; + b->size = &size_; + } + + private: + void + init (const T& v) + { + bool dummy; + std::size_t size (0); + value_traits::set_image (image_, 4000, size, dummy, v); + size_ = static_cast (size); + } + + private: + char image_[4000]; + ub2 size_; + }; + } +} + +// odb::query specialization for Oracle. +// +namespace odb +{ + template + class query: public query_selector::type + { + public: + // We don't define any typedefs here since they may clash with + // column names defined by our base type. + // + + query () + { + } + + explicit + query (bool v) + : query_selector::type (v) + { + } + + explicit + query (const char* q) + : query_selector::type (q) + { + } + + explicit + query (const std::string& q) + : query_selector::type (q) + { + } + + template + explicit + query (oracle::val_bind v) + : query_selector::type (oracle::query (v)) + { + } + + template + explicit + query (oracle::ref_bind r) + : query_selector::type (oracle::query (r)) + { + } + + query (const oracle::query& q) + : query_selector::type (q) + { + } + + template + query (const oracle::query_column& qc) + : query_selector::type (qc) + { + } + }; +} + +#include +#include + +#include + +#endif // ODB_ORACLE_QUERY_HXX diff --git a/odb/oracle/query.ixx b/odb/oracle/query.ixx new file mode 100644 index 0000000..5e61f4e --- /dev/null +++ b/odb/oracle/query.ixx @@ -0,0 +1,28 @@ +// file : odb/oracle/query.ixx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +namespace odb +{ + namespace oracle + { + 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/oracle/query.txx b/odb/oracle/query.txx new file mode 100644 index 0000000..b8bd3c5 --- /dev/null +++ b/odb/oracle/query.txx @@ -0,0 +1,111 @@ +// file : odb/oracle/query.txx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +namespace odb +{ + namespace oracle + { + // query + // + + template + query:: + query (const query_column& c) + : binding_ (0, 0) + { + // Cannot use IS TRUE here since database type can be a non- + // integral type. + // + append (c.table (), c.column ()); + append ("="); + append (val_bind (true)); + } + + // query_column + // + template + query query_column:: + in (const T& v1, const T& v2) const + { + query q (table_, column_); + 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 (table_, column_); + 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 (table_, column_); + 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 (table_, column_); + 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 (table_, column_); + 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/oracle/result.hxx b/odb/oracle/result.hxx new file mode 100644 index 0000000..88b6aaa --- /dev/null +++ b/odb/oracle/result.hxx @@ -0,0 +1,35 @@ +// file : odb/oracle/result.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#ifndef ODB_ORACLE_RESULT_HXX +#define ODB_ORACLE_RESULT_HXX + +#include + +#include +#include + +#include +#include + +namespace odb +{ + namespace oracle + { + template + class result_impl; + } +} + +#include + +#endif // ODB_ORACLE_RESULT_HXX + +// Include result specializations so that the user code only needs +// to include this header. +// + +#include +#include diff --git a/odb/oracle/view-result.hxx b/odb/oracle/view-result.hxx new file mode 100644 index 0000000..abc0aff --- /dev/null +++ b/odb/oracle/view-result.hxx @@ -0,0 +1,68 @@ +// file : odb/oracle/view-result.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#ifndef ODB_ORACLE_VIEW_RESULT_HXX +#define ODB_ORACLE_VIEW_RESULT_HXX + +#include + +#include // std::size_t + +#include + +#include +#include // query, view_statements +#include +#include + +namespace odb +{ + namespace oracle + { + template + class result_impl: public odb::result_impl + { + public: + typedef odb::result_impl base_type; + + typedef typename base_type::view_type view_type; + typedef typename base_type::view_traits view_traits; + + typedef typename base_type::pointer_type pointer_type; + typedef typename base_type::pointer_traits pointer_traits; + + virtual + ~result_impl (); + + result_impl (const query&, + details::shared_ptr, + view_statements&); + + virtual void + load (view_type&); + + virtual void + next (); + + virtual void + cache (); + + virtual std::size_t + size (); + + using base_type::current; + + private: + details::shared_ptr statement_; + view_statements& statements_; + }; + } +} + +#include + +#include + +#endif // ODB_ORACLE_VIEW_RESULT_HXX diff --git a/odb/oracle/view-result.txx b/odb/oracle/view-result.txx new file mode 100644 index 0000000..b2049b8 --- /dev/null +++ b/odb/oracle/view-result.txx @@ -0,0 +1,77 @@ +// file : odb/oracle/view-result.txx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#include +#include + +#include + +namespace odb +{ + namespace oracle + { + template + result_impl:: + ~result_impl () + { + } + + template + result_impl:: + result_impl (const query&, + details::shared_ptr statement, + view_statements& statements) + : base_type (statements.connection ().database ()), + statement_ (statement), + statements_ (statements) + { + } + + template + void result_impl:: + load (view_type& view) + { + odb::database& db (this->database ()); + + view_traits::callback (db, view, callback_event::pre_load); + view_traits::init (view, statements_.image (), db); + statement_->stream_result (); + view_traits::callback (db, view, callback_event::post_load); + } + + template + void result_impl:: + next () + { + this->current (pointer_type ()); + + typename view_traits::image_type& im (statements_.image ()); + + if (im.version != statements_.image_version ()) + { + binding& b (statements_.image_binding ()); + view_traits::bind (b.bind, im); + statements_.image_version (im.version); + b.version++; + } + + this->end_ = this->end_ || + (statement_->fetch () == select_statement::success ? false : true); + } + + template + void result_impl:: + cache () + { + } + + template + std::size_t result_impl:: + size () + { + throw result_not_cached (); + } + } +} -- cgit v1.1