aboutsummaryrefslogtreecommitdiff
path: root/odb/pgsql/query.hxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-10-02 17:07:20 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-10-02 17:07:20 +0200
commit7d3b47fc855c183a0269ee4fb8c66d5ef9545f4d (patch)
tree4af3a222a1a48893e280366ce840d38c386e96b0 /odb/pgsql/query.hxx
parent219d6ffefb5f71fc2a262f8b5c93f2f88e9aecd9 (diff)
If query substituting placeholder is empty, pass true expression instead
This allows uniform usage of views both with and without any extra conditions. Also optimize some common cases so that we don't have useless WHERE TRUE clauses or (...) AND (TRUE) expressions.
Diffstat (limited to 'odb/pgsql/query.hxx')
-rw-r--r--odb/pgsql/query.hxx62
1 files changed, 31 insertions, 31 deletions
diff --git a/odb/pgsql/query.hxx b/odb/pgsql/query.hxx
index 438f2f5..0c3f837 100644
--- a/odb/pgsql/query.hxx
+++ b/odb/pgsql/query.hxx
@@ -95,14 +95,17 @@ namespace odb
{
column,
param,
- native
+ native,
+ boolean
};
clause_part (kind_type k): kind (k) {}
clause_part (kind_type k, const std::string& p): kind (k), part (p) {}
+ clause_part (bool p): kind (boolean), bool_part (p) {}
kind_type kind;
std::string part;
+ bool bool_part;
};
query ()
@@ -116,8 +119,7 @@ namespace odb
query (bool v)
: binding_ (0, 0), native_binding_ (0, 0, 0, 0)
{
- clause_.push_back (
- clause_part (clause_part::native, v ? "TRUE" : "FALSE"));
+ clause_.push_back (clause_part (v));
}
explicit
@@ -187,6 +189,26 @@ namespace odb
}
public:
+ bool
+ empty () const
+ {
+ return clause_.empty ();
+ }
+
+ static const query true_expr;
+
+ bool
+ const_true () const
+ {
+ return clause_.size () == 1 &&
+ clause_.front ().kind == clause_part::boolean &&
+ clause_.front ().bool_part;
+ }
+
+ void
+ optimize ();
+
+ public:
template <typename T>
static val_bind<T>
_val (const T& x)
@@ -364,36 +386,14 @@ namespace odb
return r;
}
- inline query
- operator&& (const query& x, const query& y)
- {
- query r ("(");
- r += x;
- r += ") AND (";
- r += y;
- r += ")";
- return r;
- }
+ query
+ operator&& (const query&, const query&);
- inline query
- operator|| (const query& x, const query& y)
- {
- query r ("(");
- r += x;
- r += ") OR (";
- r += y;
- r += ")";
- return r;
- }
+ query
+ operator|| (const query&, const query&);
- inline query
- operator! (const query& x)
- {
- query r ("NOT (");
- r += x;
- r += ")";
- return r;
- }
+ query
+ operator! (const query&);
// query_column
//