From db0bbd6e3fefa939bcaeb5433fbb964f653a4f40 Mon Sep 17 00:00:00 2001 From: rbock Date: Wed, 9 Apr 2014 08:50:34 +0200 Subject: [PATCH] Moved select_colum_list methods to the respective classes --- include/sqlpp11/select.h | 28 +----------- include/sqlpp11/vendor/select_column_list.h | 47 +++++++++++++++++---- tests/SelectTest.cpp | 17 ++++++++ 3 files changed, 57 insertions(+), 35 deletions(-) diff --git a/include/sqlpp11/select.h b/include/sqlpp11/select.h index 1523bf7c..bc27fc43 100644 --- a/include/sqlpp11/select.h +++ b/include/sqlpp11/select.h @@ -131,6 +131,7 @@ namespace sqlpp > 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>, @@ -189,33 +190,6 @@ namespace sqlpp select_t& operator=(select_t&& r) = default; ~select_t() = default; - // type update functions - template - auto columns(Args... args) - -> _policies_update_t> - { - static_assert(is_noop_t::value, "columns()/dynamic_columns() must not be called twice"); - return { *this, vendor::select_column_list_t{args...} }; - } - - template - auto dynamic_columns(Args... args) - -> _policies_update_t> - { - static_assert(is_noop_t::value, "columns()/dynamic_columns() must not be called twice"); - static_assert(_is_dynamic::value, "dynamic_columns must not be called in a static statement"); - return { *this, vendor::select_column_list_t<_database_t, Args...>{args...} }; - } - - // value adding methods - template - void add_column(Args... args) - { - static_assert(is_select_column_list_t::value, "cannot call add_column() before dynamic_columns()"); - static_assert(is_dynamic_t::value, "cannot call add_column() before dynamic_columns()"); - return _column_list.add_column(*this, args...); - } - // PseudoTable template struct _pseudo_table_t diff --git a/include/sqlpp11/vendor/select_column_list.h b/include/sqlpp11/vendor/select_column_list.h index fdb9427a..58846181 100644 --- a/include/sqlpp11/vendor/select_column_list.h +++ b/include/sqlpp11/vendor/select_column_list.h @@ -68,7 +68,7 @@ namespace sqlpp _names_t _dynamic_expression_names; template - void push_back(Expr expr) + void emplace_back(Expr expr) { _dynamic_expression_names.push_back(Expr::_name_t::_get_name()); _dynamic_columns.emplace_back(expr); @@ -89,8 +89,9 @@ namespace sqlpp }; _names_t _dynamic_expression_names; +#warning: Put an assert here or remove implementation to make sure this never aktually gets called template - void push_back(const T&) {} + void emplace_back(const T&) {} static constexpr bool empty() { @@ -185,13 +186,19 @@ namespace sqlpp select_column_list_t& operator=(select_column_list_t&&) = default; ~select_column_list_t() = default; - template - void add_column(const Select&, Expr namedExpr) + template + struct _methods_t { - static_assert(is_named_expression_t::value, "select() arguments require to be named expressions"); - static_assert(_is_dynamic::value, "cannot add columns to a non-dynamic column list"); - _dynamic_columns.push_back(namedExpr); - } + template + void add_column(NamedExpression namedExpression) + { + 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); + } + }; + const select_column_list_t& _column_list() const { return *this; } _parameter_tuple_t _columns; @@ -212,6 +219,30 @@ namespace sqlpp { static_assert(wrong_t::value, "Cannot use a select as a table when no columns have been selected yet"); }; + + template + struct _methods_t + { + using _database_t = typename Policies::_database_t; + template + using _new_select_t = typename Policies::template _policies_update_t; + + template + auto columns(Args... args) + -> _new_select_t> + { +#warning need to handle all_of_t here + return { *static_cast(this), select_column_list_t{args...} }; + } + + template + auto dynamic_columns(Args... args) + -> _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...} }; + } + }; }; // Interpreters diff --git a/tests/SelectTest.cpp b/tests/SelectTest.cpp index 423ff488..8880da29 100644 --- a/tests/SelectTest.cpp +++ b/tests/SelectTest.cpp @@ -49,6 +49,7 @@ int main() test::TabFoo f; test::TabBar t; + /* for (const auto& row : db(select(all_of(t)).from(t).where(true))) { int64_t a = row.alpha; @@ -67,6 +68,22 @@ int main() const std::string b = row.tabBar.beta; const bool g = row.gamma; } + */ + + auto s = dynamic_select(db).dynamic_columns(t.alpha).dynamic_flags().dynamic_from(t).dynamic_where().dynamic_group_by(t.alpha).dynamic_order_by().dynamic_having(t.gamma).dynamic_limit().dynamic_offset(); + s.add_flag(sqlpp::distinct); + s.add_column(t.beta); + s.add_from(f); + s.add_where(t.alpha > 7); + s.add_having(t.alpha > 7); + s.set_limit(3); + s.set_offset(3); + s.add_group_by(t.beta); + s.add_order_by(t.beta.asc()); + for (const auto& row : db(s)) + { + int64_t a = row.alpha; + } return 0; }