mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-16 04:47:18 +08:00
Added portable static asserts for joins and first static tests
This commit is contained in:
parent
57d6d3c992
commit
5c7d588450
@ -33,6 +33,26 @@
|
||||
|
||||
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_unique_names_t, "joined table names have to be unique");
|
||||
|
||||
template <typename Lhs, typename Rhs>
|
||||
struct check_cross_join
|
||||
{
|
||||
using type = static_combined_check_t<
|
||||
static_check_t<is_table_t<Lhs>::value, assert_cross_join_lhs_table_t>,
|
||||
static_check_t<is_table_t<Rhs>::value, assert_cross_join_rhs_table_t>,
|
||||
static_check_t<not is_join_t<Rhs>::value, assert_cross_join_rhs_no_join_t>,
|
||||
static_check_t<detail::is_disjunct_from<detail::make_name_of_set_t<provided_tables_of<Lhs>>,
|
||||
detail::make_name_of_set_t<provided_tables_of<Rhs>>>::value,
|
||||
assert_cross_join_unique_names_t>>;
|
||||
};
|
||||
|
||||
template <typename Lhs, typename Rhs>
|
||||
using check_cross_join_t = typename check_cross_join<Lhs, Rhs>::type;
|
||||
|
||||
template <typename CrossJoin, typename On>
|
||||
struct join_t;
|
||||
|
||||
@ -86,6 +106,56 @@ namespace sqlpp
|
||||
return context;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Lhs, typename Rhs>
|
||||
auto join(Lhs lhs, Rhs rhs) -> typename std::conditional<check_cross_join_t<Lhs, Rhs>::value,
|
||||
cross_join_t<inner_join_t, Lhs, Rhs>,
|
||||
bad_statement>::type
|
||||
{
|
||||
check_cross_join_t<Lhs, Rhs>::_();
|
||||
|
||||
return {lhs, rhs};
|
||||
}
|
||||
|
||||
template <typename Lhs, typename Rhs>
|
||||
auto inner_join(Lhs lhs, Rhs rhs) -> typename std::conditional<check_cross_join_t<Lhs, Rhs>::value,
|
||||
cross_join_t<inner_join_t, Lhs, Rhs>,
|
||||
bad_statement>::type
|
||||
{
|
||||
check_cross_join_t<Lhs, Rhs>::_();
|
||||
|
||||
return {lhs, rhs};
|
||||
}
|
||||
|
||||
template <typename Lhs, typename Rhs>
|
||||
auto left_outer_join(Lhs lhs, Rhs rhs) -> typename std::conditional<check_cross_join_t<Lhs, Rhs>::value,
|
||||
cross_join_t<left_outer_join_t, Lhs, Rhs>,
|
||||
bad_statement>::type
|
||||
{
|
||||
check_cross_join_t<Lhs, Rhs>::_();
|
||||
|
||||
return {lhs, rhs};
|
||||
}
|
||||
|
||||
template <typename Lhs, typename Rhs>
|
||||
auto right_outer_join(Lhs lhs, Rhs rhs) -> typename std::conditional<check_cross_join_t<Lhs, Rhs>::value,
|
||||
cross_join_t<right_outer_join_t, Lhs, Rhs>,
|
||||
bad_statement>::type
|
||||
{
|
||||
check_cross_join_t<Lhs, Rhs>::_();
|
||||
|
||||
return {lhs, rhs};
|
||||
}
|
||||
|
||||
template <typename Lhs, typename Rhs>
|
||||
auto outer_join(Lhs lhs, Rhs rhs) -> typename std::conditional<check_cross_join_t<Lhs, Rhs>::value,
|
||||
cross_join_t<right_outer_join_t, Lhs, Rhs>,
|
||||
bad_statement>::type
|
||||
{
|
||||
check_cross_join_t<Lhs, Rhs>::_();
|
||||
|
||||
return {lhs, rhs};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2015, Roland Bock
|
||||
* Copyright (c) 2013-2016, Roland Bock
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
@ -50,33 +50,33 @@ namespace sqlpp
|
||||
"on() condition must not depend on other tables");
|
||||
|
||||
template <typename T>
|
||||
cross_join_t<inner_join_t, join_t, T> join(T t)
|
||||
auto join(T t) const -> decltype(::sqlpp::join(*this, t))
|
||||
{
|
||||
return {*this, t};
|
||||
return ::sqlpp::join(*this, t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
cross_join_t<inner_join_t, join_t, T> inner_join(T t)
|
||||
auto inner_join(T t) const -> decltype(::sqlpp::inner_join(*this, t))
|
||||
{
|
||||
return {*this, t};
|
||||
return ::sqlpp::inner_join(*this, t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
cross_join_t<outer_join_t, join_t, T> outer_join(T t)
|
||||
auto left_outer_join(T t) const -> decltype(::sqlpp::left_outer_join(*this, t))
|
||||
{
|
||||
return {*this, t};
|
||||
return ::sqlpp::left_outer_join(*this, t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
cross_join_t<left_outer_join_t, join_t, T> left_outer_join(T t)
|
||||
auto right_outer_join(T t) const -> decltype(::sqlpp::right_outer_join(*this, t))
|
||||
{
|
||||
return {*this, t};
|
||||
return ::sqlpp::right_outer_join(*this, t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
cross_join_t<right_outer_join_t, join_t, T> right_outer_join(T t)
|
||||
auto outer_join(T t) const -> decltype(::sqlpp::outer_join(*this, t))
|
||||
{
|
||||
return {*this, t};
|
||||
return ::sqlpp::outer_join(*this, t);
|
||||
}
|
||||
|
||||
CrossJoin _cross_join;
|
||||
|
@ -62,33 +62,33 @@ namespace sqlpp
|
||||
using _alias_t = table_alias_t<AliasProvider, Table, ColumnSpec...>;
|
||||
|
||||
template <typename T>
|
||||
cross_join_t<inner_join_t, Table, T> join(T t) const
|
||||
auto join(T t) const -> decltype(::sqlpp::join(std::declval<Table>(), t))
|
||||
{
|
||||
return {*static_cast<const Table*>(this), t};
|
||||
return ::sqlpp::join(*static_cast<const Table*>(this), t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
cross_join_t<inner_join_t, Table, T> inner_join(T t) const
|
||||
auto inner_join(T t) const -> decltype(::sqlpp::inner_join(std::declval<Table>(), t))
|
||||
{
|
||||
return {*static_cast<const Table*>(this), t};
|
||||
return ::sqlpp::inner_join(*static_cast<const Table*>(this), t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
cross_join_t<outer_join_t, Table, T> outer_join(T t) const
|
||||
auto left_outer_join(T t) const -> decltype(::sqlpp::left_outer_join(std::declval<Table>(), t))
|
||||
{
|
||||
return {*static_cast<const Table*>(this), t};
|
||||
return ::sqlpp::left_outer_join(*static_cast<const Table*>(this), t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
cross_join_t<left_outer_join_t, Table, T> left_outer_join(T t) const
|
||||
auto right_outer_join(T t) const -> decltype(::sqlpp::right_outer_join(std::declval<Table>(), t))
|
||||
{
|
||||
return {*static_cast<const Table*>(this), t};
|
||||
return ::sqlpp::right_outer_join(*static_cast<const Table*>(this), t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
cross_join_t<right_outer_join_t, Table, T> right_outer_join(T t) const
|
||||
auto outer_join(T t) const -> decltype(::sqlpp::outer_join(std::declval<Table>(), t))
|
||||
{
|
||||
return {*static_cast<const Table*>(this), t};
|
||||
return ::sqlpp::outer_join(*static_cast<const Table*>(this), t);
|
||||
}
|
||||
|
||||
template <typename AliasProvider>
|
||||
|
@ -61,33 +61,33 @@ namespace sqlpp
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
cross_join_t<inner_join_t, table_alias_t, T> join(T t) const
|
||||
auto join(T t) const -> decltype(::sqlpp::join(*this, t))
|
||||
{
|
||||
return {*this, t};
|
||||
return ::sqlpp::join(*this, t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
cross_join_t<inner_join_t, table_alias_t, T> inner_join(T t) const
|
||||
auto inner_join(T t) const -> decltype(::sqlpp::inner_join(*this, t))
|
||||
{
|
||||
return {*this, t};
|
||||
return ::sqlpp::inner_join(*this, t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
cross_join_t<outer_join_t, table_alias_t, T> outer_join(T t) const
|
||||
auto left_outer_join(T t) const -> decltype(::sqlpp::left_outer_join(*this, t))
|
||||
{
|
||||
return {*this, t};
|
||||
return ::sqlpp::left_outer_join(*this, t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
cross_join_t<left_outer_join_t, table_alias_t, T> left_outer_join(T t) const
|
||||
auto right_outer_join(T t) const -> decltype(::sqlpp::right_outer_join(*this, t))
|
||||
{
|
||||
return {*this, t};
|
||||
return ::sqlpp::right_outer_join(*this, t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
cross_join_t<right_outer_join_t, table_alias_t, T> right_outer_join(T t) const
|
||||
auto outer_join(T t) const -> decltype(::sqlpp::outer_join(*this, t))
|
||||
{
|
||||
return {*this, t};
|
||||
return ::sqlpp::outer_join(*this, t);
|
||||
}
|
||||
|
||||
Table _table;
|
||||
|
@ -31,6 +31,7 @@ endfunction()
|
||||
test_compile(aggregates)
|
||||
test_compile(case)
|
||||
test_compile(from)
|
||||
test_compile(join)
|
||||
test_compile(where)
|
||||
test_compile(insert)
|
||||
test_compile(date)
|
||||
|
167
test_static_asserts/join.cpp
Normal file
167
test_static_asserts/join.cpp
Normal file
@ -0,0 +1,167 @@
|
||||
/*
|
||||
* Copyright (c) 2015-2016, 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 <iostream>
|
||||
#include "MockDb.h"
|
||||
#include "Sample.h"
|
||||
#include <sqlpp11/sqlpp11.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
constexpr auto t = test::TabBar{};
|
||||
constexpr auto f = test::TabFoo{};
|
||||
|
||||
template <typename T>
|
||||
void print_type_on_error(std::true_type)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void print_type_on_error(std::false_type)
|
||||
{
|
||||
T::_print_me_;
|
||||
}
|
||||
|
||||
template <typename Assert, typename Lhs, typename Rhs>
|
||||
void join_static_check(const Lhs& lhs, const Rhs& rhs)
|
||||
{
|
||||
using CheckResult = sqlpp::check_cross_join_t<Lhs, Rhs>;
|
||||
using ExpectedCheckResult = std::is_same<CheckResult, Assert>;
|
||||
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
|
||||
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 Expression>
|
||||
void join_dynamic_check(const Expression& expression)
|
||||
{
|
||||
static auto db = MockDb{};
|
||||
using CheckResult = sqlpp::check_join_dynamic_t<decltype(db), Expression>;
|
||||
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{});
|
||||
static_assert(ExpectedReturnType::value, "Unexpected return type");
|
||||
}
|
||||
*/
|
||||
|
||||
void static_join()
|
||||
{
|
||||
// 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));
|
||||
|
||||
// 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);
|
||||
|
||||
// Prepare a join for tests:
|
||||
const auto j = join(t.as(sqlpp::alias::a), t.as(sqlpp::alias::b)).unconditionally();
|
||||
|
||||
// OK: Add a third table
|
||||
join_static_check<sqlpp::consistent_t>(j, f);
|
||||
join_static_check<sqlpp::consistent_t>(j, t.as(sqlpp::alias::c));
|
||||
join_static_check<sqlpp::consistent_t>(j, t);
|
||||
|
||||
// Try a bunch of non-tables
|
||||
join_static_check<sqlpp::assert_cross_join_rhs_table_t>(t, 7);
|
||||
join_static_check<sqlpp::assert_cross_join_rhs_table_t>(t, t.alpha);
|
||||
join_static_check<sqlpp::assert_cross_join_rhs_table_t>(t, t.beta);
|
||||
join_static_check<sqlpp::assert_cross_join_rhs_table_t>(t, t.gamma);
|
||||
join_static_check<sqlpp::assert_cross_join_rhs_table_t>(t, t.delta);
|
||||
|
||||
join_static_check<sqlpp::assert_cross_join_lhs_table_t>(7, t);
|
||||
join_static_check<sqlpp::assert_cross_join_lhs_table_t>(t.alpha, t);
|
||||
join_static_check<sqlpp::assert_cross_join_lhs_table_t>(t.beta, t);
|
||||
join_static_check<sqlpp::assert_cross_join_lhs_table_t>(t.gamma, t);
|
||||
join_static_check<sqlpp::assert_cross_join_lhs_table_t>(t.delta, t);
|
||||
|
||||
// Try to join with join (rhs)
|
||||
join_static_check<sqlpp::assert_cross_join_rhs_no_join_t>(t, j);
|
||||
join_static_check<sqlpp::assert_cross_join_rhs_no_join_t>(f, j);
|
||||
join_static_check<sqlpp::assert_cross_join_rhs_no_join_t>(t.as(sqlpp::alias::left), j);
|
||||
|
||||
// Try to join identical table names
|
||||
join_static_check<sqlpp::assert_cross_join_unique_names_t>(t, t);
|
||||
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));
|
||||
}
|
||||
|
||||
void dynamic_join()
|
||||
{
|
||||
/*
|
||||
// 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));
|
||||
|
||||
// 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);
|
||||
|
||||
// Try cross joins (missing condition)
|
||||
join_dynamic_check<sqlpp::assert_join_not_cross_join_t>(t.join(f));
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, char* [])
|
||||
{
|
||||
static_join();
|
||||
dynamic_join();
|
||||
}
|
@ -326,7 +326,6 @@ int SelectType(int, char* [])
|
||||
|
||||
{
|
||||
auto s = dynamic_select(db, all_of(t)).dynamic_from(t).dynamic_where().dynamic_limit().dynamic_offset();
|
||||
#warning : Need to test the deprecated stuff
|
||||
s.from.add(dynamic_join(f).on(f.omega > t.alpha));
|
||||
s.where.add_ntc(t.alpha > 7 and t.alpha == any(select(t.alpha).from(t).where(t.alpha < 3)));
|
||||
s.limit.set(30);
|
||||
|
Loading…
Reference in New Issue
Block a user