From 382035d872a2cbb22fc14d3c87db93b1f39b407b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 8 Oct 2012 16:09:08 +0200 Subject: Ground work for multi-database support All generated code now includes database id. The database-specific database class interface has been updated to include all the database operations. The database-specific tests now use this interface. --- odb/sqlite/query.hxx | 424 +++++++++++++++++++++++++++++---------------------- 1 file changed, 243 insertions(+), 181 deletions(-) (limited to 'odb/sqlite/query.hxx') diff --git a/odb/sqlite/query.hxx b/odb/sqlite/query.hxx index ffd3559..fdae3a2 100644 --- a/odb/sqlite/query.hxx +++ b/odb/sqlite/query.hxx @@ -75,7 +75,7 @@ namespace odb const void* value_; }; - class query; + class query_base; class LIBODB_SQLITE_EXPORT query_params: public details::shared_base { @@ -86,7 +86,7 @@ namespace odb binding (); private: - friend class query; + friend class query_base; query_params (): binding_ (0, 0) {} query_params (const query_params&); @@ -113,7 +113,7 @@ namespace odb template struct query_column; - class LIBODB_SQLITE_EXPORT query + class LIBODB_SQLITE_EXPORT query_base { public: struct clause_part @@ -135,7 +135,7 @@ namespace odb bool bool_part; }; - query () + query_base () : parameters_ (new (details::shared) query_params) { } @@ -143,27 +143,27 @@ namespace odb // True or false literal. // explicit - query (bool v) + query_base (bool v) : parameters_ (new (details::shared) query_params) { clause_.push_back (clause_part (v)); } explicit - query (const char* native) + query_base (const char* native) : parameters_ (new (details::shared) query_params) { clause_.push_back (clause_part (clause_part::native, native)); } explicit - query (const std::string& native) + query_base (const std::string& native) : parameters_ (new (details::shared) query_params) { clause_.push_back (clause_part (clause_part::native, native)); } - query (const char* table, const char* column) + query_base (const char* table, const char* column) : parameters_ (new (details::shared) query_params) { append (table, column); @@ -171,7 +171,7 @@ namespace odb template explicit - query (val_bind v) + query_base (val_bind v) : parameters_ (new (details::shared) query_params) { append::db_type_id> ( @@ -180,7 +180,7 @@ namespace odb template explicit - query (ref_bind r) + query_base (ref_bind r) : parameters_ (new (details::shared) query_params) { append::db_type_id> ( @@ -188,12 +188,12 @@ namespace odb } template - query (const query_column&); + query_base (const query_column&); - query (const query&); + query_base (const query_base&); - query& - operator= (const query&); + query_base& + operator= (const query_base&); public: std::string @@ -215,7 +215,7 @@ namespace odb return clause_.empty (); } - static const query true_expr; + static const query_base true_expr; bool const_true () const @@ -244,10 +244,10 @@ namespace odb } public: - query& - operator+= (const query&); + query_base& + operator+= (const query_base&); - query& + query_base& operator+= (const std::string& q) { append (q); @@ -255,7 +255,7 @@ namespace odb } template - query& + query_base& operator+= (val_bind v) { append::db_type_id> ( @@ -264,7 +264,7 @@ namespace odb } template - query& + query_base& operator+= (ref_bind r) { append::db_type_id> ( @@ -298,114 +298,114 @@ namespace odb details::shared_ptr parameters_; }; - inline query - operator+ (const query& x, const query& y) + inline query_base + operator+ (const query_base& x, const query_base& y) { - query r (x); + query_base r (x); r += y; return r; } template - inline query - operator+ (const query& q, val_bind b) + inline query_base + operator+ (const query_base& q, val_bind b) { - query r (q); + query_base r (q); r += b; return r; } template - inline query - operator+ (const query& q, ref_bind b) + inline query_base + operator+ (const query_base& q, ref_bind b) { - query r (q); + query_base r (q); r += b; return r; } template - inline query - operator+ (val_bind b, const query& q) + inline query_base + operator+ (val_bind b, const query_base& q) { - query r; + query_base r; r += b; r += q; return r; } template - inline query - operator+ (ref_bind b, const query& q) + inline query_base + operator+ (ref_bind b, const query_base& q) { - query r; + query_base r; r += b; r += q; return r; } - inline query - operator+ (const query& q, const std::string& s) + inline query_base + operator+ (const query_base& q, const std::string& s) { - query r (q); + query_base r (q); r += s; return r; } - inline query - operator+ (const std::string& s, const query& q) + inline query_base + operator+ (const std::string& s, const query_base& q) { - query r (s); + query_base r (s); r += q; return r; } template - inline query + inline query_base operator+ (const std::string& s, val_bind b) { - query r (s); + query_base r (s); r += b; return r; } template - inline query + inline query_base operator+ (const std::string& s, ref_bind b) { - query r (s); + query_base r (s); r += b; return r; } template - inline query + inline query_base operator+ (val_bind b, const std::string& s) { - query r; + query_base r; r += b; r += s; return r; } template - inline query + inline query_base operator+ (ref_bind b, const std::string& s) { - query r; + query_base r; r += b; r += s; return r; } - LIBODB_SQLITE_EXPORT query - operator&& (const query&, const query&); + LIBODB_SQLITE_EXPORT query_base + operator&& (const query_base&, const query_base&); - LIBODB_SQLITE_EXPORT query - operator|| (const query&, const query&); + LIBODB_SQLITE_EXPORT query_base + operator|| (const query_base&, const query_base&); - LIBODB_SQLITE_EXPORT query - operator! (const query&); + LIBODB_SQLITE_EXPORT query_base + operator! (const query_base&); // query_column // @@ -458,18 +458,18 @@ namespace odb // is_null, is_not_null // public: - query + query_base is_null () const { - query q (table_, column_); + query_base q (table_, column_); q += "IS NULL"; return q; } - query + query_base is_not_null () const { - query q (table_, column_); + query_base q (table_, column_); q += "IS NOT NULL"; return q; } @@ -477,102 +477,102 @@ namespace odb // in // public: - query + query_base in (const T&, const T&) const; - query + query_base in (const T&, const T&, const T&) const; - query + query_base in (const T&, const T&, const T&, const T&) const; - query + query_base in (const T&, const T&, const T&, const T&, const T&) const; template - query + query_base in_range (I begin, I end) const; // = // public: - query + query_base equal (const T& v) const { return equal (val_bind (v)); } - query + query_base equal (val_bind v) const { - query q (table_, column_); + query_base q (table_, column_); q += "="; q.append (v, conversion_); return q; } template - query + query_base equal (val_bind v) const { copy_bind c (v.val); return equal (c); } - query + query_base equal (ref_bind r) const { - query q (table_, column_); + query_base q (table_, column_); q += "="; q.append (r, conversion_); return q; } - friend query + friend query_base operator== (const query_column& c, const T& v) { return c.equal (v); } - friend query + friend query_base operator== (const T& v, const query_column& c) { return c.equal (v); } - friend query + friend query_base operator== (const query_column& c, val_bind v) { return c.equal (v); } - friend query + friend query_base operator== (val_bind v, const query_column& c) { return c.equal (v); } template - friend query + friend query_base operator== (const query_column& c, val_bind v) { return c.equal (v); } template - friend query + friend query_base operator== (val_bind v, const query_column& c) { return c.equal (v); } - friend query + friend query_base operator== (const query_column& c, ref_bind r) { return c.equal (r); } - friend query + friend query_base operator== (ref_bind r, const query_column& c) { return c.equal (r); @@ -581,83 +581,83 @@ namespace odb // != // public: - query + query_base unequal (const T& v) const { return unequal (val_bind (v)); } - query + query_base unequal (val_bind v) const { - query q (table_, column_); + query_base q (table_, column_); q += "!="; q.append (v, conversion_); return q; } template - query + query_base unequal (val_bind v) const { copy_bind c (v.val); return unequal (c); } - query + query_base unequal (ref_bind r) const { - query q (table_, column_); + query_base q (table_, column_); q += "!="; q.append (r, conversion_); return q; } - friend query + friend query_base operator!= (const query_column& c, const T& v) { return c.unequal (v); } - friend query + friend query_base operator!= (const T& v, const query_column& c) { return c.unequal (v); } - friend query + friend query_base operator!= (const query_column& c, val_bind v) { return c.unequal (v); } - friend query + friend query_base operator!= (val_bind v, const query_column& c) { return c.unequal (v); } template - friend query + friend query_base operator!= (const query_column& c, val_bind v) { return c.unequal (v); } template - friend query + friend query_base operator!= (val_bind v, const query_column& c) { return c.unequal (v); } - friend query + friend query_base operator!= (const query_column& c, ref_bind r) { return c.unequal (r); } - friend query + friend query_base operator!= (ref_bind r, const query_column& c) { return c.unequal (r); @@ -666,83 +666,83 @@ namespace odb // < // public: - query + query_base less (const T& v) const { return less (val_bind (v)); } - query + query_base less (val_bind v) const { - query q (table_, column_); + query_base q (table_, column_); q += "<"; q.append (v, conversion_); return q; } template - query + query_base less (val_bind v) const { copy_bind c (v.val); return less (c); } - query + query_base less (ref_bind r) const { - query q (table_, column_); + query_base q (table_, column_); q += "<"; q.append (r, conversion_); return q; } - friend query + friend query_base operator< (const query_column& c, const T& v) { return c.less (v); } - friend query + friend query_base operator< (const T& v, const query_column& c) { return c.greater (v); } - friend query + friend query_base operator< (const query_column& c, val_bind v) { return c.less (v); } - friend query + friend query_base operator< (val_bind v, const query_column& c) { return c.greater (v); } template - friend query + friend query_base operator< (const query_column& c, val_bind v) { return c.less (v); } template - friend query + friend query_base operator< (val_bind v, const query_column& c) { return c.greater (v); } - friend query + friend query_base operator< (const query_column& c, ref_bind r) { return c.less (r); } - friend query + friend query_base operator< (ref_bind r, const query_column& c) { return c.greater (r); @@ -751,83 +751,83 @@ namespace odb // > // public: - query + query_base greater (const T& v) const { return greater (val_bind (v)); } - query + query_base greater (val_bind v) const { - query q (table_, column_); + query_base q (table_, column_); q += ">"; q.append (v, conversion_); return q; } template - query + query_base greater (val_bind v) const { copy_bind c (v.val); return greater (c); } - query + query_base greater (ref_bind r) const { - query q (table_, column_); + query_base q (table_, column_); q += ">"; q.append (r, conversion_); return q; } - friend query + friend query_base operator> (const query_column& c, const T& v) { return c.greater (v); } - friend query + friend query_base operator> (const T& v, const query_column& c) { return c.less (v); } - friend query + friend query_base operator> (const query_column& c, val_bind v) { return c.greater (v); } - friend query + friend query_base operator> (val_bind v, const query_column& c) { return c.less (v); } template - friend query + friend query_base operator> (const query_column& c, val_bind v) { return c.greater (v); } template - friend query + friend query_base operator> (val_bind v, const query_column& c) { return c.less (v); } - friend query + friend query_base operator> (const query_column& c, ref_bind r) { return c.greater (r); } - friend query + friend query_base operator> (ref_bind r, const query_column& c) { return c.less (r); @@ -836,83 +836,83 @@ namespace odb // <= // public: - query + query_base less_equal (const T& v) const { return less_equal (val_bind (v)); } - query + query_base less_equal (val_bind v) const { - query q (table_, column_); + query_base q (table_, column_); q += "<="; q.append (v, conversion_); return q; } template - query + query_base less_equal (val_bind v) const { copy_bind c (v.val); return less_equal (c); } - query + query_base less_equal (ref_bind r) const { - query q (table_, column_); + query_base q (table_, column_); q += "<="; q.append (r, conversion_); return q; } - friend query + friend query_base operator<= (const query_column& c, const T& v) { return c.less_equal (v); } - friend query + friend query_base operator<= (const T& v, const query_column& c) { return c.greater_equal (v); } - friend query + friend query_base operator<= (const query_column& c, val_bind v) { return c.less_equal (v); } - friend query + friend query_base operator<= (val_bind v, const query_column& c) { return c.greater_equal (v); } template - friend query + friend query_base operator<= (const query_column& c, val_bind v) { return c.less_equal (v); } template - friend query + friend query_base operator<= (val_bind v, const query_column& c) { return c.greater_equal (v); } - friend query + friend query_base operator<= (const query_column& c, ref_bind r) { return c.less_equal (r); } - friend query + friend query_base operator<= (ref_bind r, const query_column& c) { return c.greater_equal (r); @@ -921,83 +921,83 @@ namespace odb // >= // public: - query + query_base greater_equal (const T& v) const { return greater_equal (val_bind (v)); } - query + query_base greater_equal (val_bind v) const { - query q (table_, column_); + query_base q (table_, column_); q += ">="; q.append (v, conversion_); return q; } template - query + query_base greater_equal (val_bind v) const { copy_bind c (v.val); return greater_equal (c); } - query + query_base greater_equal (ref_bind r) const { - query q (table_, column_); + query_base q (table_, column_); q += ">="; q.append (r, conversion_); return q; } - friend query + friend query_base operator>= (const query_column& c, const T& v) { return c.greater_equal (v); } - friend query + friend query_base operator>= (const T& v, const query_column& c) { return c.less_equal (v); } - friend query + friend query_base operator>= (const query_column& c, val_bind v) { return c.greater_equal (v); } - friend query + friend query_base operator>= (val_bind v, const query_column& c) { return c.less_equal (v); } template - friend query + friend query_base operator>= (const query_column& c, val_bind v) { return c.greater_equal (v); } template - friend query + friend query_base operator>= (val_bind v, const query_column& c) { return c.less_equal (v); } - friend query + friend query_base operator>= (const query_column& c, ref_bind r) { return c.greater_equal (r); } - friend query + friend query_base operator>= (ref_bind r, const query_column& c) { return c.less_equal (r); @@ -1007,84 +1007,84 @@ namespace odb // public: template - query + query_base 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_); + query_base q (table_, column_); q += "="; q.append (c.table (), c.column ()); return q; } template - query + query_base 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_); + query_base q (table_, column_); q += "!="; q.append (c.table (), c.column ()); return q; } template - query + query_base 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_); + query_base q (table_, column_); q += "<"; q.append (c.table (), c.column ()); return q; } template - query + query_base 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_); + query_base q (table_, column_); q += ">"; q.append (c.table (), c.column ()); return q; } template - query + query_base 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_); + query_base q (table_, column_); q += "<="; q.append (c.table (), c.column ()); return q; } template - query + query_base 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_); + query_base q (table_, column_); q += ">="; q.append (c.table (), c.column ()); return q; @@ -1100,37 +1100,37 @@ namespace odb // query fragments (e.g., ORDER BY). // template - inline query + inline query_base operator+ (const query_column& c, const std::string& s) { - query q (c.table (), c.column ()); + query_base q (c.table (), c.column ()); q += s; return q; } template - inline query + inline query_base operator+ (const std::string& s, const query_column& c) { - query q (s); + query_base q (s); q.append (c.table (), c.column ()); return q; } template - inline query - operator+ (const query_column& c, const query& q) + inline query_base + operator+ (const query_column& c, const query_base& q) { - query r (c.table (), c.column ()); + query_base r (c.table (), c.column ()); r += q; return r; } template - inline query - operator+ (const query& q, const query_column& c) + inline query_base + operator+ (const query_base& q, const query_column& c) { - query r (q); + query_base r (q); r.append (c.table (), c.column ()); return r; } @@ -1284,13 +1284,75 @@ namespace odb } } -// odb::query specialization for SQLite. +// odb::sqlite::query and odb::query specialization for SQLite. // namespace odb { + namespace sqlite + { + template + class query: public query_base, + public query_selector::columns_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_base (v) + { + } + + explicit + query (const char* q) + : query_base (q) + { + } + + explicit + query (const std::string& q) + : query_base (q) + { + } + + template + explicit + query (val_bind v) + : query_base (v) + { + } + + template + explicit + query (ref_bind r) + : query_base (r) + { + } + + query (const query_base& q) + : query_base (q) + { + } + + template + query (const query_column& qc) + : query_base (qc) + { + } + }; + } + + // Derive odb::query from odb::sqlite::query so that it can be + // implicitly converted in sqlite::database::query() calls. + // template - class query: public sqlite::query, - public query_selector::columns_type + class query: public sqlite::query { public: // We don't define any typedefs here since they may clash with @@ -1303,44 +1365,44 @@ namespace odb explicit query (bool v) - : sqlite::query (v) + : sqlite::query (v) { } explicit query (const char* q) - : sqlite::query (q) + : sqlite::query (q) { } explicit query (const std::string& q) - : sqlite::query (q) + : sqlite::query (q) { } template explicit query (sqlite::val_bind v) - : sqlite::query (sqlite::query (v)) + : sqlite::query (v) { } template explicit query (sqlite::ref_bind r) - : sqlite::query (sqlite::query (r)) + : sqlite::query (r) { } - query (const sqlite::query& q) - : sqlite::query (q) + query (const sqlite::query_base& q) + : sqlite::query (q) { } template query (const sqlite::query_column& qc) - : sqlite::query (qc) + : sqlite::query (qc) { } }; -- cgit v1.1