mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-15 20:31:16 +08:00
Reworked join syntax
This commit is contained in:
parent
c59c8d9ba9
commit
cff18e917b
@ -28,24 +28,29 @@
|
|||||||
#define SQLPP_JOIN_H
|
#define SQLPP_JOIN_H
|
||||||
|
|
||||||
#include <sqlpp11/type_traits.h>
|
#include <sqlpp11/type_traits.h>
|
||||||
|
#include <sqlpp11/on.h>
|
||||||
|
|
||||||
namespace sqlpp
|
namespace sqlpp
|
||||||
{
|
{
|
||||||
struct inner_join_t
|
struct inner_join_t
|
||||||
{
|
{
|
||||||
static constexpr const char* _name = "INNER";
|
static constexpr bool _require_on = true;
|
||||||
|
static constexpr const char* _name = " INNER ";
|
||||||
};
|
};
|
||||||
struct outer_join_t
|
struct outer_join_t
|
||||||
{
|
{
|
||||||
static constexpr const char* _name = "OUTER";
|
static constexpr bool _require_on = true;
|
||||||
|
static constexpr const char* _name = " OUTER ";
|
||||||
};
|
};
|
||||||
struct left_join_t
|
struct left_outer_join_t
|
||||||
{
|
{
|
||||||
static constexpr const char* _name = "LEFT OUTER";
|
static constexpr bool _require_on = true;
|
||||||
|
static constexpr const char* _name = " LEFT OUTER ";
|
||||||
};
|
};
|
||||||
struct right_join_t
|
struct right_outer_join_t
|
||||||
{
|
{
|
||||||
static constexpr const char* _name = "RIGHT OUTER";
|
static constexpr bool _require_on = true;
|
||||||
|
static constexpr const char* _name = " RIGHT OUTER ";
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename JoinType, typename Lhs, typename Rhs, typename On = noop>
|
template<typename JoinType, typename Lhs, typename Rhs, typename On = noop>
|
||||||
@ -53,42 +58,66 @@ namespace sqlpp
|
|||||||
{
|
{
|
||||||
static_assert(is_table_t<Lhs>::value, "invalid lhs argument for join()");
|
static_assert(is_table_t<Lhs>::value, "invalid lhs argument for join()");
|
||||||
static_assert(is_table_t<Rhs>::value, "invalid rhs argument for join()");
|
static_assert(is_table_t<Rhs>::value, "invalid rhs argument for join()");
|
||||||
static_assert(is_noop<On>::value or is_expression_t<On>::value, "invalid on expression in join().on()");
|
static_assert(is_noop<On>::value or is_on_t<On>::value, "invalid on expression in join().on()");
|
||||||
|
|
||||||
static_assert(Lhs::_table_set::template is_disjunct_from<typename Rhs::_table_set>::value, "joined tables must not be identical");
|
static_assert(Lhs::_table_set::template is_disjunct_from<typename Rhs::_table_set>::value, "joined tables must not be identical");
|
||||||
|
|
||||||
using _is_table = typename std::conditional<is_noop<On>::value,
|
using _is_table = std::true_type;
|
||||||
std::false_type,
|
using _table_set = typename Lhs::_table_set::template join<typename Rhs::_table_set>::type;
|
||||||
std::true_type>::type;
|
|
||||||
using _table_set = typename std::conditional<is_noop<On>::value,
|
template<typename OnT>
|
||||||
void,
|
using set_on_t = join_t<JoinType, Lhs, Rhs, OnT>;
|
||||||
typename Lhs::_table_set::template join<typename Rhs::_table_set>::type>::type;
|
|
||||||
|
|
||||||
template<typename Expr>
|
template<typename Expr>
|
||||||
using add_on_t = join_t<JoinType, Lhs, Rhs, typename std::decay<Expr>::type>;
|
set_on_t<on_t<typename std::decay<Expr>::type>> on(Expr&& expr)
|
||||||
|
|
||||||
template<typename Expr>
|
|
||||||
add_on_t<Expr> on(Expr&& expr)
|
|
||||||
{
|
{
|
||||||
return { _lhs, _rhs, std::forward<Expr>(expr) };
|
return { _lhs, _rhs, {std::forward<Expr>(expr)} };
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
join_t<inner_join_t, join_t, typename std::decay<T>::type> join(T&& t)
|
join_t<inner_join_t, join_t, typename std::decay<T>::type> join(T&& t)
|
||||||
{
|
{
|
||||||
|
static_assert(not (JoinType::_require_on and is_noop<On>::value), "join type requires on()");
|
||||||
|
return { *this, std::forward<T>(t) };
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
join_t<inner_join_t, join_t, typename std::decay<T>::type> inner_join(T&& t)
|
||||||
|
{
|
||||||
|
static_assert(not (JoinType::_require_on and is_noop<On>::value), "join type requires on()");
|
||||||
|
return { *this, std::forward<T>(t) };
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
join_t<outer_join_t, join_t, typename std::decay<T>::type> outer_join(T&& t)
|
||||||
|
{
|
||||||
|
static_assert(not (JoinType::_require_on and is_noop<On>::value), "join type requires on()");
|
||||||
|
return { *this, std::forward<T>(t) };
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
join_t<left_outer_join_t, join_t, typename std::decay<T>::type> left_outer_join(T&& t)
|
||||||
|
{
|
||||||
|
static_assert(not (JoinType::_require_on and is_noop<On>::value), "join type requires on()");
|
||||||
|
return { *this, std::forward<T>(t) };
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
join_t<right_outer_join_t, join_t, typename std::decay<T>::type> right_outer_join(T&& t)
|
||||||
|
{
|
||||||
|
static_assert(not (JoinType::_require_on and is_noop<On>::value), "join type requires on()");
|
||||||
return { *this, std::forward<T>(t) };
|
return { *this, std::forward<T>(t) };
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Db>
|
template<typename Db>
|
||||||
void serialize(std::ostream& os, Db& db) const
|
void serialize(std::ostream& os, Db& db) const
|
||||||
{
|
{
|
||||||
os << " (";
|
static_assert(not (JoinType::_require_on and is_noop<On>::value), "join type requires on()");
|
||||||
_lhs.serialize(os, db);
|
_lhs.serialize(os, db);
|
||||||
os << ") " << JoinType::_name << " JOIN (";
|
os << JoinType::_name;
|
||||||
|
os << " JOIN ";
|
||||||
_rhs.serialize(os, db);
|
_rhs.serialize(os, db);
|
||||||
os << ") ON (";
|
|
||||||
_on.serialize(os, db);
|
_on.serialize(os, db);
|
||||||
os << ")";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Lhs _lhs;
|
Lhs _lhs;
|
||||||
|
89
include/sqlpp11/on.h
Normal file
89
include/sqlpp11/on.h
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef SQLPP_ON_H
|
||||||
|
#define SQLPP_ON_H
|
||||||
|
|
||||||
|
#include <ostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <sqlpp11/type_traits.h>
|
||||||
|
#include <sqlpp11/detail/serializable.h>
|
||||||
|
|
||||||
|
namespace sqlpp
|
||||||
|
{
|
||||||
|
template<typename Expr>
|
||||||
|
struct on_t
|
||||||
|
{
|
||||||
|
static_assert(is_expression_t<Expr>::value, "invalid expression argument in on()");
|
||||||
|
|
||||||
|
using _is_on = std::true_type;
|
||||||
|
|
||||||
|
template<typename Db>
|
||||||
|
void serialize(std::ostream& os, Db& db) const
|
||||||
|
{
|
||||||
|
os << " ON ";
|
||||||
|
_expr.serialize(os, db);
|
||||||
|
}
|
||||||
|
|
||||||
|
Expr _expr;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Db>
|
||||||
|
struct dynamic_on_t
|
||||||
|
{
|
||||||
|
|
||||||
|
using _is_on = std::true_type;
|
||||||
|
using _is_dynamic = std::true_type;
|
||||||
|
|
||||||
|
template<typename Expr>
|
||||||
|
void add(Expr&& expr)
|
||||||
|
{
|
||||||
|
static_assert(is_expression_t<typename std::decay<Expr>::type>::value, "invalid expression argument in on()");
|
||||||
|
_conditions.push_back(std::forward<Expr>(expr));
|
||||||
|
}
|
||||||
|
|
||||||
|
void serialize(std::ostream& os, Db& db) const
|
||||||
|
{
|
||||||
|
if (_conditions.empty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
os << " ON ";
|
||||||
|
bool first = true;
|
||||||
|
for (const auto& condition : _conditions)
|
||||||
|
{
|
||||||
|
if (not first)
|
||||||
|
os << " AND ";
|
||||||
|
condition.serialize(os, db);
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<detail::serializable_t<Db>> _conditions;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -54,6 +54,30 @@ namespace sqlpp
|
|||||||
return { *static_cast<const Table*>(this), std::forward<T>(t) };
|
return { *static_cast<const Table*>(this), std::forward<T>(t) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
join_t<inner_join_t, Table, typename std::decay<T>::type> inner_join(T&& t)
|
||||||
|
{
|
||||||
|
return { *static_cast<const Table*>(this), std::forward<T>(t) };
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
join_t<outer_join_t, Table, typename std::decay<T>::type> outer_join(T&& t)
|
||||||
|
{
|
||||||
|
return { *static_cast<const Table*>(this), std::forward<T>(t) };
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
join_t<left_outer_join_t, Table, typename std::decay<T>::type> left_outer_join(T&& t)
|
||||||
|
{
|
||||||
|
return { *static_cast<const Table*>(this), std::forward<T>(t) };
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
join_t<right_outer_join_t, Table, typename std::decay<T>::type> right_outer_join(T&& t)
|
||||||
|
{
|
||||||
|
return { *static_cast<const Table*>(this), std::forward<T>(t) };
|
||||||
|
}
|
||||||
|
|
||||||
template<typename AliasProvider>
|
template<typename AliasProvider>
|
||||||
struct alias_t: public ColumnSpec::_name_t::template _member_t<column_t<AliasProvider, ColumnSpec>>...
|
struct alias_t: public ColumnSpec::_name_t::template _member_t<column_t<AliasProvider, ColumnSpec>>...
|
||||||
{
|
{
|
||||||
|
@ -96,6 +96,7 @@ namespace sqlpp
|
|||||||
SQLPP_TYPE_TRAIT_GENERATOR(is_select_flag_list);
|
SQLPP_TYPE_TRAIT_GENERATOR(is_select_flag_list);
|
||||||
SQLPP_TYPE_TRAIT_GENERATOR(is_select_expression_list);
|
SQLPP_TYPE_TRAIT_GENERATOR(is_select_expression_list);
|
||||||
SQLPP_TYPE_TRAIT_GENERATOR(is_from);
|
SQLPP_TYPE_TRAIT_GENERATOR(is_from);
|
||||||
|
SQLPP_TYPE_TRAIT_GENERATOR(is_on);
|
||||||
SQLPP_TYPE_TRAIT_GENERATOR(is_dynamic);
|
SQLPP_TYPE_TRAIT_GENERATOR(is_dynamic);
|
||||||
SQLPP_TYPE_TRAIT_GENERATOR(is_where);
|
SQLPP_TYPE_TRAIT_GENERATOR(is_where);
|
||||||
SQLPP_TYPE_TRAIT_GENERATOR(is_group_by);
|
SQLPP_TYPE_TRAIT_GENERATOR(is_group_by);
|
||||||
|
Loading…
Reference in New Issue
Block a user