/* * Copyright (c) 2016-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 "MockDb.h" #include "Sample.h" #include #include namespace { constexpr auto t = test::TabBar{}; constexpr auto f = test::TabFoo{}; template void print_type_on_error(std::true_type) { } template void print_type_on_error(std::false_type) { T::_print_me_; } template void join_static_check(const Lhs& lhs, const Rhs& rhs) { using CheckResult = sqlpp::check_pre_join_t; using ExpectedCheckResult = std::is_same; print_type_on_error(ExpectedCheckResult{}); static_assert(ExpectedCheckResult::value, "Unexpected check result"); 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 CrossJoinType = decltype(sqlpp::cross_join(lhs, rhs)); using ExpectedReturnType = sqlpp::logic::all< (Assert::value and sqlpp::is_pre_join_t::value and sqlpp::is_pre_join_t::value and sqlpp::is_pre_join_t::value and sqlpp::is_pre_join_t::value and sqlpp::is_pre_join_t::value and sqlpp::is_join_t::value) xor (std::is_same::value and std::is_same::value and std::is_same::value and std::is_same::value and std::is_same::value and std::is_same::value)>; print_type_on_error(ExpectedReturnType{}); print_type_on_error(ExpectedReturnType{}); print_type_on_error(ExpectedReturnType{}); print_type_on_error(ExpectedReturnType{}); print_type_on_error(ExpectedReturnType{}); print_type_on_error(ExpectedReturnType{}); static_assert(ExpectedReturnType::value, "Unexpected return type"); } template void on_static_check(const Lhs& lhs, const Rhs& rhs) { using CheckResult = sqlpp::check_join_on_t; using ExpectedCheckResult = std::is_same; print_type_on_error(ExpectedCheckResult{}); static_assert(ExpectedCheckResult::value, "Unexpected check result"); using ResultType = decltype(lhs.on(rhs)); using ExpectedReturnType = sqlpp::logic::all<(Assert::value and sqlpp::is_join_t::value) xor std::is_same::value>; print_type_on_error(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(t, f); join_static_check(t, fa); join_static_check(ta, fb); // OK: Self join join_static_check(ta, tb); join_static_check(t, tb); join_static_check(ta, t); // Prepare a join for tests: const auto j = cross_join(ta, tb); // OK: Add a third table join_static_check(j, f); join_static_check(j, t.as(sqlpp::alias::c)); join_static_check(j, t); // Try a bunch of non-tables join_static_check(t, 7); join_static_check(t, t.id); join_static_check(t, t.textN); join_static_check(t, t.boolNn); join_static_check(t, t.intN); join_static_check(7, t); join_static_check(t.id, t); join_static_check(t.textN, t); join_static_check(t.boolNn, t); join_static_check(t.intN, t); // Try to join with join (rhs) join_static_check(t, j); join_static_check(f, j); join_static_check(t.as(sqlpp::alias::left), j); // Try to join identical table names join_static_check(t, t); join_static_check(f, f); join_static_check(t.as(f), f); join_static_check(t, f.as(t)); join_static_check(ta, fa); join_static_check(j, fa); join_static_check(j, fb); join_static_check(j, ta); join_static_check(j, tb); // Prepare a pre_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(t_f, t.id > f.doubleN); on_static_check(f_t, t.id < f.doubleN); on_static_check(f_f, fa.doubleN == fb.doubleN); on_static_check(t_t, ta.id == tb.id); on_static_check(t_f, t.boolNn); // Try join.on(non-expression) on_static_check(t_f, true); on_static_check(t_f, 7); on_static_check(t_f, t); // Try join.on(non-boolean) on_static_check(t_f, t.id); on_static_check(t_f, t.textN); on_static_check(t_f, f.doubleN); // Try join.on(foreign-table) on_static_check(t_f, ta.id != 0); on_static_check(t_t, t.boolNn); on_static_check(f_f, f.doubleN > fa.doubleN); } } int main(int, char* []) { static_join(); #warning add tests for optional from }