diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-10-02 17:07:20 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-10-02 17:07:20 +0200 |
commit | 2fbd31985a169c9c6a212460a01d0a78805933e3 (patch) | |
tree | fc483eb8761a8c0d6c1fb5086fc506267f54b96f | |
parent | a7bd7367e246ec5b6bb2e2f018a05173ff7e6301 (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.
-rw-r--r-- | odb/relational/source.cxx | 2 | ||||
-rw-r--r-- | odb/relational/source.hxx | 20 |
2 files changed, 16 insertions, 6 deletions
diff --git a/odb/relational/source.cxx b/odb/relational/source.cxx index 5dfc15a..fe23ac9 100644 --- a/odb/relational/source.cxx +++ b/odb/relational/source.cxx @@ -499,7 +499,7 @@ namespace relational if (tt == CPP_CLOSE_PAREN) { - r += 'q'; + r += "q.empty () ? query_base_type::true_expr : q"; *placeholder = true; } else diff --git a/odb/relational/source.hxx b/odb/relational/source.hxx index fbe9b1c..4d120db 100644 --- a/odb/relational/source.hxx +++ b/odb/relational/source.hxx @@ -3082,8 +3082,8 @@ namespace relational if (p != string::npos) { ph = true; - os << strlit (string (vq.literal, 0, p + 1)) - << " + q + " + os << strlit (string (vq.literal, 0, p + 1)) << " +" << endl + << "(q.empty () ? query_base_type::true_expr : q) +" << endl << strlit (string (vq.literal, p + 2)); } else @@ -3564,14 +3564,17 @@ namespace relational if (p != string::npos) { ph = true; - os << strlit (string (vq.literal, 0, p + 1)) - << " + q + " + os << strlit (string (vq.literal, 0, p + 1))<< " +" << endl + << "(q.empty () ? query_base_type::true_expr : q) +" << endl << strlit (string (vq.literal, p + 2)); } else os << strlit (vq.literal); + + os << ");"; } else + { // Output the pragma location for easier error tracking. // os << "// From " << @@ -3581,7 +3584,14 @@ namespace relational << translate_expression ( c, vq.expr, vq.scope, vq.loc, "query", &ph).value; - os << ");"; + os << ");"; + + // Optimize the query if it had a placeholder. This gets + // rid of useless clauses like WHERE TRUE. + // + if (ph) + os << "c.optimize ();"; + } if (!ph) os << "c += q;"; |