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

Fix join with CTE

At least for basic tests.
This commit is contained in:
Roland Bock 2024-10-19 20:25:50 +02:00
parent e46eeddc45
commit 32529eac90
8 changed files with 59 additions and 23 deletions

View File

@ -121,6 +121,7 @@ namespace sqlpp
{
using T = column_t<Table, ColumnSpec>;
return name_to_sql_string(context, name_tag_of_t<Table>::name) + "." + name_to_sql_string(context, name_tag_of_t<T>::name);
return name_to_sql_string(context, name_tag_of_t<Table>::name) + "." +
name_to_sql_string(context, name_tag_of_t<T>::name);
}
} // namespace sqlpp

View File

@ -43,6 +43,7 @@ namespace sqlpp
template <typename T>
auto join(T t) const -> decltype(::sqlpp::join(this->derived(), t))
{
#warning: move?
return ::sqlpp::join(this->derived(), t);
}

View File

@ -187,40 +187,41 @@ namespace sqlpp
// Note: See sqlpp11/core/basic/join_fwd.h for forward declarations including check_join_args.
#warning: reactivate check_join_args?
template <typename Lhs, typename Rhs, typename /* = check_join_args<Lhs, Rhs> */>
auto join(Lhs lhs, Rhs rhs) -> pre_join_t<Lhs, inner_join_t, Rhs>
auto join(Lhs lhs, Rhs rhs) -> pre_join_t<table_ref_t<Lhs>, inner_join_t, table_ref_t<Rhs>>
{
return {std::move(lhs), std::move(rhs)};
return {make_table_ref(std::move(lhs)), make_table_ref(std::move(rhs))};
}
template <typename Lhs, typename Rhs, typename /* = check_join_args<Lhs, Rhs> */>
auto inner_join(Lhs lhs, Rhs rhs) -> pre_join_t<Lhs, inner_join_t, Rhs>
auto inner_join(Lhs lhs, Rhs rhs) -> pre_join_t<table_ref_t<Lhs>, inner_join_t, table_ref_t<Rhs>>
{
return {std::move(lhs), std::move(rhs)};
return {make_table_ref(std::move(lhs)), make_table_ref(std::move(rhs))};
}
template <typename Lhs, typename Rhs, typename /* = check_join_args<Lhs, Rhs> */>
auto left_outer_join(Lhs lhs, Rhs rhs) -> pre_join_t<Lhs, left_outer_join_t, Rhs>
auto left_outer_join(Lhs lhs, Rhs rhs) -> pre_join_t<table_ref_t<Lhs>, left_outer_join_t, table_ref_t<Rhs>>
{
return {std::move(lhs), std::move(rhs)};
return {make_table_ref(std::move(lhs)), make_table_ref(std::move(rhs))};
}
template <typename Lhs, typename Rhs, typename /* = check_join_args<Lhs, Rhs> */>
auto right_outer_join(Lhs lhs, Rhs rhs) -> pre_join_t<Lhs, right_outer_join_t, Rhs>
auto right_outer_join(Lhs lhs, Rhs rhs) -> pre_join_t<table_ref_t<Lhs>, right_outer_join_t, table_ref_t<Rhs>>
{
return {std::move(lhs), std::move(rhs)};
return {make_table_ref(std::move(lhs)), make_table_ref(std::move(rhs))};
}
template <typename Lhs, typename Rhs, typename /* = check_join_args<Lhs, Rhs> */>
auto full_outer_join(Lhs lhs, Rhs rhs) -> pre_join_t<Lhs, full_outer_join_t, Rhs>
auto full_outer_join(Lhs lhs, Rhs rhs) -> pre_join_t<table_ref_t<Lhs>, full_outer_join_t, table_ref_t<Rhs>>
{
return {std::move(lhs), std::move(rhs)};
return {make_table_ref(std::move(lhs)), make_table_ref(std::move(rhs))};
}
template <typename Lhs, typename Rhs, typename /* = check_join_args<Lhs, Rhs> */>
auto cross_join(Lhs lhs, Rhs rhs) -> join_t<Lhs, cross_join_t, Rhs, unconditional_t>
auto cross_join(Lhs lhs, Rhs rhs) -> join_t<table_ref_t<Lhs>, cross_join_t, table_ref_t<Rhs>, unconditional_t>
{
return {std::move(lhs), std::move(rhs), {}};
return {make_table_ref(std::move(lhs)), make_table_ref(std::move(rhs)), {}};
}
} // namespace sqlpp

View File

