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.hxx | 99 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 39 deletions(-) (limited to 'odb/pgsql/query.hxx') 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); + } }; } -- cgit v1.1