mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-16 12:51:13 +08:00
Add more WITH tests
This commit is contained in:
parent
aa6ea6c4f0
commit
f56f20cfc1
@ -183,9 +183,6 @@ namespace sqlpp
|
|||||||
struct cte_t : public cte_member<NameTagProvider, FieldSpecs>::type...,
|
struct cte_t : public cte_member<NameTagProvider, FieldSpecs>::type...,
|
||||||
public enable_join<cte_t<NameTagProvider, Statement, FieldSpecs...>>
|
public enable_join<cte_t<NameTagProvider, Statement, FieldSpecs...>>
|
||||||
{
|
{
|
||||||
#warning: Need to test this.
|
|
||||||
constexpr static bool _is_recursive = required_ctes_of_t<Statement>::template contains<cte_ref_t<NameTagProvider>>::value;
|
|
||||||
|
|
||||||
using _column_tuple_t = std::tuple<column_t<cte_ref_t<NameTagProvider>, FieldSpecs>...>;
|
using _column_tuple_t = std::tuple<column_t<cte_ref_t<NameTagProvider>, FieldSpecs>...>;
|
||||||
|
|
||||||
using _result_row_t = result_row_t<void, FieldSpecs...>;
|
using _result_row_t = result_row_t<void, FieldSpecs...>;
|
||||||
@ -257,6 +254,13 @@ namespace sqlpp
|
|||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename NameTagProvider, typename Statement, typename... ColumnSpecs>
|
||||||
|
struct is_recursive_cte<cte_t<NameTagProvider, Statement, ColumnSpecs...>> : public std::true_type
|
||||||
|
{
|
||||||
|
#warning: Need to test this.
|
||||||
|
constexpr static bool value = required_ctes_of_t<Statement>::template contains<cte_ref_t<NameTagProvider>>::value;
|
||||||
|
};
|
||||||
|
|
||||||
template <typename NameTagProvider, typename Statement, typename... ColumnSpecs>
|
template <typename NameTagProvider, typename Statement, typename... ColumnSpecs>
|
||||||
struct is_table<cte_t<NameTagProvider, Statement, ColumnSpecs...>> : public std::true_type
|
struct is_table<cte_t<NameTagProvider, Statement, ColumnSpecs...>> : public std::true_type
|
||||||
{
|
{
|
||||||
|
@ -42,8 +42,6 @@ namespace sqlpp
|
|||||||
template <typename... Expressions>
|
template <typename... Expressions>
|
||||||
struct with_data_t
|
struct with_data_t
|
||||||
{
|
{
|
||||||
using _is_recursive = logic::any<Expressions::_is_recursive...>;
|
|
||||||
|
|
||||||
with_data_t(Expressions... expressions) : _expressions(expressions...)
|
with_data_t(Expressions... expressions) : _expressions(expressions...)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -116,6 +114,7 @@ namespace sqlpp
|
|||||||
-> new_statement_t<consistent_t, typename Statement::_policies_t, no_with_t, with_t<Expressions...>>
|
-> new_statement_t<consistent_t, typename Statement::_policies_t, no_with_t, with_t<Expressions...>>
|
||||||
{
|
{
|
||||||
#warning: check that no cte refers to any of the ctes to the right
|
#warning: check that no cte refers to any of the ctes to the right
|
||||||
|
#warning: check that ctes have different names
|
||||||
return {statement, _data};
|
return {statement, _data};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -124,9 +123,9 @@ namespace sqlpp
|
|||||||
template <typename Context, typename... Expressions>
|
template <typename Context, typename... Expressions>
|
||||||
auto to_sql_string(Context& context, const with_data_t<Expressions...>& t) -> std::string
|
auto to_sql_string(Context& context, const with_data_t<Expressions...>& t) -> std::string
|
||||||
{
|
{
|
||||||
using T = with_data_t<Expressions...>;
|
static constexpr bool _is_recursive = logic::any<is_recursive_cte<Expressions>::value...>::value;
|
||||||
#warning : If there is a recursive CTE, add a "RECURSIVE" here
|
|
||||||
return std::string("WITH ") + (T::_is_recursive::value ? "RECURSIVE " : "") +
|
return std::string("WITH ") + (_is_recursive ? "RECURSIVE " : "") +
|
||||||
tuple_to_sql_string(context, t._expressions, tuple_operand{", "}) + " ";
|
tuple_to_sql_string(context, t._expressions, tuple_operand{", "}) + " ";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -595,4 +595,10 @@ namespace sqlpp
|
|||||||
struct is_cte : public std::false_type
|
struct is_cte : public std::false_type
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct is_recursive_cte : public std::false_type
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace sqlpp
|
} // namespace sqlpp
|
||||||
|
@ -35,13 +35,13 @@ int main(int, char* [])
|
|||||||
// No expression (not super useful).
|
// No expression (not super useful).
|
||||||
SQLPP_COMPARE(cte(sqlpp::alias::x), "x");
|
SQLPP_COMPARE(cte(sqlpp::alias::x), "x");
|
||||||
|
|
||||||
// WITH Simple CTE: X AS SELECT
|
// WITH simple CTE: X AS SELECT
|
||||||
{
|
{
|
||||||
const auto x = cte(sqlpp::alias::x).as(select(foo.id).from(foo).unconditionally());
|
const auto x = cte(sqlpp::alias::x).as(select(foo.id).from(foo).unconditionally());
|
||||||
SQLPP_COMPARE(with(x), "WITH x AS (SELECT tab_foo.id FROM tab_foo) ");
|
SQLPP_COMPARE(with(x), "WITH x AS (SELECT tab_foo.id FROM tab_foo) ");
|
||||||
}
|
}
|
||||||
|
|
||||||
// WITH Non-recursive union CTE: X AS SELECT ... UNION ALL SELECT ...
|
// WITH non-recursive union CTE: X AS SELECT ... UNION ALL SELECT ...
|
||||||
{
|
{
|
||||||
const auto x =
|
const auto x =
|
||||||
cte(sqlpp::alias::x)
|
cte(sqlpp::alias::x)
|
||||||
@ -49,7 +49,7 @@ int main(int, char* [])
|
|||||||
SQLPP_COMPARE(with(x), "WITH x AS (SELECT tab_foo.id FROM tab_foo UNION ALL SELECT tab_bar.id FROM tab_bar) ");
|
SQLPP_COMPARE(with(x), "WITH x AS (SELECT tab_foo.id FROM tab_foo UNION ALL SELECT tab_bar.id FROM tab_bar) ");
|
||||||
}
|
}
|
||||||
|
|
||||||
// WITH Recursive union CTE: X AS SELECT ... UNION ALL SELECT ... FROM X ...
|
// WITH recursive union CTE: X AS SELECT ... UNION ALL SELECT ... FROM X ...
|
||||||
{
|
{
|
||||||
const auto x_base = cte(sqlpp::alias::x).as(select(sqlpp::value(0).as(sqlpp::alias::a)));
|
const auto x_base = cte(sqlpp::alias::x).as(select(sqlpp::value(0).as(sqlpp::alias::a)));
|
||||||
const auto x = x_base.union_all(select((x_base.a + 1).as(sqlpp::alias::a)).from(x_base).where(x_base.a < 10));
|
const auto x = x_base.union_all(select((x_base.a + 1).as(sqlpp::alias::a)).from(x_base).where(x_base.a < 10));
|
||||||
@ -87,5 +87,26 @@ int main(int, char* [])
|
|||||||
SQLPP_COMPARE(with(x), "WITH RECURSIVE x AS (SELECT 0 AS a UNION ALL SELECT (x.a + 1) AS a FROM x WHERE x.a < 10) ");
|
SQLPP_COMPARE(with(x), "WITH RECURSIVE x AS (SELECT 0 AS a UNION ALL SELECT (x.a + 1) AS a FROM x WHERE x.a < 10) ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WITH two CTEs, no recursive
|
||||||
|
{
|
||||||
|
const auto x = cte(sqlpp::alias::x).as(select(foo.id).from(foo).unconditionally());
|
||||||
|
const auto y = cte(sqlpp::alias::y).as(select(foo.id).from(foo).unconditionally());
|
||||||
|
|
||||||
|
#warning: Need to test that CTEs have different names!
|
||||||
|
SQLPP_COMPARE(with(x, y), "WITH x AS (SELECT tab_foo.id FROM tab_foo), y AS (SELECT tab_foo.id FROM tab_foo) ");
|
||||||
|
SQLPP_COMPARE(with(y, x), "WITH y AS (SELECT tab_foo.id FROM tab_foo), x AS (SELECT tab_foo.id FROM tab_foo) ");
|
||||||
|
}
|
||||||
|
|
||||||
|
// WITH two CTEs, one of them recursive
|
||||||
|
{
|
||||||
|
const auto x_base = cte(sqlpp::alias::x).as(select(sqlpp::value(0).as(sqlpp::alias::a)));
|
||||||
|
const auto x = x_base.union_all(select((x_base.a + 1).as(sqlpp::alias::a)).from(x_base).where(x_base.a < 10));
|
||||||
|
const auto y = cte(sqlpp::alias::y).as(select(foo.id).from(foo).unconditionally());
|
||||||
|
|
||||||
|
#warning: Need to test that recursive CTEs are detected as being recursive.
|
||||||
|
SQLPP_COMPARE(with(x, y), "WITH RECURSIVE x AS (SELECT 0 AS a UNION ALL SELECT (x.a + 1) AS a FROM x WHERE x.a < 10), y AS (SELECT tab_foo.id FROM tab_foo) ");
|
||||||
|
SQLPP_COMPARE(with(y, x), "WITH RECURSIVE y AS (SELECT tab_foo.id FROM tab_foo), x AS (SELECT 0 AS a UNION ALL SELECT (x.a + 1) AS a FROM x WHERE x.a < 10) ");
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user