@ -74,21 +74,21 @@ namespace sqlpp
sqlpp::detail::transform_t<provided_tables_of_t<Rhs>, make_char_sequence>{})>;
template <typename Lhs, typename Rhs, typename = check_join_args<Lhs, Rhs>>
auto join(Lhs lhs, Rhs rhs) -> pre_join_t<Lhs, inner_join_t, Rhs>;
auto join(Lhs lhs, Rhs rhs) -> pre_join_t<table_ref_t<Lhs>, inner_join_t, table_ref_t<Rhs>>;
template <typename Lhs, typename Rhs, typename = check_join_args<Lhs, Rhs>>
auto inner_join(Lhs lhs, Rhs rhs) -> pre_join_t<Lhs, inner_join_t, Rhs>;
auto inner_join(Lhs lhs, Rhs rhs) -> pre_join_t<table_ref_t<Lhs>, inner_join_t, table_ref_t<Rhs>>;
template <typename Lhs, typename Rhs, typename = check_join_args<Lhs, Rhs>>
auto left_outer_join(Lhs lhs, Rhs rhs) -> pre_join_t<Lhs, left_outer_join_t, Rhs>;
auto left_outer_join(Lhs lhs, Rhs rhs) -> pre_join_t<table_ref_t<Lhs>, left_outer_join_t, table_ref_t<Rhs>>;
template <typename Lhs, typename Rhs, typename = check_join_args<Lhs, Rhs>>
auto right_outer_join(Lhs lhs, Rhs rhs) -> pre_join_t<Lhs, right_outer_join_t, Rhs>;
auto right_outer_join(Lhs lhs, Rhs rhs) -> pre_join_t<table_ref_t<Lhs>, right_outer_join_t, table_ref_t<Rhs>>;
template <typename Lhs, typename Rhs, typename = check_join_args<Lhs, Rhs>>
auto full_outer_join(Lhs lhs, Rhs rhs) -> pre_join_t<Lhs, full_outer_join_t, Rhs>;
auto full_outer_join(Lhs lhs, Rhs rhs) -> pre_join_t<table_ref_t<Lhs>, full_outer_join_t, table_ref_t<Rhs>>;
template <typename Lhs, typename Rhs, typename = check_join_args<Lhs, Rhs>>
auto cross_join(Lhs lhs, Rhs rhs) -> join_t<Lhs, cross_join_t, Rhs, unconditional_t>;
auto cross_join(Lhs lhs, Rhs rhs) -> join_t<table_ref_t<Lhs>, cross_join_t, table_ref_t<Rhs>, unconditional_t>;
} // namespace sqlpp

View File

@ -45,7 +45,7 @@ namespace sqlpp
using from_table_t = typename from_table_impl<T>::type;
template <typename T>
auto table_ref(T t) -> T
auto make_table_ref(T t) -> T
{
return t;
}

View File

@ -76,6 +76,18 @@ namespace sqlpp
template <typename NameTagProvider>
struct cte_ref_t;
template <typename NameTagProvider, typename Statement, typename... FieldSpecs>
struct table_ref<cte_t<NameTagProvider, Statement, FieldSpecs...>>
{
using type = cte_ref_t<NameTagProvider>;
};
template <typename NameTagProvider, typename Statement, typename... FieldSpecs>
auto make_table_ref(cte_t<NameTagProvider, Statement, FieldSpecs...> /* unused */) -> cte_ref_t<NameTagProvider>
{
return {};
}
template <typename NameTagProvider, typename Statement, typename... FieldSpecs>
auto from_table(cte_t<NameTagProvider, Statement, FieldSpecs...> /*unused*/) -> cte_ref_t<NameTagProvider>
{
@ -97,6 +109,9 @@ namespace sqlpp
using value_type = value_type_of_t<FieldSpec>;
};
template<typename FieldSpec>
struct name_tag_of<cte_column_spec_t<FieldSpec>> : public name_tag_of<FieldSpec>{};
template <typename NameTagProvider, typename Statement, typename ResultRow>
struct make_cte_impl
{
@ -119,7 +134,7 @@ namespace sqlpp
template <typename NameTagProvider, typename FieldSpec>
struct cte_base
{
using type = member_t<FieldSpec, column_t<NameTagProvider, cte_column_spec_t<FieldSpec>>>;
using type = member_t<FieldSpec, column_t<cte_ref_t<NameTagProvider>, cte_column_spec_t<FieldSpec>>>;
};
template <typename Check, typename Union>
@ -262,6 +277,17 @@ namespace sqlpp
template<typename NameTagProvider>
struct is_table<cte_ref_t<NameTagProvider>> : public std::true_type{};
template<typename NameTagProvider>
struct provided_tables_of<cte_ref_t<NameTagProvider>>
{
using type = sqlpp::detail::type_vector<cte_ref_t<NameTagProvider>>;
};
template<typename NameTagProvider>
struct provided_static_tables_of<cte_ref_t<NameTagProvider>> : public provided_tables_of<cte_ref_t<NameTagProvider>>
{
};
template<typename NameTagProvider>
struct name_tag_of<cte_ref_t<NameTagProvider>> : public name_tag_of<NameTagProvider>{};

View File

@ -567,6 +567,14 @@ namespace sqlpp
using type = no_context_t;
};
template<typename T>
struct table_ref {
using type = T;
};
template<typename T>
using table_ref_t = typename table_ref<T>::type;
template <typename Db>
struct serializer_context_of_impl<Db, ::sqlpp::void_t<typename Db::_serializer_context_t>>
{

View File

@ -53,8 +53,7 @@ int main()
// CTE
SQLPP_COMPARE(from(x), " FROM x");
x.join(foo).on(x.id == foo.id);
//SQLPP_COMPARE(from(x.join(foo).on(x.id == foo.id)), " FROM x");
SQLPP_COMPARE(from(x.join(foo).on(x.id == foo.id)), " FROM x INNER JOIN tab_foo ON x.id = tab_foo.id");
#warning add tests for dynamic joins