From b95a23b1615997871d314a1926814d882fea712e Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sat, 17 Aug 2024 12:35:06 +0200 Subject: [PATCH] More tests and cleaner detection for result clause (that is the clause that determines the result type of the statement) --- include/sqlpp11/core/clause/insert.h | 5 +- include/sqlpp11/core/clause/remove.h | 5 +- .../sqlpp11/core/clause/select_column_list.h | 21 +++++++- include/sqlpp11/core/clause/union.h | 4 +- include/sqlpp11/core/clause/update.h | 5 +- include/sqlpp11/core/detail/get_last.h | 13 ++--- include/sqlpp11/core/group_by_column.h | 5 ++ include/sqlpp11/core/operator/as_expression.h | 15 ++++++ include/sqlpp11/core/query/custom_query.h | 3 +- include/sqlpp11/core/query/statement.h | 8 +-- include/sqlpp11/core/type_traits.h | 4 +- include/sqlpp11/core/type_traits/aggregates.h | 6 +++ include/sqlpp11/sqlite3/insert_or.h | 5 +- tests/core/types/CMakeLists.txt | 1 + tests/core/types/clause/select_columns.cpp | 21 ++++++++ tests/core/types/detail/CMakeLists.txt | 32 +++++++++++ tests/core/types/detail/get_last_if.cpp | 54 +++++++++++++++++++ 17 files changed, 187 insertions(+), 20 deletions(-) create mode 100644 tests/core/types/detail/CMakeLists.txt create mode 100644 tests/core/types/detail/get_last_if.cpp diff --git a/include/sqlpp11/core/clause/insert.h b/include/sqlpp11/core/clause/insert.h index 87866118..b51db566 100644 --- a/include/sqlpp11/core/clause/insert.h +++ b/include/sqlpp11/core/clause/insert.h @@ -43,7 +43,7 @@ namespace sqlpp struct insert_t : public statement_name_t { - using _traits = make_traits; + using _traits = make_traits; struct _sqlpp_name_tag { }; @@ -86,6 +86,9 @@ namespace sqlpp }; }; + template<> + struct is_result_clause : public std::true_type {}; + template auto to_sql_string(Context& , const insert_name_t&) -> std::string { diff --git a/include/sqlpp11/core/clause/remove.h b/include/sqlpp11/core/clause/remove.h index ee786267..446b9a32 100644 --- a/include/sqlpp11/core/clause/remove.h +++ b/include/sqlpp11/core/clause/remove.h @@ -42,7 +42,7 @@ namespace sqlpp }; struct remove_t : public statement_name_t { - using _traits = make_traits; + using _traits = make_traits; struct _sqlpp_name_tag { }; @@ -85,6 +85,9 @@ namespace sqlpp }; }; + template<> + struct is_result_clause : public std::true_type {}; + template auto to_sql_string(Context& , const remove_name_t&) -> std::string { diff --git a/include/sqlpp11/core/clause/select_column_list.h b/include/sqlpp11/core/clause/select_column_list.h index ee7372db..2130cda5 100644 --- a/include/sqlpp11/core/clause/select_column_list.h +++ b/include/sqlpp11/core/clause/select_column_list.h @@ -67,7 +67,7 @@ namespace sqlpp template struct select_traits { - using _traits = make_traits; + using _traits = make_traits; }; template @@ -75,7 +75,6 @@ namespace sqlpp { using _traits = make_traits, tag::is_select_column_list, - tag::is_return_value, tag::is_expression, tag::is_selectable>; }; @@ -227,6 +226,24 @@ namespace sqlpp { }; + // If a GROUP BY clause defines known aggregate columns or the SELECT columns contain an aggregate function then ALL + // columns need to be aggregate. + template + struct has_correct_aggregates> + : public std::integral_constant< + bool, + (detail::type_vector_size::value == 0 and + logic::none_t>>::value...>::value) or + logic::all_t>>::value...>::value> + { + }; + + template + struct is_result_clause> : public std::true_type + { + }; + template struct nodes_of> { diff --git a/include/sqlpp11/core/clause/union.h b/include/sqlpp11/core/clause/union.h index 807ba31d..9fe191cf 100644 --- a/include/sqlpp11/core/clause/union.h +++ b/include/sqlpp11/core/clause/union.h @@ -60,7 +60,7 @@ namespace sqlpp template struct union_t { - using _traits = make_traits; + using _traits = make_traits; using _nodes = detail::type_vector; using _data_t = union_data_t; @@ -91,8 +91,6 @@ namespace sqlpp typename Rhs::_consistency_check>; }; - template - using _result_methods_t = typename Lhs::template _result_methods_t; }; SQLPP_PORTABLE_STATIC_ASSERT(assert_union_args_are_statements_t, "arguments for union() must be statements"); diff --git a/include/sqlpp11/core/clause/update.h b/include/sqlpp11/core/clause/update.h index 61d69e1a..f6841f46 100644 --- a/include/sqlpp11/core/clause/update.h +++ b/include/sqlpp11/core/clause/update.h @@ -43,7 +43,7 @@ namespace sqlpp struct update_t : public statement_name_t { - using _traits = make_traits; + using _traits = make_traits; struct _sqlpp_name_tag { }; @@ -86,6 +86,9 @@ namespace sqlpp }; }; + template<> + struct is_result_clause : public std::true_type {}; + template auto to_sql_string(Context& , const update_name_t&) -> std::string { diff --git a/include/sqlpp11/core/detail/get_last.h b/include/sqlpp11/core/detail/get_last.h index d94c0e55..93584193 100644 --- a/include/sqlpp11/core/detail/get_last.h +++ b/include/sqlpp11/core/detail/get_last.h @@ -33,22 +33,23 @@ namespace sqlpp namespace detail { template