0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 04:47:18 +08:00

Added tests for dynamic joins

This commit is contained in:
rbock 2016-03-05 22:27:11 +01:00
parent 225d387967
commit 20b74206ae
3 changed files with 188 additions and 56 deletions

View File

@ -35,7 +35,7 @@ namespace sqlpp
{
SQLPP_PORTABLE_STATIC_ASSERT(assert_cross_join_lhs_table_t, "lhs argument of join() has to be a table or a join");
SQLPP_PORTABLE_STATIC_ASSERT(assert_cross_join_rhs_table_t, "rhs argument of join() has to be a table");
SQLPP_PORTABLE_STATIC_ASSERT(assert_cross_join_rhs_no_join_t, "rhs argument of join() must not be a table");
SQLPP_PORTABLE_STATIC_ASSERT(assert_cross_join_rhs_no_join_t, "rhs argument of join() must not be a join");
SQLPP_PORTABLE_STATIC_ASSERT(assert_cross_join_unique_names_t, "joined table names have to be unique");
template <typename Lhs, typename Rhs>
@ -102,7 +102,7 @@ namespace sqlpp
static_assert(required_tables_of<cross_join_t>::size::value == 0, "joined tables must not depend on other tables");
template <typename Expr>
auto on(Expr expr) -> typename std::conditional<check_join_on_t<cross_join_t, Expr>::value,
auto on(Expr expr) const -> typename std::conditional<check_join_on_t<cross_join_t, Expr>::value,
join_t<cross_join_t, on_t<Expr>>,
bad_statement>::type
{

View File

@ -32,6 +32,20 @@
namespace sqlpp
{
SQLPP_PORTABLE_STATIC_ASSERT(assert_dynamic_cross_join_table_t, "argument of dynamic_join() has to be a table");
SQLPP_PORTABLE_STATIC_ASSERT(assert_dynamic_cross_join_no_join_t, "argument of dynamic_join() must not be a table");
template <typename Table>
struct check_dynamic_cross_join
{
using type =
static_combined_check_t<static_check_t<is_table_t<Table>::value, assert_dynamic_cross_join_table_t>,
static_check_t<not is_join_t<Table>::value, assert_dynamic_cross_join_no_join_t>>;
};
template <typename Table>
using check_dynamic_cross_join_t = typename check_dynamic_cross_join<Table>::type;
SQLPP_PORTABLE_STATIC_ASSERT(assert_dynamic_join_consist_of_cross_join_and_on_t,
"dynamic join has to consist of a dynamic cross_join and a join condition");
SQLPP_PORTABLE_STATIC_ASSERT(assert_dynamic_join_no_table_dependencies_t,
@ -75,7 +89,7 @@ namespace sqlpp
"joined tables must not depend on other tables");
template <typename Expr>
auto on(Expr expr) -> typename std::conditional<check_dynamic_join_on_t<dynamic_cross_join_t, Expr>::value,
auto on(Expr expr) const -> typename std::conditional<check_dynamic_join_on_t<dynamic_cross_join_t, Expr>::value,
dynamic_join_t<dynamic_cross_join_t, on_t<Expr>>,
bad_statement>::type
{
@ -106,33 +120,43 @@ namespace sqlpp
}
};
template <typename JoinType, typename Table>
using make_dynamic_cross_join_t = typename std::conditional<check_dynamic_cross_join_t<Table>::value,
dynamic_cross_join_t<JoinType, Table>,
bad_statement>::type;
template <typename Table>
dynamic_cross_join_t<inner_join_t, Table> dynamic_join(Table table)
auto dynamic_join(Table table) -> make_dynamic_cross_join_t<inner_join_t, Table>
{
check_dynamic_cross_join_t<Table>::_();
return {table};
}
template <typename Table>
dynamic_cross_join_t<inner_join_t, Table> dynamic_inner_join(Table table)
auto dynamic_inner_join(Table table) -> make_dynamic_cross_join_t<inner_join_t, Table>
{
check_dynamic_cross_join_t<Table>::_();
return {table};
}
template <typename Table>
dynamic_cross_join_t<outer_join_t, Table> outer_join(Table table)
auto dynamic_left_outer_join(Table table) -> make_dynamic_cross_join_t<left_outer_join_t, Table>
{
check_dynamic_cross_join_t<Table>::_();
return {table};
}
template <typename Table>
dynamic_cross_join_t<left_outer_join_t, Table> left_outer_join(Table table)
auto dynamic_right_outer_join(Table table) -> make_dynamic_cross_join_t<right_outer_join_t, Table>
{
check_dynamic_cross_join_t<Table>::_();
return {table};
}
template <typename Table>
dynamic_cross_join_t<right_outer_join_t, Table> right_outer_join(Table table)
auto dynamic_outer_join(Table table) -> make_dynamic_cross_join_t<outer_join_t, Table>
{
check_dynamic_cross_join_t<Table>::_();
return {table};
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2016, Roland Bock
* Copyright (c) 2016-2016, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
@ -52,13 +52,16 @@ namespace
print_type_on_error<CheckResult>(ExpectedCheckResult{});
static_assert(ExpectedCheckResult::value, "Unexpected check result");
using JoinType = decltype(join(lhs, rhs));
using InnerJoinType = decltype(inner_join(lhs, rhs));
using LeftOuterJoinType = decltype(left_outer_join(lhs, rhs));
using RightOuterJoinType = decltype(right_outer_join(lhs, rhs));
using OuterJoinType = decltype(outer_join(lhs, rhs));
using ExpectedReturnType =
sqlpp::logic::all_t<Assert::value xor (std::is_same<JoinType, sqlpp::bad_statement>::value and
using JoinType = decltype(sqlpp::join(lhs, rhs));
using InnerJoinType = decltype(sqlpp::inner_join(lhs, rhs));
using LeftOuterJoinType = decltype(sqlpp::left_outer_join(lhs, rhs));
using RightOuterJoinType = decltype(sqlpp::right_outer_join(lhs, rhs));
using OuterJoinType = decltype(sqlpp::outer_join(lhs, rhs));
using ExpectedReturnType = sqlpp::logic::all_t<
(Assert::value and sqlpp::is_cross_join_t<JoinType>::value and sqlpp::is_cross_join_t<InnerJoinType>::value and
sqlpp::is_cross_join_t<LeftOuterJoinType>::value and sqlpp::is_cross_join_t<RightOuterJoinType>::value and
sqlpp::is_cross_join_t<OuterJoinType>::value) xor
(std::is_same<JoinType, sqlpp::bad_statement>::value and
std::is_same<InnerJoinType, sqlpp::bad_statement>::value and
std::is_same<LeftOuterJoinType, sqlpp::bad_statement>::value and
std::is_same<RightOuterJoinType, sqlpp::bad_statement>::value and
@ -71,38 +74,41 @@ namespace
static_assert(ExpectedReturnType::value, "Unexpected return type");
}
/*
template <typename Assert, typename Expression>
void join_dynamic_check(const Expression& expression)
template <typename Assert, typename Lhs, typename Rhs>
void on_static_check(const Lhs& lhs, const Rhs& rhs)
{
static auto db = MockDb{};
using CheckResult = sqlpp::check_join_dynamic_t<decltype(db), Expression>;
using CheckResult = sqlpp::check_join_on_t<Lhs, Rhs>;
using ExpectedCheckResult = std::is_same<CheckResult, Assert>;
print_type_on_error<CheckResult>(ExpectedCheckResult{});
static_assert(ExpectedCheckResult::value, "Unexpected check result");
using ReturnType = decltype(dynamic_select(db, t.alpha).dynamic_join(expression));
using ExpectedReturnType =
sqlpp::logic::all_t<Assert::value xor std::is_same<ReturnType, sqlpp::bad_statement>::value>;
print_type_on_error<ReturnType>(ExpectedReturnType{});
using ResultType = decltype(lhs.on(rhs));
using ExpectedReturnType = sqlpp::logic::all_t<(Assert::value and sqlpp::is_join_t<ResultType>::value) xor
std::is_same<ResultType, sqlpp::bad_statement>::value>;
print_type_on_error<ResultType>(ExpectedReturnType{});
static_assert(ExpectedReturnType::value, "Unexpected return type");
}
*/
void static_join()
{
// Prepare a few table aliases for tests
const auto ta = t.as(sqlpp::alias::a);
const auto tb = t.as(sqlpp::alias::b);
const auto fa = f.as(sqlpp::alias::a);
const auto fb = f.as(sqlpp::alias::b);
// OK: Join two different tables
join_static_check<sqlpp::consistent_t>(t, f);
join_static_check<sqlpp::consistent_t>(t, f.as(sqlpp::alias::a));
join_static_check<sqlpp::consistent_t>(t.as(sqlpp::alias::a), f.as(sqlpp::alias::b));
join_static_check<sqlpp::consistent_t>(t, fa);
join_static_check<sqlpp::consistent_t>(ta, fb);
// OK: Self join
join_static_check<sqlpp::consistent_t>(t.as(sqlpp::alias::a), t.as(sqlpp::alias::b));
join_static_check<sqlpp::consistent_t>(t, t.as(sqlpp::alias::b));
join_static_check<sqlpp::consistent_t>(t.as(sqlpp::alias::a), t);
join_static_check<sqlpp::consistent_t>(ta, tb);
join_static_check<sqlpp::consistent_t>(t, tb);
join_static_check<sqlpp::consistent_t>(ta, t);
// Prepare a join for tests:
const auto j = join(t.as(sqlpp::alias::a), t.as(sqlpp::alias::b)).unconditionally();
const auto j = join(ta, tb).unconditionally();
// OK: Add a third table
join_static_check<sqlpp::consistent_t>(j, f);
@ -132,31 +138,133 @@ namespace
join_static_check<sqlpp::assert_cross_join_unique_names_t>(f, f);
join_static_check<sqlpp::assert_cross_join_unique_names_t>(t.as(f), f);
join_static_check<sqlpp::assert_cross_join_unique_names_t>(t, f.as(t));
join_static_check<sqlpp::assert_cross_join_unique_names_t>(t.as(sqlpp::alias::a), f.as(sqlpp::alias::a));
join_static_check<sqlpp::assert_cross_join_unique_names_t>(j, f.as(sqlpp::alias::a));
join_static_check<sqlpp::assert_cross_join_unique_names_t>(j, f.as(sqlpp::alias::b));
join_static_check<sqlpp::assert_cross_join_unique_names_t>(j, t.as(sqlpp::alias::a));
join_static_check<sqlpp::assert_cross_join_unique_names_t>(j, t.as(sqlpp::alias::b));
join_static_check<sqlpp::assert_cross_join_unique_names_t>(ta, fa);
join_static_check<sqlpp::assert_cross_join_unique_names_t>(j, fa);
join_static_check<sqlpp::assert_cross_join_unique_names_t>(j, fb);
join_static_check<sqlpp::assert_cross_join_unique_names_t>(j, ta);
join_static_check<sqlpp::assert_cross_join_unique_names_t>(j, tb);
// Prepare a cross_joins for tests:
const auto t_f = join(t, f);
const auto f_t = join(f, t);
const auto t_t = join(ta, tb);
const auto f_f = join(fa, fb);
// OK join.on()
on_static_check<sqlpp::consistent_t>(t_f, t.alpha > f.omega);
on_static_check<sqlpp::consistent_t>(f_t, t.alpha < f.omega);
on_static_check<sqlpp::consistent_t>(f_f, fa.omega == fb.omega);
on_static_check<sqlpp::consistent_t>(t_t, ta.alpha == tb.alpha);
on_static_check<sqlpp::consistent_t>(t_f, t.gamma);
// Try join.on(non-expression)
on_static_check<sqlpp::assert_on_is_expression_t>(t_f, true);
on_static_check<sqlpp::assert_on_is_expression_t>(t_f, 7);
on_static_check<sqlpp::assert_on_is_expression_t>(t_f, t);
// Try join.on(non-boolean)
on_static_check<sqlpp::assert_on_is_boolean_expression_t>(t_f, t.alpha);
on_static_check<sqlpp::assert_on_is_boolean_expression_t>(t_f, t.beta);
on_static_check<sqlpp::assert_on_is_boolean_expression_t>(t_f, f.omega);
// Try join.on(foreign-table)
on_static_check<sqlpp::assert_join_on_no_foreign_table_dependencies_t>(t_f, ta.alpha != 0);
on_static_check<sqlpp::assert_join_on_no_foreign_table_dependencies_t>(t_t, t.gamma);
on_static_check<sqlpp::assert_join_on_no_foreign_table_dependencies_t>(f_f, f.omega > fa.omega);
}
template <typename Assert, typename Table>
void join_dynamic_check(const Table& table)
{
using CheckResult = sqlpp::check_dynamic_cross_join_t<Table>;
using ExpectedCheckResult = std::is_same<CheckResult, Assert>;
print_type_on_error<CheckResult>(ExpectedCheckResult{});
static_assert(ExpectedCheckResult::value, "Unexpected check result");
using JoinType = decltype(sqlpp::dynamic_join(table));
using InnerJoinType = decltype(sqlpp::dynamic_inner_join(table));
using LeftOuterJoinType = decltype(sqlpp::dynamic_left_outer_join(table));
using RightOuterJoinType = decltype(sqlpp::dynamic_right_outer_join(table));
using OuterJoinType = decltype(sqlpp::dynamic_outer_join(table));
using ExpectedReturnType =
sqlpp::logic::all_t<(Assert::value and sqlpp::is_dynamic_cross_join_t<JoinType>::value and
sqlpp::is_dynamic_cross_join_t<InnerJoinType>::value and
sqlpp::is_dynamic_cross_join_t<LeftOuterJoinType>::value and
sqlpp::is_dynamic_cross_join_t<RightOuterJoinType>::value and
sqlpp::is_dynamic_cross_join_t<OuterJoinType>::value) xor
(std::is_same<JoinType, sqlpp::bad_statement>::value and
std::is_same<InnerJoinType, sqlpp::bad_statement>::value and
std::is_same<LeftOuterJoinType, sqlpp::bad_statement>::value and
std::is_same<RightOuterJoinType, sqlpp::bad_statement>::value and
std::is_same<OuterJoinType, sqlpp::bad_statement>::value)>;
print_type_on_error<JoinType>(ExpectedReturnType{});
print_type_on_error<InnerJoinType>(ExpectedReturnType{});
print_type_on_error<LeftOuterJoinType>(ExpectedReturnType{});
print_type_on_error<RightOuterJoinType>(ExpectedReturnType{});
print_type_on_error<OuterJoinType>(ExpectedReturnType{});
static_assert(ExpectedReturnType::value, "Unexpected return type");
}
template <typename Assert, typename Lhs, typename Rhs>
void on_dynamic_check(const Lhs& lhs, const Rhs& rhs)
{
using CheckResult = sqlpp::check_dynamic_join_on_t<Lhs, Rhs>;
using ExpectedCheckResult = std::is_same<CheckResult, Assert>;
print_type_on_error<CheckResult>(ExpectedCheckResult{});
static_assert(ExpectedCheckResult::value, "Unexpected check result");
using ResultType = decltype(lhs.on(rhs));
using ExpectedReturnType = sqlpp::logic::all_t<(Assert::value and sqlpp::is_dynamic_join_t<ResultType>::value) xor
std::is_same<ResultType, sqlpp::bad_statement>::value>;
print_type_on_error<ResultType>(ExpectedReturnType{});
static_assert(ExpectedReturnType::value, "Unexpected return type");
}
void dynamic_join()
{
/*
// Prepare a few table aliases for tests
const auto ta = t.as(sqlpp::alias::a);
const auto fa = f.as(sqlpp::alias::a);
// OK
join_dynamic_check<sqlpp::consistent_t>(t);
join_dynamic_check<sqlpp::consistent_t>(t.join(f).unconditionally());
join_dynamic_check<sqlpp::consistent_t>(t.join(f).on(t.alpha > f.omega));
join_dynamic_check<sqlpp::consistent_t>(f);
join_dynamic_check<sqlpp::consistent_t>(ta);
join_dynamic_check<sqlpp::consistent_t>(fa);
// Try a bunch of non-tables
join_dynamic_check<sqlpp::assert_join_table_t>(7);
join_dynamic_check<sqlpp::assert_join_table_t>(t.alpha);
join_dynamic_check<sqlpp::assert_join_table_t>(t.beta);
join_dynamic_check<sqlpp::assert_join_table_t>(t.gamma);
join_dynamic_check<sqlpp::assert_join_table_t>(t.delta);
join_dynamic_check<sqlpp::assert_dynamic_cross_join_table_t>(7);
join_dynamic_check<sqlpp::assert_dynamic_cross_join_table_t>(t.alpha);
join_dynamic_check<sqlpp::assert_dynamic_cross_join_table_t>(t.beta);
join_dynamic_check<sqlpp::assert_dynamic_cross_join_table_t>(t.gamma);
join_dynamic_check<sqlpp::assert_dynamic_cross_join_table_t>(t.delta);
// Try cross joins (missing condition)
join_dynamic_check<sqlpp::assert_join_not_cross_join_t>(t.join(f));
*/
// Try (cross) joins
join_dynamic_check<sqlpp::assert_dynamic_cross_join_table_t>(t.join(f));
join_dynamic_check<sqlpp::assert_dynamic_cross_join_no_join_t>(t.join(f).unconditionally());
// Prepare a dynamic_cross_joins for tests:
const auto tj = dynamic_join(t);
const auto fj = dynamic_join(f);
// OK dynamic_join.on()
on_dynamic_check<sqlpp::consistent_t>(tj, t.alpha > f.omega);
on_dynamic_check<sqlpp::consistent_t>(fj, t.alpha < f.omega);
// Try dynamic_join.on(non-expression)
on_dynamic_check<sqlpp::assert_on_is_expression_t>(tj, true);
on_dynamic_check<sqlpp::assert_on_is_expression_t>(tj, 7);
on_dynamic_check<sqlpp::assert_on_is_expression_t>(tj, t);
// Try dynamic_join.on(non-boolean)
on_dynamic_check<sqlpp::assert_on_is_boolean_expression_t>(tj, t.alpha);
on_dynamic_check<sqlpp::assert_on_is_boolean_expression_t>(tj, t.beta);
on_dynamic_check<sqlpp::assert_on_is_boolean_expression_t>(tj, f.omega);
// OK dynamic_join.on(foreign-table)
on_dynamic_check<sqlpp::consistent_t>(tj, ta.alpha != 0);
on_dynamic_check<sqlpp::consistent_t>(tj, t.gamma);
on_dynamic_check<sqlpp::consistent_t>(tj, f.omega > fa.omega);
}
}