0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 21:04:15 +08:00
sqlpp11/tests/core/serialize/clause/cte.cpp
2024-10-27 11:25:43 +01:00

82 lines
3.3 KiB
C++

/*
* Copyright (c) 2024, 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.
*/
#include "Sample.h"
#include "../compare.h"
#include <sqlpp11/sqlpp11.h>
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");
// 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");
}
// 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;
}