From 1339cbd0e676973a8c9d6ccec65fd8e78e787538 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sat, 10 Aug 2024 09:30:20 +0200 Subject: [PATCH] Fix parentheses for aggregate functions --- include/sqlpp11/core/aggregate_function/avg.h | 4 +- .../sqlpp11/core/aggregate_function/count.h | 2 +- include/sqlpp11/core/aggregate_function/max.h | 2 +- include/sqlpp11/core/aggregate_function/min.h | 2 +- include/sqlpp11/core/aggregate_function/sum.h | 2 +- tests/core/serialize/CMakeLists.txt | 7 +--- .../aggregate_function/CMakeLists.txt | 38 +++++++++++++++++++ .../{Avg.cpp => aggregate_function/avg.cpp} | 15 ++++---- .../count.cpp} | 15 ++++---- .../{Max.cpp => aggregate_function/max.cpp} | 15 ++++---- .../{Min.cpp => aggregate_function/min.cpp} | 14 +++---- .../{Over.cpp => aggregate_function/over.cpp} | 14 +++---- .../{Sum.cpp => aggregate_function/sum.cpp} | 14 +++---- 13 files changed, 87 insertions(+), 57 deletions(-) create mode 100644 tests/core/serialize/aggregate_function/CMakeLists.txt rename tests/core/serialize/{Avg.cpp => aggregate_function/avg.cpp} (85%) rename tests/core/serialize/{Count.cpp => aggregate_function/count.cpp} (85%) rename tests/core/serialize/{Max.cpp => aggregate_function/max.cpp} (86%) rename tests/core/serialize/{Min.cpp => aggregate_function/min.cpp} (88%) rename tests/core/serialize/{Over.cpp => aggregate_function/over.cpp} (71%) rename tests/core/serialize/{Sum.cpp => aggregate_function/sum.cpp} (88%) diff --git a/include/sqlpp11/core/aggregate_function/avg.h b/include/sqlpp11/core/aggregate_function/avg.h index 073bbff0..7f3c1fc8 100644 --- a/include/sqlpp11/core/aggregate_function/avg.h +++ b/include/sqlpp11/core/aggregate_function/avg.h @@ -83,13 +83,13 @@ namespace sqlpp template Context& serialize(Context& context, const avg_t& t) { - context << "MAX("; + context << "AVG("; if (std::is_same::value) { serialize(context, Flag()); context << ' '; } - serialize_operand(context, t._expr); + serialize(context, t._expr); context << ")"; return context; } diff --git a/include/sqlpp11/core/aggregate_function/count.h b/include/sqlpp11/core/aggregate_function/count.h index 8760aa04..32aae700 100644 --- a/include/sqlpp11/core/aggregate_function/count.h +++ b/include/sqlpp11/core/aggregate_function/count.h @@ -89,7 +89,7 @@ namespace sqlpp serialize(context, Flag()); context << ' '; } - serialize_operand(context, t._expr); + serialize(context, t._expr); context << ")"; return context; } diff --git a/include/sqlpp11/core/aggregate_function/max.h b/include/sqlpp11/core/aggregate_function/max.h index 612478e9..afbffc34 100644 --- a/include/sqlpp11/core/aggregate_function/max.h +++ b/include/sqlpp11/core/aggregate_function/max.h @@ -88,7 +88,7 @@ namespace sqlpp serialize(context, Flag()); context << ' '; } - serialize_operand(context, t._expr); + serialize(context, t._expr); context << ")"; return context; } diff --git a/include/sqlpp11/core/aggregate_function/min.h b/include/sqlpp11/core/aggregate_function/min.h index 8c154451..6b0de98a 100644 --- a/include/sqlpp11/core/aggregate_function/min.h +++ b/include/sqlpp11/core/aggregate_function/min.h @@ -88,7 +88,7 @@ namespace sqlpp serialize(context, Flag()); context << ' '; } - serialize_operand(context, t._expr); + serialize(context, t._expr); context << ")"; return context; } diff --git a/include/sqlpp11/core/aggregate_function/sum.h b/include/sqlpp11/core/aggregate_function/sum.h index 52d4cc9a..ca806f7e 100644 --- a/include/sqlpp11/core/aggregate_function/sum.h +++ b/include/sqlpp11/core/aggregate_function/sum.h @@ -90,7 +90,7 @@ namespace sqlpp serialize(context, Flag()); context << ' '; } - serialize_operand(context, t._expr); + serialize(context, t._expr); context << ")"; return context; } diff --git a/tests/core/serialize/CMakeLists.txt b/tests/core/serialize/CMakeLists.txt index 4ba89a4f..13ce3eae 100644 --- a/tests/core/serialize/CMakeLists.txt +++ b/tests/core/serialize/CMakeLists.txt @@ -23,9 +23,7 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. set(test_files - Avg.cpp Blob.cpp - Count.cpp CustomQuery.cpp DynamicWhere.cpp Float.cpp @@ -35,15 +33,11 @@ set(test_files IsNotNull.cpp IsNull.cpp Lower.cpp - Max.cpp - Min.cpp - Over.cpp Parameter.cpp ParameterizedVerbatim.cpp SelectAs.cpp SelectColumns.cpp SelectFlags.cpp - Sum.cpp TableAlias.cpp Trim.cpp Upper.cpp @@ -61,4 +55,5 @@ foreach(test_file IN LISTS test_files) ) endforeach() +add_subdirectory(aggregate_function) add_subdirectory(operator) diff --git a/tests/core/serialize/aggregate_function/CMakeLists.txt b/tests/core/serialize/aggregate_function/CMakeLists.txt new file mode 100644 index 00000000..85297c18 --- /dev/null +++ b/tests/core/serialize/aggregate_function/CMakeLists.txt @@ -0,0 +1,38 @@ +# Copyright (c) 2024, Roland Bock +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# Redistributions in binary form must reproduce the above copyright notice, this +# list of conditions and the following disclaimer in the documentation and/or +# other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +function(create_test name) + set(target sqlpp11_core_serialize_aggregate_function_${name}) + add_executable(${target} ${name}.cpp) + target_link_libraries(${target} PRIVATE sqlpp11::sqlpp11 sqlpp11_testing) + add_test(NAME ${target} COMMAND ${target}) +endfunction() + +create_test(avg) +create_test(count) +create_test(max) +create_test(min) +create_test(over) +create_test(sum) + diff --git a/tests/core/serialize/Avg.cpp b/tests/core/serialize/aggregate_function/avg.cpp similarity index 85% rename from tests/core/serialize/Avg.cpp rename to tests/core/serialize/aggregate_function/avg.cpp index 2d88ef2c..73c57357 100644 --- a/tests/core/serialize/Avg.cpp +++ b/tests/core/serialize/aggregate_function/avg.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2023, Roland Bock + * Copyright (c) 2023, Roland Bock * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -24,10 +24,10 @@ */ #include "Sample.h" -#include "compare.h" +#include "../compare.h" #include -int Avg(int, char* []) +int main(int, char* []) { const auto bar = test::TabBar{}; @@ -36,13 +36,12 @@ int Avg(int, char* []) SQLPP_COMPARE(avg(sqlpp::distinct, bar.id), "AVG(DISTINCT tab_bar.id)"); // Expression. -#warning: Note that the inner parens aren't necessary. - SQLPP_COMPARE(avg(bar.id + 7), "AVG((tab_bar.id + 7))"); - SQLPP_COMPARE(avg(sqlpp::distinct, bar.id + 7), "AVG(DISTINCT (tab_bar.id + 7))"); + SQLPP_COMPARE(avg(bar.id + 7), "AVG(tab_bar.id + 7)"); + SQLPP_COMPARE(avg(sqlpp::distinct, bar.id + 7), "AVG(DISTINCT tab_bar.id + 7)"); // With sub select. - SQLPP_COMPARE(avg(select(sqlpp::value(7).as(sqlpp::alias::a))), "AVG((SELECT 7 AS a))"); - SQLPP_COMPARE(avg(sqlpp::distinct, select(sqlpp::value(7).as(sqlpp::alias::a))), "AVG(DISTINCT (SELECT 7 AS a))"); + SQLPP_COMPARE(avg(select(sqlpp::value(7).as(sqlpp::alias::a))), "AVG(SELECT 7 AS a)"); + SQLPP_COMPARE(avg(sqlpp::distinct, select(sqlpp::value(7).as(sqlpp::alias::a))), "AVG(DISTINCT SELECT 7 AS a)"); return 0; } diff --git a/tests/core/serialize/Count.cpp b/tests/core/serialize/aggregate_function/count.cpp similarity index 85% rename from tests/core/serialize/Count.cpp rename to tests/core/serialize/aggregate_function/count.cpp index 6cca2e6d..3a89210b 100644 --- a/tests/core/serialize/Count.cpp +++ b/tests/core/serialize/aggregate_function/count.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2023, Roland Bock + * Copyright (c) 2023, Roland Bock * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -24,10 +24,10 @@ */ #include "Sample.h" -#include "compare.h" +#include "../compare.h" #include -int Count(int, char* []) +int main(int, char* []) { const auto bar = test::TabBar{}; @@ -36,13 +36,12 @@ int Count(int, char* []) SQLPP_COMPARE(count(sqlpp::distinct, bar.id), "COUNT(DISTINCT tab_bar.id)"); // Expression. -#warning: Note that the inner parens aren't necessary. - SQLPP_COMPARE(count(bar.id + 7), "COUNT((tab_bar.id + 7))"); - SQLPP_COMPARE(count(sqlpp::distinct, bar.id + 7), "COUNT(DISTINCT (tab_bar.id + 7))"); + SQLPP_COMPARE(count(bar.id + 7), "COUNT(tab_bar.id + 7)"); + SQLPP_COMPARE(count(sqlpp::distinct, bar.id + 7), "COUNT(DISTINCT tab_bar.id + 7)"); // With sub select. - SQLPP_COMPARE(count(select(sqlpp::value(7).as(sqlpp::alias::a))), "COUNT((SELECT 7 AS a))"); - SQLPP_COMPARE(count(sqlpp::distinct, select(sqlpp::value(7).as(sqlpp::alias::a))), "COUNT(DISTINCT (SELECT 7 AS a))"); + SQLPP_COMPARE(count(select(sqlpp::value(7).as(sqlpp::alias::a))), "COUNT(SELECT 7 AS a)"); + SQLPP_COMPARE(count(sqlpp::distinct, select(sqlpp::value(7).as(sqlpp::alias::a))), "COUNT(DISTINCT SELECT 7 AS a)"); return 0; } diff --git a/tests/core/serialize/Max.cpp b/tests/core/serialize/aggregate_function/max.cpp similarity index 86% rename from tests/core/serialize/Max.cpp rename to tests/core/serialize/aggregate_function/max.cpp index ecd7e5c1..33988afd 100644 --- a/tests/core/serialize/Max.cpp +++ b/tests/core/serialize/aggregate_function/max.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2023, Roland Bock + * Copyright (c) 2023, Roland Bock * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -24,10 +24,10 @@ */ #include "Sample.h" -#include "compare.h" +#include "../compare.h" #include -int Max(int, char* []) +int main(int, char* []) { const auto bar = test::TabBar{}; @@ -36,13 +36,12 @@ int Max(int, char* []) SQLPP_COMPARE(max(sqlpp::distinct, bar.id), "MAX(DISTINCT tab_bar.id)"); // Expression. - // Note that the inner parens aren't necessary. - SQLPP_COMPARE(max(bar.id + 7), "MAX((tab_bar.id + 7))"); - SQLPP_COMPARE(max(sqlpp::distinct, bar.id + 7), "MAX(DISTINCT (tab_bar.id + 7))"); + SQLPP_COMPARE(max(bar.id + 7), "MAX(tab_bar.id + 7)"); + SQLPP_COMPARE(max(sqlpp::distinct, bar.id + 7), "MAX(DISTINCT tab_bar.id + 7)"); // With sub select. - SQLPP_COMPARE(max(select(sqlpp::value(7).as(sqlpp::alias::a))), "MAX((SELECT 7 AS a))"); - SQLPP_COMPARE(max(sqlpp::distinct, select(sqlpp::value(7).as(sqlpp::alias::a))), "MAX(DISTINCT (SELECT 7 AS a))"); + SQLPP_COMPARE(max(select(sqlpp::value(7).as(sqlpp::alias::a))), "MAX(SELECT 7 AS a)"); + SQLPP_COMPARE(max(sqlpp::distinct, select(sqlpp::value(7).as(sqlpp::alias::a))), "MAX(DISTINCT SELECT 7 AS a)"); return 0; } diff --git a/tests/core/serialize/Min.cpp b/tests/core/serialize/aggregate_function/min.cpp similarity index 88% rename from tests/core/serialize/Min.cpp rename to tests/core/serialize/aggregate_function/min.cpp index 96806e0a..c81198c2 100644 --- a/tests/core/serialize/Min.cpp +++ b/tests/core/serialize/aggregate_function/min.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2023, Roland Bock + * Copyright (c) 2023, Roland Bock * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -24,10 +24,10 @@ */ #include "Sample.h" -#include "compare.h" +#include "../compare.h" #include -int Min(int, char* []) +int main(int, char* []) { const auto bar = test::TabBar{}; @@ -37,12 +37,12 @@ int Min(int, char* []) // Expression. // Note that the inner parens aren't necessary. - SQLPP_COMPARE(min(bar.id + 7), "MIN((tab_bar.id + 7))"); - SQLPP_COMPARE(min(sqlpp::distinct, bar.id + 7), "MIN(DISTINCT (tab_bar.id + 7))"); + SQLPP_COMPARE(min(bar.id + 7), "MIN(tab_bar.id + 7)"); + SQLPP_COMPARE(min(sqlpp::distinct, bar.id + 7), "MIN(DISTINCT tab_bar.id + 7)"); // With sub select. - SQLPP_COMPARE(min(select(sqlpp::value(7).as(sqlpp::alias::a))), "MIN((SELECT 7 AS a))"); - SQLPP_COMPARE(min(sqlpp::distinct, select(sqlpp::value(7).as(sqlpp::alias::a))), "MIN(DISTINCT (SELECT 7 AS a))"); + SQLPP_COMPARE(min(select(sqlpp::value(7).as(sqlpp::alias::a))), "MIN(SELECT 7 AS a)"); + SQLPP_COMPARE(min(sqlpp::distinct, select(sqlpp::value(7).as(sqlpp::alias::a))), "MIN(DISTINCT SELECT 7 AS a)"); return 0; } diff --git a/tests/core/serialize/Over.cpp b/tests/core/serialize/aggregate_function/over.cpp similarity index 71% rename from tests/core/serialize/Over.cpp rename to tests/core/serialize/aggregate_function/over.cpp index 02dcc16a..aa70df8b 100644 --- a/tests/core/serialize/Over.cpp +++ b/tests/core/serialize/aggregate_function/over.cpp @@ -24,19 +24,19 @@ */ #include "Sample.h" -#include "compare.h" +#include "../compare.h" #include SQLPP_ALIAS_PROVIDER(cheese); -int Over(int, char* []) { +int main(int, char* []) { auto const foo = test::TabFoo{}; - SQLPP_COMPARE(select(avg(foo.doubleN).over().as(cheese)), "SELECT AVG(tab_foo.double_n) OVER() AS cheese"); - SQLPP_COMPARE(select(count(foo.doubleN).over().as(cheese)), "SELECT COUNT(tab_foo.double_n) OVER() AS cheese"); - SQLPP_COMPARE(select(max(foo.doubleN).over().as(cheese)), "SELECT MAX(tab_foo.double_n) OVER() AS cheese"); - SQLPP_COMPARE(select(min(foo.doubleN).over().as(cheese)), "SELECT MIN(tab_foo.double_n) OVER() AS cheese"); - SQLPP_COMPARE(select(sum(foo.doubleN).over().as(cheese)), "SELECT SUM(tab_foo.double_n) OVER() AS cheese"); + SQLPP_COMPARE(avg(foo.doubleN).over().as(cheese), "AVG(tab_foo.double_n) OVER() AS cheese"); + SQLPP_COMPARE(count(foo.doubleN).over().as(cheese), "COUNT(tab_foo.double_n) OVER() AS cheese"); + SQLPP_COMPARE(max(foo.doubleN).over().as(cheese), "MAX(tab_foo.double_n) OVER() AS cheese"); + SQLPP_COMPARE(min(foo.doubleN).over().as(cheese), "MIN(tab_foo.double_n) OVER() AS cheese"); + SQLPP_COMPARE(sum(foo.doubleN).over().as(cheese), "SUM(tab_foo.double_n) OVER() AS cheese"); return 0; } diff --git a/tests/core/serialize/Sum.cpp b/tests/core/serialize/aggregate_function/sum.cpp similarity index 88% rename from tests/core/serialize/Sum.cpp rename to tests/core/serialize/aggregate_function/sum.cpp index e0adeda0..2a7c56f9 100644 --- a/tests/core/serialize/Sum.cpp +++ b/tests/core/serialize/aggregate_function/sum.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2023, Roland Bock + * Copyright (c) 2023, Roland Bock * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -24,10 +24,10 @@ */ #include "Sample.h" -#include "compare.h" +#include "../compare.h" #include -int Sum(int, char* []) +int main(int, char* []) { const auto bar = test::TabBar{}; @@ -37,12 +37,12 @@ int Sum(int, char* []) // Expression. // Note that the inner parens aren't necessary. - SQLPP_COMPARE(sum(bar.id + 7), "SUM((tab_bar.id + 7))"); - SQLPP_COMPARE(sum(sqlpp::distinct, bar.id + 7), "SUM(DISTINCT (tab_bar.id + 7))"); + SQLPP_COMPARE(sum(bar.id + 7), "SUM(tab_bar.id + 7)"); + SQLPP_COMPARE(sum(sqlpp::distinct, bar.id + 7), "SUM(DISTINCT tab_bar.id + 7)"); // With sub select. - SQLPP_COMPARE(sum(select(sqlpp::value(7).as(sqlpp::alias::a))), "SUM((SELECT 7 AS a))"); - SQLPP_COMPARE(sum(sqlpp::distinct, select(sqlpp::value(7).as(sqlpp::alias::a))), "SUM(DISTINCT (SELECT 7 AS a))"); + SQLPP_COMPARE(sum(select(sqlpp::value(7).as(sqlpp::alias::a))), "SUM(SELECT 7 AS a)"); + SQLPP_COMPARE(sum(sqlpp::distinct, select(sqlpp::value(7).as(sqlpp::alias::a))), "SUM(DISTINCT SELECT 7 AS a)"); return 0; }