From 6e3ac760696d4ec43138b1aba82426582c767072 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 19 Aug 2011 14:08:16 +0200 Subject: Rework query machinery not to use '_' as primary table alias Now we always qualify with the actual table name and use the '_' alias for situations where an object is referencing itself. --- odb/pgsql/query.cxx | 155 ++++++++++++++++++++++++++++------------------------ odb/pgsql/query.hxx | 99 ++++++++++++++++++++------------- odb/pgsql/query.txx | 16 +++--- 3 files changed, 152 insertions(+), 118 deletions(-) diff --git a/odb/pgsql/query.cxx b/odb/pgsql/query.cxx index 1c5f9d4..856c031 100644 --- a/odb/pgsql/query.cxx +++ b/odb/pgsql/query.cxx @@ -28,7 +28,6 @@ namespace odb query (const query& q) : clause_ (q.clause_), parameters_ (q.parameters_), - parameter_offsets_ (q.parameter_offsets_), bind_ (q.bind_), binding_ (0, 0), values_ (q.values_), @@ -70,7 +69,6 @@ namespace odb { clause_ = q.clause_; parameters_ = q.parameters_; - parameter_offsets_ = q.parameter_offsets_; bind_ = q.bind_; size_t n (bind_.size ()); @@ -108,56 +106,7 @@ namespace odb query& query:: operator+= (const query& q) { - size_t cur_pos (0); - - // Append the argument clause to the clause of this instance. - // - for (size_t i (0), - p (parameters_.size () + 1), - e (q.parameter_offsets_.size ()); - i < e; - ++i, ++p) - { - size_t n (clause_.size ()); - - if (n != 0 && clause_[n - 1] != ' ' && q.clause_[cur_pos] != ' ') - clause_ += ' '; - - parameter_offset o (q.parameter_offsets_[i]); - - // Append all characters up to the start of the current - // parameter specifier, including the '$'. - // - clause_.append (q.clause_, cur_pos, o.first - cur_pos + 1); - - // Advance current position in source clause to 1 element past - // the current parameter specifier. - // - cur_pos = o.second; - - // Insert the correct parameter number and update the parameter - // offset to describe its offset in the new clause. - // - o.first = clause_.size () - 1; - ostringstream os; - os << p; - clause_.append (os.str ()); - o.second = clause_.size (); - - parameter_offsets_.push_back (o); - } - - // Copy any remaining characters in q.clause_. - // - if (cur_pos < q.clause_.size ()) - { - size_t n (clause_.size ()); - - if (n != 0 && clause_[n - 1] != ' ' && q.clause_[cur_pos] != ' ') - clause_ += ' '; - - clause_.append (q.clause_, cur_pos, string::npos); - } + clause_.insert (clause_.end (), q.clause_.begin (), q.clause_.end ()); size_t n (bind_.size ()); @@ -204,22 +153,34 @@ namespace odb } void query:: - add (details::shared_ptr p) + append (const string& q, clause_part::kind_type k) { - size_t n (clause_.size ()); + if (k == clause_part::native && + !clause_.empty () && + clause_.back ().kind == clause_part::native) + { + string& s (clause_.back ().part); - if (n != 0 && clause_[n - 1] != ' ') - clause_ += ' '; + char first (!q.empty () ? q[0] : ' '); + char last (!s.empty () ? s[s.size () - 1] : ' '); - parameter_offset o; - o.first = clause_.size (); + // We don't want extra spaces after '(' as well as before ',' + // and ')'. + // + if (last != ' ' && last != '(' && + first != ' ' && first != ',' && first != ')') + s += ' '; - ostringstream ss; - ss << parameters_.size () + 1; - clause_ += '$' + ss.str (); + s += q; + } + else + clause_.push_back (clause_part (k, q)); + } - o.second = clause_.size (); - parameter_offsets_.push_back (o); + void query:: + add (details::shared_ptr p) + { + clause_.push_back (clause_part (clause_part::param)); parameters_.push_back (p); bind_.push_back (bind ()); @@ -288,17 +249,67 @@ namespace odb return native_binding_; } - std::string query:: - clause () const + string query:: + clause (string const& table) 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_; + 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 += ' '; + + if (i->part[0] == '.') + r += table; + + 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; + } + } + } + + if (r.empty () || + r.compare (0, 6, "WHERE ") == 0 || + r.compare (0, 9, "ORDER BY ") == 0 || + r.compare (0, 9, "GROUP BY ") == 0 || + r.compare (0, 7, "HAVING ") == 0) + return r; else - return "WHERE " + clause_; + return "WHERE " + r; } } } diff --git a/odb/pgsql/query.hxx b/odb/pgsql/query.hxx index 9e0a7d1..96fa126 100644 --- a/odb/pgsql/query.hxx +++ b/odb/pgsql/query.hxx @@ -89,15 +89,33 @@ namespace odb class LIBODB_PGSQL_EXPORT query { public: + struct clause_part + { + enum kind_type + { + column, + param, + native + }; + + clause_part (kind_type k): kind (k) {} + clause_part (kind_type k, const std::string& p): kind (k), part (p) {} + + kind_type kind; + std::string part; + }; + query () : binding_ (0, 0), native_binding_ (0, 0, 0, 0) { } explicit - query (const std::string& q) - : clause_ (q), binding_ (0, 0), native_binding_ (0, 0, 0, 0) + query (const std::string& q, + clause_part::kind_type k = clause_part::native) + : binding_ (0, 0), native_binding_ (0, 0, 0, 0) { + clause_.push_back (clause_part (k, q)); } template @@ -126,7 +144,7 @@ namespace odb public: std::string - clause () const; + clause (std::string const& default_table) const; native_binding& parameters_binding () const; @@ -165,12 +183,7 @@ namespace odb query& operator+= (const std::string& q) { - size_t n (clause_.size ()); - - if (n != 0 && clause_[n - 1] != ' ' && !q.empty () && q[0] != ' ') - clause_ += ' '; - - clause_ += q; + append (q, clause_part::native); return *this; } @@ -199,19 +212,20 @@ namespace odb void append (ref_bind); + void + append (const std::string&, clause_part::kind_type); + private: void add (details::shared_ptr); private: + typedef std::vector clause_type; typedef std::vector > parameters_type; - std::string clause_; + clause_type clause_; parameters_type parameters_; - typedef std::pair parameter_offset; - std::vector parameter_offsets_; - mutable std::vector bind_; mutable binding binding_; @@ -391,7 +405,7 @@ namespace odb query is_null () const { - query q (name_); + query q (name_, query::clause_part::column); q += "IS NULL"; return q; } @@ -399,7 +413,7 @@ namespace odb query is_not_null () const { - query q (name_); + query q (name_, query::clause_part::column); q += "IS NOT NULL"; return q; } @@ -435,7 +449,7 @@ namespace odb query equal (val_bind v) const { - query q (name_); + query q (name_, query::clause_part::column); q += "="; q.append (v); return q; @@ -452,7 +466,7 @@ namespace odb query equal (ref_bind r) const { - query q (name_); + query q (name_, query::clause_part::column); q += "="; q.append (r); return q; @@ -520,7 +534,7 @@ namespace odb query unequal (val_bind v) const { - query q (name_); + query q (name_, query::clause_part::column); q += "!="; q.append (v); return q; @@ -537,7 +551,7 @@ namespace odb query unequal (ref_bind r) const { - query q (name_); + query q (name_, query::clause_part::column); q += "!="; q.append (r); return q; @@ -605,7 +619,7 @@ namespace odb query less (val_bind v) const { - query q (name_); + query q (name_, query::clause_part::column); q += "<"; q.append (v); return q; @@ -622,7 +636,7 @@ namespace odb query less (ref_bind r) const { - query q (name_); + query q (name_, query::clause_part::column); q += "<"; q.append (r); return q; @@ -690,7 +704,7 @@ namespace odb query greater (val_bind v) const { - query q (name_); + query q (name_, query::clause_part::column); q += ">"; q.append (v); return q; @@ -707,7 +721,7 @@ namespace odb query greater (ref_bind r) const { - query q (name_); + query q (name_, query::clause_part::column); q += ">"; q.append (r); return q; @@ -775,7 +789,7 @@ namespace odb query less_equal (val_bind v) const { - query q (name_); + query q (name_, query::clause_part::column); q += "<="; q.append (v); return q; @@ -792,7 +806,7 @@ namespace odb query less_equal (ref_bind r) const { - query q (name_); + query q (name_, query::clause_part::column); q += "<="; q.append (r); return q; @@ -860,7 +874,7 @@ namespace odb query greater_equal (val_bind v) const { - query q (name_); + query q (name_, query::clause_part::column); q += ">="; q.append (v); return q; @@ -877,7 +891,7 @@ namespace odb query greater_equal (ref_bind r) const { - query q (name_); + query q (name_, query::clause_part::column); q += ">="; q.append (r); return q; @@ -944,9 +958,9 @@ namespace odb // (void) (sizeof (type_instance () == type_instance ())); - query q (name_); + query q (name_, query::clause_part::column); q += "="; - q += c.name (); + q.append (c.name (), query::clause_part::column); return q; } @@ -958,9 +972,9 @@ namespace odb // (void) (sizeof (type_instance () != type_instance ())); - query q (name_); + query q (name_, query::clause_part::column); q += "!="; - q += c.name (); + q.append (c.name (), query::clause_part::column); return q; } @@ -972,9 +986,9 @@ namespace odb // (void) (sizeof (type_instance () < type_instance ())); - query q (name_); + query q (name_, query::clause_part::column); q += "<"; - q += c.name (); + q.append (c.name (), query::clause_part::column); return q; } @@ -986,9 +1000,9 @@ namespace odb // (void) (sizeof (type_instance () > type_instance ())); - query q (name_); + query q (name_, query::clause_part::column); q += ">"; - q += c.name (); + q.append (c.name (), query::clause_part::column); return q; } @@ -1000,9 +1014,9 @@ namespace odb // (void) (sizeof (type_instance () <= type_instance ())); - query q (name_); + query q (name_, query::clause_part::column); q += "<="; - q += c.name (); + q.append (c.name (), query::clause_part::column); return q; } @@ -1014,9 +1028,9 @@ namespace odb // (void) (sizeof (type_instance () >= type_instance ())); - query q (name_); + query q (name_, query::clause_part::column); q += ">="; - q += c.name (); + q.append (c.name (), query::clause_part::column); return q; } @@ -1701,6 +1715,13 @@ namespace odb : object_traits::query_type (qc) { } + + std::string + clause () const + { + return object_traits::query_type::clause ( + object_traits::table_name); + } }; } diff --git a/odb/pgsql/query.txx b/odb/pgsql/query.txx index 7d80f2b..6fdc4fe 100644 --- a/odb/pgsql/query.txx +++ b/odb/pgsql/query.txx @@ -13,12 +13,14 @@ namespace odb template query:: query (const query_column& c) - : clause_ (c.name ()), binding_ (0, 0), native_binding_ (0, 0, 0, 0) + : binding_ (0, 0), native_binding_ (0, 0, 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_ += " = "; + clause_.push_back (clause_part (clause_part::native, "=")); append (val_bind (true)); } @@ -28,7 +30,7 @@ namespace odb query query_column:: in (const T& v1, const T& v2) const { - query q (name_); + query q (name_, query::clause_part::column); q += "IN ("; q.append (val_bind (v1)); q += ","; @@ -41,7 +43,7 @@ namespace odb query query_column:: in (const T& v1, const T& v2, const T& v3) const { - query q (name_); + query q (name_, query::clause_part::column); q += "IN ("; q.append (val_bind (v1)); q += ","; @@ -56,7 +58,7 @@ namespace odb query query_column:: in (const T& v1, const T& v2, const T& v3, const T& v4) const { - query q (name_); + query q (name_, query::clause_part::column); q += "IN ("; q.append (val_bind (v1)); q += ","; @@ -73,7 +75,7 @@ namespace odb query query_column:: in (const T& v1, const T& v2, const T& v3, const T& v4, const T& v5) const { - query q (name_); + query q (name_, query::clause_part::column); q += "IN ("; q.append (val_bind (v1)); q += ","; @@ -93,7 +95,7 @@ namespace odb query query_column:: in_range (I begin, I end) const { - query q (name_); + query q (name_, query::clause_part::column); q += "IN ("; for (I i (begin); i != end; ++i) -- cgit v1.1