mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-16 12:51:13 +08:00
with working tests
This commit is contained in:
parent
7b4240e4f4
commit
b0a5059b59
@ -36,20 +36,20 @@ int As(int, char* [])
|
||||
const auto foo = test::TabFoo{};
|
||||
const auto bar = test::TabBar{};
|
||||
|
||||
compare(__LINE__, foo.omega.as(cheese), "tab_foo.omega AS cheese");
|
||||
compare(__LINE__, (foo.omega + 17).as(cheese), "(tab_foo.omega+17) AS cheese");
|
||||
compare(__LINE__, (foo.omega - 17).as(cheese), "(tab_foo.omega-17) AS cheese");
|
||||
compare(__LINE__, (foo.omega - uint32_t(17)).as(cheese), "(tab_foo.omega-17) AS cheese");
|
||||
compare(__LINE__, (foo.omega - bar.alpha).as(cheese), "(tab_foo.omega-tab_bar.alpha) AS cheese");
|
||||
compare(__LINE__, (count(foo.omega) - bar.alpha).as(cheese), "(COUNT(tab_foo.omega)-tab_bar.alpha) AS cheese");
|
||||
compare(__LINE__, (count(foo.omega) - uint32_t(17)).as(cheese), "(COUNT(tab_foo.omega)-17) AS cheese");
|
||||
compare(__LINE__, foo.omega.as(cheese), "\"tab_foo\".\"omega\" AS \"cheese\"");
|
||||
compare(__LINE__, (foo.omega + 17).as(cheese), "(\"tab_foo\".\"omega\"+17) AS \"cheese\"");
|
||||
compare(__LINE__, (foo.omega - 17).as(cheese), "(\"tab_foo\".\"omega\"-17) AS \"cheese\"");
|
||||
compare(__LINE__, (foo.omega - uint32_t(17)).as(cheese), "(\"tab_foo\".\"omega\"-17) AS \"cheese\"");
|
||||
compare(__LINE__, (foo.omega - bar.alpha).as(cheese), "(\"tab_foo\".\"omega\"-\"tab_bar\".\"alpha\") AS \"cheese\"");
|
||||
compare(__LINE__, (count(foo.omega) - bar.alpha).as(cheese), "(COUNT(\"tab_foo\".\"omega\")-\"tab_bar\".\"alpha\") AS \"cheese\"");
|
||||
compare(__LINE__, (count(foo.omega) - uint32_t(17)).as(cheese), "(COUNT(\"tab_foo\".\"omega\")-17) AS \"cheese\"");
|
||||
|
||||
// Auto alias
|
||||
compare(__LINE__, select(max(bar.alpha)), "SELECT MAX(tab_bar.alpha) AS max_");
|
||||
compare(__LINE__, select(max(bar.alpha).as(cheese)), "SELECT MAX(tab_bar.alpha) AS cheese");
|
||||
compare(__LINE__, select(max(bar.alpha)), "SELECT MAX(\"tab_bar\".\"alpha\") AS \"max_\"");
|
||||
compare(__LINE__, select(max(bar.alpha).as(cheese)), "SELECT MAX(\"tab_bar\".\"alpha\") AS \"cheese\"");
|
||||
compare(__LINE__, select(max(bar.alpha)).from(bar).unconditionally().as(cheese),
|
||||
"(SELECT MAX(tab_bar.alpha) AS max_ FROM tab_bar) AS cheese");
|
||||
compare(__LINE__, select(max(bar.alpha)).from(bar).unconditionally().as(cheese).max, "cheese.max_");
|
||||
"(SELECT MAX(\"tab_bar\".\"alpha\") AS \"max_\" FROM \"tab_bar\") AS \"cheese\"");
|
||||
compare(__LINE__, select(max(bar.alpha)).from(bar).unconditionally().as(cheese).max, "\"cheese\".\"max_\"");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -40,15 +40,15 @@ int CustomQuery(int, char* [])
|
||||
// Unconditionally
|
||||
compare(__LINE__, custom_query(sqlpp::select(), select_flags(sqlpp::distinct), select_columns(foo.omega), from(foo),
|
||||
sqlpp::unconditionally()),
|
||||
"SELECT DISTINCT tab_foo.omega FROM tab_foo ");
|
||||
"SELECT DISTINCT \"tab_foo\".\"omega\" FROM \"tab_foo\" ");
|
||||
|
||||
// A full select statement made individual clauses
|
||||
compare(__LINE__,
|
||||
custom_query(sqlpp::select(), select_flags(sqlpp::distinct), select_columns(foo.omega),
|
||||
from(foo.join(bar).on(foo.omega == bar.alpha)), where(bar.alpha > 17), group_by(foo.omega),
|
||||
having(avg(bar.alpha) > 19), order_by(foo.omega.asc()), sqlpp::limit(10u), sqlpp::offset(100u)),
|
||||
"SELECT DISTINCT tab_foo.omega FROM tab_foo INNER JOIN tab_bar ON (tab_foo.omega=tab_bar.alpha) WHERE "
|
||||
"(tab_bar.alpha>17) GROUP BY tab_foo.omega HAVING (AVG(tab_bar.alpha)>19) ORDER BY tab_foo.omega ASC "
|
||||
"SELECT DISTINCT \"tab_foo\".\"omega\" FROM \"tab_foo\" INNER JOIN \"tab_bar\" ON (\"tab_foo\".\"omega\"=\"tab_bar\".\"alpha\") WHERE "
|
||||
"(\"tab_bar\".\"alpha\">17) GROUP BY \"tab_foo\".\"omega\" HAVING (AVG(\"tab_bar\".\"alpha\")>19) ORDER BY \"tab_foo\".\"omega\" ASC "
|
||||
"LIMIT 10 OFFSET 100");
|
||||
|
||||
// A full select statement made individual clauses
|
||||
@ -59,9 +59,9 @@ int CustomQuery(int, char* [])
|
||||
dynamic_group_by(db, foo.omega), dynamic_having(db, avg(bar.alpha) > 19),
|
||||
dynamic_order_by(db, foo.omega.asc(), foo.psi.order(sqlpp::sort_type::desc)),
|
||||
sqlpp::dynamic_limit(db), sqlpp::dynamic_offset(db)),
|
||||
"SELECT DISTINCT tab_foo.omega FROM tab_foo INNER JOIN tab_bar ON (tab_foo.omega=tab_bar.alpha) WHERE "
|
||||
"(tab_bar.alpha>17) GROUP BY tab_foo.omega HAVING (AVG(tab_bar.alpha)>19) ORDER BY tab_foo.omega "
|
||||
"ASC,tab_foo.psi DESC ");
|
||||
"SELECT DISTINCT \"tab_foo\".\"omega\" FROM \"tab_foo\" INNER JOIN \"tab_bar\" ON (\"tab_foo\".\"omega\"=\"tab_bar\".\"alpha\") WHERE "
|
||||
"(\"tab_bar\".\"alpha\">17) GROUP BY \"tab_foo\".\"omega\" HAVING (AVG(\"tab_bar\".\"alpha\")>19) ORDER BY \"tab_foo\".\"omega\" "
|
||||
"ASC,\"tab_foo\".\"psi\" DESC ");
|
||||
|
||||
// A pragma query for sqlite
|
||||
compare(__LINE__,
|
||||
|
@ -41,86 +41,86 @@ int From(int, char* [])
|
||||
const auto cFoo = foo.as(sqlpp::alias::c);
|
||||
|
||||
// Single table
|
||||
compare(__LINE__, from(foo), " FROM tab_foo");
|
||||
compare(__LINE__, from(bar), " FROM tab_bar");
|
||||
compare(__LINE__, from(foo), " FROM \"tab_foo\"");
|
||||
compare(__LINE__, from(bar), " FROM \"tab_bar\"");
|
||||
|
||||
// Static joins
|
||||
compare(__LINE__, from(foo.cross_join(bar)), " FROM tab_foo CROSS JOIN tab_bar");
|
||||
compare(__LINE__, from(foo.cross_join(bar)), " FROM \"tab_foo\" CROSS JOIN \"tab_bar\"");
|
||||
compare(__LINE__, from(foo.join(bar).on(foo.omega > bar.alpha)),
|
||||
" FROM tab_foo INNER JOIN tab_bar ON (tab_foo.omega>tab_bar.alpha)");
|
||||
" FROM \"tab_foo\" INNER JOIN \"tab_bar\" ON (\"tab_foo\".\"omega\">\"tab_bar\".\"alpha\")");
|
||||
compare(__LINE__, from(foo.inner_join(bar).on(foo.omega > bar.alpha)),
|
||||
" FROM tab_foo INNER JOIN tab_bar ON (tab_foo.omega>tab_bar.alpha)");
|
||||
" FROM \"tab_foo\" INNER JOIN \"tab_bar\" ON (\"tab_foo\".\"omega\">\"tab_bar\".\"alpha\")");
|
||||
compare(__LINE__, from(foo.outer_join(bar).on(foo.omega > bar.alpha)),
|
||||
" FROM tab_foo OUTER JOIN tab_bar ON (tab_foo.omega>tab_bar.alpha)");
|
||||
" FROM \"tab_foo\" OUTER JOIN \"tab_bar\" ON (\"tab_foo\".\"omega\">\"tab_bar\".\"alpha\")");
|
||||
compare(__LINE__, from(foo.left_outer_join(bar).on(foo.omega > bar.alpha)),
|
||||
" FROM tab_foo LEFT OUTER JOIN tab_bar ON (tab_foo.omega>tab_bar.alpha)");
|
||||
" FROM \"tab_foo\" LEFT OUTER JOIN \"tab_bar\" ON (\"tab_foo\".\"omega\">\"tab_bar\".\"alpha\")");
|
||||
compare(__LINE__, from(foo.right_outer_join(bar).on(foo.omega > bar.alpha)),
|
||||
" FROM tab_foo RIGHT OUTER JOIN tab_bar ON (tab_foo.omega>tab_bar.alpha)");
|
||||
" FROM \"tab_foo\" RIGHT OUTER JOIN \"tab_bar\" ON (\"tab_foo\".\"omega\">\"tab_bar\".\"alpha\")");
|
||||
compare(__LINE__, from(aFoo.join(bFoo).on(aFoo.omega > bFoo.omega)),
|
||||
" FROM tab_foo AS a INNER JOIN tab_foo AS b ON (a.omega>b.omega)");
|
||||
" FROM \"tab_foo\" AS \"a\" INNER JOIN \"tab_foo\" AS \"b\" ON (\"a\".\"omega\">\"b\".\"omega\")");
|
||||
compare(
|
||||
__LINE__, from(aFoo.join(bFoo).on(aFoo.omega > bFoo.omega).join(cFoo).on(bFoo.omega > cFoo.omega)),
|
||||
" FROM tab_foo AS a INNER JOIN tab_foo AS b ON (a.omega>b.omega) INNER JOIN tab_foo AS c ON (b.omega>c.omega)");
|
||||
compare(__LINE__, from(foo.join(bar).unconditionally()), " FROM tab_foo INNER JOIN tab_bar");
|
||||
" FROM \"tab_foo\" AS \"a\" INNER JOIN \"tab_foo\" AS \"b\" ON (\"a\".\"omega\">\"b\".\"omega\") INNER JOIN \"tab_foo\" AS \"c\" ON (\"b\".\"omega\">\"c\".\"omega\")");
|
||||
compare(__LINE__, from(foo.join(bar).unconditionally()), " FROM \"tab_foo\" INNER JOIN \"tab_bar\"");
|
||||
|
||||
// Static joins involving verbatim tables
|
||||
compare(__LINE__, from(aFoo.join(sqlpp::verbatim_table("unknown_table"))
|
||||
.on(aFoo.omega > sqlpp::verbatim<sqlpp::floating_point>("unknown_table.column_x"))),
|
||||
" FROM tab_foo AS a INNER JOIN unknown_table ON (a.omega>unknown_table.column_x)");
|
||||
" FROM \"tab_foo\" AS \"a\" INNER JOIN unknown_table ON (\"a\".\"omega\">unknown_table.column_x)");
|
||||
compare(__LINE__, from(sqlpp::verbatim_table("unknown_table")
|
||||
.join(aFoo)
|
||||
.on(aFoo.omega > sqlpp::verbatim<sqlpp::floating_point>("unknown_table.column_x"))),
|
||||
" FROM unknown_table INNER JOIN tab_foo AS a ON (a.omega>unknown_table.column_x)");
|
||||
" FROM unknown_table INNER JOIN \"tab_foo\" AS \"a\" ON (\"a\".\"omega\">unknown_table.column_x)");
|
||||
compare(__LINE__, from(sqlpp::verbatim_table("unknown_table")
|
||||
.as(sqlpp::alias::a)
|
||||
.join(sqlpp::verbatim_table("another_table"))
|
||||
.on(sqlpp::verbatim<sqlpp::boolean>("a.column_x>another_table.x"))),
|
||||
" FROM unknown_table AS a INNER JOIN another_table ON a.column_x>another_table.x");
|
||||
" FROM unknown_table AS \"a\" INNER JOIN another_table ON a.column_x>another_table.x");
|
||||
|
||||
// Dynamic joins
|
||||
const auto df = dynamic_from(db, foo);
|
||||
compare(__LINE__, df, " FROM tab_foo");
|
||||
compare(__LINE__, df, " FROM \"tab_foo\"");
|
||||
{
|
||||
auto dfa = df;
|
||||
dfa.from.add(dynamic_cross_join(bar));
|
||||
compare(__LINE__, dfa, " FROM tab_foo CROSS JOIN tab_bar");
|
||||
compare(__LINE__, dfa, " FROM \"tab_foo\" CROSS JOIN \"tab_bar\"");
|
||||
}
|
||||
{
|
||||
auto dfa = df;
|
||||
dfa.from.add(dynamic_join(bar).on(bar.alpha > foo.omega));
|
||||
compare(__LINE__, dfa, " FROM tab_foo INNER JOIN tab_bar ON (tab_bar.alpha>tab_foo.omega)");
|
||||
compare(__LINE__, dfa, " FROM \"tab_foo\" INNER JOIN \"tab_bar\" ON (\"tab_bar\".\"alpha\">\"tab_foo\".\"omega\")");
|
||||
}
|
||||
{
|
||||
auto dfa = df;
|
||||
dfa.from.add(dynamic_inner_join(bar).on(bar.alpha > foo.omega));
|
||||
compare(__LINE__, dfa, " FROM tab_foo INNER JOIN tab_bar ON (tab_bar.alpha>tab_foo.omega)");
|
||||
compare(__LINE__, dfa, " FROM \"tab_foo\" INNER JOIN \"tab_bar\" ON (\"tab_bar\".\"alpha\">\"tab_foo\".\"omega\")");
|
||||
}
|
||||
{
|
||||
auto dfa = df;
|
||||
dfa.from.add(dynamic_outer_join(bar).on(bar.alpha > foo.omega));
|
||||
compare(__LINE__, dfa, " FROM tab_foo OUTER JOIN tab_bar ON (tab_bar.alpha>tab_foo.omega)");
|
||||
compare(__LINE__, dfa, " FROM \"tab_foo\" OUTER JOIN \"tab_bar\" ON (\"tab_bar\".\"alpha\">\"tab_foo\".\"omega\")");
|
||||
}
|
||||
{
|
||||
auto dfa = df;
|
||||
dfa.from.add(dynamic_left_outer_join(bar).on(bar.alpha > foo.omega));
|
||||
compare(__LINE__, dfa, " FROM tab_foo LEFT OUTER JOIN tab_bar ON (tab_bar.alpha>tab_foo.omega)");
|
||||
compare(__LINE__, dfa, " FROM \"tab_foo\" LEFT OUTER JOIN \"tab_bar\" ON (\"tab_bar\".\"alpha\">\"tab_foo\".\"omega\")");
|
||||
}
|
||||
{
|
||||
auto dfa = df;
|
||||
dfa.from.add(dynamic_right_outer_join(bar).on(bar.alpha > foo.omega));
|
||||
compare(__LINE__, dfa, " FROM tab_foo RIGHT OUTER JOIN tab_bar ON (tab_bar.alpha>tab_foo.omega)");
|
||||
compare(__LINE__, dfa, " FROM \"tab_foo\" RIGHT OUTER JOIN \"tab_bar\" ON (\"tab_bar\".\"alpha\">\"tab_foo\".\"omega\")");
|
||||
}
|
||||
{
|
||||
auto dfa = df;
|
||||
dfa.from.add(dynamic_join(bar).unconditionally());
|
||||
compare(__LINE__, dfa, " FROM tab_foo INNER JOIN tab_bar");
|
||||
compare(__LINE__, dfa, " FROM \"tab_foo\" INNER JOIN \"tab_bar\"");
|
||||
}
|
||||
{
|
||||
auto dfa = df;
|
||||
dfa.from.add(dynamic_inner_join(bar).on(bar.alpha > foo.omega));
|
||||
dfa.from.add(dynamic_outer_join(aFoo).on(without_table_check(bar.alpha > aFoo.omega)));
|
||||
compare(__LINE__, dfa, " FROM tab_foo INNER JOIN tab_bar ON (tab_bar.alpha>tab_foo.omega) OUTER JOIN tab_foo AS a "
|
||||
"ON (tab_bar.alpha>a.omega)");
|
||||
compare(__LINE__, dfa, " FROM \"tab_foo\" INNER JOIN \"tab_bar\" ON (\"tab_bar\".\"alpha\">\"tab_foo\".\"omega\") OUTER JOIN \"tab_foo\" AS \"a\" "
|
||||
"ON (\"tab_bar\".\"alpha\">\"a\".\"omega\")");
|
||||
}
|
||||
|
||||
// Dynamic joins involving verbatim table
|
||||
@ -129,7 +129,7 @@ int From(int, char* [])
|
||||
dfa.from.add(
|
||||
dynamic_inner_join(sqlpp::verbatim_table("unknown_table"))
|
||||
.on(without_table_check(bar.alpha > sqlpp::verbatim<sqlpp::floating_point>("unknown_table.column_x"))));
|
||||
compare(__LINE__, dfa, " FROM tab_foo INNER JOIN unknown_table ON (tab_bar.alpha>unknown_table.column_x)");
|
||||
compare(__LINE__, dfa, " FROM \"tab_foo\" INNER JOIN unknown_table ON (\"tab_bar\".\"alpha\">unknown_table.column_x)");
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -44,17 +44,17 @@ int In(int, char* [])
|
||||
const auto bar = test::TabBar{};
|
||||
|
||||
// Individual values
|
||||
compare(__LINE__, foo.omega.in(17), "tab_foo.omega IN(17)");
|
||||
compare(__LINE__, foo.omega.in(17, bar.alpha), "tab_foo.omega IN(17,tab_bar.alpha)");
|
||||
compare(__LINE__, foo.omega.in(17, bar.alpha, sqlpp::value(19)), "tab_foo.omega IN(17,tab_bar.alpha,19)");
|
||||
compare(__LINE__, foo.omega.in(17), "\"tab_foo\".\"omega\" IN(17)");
|
||||
compare(__LINE__, foo.omega.in(17, bar.alpha), "\"tab_foo\".\"omega\" IN(17,\"tab_bar\".\"alpha\")");
|
||||
compare(__LINE__, foo.omega.in(17, bar.alpha, sqlpp::value(19)), "\"tab_foo\".\"omega\" IN(17,\"tab_bar\".\"alpha\",19)");
|
||||
|
||||
// Lists
|
||||
compare(__LINE__, foo.omega.in(sqlpp::value_list(std::vector<float>{1.7f, 2.5f, 17.f, 0.f})),
|
||||
"tab_foo.omega IN(1.7,2.5,17,0)");
|
||||
"\"tab_foo\".\"omega\" IN(1.7,2.5,17,0)");
|
||||
|
||||
// Sub select
|
||||
compare(__LINE__, foo.omega.in(select(bar.alpha).from(bar).unconditionally()),
|
||||
"tab_foo.omega IN(SELECT tab_bar.alpha FROM tab_bar)");
|
||||
"\"tab_foo\".\"omega\" IN(SELECT \"tab_bar\".\"alpha\" FROM \"tab_bar\")");
|
||||
|
||||
// Empty lists (not normally covered by SQL)
|
||||
compare(__LINE__, foo.omega.in(), getFalse());
|
||||
|
@ -42,9 +42,9 @@ int Insert(int, char* [])
|
||||
{
|
||||
const auto bar = test::TabBar{};
|
||||
|
||||
compare(__LINE__, insert_into(bar).default_values(), "INSERT INTO tab_bar DEFAULT VALUES");
|
||||
compare(__LINE__, insert_into(bar).default_values(), "INSERT INTO \"tab_bar\" DEFAULT VALUES");
|
||||
compare(__LINE__, insert_into(bar).set(bar.beta = "cheesecake", bar.gamma = true),
|
||||
"INSERT INTO tab_bar (beta,gamma) VALUES('cheesecake'," + getTrue() + ")");
|
||||
"INSERT INTO \"tab_bar\" (\"beta\",\"gamma\") VALUES('cheesecake'," + getTrue() + ")");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -35,9 +35,9 @@ int TableAlias(int, char* [])
|
||||
const auto bar = test::TabBar{};
|
||||
|
||||
// Individual values
|
||||
compare(__LINE__, foo.as(bar), "tab_foo AS tab_bar");
|
||||
compare(__LINE__, foo.as(bar), "\"tab_foo\" AS \"tab_bar\"");
|
||||
compare(__LINE__, select(foo.omega).from(foo).unconditionally().as(bar),
|
||||
"(SELECT tab_foo.omega FROM tab_foo) AS tab_bar");
|
||||
"(SELECT \"tab_foo\".\"omega\" FROM \"tab_foo\") AS \"tab_bar\"");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -50,18 +50,18 @@ int Where(int, char* [])
|
||||
const auto bar = test::TabBar{};
|
||||
|
||||
// Unconditionally
|
||||
compare(__LINE__, select(foo.omega).from(foo).unconditionally(), "SELECT tab_foo.omega FROM tab_foo");
|
||||
compare(__LINE__, remove_from(foo).unconditionally(), "DELETE FROM tab_foo");
|
||||
compare(__LINE__, update(foo).set(foo.omega = 42).unconditionally(), "UPDATE tab_foo SET omega=42");
|
||||
compare(__LINE__, select(foo.omega).from(foo).unconditionally(), "SELECT \"tab_foo\".\"omega\" FROM \"tab_foo\"");
|
||||
compare(__LINE__, remove_from(foo).unconditionally(), "DELETE FROM \"tab_foo\"");
|
||||
compare(__LINE__, update(foo).set(foo.omega = 42).unconditionally(), "UPDATE \"tab_foo\" SET \"omega\"=42");
|
||||
compare(__LINE__, where(sqlpp::value(true)), " WHERE " + getTrue());
|
||||
|
||||
// Never
|
||||
compare(__LINE__, where(sqlpp::value(false)), " WHERE " + getFalse());
|
||||
|
||||
// Sometimes
|
||||
compare(__LINE__, where(bar.gamma), " WHERE tab_bar.gamma");
|
||||
compare(__LINE__, where(bar.gamma == false), " WHERE (tab_bar.gamma=" + getFalse() + ")");
|
||||
compare(__LINE__, where(bar.beta == "SQL"), " WHERE (tab_bar.beta='SQL')");
|
||||
compare(__LINE__, where(bar.gamma), " WHERE \"tab_bar\".\"gamma\"");
|
||||
compare(__LINE__, where(bar.gamma == false), " WHERE (\"tab_bar\".\"gamma\"=" + getFalse() + ")");
|
||||
compare(__LINE__, where(bar.beta == "SQL"), " WHERE (\"tab_bar\".\"beta\"='SQL')");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user