Added support for joins (#25)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2025-07-15 08:55:34 +02:00
committed by GitHub
parent f95156df77
commit b283ccf388
48 changed files with 2108 additions and 308 deletions

View File

@@ -23,7 +23,7 @@ TEST(postgres, test_is_null_dry) {
order_by("first_name"_c.desc()));
const std::string expected =
R"(SELECT "id", "first_name", "last_name", "age" FROM "Person" WHERE "age" IS NULL ORDER BY "first_name" DESC;)";
R"(SELECT "id", "first_name", "last_name", "age" FROM "Person" WHERE "age" IS NULL ORDER BY "first_name" DESC)";
EXPECT_EQ(sql, expected);
}

View File

@@ -0,0 +1,64 @@
#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY
#include <gtest/gtest.h>
#include <ranges>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <sqlgen.hpp>
#include <sqlgen/postgres.hpp>
#include <vector>
namespace test_join {
struct Person {
sqlgen::PrimaryKey<uint32_t> id;
std::string first_name;
std::string last_name;
double age;
};
TEST(postgres, test_join) {
static_assert(std::ranges::input_range<sqlgen::Range<Person>>,
"Must be an input range.");
const auto people1 = std::vector<Person>(
{Person{
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
Person{.id = 1, .first_name = "Bart", .last_name = "Simpson", .age = 10},
Person{.id = 2, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
Person{
.id = 3, .first_name = "Maggie", .last_name = "Simpson", .age = 0}});
const auto credentials = sqlgen::postgres::Credentials{.user = "postgres",
.password = "password",
.host = "localhost",
.dbname = "postgres"};
using namespace sqlgen;
const auto get_people =
select_from<Person, "t1">(
"id"_t1 | as<"id">, "first_name"_t1 | as<"first_name">,
"last_name"_t2 | as<"last_name">, "age"_t2 | as<"age">) |
left_join<Person, "t2">("id"_t1 == "id"_t2) | order_by("id"_t1) |
to<std::vector<Person>>;
const auto people = postgres::connect(credentials)
.and_then(drop<Person> | if_exists)
.and_then(write(std::ref(people1)))
.and_then(get_people)
.value();
const std::string expected_query =
R"(SELECT t1."id" AS "id", t1."first_name" AS "first_name", t2."last_name" AS "last_name", t2."age" AS "age" FROM "Person" t1 LEFT JOIN "Person" t2 ON t1."id" = t2."id" ORDER BY t1."id")";
const std::string expected =
R"([{"id":0,"first_name":"Homer","last_name":"Simpson","age":45.0},{"id":1,"first_name":"Bart","last_name":"Simpson","age":10.0},{"id":2,"first_name":"Lisa","last_name":"Simpson","age":8.0},{"id":3,"first_name":"Maggie","last_name":"Simpson","age":0.0}])";
EXPECT_EQ(postgres::to_sql(get_people), expected_query);
EXPECT_EQ(rfl::json::write(people), expected);
}
} // namespace test_join
#endif

View File

@@ -0,0 +1,94 @@
#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY
#include <gtest/gtest.h>
#include <ranges>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <sqlgen.hpp>
#include <sqlgen/postgres.hpp>
#include <vector>
namespace test_joins_nested {
struct Person {
sqlgen::PrimaryKey<uint32_t> id;
std::string first_name;
std::string last_name;
double age;
};
struct Relationship {
sqlgen::PrimaryKey<uint32_t> parent_id;
sqlgen::PrimaryKey<uint32_t> child_id;
};
TEST(postgres, test_joins_nested) {
const auto people1 = std::vector<Person>(
{Person{
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
Person{
.id = 1, .first_name = "Marge", .last_name = "Simpson", .age = 40},
Person{.id = 2, .first_name = "Bart", .last_name = "Simpson", .age = 10},
Person{.id = 3, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
Person{
.id = 4, .first_name = "Maggie", .last_name = "Simpson", .age = 0}});
const auto relationships =
std::vector<Relationship>({Relationship{.parent_id = 0, .child_id = 2},
Relationship{.parent_id = 0, .child_id = 3},
Relationship{.parent_id = 0, .child_id = 4},
Relationship{.parent_id = 1, .child_id = 2},
Relationship{.parent_id = 1, .child_id = 3},
Relationship{.parent_id = 1, .child_id = 4}});
const auto credentials = sqlgen::postgres::Credentials{.user = "postgres",
.password = "password",
.host = "localhost",
.dbname = "postgres"};
using namespace sqlgen;
struct ParentAndChild {
std::string last_name;
std::string first_name_parent;
std::string first_name_child;
double parent_age_at_birth;
};
const auto get_children =
select_from<Relationship, "t1">("parent_id"_t1 | as<"id">,
"first_name"_t2 | as<"first_name">,
"age"_t2 | as<"age">) |
left_join<Person, "t2">("id"_t2 == "child_id"_t1);
const auto get_people =
select_from<Person, "t1">(
"last_name"_t1 | as<"last_name">,
"first_name"_t1 | as<"first_name_parent">,
"first_name"_t2 | as<"first_name_child">,
("age"_t1 - "age"_t2) | as<"parent_age_at_birth">) |
inner_join<"t2">(get_children, "id"_t1 == "id"_t2) |
order_by("id"_t1, "id"_t2) | to<std::vector<ParentAndChild>>;
const auto people = postgres::connect(credentials)
.and_then(drop<Person> | if_exists)
.and_then(drop<Relationship> | if_exists)
.and_then(write(std::ref(people1)))
.and_then(write(std::ref(relationships)))
.and_then(get_people)
.value();
const std::string expected_query =
R"(SELECT t1."last_name" AS "last_name", t1."first_name" AS "first_name_parent", t2."first_name" AS "first_name_child", (t1."age") - (t2."age") AS "parent_age_at_birth" FROM "Person" t1 INNER JOIN (SELECT t1."parent_id" AS "id", t2."first_name" AS "first_name", t2."age" AS "age" FROM "Relationship" t1 LEFT JOIN "Person" t2 ON t2."id" = t1."child_id") t2 ON t1."id" = t2."id" ORDER BY t1."id", t2."id")";
const std::string expected =
R"([{"last_name":"Simpson","first_name_parent":"Homer","first_name_child":"Bart","parent_age_at_birth":35.0},{"last_name":"Simpson","first_name_parent":"Homer","first_name_child":"Lisa","parent_age_at_birth":37.0},{"last_name":"Simpson","first_name_parent":"Homer","first_name_child":"Maggie","parent_age_at_birth":45.0},{"last_name":"Simpson","first_name_parent":"Marge","first_name_child":"Bart","parent_age_at_birth":30.0},{"last_name":"Simpson","first_name_parent":"Marge","first_name_child":"Lisa","parent_age_at_birth":32.0},{"last_name":"Simpson","first_name_parent":"Marge","first_name_child":"Maggie","parent_age_at_birth":40.0}])";
EXPECT_EQ(postgres::to_sql(get_people), expected_query);
EXPECT_EQ(rfl::json::write(people), expected);
}
} // namespace test_joins_nested
#endif

View File

@@ -0,0 +1,93 @@
#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY
#include <gtest/gtest.h>
#include <ranges>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <sqlgen.hpp>
#include <sqlgen/postgres.hpp>
#include <vector>
namespace test_joins_nested_grouped {
struct Person {
sqlgen::PrimaryKey<uint32_t> id;
std::string first_name;
std::string last_name;
double age;
};
struct Relationship {
sqlgen::PrimaryKey<uint32_t> parent_id;
sqlgen::PrimaryKey<uint32_t> child_id;
};
TEST(postgres, test_joins_nested) {
const auto people1 = std::vector<Person>(
{Person{
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
Person{
.id = 1, .first_name = "Marge", .last_name = "Simpson", .age = 40},
Person{.id = 2, .first_name = "Bart", .last_name = "Simpson", .age = 10},
Person{.id = 3, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
Person{
.id = 4, .first_name = "Maggie", .last_name = "Simpson", .age = 0}});
const auto relationships =
std::vector<Relationship>({Relationship{.parent_id = 0, .child_id = 2},
Relationship{.parent_id = 0, .child_id = 3},
Relationship{.parent_id = 0, .child_id = 4},
Relationship{.parent_id = 1, .child_id = 2},
Relationship{.parent_id = 1, .child_id = 3},
Relationship{.parent_id = 1, .child_id = 4}});
const auto credentials = sqlgen::postgres::Credentials{.user = "postgres",
.password = "password",
.host = "localhost",
.dbname = "postgres"};
using namespace sqlgen;
struct ParentAndChild {
std::string last_name;
std::string first_name_child;
double avg_parent_age_at_birth;
};
const auto get_children =
select_from<Relationship, "t1">("parent_id"_t1 | as<"id">,
"first_name"_t2 | as<"first_name">,
"age"_t2 | as<"age">) |
left_join<Person, "t2">("id"_t2 == "child_id"_t1);
const auto get_people =
select_from<Person, "t1">(
"last_name"_t1 | as<"last_name">,
"first_name"_t2 | as<"first_name_child">,
avg("age"_t1 - "age"_t2) | as<"avg_parent_age_at_birth">) |
inner_join<"t2">(get_children, "id"_t1 == "id"_t2) |
group_by("last_name"_t1, "first_name"_t2) | order_by("first_name"_t2) |
to<std::vector<ParentAndChild>>;
const auto people = postgres::connect(credentials)
.and_then(drop<Person> | if_exists)
.and_then(drop<Relationship> | if_exists)
.and_then(write(std::ref(people1)))
.and_then(write(std::ref(relationships)))
.and_then(get_people)
.value();
const std::string expected_query =
R"(SELECT t1."last_name" AS "last_name", t2."first_name" AS "first_name_child", AVG((t1."age") - (t2."age")) AS "avg_parent_age_at_birth" FROM "Person" t1 INNER JOIN (SELECT t1."parent_id" AS "id", t2."first_name" AS "first_name", t2."age" AS "age" FROM "Relationship" t1 LEFT JOIN "Person" t2 ON t2."id" = t1."child_id") t2 ON t1."id" = t2."id" GROUP BY t1."last_name", t2."first_name" ORDER BY t2."first_name")";
const std::string expected =
R"([{"last_name":"Simpson","first_name_child":"Bart","avg_parent_age_at_birth":32.5},{"last_name":"Simpson","first_name_child":"Lisa","avg_parent_age_at_birth":34.5},{"last_name":"Simpson","first_name_child":"Maggie","avg_parent_age_at_birth":42.5}])";
EXPECT_EQ(postgres::to_sql(get_people), expected_query);
EXPECT_EQ(rfl::json::write(people), expected);
}
} // namespace test_joins_nested_grouped
#endif

View File

@@ -0,0 +1,88 @@
#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY
#include <gtest/gtest.h>
#include <ranges>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <sqlgen.hpp>
#include <sqlgen/postgres.hpp>
#include <vector>
namespace test_joins_two_tables {
struct Person {
sqlgen::PrimaryKey<uint32_t> id;
std::string first_name;
std::string last_name;
double age;
};
struct Relationship {
sqlgen::PrimaryKey<uint32_t> parent_id;
sqlgen::PrimaryKey<uint32_t> child_id;
};
TEST(postgres, test_joins_two_tables) {
const auto people1 = std::vector<Person>(
{Person{
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
Person{
.id = 1, .first_name = "Marge", .last_name = "Simpson", .age = 40},
Person{.id = 2, .first_name = "Bart", .last_name = "Simpson", .age = 10},
Person{.id = 3, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
Person{
.id = 4, .first_name = "Maggie", .last_name = "Simpson", .age = 0}});
const auto relationships =
std::vector<Relationship>({Relationship{.parent_id = 0, .child_id = 2},
Relationship{.parent_id = 0, .child_id = 3},
Relationship{.parent_id = 0, .child_id = 4},
Relationship{.parent_id = 1, .child_id = 2},
Relationship{.parent_id = 1, .child_id = 3},
Relationship{.parent_id = 1, .child_id = 4}});
const auto credentials = sqlgen::postgres::Credentials{.user = "postgres",
.password = "password",
.host = "localhost",
.dbname = "postgres"};
using namespace sqlgen;
struct ParentAndChild {
std::string last_name;
std::string first_name_parent;
std::string first_name_child;
double parent_age_at_birth;
};
const auto get_people =
select_from<Person, "t1">(
"last_name"_t1 | as<"last_name">,
"first_name"_t1 | as<"first_name_parent">,
"first_name"_t3 | as<"first_name_child">,
("age"_t1 - "age"_t3) | as<"parent_age_at_birth">) |
inner_join<Relationship, "t2">("id"_t1 == "parent_id"_t2) |
left_join<Person, "t3">("id"_t3 == "child_id"_t2) |
order_by("id"_t1, "id"_t3) | to<std::vector<ParentAndChild>>;
const auto people = postgres::connect(credentials)
.and_then(drop<Person> | if_exists)
.and_then(drop<Relationship> | if_exists)
.and_then(write(std::ref(people1)))
.and_then(write(std::ref(relationships)))
.and_then(get_people)
.value();
const std::string expected_query =
R"(SELECT t1."last_name" AS "last_name", t1."first_name" AS "first_name_parent", t3."first_name" AS "first_name_child", (t1."age") - (t3."age") AS "parent_age_at_birth" FROM "Person" t1 INNER JOIN "Relationship" t2 ON t1."id" = t2."parent_id" LEFT JOIN "Person" t3 ON t3."id" = t2."child_id" ORDER BY t1."id", t3."id")";
const std::string expected =
R"([{"last_name":"Simpson","first_name_parent":"Homer","first_name_child":"Bart","parent_age_at_birth":35.0},{"last_name":"Simpson","first_name_parent":"Homer","first_name_child":"Lisa","parent_age_at_birth":37.0},{"last_name":"Simpson","first_name_parent":"Homer","first_name_child":"Maggie","parent_age_at_birth":45.0},{"last_name":"Simpson","first_name_parent":"Marge","first_name_child":"Bart","parent_age_at_birth":30.0},{"last_name":"Simpson","first_name_parent":"Marge","first_name_child":"Lisa","parent_age_at_birth":32.0},{"last_name":"Simpson","first_name_parent":"Marge","first_name_child":"Maggie","parent_age_at_birth":40.0}])";
EXPECT_EQ(postgres::to_sql(get_people), expected_query);
EXPECT_EQ(rfl::json::write(people), expected);
}
} // namespace test_joins_two_tables
#endif

View File

@@ -0,0 +1,89 @@
#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY
#include <gtest/gtest.h>
#include <ranges>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <sqlgen.hpp>
#include <sqlgen/postgres.hpp>
#include <vector>
namespace test_joins_two_tables_grouped {
struct Person {
sqlgen::PrimaryKey<uint32_t> id;
std::string first_name;
std::string last_name;
double age;
};
struct Relationship {
sqlgen::PrimaryKey<uint32_t> parent_id;
sqlgen::PrimaryKey<uint32_t> child_id;
};
TEST(postgres, test_joins_two_tables_grouped) {
const auto people1 = std::vector<Person>(
{Person{
.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
Person{
.id = 1, .first_name = "Marge", .last_name = "Simpson", .age = 40},
Person{.id = 2, .first_name = "Bart", .last_name = "Simpson", .age = 10},
Person{.id = 3, .first_name = "Lisa", .last_name = "Simpson", .age = 8},
Person{
.id = 4, .first_name = "Maggie", .last_name = "Simpson", .age = 0}});
const auto relationships =
std::vector<Relationship>({Relationship{.parent_id = 0, .child_id = 2},
Relationship{.parent_id = 0, .child_id = 3},
Relationship{.parent_id = 0, .child_id = 4},
Relationship{.parent_id = 1, .child_id = 2},
Relationship{.parent_id = 1, .child_id = 3},
Relationship{.parent_id = 1, .child_id = 4}});
const auto credentials = sqlgen::postgres::Credentials{.user = "postgres",
.password = "password",
.host = "localhost",
.dbname = "postgres"};
using namespace sqlgen;
struct ParentAndChild {
std::string last_name;
std::string first_name_child;
double avg_parent_age_at_birth;
};
const auto get_people =
select_from<Person, "t1">(
"last_name"_t1 | as<"last_name">,
"first_name"_t3 | as<"first_name_child">,
avg("age"_t1 - "age"_t3) | as<"avg_parent_age_at_birth">) |
inner_join<Relationship, "t2">("id"_t1 == "parent_id"_t2) |
left_join<Person, "t3">("id"_t3 == "child_id"_t2) |
group_by("last_name"_t1, "first_name"_t3) |
order_by("last_name"_t1, "first_name"_t3) |
to<std::vector<ParentAndChild>>;
const auto people = postgres::connect(credentials)
.and_then(drop<Person> | if_exists)
.and_then(drop<Relationship> | if_exists)
.and_then(write(std::ref(people1)))
.and_then(write(std::ref(relationships)))
.and_then(get_people)
.value();
const std::string expected_query =
R"(SELECT t1."last_name" AS "last_name", t3."first_name" AS "first_name_child", AVG((t1."age") - (t3."age")) AS "avg_parent_age_at_birth" FROM "Person" t1 INNER JOIN "Relationship" t2 ON t1."id" = t2."parent_id" LEFT JOIN "Person" t3 ON t3."id" = t2."child_id" GROUP BY t1."last_name", t3."first_name" ORDER BY t1."last_name", t3."first_name")";
const std::string expected =
R"([{"last_name":"Simpson","first_name_child":"Bart","avg_parent_age_at_birth":32.5},{"last_name":"Simpson","first_name_child":"Lisa","avg_parent_age_at_birth":34.5},{"last_name":"Simpson","first_name_child":"Maggie","avg_parent_age_at_birth":42.5}])";
EXPECT_EQ(postgres::to_sql(get_people), expected_query);
EXPECT_EQ(rfl::json::write(people), expected);
}
} // namespace test_joins_two_tables_grouped
#endif

View File

@@ -23,7 +23,7 @@ TEST(postgres, test_like_dry) {
where("first_name"_c.like("H%")) | order_by("age"_c));
const std::string expected =
R"(SELECT "id", "first_name", "last_name", "age" FROM "Person" WHERE "first_name" LIKE 'H%' ORDER BY "age";)";
R"(SELECT "id", "first_name", "last_name", "age" FROM "Person" WHERE "first_name" LIKE 'H%' ORDER BY "age")";
EXPECT_EQ(sql, expected);
}

View File

@@ -75,7 +75,7 @@ TEST(postgres, test_range_select_from_with_timestamps) {
.value();
const std::string expected_query =
R"(SELECT "birthday" + INTERVAL '10 days' AS "birthday", cast((cast(extract(YEAR from "birthday") as TEXT) || '-' || cast(extract(MONTH from "birthday") as TEXT) || '-' || cast(extract(DAY from "birthday") as TEXT)) as TIMESTAMP) + INTERVAL '10 days' AS "birthday_recreated", cast('2011-01-01' as DATE) - cast("birthday" as DATE) AS "age_in_days", extract(EPOCH FROM "birthday" + INTERVAL '10 days') AS "birthday_unixepoch", extract(HOUR from "birthday") AS "hour", extract(MINUTE from "birthday") AS "minute", extract(SECOND from "birthday") AS "second", extract(DOW from "birthday") AS "weekday" FROM "Person" ORDER BY "id";)";
R"(SELECT "birthday" + INTERVAL '10 days' AS "birthday", cast((cast(extract(YEAR from "birthday") as TEXT) || '-' || cast(extract(MONTH from "birthday") as TEXT) || '-' || cast(extract(DAY from "birthday") as TEXT)) as TIMESTAMP) + INTERVAL '10 days' AS "birthday_recreated", cast('2011-01-01' as DATE) - cast("birthday" as DATE) AS "age_in_days", extract(EPOCH FROM "birthday" + INTERVAL '10 days') AS "birthday_unixepoch", extract(HOUR from "birthday") AS "hour", extract(MINUTE from "birthday") AS "minute", extract(SECOND from "birthday") AS "second", extract(DOW from "birthday") AS "weekday" FROM "Person" ORDER BY "id")";
const std::string expected =
R"([{"birthday":"1970-01-11","birthday_recreated":"1970-01-11","birthday_unixepoch":864000,"age_in_days":14975.0,"hour":0,"minute":0,"second":0,"weekday":4},{"birthday":"2000-01-11","birthday_recreated":"2000-01-11","birthday_unixepoch":947548800,"age_in_days":4018.0,"hour":0,"minute":0,"second":0,"weekday":6},{"birthday":"2002-01-11","birthday_recreated":"2002-01-11","birthday_unixepoch":1010707200,"age_in_days":3287.0,"hour":0,"minute":0,"second":0,"weekday":2},{"birthday":"2010-01-11","birthday_recreated":"2010-01-11","birthday_unixepoch":1263168000,"age_in_days":365.0,"hour":0,"minute":0,"second":0,"weekday":5}])";

View File

@@ -24,7 +24,7 @@ TEST(postgres, test_to_select_from2_dry) {
order_by("field1"_c) | limit(10);
const auto expected =
R"(SELECT "field1" AS "field", AVG("field2") AS "avg_field2", "nullable" AS "nullable_field", 1 AS "one", 'hello' AS "hello" FROM "TestTable" WHERE "id" > 0 GROUP BY "field1", "nullable" ORDER BY "field1" LIMIT 10;)";
R"(SELECT "field1" AS "field", AVG("field2") AS "avg_field2", "nullable" AS "nullable_field", 1 AS "one", 'hello' AS "hello" FROM "TestTable" WHERE "id" > 0 GROUP BY "field1", "nullable" ORDER BY "field1" LIMIT 10)";
EXPECT_EQ(sqlgen::postgres::to_sql(query), expected);
}

View File

@@ -16,7 +16,7 @@ TEST(postgres, test_to_select_from_dry) {
const auto query = sqlgen::read<std::vector<TestTable>>;
const auto expected =
R"(SELECT "field1", "field2", "id", "nullable" FROM "TestTable";)";
R"(SELECT "field1", "field2", "id", "nullable" FROM "TestTable")";
EXPECT_EQ(sqlgen::postgres::to_sql(query), expected);
}

View File

@@ -21,7 +21,7 @@ TEST(postgres, test_where_dry) {
order_by("age"_c);
const auto expected =
R"(SELECT "id", "first_name", "last_name", "age" FROM "Person" WHERE ("age" < 18) AND ("first_name" != 'Hugo') ORDER BY "age";)";
R"(SELECT "id", "first_name", "last_name", "age" FROM "Person" WHERE ("age" < 18) AND ("first_name" != 'Hugo') ORDER BY "age")";
EXPECT_EQ(sqlgen::postgres::to_sql(query), expected);
}