0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Moved select_colum_list methods to the respective classes

This commit is contained in:
rbock 2014-04-09 08:50:34 +02:00
parent 18d244dd9f
commit db0bbd6e3f
3 changed files with 57 additions and 35 deletions

View File

@ -131,6 +131,7 @@ namespace sqlpp
> >
struct select_t: public detail::select_helper_t<ColumnList, From>::_value_type::template expression_operators<select_t<Database, FlagList, ColumnList, From, Where, GroupBy, Having, OrderBy, Limit, Offset>>, struct select_t: public detail::select_helper_t<ColumnList, From>::_value_type::template expression_operators<select_t<Database, FlagList, ColumnList, From, Where, GroupBy, Having, OrderBy, Limit, Offset>>,
FlagList::template _methods_t<detail::select_policies_t<Database, FlagList, ColumnList, From, Where, GroupBy, Having, OrderBy, Limit, Offset>>, FlagList::template _methods_t<detail::select_policies_t<Database, FlagList, ColumnList, From, Where, GroupBy, Having, OrderBy, Limit, Offset>>,
ColumnList::template _methods_t<detail::select_policies_t<Database, FlagList, ColumnList, From, Where, GroupBy, Having, OrderBy, Limit, Offset>>,
From::template _methods_t<detail::select_policies_t<Database, FlagList, ColumnList, From, Where, GroupBy, Having, OrderBy, Limit, Offset>>, From::template _methods_t<detail::select_policies_t<Database, FlagList, ColumnList, From, Where, GroupBy, Having, OrderBy, Limit, Offset>>,
Where::template _methods_t<detail::select_policies_t<Database, FlagList, ColumnList, From, Where, GroupBy, Having, OrderBy, Limit, Offset>>, Where::template _methods_t<detail::select_policies_t<Database, FlagList, ColumnList, From, Where, GroupBy, Having, OrderBy, Limit, Offset>>,
GroupBy::template _methods_t<detail::select_policies_t<Database, FlagList, ColumnList, From, Where, GroupBy, Having, OrderBy, Limit, Offset>>, GroupBy::template _methods_t<detail::select_policies_t<Database, FlagList, ColumnList, From, Where, GroupBy, Having, OrderBy, Limit, Offset>>,
@ -189,33 +190,6 @@ namespace sqlpp
select_t& operator=(select_t&& r) = default; select_t& operator=(select_t&& r) = default;
~select_t() = default; ~select_t() = default;
// type update functions
template<typename... Args>
auto columns(Args... args)
-> _policies_update_t<vendor::no_select_column_list_t, vendor::select_column_list_t<void, Args...>>
{
static_assert(is_noop_t<ColumnList>::value, "columns()/dynamic_columns() must not be called twice");
return { *this, vendor::select_column_list_t<void, Args...>{args...} };
}
template<typename... Args>
auto dynamic_columns(Args... args)
-> _policies_update_t<vendor::no_select_column_list_t, vendor::select_column_list_t<_database_t, Args...>>
{
static_assert(is_noop_t<ColumnList>::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<typename... Args>
void add_column(Args... args)
{
static_assert(is_select_column_list_t<ColumnList>::value, "cannot call add_column() before dynamic_columns()");
static_assert(is_dynamic_t<ColumnList>::value, "cannot call add_column() before dynamic_columns()");
return _column_list.add_column(*this, args...);
}
// PseudoTable // PseudoTable
template<typename AliasProvider> template<typename AliasProvider>
struct _pseudo_table_t struct _pseudo_table_t

View File

@ -68,7 +68,7 @@ namespace sqlpp
_names_t _dynamic_expression_names; _names_t _dynamic_expression_names;
template<typename Expr> template<typename Expr>
void push_back(Expr expr) void emplace_back(Expr expr)
{ {
_dynamic_expression_names.push_back(Expr::_name_t::_get_name()); _dynamic_expression_names.push_back(Expr::_name_t::_get_name());
_dynamic_columns.emplace_back(expr); _dynamic_columns.emplace_back(expr);
@ -89,8 +89,9 @@ namespace sqlpp
}; };
_names_t _dynamic_expression_names; _names_t _dynamic_expression_names;
#warning: Put an assert here or remove implementation to make sure this never aktually gets called
template<typename T> template<typename T>
void push_back(const T&) {} void emplace_back(const T&) {}
static constexpr bool empty() static constexpr bool empty()
{ {
@ -185,13 +186,19 @@ namespace sqlpp
select_column_list_t& operator=(select_column_list_t&&) = default; select_column_list_t& operator=(select_column_list_t&&) = default;
~select_column_list_t() = default; ~select_column_list_t() = default;
template<typename Select, typename Expr> template<typename Policies>
void add_column(const Select&, Expr namedExpr) struct _methods_t
{ {
static_assert(is_named_expression_t<Expr>::value, "select() arguments require to be named expressions"); template<typename NamedExpression>
static_assert(_is_dynamic::value, "cannot add columns to a non-dynamic column list"); void add_column(NamedExpression namedExpression)
_dynamic_columns.push_back(namedExpr); {
} static_assert(_is_dynamic::value, "add_column can only be called for dynamic_column");
static_assert(is_named_expression_t<NamedExpression>::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<typename Policies::_select_t*>(this)->_column_list._dynamic_columns.emplace_back(namedExpression);
}
};
const select_column_list_t& _column_list() const { return *this; } const select_column_list_t& _column_list() const { return *this; }
_parameter_tuple_t _columns; _parameter_tuple_t _columns;
@ -212,6 +219,30 @@ namespace sqlpp
{ {
static_assert(wrong_t<T>::value, "Cannot use a select as a table when no columns have been selected yet"); static_assert(wrong_t<T>::value, "Cannot use a select as a table when no columns have been selected yet");
}; };
template<typename Policies>
struct _methods_t
{
using _database_t = typename Policies::_database_t;
template<typename T>
using _new_select_t = typename Policies::template _policies_update_t<no_select_column_list_t, T>;
template<typename... Args>
auto columns(Args... args)
-> _new_select_t<select_column_list_t<void, Args...>>
{
#warning need to handle all_of_t here
return { *static_cast<typename Policies::_select_t*>(this), select_column_list_t<void, Args...>{args...} };
}
template<typename... Args>
auto dynamic_columns(Args... args)
-> _new_select_t<select_column_list_t<_database_t, Args...>>
{
static_assert(not std::is_same<_database_t, void>::value, "dynamic_columns must not be called in a static statement");
return { *static_cast<typename Policies::_select_t*>(this), vendor::select_column_list_t<_database_t, Args...>{args...} };
}
};
}; };
// Interpreters // Interpreters

View File

@ -49,6 +49,7 @@ int main()
test::TabFoo f; test::TabFoo f;
test::TabBar t; test::TabBar t;
/*
for (const auto& row : db(select(all_of(t)).from(t).where(true))) for (const auto& row : db(select(all_of(t)).from(t).where(true)))
{ {
int64_t a = row.alpha; int64_t a = row.alpha;
@ -67,6 +68,22 @@ int main()
const std::string b = row.tabBar.beta; const std::string b = row.tabBar.beta;
const bool g = row.gamma; 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; return 0;
} }