0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 04:47:18 +08:00

Made boolean expression more usable

This commit is contained in:
rbock 2014-08-12 18:48:50 +02:00
parent c0902f8683
commit ad1c5f94bb
3 changed files with 29 additions and 22 deletions

View File

@ -33,17 +33,17 @@
namespace sqlpp namespace sqlpp
{ {
template<typename Database> template<typename Database>
struct boolean_expression_t struct boolean_expression_t: public boolean::template expression_operators<boolean_expression_t<Database>>
{ {
using _traits = make_traits<boolean, tag::is_expression>; using _traits = make_traits<boolean, tag::is_expression>;
using _recursive_traits = make_recursive_traits<>; using _recursive_traits = make_recursive_traits<>;
template<typename Expression> template<typename Expr>
boolean_expression_t(Expression expression): boolean_expression_t(Expr expr):
_expression(expression) _expr(expr)
{ {
static_assert(is_expression_t<Expression>::value, "boolean_expression requires a boolean expression argument"); static_assert(is_expression_t<Expr>::value, "boolean_expression requires a boolean expression argument");
static_assert(is_boolean_t<Expression>::value, "boolean_expression requires a boolean expression argument"); static_assert(is_boolean_t<Expr>::value, "boolean_expression requires a boolean expression argument");
} }
boolean_expression_t(const boolean_expression_t&) = default; 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& operator=(boolean_expression_t&&) = default;
~boolean_expression_t() = default; ~boolean_expression_t() = default;
interpretable_t<Database> _expression; interpretable_t<Database> _expr;
}; };
template<typename Database, typename T>
boolean_expression_t<Database> boolean_expression(const Database&, T t)
{
return {t};
}
template<typename Database, typename T> template<typename Database, typename T>
boolean_expression_t<Database> boolean_expression(T t) boolean_expression_t<Database> boolean_expression(T t)
{ {
return {t}; using Expr = wrap_operand_t<T>;
return {Expr{t}};
}
template<typename Database, typename T>
boolean_expression_t<Database> boolean_expression(const Database&, T t)
{
return boolean_expression<Database>(t);
} }
template<typename Context, typename Database> template<typename Context, typename Database>
@ -74,7 +75,7 @@ namespace sqlpp
static Context& _(const T& t, Context& context) static Context& _(const T& t, Context& context)
{ {
return serialize(t._expression); return serialize(t._expr, context);
} }
}; };

View File

@ -35,8 +35,10 @@ int main()
test::TabBar t; test::TabBar t;
auto x = boolean_expression(db, t.alpha == 7); auto x = boolean_expression(db, t.alpha == 7);
x = boolean_expression(db, t.gamma);
x = sqlpp::boolean_expression<MockDb>(t.beta.like("%cheesecake")); x = sqlpp::boolean_expression<MockDb>(t.beta.like("%cheesecake"));
x = x and boolean_expression(db, t.gamma);
db(select(t.alpha).from(t).where(x));
return 0; return 0;
} }

View File

@ -25,12 +25,7 @@
#include "Sample.h" #include "Sample.h"
#include "MockDb.h" #include "MockDb.h"
#include <sqlpp11/alias_provider.h> #include <sqlpp11/sqlpp11.h>
#include <sqlpp11/insert.h>
#include <sqlpp11/select.h>
#include <sqlpp11/update.h>
#include <sqlpp11/remove.h>
#include <sqlpp11/functions.h>
#include <iostream> #include <iostream>
@ -177,5 +172,14 @@ int main()
flatten(t.alpha == 7, db); flatten(t.alpha == 7, db);
auto x = boolean_expression(db, t.alpha == 7);
x = sqlpp::boolean_expression<MockDb>(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; return 0;
} }