0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 04:47:18 +08:00

Enforce having expression to consist of aggregates

This commit is contained in:
rbock 2016-03-17 19:18:14 +01:00
parent e70cc74198
commit 153427d2b8
5 changed files with 26 additions and 15 deletions

View File

@ -59,6 +59,8 @@ namespace sqlpp
assert_no_unknown_tables_in_having_t,
"at least one having-expression requires a table which is otherwise not known in the statement");
SQLPP_PORTABLE_STATIC_ASSERT(assert_no_non_aggregates_t, "having expression not built out of aggregate expressions");
// HAVING
template <typename Database, typename Expression>
struct having_t
@ -139,9 +141,15 @@ namespace sqlpp
return t.having;
}
using _consistency_check = typename std::conditional<Policies::template _no_unknown_tables<having_t>::value,
using _table_check = typename std::conditional<Policies::template _no_unknown_tables<having_t>::value,
consistent_t,
assert_no_unknown_tables_in_having_t>::type;
using _aggregate_check = typename std::conditional<Policies::template _no_non_aggregates<Expression>::value,
consistent_t,
assert_no_non_aggregates_t>::type;
using _consistency_check = detail::get_first_if<is_inconsistent_t, consistent_t, _table_check, _aggregate_check>;
};
};

View File

@ -91,7 +91,7 @@ namespace sqlpp
SQLPP_PORTABLE_STATIC_ASSERT(
assert_no_unknown_tables_in_selected_columns_t,
"at least one selected column requires a table which is otherwise not known in the statement");
SQLPP_PORTABLE_STATIC_ASSERT(assert_aggregates_t,
SQLPP_PORTABLE_STATIC_ASSERT(assert_no_unknown_aggregates_t,
"not all columns are made of aggregates, despite group_by or similar");
// SELECTED COLUMNS
@ -195,16 +195,15 @@ namespace sqlpp
return t.selected_columns;
}
using _column_check =
typename std::conditional<Policies::template _no_unknown_tables<select_column_list_t>::value,
using _table_check = typename std::conditional<Policies::template _no_unknown_tables<select_column_list_t>::value,
consistent_t,
assert_no_unknown_tables_in_selected_columns_t>::type;
using _aggregate_check = typename std::conditional<Policies::template _no_unknown_aggregates<Columns...>::value,
consistent_t,
assert_aggregates_t>::type;
assert_no_unknown_aggregates_t>::type;
using _consistency_check = detail::get_first_if<is_inconsistent_t, consistent_t, _column_check, _aggregate_check>;
using _consistency_check = detail::get_first_if<is_inconsistent_t, consistent_t, _table_check, _aggregate_check>;
};
// Result methods

View File

@ -93,6 +93,10 @@ namespace sqlpp
logic::all_t<detail::is_aggregate_expression_impl<_all_provided_aggregates,
Expressions>::type::value...>::value>;
template <typename... Expressions>
using _no_non_aggregates = logic::any_t<logic::all_t<
detail::is_aggregate_expression_impl<_all_provided_aggregates, Expressions>::type::value...>::value>;
template <template <typename> class Predicate>
using any_t = logic::any_t<Predicate<Policies>::value...>;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2015, Roland Bock
* Copyright (c) 2015-2016, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
@ -117,10 +117,10 @@ namespace
// Failures with static group_by and selected non-aggregates or incorrect aggregates
void static_group_by_nok()
{
run_check<sqlpp::assert_aggregates_t>(select(t.beta).from(t).unconditionally().group_by(t.alpha));
run_check<sqlpp::assert_aggregates_t>(
run_check<sqlpp::assert_no_unknown_aggregates_t>(select(t.beta).from(t).unconditionally().group_by(t.alpha));
run_check<sqlpp::assert_no_unknown_aggregates_t>(
select((t.alpha + t.delta).as(whatever)).from(t).unconditionally().group_by(t.alpha));
run_check<sqlpp::assert_aggregates_t>(
run_check<sqlpp::assert_no_unknown_aggregates_t>(
select((t.alpha + t.delta).as(whatever)).from(t).unconditionally().group_by(t.alpha, t.alpha + t.delta * 17));
}
}

View File

@ -139,7 +139,7 @@ int Select(int, char* [])
.dynamic_where()
.dynamic_group_by(t.alpha)
.dynamic_order_by()
.dynamic_having(t.gamma)
.dynamic_having(sum(t.alpha) > 17)
.dynamic_limit()
.dynamic_offset();
s.select_flags.add(sqlpp::distinct);