From ad1c5f94bbbd46c93306db16b87a0ee2fbe6c45b Mon Sep 17 00:00:00 2001 From: rbock Date: Tue, 12 Aug 2014 18:48:50 +0200 Subject: [PATCH] Made boolean expression more usable --- include/sqlpp11/boolean_expression.h | 31 ++++++++++++++-------------- tests/BooleanExpressionTest.cpp | 4 +++- tests/InterpretTest.cpp | 16 ++++++++------ 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/include/sqlpp11/boolean_expression.h b/include/sqlpp11/boolean_expression.h index b2c66acc..5463f94f 100644 --- a/include/sqlpp11/boolean_expression.h +++ b/include/sqlpp11/boolean_expression.h @@ -33,17 +33,17 @@ namespace sqlpp { template - struct boolean_expression_t + struct boolean_expression_t: public boolean::template expression_operators> { using _traits = make_traits; using _recursive_traits = make_recursive_traits<>; - template - boolean_expression_t(Expression expression): - _expression(expression) + template + boolean_expression_t(Expr expr): + _expr(expr) { - static_assert(is_expression_t::value, "boolean_expression requires a boolean expression argument"); - static_assert(is_boolean_t::value, "boolean_expression requires a boolean expression argument"); + static_assert(is_expression_t::value, "boolean_expression requires a boolean expression argument"); + static_assert(is_boolean_t::value, "boolean_expression requires a boolean expression argument"); } boolean_expression_t(const boolean_expression_t&) = default; @@ -52,19 +52,20 @@ namespace sqlpp boolean_expression_t& operator=(boolean_expression_t&&) = default; ~boolean_expression_t() = default; - interpretable_t _expression; + interpretable_t _expr; }; - template - boolean_expression_t boolean_expression(const Database&, T t) - { - return {t}; - } - template boolean_expression_t boolean_expression(T t) { - return {t}; + using Expr = wrap_operand_t; + return {Expr{t}}; + } + + template + boolean_expression_t boolean_expression(const Database&, T t) + { + return boolean_expression(t); } template @@ -74,7 +75,7 @@ namespace sqlpp static Context& _(const T& t, Context& context) { - return serialize(t._expression); + return serialize(t._expr, context); } }; diff --git a/tests/BooleanExpressionTest.cpp b/tests/BooleanExpressionTest.cpp index 3eaf8544..6f0892b9 100644 --- a/tests/BooleanExpressionTest.cpp +++ b/tests/BooleanExpressionTest.cpp @@ -35,8 +35,10 @@ int main() test::TabBar t; auto x = boolean_expression(db, t.alpha == 7); - x = boolean_expression(db, t.gamma); x = sqlpp::boolean_expression(t.beta.like("%cheesecake")); + x = x and boolean_expression(db, t.gamma); + + db(select(t.alpha).from(t).where(x)); return 0; } diff --git a/tests/InterpretTest.cpp b/tests/InterpretTest.cpp index 13209e46..6d0943ac 100644 --- a/tests/InterpretTest.cpp +++ b/tests/InterpretTest.cpp @@ -25,12 +25,7 @@ #include "Sample.h" #include "MockDb.h" -#include -#include -#include -#include -#include -#include +#include #include @@ -177,5 +172,14 @@ int main() flatten(t.alpha == 7, db); + auto x = boolean_expression(db, t.alpha == 7); + x = sqlpp::boolean_expression(t.beta.like("%cheesecake")); + x = x and boolean_expression(db, t.gamma); + std::cerr << "----------------------------" << std::endl; + printer.reset(); + std::cerr << serialize(x, printer).str() << std::endl; + + + return 0; }