From 894c72c73dc07ce417e5411bd3ac50f4c0461a60 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/sqlite/query.hxx | 61 ++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 30 deletions(-) (limited to 'odb/sqlite/query.hxx') diff --git a/odb/sqlite/query.hxx b/odb/sqlite/query.hxx index c712b58..a3c63ba 100644 --- a/odb/sqlite/query.hxx +++ b/odb/sqlite/query.hxx @@ -121,14 +121,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 () @@ -142,7 +145,7 @@ namespace odb query (bool v) : parameters_ (new (details::shared) query_params) { - clause_.push_back (clause_part (clause_part::native, v ? "1" : "0")); + clause_.push_back (clause_part (v)); } explicit @@ -203,6 +206,26 @@ namespace odb parameters () const; 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 static val_bind _val (const T& x) @@ -370,36 +393,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 // -- cgit v1.1