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:
parent
e70cc74198
commit
153427d2b8
@ -59,6 +59,8 @@ namespace sqlpp
|
|||||||
assert_no_unknown_tables_in_having_t,
|
assert_no_unknown_tables_in_having_t,
|
||||||
"at least one having-expression requires a table which is otherwise not known in the statement");
|
"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
|
// HAVING
|
||||||
template <typename Database, typename Expression>
|
template <typename Database, typename Expression>
|
||||||
struct having_t
|
struct having_t
|
||||||
@ -139,9 +141,15 @@ namespace sqlpp
|
|||||||
return t.having;
|
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,
|
consistent_t,
|
||||||
assert_no_unknown_tables_in_having_t>::type;
|
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>;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ namespace sqlpp
|
|||||||
SQLPP_PORTABLE_STATIC_ASSERT(
|
SQLPP_PORTABLE_STATIC_ASSERT(
|
||||||
assert_no_unknown_tables_in_selected_columns_t,
|
assert_no_unknown_tables_in_selected_columns_t,
|
||||||
"at least one selected column requires a table which is otherwise not known in the statement");
|
"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");
|
"not all columns are made of aggregates, despite group_by or similar");
|
||||||
|
|
||||||
// SELECTED COLUMNS
|
// SELECTED COLUMNS
|
||||||
@ -195,16 +195,15 @@ namespace sqlpp
|
|||||||
return t.selected_columns;
|
return t.selected_columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
using _column_check =
|
using _table_check = typename std::conditional<Policies::template _no_unknown_tables<select_column_list_t>::value,
|
||||||
typename std::conditional<Policies::template _no_unknown_tables<select_column_list_t>::value,
|
consistent_t,
|
||||||
consistent_t,
|
assert_no_unknown_tables_in_selected_columns_t>::type;
|
||||||
assert_no_unknown_tables_in_selected_columns_t>::type;
|
|
||||||
|
|
||||||
using _aggregate_check = typename std::conditional<Policies::template _no_unknown_aggregates<Columns...>::value,
|
using _aggregate_check = typename std::conditional<Policies::template _no_unknown_aggregates<Columns...>::value,
|
||||||
consistent_t,
|
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
|
// Result methods
|
||||||
|
@ -93,6 +93,10 @@ namespace sqlpp
|
|||||||
logic::all_t<detail::is_aggregate_expression_impl<_all_provided_aggregates,
|
logic::all_t<detail::is_aggregate_expression_impl<_all_provided_aggregates,
|
||||||
Expressions>::type::value...>::value>;
|
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>
|
template <template <typename> class Predicate>
|
||||||
using any_t = logic::any_t<Predicate<Policies>::value...>;
|
using any_t = logic::any_t<Predicate<Policies>::value...>;
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2015-2015, Roland Bock
|
* Copyright (c) 2015-2016, Roland Bock
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without modification,
|
* 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
|
// Failures with static group_by and selected non-aggregates or incorrect aggregates
|
||||||
void static_group_by_nok()
|
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_no_unknown_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.alpha + t.delta).as(whatever)).from(t).unconditionally().group_by(t.alpha));
|
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));
|
select((t.alpha + t.delta).as(whatever)).from(t).unconditionally().group_by(t.alpha, t.alpha + t.delta * 17));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,7 +139,7 @@ int Select(int, char* [])
|
|||||||
.dynamic_where()
|
.dynamic_where()
|
||||||
.dynamic_group_by(t.alpha)
|
.dynamic_group_by(t.alpha)
|
||||||
.dynamic_order_by()
|
.dynamic_order_by()
|
||||||
.dynamic_having(t.gamma)
|
.dynamic_having(sum(t.alpha) > 17)
|
||||||
.dynamic_limit()
|
.dynamic_limit()
|
||||||
.dynamic_offset();
|
.dynamic_offset();
|
||||||
s.select_flags.add(sqlpp::distinct);
|
s.select_flags.add(sqlpp::distinct);
|
||||||
|
Loading…
Reference in New Issue
Block a user