From 2fbd31985a169c9c6a212460a01d0a78805933e3 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 2 Oct 2011 17:07:20 +0200 Subject: 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. --- odb/relational/source.cxx | 2 +- odb/relational/source.hxx | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) (limited to 'odb/relational') 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;"; -- cgit v1.1