From bd385f7377adda2919369040f855286dbedb8dc7 Mon Sep 17 00:00:00 2001 From: rbock Date: Sat, 3 Oct 2015 17:00:53 +0200 Subject: [PATCH] Added check if all selected columns are aggregates if group_by is present --- include/sqlpp11/group_by.h | 3 ++ include/sqlpp11/select_column_list.h | 19 +++++++++++- include/sqlpp11/statement.h | 6 ++++ include/sqlpp11/type_traits.h | 34 +++++++++++++++++++++ tests/Aggregates.cpp | 45 ++++++++++++++++++++++++++++ tests/CMakeLists.txt | 1 + tests/SelectType.cpp | 2 +- 7 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 tests/Aggregates.cpp diff --git a/include/sqlpp11/group_by.h b/include/sqlpp11/group_by.h index 7bd4c296..df168583 100644 --- a/include/sqlpp11/group_by.h +++ b/include/sqlpp11/group_by.h @@ -75,6 +75,9 @@ namespace sqlpp using _nodes = detail::type_vector; using _is_dynamic = is_database; + using _provided_aggregates = typename std::conditional<_is_dynamic::value, + detail::type_set<>, + detail::make_type_set_t>::type; // Data using _data_t = group_by_data_t; diff --git a/include/sqlpp11/select_column_list.h b/include/sqlpp11/select_column_list.h index 5322c103..7a90bdef 100644 --- a/include/sqlpp11/select_column_list.h +++ b/include/sqlpp11/select_column_list.h @@ -168,6 +168,17 @@ namespace sqlpp } }; + struct assert_aggregates_t + { + using type = std::false_type; + + template + static void _() + { + static_assert(wrong_t::value, "not all columns are made of aggregates, despite group_by or similar"); + } + }; + // SELECTED COLUMNS template struct select_column_list_t @@ -262,10 +273,16 @@ namespace sqlpp return t.selected_columns; } - using _consistency_check = + using _column_check = typename std::conditional::value, consistent_t, assert_no_unknown_tables_in_selected_columns_t>::type; + + using _aggregate_check = typename std::conditional::value, + consistent_t, + assert_aggregates_t>::type; + + using _consistency_check = detail::get_first_if; }; // Result methods diff --git a/include/sqlpp11/statement.h b/include/sqlpp11/statement.h index cf4e5c9d..b8e48769 100644 --- a/include/sqlpp11/statement.h +++ b/include/sqlpp11/statement.h @@ -103,12 +103,18 @@ namespace sqlpp using _all_provided_tables = detail::make_joined_set_t...>; using _all_provided_outer_tables = detail::make_joined_set_t...>; using _all_extra_tables = detail::make_joined_set_t...>; + using _all_provided_aggregates = detail::make_joined_set_t...>; using _known_tables = detail::make_joined_set_t<_all_provided_tables, _all_extra_tables>; template using _no_unknown_tables = detail::is_subset_of, _known_tables>; + template + using _no_unknown_aggregates = + logic::any_t<_all_provided_aggregates::size::value == 0, + logic::all_t::value...>::value>; + template