2016-02-28 19:07:56 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-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.
|
|
|
|
*/
|
|
|
|
|
2016-03-06 17:36:42 +08:00
|
|
|
#ifndef SQLPP_PRE_JOIN_H
|
|
|
|
#define SQLPP_PRE_JOIN_H
|
2016-02-28 19:07:56 +08:00
|
|
|
|
2016-04-10 03:46:31 +08:00
|
|
|
#include <sqlpp11/bad_statement.h>
|
2016-02-28 19:07:56 +08:00
|
|
|
#include <sqlpp11/join_types.h>
|
|
|
|
#include <sqlpp11/on.h>
|
|
|
|
#include <sqlpp11/noop.h>
|
|
|
|
|
|
|
|
namespace sqlpp
|
|
|
|
{
|
2016-03-06 17:36:42 +08:00
|
|
|
SQLPP_PORTABLE_STATIC_ASSERT(assert_pre_join_lhs_table_t, "lhs argument of join() has to be a table or a join");
|
|
|
|
SQLPP_PORTABLE_STATIC_ASSERT(assert_pre_join_rhs_table_t, "rhs argument of join() has to be a table");
|
|
|
|
SQLPP_PORTABLE_STATIC_ASSERT(assert_pre_join_rhs_no_join_t, "rhs argument of join() must not be a join");
|
|
|
|
SQLPP_PORTABLE_STATIC_ASSERT(assert_pre_join_unique_names_t, "joined table names have to be unique");
|
2016-03-05 04:07:42 +08:00
|
|
|
|
|
|
|
template <typename Lhs, typename Rhs>
|
2016-03-06 17:36:42 +08:00
|
|
|
struct check_pre_join
|
2016-03-05 04:07:42 +08:00
|
|
|
{
|
|
|
|
using type = static_combined_check_t<
|
2016-03-06 17:36:42 +08:00
|
|
|
static_check_t<is_table_t<Lhs>::value, assert_pre_join_lhs_table_t>,
|
|
|
|
static_check_t<is_table_t<Rhs>::value, assert_pre_join_rhs_table_t>,
|
|
|
|
static_check_t<not is_join_t<Rhs>::value, assert_pre_join_rhs_no_join_t>,
|
2016-03-05 04:07:42 +08:00
|
|
|
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,
|
2016-03-06 17:36:42 +08:00
|
|
|
assert_pre_join_unique_names_t>>;
|
2016-03-05 04:07:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Lhs, typename Rhs>
|
2016-03-06 17:36:42 +08:00
|
|
|
using check_pre_join_t = typename check_pre_join<Lhs, Rhs>::type;
|
2016-03-05 04:07:42 +08:00
|
|
|
|
2016-03-06 17:36:42 +08:00
|
|
|
SQLPP_PORTABLE_STATIC_ASSERT(assert_join_consist_of_pre_join_and_on_t,
|
|
|
|
"join has to consist of a pre_join and a join condition");
|
2016-03-06 02:32:27 +08:00
|
|
|
SQLPP_PORTABLE_STATIC_ASSERT(assert_join_no_table_dependencies_t, "joined tables must not depend on other tables");
|
|
|
|
SQLPP_PORTABLE_STATIC_ASSERT(assert_join_on_no_foreign_table_dependencies_t,
|
|
|
|
"on() condition must not depend on other tables");
|
|
|
|
|
2016-03-06 17:36:42 +08:00
|
|
|
template <typename PreJoin, typename On>
|
2016-03-06 02:32:27 +08:00
|
|
|
struct check_join
|
|
|
|
{
|
|
|
|
using type = static_combined_check_t<
|
2016-03-06 17:36:42 +08:00
|
|
|
static_check_t<is_pre_join_t<PreJoin>::value, assert_join_consist_of_pre_join_and_on_t>,
|
|
|
|
static_check_t<is_on_t<On>::value, assert_join_consist_of_pre_join_and_on_t>,
|
|
|
|
static_check_t<required_tables_of<PreJoin>::size::value == 0, assert_join_no_table_dependencies_t>,
|
|
|
|
static_check_t<detail::is_subset_of<required_tables_of<On>, provided_tables_of<PreJoin>>::value,
|
2016-03-06 02:32:27 +08:00
|
|
|
assert_join_on_no_foreign_table_dependencies_t>>;
|
|
|
|
};
|
|
|
|
|
2016-03-06 17:36:42 +08:00
|
|
|
template <typename PreJoin, typename On>
|
|
|
|
using check_join_t = typename check_join<PreJoin, On>::type;
|
2016-03-06 02:32:27 +08:00
|
|
|
|
2016-03-06 17:36:42 +08:00
|
|
|
template <typename PreJoin, typename Expr>
|
2016-03-06 02:32:27 +08:00
|
|
|
struct check_join_on
|
|
|
|
{
|
2016-03-06 17:36:42 +08:00
|
|
|
using type = static_combined_check_t<check_on_t<Expr>, check_join_t<PreJoin, on_t<Expr>>>;
|
2016-03-06 02:32:27 +08:00
|
|
|
};
|
|
|
|
|
2016-03-06 17:36:42 +08:00
|
|
|
template <typename PreJoin, typename Expr>
|
|
|
|
using check_join_on_t = typename check_join_on<PreJoin, Expr>::type;
|
2016-03-06 02:32:27 +08:00
|
|
|
|
2016-03-06 17:36:42 +08:00
|
|
|
template <typename PreJoin, typename On>
|
2016-02-28 19:07:56 +08:00
|
|
|
struct join_t;
|
|
|
|
|
|
|
|
template <typename JoinType, typename Lhs, typename Rhs>
|
2016-03-06 17:36:42 +08:00
|
|
|
struct pre_join_t
|
2016-02-28 19:07:56 +08:00
|
|
|
{
|
2016-03-06 17:36:42 +08:00
|
|
|
using _traits = make_traits<no_value_t, tag::is_pre_join>;
|
2016-02-28 19:07:56 +08:00
|
|
|
using _nodes = detail::type_vector<Lhs, Rhs>;
|
|
|
|
using _can_be_null = std::false_type;
|
2016-03-11 16:25:36 +08:00
|
|
|
using _provided_outer_tables = typename JoinType::template _provided_outer_tables<Lhs, Rhs>;
|
2016-02-28 19:07:56 +08:00
|
|
|
|
|
|
|
static_assert(is_table_t<Lhs>::value, "lhs argument for join() has to be a table or join");
|
|
|
|
static_assert(is_table_t<Rhs>::value, "rhs argument for join() has to be a table");
|
|
|
|
static_assert(not is_join_t<Rhs>::value, "rhs argument for join must not be a join");
|
|
|
|
|
|
|
|
static_assert(detail::is_disjunct_from<provided_tables_of<Lhs>, provided_tables_of<Rhs>>::value,
|
|
|
|
"joined tables must not be identical");
|
|
|
|
|
2016-03-06 17:36:42 +08:00
|
|
|
static_assert(required_tables_of<pre_join_t>::size::value == 0, "joined tables must not depend on other tables");
|
2016-02-28 19:07:56 +08:00
|
|
|
|
|
|
|
template <typename Expr>
|
2016-03-06 17:36:42 +08:00
|
|
|
auto on(Expr expr) const -> typename std::conditional<check_join_on_t<pre_join_t, Expr>::value,
|
|
|
|
join_t<pre_join_t, on_t<Expr>>,
|
2016-03-06 05:27:11 +08:00
|
|
|
bad_statement>::type
|
2016-02-28 19:07:56 +08:00
|
|
|
{
|
2016-03-06 23:59:21 +08:00
|
|
|
using Check = check_join_on_t<pre_join_t, Expr>;
|
|
|
|
Check::_();
|
2016-02-28 19:07:56 +08:00
|
|
|
|
2016-03-06 23:59:21 +08:00
|
|
|
return on_impl(Check{}, expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename Expr>
|
|
|
|
auto on_impl(const std::false_type&, const Expr&) const -> bad_statement;
|
|
|
|
|
|
|
|
template <typename Expr>
|
|
|
|
auto on_impl(const std::true_type&, const Expr& expr) const -> join_t<pre_join_t, on_t<Expr>>
|
|
|
|
{
|
2016-03-06 02:32:27 +08:00
|
|
|
return {*this, {expr}};
|
2016-02-28 19:07:56 +08:00
|
|
|
}
|
|
|
|
|
2016-03-06 23:59:21 +08:00
|
|
|
public:
|
2016-02-28 19:07:56 +08:00
|
|
|
Lhs _lhs;
|
|
|
|
Rhs _rhs;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Context, typename JoinType, typename Lhs, typename Rhs>
|
2016-03-06 17:36:42 +08:00
|
|
|
struct serializer_t<Context, pre_join_t<JoinType, Lhs, Rhs>>
|
2016-02-28 19:07:56 +08:00
|
|
|
{
|
|
|
|
using _serialize_check = serialize_check_of<Context, Lhs, Rhs>;
|
2016-03-06 17:36:42 +08:00
|
|
|
using T = pre_join_t<JoinType, Lhs, Rhs>;
|
2016-02-28 19:07:56 +08:00
|
|
|
|
|
|
|
static Context& _(const T& t, Context& context)
|
|
|
|
{
|
|
|
|
serialize(t._lhs, context);
|
|
|
|
context << JoinType::_name;
|
|
|
|
context << " JOIN ";
|
|
|
|
serialize(t._rhs, context);
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
};
|
2016-03-05 04:07:42 +08:00
|
|
|
|
|
|
|
template <typename Lhs, typename Rhs>
|
2016-03-06 17:36:42 +08:00
|
|
|
auto join(Lhs lhs, Rhs rhs) -> typename std::conditional<check_pre_join_t<Lhs, Rhs>::value,
|
|
|
|
pre_join_t<inner_join_t, Lhs, Rhs>,
|
2016-03-05 04:07:42 +08:00
|
|
|
bad_statement>::type
|
|
|
|
{
|
2016-03-06 17:36:42 +08:00
|
|
|
check_pre_join_t<Lhs, Rhs>::_();
|
2016-03-05 04:07:42 +08:00
|
|
|
|
|
|
|
return {lhs, rhs};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Lhs, typename Rhs>
|
2016-03-06 17:36:42 +08:00
|
|
|
auto inner_join(Lhs lhs, Rhs rhs) -> typename std::conditional<check_pre_join_t<Lhs, Rhs>::value,
|
|
|
|
pre_join_t<inner_join_t, Lhs, Rhs>,
|
2016-03-05 04:07:42 +08:00
|
|
|
bad_statement>::type
|
|
|
|
{
|
2016-03-06 17:36:42 +08:00
|
|
|
check_pre_join_t<Lhs, Rhs>::_();
|
2016-03-05 04:07:42 +08:00
|
|
|
|
|
|
|
return {lhs, rhs};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Lhs, typename Rhs>
|
2016-03-06 17:36:42 +08:00
|
|
|
auto left_outer_join(Lhs lhs, Rhs rhs) -> typename std::conditional<check_pre_join_t<Lhs, Rhs>::value,
|
|
|
|
pre_join_t<left_outer_join_t, Lhs, Rhs>,
|
2016-03-05 04:07:42 +08:00
|
|
|
bad_statement>::type
|
|
|
|
{
|
2016-03-06 17:36:42 +08:00
|
|
|
check_pre_join_t<Lhs, Rhs>::_();
|
2016-03-05 04:07:42 +08:00
|
|
|
|
|
|
|
return {lhs, rhs};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Lhs, typename Rhs>
|
2016-03-06 17:36:42 +08:00
|
|
|
auto right_outer_join(Lhs lhs, Rhs rhs) -> typename std::conditional<check_pre_join_t<Lhs, Rhs>::value,
|
|
|
|
pre_join_t<right_outer_join_t, Lhs, Rhs>,
|
2016-03-05 04:07:42 +08:00
|
|
|
bad_statement>::type
|
|
|
|
{
|
2016-03-06 17:36:42 +08:00
|
|
|
check_pre_join_t<Lhs, Rhs>::_();
|
2016-03-05 04:07:42 +08:00
|
|
|
|
|
|
|
return {lhs, rhs};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Lhs, typename Rhs>
|
2016-03-06 17:36:42 +08:00
|
|
|
auto outer_join(Lhs lhs, Rhs rhs) -> typename std::conditional<check_pre_join_t<Lhs, Rhs>::value,
|
2016-03-11 16:25:36 +08:00
|
|
|
pre_join_t<outer_join_t, Lhs, Rhs>,
|
2016-03-05 04:07:42 +08:00
|
|
|
bad_statement>::type
|
|
|
|
{
|
2016-03-06 17:36:42 +08:00
|
|
|
check_pre_join_t<Lhs, Rhs>::_();
|
2016-03-05 04:07:42 +08:00
|
|
|
|
|
|
|
return {lhs, rhs};
|
|
|
|
}
|
2016-03-06 17:36:42 +08:00
|
|
|
|
|
|
|
template <typename Lhs, typename Rhs>
|
|
|
|
auto cross_join(Lhs lhs, Rhs rhs) ->
|
|
|
|
typename std::conditional<check_pre_join_t<Lhs, Rhs>::value,
|
|
|
|
join_t<pre_join_t<cross_join_t, Lhs, Rhs>, on_t<unconditional_t>>,
|
|
|
|
bad_statement>::type
|
|
|
|
{
|
|
|
|
check_pre_join_t<Lhs, Rhs>::_();
|
|
|
|
|
|
|
|
return {pre_join_t<cross_join_t, Lhs, Rhs>{lhs, rhs}, {}};
|
|
|
|
}
|
2016-02-28 19:07:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|