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

Fix parentheses for aggregate functions

This commit is contained in:
Roland Bock 2024-08-10 09:30:20 +02:00
parent 7a900b147c
commit 1339cbd0e6
13 changed files with 87 additions and 57 deletions

View File

@ -83,13 +83,13 @@ namespace sqlpp
template <typename Context, typename Flag, typename Expr>
Context& serialize(Context& context, const avg_t<Flag, Expr>& t)
{
context << "MAX(";
context << "AVG(";
if (std::is_same<distinct_t, Flag>::value)
{
serialize(context, Flag());
context << ' ';
}
serialize_operand(context, t._expr);
serialize(context, t._expr);
context << ")";
return context;
}

View File

@ -89,7 +89,7 @@ namespace sqlpp
serialize(context, Flag());
context << ' ';
}
serialize_operand(context, t._expr);
serialize(context, t._expr);
context << ")";
return context;
}

View File

@ -88,7 +88,7 @@ namespace sqlpp
serialize(context, Flag());
context << ' ';
}
serialize_operand(context, t._expr);
serialize(context, t._expr);
context << ")";
return context;
}

View File

@ -88,7 +88,7 @@ namespace sqlpp
serialize(context, Flag());
context << ' ';
}
serialize_operand(context, t._expr);
serialize(context, t._expr);
context << ")";
return context;
}

View File

@ -90,7 +90,7 @@ namespace sqlpp
serialize(context, Flag());
context << ' ';
}
serialize_operand(context, t._expr);
serialize(context, t._expr);
context << ")";
return context;
}

View File

@ -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)

View File

@ -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)

View File

@ -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 <sqlpp11/sqlpp11.h>
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;
}

View File

@ -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 <sqlpp11/sqlpp11.h>
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;
}

View File

@ -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 <sqlpp11/sqlpp11.h>
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;
}

View File

@ -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 <sqlpp11/sqlpp11.h>
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;
}

View File

@ -24,19 +24,19 @@
*/
#include "Sample.h"
#include "compare.h"
#include "../compare.h"
#include <sqlpp11/sqlpp11.h>
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;
}

View File

@ -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 <sqlpp11/sqlpp11.h>
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;
}