From 13143fe7a6858ccf811e98cafffde1eb9c744ab3 Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Thu, 23 Oct 2025 22:41:47 +0200 Subject: [PATCH] Added tests for schemata --- src/sqlgen/mysql/to_sql.cpp | 3 +++ src/sqlgen/sqlite/to_sql.cpp | 3 +++ .../test_to_select_from_with_schema_dry.cpp | 26 ++++++++++++++++++ .../test_to_select_from_with_schema_dry.cpp | 26 ++++++++++++++++++ .../test_to_select_from_with_schema.cpp | 27 +++++++++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 tests/mysql/test_to_select_from_with_schema_dry.cpp create mode 100644 tests/postgres/test_to_select_from_with_schema_dry.cpp create mode 100644 tests/sqlite/test_to_select_from_with_schema.cpp diff --git a/src/sqlgen/mysql/to_sql.cpp b/src/sqlgen/mysql/to_sql.cpp index f10c9417..99614e49 100644 --- a/src/sqlgen/mysql/to_sql.cpp +++ b/src/sqlgen/mysql/to_sql.cpp @@ -829,6 +829,9 @@ std::string table_or_query_to_sql( return _table_or_query.visit([](const auto& _t) -> std::string { using Type = std::remove_cvref_t; if constexpr (std::is_same_v) { + if (_t.schema) { + return wrap_in_quotes(*_t.schema) + "." + wrap_in_quotes(_t.name); + } return wrap_in_quotes(_t.name); } else { return "(" + select_from_to_sql(*_t) + ")"; diff --git a/src/sqlgen/sqlite/to_sql.cpp b/src/sqlgen/sqlite/to_sql.cpp index 371e5ba8..900ed65d 100644 --- a/src/sqlgen/sqlite/to_sql.cpp +++ b/src/sqlgen/sqlite/to_sql.cpp @@ -712,6 +712,9 @@ std::string table_or_query_to_sql( return _table_or_query.visit([](const auto& _t) -> std::string { using Type = std::remove_cvref_t; if constexpr (std::is_same_v) { + if (_t.schema) { + return wrap_in_quotes(*_t.schema) + "." + wrap_in_quotes(_t.name); + } return wrap_in_quotes(_t.name); } else { return "(" + select_from_to_sql(*_t) + ")"; diff --git a/tests/mysql/test_to_select_from_with_schema_dry.cpp b/tests/mysql/test_to_select_from_with_schema_dry.cpp new file mode 100644 index 00000000..959cb5f0 --- /dev/null +++ b/tests/mysql/test_to_select_from_with_schema_dry.cpp @@ -0,0 +1,26 @@ +#include + +#include +#include + +namespace test_to_select_from_with_schema_dry { + +struct TestTable { + constexpr static const char* tablename = "test_table"; + constexpr static const char* schema = "my_schema"; + + std::string field1; + int32_t field2; + sqlgen::PrimaryKey id; + std::optional nullable; +}; + +TEST(mysql, test_to_select_from_with_schema_dry) { + const auto query = sqlgen::read>; + + const auto expected = + R"(SELECT `field1`, `field2`, `id`, `nullable` FROM `my_schema`.`test_table`)"; + + EXPECT_EQ(sqlgen::mysql::to_sql(query), expected); +} +} // namespace test_to_select_from_with_schema_dry diff --git a/tests/postgres/test_to_select_from_with_schema_dry.cpp b/tests/postgres/test_to_select_from_with_schema_dry.cpp new file mode 100644 index 00000000..216ada5a --- /dev/null +++ b/tests/postgres/test_to_select_from_with_schema_dry.cpp @@ -0,0 +1,26 @@ +#include + +#include +#include + +namespace test_to_select_from_with_schema_dry { + +struct TestTable { + constexpr static const char* tablename = "test_table"; + constexpr static const char* schema = "my_schema"; + + std::string field1; + int32_t field2; + sqlgen::PrimaryKey id; + std::optional nullable; +}; + +TEST(postgres, test_to_select_from_with_schema_dry) { + const auto query = sqlgen::read>; + + const auto expected = + R"(SELECT "field1", "field2", "id", "nullable" FROM "my_schema"."test_table")"; + + EXPECT_EQ(sqlgen::postgres::to_sql(query), expected); +} +} // namespace test_to_select_from_with_schema_dry diff --git a/tests/sqlite/test_to_select_from_with_schema.cpp b/tests/sqlite/test_to_select_from_with_schema.cpp new file mode 100644 index 00000000..38c097f0 --- /dev/null +++ b/tests/sqlite/test_to_select_from_with_schema.cpp @@ -0,0 +1,27 @@ +#include + +#include +#include + +namespace test_to_select_from_with_schema { + +struct TestTable { + constexpr static const char* tablename = "test_table"; + constexpr static const char* schema = "my_schema"; + + std::string field1; + int32_t field2; + sqlgen::PrimaryKey id; + std::optional nullable; +}; + +TEST(sqlite, test_to_select_from_with_schema) { + const auto select_from_stmt = + sqlgen::transpilation::read_to_select_from(); + const auto conn = sqlgen::sqlite::connect().value(); + const auto expected = + R"(SELECT "field1", "field2", "id", "nullable" FROM "my_schema"."test_table")"; + + EXPECT_EQ(conn->to_sql(select_from_stmt), expected); +} +} // namespace test_to_select_from_with_schema