mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-16 12:51:13 +08:00
More tests for aggregate functions
This commit is contained in:
parent
7f59918a09
commit
a6843f4518
@ -63,6 +63,11 @@ namespace sqlpp
|
|||||||
Expr _expr;
|
Expr _expr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename Flag, typename Expr>
|
||||||
|
struct is_aggregate<avg_t<Flag, Expr>> : public std::true_type
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
template <typename Flag, typename Expr>
|
template <typename Flag, typename Expr>
|
||||||
struct name_tag_of<avg_t<Flag, Expr>>: public name_tag_of<alias::avg_t>
|
struct name_tag_of<avg_t<Flag, Expr>>: public name_tag_of<alias::avg_t>
|
||||||
{
|
{
|
||||||
@ -88,7 +93,7 @@ namespace sqlpp
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
using check_avg_arg =
|
using check_avg_arg =
|
||||||
::sqlpp::enable_if_t<(is_numeric<T>::value or is_boolean<T>::value) and not contains_aggregate_function_t<T>::value>;
|
::sqlpp::enable_if_t<(is_numeric<T>::value or is_boolean<T>::value) and not contains_aggregate<T>::value>;
|
||||||
|
|
||||||
template <typename T, typename = check_avg_arg<T>>
|
template <typename T, typename = check_avg_arg<T>>
|
||||||
auto avg(T t) -> avg_t<noop, T>
|
auto avg(T t) -> avg_t<noop, T>
|
||||||
|
@ -63,6 +63,11 @@ namespace sqlpp
|
|||||||
Expr _expr;
|
Expr _expr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename Flag, typename Expr>
|
||||||
|
struct is_aggregate<count_t<Flag, Expr>> : public std::true_type
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
template <typename Flag, typename Expr>
|
template <typename Flag, typename Expr>
|
||||||
struct name_tag_of<count_t<Flag, Expr>>: public name_tag_of<alias::count_t>
|
struct name_tag_of<count_t<Flag, Expr>>: public name_tag_of<alias::count_t>
|
||||||
{
|
{
|
||||||
@ -88,7 +93,7 @@ namespace sqlpp
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
using check_count_arg =
|
using check_count_arg =
|
||||||
::sqlpp::enable_if_t<values_are_comparable<T, T>::value and not contains_aggregate_function_t<T>::value>;
|
::sqlpp::enable_if_t<values_are_comparable<T, T>::value and not contains_aggregate<T>::value>;
|
||||||
|
|
||||||
template <typename T, typename = check_count_arg<T>>
|
template <typename T, typename = check_count_arg<T>>
|
||||||
auto count(T t) -> count_t<noop, T>
|
auto count(T t) -> count_t<noop, T>
|
||||||
|
@ -62,6 +62,11 @@ namespace sqlpp
|
|||||||
Expr _expr;
|
Expr _expr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename Flag, typename Expr>
|
||||||
|
struct is_aggregate<max_t<Flag, Expr>> : public std::true_type
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
template <typename Flag, typename Expr>
|
template <typename Flag, typename Expr>
|
||||||
struct name_tag_of<max_t<Flag, Expr>>: public name_tag_of<alias::max_t>
|
struct name_tag_of<max_t<Flag, Expr>>: public name_tag_of<alias::max_t>
|
||||||
{
|
{
|
||||||
@ -87,7 +92,7 @@ namespace sqlpp
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
using check_max_arg =
|
using check_max_arg =
|
||||||
::sqlpp::enable_if_t<values_are_comparable<T, T>::value and not contains_aggregate_function_t<T>::value>;
|
::sqlpp::enable_if_t<values_are_comparable<T, T>::value and not contains_aggregate<T>::value>;
|
||||||
|
|
||||||
template <typename T, typename = check_max_arg<T>>
|
template <typename T, typename = check_max_arg<T>>
|
||||||
auto max(T t) -> max_t<noop, T>
|
auto max(T t) -> max_t<noop, T>
|
||||||
|
@ -62,6 +62,11 @@ namespace sqlpp
|
|||||||
Expr _expr;
|
Expr _expr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename Flag, typename Expr>
|
||||||
|
struct is_aggregate<min_t<Flag, Expr>> : public std::true_type
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
template <typename Flag, typename Expr>
|
template <typename Flag, typename Expr>
|
||||||
struct name_tag_of<min_t<Flag, Expr>>: public name_tag_of<alias::min_t>
|
struct name_tag_of<min_t<Flag, Expr>>: public name_tag_of<alias::min_t>
|
||||||
{
|
{
|
||||||
@ -87,7 +92,7 @@ namespace sqlpp
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
using check_min_arg =
|
using check_min_arg =
|
||||||
::sqlpp::enable_if_t<values_are_comparable<T, T>::value and not contains_aggregate_function_t<T>::value>;
|
::sqlpp::enable_if_t<values_are_comparable<T, T>::value and not contains_aggregate<T>::value>;
|
||||||
|
|
||||||
template <typename T, typename = check_min_arg<T>>
|
template <typename T, typename = check_min_arg<T>>
|
||||||
auto min(T t) -> min_t<noop, T>
|
auto min(T t) -> min_t<noop, T>
|
||||||
|
@ -36,8 +36,6 @@ namespace sqlpp
|
|||||||
struct over_t : public enable_as<over_t<Expr>>,
|
struct over_t : public enable_as<over_t<Expr>>,
|
||||||
public enable_comparison<over_t<Expr>>
|
public enable_comparison<over_t<Expr>>
|
||||||
{
|
{
|
||||||
using _traits = make_traits<integral, tag::is_expression>;
|
|
||||||
|
|
||||||
over_t(Expr expr)
|
over_t(Expr expr)
|
||||||
: _expr(expr)
|
: _expr(expr)
|
||||||
{
|
{
|
||||||
@ -52,18 +50,24 @@ namespace sqlpp
|
|||||||
Expr _expr;
|
Expr _expr;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Expr>
|
template <typename Expr>
|
||||||
struct nodes_of<over_t<Expr>>
|
struct is_aggregate<over_t<Expr>> : public is_aggregate<Expr>
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Expr>
|
||||||
|
struct name_tag_of<over_t<Expr>> : public name_tag_of<Expr> {};
|
||||||
|
|
||||||
|
template<typename Expr>
|
||||||
|
struct nodes_of<over_t<Expr>>: public nodes_of<Expr>
|
||||||
{
|
{
|
||||||
using type = sqlpp::detail::type_vector<Expr>;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Expr>
|
template<typename Expr>
|
||||||
struct value_type_of<over_t<Expr>>: public value_type_of<Expr> {};
|
struct value_type_of<over_t<Expr>>: public value_type_of<Expr> {};
|
||||||
|
|
||||||
#warning: should this be "is_aggregate_function"?
|
|
||||||
template<typename Expr>
|
template<typename Expr>
|
||||||
using check_over_args = ::sqlpp::enable_if_t<contains_aggregate_function_t<Expr>::value>;
|
using check_over_args = ::sqlpp::enable_if_t<is_aggregate<Expr>::value>;
|
||||||
|
|
||||||
template <typename Context, typename Expr>
|
template <typename Context, typename Expr>
|
||||||
auto to_sql_string(Context& context, const over_t<Expr>& t) -> std::string
|
auto to_sql_string(Context& context, const over_t<Expr>& t) -> std::string
|
||||||
@ -71,7 +75,7 @@ namespace sqlpp
|
|||||||
return operand_to_sql_string(context, t._expr) + " OVER()";
|
return operand_to_sql_string(context, t._expr) + " OVER()";
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Expr>
|
template <typename Expr, typename = check_over_args<Expr>>
|
||||||
auto over(Expr t) -> over_t<Expr>
|
auto over(Expr t) -> over_t<Expr>
|
||||||
{
|
{
|
||||||
return {std::move(t)};
|
return {std::move(t)};
|
||||||
|
@ -62,7 +62,11 @@ namespace sqlpp
|
|||||||
Expr _expr;
|
Expr _expr;
|
||||||
};
|
};
|
||||||
|
|
||||||
#warning: test that aggregate functions can be used as result columns directly, but can also be aliased
|
template <typename Flag, typename Expr>
|
||||||
|
struct is_aggregate<sum_t<Flag, Expr>> : public std::true_type
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
template <typename Flag, typename Expr>
|
template <typename Flag, typename Expr>
|
||||||
struct name_tag_of<sum_t<Flag, Expr>>: public name_tag_of<alias::sum_t>
|
struct name_tag_of<sum_t<Flag, Expr>>: public name_tag_of<alias::sum_t>
|
||||||
{
|
{
|
||||||
@ -89,7 +93,7 @@ namespace sqlpp
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
using check_sum_arg =
|
using check_sum_arg =
|
||||||
::sqlpp::enable_if_t<(is_numeric<T>::value or is_boolean<T>::value) and not contains_aggregate_function_t<T>::value>;
|
::sqlpp::enable_if_t<(is_numeric<T>::value or is_boolean<T>::value) and not contains_aggregate<T>::value>;
|
||||||
|
|
||||||
template <typename T, typename = check_sum_arg<T>>
|
template <typename T, typename = check_sum_arg<T>>
|
||||||
auto sum(T t) -> sum_t<noop, T>
|
auto sum(T t) -> sum_t<noop, T>
|
||||||
|
@ -110,7 +110,7 @@ namespace sqlpp
|
|||||||
|
|
||||||
SQLPP_PORTABLE_STATIC_ASSERT(assert_where_arg_is_boolean_expression_t,
|
SQLPP_PORTABLE_STATIC_ASSERT(assert_where_arg_is_boolean_expression_t,
|
||||||
"where() argument has to be an sqlpp boolean expression.");
|
"where() argument has to be an sqlpp boolean expression.");
|
||||||
SQLPP_PORTABLE_STATIC_ASSERT(assert_where_arg_contains_no_aggregate_functions_t,
|
SQLPP_PORTABLE_STATIC_ASSERT(assert_where_arg_contains_no_aggregate_t,
|
||||||
"at least one aggregate function used in where()");
|
"at least one aggregate function used in where()");
|
||||||
|
|
||||||
// workaround for msvc bugs https://connect.microsoft.com/VisualStudio/Feedback/Details/2086629 &
|
// workaround for msvc bugs https://connect.microsoft.com/VisualStudio/Feedback/Details/2086629 &
|
||||||
@ -119,15 +119,15 @@ namespace sqlpp
|
|||||||
// using check_where_t = static_combined_check_t<
|
// using check_where_t = static_combined_check_t<
|
||||||
// static_check_t<logic::all_t<is_boolean<Expressions>::value...>::value,
|
// static_check_t<logic::all_t<is_boolean<Expressions>::value...>::value,
|
||||||
// assert_where_arg_is_boolean_expression_t>,
|
// assert_where_arg_is_boolean_expression_t>,
|
||||||
// static_check_t<logic::all_t<(not contains_aggregate_function_t<Expressions>::value)...>::value,
|
// static_check_t<logic::all_t<(not contains_aggregate<Expressions>::value)...>::value,
|
||||||
// assert_where_arg_contains_no_aggregate_functions_t>>;
|
// assert_where_arg_contains_no_aggregate_t>>;
|
||||||
template <typename Expression>
|
template <typename Expression>
|
||||||
struct check_where
|
struct check_where
|
||||||
{
|
{
|
||||||
using type = static_combined_check_t<
|
using type = static_combined_check_t<
|
||||||
static_check_t<is_boolean<Expression>::value, assert_where_arg_is_boolean_expression_t>,
|
static_check_t<is_boolean<Expression>::value, assert_where_arg_is_boolean_expression_t>,
|
||||||
static_check_t<not contains_aggregate_function_t<Expression>::value,
|
static_check_t<not contains_aggregate<Expression>::value,
|
||||||
assert_where_arg_contains_no_aggregate_functions_t>>;
|
assert_where_arg_contains_no_aggregate_t>>;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Expression>
|
template <typename Expression>
|
||||||
|
@ -540,30 +540,6 @@ namespace sqlpp
|
|||||||
SQLPP_RECURSIVE_TRAIT_SET_GENERATOR(provided_outer_tables)
|
SQLPP_RECURSIVE_TRAIT_SET_GENERATOR(provided_outer_tables)
|
||||||
SQLPP_RECURSIVE_TRAIT_SET_GENERATOR(provided_aggregates)
|
SQLPP_RECURSIVE_TRAIT_SET_GENERATOR(provided_aggregates)
|
||||||
|
|
||||||
#define SQLPP_RECURSIVE_TRAIT_GENERATOR(trait) \
|
|
||||||
namespace detail \
|
|
||||||
{ \
|
|
||||||
template <typename T, typename Leaf = void> \
|
|
||||||
struct trait##_impl \
|
|
||||||
{ \
|
|
||||||
using type = typename trait##_impl<nodes_of_t<T>>::type; \
|
|
||||||
}; \
|
|
||||||
template <typename T> \
|
|
||||||
struct trait##_impl<T, ::sqlpp::void_t<typename T::_##trait>> \
|
|
||||||
{ \
|
|
||||||
using type = typename T::_##trait; \
|
|
||||||
}; \
|
|
||||||
template <typename... Nodes> \
|
|
||||||
struct trait##_impl<type_vector<Nodes...>, void> \
|
|
||||||
{ \
|
|
||||||
using type = logic::any_t<trait##_impl<Nodes>::type::value...>; \
|
|
||||||
}; \
|
|
||||||
} \
|
|
||||||
template <typename T> \
|
|
||||||
using trait##_t = typename detail::trait##_impl<T>::type;
|
|
||||||
|
|
||||||
SQLPP_RECURSIVE_TRAIT_GENERATOR(contains_aggregate_function)
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct lhs
|
struct lhs
|
||||||
{
|
{
|
||||||
@ -609,6 +585,13 @@ namespace sqlpp
|
|||||||
;
|
;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct is_aggregate : public std::false_type{};
|
||||||
|
|
||||||
|
#warning: Need to make this recursive! and then add tests!
|
||||||
|
template<typename T>
|
||||||
|
struct contains_aggregate : public std::false_type{};
|
||||||
|
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
template <typename KnownAggregates, typename T, typename Leaf = void>
|
template <typename KnownAggregates, typename T, typename Leaf = void>
|
||||||
|
@ -29,11 +29,11 @@ function(test_compile name)
|
|||||||
# conditionally bump to a higher C++ standard to test compatibility
|
# conditionally bump to a higher C++ standard to test compatibility
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
add_subdirectory(operator)
|
|
||||||
test_compile(aggregate_function)
|
|
||||||
test_compile(case_when)
|
test_compile(case_when)
|
||||||
test_compile(dynamic)
|
test_compile(dynamic)
|
||||||
test_compile(result_row)
|
test_compile(result_row)
|
||||||
test_compile(select_as)
|
test_compile(select_as)
|
||||||
test_compile(value)
|
test_compile(value)
|
||||||
|
|
||||||
|
add_subdirectory(aggregate_function)
|
||||||
|
add_subdirectory(operator)
|
||||||
|
37
tests/core/types/aggregate_function/CMakeLists.txt
Normal file
37
tests/core/types/aggregate_function/CMakeLists.txt
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
# 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(test_compile name)
|
||||||
|
set(target sqlpp11_core_types_aggregate_function_${name})
|
||||||
|
add_executable(${target} ${name}.cpp)
|
||||||
|
target_link_libraries(${target} PRIVATE sqlpp11::sqlpp11 sqlpp11_testing)
|
||||||
|
endfunction()
|
||||||
|
|
||||||
|
test_compile(avg)
|
||||||
|
test_compile(count)
|
||||||
|
test_compile(max)
|
||||||
|
test_compile(min)
|
||||||
|
test_compile(over)
|
||||||
|
test_compile(sum)
|
||||||
|
|
114
tests/core/types/aggregate_function/avg.cpp
Normal file
114
tests/core/types/aggregate_function/avg.cpp
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "MockDb.h"
|
||||||
|
#include "Sample.h"
|
||||||
|
#include <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
auto db = MockDb{};
|
||||||
|
|
||||||
|
template <typename T, typename V>
|
||||||
|
using is_same_type = std::is_same<sqlpp::value_type_of_t<T>, V>;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Value>
|
||||||
|
void test_avg(Value v)
|
||||||
|
{
|
||||||
|
auto v_not_null = sqlpp::value(v);
|
||||||
|
auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v));
|
||||||
|
|
||||||
|
using OptFloat = sqlpp::value_type_of_t<::sqlpp::optional<float>>;
|
||||||
|
|
||||||
|
// avg non-nullable can be null because there could be zero result rows.
|
||||||
|
static_assert(is_same_type<decltype(avg(v_not_null)), OptFloat>::value, "");
|
||||||
|
static_assert(is_same_type<decltype(avg(sqlpp::distinct, v_not_null)), OptFloat>::value, "");
|
||||||
|
|
||||||
|
// avg nullable
|
||||||
|
static_assert(is_same_type<decltype(avg(v_maybe_null)), OptFloat>::value, "");
|
||||||
|
static_assert(is_same_type<decltype(avg(sqlpp::distinct, v_maybe_null)), OptFloat>::value, "");
|
||||||
|
|
||||||
|
// avg enables the `as` member function.
|
||||||
|
static_assert(sqlpp::has_enabled_as<decltype(avg(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_as<decltype(avg(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
// avg has a name
|
||||||
|
static_assert(sqlpp::has_name<decltype(avg(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_name<decltype(avg(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
static_assert(sqlpp::name_tag_of_t<decltype(avg(v_not_null))>::name == sqlpp::string_view("avg"), "");
|
||||||
|
static_assert(sqlpp::name_tag_of_t<decltype(avg(sqlpp::distinct, v_not_null))>::name == sqlpp::string_view("avg"), "");
|
||||||
|
|
||||||
|
// avg enables OVER.
|
||||||
|
static_assert(sqlpp::has_enabled_over<decltype(avg(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_over<decltype(avg(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
// avg enables comparison member functions.
|
||||||
|
static_assert(sqlpp::has_enabled_comparison<decltype(avg(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_comparison<decltype(avg(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
// avg has its argument as nodes
|
||||||
|
using L = typename std::decay<decltype(v_not_null)>::type;
|
||||||
|
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(avg(v_not_null))>, sqlpp::detail::type_vector<L>>::value, "");
|
||||||
|
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(avg(sqlpp::distinct, v_not_null))>, sqlpp::detail::type_vector<L>>::value, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// boolean
|
||||||
|
test_avg(bool{true});
|
||||||
|
|
||||||
|
// integral
|
||||||
|
test_avg(int8_t{7});
|
||||||
|
test_avg(int16_t{7});
|
||||||
|
test_avg(int32_t{7});
|
||||||
|
test_avg(int64_t{7});
|
||||||
|
|
||||||
|
// unsigned integral
|
||||||
|
test_avg(uint8_t{7});
|
||||||
|
test_avg(uint16_t{7});
|
||||||
|
test_avg(uint32_t{7});
|
||||||
|
test_avg(uint64_t{7});
|
||||||
|
|
||||||
|
// floating point
|
||||||
|
test_avg(float{7.7});
|
||||||
|
test_avg(double{7.7});
|
||||||
|
|
||||||
|
#warning: Should there be avg date time duration?
|
||||||
|
#if 0
|
||||||
|
// date
|
||||||
|
test_avg(::sqlpp::chrono::day_point{});
|
||||||
|
|
||||||
|
// timestamp
|
||||||
|
test_avg(::sqlpp::chrono::microsecond_point{});
|
||||||
|
using minute_point = std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>;
|
||||||
|
test_avg(minute_point{});
|
||||||
|
|
||||||
|
// time_of_day
|
||||||
|
test_avg(std::chrono::microseconds{});
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
118
tests/core/types/aggregate_function/count.cpp
Normal file
118
tests/core/types/aggregate_function/count.cpp
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "MockDb.h"
|
||||||
|
#include "Sample.h"
|
||||||
|
#include <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
auto db = MockDb{};
|
||||||
|
|
||||||
|
template <typename T, typename V>
|
||||||
|
using is_same_type = std::is_same<sqlpp::value_type_of_t<T>, V>;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Value>
|
||||||
|
void test_count(Value v)
|
||||||
|
{
|
||||||
|
auto v_not_null = sqlpp::value(v);
|
||||||
|
auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v));
|
||||||
|
|
||||||
|
// count of non-nullable
|
||||||
|
static_assert(is_same_type<decltype(count(v_not_null)), sqlpp::integral>::value, "");
|
||||||
|
static_assert(is_same_type<decltype(count(sqlpp::distinct, v_not_null)), sqlpp::integral>::value, "");
|
||||||
|
|
||||||
|
// count of nullable
|
||||||
|
static_assert(is_same_type<decltype(count(v_maybe_null)), sqlpp::integral>::value, "");
|
||||||
|
static_assert(is_same_type<decltype(count(sqlpp::distinct, v_maybe_null)), sqlpp::integral>::value, "");
|
||||||
|
|
||||||
|
// count enables the `as` member function.
|
||||||
|
static_assert(sqlpp::has_enabled_as<decltype(count(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_as<decltype(sqlpp::distinct, count(v_not_null))>::value, "");
|
||||||
|
|
||||||
|
// count has a name
|
||||||
|
static_assert(sqlpp::has_name<decltype(count(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_name<decltype(count(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
static_assert(sqlpp::name_tag_of_t<decltype(count(v_not_null))>::name == sqlpp::string_view("count"), "");
|
||||||
|
static_assert(sqlpp::name_tag_of_t<decltype(sqlpp::distinct, count(v_not_null))>::name == sqlpp::string_view("count"), "");
|
||||||
|
|
||||||
|
// count enables comparison member functions.
|
||||||
|
static_assert(sqlpp::has_enabled_comparison<decltype(count(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_comparison<decltype(count(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
// count enables OVER.
|
||||||
|
static_assert(sqlpp::has_enabled_over<decltype(count(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_over<decltype(count(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
// count has its argument as nodes
|
||||||
|
using L = typename std::decay<decltype(v_not_null)>::type;
|
||||||
|
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(count(v_not_null))>, sqlpp::detail::type_vector<L>>::value, "");
|
||||||
|
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(count(sqlpp::distinct, v_not_null))>, sqlpp::detail::type_vector<L>>::value, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// boolean
|
||||||
|
test_count(bool{true});
|
||||||
|
|
||||||
|
// integral
|
||||||
|
test_count(int8_t{7});
|
||||||
|
test_count(int16_t{7});
|
||||||
|
test_count(int32_t{7});
|
||||||
|
test_count(int64_t{7});
|
||||||
|
|
||||||
|
// unsigned integral
|
||||||
|
test_count(uint8_t{7});
|
||||||
|
test_count(uint16_t{7});
|
||||||
|
test_count(uint32_t{7});
|
||||||
|
test_count(uint64_t{7});
|
||||||
|
|
||||||
|
// floating point
|
||||||
|
test_count(float{7.7});
|
||||||
|
test_count(double{7.7});
|
||||||
|
|
||||||
|
// text
|
||||||
|
test_count('7');
|
||||||
|
test_count("seven");
|
||||||
|
test_count(std::string("seven"));
|
||||||
|
test_count(::sqlpp::string_view("seven"));
|
||||||
|
|
||||||
|
// blob
|
||||||
|
test_count(std::vector<uint8_t>{});
|
||||||
|
|
||||||
|
// date
|
||||||
|
test_count(::sqlpp::chrono::day_point{});
|
||||||
|
|
||||||
|
// timestamp
|
||||||
|
test_count(::sqlpp::chrono::microsecond_point{});
|
||||||
|
using minute_point = std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>;
|
||||||
|
test_count(minute_point{});
|
||||||
|
|
||||||
|
// time_of_day
|
||||||
|
test_count(std::chrono::microseconds{});
|
||||||
|
}
|
||||||
|
|
120
tests/core/types/aggregate_function/max.cpp
Normal file
120
tests/core/types/aggregate_function/max.cpp
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "MockDb.h"
|
||||||
|
#include "Sample.h"
|
||||||
|
#include <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
auto db = MockDb{};
|
||||||
|
|
||||||
|
template <typename T, typename V>
|
||||||
|
using is_same_type = std::is_same<sqlpp::value_type_of_t<T>, V>;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Value>
|
||||||
|
void test_max(Value v)
|
||||||
|
{
|
||||||
|
auto v_not_null = sqlpp::value(v);
|
||||||
|
auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v));
|
||||||
|
|
||||||
|
using OptValueType = sqlpp::value_type_of_t<sqlpp::optional<Value>>;
|
||||||
|
|
||||||
|
// max of non-nullable can be null because there could be zero result rows.
|
||||||
|
static_assert(is_same_type<decltype(max(v_not_null)), OptValueType>::value, "");
|
||||||
|
static_assert(is_same_type<decltype(max(sqlpp::distinct, v_not_null)), OptValueType>::value, "");
|
||||||
|
|
||||||
|
// max of nullable
|
||||||
|
static_assert(is_same_type<decltype(max(v_maybe_null)), OptValueType>::value, "");
|
||||||
|
static_assert(is_same_type<decltype(max(sqlpp::distinct, v_maybe_null)), OptValueType>::value, "");
|
||||||
|
|
||||||
|
// max enables the `as` member function.
|
||||||
|
static_assert(sqlpp::has_enabled_as<decltype(max(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_as<decltype(sqlpp::distinct, max(v_not_null))>::value, "");
|
||||||
|
|
||||||
|
// max has a name
|
||||||
|
static_assert(sqlpp::has_name<decltype(max(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_name<decltype(max(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
static_assert(sqlpp::name_tag_of_t<decltype(max(v_not_null))>::name == sqlpp::string_view("max"), "");
|
||||||
|
static_assert(sqlpp::name_tag_of_t<decltype(sqlpp::distinct, max(v_not_null))>::name == sqlpp::string_view("max"), "");
|
||||||
|
|
||||||
|
// max enables comparison member functions.
|
||||||
|
static_assert(sqlpp::has_enabled_comparison<decltype(max(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_comparison<decltype(max(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
// max enables OVER.
|
||||||
|
static_assert(sqlpp::has_enabled_over<decltype(max(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_over<decltype(max(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
// max has its argument as nodes
|
||||||
|
using L = typename std::decay<decltype(v_not_null)>::type;
|
||||||
|
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(max(v_not_null))>, sqlpp::detail::type_vector<L>>::value, "");
|
||||||
|
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(max(sqlpp::distinct, v_not_null))>, sqlpp::detail::type_vector<L>>::value, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// boolean
|
||||||
|
test_max(bool{true});
|
||||||
|
|
||||||
|
// integral
|
||||||
|
test_max(int8_t{7});
|
||||||
|
test_max(int16_t{7});
|
||||||
|
test_max(int32_t{7});
|
||||||
|
test_max(int64_t{7});
|
||||||
|
|
||||||
|
// unsigned integral
|
||||||
|
test_max(uint8_t{7});
|
||||||
|
test_max(uint16_t{7});
|
||||||
|
test_max(uint32_t{7});
|
||||||
|
test_max(uint64_t{7});
|
||||||
|
|
||||||
|
// floating point
|
||||||
|
test_max(float{7.7});
|
||||||
|
test_max(double{7.7});
|
||||||
|
|
||||||
|
// text
|
||||||
|
test_max('7');
|
||||||
|
test_max("seven");
|
||||||
|
test_max(std::string("seven"));
|
||||||
|
test_max(::sqlpp::string_view("seven"));
|
||||||
|
|
||||||
|
// blob
|
||||||
|
test_max(std::vector<uint8_t>{});
|
||||||
|
|
||||||
|
// date
|
||||||
|
test_max(::sqlpp::chrono::day_point{});
|
||||||
|
|
||||||
|
// timestamp
|
||||||
|
test_max(::sqlpp::chrono::microsecond_point{});
|
||||||
|
using minute_point = std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>;
|
||||||
|
test_max(minute_point{});
|
||||||
|
|
||||||
|
// time_of_day
|
||||||
|
test_max(std::chrono::microseconds{});
|
||||||
|
}
|
||||||
|
|
120
tests/core/types/aggregate_function/min.cpp
Normal file
120
tests/core/types/aggregate_function/min.cpp
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "MockDb.h"
|
||||||
|
#include "Sample.h"
|
||||||
|
#include <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
auto db = MockDb{};
|
||||||
|
|
||||||
|
template <typename T, typename V>
|
||||||
|
using is_same_type = std::is_same<sqlpp::value_type_of_t<T>, V>;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Value>
|
||||||
|
void test_min(Value v)
|
||||||
|
{
|
||||||
|
auto v_not_null = sqlpp::value(v);
|
||||||
|
auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v));
|
||||||
|
|
||||||
|
using OptValueType = sqlpp::value_type_of_t<sqlpp::optional<Value>>;
|
||||||
|
|
||||||
|
// min of non-nullable can be null because there could be zero result rows.
|
||||||
|
static_assert(is_same_type<decltype(min(v_not_null)), OptValueType>::value, "");
|
||||||
|
static_assert(is_same_type<decltype(min(sqlpp::distinct, v_not_null)), OptValueType>::value, "");
|
||||||
|
|
||||||
|
// min of nullable
|
||||||
|
static_assert(is_same_type<decltype(min(v_maybe_null)), OptValueType>::value, "");
|
||||||
|
static_assert(is_same_type<decltype(min(sqlpp::distinct, v_maybe_null)), OptValueType>::value, "");
|
||||||
|
|
||||||
|
// min enables the `as` member function.
|
||||||
|
static_assert(sqlpp::has_enabled_as<decltype(min(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_as<decltype(sqlpp::distinct, min(v_not_null))>::value, "");
|
||||||
|
|
||||||
|
// min has a name
|
||||||
|
static_assert(sqlpp::has_name<decltype(min(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_name<decltype(min(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
static_assert(sqlpp::name_tag_of_t<decltype(min(v_not_null))>::name == sqlpp::string_view("min"), "");
|
||||||
|
static_assert(sqlpp::name_tag_of_t<decltype(sqlpp::distinct, min(v_not_null))>::name == sqlpp::string_view("min"), "");
|
||||||
|
|
||||||
|
// min enables comparison member functions.
|
||||||
|
static_assert(sqlpp::has_enabled_comparison<decltype(min(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_comparison<decltype(min(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
// min enables OVER.
|
||||||
|
static_assert(sqlpp::has_enabled_over<decltype(min(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_over<decltype(min(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
// min has its argument as nodes
|
||||||
|
using L = typename std::decay<decltype(v_not_null)>::type;
|
||||||
|
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(min(v_not_null))>, sqlpp::detail::type_vector<L>>::value, "");
|
||||||
|
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(min(sqlpp::distinct, v_not_null))>, sqlpp::detail::type_vector<L>>::value, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// boolean
|
||||||
|
test_min(bool{true});
|
||||||
|
|
||||||
|
// integral
|
||||||
|
test_min(int8_t{7});
|
||||||
|
test_min(int16_t{7});
|
||||||
|
test_min(int32_t{7});
|
||||||
|
test_min(int64_t{7});
|
||||||
|
|
||||||
|
// unsigned integral
|
||||||
|
test_min(uint8_t{7});
|
||||||
|
test_min(uint16_t{7});
|
||||||
|
test_min(uint32_t{7});
|
||||||
|
test_min(uint64_t{7});
|
||||||
|
|
||||||
|
// floating point
|
||||||
|
test_min(float{7.7});
|
||||||
|
test_min(double{7.7});
|
||||||
|
|
||||||
|
// text
|
||||||
|
test_min('7');
|
||||||
|
test_min("seven");
|
||||||
|
test_min(std::string("seven"));
|
||||||
|
test_min(::sqlpp::string_view("seven"));
|
||||||
|
|
||||||
|
// blob
|
||||||
|
test_min(std::vector<uint8_t>{});
|
||||||
|
|
||||||
|
// date
|
||||||
|
test_min(::sqlpp::chrono::day_point{});
|
||||||
|
|
||||||
|
// timestamp
|
||||||
|
test_min(::sqlpp::chrono::microsecond_point{});
|
||||||
|
using minute_point = std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>;
|
||||||
|
test_min(minute_point{});
|
||||||
|
|
||||||
|
// time_of_day
|
||||||
|
test_min(std::chrono::microseconds{});
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2016-2016, Roland Bock
|
* Copyright (c) 2024, Roland Bock
|
||||||
* 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,
|
||||||
@ -44,89 +44,87 @@ void test_aggregate_functions(Value v)
|
|||||||
using OptValueType = sqlpp::value_type_of_t<::sqlpp::optional<Value>>;
|
using OptValueType = sqlpp::value_type_of_t<::sqlpp::optional<Value>>;
|
||||||
|
|
||||||
// Aggregate of non-nullable
|
// Aggregate of non-nullable
|
||||||
static_assert(is_same_type<decltype(count(v_not_null)), sqlpp::integral>::value, "");
|
static_assert(is_same_type<decltype(count(v_not_null).over()), sqlpp::integral>::value, "");
|
||||||
static_assert(is_same_type<decltype(max(v_not_null)), OptValueType>::value, "");
|
static_assert(is_same_type<decltype(max(v_not_null).over()), OptValueType>::value, "");
|
||||||
static_assert(is_same_type<decltype(min(v_not_null)), OptValueType>::value, "");
|
static_assert(is_same_type<decltype(min(v_not_null).over()), OptValueType>::value, "");
|
||||||
|
|
||||||
// Aggregate of nullable
|
// Aggregate of nullable
|
||||||
static_assert(is_same_type<decltype(count(v_not_null)), sqlpp::integral>::value, "");
|
static_assert(is_same_type<decltype(count(v_not_null).over()), sqlpp::integral>::value, "");
|
||||||
static_assert(is_same_type<decltype(max(v_maybe_null)), OptValueType>::value, "");
|
static_assert(is_same_type<decltype(max(v_maybe_null).over()), OptValueType>::value, "");
|
||||||
static_assert(is_same_type<decltype(min(v_maybe_null)), OptValueType>::value, "");
|
static_assert(is_same_type<decltype(min(v_maybe_null).over()), OptValueType>::value, "");
|
||||||
|
|
||||||
// Aggregate functions enable the `as` member function.
|
// Aggregate functions enable the `as` member function.
|
||||||
static_assert(sqlpp::has_enabled_as<decltype(count(v_not_null))>::value, "");
|
static_assert(sqlpp::has_enabled_as<decltype(count(v_not_null).over())>::value, "");
|
||||||
static_assert(sqlpp::has_enabled_as<decltype(max(v_not_null))>::value, "");
|
static_assert(sqlpp::has_enabled_as<decltype(max(v_not_null).over())>::value, "");
|
||||||
static_assert(sqlpp::has_enabled_as<decltype(min(v_not_null))>::value, "");
|
static_assert(sqlpp::has_enabled_as<decltype(min(v_not_null).over())>::value, "");
|
||||||
|
|
||||||
// Aggregate functions have a name
|
// Aggregate functions have a name
|
||||||
static_assert(sqlpp::has_name<decltype(count(v_not_null))>::value, "");
|
static_assert(sqlpp::has_name<decltype(count(v_not_null).over())>::value, "");
|
||||||
static_assert(sqlpp::has_name<decltype(max(v_not_null))>::value, "");
|
static_assert(sqlpp::has_name<decltype(max(v_not_null).over())>::value, "");
|
||||||
static_assert(sqlpp::has_name<decltype(min(v_not_null))>::value, "");
|
static_assert(sqlpp::has_name<decltype(min(v_not_null).over())>::value, "");
|
||||||
|
|
||||||
static_assert(sqlpp::name_tag_of_t<decltype(count(v_not_null))>::name == sqlpp::string_view("count"), "");
|
static_assert(sqlpp::name_tag_of_t<decltype(count(v_not_null).over())>::name == sqlpp::string_view("count"), "");
|
||||||
static_assert(sqlpp::name_tag_of_t<decltype(max(v_not_null))>::name == sqlpp::string_view("max"), "");
|
static_assert(sqlpp::name_tag_of_t<decltype(max(v_not_null).over())>::name == sqlpp::string_view("max"), "");
|
||||||
static_assert(sqlpp::name_tag_of_t<decltype(min(v_not_null))>::name == sqlpp::string_view("min"), "");
|
static_assert(sqlpp::name_tag_of_t<decltype(min(v_not_null).over())>::name == sqlpp::string_view("min"), "");
|
||||||
|
|
||||||
// Aggregate functions enable comparison member functions.
|
// Aggregate functions enable comparison member functions.
|
||||||
static_assert(sqlpp::has_enabled_comparison<decltype(count(v_not_null))>::value, "");
|
static_assert(sqlpp::has_enabled_comparison<decltype(count(v_not_null).over())>::value, "");
|
||||||
static_assert(sqlpp::has_enabled_comparison<decltype(max(v_not_null))>::value, "");
|
static_assert(sqlpp::has_enabled_comparison<decltype(max(v_not_null).over())>::value, "");
|
||||||
static_assert(sqlpp::has_enabled_comparison<decltype(min(v_not_null))>::value, "");
|
static_assert(sqlpp::has_enabled_comparison<decltype(min(v_not_null).over())>::value, "");
|
||||||
|
|
||||||
// Aggregate functions enable OVER.
|
// Aggregate functions enable OVER.
|
||||||
static_assert(sqlpp::has_enabled_over<decltype(count(v_not_null))>::value, "");
|
static_assert(not sqlpp::has_enabled_over<decltype(count(v_not_null).over())>::value, "");
|
||||||
static_assert(sqlpp::has_enabled_over<decltype(max(v_not_null))>::value, "");
|
static_assert(not sqlpp::has_enabled_over<decltype(max(v_not_null).over())>::value, "");
|
||||||
static_assert(sqlpp::has_enabled_over<decltype(min(v_not_null))>::value, "");
|
static_assert(not sqlpp::has_enabled_over<decltype(min(v_not_null).over())>::value, "");
|
||||||
|
|
||||||
// Aggregate functions have their arguments as nodes
|
// Aggregate functions have their arguments as nodes
|
||||||
using L = typename std::decay<decltype(v_not_null)>::type;
|
using L = typename std::decay<decltype(v_not_null)>::type;
|
||||||
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(count(v_not_null))>, sqlpp::detail::type_vector<L>>::value, "");
|
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(count(v_not_null).over())>, sqlpp::detail::type_vector<L>>::value, "");
|
||||||
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(max(v_not_null))>, sqlpp::detail::type_vector<L>>::value, "");
|
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(max(v_not_null).over())>, sqlpp::detail::type_vector<L>>::value, "");
|
||||||
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(min(v_not_null))>, sqlpp::detail::type_vector<L>>::value, "");
|
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(min(v_not_null).over())>, sqlpp::detail::type_vector<L>>::value, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Value>
|
template <typename Value>
|
||||||
void test_numeric_aggregate_functions(Value v)
|
void test_numeric_aggregate_functions(Value v)
|
||||||
{
|
{
|
||||||
auto v_not_null = sqlpp::value(v);
|
auto v_not_null = sqlpp::value(v);
|
||||||
auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v));
|
auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v).over());
|
||||||
|
|
||||||
using ValueType = typename std::conditional<std::is_same<Value, bool>::value, int, Value>::type;
|
using ValueType = typename std::conditional<std::is_same<Value, bool>::value, int, Value>::type;
|
||||||
using OptValueType = sqlpp::value_type_of_t<::sqlpp::optional<ValueType>>;
|
using OptValueType = sqlpp::value_type_of_t<::sqlpp::optional<ValueType>>;
|
||||||
using OptFloat = sqlpp::value_type_of_t<::sqlpp::optional<float>>;
|
using OptFloat = sqlpp::value_type_of_t<::sqlpp::optional<float>>;
|
||||||
|
|
||||||
// Aggregate of non-nullable
|
// Aggregate of non-nullable
|
||||||
static_assert(is_same_type<decltype(sum(v_not_null)), OptValueType>::value, "");
|
static_assert(is_same_type<decltype(sum(v_not_null).over()), OptValueType>::value, "");
|
||||||
static_assert(is_same_type<decltype(avg(v_not_null)), OptFloat>::value, "");
|
static_assert(is_same_type<decltype(avg(v_not_null).over()), OptFloat>::value, "");
|
||||||
|
|
||||||
// Aggregate of nullable
|
// Aggregate of nullable
|
||||||
static_assert(is_same_type<decltype(sum(v_maybe_null)), OptValueType>::value, "");
|
static_assert(is_same_type<decltype(sum(v_maybe_null).over()), OptValueType>::value, "");
|
||||||
static_assert(is_same_type<decltype(avg(v_maybe_null)), OptFloat>::value, "");
|
static_assert(is_same_type<decltype(avg(v_maybe_null).over()), OptFloat>::value, "");
|
||||||
|
|
||||||
// Aggregate functions enable the `as` member function.
|
// Aggregate functions enable the `as` member function.
|
||||||
static_assert(sqlpp::has_enabled_as<decltype(sum(v_not_null))>::value, "");
|
static_assert(sqlpp::has_enabled_as<decltype(sum(v_not_null).over())>::value, "");
|
||||||
static_assert(sqlpp::has_enabled_as<decltype(avg(v_not_null))>::value, "");
|
static_assert(sqlpp::has_enabled_as<decltype(avg(v_not_null).over())>::value, "");
|
||||||
|
|
||||||
// Aggregate functions have a name
|
// Aggregate functions have a name
|
||||||
static_assert(sqlpp::has_name<decltype(sum(v_not_null))>::value, "");
|
static_assert(sqlpp::has_name<decltype(sum(v_not_null).over())>::value, "");
|
||||||
static_assert(sqlpp::has_name<decltype(avg(v_not_null))>::value, "");
|
static_assert(sqlpp::has_name<decltype(avg(v_not_null).over())>::value, "");
|
||||||
|
|
||||||
static_assert(sqlpp::name_tag_of_t<decltype(sum(v_not_null))>::name == sqlpp::string_view("sum"), "");
|
static_assert(sqlpp::name_tag_of_t<decltype(sum(v_not_null).over())>::name == sqlpp::string_view("sum"), "");
|
||||||
static_assert(sqlpp::name_tag_of_t<decltype(avg(v_not_null))>::name == sqlpp::string_view("avg"), "");
|
static_assert(sqlpp::name_tag_of_t<decltype(avg(v_not_null).over())>::name == sqlpp::string_view("avg"), "");
|
||||||
|
|
||||||
// Aggregate functions enable OVER.
|
// Aggregate functions enable OVER.
|
||||||
static_assert(sqlpp::has_enabled_over<decltype(sum(v_not_null))>::value, "");
|
static_assert(not sqlpp::has_enabled_over<decltype(sum(v_not_null).over())>::value, "");
|
||||||
static_assert(sqlpp::has_enabled_over<decltype(avg(v_not_null))>::value, "");
|
static_assert(not sqlpp::has_enabled_over<decltype(avg(v_not_null).over())>::value, "");
|
||||||
|
|
||||||
// Aggregate functions enable comparison member functions.
|
// Aggregate functions enable comparison member functions.
|
||||||
static_assert(sqlpp::has_enabled_comparison<decltype(sum(v_not_null))>::value, "");
|
static_assert(sqlpp::has_enabled_comparison<decltype(sum(v_not_null).over())>::value, "");
|
||||||
static_assert(sqlpp::has_enabled_comparison<decltype(avg(v_not_null))>::value, "");
|
static_assert(sqlpp::has_enabled_comparison<decltype(avg(v_not_null).over())>::value, "");
|
||||||
|
|
||||||
// Aggregate functions have their arguments as nodes
|
// Aggregate functions have their arguments as nodes
|
||||||
using L = typename std::decay<decltype(v_not_null)>::type;
|
using L = typename std::decay<decltype(v_not_null)>::type;
|
||||||
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(sum(v_not_null))>, sqlpp::detail::type_vector<L>>::value, "");
|
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(sum(v_not_null).over())>, sqlpp::detail::type_vector<L>>::value, "");
|
||||||
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(avg(v_not_null))>, sqlpp::detail::type_vector<L>>::value, "");
|
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(avg(v_not_null).over())>, sqlpp::detail::type_vector<L>>::value, "");
|
||||||
|
|
||||||
#warning: test enable_over
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
107
tests/core/types/aggregate_function/sum.cpp
Normal file
107
tests/core/types/aggregate_function/sum.cpp
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "MockDb.h"
|
||||||
|
#include "Sample.h"
|
||||||
|
#include <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
auto db = MockDb{};
|
||||||
|
|
||||||
|
template <typename T, typename V>
|
||||||
|
using is_same_type = std::is_same<sqlpp::value_type_of_t<T>, V>;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Value>
|
||||||
|
void test_sum(Value v)
|
||||||
|
{
|
||||||
|
auto v_not_null = sqlpp::value(v);
|
||||||
|
auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v));
|
||||||
|
|
||||||
|
using ValueType = typename std::conditional<std::is_same<Value, bool>::value, int, Value>::type;
|
||||||
|
using OptValueType = sqlpp::value_type_of_t<::sqlpp::optional<ValueType>>;
|
||||||
|
|
||||||
|
// sum non-nullable can be null because there could be zero result rows.
|
||||||
|
static_assert(is_same_type<decltype(sum(v_not_null)), OptValueType>::value, "");
|
||||||
|
static_assert(is_same_type<decltype(sum(sqlpp::distinct, v_not_null)), OptValueType>::value, "");
|
||||||
|
|
||||||
|
// sum nullable
|
||||||
|
static_assert(is_same_type<decltype(sum(v_maybe_null)), OptValueType>::value, "");
|
||||||
|
static_assert(is_same_type<decltype(sum(sqlpp::distinct, v_maybe_null)), OptValueType>::value, "");
|
||||||
|
|
||||||
|
// sum enables the `as` member function.
|
||||||
|
static_assert(sqlpp::has_enabled_as<decltype(sum(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_as<decltype(sum(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
// sum has a name
|
||||||
|
static_assert(sqlpp::has_name<decltype(sum(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_name<decltype(sum(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
static_assert(sqlpp::name_tag_of_t<decltype(sum(v_not_null))>::name == sqlpp::string_view("sum"), "");
|
||||||
|
static_assert(sqlpp::name_tag_of_t<decltype(sum(sqlpp::distinct, v_not_null))>::name == sqlpp::string_view("sum"), "");
|
||||||
|
|
||||||
|
// sum enables OVER.
|
||||||
|
static_assert(sqlpp::has_enabled_over<decltype(sum(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_over<decltype(sum(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
// sum enables comparison member functions.
|
||||||
|
static_assert(sqlpp::has_enabled_comparison<decltype(sum(v_not_null))>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_comparison<decltype(sum(sqlpp::distinct, v_not_null))>::value, "");
|
||||||
|
|
||||||
|
// sum has its argument as nodes
|
||||||
|
using L = typename std::decay<decltype(v_not_null)>::type;
|
||||||
|
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(sum(v_not_null))>, sqlpp::detail::type_vector<L>>::value, "");
|
||||||
|
static_assert(std::is_same<sqlpp::nodes_of_t<decltype(sum(sqlpp::distinct, v_not_null))>, sqlpp::detail::type_vector<L>>::value, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// boolean
|
||||||
|
test_sum(bool{true});
|
||||||
|
|
||||||
|
// integral
|
||||||
|
test_sum(int8_t{7});
|
||||||
|
test_sum(int16_t{7});
|
||||||
|
test_sum(int32_t{7});
|
||||||
|
test_sum(int64_t{7});
|
||||||
|
|
||||||
|
// unsigned integral
|
||||||
|
test_sum(uint8_t{7});
|
||||||
|
test_sum(uint16_t{7});
|
||||||
|
test_sum(uint32_t{7});
|
||||||
|
test_sum(uint64_t{7});
|
||||||
|
|
||||||
|
// floating point
|
||||||
|
test_sum(float{7.7});
|
||||||
|
test_sum(double{7.7});
|
||||||
|
|
||||||
|
#warning: Should there be sum duration?
|
||||||
|
#if 0
|
||||||
|
// time_of_day
|
||||||
|
test_sum(std::chrono::microseconds{});
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2016-2016, Roland Bock
|
* Copyright (c) 2024, Roland Bock
|
||||||
* 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,
|
||||||
|
Loading…
Reference in New Issue
Block a user