From c595353286f396658c80353b0fdf4c02cf816052 Mon Sep 17 00:00:00 2001 From: rbock Date: Tue, 8 Apr 2014 20:59:45 +0200 Subject: [PATCH] Moved where-methods to where-classes. --- include/sqlpp11/select.h | 96 +++++++++++++++++++++------------- include/sqlpp11/vendor/where.h | 44 +++++++++++++--- 2 files changed, 98 insertions(+), 42 deletions(-) diff --git a/include/sqlpp11/select.h b/include/sqlpp11/select.h index 6888db64..da7c4749 100644 --- a/include/sqlpp11/select.h +++ b/include/sqlpp11/select.h @@ -51,6 +51,54 @@ namespace sqlpp { + template + struct select_t; + namespace detail + { + template + struct select_policies_t + { + using _database_t = Db; + using _select_t = select_t; + template + struct _policies_update_impl + { + using type = select_t...>; + }; + + template + using _policies_update_t = typename _policies_update_impl::type; + + static_assert(is_noop_t::value or sqlpp::is_select_column_list_t::value, "column list of select is neither naught nor a valid column list"); + static_assert(is_noop_t::value or sqlpp::is_from_t::value, "from() part of select is neither naught nor a valid from()"); + using _value_type = typename std::conditional< + sqlpp::is_from_t::value, + typename ColumnList::_value_type, + no_value_t // If there is no from, the select is not complete (this logic is a bit simple, but better than nothing) + >::type; + }; + } + namespace detail { template< @@ -70,18 +118,19 @@ namespace sqlpp } // SELECT - template - struct select_t: public detail::select_helper_t::_value_type::template expression_operators> + struct select_t: public detail::select_helper_t::_value_type::template expression_operators>, + Where::template _methods_t> { using _database_t = Database; using _is_dynamic = typename std::conditional::value, std::false_type, std::true_type>::type; @@ -183,23 +232,6 @@ namespace sqlpp return { *this, vendor::from_t<_database_t, Args...>{args...} }; } - template - auto where(Args... args) - -> _policies_update_t> - { - static_assert(is_noop_t::value, "cannot call where()/dynamic_where() twice"); - return { *this, vendor::where_t{args...} }; - } - - template - auto dynamic_where(Args... args) - -> _policies_update_t> - { - static_assert(is_noop_t::value, "cannot call where()/dynamic_where() twice"); - static_assert(not std::is_same<_database_t, void>::value, "dynamic_where must not be called in a static statement"); - return { *this, vendor::where_t<_database_t, Args...>{args...} }; - } - template auto group_by(Args... args) -> _policies_update_t> @@ -308,14 +340,6 @@ namespace sqlpp return _from.add_from(*this, args...); } - template - void add_where(Args... args) - { - static_assert(is_where_t::value, "cannot call add_where() before dynamic_where()"); - static_assert(is_dynamic_t::value, "cannot call add_where() before dynamic_where()"); - return _where.add_where(*this, args...); - } - template void add_group_by(Args... args) { diff --git a/include/sqlpp11/vendor/where.h b/include/sqlpp11/vendor/where.h index 10dc8692..aef0d5f5 100644 --- a/include/sqlpp11/vendor/where.h +++ b/include/sqlpp11/vendor/where.h @@ -64,13 +64,17 @@ namespace sqlpp where_t& operator=(where_t&&) = default; ~where_t() = default; - template - void add_where(const Statement&, Expression expression) + template + struct _methods_t { - static_assert(_is_dynamic::value, "add_where can only be called for dynamic_where"); - static_assert(is_expression_t::value, "invalid expression argument in add_where()"); - _dynamic_expressions.emplace_back(expression); - } + template + void add_where(Expression expression) + { + static_assert(is_expression_t::value, "invalid expression argument in add_where()"); +#warning: Need to dispatch to actual add method to prevent error messages from being generated + return static_cast(this)->_where._dynamic_expressions.emplace_back(expression); + } + }; _parameter_tuple_t _expressions; vendor::interpretable_list_t _dynamic_expressions; @@ -93,6 +97,11 @@ namespace sqlpp where_t& operator=(where_t&&) = default; ~where_t() = default; + template + struct _methods_t + { + }; + bool _condition; }; @@ -100,6 +109,29 @@ namespace sqlpp { using _is_noop = std::true_type; using _table_set = ::sqlpp::detail::type_set<>; + + template + struct _methods_t + { + using _database_t = typename Policies::_database_t; + template + using _new_select_t = typename Policies::template _policies_update_t; + template + auto where(Args... args) + -> _new_select_t> + { + return { *static_cast(this), where_t{args...} }; + } + + template + auto dynamic_where(Args... args) + -> _new_select_t> + { + static_assert(not std::is_same<_database_t, void>::value, "dynamic_where must not be called in a static statement"); + return { *static_cast(this), vendor::where_t<_database_t, Args...>{args...} }; + } + + }; }; // Interpreters