aboutsummaryrefslogtreecommitdiff
path: root/odb/pgsql/query.hxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-08-19 14:08:16 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-08-19 14:08:16 +0200
commit6e3ac760696d4ec43138b1aba82426582c767072 (patch)
tree00c30ed11c75b4f2d9daf1ebb85c026a90658129 /odb/pgsql/query.hxx
parent7391841a063328828621c8b70e02a81674686c97 (diff)
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.
Diffstat (limited to 'odb/pgsql/query.hxx')
-rw-r--r--odb/pgsql/query.hxx99
1 files changed, 60 insertions, 39 deletions
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 <typename T>
@@ -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<T>);
+ void
+ append (const std::string&, clause_part::kind_type);
+
private:
void
add (details::shared_ptr<query_param>);
private:
+ typedef std::vector<clause_part> clause_type;
typedef std::vector<details::shared_ptr<query_param> > parameters_type;
- std::string clause_;
+ clause_type clause_;
parameters_type parameters_;
- typedef std::pair<std::size_t, std::size_t> parameter_offset;
- std::vector<parameter_offset> parameter_offsets_;
-
mutable std::vector<bind> 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<T> v) const
{
- query q (name_);
+ query q (name_, query::clause_part::column);
q += "=";
q.append<T, ID> (v);
return q;
@@ -452,7 +466,7 @@ namespace odb
query
equal (ref_bind<T> r) const
{
- query q (name_);
+ query q (name_, query::clause_part::column);
q += "=";
q.append<T, ID> (r);
return q;
@@ -520,7 +534,7 @@ namespace odb
query
unequal (val_bind<T> v) const
{
- query q (name_);
+ query q (name_, query::clause_part::column);
q += "!=";
q.append<T, ID> (v);
return q;
@@ -537,7 +551,7 @@ namespace odb
query
unequal (ref_bind<T> r) const
{
- query q (name_);
+ query q (name_, query::clause_part::column);
q += "!=";
q.append<T, ID> (r);
return q;
@@ -605,7 +619,7 @@ namespace odb
query
less (val_bind<T> v) const
{
- query q (name_);
+ query q (name_, query::clause_part::column);
q += "<";
q.append<T, ID> (v);
return q;
@@ -622,7 +636,7 @@ namespace odb
query
less (ref_bind<T> r) const
{
- query q (name_);
+ query q (name_, query::clause_part::column);
q += "<";
q.append<T, ID> (r);
return q;
@@ -690,7 +704,7 @@ namespace odb
query
greater (val_bind<T> v) const
{
- query q (name_);
+ query q (name_, query::clause_part::column);
q += ">";
q.append<T, ID> (v);
return q;
@@ -707,7 +721,7 @@ namespace odb
query
greater (ref_bind<T> r) const
{
- query q (name_);
+ query q (name_, query::clause_part::column);
q += ">";
q.append<T, ID> (r);
return q;
@@ -775,7 +789,7 @@ namespace odb
query
less_equal (val_bind<T> v) const
{
- query q (name_);
+ query q (name_, query::clause_part::column);
q += "<=";
q.append<T, ID> (v);
return q;
@@ -792,7 +806,7 @@ namespace odb
query
less_equal (ref_bind<T> r) const
{
- query q (name_);
+ query q (name_, query::clause_part::column);
q += "<=";
q.append<T, ID> (r);
return q;
@@ -860,7 +874,7 @@ namespace odb
query
greater_equal (val_bind<T> v) const
{
- query q (name_);
+ query q (name_, query::clause_part::column);
q += ">=";
q.append<T, ID> (v);
return q;
@@ -877,7 +891,7 @@ namespace odb
query
greater_equal (ref_bind<T> r) const
{
- query q (name_);
+ query q (name_, query::clause_part::column);
q += ">=";
q.append<T, ID> (r);
return q;
@@ -944,9 +958,9 @@ namespace odb
//
(void) (sizeof (type_instance<T> () == type_instance<T2> ()));
- 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<T> () != type_instance<T2> ()));
- 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<T> () < type_instance<T2> ()));
- 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<T> () > type_instance<T2> ()));
- 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<T> () <= type_instance<T2> ()));
- 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<T> () >= type_instance<T2> ()));
- 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<T>::query_type (qc)
{
}
+
+ std::string
+ clause () const
+ {
+ return object_traits<T>::query_type::clause (
+ object_traits<T>::table_name);
+ }
};
}