From 4b4048bd11ec5db6f77e1fb32b4cc94dba7f3a24 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sat, 16 Nov 2013 09:37:38 +0100 Subject: [PATCH] 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. --- include/sqlpp11/detail/serializable_list.h | 8 +++- include/sqlpp11/from.h | 2 +- include/sqlpp11/remove.h | 15 +++--- include/sqlpp11/select.h | 17 +++---- include/sqlpp11/select_expression_list.h | 1 + include/sqlpp11/select_fwd.h | 2 +- include/sqlpp11/update.h | 15 +++--- include/sqlpp11/where.h | 54 +++++++++------------- 8 files changed, 56 insertions(+), 58 deletions(-) diff --git a/include/sqlpp11/detail/serializable_list.h b/include/sqlpp11/detail/serializable_list.h index 60aa2bdc..67bc0c48 100644 --- a/include/sqlpp11/detail/serializable_list.h +++ b/include/sqlpp11/detail/serializable_list.h @@ -66,7 +66,8 @@ namespace sqlpp } } - void serialize(std::ostream& os, Db& db, const std::string& separator, bool first) const + template + 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 + void serialize(std::ostream& os, Db& db, const Separator& separator, bool first) const + { + } + }; } diff --git a/include/sqlpp11/from.h b/include/sqlpp11/from.h index f6a381e5..ee63fdcc 100644 --- a/include/sqlpp11/from.h +++ b/include/sqlpp11/from.h @@ -49,7 +49,7 @@ namespace sqlpp // check for invalid arguments using _valid_expressions = typename detail::make_set_if::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 diff --git a/include/sqlpp11/remove.h b/include/sqlpp11/remove.h index e5a67958..5a88ad40 100644 --- a/include/sqlpp11/remove.h +++ b/include/sqlpp11/remove.h @@ -94,26 +94,27 @@ namespace sqlpp return *this; } - template - auto where(Expr&& where) - -> set_where_t::type>> + template + auto where(Expr&&... expr) + -> set_where_t::type...>> { static_assert(std::is_same::value, "cannot call where() twice"); return { _table, _using, - {std::forward(where)}, + {std::tuple::type...>{std::forward(expr)...}}, }; } - auto dynamic_where() - -> set_where_t> + template + auto dynamic_where(Expr&&... expr) + -> set_where_t::type...>> { static_assert(std::is_same::value, "cannot call where() twice"); return { _table, _using, - {{}}, + {std::tuple::type...>{std::forward(expr)...}}, }; } diff --git a/include/sqlpp11/select.h b/include/sqlpp11/select.h index c615b826..5f215d2d 100644 --- a/include/sqlpp11/select.h +++ b/include/sqlpp11/select.h @@ -243,9 +243,9 @@ namespace sqlpp return *this; } - template - auto where(Expr&& expr) - -> set_where_t::type>> + template + auto where(Expr&&... expr) + -> set_where_t::type...>> { static_assert(not is_noop::value, "cannot call where() without a from()"); static_assert(is_noop::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)}, + {std::tuple::type...>{std::forward(expr)...}}, _group_by, _having, _order_by, @@ -262,8 +262,9 @@ namespace sqlpp }; } - auto dynamic_where() - -> set_where_t> + template + auto dynamic_where(Expr&&... expr) + -> set_where_t::type...>> { static_assert(not is_noop::value, "cannot call dynamic_where() without a from()"); static_assert(is_noop::value, "cannot call where() or dynamic_where() twice for a single select"); @@ -271,7 +272,7 @@ namespace sqlpp _flags, _expression_list, _from, - {{}}, + {std::tuple::type...>{std::forward(expr)...}}, _group_by, _having, _order_by, @@ -283,7 +284,7 @@ namespace sqlpp template select_t& add_where(Expr&& expr) { - static_assert(is_dynamic_t::value, "cannot call add_where() in a non-dynamic where"); + static_assert(is_dynamic_t::value, "cannot call add_where() with a non-dynamic where"); _where.add(std::forward(expr)); diff --git a/include/sqlpp11/select_expression_list.h b/include/sqlpp11/select_expression_list.h index f39b57c7..3fb6e0f5 100644 --- a/include/sqlpp11/select_expression_list.h +++ b/include/sqlpp11/select_expression_list.h @@ -146,6 +146,7 @@ namespace sqlpp void add(Expr&& namedExpr) { static_assert(is_named_expression_t::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(namedExpr)); } diff --git a/include/sqlpp11/select_fwd.h b/include/sqlpp11/select_fwd.h index 48efba87..fa62388b 100644 --- a/include/sqlpp11/select_fwd.h +++ b/include/sqlpp11/select_fwd.h @@ -42,7 +42,7 @@ namespace sqlpp template struct from_t; - template struct where_t; + template struct where_t; template struct group_by_t; diff --git a/include/sqlpp11/update.h b/include/sqlpp11/update.h index bfc074d5..60cc9fec 100644 --- a/include/sqlpp11/update.h +++ b/include/sqlpp11/update.h @@ -94,28 +94,29 @@ namespace sqlpp return *this; } - template - auto where(Expr&& where) - -> set_where_t::type>> + template + auto where(Expr&&... expr) + -> set_where_t::type...>> { static_assert(not std::is_same::value, "cannot call where() if set() hasn't been called yet"); static_assert(std::is_same::value, "cannot call where() twice"); return { _table, _assignments, - {std::forward(where)}, + {std::tuple::type...>{std::forward(expr)...}}, }; } - auto dynamic_where() - -> set_where_t> + template + auto dynamic_where(Expr&&... expr) + -> set_where_t::type...>> { static_assert(not std::is_same::value, "cannot call where() if set() hasn't been called yet"); static_assert(std::is_same::value, "cannot call where() twice"); return { _table, _assignments, - {{}}, + {std::tuple::type...>{std::forward(expr)...}}, }; } diff --git a/include/sqlpp11/where.h b/include/sqlpp11/where.h index 6c03f95f..98b9d270 100644 --- a/include/sqlpp11/where.h +++ b/include/sqlpp11/where.h @@ -32,54 +32,42 @@ #include #include #include +#include #include namespace sqlpp { - template + template struct where_t { - static_assert(is_expression_t::value, "invalid expression argument in where()"); - using _is_where = std::true_type; + using _is_dynamic = typename std::conditional::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::type; + static_assert(_valid_expressions::size::value == sizeof...(Expr), "at least one argument is not an expression in where()"); + + template + void add(E&& expr) + { + static_assert(is_expression_t::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(expr)); + } template 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 _expressions; + detail::serializable_list _dynamic_expressions; }; - - template - struct dynamic_where_t - { - - using _is_where = std::true_type; - using _is_dynamic = std::true_type; - - template - void add(Expr&& expr) - { - static_assert(is_expression_t::type>::value, "invalid expression argument in where()"); - _conditions.emplace_back(std::forward(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 _conditions; - }; - } #endif