diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-09-09 12:18:38 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-09-09 12:18:38 +0200 |
commit | 0a7c90251190205e8bd9f5193b7402e25efa2f81 (patch) | |
tree | 84d91a66ef37105c082c4154fbf3fca37c11e98b | |
parent | 932ae000f66ec5935c368b953da24dfd6cf7581b (diff) |
New templated query_columns architecture
We also now use the correct separate "role"-base join approach instead of
having a single merged join for each table.
-rw-r--r-- | odb/mysql/query.cxx | 25 | ||||
-rw-r--r-- | odb/mysql/query.hxx | 99 | ||||
-rw-r--r-- | odb/mysql/query.txx | 15 |
3 files changed, 77 insertions, 62 deletions
diff --git a/odb/mysql/query.cxx b/odb/mysql/query.cxx index ca858b1..14477d6 100644 --- a/odb/mysql/query.cxx +++ b/odb/mysql/query.cxx @@ -84,11 +84,9 @@ namespace odb } void query:: - append (const string& q, clause_part::kind_type k) + append (const string& q) { - if (k == clause_part::native && - !clause_.empty () && - clause_.back ().kind == clause_part::native) + if (!clause_.empty () && clause_.back ().kind == clause_part::native) { string& s (clause_.back ().part); @@ -105,7 +103,19 @@ namespace odb s += q; } else - clause_.push_back (clause_part (k, q)); + 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:: @@ -157,7 +167,7 @@ namespace odb } string query:: - clause (string const& table) const + clause () const { string r; @@ -173,9 +183,6 @@ namespace odb if (last != ' ' && last != '(') r += ' '; - if (i->part[0] == '.') - r += table; - r += i->part; break; } diff --git a/odb/mysql/query.hxx b/odb/mysql/query.hxx index 17aa724..57d43a1 100644 --- a/odb/mysql/query.hxx +++ b/odb/mysql/query.hxx @@ -107,11 +107,16 @@ namespace odb } explicit - query (const std::string& q, - clause_part::kind_type k = clause_part::native) + query (const std::string& native) : binding_ (0, 0) { - clause_.push_back (clause_part (k, q)); + clause_.push_back (clause_part (clause_part::native, native)); + } + + query (const char* table, const char* column) + : binding_ (0, 0) + { + append (table, column); } template <typename T> @@ -140,7 +145,7 @@ namespace odb public: std::string - clause (std::string const& default_table) const; + clause () const; binding& parameters_binding () const; @@ -167,7 +172,7 @@ namespace odb query& operator+= (const std::string& q) { - append (q, clause_part::native); + append (q); return *this; } @@ -197,7 +202,10 @@ namespace odb append (ref_bind<T>); void - append (const std::string&, clause_part::kind_type); + append (const std::string& native); + + void + append (const char* table, const char* column); private: void @@ -364,16 +372,23 @@ namespace odb template <typename T, database_type_id ID> struct query_column { - explicit - query_column (const char* name) - : name_ (name) + // 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* - name () const + table () const { - return name_; + return table_; + } + + const char* + column () const + { + return column_; } // is_null, is_not_null @@ -382,7 +397,7 @@ namespace odb query is_null () const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += "IS NULL"; return q; } @@ -390,7 +405,7 @@ namespace odb query is_not_null () const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += "IS NOT NULL"; return q; } @@ -426,7 +441,7 @@ namespace odb query equal (val_bind<T> v) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += "="; q.append<T, ID> (v); return q; @@ -443,7 +458,7 @@ namespace odb query equal (ref_bind<T> r) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += "="; q.append<T, ID> (r); return q; @@ -511,7 +526,7 @@ namespace odb query unequal (val_bind<T> v) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += "!="; q.append<T, ID> (v); return q; @@ -528,7 +543,7 @@ namespace odb query unequal (ref_bind<T> r) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += "!="; q.append<T, ID> (r); return q; @@ -596,7 +611,7 @@ namespace odb query less (val_bind<T> v) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += "<"; q.append<T, ID> (v); return q; @@ -613,7 +628,7 @@ namespace odb query less (ref_bind<T> r) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += "<"; q.append<T, ID> (r); return q; @@ -681,7 +696,7 @@ namespace odb query greater (val_bind<T> v) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += ">"; q.append<T, ID> (v); return q; @@ -698,7 +713,7 @@ namespace odb query greater (ref_bind<T> r) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += ">"; q.append<T, ID> (r); return q; @@ -766,7 +781,7 @@ namespace odb query less_equal (val_bind<T> v) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += "<="; q.append<T, ID> (v); return q; @@ -783,7 +798,7 @@ namespace odb query less_equal (ref_bind<T> r) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += "<="; q.append<T, ID> (r); return q; @@ -851,7 +866,7 @@ namespace odb query greater_equal (val_bind<T> v) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += ">="; q.append<T, ID> (v); return q; @@ -868,7 +883,7 @@ namespace odb query greater_equal (ref_bind<T> r) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += ">="; q.append<T, ID> (r); return q; @@ -935,9 +950,9 @@ namespace odb // (void) (sizeof (type_instance<T> () == type_instance<T2> ())); - query q (name_, query::clause_part::column); + query q (table_, column_); q += "="; - q.append (c.name (), query::clause_part::column); + q.append (c.table (), c.column ()); return q; } @@ -949,9 +964,9 @@ namespace odb // (void) (sizeof (type_instance<T> () != type_instance<T2> ())); - query q (name_, query::clause_part::column); + query q (table_, column_); q += "!="; - q.append (c.name (), query::clause_part::column); + q.append (c.table (), c.column ()); return q; } @@ -963,9 +978,9 @@ namespace odb // (void) (sizeof (type_instance<T> () < type_instance<T2> ())); - query q (name_, query::clause_part::column); + query q (table_, column_); q += "<"; - q.append (c.name (), query::clause_part::column); + q.append (c.table (), c.column ()); return q; } @@ -977,9 +992,9 @@ namespace odb // (void) (sizeof (type_instance<T> () > type_instance<T2> ())); - query q (name_, query::clause_part::column); + query q (table_, column_); q += ">"; - q.append (c.name (), query::clause_part::column); + q.append (c.table (), c.column ()); return q; } @@ -991,9 +1006,9 @@ namespace odb // (void) (sizeof (type_instance<T> () <= type_instance<T2> ())); - query q (name_, query::clause_part::column); + query q (table_, column_); q += "<="; - q.append (c.name (), query::clause_part::column); + q.append (c.table (), c.column ()); return q; } @@ -1005,14 +1020,15 @@ namespace odb // (void) (sizeof (type_instance<T> () >= type_instance<T2> ())); - query q (name_, query::clause_part::column); + query q (table_, column_); q += ">="; - q.append (c.name (), query::clause_part::column); + q.append (c.table (), c.column ()); return q; } private: - const char* name_; + const char* table_; + const char* column_; }; // @@ -1814,13 +1830,6 @@ namespace odb : query_selector<T>::type (qc) { } - - std::string - clause () const - { - return query_selector<T>::type::clause ( - query_selector<T>::table_name ()); - } }; } diff --git a/odb/mysql/query.txx b/odb/mysql/query.txx index 6addb65..c28155d 100644 --- a/odb/mysql/query.txx +++ b/odb/mysql/query.txx @@ -15,12 +15,11 @@ namespace odb query (const query_column<bool, ID>& c) : binding_ (0, 0) { - clause_.push_back (clause_part (clause_part::column, c.name ())); - // Cannot use IS TRUE here since database type can be a non- // integral type. // - clause_.push_back (clause_part (clause_part::native, "=")); + append (c.table (), c.column ()); + append ("="); append<bool, ID> (val_bind<bool> (true)); } @@ -30,7 +29,7 @@ namespace odb query query_column<T, ID>:: in (const T& v1, const T& v2) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += "IN ("; q.append<T, ID> (val_bind<T> (v1)); q += ","; @@ -43,7 +42,7 @@ namespace odb query query_column<T, ID>:: in (const T& v1, const T& v2, const T& v3) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += "IN ("; q.append<T, ID> (val_bind<T> (v1)); q += ","; @@ -58,7 +57,7 @@ namespace odb query query_column<T, ID>:: in (const T& v1, const T& v2, const T& v3, const T& v4) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += "IN ("; q.append<T, ID> (val_bind<T> (v1)); q += ","; @@ -75,7 +74,7 @@ namespace odb query query_column<T, ID>:: in (const T& v1, const T& v2, const T& v3, const T& v4, const T& v5) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += "IN ("; q.append<T, ID> (val_bind<T> (v1)); q += ","; @@ -95,7 +94,7 @@ namespace odb query query_column<T, ID>:: in_range (I begin, I end) const { - query q (name_, query::clause_part::column); + query q (table_, column_); q += "IN ("; for (I i (begin); i != end; ++i) |