aboutsummaryrefslogtreecommitdiff
path: root/odb/mysql/query.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-10-02 17:07:19 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-10-02 17:07:19 +0200
commitd3da494f32febfdf900812ea59db9ae1ce5a76cf (patch)
tree579f736fac20289f822322dbc9d75bdf14357633 /odb/mysql/query.cxx
parentf2fcf90f47cf3ee8b1fc93ddd3d340279bf1d369 (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/mysql/query.cxx')
-rw-r--r--odb/mysql/query.cxx139
1 files changed, 111 insertions, 28 deletions
diff --git a/odb/mysql/query.cxx b/odb/mysql/query.cxx
index c87fee5..57ebc11 100644
--- a/odb/mysql/query.cxx
+++ b/odb/mysql/query.cxx
@@ -21,6 +21,10 @@ namespace odb
{
}
+ // query
+ //
+ const query query::true_expr (true);
+
query::
query (const query& q)
: clause_ (q.clause_),
@@ -166,6 +170,56 @@ namespace odb
return r;
}
+ static bool
+ check_prefix (const string& s)
+ {
+ string::size_type n;
+
+ // It is easier to compare to upper and lower-case versions
+ // rather than getting involved with the portable case-
+ // insensitive string comparison mess.
+ //
+ if (s.compare (0, (n = 5), "WHERE") == 0 ||
+ s.compare (0, (n = 5), "where") == 0 ||
+ s.compare (0, (n = 6), "SELECT") == 0 ||
+ s.compare (0, (n = 6), "select") == 0 ||
+ s.compare (0, (n = 8), "ORDER BY") == 0 ||
+ s.compare (0, (n = 8), "order by") == 0 ||
+ s.compare (0, (n = 8), "GROUP BY") == 0 ||
+ s.compare (0, (n = 8), "group by") == 0 ||
+ s.compare (0, (n = 6), "HAVING") == 0 ||
+ s.compare (0, (n = 6), "having") == 0)
+ {
+ // It either has to be an exact match, or there should be
+ // a whitespace following the keyword.
+ //
+ if (s.size () == n || s[n] == ' ' || s[n] =='\t')
+ return true;
+ }
+
+ return false;
+ }
+
+ void query::
+ optimize ()
+ {
+ // Remove a single TRUE literal or one that is followe by one of
+ // the other clauses. This avoids usless WHERE clauses like
+ //
+ // WHERE TRUE GROUP BY foo
+ //
+ clause_type::iterator i (clause_.begin ()), e (clause_.end ());
+
+ if (i != e && i->kind == clause_part::boolean && i->bool_part)
+ {
+ clause_type::iterator j (i + 1);
+
+ if (j == e ||
+ (j->kind == clause_part::native && check_prefix (j->part)))
+ clause_.erase (i);
+ }
+ }
+
const char* query::
clause_prefix () const
{
@@ -173,33 +227,8 @@ namespace odb
{
const clause_part& p (clause_.front ());
- if (p.kind == clause_part::native)
- {
- const string& s (p.part);
- string::size_type n;
-
- // It is easier to compare to upper and lower-case versions
- // rather than getting involved with the portable case-
- // insensitive string comparison mess.
- //
- if (s.compare (0, (n = 5), "WHERE") == 0 ||
- s.compare (0, (n = 5), "where") == 0 ||
- s.compare (0, (n = 6), "SELECT") == 0 ||
- s.compare (0, (n = 6), "select") == 0 ||
- s.compare (0, (n = 8), "ORDER BY") == 0 ||
- s.compare (0, (n = 8), "order by") == 0 ||
- s.compare (0, (n = 8), "GROUP BY") == 0 ||
- s.compare (0, (n = 8), "group by") == 0 ||
- s.compare (0, (n = 6), "HAVING") == 0 ||
- s.compare (0, (n = 6), "having") == 0)
- {
- // It either has to be an exact match, or there should be
- // a whitespace following the keyword.
- //
- if (s.size () == n || s[n] == ' ' || s[n] =='\t')
- return "";
- }
- }
+ if (p.kind == clause_part::native && check_prefix (p.part))
+ return "";
return "WHERE ";
}
@@ -213,7 +242,9 @@ namespace odb
string r;
for (clause_type::const_iterator i (clause_.begin ()),
- end (clause_.end ()); i != end; ++i)
+ end (clause_.end ());
+ i != end;
+ ++i)
{
char last (!r.empty () ? r[r.size () - 1] : ' ');
@@ -250,10 +281,62 @@ namespace odb
r += p;
break;
}
+ case clause_part::boolean:
+ {
+ if (last != ' ' && last != '(')
+ r += ' ';
+
+ r += i->bool_part ? "TRUE" : "FALSE";
+ break;
+ }
}
}
return clause_prefix () + r;
}
+
+ query
+ operator&& (const query& x, const query& y)
+ {
+ // Optimize cases where one or both sides are constant truth.
+ //
+ bool xt (x.const_true ()), yt (y.const_true ());
+
+ if (xt && yt)
+ return x;
+
+ if (xt)
+ return y;
+
+ if (yt)
+ return x;
+
+ query r ("(");
+ r += x;
+ r += ") AND (";
+ r += y;
+ r += ")";
+ return r;
+ }
+
+ query
+ operator|| (const query& x, const query& y)
+ {
+ query r ("(");
+ r += x;
+ r += ") OR (";
+ r += y;
+ r += ")";
+ return r;
+ }
+
+ query
+ operator! (const query& x)
+ {
+ query r ("NOT (");
+ r += x;
+ r += ")";
+ return r;
+ }
}
}