mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-15 20:31:16 +08:00
Unified interface for where and dynamic_where.
Compatible with previous interface. Both where and dynamic where can now take a variable amount of arguments. where() requires at least one argument.
This commit is contained in:
parent
0ad689c116
commit
4b4048bd11
@ -66,7 +66,8 @@ namespace sqlpp
|
||||
}
|
||||
}
|
||||
|
||||
void serialize(std::ostream& os, Db& db, const std::string& separator, bool first) const
|
||||
template<typename Separator>
|
||||
void serialize(std::ostream& os, Db& db, const Separator& separator, bool first) const
|
||||
{
|
||||
for (const auto entry : _serializables)
|
||||
{
|
||||
@ -100,6 +101,11 @@ namespace sqlpp
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Db, typename Separator>
|
||||
void serialize(std::ostream& os, Db& db, const Separator& separator, bool first) const
|
||||
{
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ namespace sqlpp
|
||||
|
||||
// check for invalid arguments
|
||||
using _valid_expressions = typename detail::make_set_if<is_table_t, TableOrJoin...>::type;
|
||||
static_assert(_valid_expressions::size::value == sizeof...(TableOrJoin), "at least one argument is not an table or join in from()");
|
||||
static_assert(_valid_expressions::size::value == sizeof...(TableOrJoin), "at least one argument is not a table or join in from()");
|
||||
|
||||
// FIXME: Joins contain two tables. This is not being dealt with at the moment
|
||||
|
||||
|
@ -94,26 +94,27 @@ namespace sqlpp
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename Expr>
|
||||
auto where(Expr&& where)
|
||||
-> set_where_t<where_t<typename std::decay<Expr>::type>>
|
||||
template<typename... Expr>
|
||||
auto where(Expr&&... expr)
|
||||
-> set_where_t<where_t<void, typename std::decay<Expr>::type...>>
|
||||
{
|
||||
static_assert(std::is_same<Where, noop>::value, "cannot call where() twice");
|
||||
return {
|
||||
_table,
|
||||
_using,
|
||||
{std::forward<Expr>(where)},
|
||||
{std::tuple<typename std::decay<Expr>::type...>{std::forward<Expr>(expr)...}},
|
||||
};
|
||||
}
|
||||
|
||||
auto dynamic_where()
|
||||
-> set_where_t<dynamic_where_t<Database>>
|
||||
template<typename... Expr>
|
||||
auto dynamic_where(Expr&&... expr)
|
||||
-> set_where_t<where_t<Database, typename std::decay<Expr>::type...>>
|
||||
{
|
||||
static_assert(std::is_same<Where, noop>::value, "cannot call where() twice");
|
||||
return {
|
||||
_table,
|
||||
_using,
|
||||
{{}},
|
||||
{std::tuple<typename std::decay<Expr>::type...>{std::forward<Expr>(expr)...}},
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -243,9 +243,9 @@ namespace sqlpp
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename Expr>
|
||||
auto where(Expr&& expr)
|
||||
-> set_where_t<where_t<typename std::decay<Expr>::type>>
|
||||
template<typename... Expr>
|
||||
auto where(Expr&&... expr)
|
||||
-> set_where_t<where_t<void, typename std::decay<Expr>::type...>>
|
||||
{
|
||||
static_assert(not is_noop<From>::value, "cannot call where() without a from()");
|
||||
static_assert(is_noop<Where>::value, "cannot call where() or dynamic_where() twice for a single select");
|
||||
@ -253,7 +253,7 @@ namespace sqlpp
|
||||
_flags,
|
||||
_expression_list,
|
||||
_from,
|
||||
{std::forward<Expr>(expr)},
|
||||
{std::tuple<typename std::decay<Expr>::type...>{std::forward<Expr>(expr)...}},
|
||||
_group_by,
|
||||
_having,
|
||||
_order_by,
|
||||
@ -262,8 +262,9 @@ namespace sqlpp
|
||||
};
|
||||
}
|
||||
|
||||
auto dynamic_where()
|
||||
-> set_where_t<dynamic_where_t<Database>>
|
||||
template<typename... Expr>
|
||||
auto dynamic_where(Expr&&... expr)
|
||||
-> set_where_t<where_t<Database, typename std::decay<Expr>::type...>>
|
||||
{
|
||||
static_assert(not is_noop<From>::value, "cannot call dynamic_where() without a from()");
|
||||
static_assert(is_noop<Where>::value, "cannot call where() or dynamic_where() twice for a single select");
|
||||
@ -271,7 +272,7 @@ namespace sqlpp
|
||||
_flags,
|
||||
_expression_list,
|
||||
_from,
|
||||
{{}},
|
||||
{std::tuple<typename std::decay<Expr>::type...>{std::forward<Expr>(expr)...}},
|
||||
_group_by,
|
||||
_having,
|
||||
_order_by,
|
||||
@ -283,7 +284,7 @@ namespace sqlpp
|
||||
template<typename Expr>
|
||||
select_t& add_where(Expr&& expr)
|
||||
{
|
||||
static_assert(is_dynamic_t<Where>::value, "cannot call add_where() in a non-dynamic where");
|
||||
static_assert(is_dynamic_t<Where>::value, "cannot call add_where() with a non-dynamic where");
|
||||
|
||||
_where.add(std::forward<Expr>(expr));
|
||||
|
||||
|
@ -146,6 +146,7 @@ namespace sqlpp
|
||||
void add(Expr&& namedExpr)
|
||||
{
|
||||
static_assert(is_named_expression_t<typename std::decay<Expr>::type>::value, "select() arguments require to be named expressions");
|
||||
static_assert(_is_dynamic::value, "cannot add columns to a non-dynamic column list");
|
||||
_dynamic_expressions.push_back(std::forward<Expr>(namedExpr));
|
||||
}
|
||||
|
||||
|
@ -42,7 +42,7 @@ namespace sqlpp
|
||||
|
||||
template<typename... TableOrJoin> struct from_t;
|
||||
|
||||
template<typename Expr> struct where_t;
|
||||
template<typename Database, typename... Expr> struct where_t;
|
||||
|
||||
template<typename... Expr> struct group_by_t;
|
||||
|
||||
|
@ -94,28 +94,29 @@ namespace sqlpp
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename Expr>
|
||||
auto where(Expr&& where)
|
||||
-> set_where_t<where_t<typename std::decay<Expr>::type>>
|
||||
template<typename... Expr>
|
||||
auto where(Expr&&... expr)
|
||||
-> set_where_t<where_t<void, typename std::decay<Expr>::type...>>
|
||||
{
|
||||
static_assert(not std::is_same<Assignments, noop>::value, "cannot call where() if set() hasn't been called yet");
|
||||
static_assert(std::is_same<Where, noop>::value, "cannot call where() twice");
|
||||
return {
|
||||
_table,
|
||||
_assignments,
|
||||
{std::forward<Expr>(where)},
|
||||
{std::tuple<typename std::decay<Expr>::type...>{std::forward<Expr>(expr)...}},
|
||||
};
|
||||
}
|
||||
|
||||
auto dynamic_where()
|
||||
-> set_where_t<dynamic_where_t<Database>>
|
||||
template<typename... Expr>
|
||||
auto dynamic_where(Expr&&... expr)
|
||||
-> set_where_t<where_t<Database, typename std::decay<Expr>::type...>>
|
||||
{
|
||||
static_assert(not std::is_same<Assignments, noop>::value, "cannot call where() if set() hasn't been called yet");
|
||||
static_assert(std::is_same<Where, noop>::value, "cannot call where() twice");
|
||||
return {
|
||||
_table,
|
||||
_assignments,
|
||||
{{}},
|
||||
{std::tuple<typename std::decay<Expr>::type...>{std::forward<Expr>(expr)...}},
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -32,54 +32,42 @@
|
||||
#include <sqlpp11/expression.h>
|
||||
#include <sqlpp11/type_traits.h>
|
||||
#include <sqlpp11/detail/set.h>
|
||||
#include <sqlpp11/detail/serialize_tuple.h>
|
||||
#include <sqlpp11/detail/serializable_list.h>
|
||||
|
||||
namespace sqlpp
|
||||
{
|
||||
template<typename Expr>
|
||||
template<typename Database, typename... Expr>
|
||||
struct where_t
|
||||
{
|
||||
static_assert(is_expression_t<Expr>::value, "invalid expression argument in where()");
|
||||
|
||||
using _is_where = std::true_type;
|
||||
using _is_dynamic = typename std::conditional<std::is_same<Database, void>::value, std::false_type, std::true_type>::type;
|
||||
|
||||
static_assert(_is_dynamic::value or sizeof...(Expr), "at least one expression argument required in where()");
|
||||
using _valid_expressions = typename detail::make_set_if<is_expression_t, Expr...>::type;
|
||||
static_assert(_valid_expressions::size::value == sizeof...(Expr), "at least one argument is not an expression in where()");
|
||||
|
||||
template<typename E>
|
||||
void add(E&& expr)
|
||||
{
|
||||
static_assert(is_expression_t<typename std::decay<E>::type>::value, "invalid expression argument in add_where()");
|
||||
static_assert(_is_dynamic::value, "cannot add expressions to a non-dynamic where()");
|
||||
_dynamic_expressions.emplace_back(std::forward<E>(expr));
|
||||
}
|
||||
|
||||
template<typename Db>
|
||||
void serialize(std::ostream& os, Db& db) const
|
||||
{
|
||||
if (sizeof...(Expr) == 0 and _dynamic_expressions.empty())
|
||||
return;
|
||||
os << " WHERE ";
|
||||
_expr.serialize(os, db);
|
||||
detail::serialize_tuple(os, db, _expressions, " AND ");
|
||||
_dynamic_expressions.serialize(os, db, " AND ", true);
|
||||
}
|
||||
|
||||
Expr _expr;
|
||||
std::tuple<Expr...> _expressions;
|
||||
detail::serializable_list<Database> _dynamic_expressions;
|
||||
};
|
||||
|
||||
template<typename Db>
|
||||
struct dynamic_where_t
|
||||
{
|
||||
|
||||
using _is_where = std::true_type;
|
||||
using _is_dynamic = std::true_type;
|
||||
|
||||
template<typename Expr>
|
||||
void add(Expr&& expr)
|
||||
{
|
||||
static_assert(is_expression_t<typename std::decay<Expr>::type>::value, "invalid expression argument in where()");
|
||||
_conditions.emplace_back(std::forward<Expr>(expr));
|
||||
}
|
||||
|
||||
void serialize(std::ostream& os, Db& db) const
|
||||
{
|
||||
if (_conditions.empty())
|
||||
return;
|
||||
|
||||
os << " WHERE ";
|
||||
bool first = true;
|
||||
_conditions.serialize(os, db, " AND ", true);
|
||||
}
|
||||
|
||||
detail::serializable_list<Db> _conditions;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user