0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Minimal over() implementation for aggregate functions (#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 <42680490+MaciumDue@users.noreply.github.com>
This commit is contained in:
MacDue 2020-02-15 06:54:22 +00:00 committed by GitHub
parent 6d2b64eff9
commit a51b6da3bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 189 additions and 14 deletions

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2013-2020, Roland Bock, MacDue
* 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.
*/
#ifndef SQLPP11_AGGREGATE_FUNCTION_OPERATORS_H
#define SQLPP11_AGGREGATE_FUNCTION_OPERATORS_H
namespace sqlpp
{
template <typename Expr>
struct aggregate_function_operators
{
over_t<Expr> over() const
{
return {*static_cast<const Expr*>(this)};
}
};
} // namespace sqlpp
#endif

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2016, Roland Bock * Copyright (c) 2013-2020, Roland Bock, MacDue
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
@ -56,6 +56,7 @@ namespace sqlpp
template <typename Flag, typename Expr> template <typename Flag, typename Expr>
struct avg_t : public expression_operators<avg_t<Flag, Expr>, floating_point>, struct avg_t : public expression_operators<avg_t<Flag, Expr>, floating_point>,
public aggregate_function_operators<avg_t<Flag, Expr>>,
public alias_operators<avg_t<Flag, Expr>> public alias_operators<avg_t<Flag, Expr>>
{ {
using _traits = make_traits<floating_point, tag::is_expression, tag::is_selectable>; using _traits = make_traits<floating_point, tag::is_expression, tag::is_selectable>;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2015, Roland Bock * Copyright (c) 2013-2020, Roland Bock, MacDue
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
@ -27,8 +27,10 @@
#ifndef SQLPP11_AGGREGATE_FUNCTIONS_COUNT_H #ifndef SQLPP11_AGGREGATE_FUNCTIONS_COUNT_H
#define SQLPP11_AGGREGATE_FUNCTIONS_COUNT_H #define SQLPP11_AGGREGATE_FUNCTIONS_COUNT_H
#include <sqlpp11/over.h>
#include <sqlpp11/char_sequence.h> #include <sqlpp11/char_sequence.h>
#include <sqlpp11/select_flags.h> #include <sqlpp11/select_flags.h>
#include <sqlpp11/aggregate_function_operators.h>
#include <sqlpp11/data_types/integral/data_type.h> #include <sqlpp11/data_types/integral/data_type.h>
namespace sqlpp namespace sqlpp
@ -57,6 +59,7 @@ namespace sqlpp
template <typename Flag, typename Expr> template <typename Flag, typename Expr>
struct count_t : public expression_operators<count_t<Flag, Expr>, integral>, struct count_t : public expression_operators<count_t<Flag, Expr>, integral>,
public aggregate_function_operators<count_t<Flag, Expr>>,
public alias_operators<count_t<Flag, Expr>> public alias_operators<count_t<Flag, Expr>>
{ {
using _traits = make_traits<integral, tag::is_expression /*, tag::is_selectable*/>; using _traits = make_traits<integral, tag::is_expression /*, tag::is_selectable*/>;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2015, Roland Bock * Copyright (c) 2013-2020, Roland Bock, MacDue
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
@ -55,7 +55,9 @@ namespace sqlpp
}; };
template <typename Expr> template <typename Expr>
struct max_t : public expression_operators<max_t<Expr>, value_type_of<Expr>>, public alias_operators<max_t<Expr>> struct max_t : public expression_operators<max_t<Expr>, value_type_of<Expr>>,
public aggregate_function_operators<max_t<Expr>>,
public alias_operators<max_t<Expr>>
{ {
using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_selectable>; using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_selectable>;
using _nodes = detail::type_vector<Expr, aggregate_function>; using _nodes = detail::type_vector<Expr, aggregate_function>;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2015, Roland Bock * Copyright (c) 2013-2020, Roland Bock, MacDue
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
@ -55,7 +55,9 @@ namespace sqlpp
}; };
template <typename Expr> template <typename Expr>
struct min_t : public expression_operators<min_t<Expr>, value_type_of<Expr>>, public alias_operators<min_t<Expr>> struct min_t : public expression_operators<min_t<Expr>, value_type_of<Expr>>,
public aggregate_function_operators<min_t<Expr>>,
public alias_operators<min_t<Expr>>
{ {
using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_selectable>; using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_selectable>;
using _nodes = detail::type_vector<Expr, aggregate_function>; using _nodes = detail::type_vector<Expr, aggregate_function>;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2015, Roland Bock * Copyright (c) 2013-2020, Roland Bock, MacDue
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
@ -56,6 +56,7 @@ namespace sqlpp
template <typename Flag, typename Expr> template <typename Flag, typename Expr>
struct sum_t : public expression_operators<sum_t<Flag, Expr>, value_type_of<Expr>>, struct sum_t : public expression_operators<sum_t<Flag, Expr>, value_type_of<Expr>>,
public aggregate_function_operators<sum_t<Flag, Expr>>,
public alias_operators<sum_t<Flag, Expr>> public alias_operators<sum_t<Flag, Expr>>
{ {
using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_selectable>; using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_selectable>;

73
include/sqlpp11/over.h Normal file
View File

@ -0,0 +1,73 @@
/*
* Copyright (c) 2013-2020, Roland Bock, MacDue
* 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.
*/
#ifndef SQLPP11_OVER_H
#define SQLPP11_OVER_H
#include <sqlpp11/type_traits.h>
#include <sqlpp11/serializer.h>
namespace sqlpp
{
template <typename AggregateExpr>
struct over_t : public expression_operators<over_t<AggregateExpr>, integral>,
public alias_operators<over_t<AggregateExpr>>
{
using _traits = make_traits<integral, tag::is_expression>;
using _nodes = detail::type_vector<AggregateExpr, aggregate_function>;
using _auto_alias_t = typename AggregateExpr::_auto_alias_t;
over_t(AggregateExpr aggregate_expression)
: _aggregate_expression(aggregate_expression)
{
}
over_t(const over_t&) = default;
over_t(over_t&&) = default;
over_t& operator=(const over_t&) = default;
over_t& operator=(over_t&&) = default;
~over_t() = default;
AggregateExpr _aggregate_expression;
};
template <typename Context, typename AggregateExpr>
struct serializer_t<Context, over_t<AggregateExpr>>
{
using _serialize_check = serialize_check_of<Context, AggregateExpr>;
using T = over_t<AggregateExpr>;
static Context& _(const T& t, Context& context)
{
serialize_operand(t._aggregate_expression, context);
context << " OVER()";
return context;
}
};
} // namespace sqlpp
#endif

View File

@ -27,12 +27,13 @@ set(test_serializer_names
Blob Blob
CustomQuery CustomQuery
DynamicWhere DynamicWhere
ForUpdate ForUpdate
From From
In In
Insert Insert
TableAlias Over
Where TableAlias
Where
) )
create_test_sourcelist(test_serializer_sources test_serializer_main.cpp ${test_serializer_names}) create_test_sourcelist(test_serializer_sources test_serializer_main.cpp ${test_serializer_names})
@ -45,7 +46,7 @@ if (SQLPP11_TESTS_CXX_STD)
set_property(TARGET sqlpp11_test_serializer PROPERTY CXX_STANDARD_REQUIRED yes) set_property(TARGET sqlpp11_test_serializer PROPERTY CXX_STANDARD_REQUIRED yes)
set_property(TARGET sqlpp11_test_serializer PROPERTY CXX_EXTENSIONS no) set_property(TARGET sqlpp11_test_serializer PROPERTY CXX_EXTENSIONS no)
endif() endif()
foreach(test_serializer IN LISTS test_serializer_names) foreach(test_serializer IN LISTS test_serializer_names)
add_test(NAME sqlpp11.test_serializer.${test_serializer} add_test(NAME sqlpp11.test_serializer.${test_serializer}
COMMAND sqlpp11_test_serializer ${test_serializer} COMMAND sqlpp11_test_serializer ${test_serializer}

50
test_serializer/Over.cpp Normal file
View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2016-2020, Roland Bock, MacDue
* 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.
*/
#include "Sample.h"
#include "compare.h"
#include <sqlpp11/sqlpp11.h>
SQLPP_ALIAS_PROVIDER(dueutil)
int Over(int, char* []) {
auto const foo = test::TabFoo{};
// no/auto alias (wrapped in select so alias is applied)
compare(__LINE__, select(avg(foo.omega).over()), "SELECT AVG(tab_foo.omega) OVER() AS avg_");
compare(__LINE__, select(count(foo.omega).over()), "SELECT COUNT(tab_foo.omega) OVER() AS count_");
compare(__LINE__, select(max(foo.omega).over()), "SELECT MAX(tab_foo.omega) OVER() AS max_");
compare(__LINE__, select(min(foo.omega).over()), "SELECT MIN(tab_foo.omega) OVER() AS min_");
compare(__LINE__, select(sum(foo.omega).over()), "SELECT SUM(tab_foo.omega) OVER() AS sum_");
// alias
compare(__LINE__, avg(foo.omega).over().as(dueutil), "AVG(tab_foo.omega) OVER() AS dueutil");
compare(__LINE__, count(foo.omega).over().as(dueutil), "COUNT(tab_foo.omega) OVER() AS dueutil");
compare(__LINE__, max(foo.omega).over().as(dueutil), "MAX(tab_foo.omega) OVER() AS dueutil");
compare(__LINE__, min(foo.omega).over().as(dueutil), "MIN(tab_foo.omega) OVER() AS dueutil");
compare(__LINE__, sum(foo.omega).over().as(dueutil), "SUM(tab_foo.omega) OVER() AS dueutil");
return 0;
}