From ad5584e52dcb7ff1367cd1863c66c8582a8d3304 Mon Sep 17 00:00:00 2001 From: rbock Date: Wed, 9 Apr 2014 20:54:28 +0200 Subject: [PATCH] Cleaned up a bit... --- include/sqlpp11/select.h | 216 +++++++++----------- include/sqlpp11/vendor/from.h | 6 +- include/sqlpp11/vendor/group_by.h | 6 +- include/sqlpp11/vendor/having.h | 6 +- include/sqlpp11/vendor/limit.h | 8 +- include/sqlpp11/vendor/offset.h | 8 +- include/sqlpp11/vendor/order_by.h | 6 +- include/sqlpp11/vendor/select_column_list.h | 6 +- include/sqlpp11/vendor/select_flag_list.h | 6 +- include/sqlpp11/vendor/where.h | 6 +- 10 files changed, 125 insertions(+), 149 deletions(-) diff --git a/include/sqlpp11/select.h b/include/sqlpp11/select.h index bc27fc43..925b6186 100644 --- a/include/sqlpp11/select.h +++ b/include/sqlpp11/select.h @@ -51,7 +51,14 @@ namespace sqlpp { - template + struct select_t; + + namespace detail + { + template - struct select_t; - namespace detail - { - template struct select_policies_t { using _database_t = Db; - using _select_t = select_t; + using _flag_list_t = FlagList; + using _column_list_t = ColumnList; + using _from_t = From; + using _where_t = Where; + using _group_by_t = GroupBy; + using _having_t = Having; + using _order_by_t = OrderBy; + using _limit_t = Limit; + using _offset_t = Offset; + + using _statement_t = select_t; + + struct _methods_t: + public _flag_list_t::template _methods_t, + public _column_list_t::template _methods_t, + public _from_t::template _methods_t, + public _where_t::template _methods_t, + public _group_by_t::template _methods_t, + public _having_t::template _methods_t, + public _order_by_t::template _methods_t, + public _limit_t::template _methods_t, + public _offset_t::template _methods_t + {}; + template struct _policies_update_impl { +#warning: Need to make sure that Needle is in Policies! 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< - typename ColumnList, - typename From - > - struct select_helper_t - { 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< @@ -118,53 +117,39 @@ namespace sqlpp } // SELECT - template - struct select_t: public detail::select_helper_t::_value_type::template expression_operators>, - FlagList::template _methods_t>, - ColumnList::template _methods_t>, - From::template _methods_t>, - Where::template _methods_t>, - GroupBy::template _methods_t>, - Having::template _methods_t>, - OrderBy::template _methods_t>, - Limit::template _methods_t>, - Offset::template _methods_t> + struct select_t: + public detail::select_policies_t::_value_type::template expression_operators>, + public detail::select_policies_t::_methods_t { - using _database_t = Database; - using _is_dynamic = typename std::conditional::value, std::false_type, std::true_type>::type; + using _policies_t = typename detail::select_policies_t; + using _database_t = typename _policies_t::_database_t; + using _flag_list_t = typename _policies_t::_flag_list_t; + using _column_list_t = typename _policies_t::_column_list_t; + using _from_t = typename _policies_t::_from_t; + using _where_t = typename _policies_t::_where_t; + using _group_by_t = typename _policies_t::_group_by_t; + using _having_t = typename _policies_t::_having_t; + using _order_by_t = typename _policies_t::_order_by_t; + using _limit_t = typename _policies_t::_limit_t; + using _offset_t = typename _policies_t::_offset_t; - template - struct _policies_update_impl - { - using type = select_t...>; - }; + using _is_dynamic = typename std::conditional::value, std::false_type, std::true_type>::type; - template - using _policies_update_t = typename _policies_update_impl::type; - - using _parameter_tuple_t = std::tuple; + using _parameter_tuple_t = std::tuple; using _parameter_list_t = typename make_parameter_list_t::type; using _table_set = ::sqlpp::detail::type_set<>; - using _column_list_t = ColumnList; - template - using _result_row_t = typename _column_list_t::template _result_row_t; + template + using _result_row_t = typename _column_list_t::template _result_row_t; using _dynamic_names_t = typename _column_list_t::_dynamic_names_t; using _is_select = std::true_type; using _requires_braces = std::true_type; - using _value_type = typename detail::select_helper_t::_value_type; + using _value_type = typename detail::select_policies_t::_value_type; using _name_t = typename _column_list_t::_name_t; // Constructors @@ -173,15 +158,15 @@ namespace sqlpp template select_t(Statement s, T t): - _flag_list(detail::arg_selector::_(s._flag_list, t)), - _column_list(detail::arg_selector::_(s._column_list, t)), - _from(detail::arg_selector::_(s._from, t)), - _where(detail::arg_selector::_(s._where, t)), - _group_by(detail::arg_selector::_(s._group_by, t)), - _having(detail::arg_selector::_(s._having, t)), - _order_by(detail::arg_selector::_(s._order_by, t)), - _limit(detail::arg_selector::_(s._limit, t)), - _offset(detail::arg_selector::_(s._offset, t)) + _flag_list(detail::arg_selector<_flag_list_t>::_(s._flag_list, t)), + _column_list(detail::arg_selector<_column_list_t>::_(s._column_list, t)), + _from(detail::arg_selector<_from_t>::_(s._from, t)), + _where(detail::arg_selector<_where_t>::_(s._where, t)), + _group_by(detail::arg_selector<_group_by_t>::_(s._group_by, t)), + _having(detail::arg_selector<_having_t>::_(s._having, t)), + _order_by(detail::arg_selector<_order_by_t>::_(s._order_by, t)), + _limit(detail::arg_selector<_limit_t>::_(s._limit, t)), + _offset(detail::arg_selector<_offset_t>::_(s._offset, t)) {} select_t(const select_t& r) = default; @@ -240,46 +225,46 @@ namespace sqlpp template struct is_table_subset_of_from { - static constexpr bool value = ::sqlpp::detail::is_subset_of::value; + static constexpr bool value = ::sqlpp::detail::is_subset_of::value; }; // Execute - template - auto _run(Db& db) const - -> result_t> + template + auto _run(Database& db) const + -> result_t> { #warning: need to check in add_xy method as well #warning: need add_wxy_without_table_check #warning: might want to add an .extra_tables() method to say which tables might also be used here, say via dynamic_from or because this is a subselect - static_assert(is_table_subset_of_from::value, "selected columns require additional tables in from()"); - static_assert(is_table_subset_of_from::value, "where() expression requires additional tables in from()"); - static_assert(is_table_subset_of_from::value, "group_by() expression requires additional tables in from()"); - static_assert(is_table_subset_of_from::value, "having() expression requires additional tables in from()"); - static_assert(is_table_subset_of_from::value, "order_by() expression requires additional tables in from()"); - static_assert(is_table_subset_of_from::value, "limit() expression requires additional tables in from()"); - static_assert(is_table_subset_of_from::value, "offset() expression requires additional tables in from()"); + static_assert(is_table_subset_of_from<_column_list_t>::value, "selected columns require additional tables in from()"); + static_assert(is_table_subset_of_from<_where_t>::value, "where() expression requires additional tables in from()"); + static_assert(is_table_subset_of_from<_group_by_t>::value, "group_by() expression requires additional tables in from()"); + static_assert(is_table_subset_of_from<_having_t>::value, "having() expression requires additional tables in from()"); + static_assert(is_table_subset_of_from<_order_by_t>::value, "order_by() expression requires additional tables in from()"); + static_assert(is_table_subset_of_from<_limit_t>::value, "limit() expression requires additional tables in from()"); + static_assert(is_table_subset_of_from<_offset_t>::value, "offset() expression requires additional tables in from()"); static_assert(_get_static_no_of_parameters() == 0, "cannot run select directly with parameters, use prepare instead"); return {db.select(*this), get_dynamic_names()}; } // Prepare - template - auto _prepare(Db& db) const - -> prepared_select_t + template + auto _prepare(Database& db) const + -> prepared_select_t { return {{}, get_dynamic_names(), db.prepare_select(*this)}; } - FlagList _flag_list; - ColumnList _column_list; - From _from; - Where _where; - GroupBy _group_by; - Having _having; - OrderBy _order_by; - Limit _limit; - Offset _offset; + _flag_list_t _flag_list; + _column_list_t _column_list; + _from_t _from; + _where_t _where; + _group_by_t _group_by; + _having_t _having; + _order_by_t _order_by; + _limit_t _limit; + _offset_t _offset; }; namespace vendor @@ -308,17 +293,8 @@ namespace sqlpp }; } - template - using blank_select_t = select_t; + template + using make_select_t = typename detail::select_policies_t::_statement_t; namespace detail { @@ -328,29 +304,29 @@ namespace sqlpp decltype(std::tuple_cat(as_tuple::_(std::declval())...))>; } - select_t select() // FIXME: These should be constexpr + make_select_t select() // FIXME: These should be constexpr { - return { select_t() }; + return { }; } template auto select(Columns... columns) - -> select_t> + -> make_select_t> { - return { select_t(), detail::make_select_column_list_t(std::tuple_cat(detail::as_tuple::_(columns)...)) }; + return { make_select_t(), detail::make_select_column_list_t(std::tuple_cat(detail::as_tuple::_(columns)...)) }; } template - select_t dynamic_select(const Database&) + make_select_t dynamic_select(const Database&) { - return { select_t() }; + return { make_select_t() }; } template auto dynamic_select(const Database&, Columns... columns) - -> select_t> + -> make_select_t> { - return { select_t(), detail::make_select_column_list_t(std::tuple_cat(detail::as_tuple::_(columns)...)) }; + return { make_select_t(), detail::make_select_column_list_t(std::tuple_cat(detail::as_tuple::_(columns)...)) }; } } diff --git a/include/sqlpp11/vendor/from.h b/include/sqlpp11/vendor/from.h index 72a66980..9a354bc8 100644 --- a/include/sqlpp11/vendor/from.h +++ b/include/sqlpp11/vendor/from.h @@ -72,7 +72,7 @@ namespace sqlpp { static_assert(is_table_t::value, "invalid expression argument in add_from()"); #warning: Need to dispatch to actual add method to prevent error messages from being generated - return static_cast(this)->_from._dynamic_tables.emplace_back(table); + return static_cast(this)->_from._dynamic_tables.emplace_back(table); } }; @@ -96,7 +96,7 @@ namespace sqlpp auto from(Args... args) -> _new_select_t> { - return { *static_cast(this), from_t{args...} }; + return { *static_cast(this), from_t{args...} }; } template @@ -104,7 +104,7 @@ namespace sqlpp -> _new_select_t> { static_assert(not std::is_same<_database_t, void>::value, "dynamic_from must not be called in a static statement"); - return { *static_cast(this), vendor::from_t<_database_t, Args...>{args...} }; + return { *static_cast(this), vendor::from_t<_database_t, Args...>{args...} }; } }; }; diff --git a/include/sqlpp11/vendor/group_by.h b/include/sqlpp11/vendor/group_by.h index 29b6438d..b4764179 100644 --- a/include/sqlpp11/vendor/group_by.h +++ b/include/sqlpp11/vendor/group_by.h @@ -74,7 +74,7 @@ namespace sqlpp static_assert(_is_dynamic::value, "add_group_by must not be called for static group_by"); static_assert(is_expression_t::value, "invalid expression argument in add_group_by()"); #warning: Need to dispatch to actual add method to prevent error messages from being generated - return static_cast(this)->_group_by._dynamic_expressions.emplace_back(expression); + return static_cast(this)->_group_by._dynamic_expressions.emplace_back(expression); } }; @@ -99,7 +99,7 @@ namespace sqlpp auto group_by(Args... args) -> _new_select_t> { - return { *static_cast(this), group_by_t{args...} }; + return { *static_cast(this), group_by_t{args...} }; } template @@ -107,7 +107,7 @@ namespace sqlpp -> _new_select_t> { static_assert(not std::is_same<_database_t, void>::value, "dynamic_group_by must not be called in a static statement"); - return { *static_cast(this), vendor::group_by_t<_database_t, Args...>{args...} }; + return { *static_cast(this), vendor::group_by_t<_database_t, Args...>{args...} }; } }; }; diff --git a/include/sqlpp11/vendor/having.h b/include/sqlpp11/vendor/having.h index c75b2a49..921ceffa 100644 --- a/include/sqlpp11/vendor/having.h +++ b/include/sqlpp11/vendor/having.h @@ -72,7 +72,7 @@ namespace sqlpp static_assert(_is_dynamic::value, "add_having must not be called for static having"); static_assert(is_expression_t::value, "invalid expression argument in add_having()"); #warning: Need to dispatch to actual add method to prevent error messages from being generated - return static_cast(this)->_having._dynamic_expressions.emplace_back(expression); + return static_cast(this)->_having._dynamic_expressions.emplace_back(expression); } }; @@ -96,7 +96,7 @@ namespace sqlpp auto having(Args... args) -> _new_select_t> { - return { *static_cast(this), having_t{args...} }; + return { *static_cast(this), having_t{args...} }; } template @@ -104,7 +104,7 @@ namespace sqlpp -> _new_select_t> { static_assert(not std::is_same<_database_t, void>::value, "dynamic_having must not be called in a static statement"); - return { *static_cast(this), vendor::having_t<_database_t, Args...>{args...} }; + return { *static_cast(this), vendor::having_t<_database_t, Args...>{args...} }; } }; }; diff --git a/include/sqlpp11/vendor/limit.h b/include/sqlpp11/vendor/limit.h index 683e7581..47cea9f9 100644 --- a/include/sqlpp11/vendor/limit.h +++ b/include/sqlpp11/vendor/limit.h @@ -94,8 +94,8 @@ namespace sqlpp void set_limit(Limit value) { using arg_t = typename wrap_operand::type; - static_cast(this)->_limit._value = arg_t{value}; - static_cast(this)->_limit._initialized = true; + static_cast(this)->_limit._value = arg_t{value}; + static_cast(this)->_limit._initialized = true; } }; @@ -119,14 +119,14 @@ namespace sqlpp auto limit(Arg arg) -> _new_select_t::type>> { - return { *static_cast(this), limit_t::type>{{arg}} }; + return { *static_cast(this), limit_t::type>{{arg}} }; } auto dynamic_limit() -> _new_select_t> { static_assert(not std::is_same<_database_t, void>::value, "dynamic_limit must not be called in a static statement"); - return { *static_cast(this), dynamic_limit_t<_database_t>{} }; + return { *static_cast(this), dynamic_limit_t<_database_t>{} }; } }; }; diff --git a/include/sqlpp11/vendor/offset.h b/include/sqlpp11/vendor/offset.h index 0c3ad9e1..0568ef9d 100644 --- a/include/sqlpp11/vendor/offset.h +++ b/include/sqlpp11/vendor/offset.h @@ -93,8 +93,8 @@ namespace sqlpp void set_offset(Offset value) { using arg_t = typename wrap_operand::type; - static_cast(this)->_offset._value = arg_t{value}; - static_cast(this)->_offset._initialized = true; + static_cast(this)->_offset._value = arg_t{value}; + static_cast(this)->_offset._initialized = true; } }; @@ -118,14 +118,14 @@ namespace sqlpp auto offset(Arg arg) -> _new_select_t::type>> { - return { *static_cast(this), offset_t::type>{{arg}} }; + return { *static_cast(this), offset_t::type>{{arg}} }; } auto dynamic_offset() -> _new_select_t> { static_assert(not std::is_same<_database_t, void>::value, "dynamic_offset must not be called in a static statement"); - return { *static_cast(this), dynamic_offset_t<_database_t>{} }; + return { *static_cast(this), dynamic_offset_t<_database_t>{} }; } }; }; diff --git a/include/sqlpp11/vendor/order_by.h b/include/sqlpp11/vendor/order_by.h index 4aff0f26..8cf3583a 100644 --- a/include/sqlpp11/vendor/order_by.h +++ b/include/sqlpp11/vendor/order_by.h @@ -73,7 +73,7 @@ namespace sqlpp static_assert(_is_dynamic::value, "add_order_by must not be called for static order_by"); static_assert(is_sort_order_t::value, "invalid expression argument in add_order_by()"); #warning: Need to dispatch to actual add method to prevent error messages from being generated - return static_cast(this)->_order_by._dynamic_expressions.emplace_back(expression); + return static_cast(this)->_order_by._dynamic_expressions.emplace_back(expression); } }; @@ -97,7 +97,7 @@ namespace sqlpp auto order_by(Args... args) -> _new_select_t> { - return { *static_cast(this), order_by_t{args...} }; + return { *static_cast(this), order_by_t{args...} }; } template @@ -105,7 +105,7 @@ namespace sqlpp -> _new_select_t> { static_assert(not std::is_same<_database_t, void>::value, "dynamic_order_by must not be called in a static statement"); - return { *static_cast(this), vendor::order_by_t<_database_t, Args...>{args...} }; + return { *static_cast(this), vendor::order_by_t<_database_t, Args...>{args...} }; } }; }; diff --git a/include/sqlpp11/vendor/select_column_list.h b/include/sqlpp11/vendor/select_column_list.h index 58846181..9d22d10f 100644 --- a/include/sqlpp11/vendor/select_column_list.h +++ b/include/sqlpp11/vendor/select_column_list.h @@ -195,7 +195,7 @@ namespace sqlpp static_assert(_is_dynamic::value, "add_column can only be called for dynamic_column"); static_assert(is_named_expression_t::value, "invalid named expression argument in add_column()"); #warning: Need to dispatch to actual add method to prevent error messages from being generated - return static_cast(this)->_column_list._dynamic_columns.emplace_back(namedExpression); + return static_cast(this)->_column_list._dynamic_columns.emplace_back(namedExpression); } }; @@ -232,7 +232,7 @@ namespace sqlpp -> _new_select_t> { #warning need to handle all_of_t here - return { *static_cast(this), select_column_list_t{args...} }; + return { *static_cast(this), select_column_list_t{args...} }; } template @@ -240,7 +240,7 @@ namespace sqlpp -> _new_select_t> { static_assert(not std::is_same<_database_t, void>::value, "dynamic_columns must not be called in a static statement"); - return { *static_cast(this), vendor::select_column_list_t<_database_t, Args...>{args...} }; + return { *static_cast(this), vendor::select_column_list_t<_database_t, Args...>{args...} }; } }; }; diff --git a/include/sqlpp11/vendor/select_flag_list.h b/include/sqlpp11/vendor/select_flag_list.h index b42440ba..1c8b2c33 100644 --- a/include/sqlpp11/vendor/select_flag_list.h +++ b/include/sqlpp11/vendor/select_flag_list.h @@ -70,7 +70,7 @@ namespace sqlpp static_assert(_is_dynamic::value, "add_flag must not be called for static select flags"); static_assert(is_select_flag_t::value, "invalid select flag argument in add_flag()"); #warning: Need to dispatch to actual add method to prevent error messages from being generated - return static_cast(this)->_flag_list._dynamic_flags.emplace_back(flag); + return static_cast(this)->_flag_list._dynamic_flags.emplace_back(flag); } }; @@ -94,7 +94,7 @@ namespace sqlpp auto flags(Args... args) -> _new_select_t> { - return { *static_cast(this), select_flag_list_t{args...} }; + return { *static_cast(this), select_flag_list_t{args...} }; } template @@ -102,7 +102,7 @@ namespace sqlpp -> _new_select_t> { static_assert(not std::is_same<_database_t, void>::value, "dynamic_flags must not be called in a static statement"); - return { *static_cast(this), vendor::select_flag_list_t<_database_t, Args...>{args...} }; + return { *static_cast(this), vendor::select_flag_list_t<_database_t, Args...>{args...} }; } }; }; diff --git a/include/sqlpp11/vendor/where.h b/include/sqlpp11/vendor/where.h index d7ddabeb..b82f4e6e 100644 --- a/include/sqlpp11/vendor/where.h +++ b/include/sqlpp11/vendor/where.h @@ -73,7 +73,7 @@ namespace sqlpp 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()"); #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); + return static_cast(this)->_where._dynamic_expressions.emplace_back(expression); } }; @@ -122,7 +122,7 @@ namespace sqlpp auto where(Args... args) -> _new_select_t> { - return { *static_cast(this), where_t{args...} }; + return { *static_cast(this), where_t{args...} }; } template @@ -130,7 +130,7 @@ namespace sqlpp -> _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...} }; + return { *static_cast(this), vendor::where_t<_database_t, Args...>{args...} }; } }; };