Skip to content

Commit a51b6da

Browse files
MacDuebenmxwl-arm
andauthored
Minimal over() implementation for aggregate functions (rbock#316)
* Minimal over() implementation for aggregate functions * auto_alias support for over() * add missing typename * Test .over() serialization * Add missing return to test * Fix testing over auto alias Co-authored-by: Ben Maxwell <[email protected]>
1 parent 6d2b64e commit a51b6da

File tree

9 files changed

+189
-14
lines changed

9 files changed

+189
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2013-2020, Roland Bock, MacDue
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* Redistributions in binary form must reproduce the above copyright notice, this
12+
* list of conditions and the following disclaimer in the documentation and/or
13+
* other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
19+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
22+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
#ifndef SQLPP11_AGGREGATE_FUNCTION_OPERATORS_H
27+
#define SQLPP11_AGGREGATE_FUNCTION_OPERATORS_H
28+
29+
namespace sqlpp
30+
{
31+
template <typename Expr>
32+
struct aggregate_function_operators
33+
{
34+
over_t<Expr> over() const
35+
{
36+
return {*static_cast<const Expr*>(this)};
37+
}
38+
};
39+
} // namespace sqlpp
40+
41+
#endif
42+

include/sqlpp11/aggregate_functions/avg.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013-2016, Roland Bock
2+
* Copyright (c) 2013-2020, Roland Bock, MacDue
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without modification,
@@ -56,6 +56,7 @@ namespace sqlpp
5656

5757
template <typename Flag, typename Expr>
5858
struct avg_t : public expression_operators<avg_t<Flag, Expr>, floating_point>,
59+
public aggregate_function_operators<avg_t<Flag, Expr>>,
5960
public alias_operators<avg_t<Flag, Expr>>
6061
{
6162
using _traits = make_traits<floating_point, tag::is_expression, tag::is_selectable>;

include/sqlpp11/aggregate_functions/count.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013-2015, Roland Bock
2+
* Copyright (c) 2013-2020, Roland Bock, MacDue
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without modification,
@@ -27,8 +27,10 @@
2727
#ifndef SQLPP11_AGGREGATE_FUNCTIONS_COUNT_H
2828
#define SQLPP11_AGGREGATE_FUNCTIONS_COUNT_H
2929

30+
#include <sqlpp11/over.h>
3031
#include <sqlpp11/char_sequence.h>
3132
#include <sqlpp11/select_flags.h>
33+
#include <sqlpp11/aggregate_function_operators.h>
3234
#include <sqlpp11/data_types/integral/data_type.h>
3335

3436
namespace sqlpp
@@ -57,6 +59,7 @@ namespace sqlpp
5759

5860
template <typename Flag, typename Expr>
5961
struct count_t : public expression_operators<count_t<Flag, Expr>, integral>,
62+
public aggregate_function_operators<count_t<Flag, Expr>>,
6063
public alias_operators<count_t<Flag, Expr>>
6164
{
6265
using _traits = make_traits<integral, tag::is_expression /*, tag::is_selectable*/>;

include/sqlpp11/aggregate_functions/max.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013-2015, Roland Bock
2+
* Copyright (c) 2013-2020, Roland Bock, MacDue
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without modification,
@@ -55,7 +55,9 @@ namespace sqlpp
5555
};
5656

5757
template <typename Expr>
58-
struct max_t : public expression_operators<max_t<Expr>, value_type_of<Expr>>, public alias_operators<max_t<Expr>>
58+
struct max_t : public expression_operators<max_t<Expr>, value_type_of<Expr>>,
59+
public aggregate_function_operators<max_t<Expr>>,
60+
public alias_operators<max_t<Expr>>
5961
{
6062
using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_selectable>;
6163
using _nodes = detail::type_vector<Expr, aggregate_function>;

include/sqlpp11/aggregate_functions/min.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013-2015, Roland Bock
2+
* Copyright (c) 2013-2020, Roland Bock, MacDue
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without modification,
@@ -55,7 +55,9 @@ namespace sqlpp
5555
};
5656

5757
template <typename Expr>
58-
struct min_t : public expression_operators<min_t<Expr>, value_type_of<Expr>>, public alias_operators<min_t<Expr>>
58+
struct min_t : public expression_operators<min_t<Expr>, value_type_of<Expr>>,
59+
public aggregate_function_operators<min_t<Expr>>,
60+
public alias_operators<min_t<Expr>>
5961
{
6062
using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_selectable>;
6163
using _nodes = detail::type_vector<Expr, aggregate_function>;

include/sqlpp11/aggregate_functions/sum.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013-2015, Roland Bock
2+
* Copyright (c) 2013-2020, Roland Bock, MacDue
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without modification,
@@ -56,6 +56,7 @@ namespace sqlpp
5656

5757
template <typename Flag, typename Expr>
5858
struct sum_t : public expression_operators<sum_t<Flag, Expr>, value_type_of<Expr>>,
59+
public aggregate_function_operators<sum_t<Flag, Expr>>,
5960
public alias_operators<sum_t<Flag, Expr>>
6061
{
6162
using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_selectable>;

include/sqlpp11/over.h

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2013-2020, Roland Bock, MacDue
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* Redistributions in binary form must reproduce the above copyright notice, this
12+
* list of conditions and the following disclaimer in the documentation and/or
13+
* other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
19+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
22+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
#ifndef SQLPP11_OVER_H
28+
#define SQLPP11_OVER_H
29+
30+
#include <sqlpp11/type_traits.h>
31+
#include <sqlpp11/serializer.h>
32+
33+
namespace sqlpp
34+
{
35+
template <typename AggregateExpr>
36+
struct over_t : public expression_operators<over_t<AggregateExpr>, integral>,
37+
public alias_operators<over_t<AggregateExpr>>
38+
{
39+
using _traits = make_traits<integral, tag::is_expression>;
40+
using _nodes = detail::type_vector<AggregateExpr, aggregate_function>;
41+
42+
using _auto_alias_t = typename AggregateExpr::_auto_alias_t;
43+
44+
over_t(AggregateExpr aggregate_expression)
45+
: _aggregate_expression(aggregate_expression)
46+
{
47+
}
48+
49+
over_t(const over_t&) = default;
50+
over_t(over_t&&) = default;
51+
over_t& operator=(const over_t&) = default;
52+
over_t& operator=(over_t&&) = default;
53+
~over_t() = default;
54+
55+
AggregateExpr _aggregate_expression;
56+
};
57+
58+
template <typename Context, typename AggregateExpr>
59+
struct serializer_t<Context, over_t<AggregateExpr>>
60+
{
61+
using _serialize_check = serialize_check_of<Context, AggregateExpr>;
62+
using T = over_t<AggregateExpr>;
63+
64+
static Context& _(const T& t, Context& context)
65+
{
66+
serialize_operand(t._aggregate_expression, context);
67+
context << " OVER()";
68+
return context;
69+
}
70+
};
71+
} // namespace sqlpp
72+
73+
#endif

test_serializer/CMakeLists.txt

+8-7
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ set(test_serializer_names
2727
Blob
2828
CustomQuery
2929
DynamicWhere
30-
ForUpdate
31-
From
32-
In
33-
Insert
34-
TableAlias
35-
Where
30+
ForUpdate
31+
From
32+
In
33+
Insert
34+
Over
35+
TableAlias
36+
Where
3637
)
3738

3839
create_test_sourcelist(test_serializer_sources test_serializer_main.cpp ${test_serializer_names})
@@ -45,7 +46,7 @@ if (SQLPP11_TESTS_CXX_STD)
4546
set_property(TARGET sqlpp11_test_serializer PROPERTY CXX_STANDARD_REQUIRED yes)
4647
set_property(TARGET sqlpp11_test_serializer PROPERTY CXX_EXTENSIONS no)
4748
endif()
48-
49+
4950
foreach(test_serializer IN LISTS test_serializer_names)
5051
add_test(NAME sqlpp11.test_serializer.${test_serializer}
5152
COMMAND sqlpp11_test_serializer ${test_serializer}

test_serializer/Over.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2016-2020, Roland Bock, MacDue
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17+
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22+
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23+
* OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
#include "Sample.h"
27+
#include "compare.h"
28+
#include <sqlpp11/sqlpp11.h>
29+
30+
SQLPP_ALIAS_PROVIDER(dueutil)
31+
32+
int Over(int, char* []) {
33+
auto const foo = test::TabFoo{};
34+
35+
// no/auto alias (wrapped in select so alias is applied)
36+
compare(__LINE__, select(avg(foo.omega).over()), "SELECT AVG(tab_foo.omega) OVER() AS avg_");
37+
compare(__LINE__, select(count(foo.omega).over()), "SELECT COUNT(tab_foo.omega) OVER() AS count_");
38+
compare(__LINE__, select(max(foo.omega).over()), "SELECT MAX(tab_foo.omega) OVER() AS max_");
39+
compare(__LINE__, select(min(foo.omega).over()), "SELECT MIN(tab_foo.omega) OVER() AS min_");
40+
compare(__LINE__, select(sum(foo.omega).over()), "SELECT SUM(tab_foo.omega) OVER() AS sum_");
41+
42+
// alias
43+
compare(__LINE__, avg(foo.omega).over().as(dueutil), "AVG(tab_foo.omega) OVER() AS dueutil");
44+
compare(__LINE__, count(foo.omega).over().as(dueutil), "COUNT(tab_foo.omega) OVER() AS dueutil");
45+
compare(__LINE__, max(foo.omega).over().as(dueutil), "MAX(tab_foo.omega) OVER() AS dueutil");
46+
compare(__LINE__, min(foo.omega).over().as(dueutil), "MIN(tab_foo.omega) OVER() AS dueutil");
47+
compare(__LINE__, sum(foo.omega).over().as(dueutil), "SUM(tab_foo.omega) OVER() AS dueutil");
48+
49+
return 0;
50+
}

0 commit comments

Comments
 (0)