0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

interpret works for where()

This commit is contained in:
rbock 2014-01-12 16:01:38 +01:00
parent 1d3ea8516f
commit d56d1422cd
4 changed files with 35 additions and 28 deletions

View File

@ -73,27 +73,27 @@ namespace sqlpp
equal_t<Base, typename Constraint<T>::type> operator==(T&& t) const equal_t<Base, typename Constraint<T>::type> operator==(T&& t) const
{ {
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand"); static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), std::forward<T>(t) }; return { *static_cast<const Base*>(this), {std::forward<T>(t)} };
} }
template<typename T> template<typename T>
not_equal_t<Base, typename Constraint<T>::type> operator!=(T&& t) const not_equal_t<Base, typename Constraint<T>::type> operator!=(T&& t) const
{ {
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand"); static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), std::forward<T>(t) }; return { *static_cast<const Base*>(this), {std::forward<T>(t)} };
} }
template<typename T> template<typename T>
binary_expression_t<Base, lt_, typename Constraint<T>::type> operator<(T&& t) const binary_expression_t<Base, lt_, typename Constraint<T>::type> operator<(T&& t) const
{ {
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand"); static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), std::forward<T>(t) }; return { *static_cast<const Base*>(this), {std::forward<T>(t)} };
} }
template<typename T> template<typename T>
binary_expression_t<Base, le_, typename Constraint<T>::type> operator<=(T&& t) const binary_expression_t<Base, le_, typename Constraint<T>::type> operator<=(T&& t) const
{ {
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand"); static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), std::forward<T>(t) }; return { *static_cast<const Base*>(this), {std::forward<T>(t)} };
} }
template<typename T> template<typename T>

View File

@ -75,10 +75,9 @@ namespace sqlpp
{ {
using _value_type = detail::boolean; using _value_type = detail::boolean;
template<typename L, typename R> equal_t(Lhs lhs, Rhs rhs):
equal_t(L&& l, R&& r): _lhs(lhs),
_lhs(std::forward<L>(l)), _rhs(rhs)
_rhs(std::forward<R>(r))
{} {}
equal_t(const equal_t&) = default; equal_t(const equal_t&) = default;
@ -87,7 +86,6 @@ namespace sqlpp
equal_t& operator=(equal_t&&) = default; equal_t& operator=(equal_t&&) = default;
~equal_t() = default; ~equal_t() = default;
private:
Lhs _lhs; Lhs _lhs;
Rhs _rhs; Rhs _rhs;
}; };
@ -97,7 +95,7 @@ namespace sqlpp
{ {
using T = equal_t<Lhs, Rhs>; using T = equal_t<Lhs, Rhs>;
static Context& interpret(const T& t, Context& context) static Context& _(const T& t, Context& context)
{ {
context << "("; context << "(";
interpret(t._lhs, context); interpret(t._lhs, context);
@ -120,10 +118,9 @@ namespace sqlpp
{ {
using _value_type = detail::boolean; using _value_type = detail::boolean;
template<typename L, typename R> not_equal_t(Lhs lhs, Rhs rhs):
not_equal_t(L&& l, R&& r): _lhs(lhs),
_lhs(std::forward<L>(l)), _rhs(rhs)
_rhs(std::forward<R>(r))
{} {}
not_equal_t(const not_equal_t&) = default; not_equal_t(const not_equal_t&) = default;
@ -141,7 +138,7 @@ namespace sqlpp
{ {
using T = not_equal_t<Lhs, Rhs>; using T = not_equal_t<Lhs, Rhs>;
static Context& interpret(const T& t, Context& context) static Context& _(const T& t, Context& context)
{ {
context << "("; context << "(";
interpret(t._lhs, context); interpret(t._lhs, context);
@ -182,7 +179,7 @@ namespace sqlpp
{ {
using T = not_t<Lhs>; using T = not_t<Lhs>;
static Context& interpret(const T& t, Context& context) static Context& _(const T& t, Context& context)
{ {
context << "("; context << "(";
if (trivial_value_is_null_t<Lhs>::value and t._lhs._is_trivial()) if (trivial_value_is_null_t<Lhs>::value and t._lhs._is_trivial())
@ -225,7 +222,7 @@ namespace sqlpp
{ {
using T = binary_expression_t<Lhs, O, Rhs>; using T = binary_expression_t<Lhs, O, Rhs>;
static Context& interpret(const T& t, Context& context) static Context& _(const T& t, Context& context)
{ {
context << "("; context << "(";
interpret(t._lhs, context); interpret(t._lhs, context);

View File

@ -62,22 +62,30 @@ namespace sqlpp
template<typename Db> template<typename Db>
void serialize(std::ostream& os, Db& db) const void serialize(std::ostream& os, Db& db) const
{ {
if (sizeof...(Expr) == 0 and _dynamic_expressions.empty())
return;
os << " WHERE ";
detail::serialize_tuple(os, db, _expressions, " AND ");
_dynamic_expressions.serialize(os, db, " AND ", sizeof...(Expr) == 0);
} }
size_t _set_parameter_index(size_t index)
{
index = set_parameter_index(_expressions, index);
return index;
}
_parameter_tuple_t _expressions; _parameter_tuple_t _expressions;
detail::serializable_list<Database> _dynamic_expressions; detail::serializable_list<Database> _dynamic_expressions;
}; };
template<typename Context, typename Database, typename... Expr>
struct interpreter_t<Context, where_t<Database, Expr...>>
{
using T = where_t<Database, Expr...>;
static Context& _(const T& t, Context& context)
{
if (sizeof...(Expr) == 0 and t._dynamic_expressions.empty())
return context;
context << " WHERE ";
interpret_tuple(t._expressions, " AND ", context);
if (sizeof...(Expr) and not t._dynamic_expressions.empty())
context << " AND ";
interpret_serializable_list(t._dynamic_expressions, " AND ", context);
return context;
}
};
} }
#endif #endif

View File

@ -39,11 +39,13 @@ int main()
interpret(t.alpha, printer).flush(); interpret(t.alpha, printer).flush();
interpret(t.alpha = 7, printer).flush(); interpret(t.alpha = 7, printer).flush();
interpret(t.alpha == 7, printer).flush();
sqlpp::text_operand o = {"kaesekuchen"}; sqlpp::text_operand o = {"kaesekuchen"};
interpret(t.beta + "kaesekuchen", printer).flush(); interpret(t.beta + "kaesekuchen", printer).flush();
interpret(select(sqlpp::distinct, t.alpha, t.beta), printer).flush(); interpret(select(sqlpp::distinct, t.alpha, t.beta), printer).flush();
interpret(select(sqlpp::distinct, t.alpha, t.beta).from(t), printer).flush(); interpret(select(sqlpp::distinct, t.alpha, t.beta).from(t), printer).flush();
interpret(select(sqlpp::distinct, t.alpha, t.beta).from(t).where(t.alpha == 3), printer).flush();
return 0; return 0;
} }