From e6b468af9bd1278bdeb232dd4247c1dbece9509f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Patrick=20Urbanke=20=28=E5=8A=89=E8=87=AA=E6=88=90?= =?UTF-8?q?=29?= Date: Fri, 24 Oct 2025 08:14:34 +0200 Subject: [PATCH] Added tests for schemata (#76) --- 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 8799cc4..35ef659 100644 --- a/src/sqlgen/mysql/to_sql.cpp +++ b/src/sqlgen/mysql/to_sql.cpp @@ -833,6 +833,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 e4fd737..d38e687 100644 --- a/src/sqlgen/sqlite/to_sql.cpp +++ b/src/sqlgen/sqlite/to_sql.cpp @@ -716,6 +716,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 0000000..959cb5f --- /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 0000000..216ada5 --- /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 0000000..38c097f --- /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