mirror of
https://github.com/rbock/sqlpp11.git
synced 2026-01-01 10:41:22 -06:00
Added several tests for static_assert (plus fixes for aggregates)
This commit is contained in:
@@ -60,6 +60,7 @@ namespace sqlpp
|
||||
{
|
||||
using _traits = make_traits<floating_point, tag::is_expression, tag::is_selectable>;
|
||||
using _nodes = detail::type_vector<Expr, aggregate_function>;
|
||||
using _is_aggregate_expression = std::true_type;
|
||||
|
||||
static_assert(is_noop<Flag>::value or std::is_same<distinct_t, Flag>::value,
|
||||
"avg() used with flag other than 'distinct'");
|
||||
|
||||
@@ -62,6 +62,7 @@ namespace sqlpp
|
||||
using _traits = make_traits<integral, tag::is_expression /*, tag::is_selectable*/>;
|
||||
|
||||
using _nodes = detail::type_vector<Expr, aggregate_function>;
|
||||
using _is_aggregate_expression = std::true_type;
|
||||
using _can_be_null = std::false_type;
|
||||
|
||||
static_assert(is_noop<Flag>::value or std::is_same<distinct_t, Flag>::value,
|
||||
|
||||
@@ -59,6 +59,7 @@ namespace sqlpp
|
||||
{
|
||||
using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_selectable>;
|
||||
using _nodes = detail::type_vector<Expr, aggregate_function>;
|
||||
using _is_aggregate_expression = std::true_type;
|
||||
|
||||
using _auto_alias_t = max_alias_t;
|
||||
|
||||
|
||||
@@ -59,6 +59,7 @@ namespace sqlpp
|
||||
{
|
||||
using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_selectable>;
|
||||
using _nodes = detail::type_vector<Expr, aggregate_function>;
|
||||
using _is_aggregate_expression = std::true_type;
|
||||
|
||||
using _auto_alias_t = min_alias_t;
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@ namespace sqlpp
|
||||
{
|
||||
using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_selectable>;
|
||||
using _nodes = detail::type_vector<Expr, aggregate_function>;
|
||||
using _is_aggregate_expression = std::true_type;
|
||||
|
||||
static_assert(is_noop<Flag>::value or std::is_same<distinct_t, Flag>::value,
|
||||
"sum() used with flag other than 'distinct'");
|
||||
|
||||
@@ -292,7 +292,6 @@ namespace sqlpp
|
||||
{
|
||||
using _nodes = detail::type_vector<>;
|
||||
using _contains_aggregate_function = std::true_type;
|
||||
using _is_aggregate_expression = std::true_type;
|
||||
};
|
||||
|
||||
template <typename NameProvider, typename Member>
|
||||
|
||||
@@ -44,6 +44,7 @@ namespace sqlpp
|
||||
{
|
||||
using _traits = make_traits<boolean, tag::is_expression, tag::is_wrapped_value>;
|
||||
using _nodes = detail::type_vector<>;
|
||||
using _is_aggregate_expression = std::true_type;
|
||||
|
||||
using _value_t = bool;
|
||||
|
||||
@@ -86,6 +87,7 @@ namespace sqlpp
|
||||
{
|
||||
using _traits = make_traits<integral, tag::is_expression, tag::is_wrapped_value>;
|
||||
using _nodes = detail::type_vector<>;
|
||||
using _is_aggregate_expression = std::true_type;
|
||||
|
||||
using _value_t = int64_t;
|
||||
|
||||
@@ -128,6 +130,7 @@ namespace sqlpp
|
||||
{
|
||||
using _traits = make_traits<floating_point, tag::is_expression, tag::is_wrapped_value>;
|
||||
using _nodes = detail::type_vector<>;
|
||||
using _is_aggregate_expression = std::true_type;
|
||||
|
||||
using _value_t = double;
|
||||
|
||||
@@ -170,6 +173,7 @@ namespace sqlpp
|
||||
{
|
||||
using _traits = make_traits<text, tag::is_expression, tag::is_wrapped_value>;
|
||||
using _nodes = detail::type_vector<>;
|
||||
using _is_aggregate_expression = std::true_type;
|
||||
|
||||
using _value_t = std::string;
|
||||
|
||||
|
||||
@@ -31,15 +31,80 @@
|
||||
#include <sqlpp11/functions.h>
|
||||
#include <sqlpp11/connection.h>
|
||||
|
||||
namespace sqlpp
|
||||
{
|
||||
namespace test
|
||||
{
|
||||
template <typename T>
|
||||
void print_type_on_error(std::true_type, const T&)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void print_type_on_error(std::false_type, const T& t)
|
||||
{
|
||||
t._print_me_;
|
||||
}
|
||||
|
||||
template <typename Assert, typename Expression>
|
||||
void run_check(const Expression&)
|
||||
{
|
||||
using Context = MockDb::_serializer_context_t;
|
||||
using CheckResult = std::is_same<sqlpp::run_check_t<Context, Expression>, Assert>;
|
||||
static_assert(CheckResult::value, "Unexpected run_check result");
|
||||
print_type_on_error(CheckResult{}, sqlpp::run_check_t<Context, Expression>{});
|
||||
}
|
||||
|
||||
template <typename Expression>
|
||||
void run_check(const Expression&)
|
||||
{
|
||||
using Context = MockDb::_serializer_context_t;
|
||||
using CheckResult = std::is_same<sqlpp::run_check_t<Context, Expression>, consistent_t>;
|
||||
static_assert(CheckResult::value, "Unexpected run_check result");
|
||||
print_type_on_error(CheckResult{}, sqlpp::run_check_t<Context, Expression>{});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SQLPP_ALIAS_PROVIDER(whatever);
|
||||
|
||||
int Aggregates(int, char**)
|
||||
{
|
||||
MockDb db = {};
|
||||
MockDb::_serializer_context_t printer;
|
||||
using sqlpp::test::run_check;
|
||||
|
||||
// test::TabFoo f;
|
||||
test::TabBar t;
|
||||
|
||||
db(select(t.alpha).from(t).where(true).group_by(t.alpha));
|
||||
// If there is no group_by, we can select whatever we want
|
||||
run_check(select(all_of(t)).from(t).where(true));
|
||||
run_check(select(t.alpha).from(t).where(true));
|
||||
run_check(select(count(t.alpha)).from(t).where(true));
|
||||
|
||||
// If there is a static group_by, selected columns must be made of group_by expressions, or aggregate expression (e.g.
|
||||
// count(t.id)) or values to be valid
|
||||
run_check(select(t.alpha).from(t).where(true).group_by(t.alpha));
|
||||
run_check(select((t.alpha + 42).as(whatever)).from(t).where(true).group_by(t.alpha));
|
||||
run_check(select((t.alpha + 42).as(whatever)).from(t).where(true).group_by(t.alpha, t.alpha + t.delta * 17));
|
||||
run_check(
|
||||
select((t.alpha + t.delta * 17).as(whatever)).from(t).where(true).group_by(t.alpha, t.alpha + t.delta * 17));
|
||||
run_check(select((t.beta + "fortytwo").as(whatever)).from(t).where(true).group_by(t.beta));
|
||||
|
||||
run_check(select(avg(t.alpha)).from(t).where(true).group_by(t.beta));
|
||||
run_check(select(count(t.alpha)).from(t).where(true).group_by(t.beta));
|
||||
run_check(select(max(t.alpha)).from(t).where(true).group_by(t.beta));
|
||||
run_check(select(min(t.alpha)).from(t).where(true).group_by(t.beta));
|
||||
run_check(select(sum(t.alpha)).from(t).where(true).group_by(t.beta));
|
||||
|
||||
run_check(select((t.alpha + count(t.delta)).as(whatever)).from(t).where(true).group_by(t.alpha));
|
||||
|
||||
run_check(select(sqlpp::value(1).as(whatever)).from(t).where(true).group_by(t.alpha));
|
||||
run_check(select(sqlpp::value("whatever").as(whatever)).from(t).where(true).group_by(t.alpha));
|
||||
|
||||
// Otherwise, they are invalid
|
||||
run_check<sqlpp::assert_aggregates_t>(select(t.beta).from(t).where(true).group_by(t.alpha));
|
||||
run_check<sqlpp::assert_aggregates_t>(select((t.alpha + t.delta).as(whatever)).from(t).where(true).group_by(t.alpha));
|
||||
run_check<sqlpp::assert_aggregates_t>(
|
||||
select((t.alpha + t.delta).as(whatever)).from(t).where(true).group_by(t.alpha, t.alpha + t.delta * 17));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user