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

Add tests for CTE

This commit is contained in:
Roland Bock 2024-10-27 11:25:43 +01:00
parent 980daac517
commit e5ad3efb43
2 changed files with 44 additions and 17 deletions

View File

@ -62,12 +62,7 @@ namespace sqlpp
template <typename Context, typename Flag, typename Lhs, typename Rhs>
auto to_sql_string(Context& context, const cte_union_t<Flag, Lhs, Rhs>& t) -> std::string
{
to_sql_string(context, t._lhs);
context << " UNION ";
to_sql_string(context, Flag{});
context << " ";
to_sql_string(context, t._rhs);
return context;
return to_sql_string(context, t._lhs) + " UNION " + to_sql_string(context, Flag{}) + to_sql_string(context, t._rhs);
}
template <typename NameTagProvider, typename Statement, typename... FieldSpecs>
@ -139,6 +134,7 @@ namespace sqlpp
struct cte_as_t : public cte_member<NewNameTagProvider, FieldSpecs>::type...,
public enable_join<cte_as_t<NameTagProvider, NewNameTagProvider, FieldSpecs...>>
{
using _column_tuple_t = std::tuple<column_t<cte_ref_t<NewNameTagProvider>, FieldSpecs>...>;
};
template <typename NameTagProvider, typename NewNameTagProvider, typename... ColumnSpecs>
@ -172,14 +168,8 @@ namespace sqlpp
struct cte_t : public cte_member<NameTagProvider, FieldSpecs>::type...,
public enable_join<cte_t<NameTagProvider, Statement, FieldSpecs...>>
{
#warning: remove
using _traits = make_traits<no_value_t, tag::is_cte>;
using _nodes = detail::type_vector<>;
using _provided_tables = detail::type_set<cte_t>;
using _required_ctes = detail::make_joined_set_t<required_ctes_of<Statement>, detail::type_set<NameTagProvider>>;
using _parameters = parameters_of<Statement>;
constexpr static bool _is_recursive = required_ctes_of<Statement>::template count<NameTagProvider>();
#warning: Need to test this.
constexpr static bool _is_recursive = required_ctes_of<Statement>::template count<cte_ref_t<NameTagProvider>>();
using _column_tuple_t = std::tuple<column_t<cte_ref_t<NameTagProvider>, FieldSpecs>...>;

View File

@ -30,15 +30,52 @@
int main(int, char* [])
{
const auto foo = test::TabFoo{};
const auto bar = test::TabBar{};
// No expression (not super useful).
SQLPP_COMPARE(cte(sqlpp::alias::x), "x");
// Select
SQLPP_COMPARE(cte(sqlpp::alias::x).as(select(foo.id).from(foo).unconditionally()), "x AS (SELECT tab_foo.id FROM tab_foo)");
// Simple CTE: X AS SELECT
{
const auto x = cte(sqlpp::alias::x).as(select(foo.id).from(foo).unconditionally());
const auto a = x.as(sqlpp::alias::a);
SQLPP_COMPARE(x, "x AS (SELECT tab_foo.id FROM tab_foo)");
SQLPP_COMPARE(make_table_ref(x), "x");
SQLPP_COMPARE(x.id, "x.id");
SQLPP_COMPARE(a, "x AS a");
SQLPP_COMPARE(a.id, "a.id");
SQLPP_COMPARE(all_of(x), "x.id");
SQLPP_COMPARE(all_of(a), "a.id");
}
// Non-recursive union CTE: X AS SELECT ... UNION ALL SELECT ...
{
const auto x =
cte(sqlpp::alias::x)
.as(select(foo.id).from(foo).unconditionally().union_all(select(bar.id).from(bar).unconditionally()));
const auto a = x.as(sqlpp::alias::a);
SQLPP_COMPARE(x, "x AS (SELECT tab_foo.id FROM tab_foo UNION ALL SELECT tab_bar.id FROM tab_bar)");
SQLPP_COMPARE(make_table_ref(x), "x");
SQLPP_COMPARE(x.id, "x.id");
SQLPP_COMPARE(a, "x AS a");
SQLPP_COMPARE(a.id, "a.id");
SQLPP_COMPARE(all_of(x), "x.id");
SQLPP_COMPARE(all_of(a), "a.id");
}
#warning: Add more tests
// 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 = x_base.union_all(select((x_base.a + 1).as(sqlpp::alias::a)).from(x_base).where(x_base.a < 10));
const auto y = x.as(sqlpp::alias::y);
SQLPP_COMPARE(x, "x AS (SELECT 0 AS a UNION ALL SELECT (x.a + 1) AS a FROM x WHERE x.a < 10)");
SQLPP_COMPARE(make_table_ref(x), "x");
SQLPP_COMPARE(x.a, "x.a");
SQLPP_COMPARE(y, "x AS y");
SQLPP_COMPARE(y.a, "y.a");
SQLPP_COMPARE(all_of(x), "x.a");
SQLPP_COMPARE(all_of(y), "y.a");
}
return 0;
}