mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-16 04:47:18 +08:00
First aggregate tests
This commit is contained in:
parent
bd54159446
commit
a2f91c4a7c
@ -63,7 +63,12 @@ namespace sqlpp
|
||||
};
|
||||
|
||||
template <typename Flag, typename Expr>
|
||||
struct contains_aggregate<avg_t<Flag, Expr>> : public std::true_type
|
||||
struct contains_aggregate_function<avg_t<Flag, Expr>> : public std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
template <typename Flag, typename Expr>
|
||||
struct contains_non_aggregate<avg_t<Flag, Expr>> : public std::false_type
|
||||
{
|
||||
};
|
||||
|
||||
@ -92,7 +97,7 @@ namespace sqlpp
|
||||
|
||||
template <typename T>
|
||||
using check_avg_arg =
|
||||
::sqlpp::enable_if_t<(is_numeric<T>::value or is_boolean<T>::value) and not contains_aggregate<T>::value>;
|
||||
::sqlpp::enable_if_t<(is_numeric<T>::value or is_boolean<T>::value) and not contains_aggregate_function<T>::value>;
|
||||
|
||||
template <typename T, typename = check_avg_arg<T>>
|
||||
auto avg(T t) -> avg_t<noop, T>
|
||||
|
@ -63,7 +63,12 @@ namespace sqlpp
|
||||
};
|
||||
|
||||
template <typename Flag, typename Expr>
|
||||
struct contains_aggregate<count_t<Flag, Expr>> : public std::true_type
|
||||
struct contains_aggregate_function<count_t<Flag, Expr>> : public std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
template <typename Flag, typename Expr>
|
||||
struct contains_non_aggregate<count_t<Flag, Expr>> : public std::false_type
|
||||
{
|
||||
};
|
||||
|
||||
@ -92,7 +97,7 @@ namespace sqlpp
|
||||
|
||||
template <typename T>
|
||||
using check_count_arg =
|
||||
::sqlpp::enable_if_t<values_are_comparable<T, T>::value and not contains_aggregate<T>::value>;
|
||||
::sqlpp::enable_if_t<values_are_comparable<T, T>::value and not contains_aggregate_function<T>::value>;
|
||||
|
||||
template <typename T, typename = check_count_arg<T>>
|
||||
auto count(T t) -> count_t<noop, T>
|
||||
|
@ -62,7 +62,12 @@ namespace sqlpp
|
||||
};
|
||||
|
||||
template <typename Flag, typename Expr>
|
||||
struct contains_aggregate<max_t<Flag, Expr>> : public std::true_type
|
||||
struct contains_aggregate_function<max_t<Flag, Expr>> : public std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
template <typename Flag, typename Expr>
|
||||
struct contains_non_aggregate<max_t<Flag, Expr>> : public std::false_type
|
||||
{
|
||||
};
|
||||
|
||||
@ -91,7 +96,7 @@ namespace sqlpp
|
||||
|
||||
template <typename T>
|
||||
using check_max_arg =
|
||||
::sqlpp::enable_if_t<values_are_comparable<T, T>::value and not contains_aggregate<T>::value>;
|
||||
::sqlpp::enable_if_t<values_are_comparable<T, T>::value and not contains_aggregate_function<T>::value>;
|
||||
|
||||
template <typename T, typename = check_max_arg<T>>
|
||||
auto max(T t) -> max_t<noop, T>
|
||||
|
@ -62,7 +62,12 @@ namespace sqlpp
|
||||
};
|
||||
|
||||
template <typename Flag, typename Expr>
|
||||
struct contains_aggregate<min_t<Flag, Expr>> : public std::true_type
|
||||
struct contains_aggregate_function<min_t<Flag, Expr>> : public std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
template <typename Flag, typename Expr>
|
||||
struct contains_non_aggregate<min_t<Flag, Expr>> : public std::false_type
|
||||
{
|
||||
};
|
||||
|
||||
@ -91,7 +96,7 @@ namespace sqlpp
|
||||
|
||||
template <typename T>
|
||||
using check_min_arg =
|
||||
::sqlpp::enable_if_t<values_are_comparable<T, T>::value and not contains_aggregate<T>::value>;
|
||||
::sqlpp::enable_if_t<values_are_comparable<T, T>::value and not contains_aggregate_function<T>::value>;
|
||||
|
||||
template <typename T, typename = check_min_arg<T>>
|
||||
auto min(T t) -> min_t<noop, T>
|
||||
|
@ -62,7 +62,7 @@ namespace sqlpp
|
||||
struct value_type_of<over_t<Expr>>: public value_type_of<Expr> {};
|
||||
|
||||
template<typename Expr>
|
||||
using check_over_args = ::sqlpp::enable_if_t<contains_aggregate<Expr>::value>;
|
||||
using check_over_args = ::sqlpp::enable_if_t<contains_aggregate_function<Expr>::value>;
|
||||
|
||||
template <typename Context, typename Expr>
|
||||
auto to_sql_string(Context& context, const over_t<Expr>& t) -> std::string
|
||||
|
@ -62,7 +62,12 @@ namespace sqlpp
|
||||
};
|
||||
|
||||
template <typename Flag, typename Expr>
|
||||
struct contains_aggregate<sum_t<Flag, Expr>> : public std::true_type
|
||||
struct contains_aggregate_function<sum_t<Flag, Expr>> : public std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
template <typename Flag, typename Expr>
|
||||
struct contains_non_aggregate<sum_t<Flag, Expr>> : public std::false_type
|
||||
{
|
||||
};
|
||||
|
||||
@ -92,7 +97,7 @@ namespace sqlpp
|
||||
|
||||
template <typename T>
|
||||
using check_sum_arg =
|
||||
::sqlpp::enable_if_t<(is_numeric<T>::value or is_boolean<T>::value) and not contains_aggregate<T>::value>;
|
||||
::sqlpp::enable_if_t<(is_numeric<T>::value or is_boolean<T>::value) and not contains_aggregate_function<T>::value>;
|
||||
|
||||
template <typename T, typename = check_sum_arg<T>>
|
||||
auto sum(T t) -> sum_t<noop, T>
|
||||
|
@ -85,6 +85,9 @@ namespace sqlpp
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Table, typename ColumnSpec>
|
||||
struct contains_non_aggregate<column_t<Table, ColumnSpec>> : public std::true_type {};
|
||||
|
||||
template<typename Table, typename ColumnSpec>
|
||||
struct value_type_of<column_t<Table, ColumnSpec>>
|
||||
{
|
||||
@ -99,12 +102,6 @@ namespace sqlpp
|
||||
{
|
||||
};
|
||||
|
||||
#warning: Do we need this?
|
||||
template<typename Table, typename ColumnSpec>
|
||||
struct has_name<column_t<Table, ColumnSpec>> : std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
template<typename Table, typename ColumnSpec>
|
||||
struct required_tables_of<column_t<Table, ColumnSpec>>
|
||||
{
|
||||
|
@ -119,14 +119,14 @@ namespace sqlpp
|
||||
// using check_where_t = static_combined_check_t<
|
||||
// static_check_t<logic::all_t<is_boolean<Expressions>::value...>::value,
|
||||
// assert_where_arg_is_boolean_expression_t>,
|
||||
// static_check_t<logic::all_t<(not contains_aggregate<Expressions>::value)...>::value,
|
||||
// static_check_t<logic::all_t<(not contains_aggregate_function<Expressions>::value)...>::value,
|
||||
// assert_where_arg_contains_no_aggregate_t>>;
|
||||
template <typename Expression>
|
||||
struct check_where
|
||||
{
|
||||
using type = static_combined_check_t<
|
||||
static_check_t<is_boolean<Expression>::value, assert_where_arg_is_boolean_expression_t>,
|
||||
static_check_t<not contains_aggregate<Expression>::value,
|
||||
static_check_t<not contains_aggregate_function<Expression>::value,
|
||||
assert_where_arg_contains_no_aggregate_t>>;
|
||||
};
|
||||
|
||||
|
@ -32,14 +32,45 @@
|
||||
|
||||
namespace sqlpp
|
||||
{
|
||||
// Finds calls to aggregate functions (avg, count, max, min, sum).
|
||||
template <typename T>
|
||||
struct contains_aggregate : public std::integral_constant<bool, contains_aggregate<nodes_of_t<T>>::value>
|
||||
struct contains_aggregate_function : public std::integral_constant<bool, contains_aggregate_function<nodes_of_t<T>>::value>
|
||||
{
|
||||
};
|
||||
|
||||
template <typename... T>
|
||||
struct contains_aggregate<detail::type_vector<T...>>
|
||||
: public std::integral_constant<bool, logic::any_t<contains_aggregate<T>::value...>::value>
|
||||
struct contains_aggregate_function<detail::type_vector<T...>>
|
||||
: public std::integral_constant<bool, logic::any_t<contains_aggregate_function<T>::value...>::value>
|
||||
{
|
||||
};
|
||||
|
||||
// Finds group_by expressions.
|
||||
// @GroupByExpressions: type_vector
|
||||
template <typename T, typename GroupByExpressions>
|
||||
struct contains_aggregate_expressions
|
||||
: public std::integral_constant<bool, contains_aggregate_expressions<nodes_of_t<T>, GroupByExpressions>::value>
|
||||
{
|
||||
};
|
||||
|
||||
template <typename... T, typename GroupByExpressions>
|
||||
struct contains_aggregate_expressions<detail::type_vector<T...>, GroupByExpressions>
|
||||
: public std::integral_constant<
|
||||
bool,
|
||||
logic::any_t<(GroupByExpressions::template contains<T>::value or
|
||||
contains_aggregate_expressions<T, GroupByExpressions>::value)...>::value>
|
||||
{
|
||||
};
|
||||
|
||||
// Finds columns.
|
||||
// Note that explicit values like `value(7)` are compatible with both aggregate and non-aggregate.
|
||||
template <typename T>
|
||||
struct contains_non_aggregate : public std::integral_constant<bool, contains_non_aggregate<nodes_of_t<T>>::value>
|
||||
{
|
||||
};
|
||||
|
||||
template <typename... T>
|
||||
struct contains_non_aggregate<detail::type_vector<T...>>
|
||||
: public std::integral_constant<bool, logic::any_t<contains_non_aggregate<T>::value...>::value>
|
||||
{
|
||||
};
|
||||
|
||||
|
@ -23,63 +23,46 @@
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "MockDb.h"
|
||||
#include "Sample.h"
|
||||
#include <sqlpp11/sqlpp11.h>
|
||||
|
||||
namespace
|
||||
void test_contains_aggregate_function()
|
||||
{
|
||||
auto db = MockDb{};
|
||||
auto v_not_null = sqlpp::value(17);
|
||||
|
||||
#warning: Need to test contains_aggregate_function
|
||||
|
||||
template <typename T, typename V>
|
||||
using is_same_type = std::is_same<sqlpp::value_type_of_t<T>, V>;
|
||||
}
|
||||
|
||||
template <typename Value>
|
||||
void test_aggregates(Value v)
|
||||
void test_contains_aggregate_expression()
|
||||
{
|
||||
auto v_not_null = sqlpp::value(v);
|
||||
auto v_maybe_null = sqlpp::value(::sqlpp::make_optional(v));
|
||||
auto v_not_null = sqlpp::value(17);
|
||||
|
||||
using OptFloat = sqlpp::value_type_of_t<::sqlpp::optional<float>>;
|
||||
#warning: Need to test contains_aggregate_expression
|
||||
|
||||
#warning: Need to test contains_aggregate
|
||||
}
|
||||
|
||||
void test_contains_non_aggregate()
|
||||
{
|
||||
auto v = sqlpp::value(17);
|
||||
auto col = test::TabFoo{}.id;
|
||||
|
||||
static_assert(sqlpp::contains_non_aggregate<decltype(col)>::value, "");
|
||||
static_assert(sqlpp::contains_non_aggregate<decltype(col + v)>::value, "");
|
||||
static_assert(not sqlpp::contains_non_aggregate<decltype(avg(col) + v)>::value, "");
|
||||
static_assert(not sqlpp::contains_non_aggregate<decltype(count(col) + v)>::value, "");
|
||||
static_assert(not sqlpp::contains_non_aggregate<decltype(min(col) + v)>::value, "");
|
||||
static_assert(not sqlpp::contains_non_aggregate<decltype(max(col) + v)>::value, "");
|
||||
static_assert(not sqlpp::contains_non_aggregate<decltype(sum(col) + v)>::value, "");
|
||||
#warning: lets test some functions, too
|
||||
#warning: lets test some clauses, too
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
void test_contains_aggregate_function();
|
||||
void test_contains_aggregate_expression();
|
||||
void test_contains_non_aggregate();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user