mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-01 06:59:42 -06:00
Added tests for schemata (#76)
This commit is contained in:
committed by
GitHub
parent
91e993ffdd
commit
e6b468af9b
@@ -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<decltype(_t)>;
|
||||
if constexpr (std::is_same_v<Type, dynamic::Table>) {
|
||||
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) + ")";
|
||||
|
||||
@@ -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<decltype(_t)>;
|
||||
if constexpr (std::is_same_v<Type, dynamic::Table>) {
|
||||
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) + ")";
|
||||
|
||||
26
tests/mysql/test_to_select_from_with_schema_dry.cpp
Normal file
26
tests/mysql/test_to_select_from_with_schema_dry.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/mysql.hpp>
|
||||
|
||||
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<uint32_t> id;
|
||||
std::optional<std::string> nullable;
|
||||
};
|
||||
|
||||
TEST(mysql, test_to_select_from_with_schema_dry) {
|
||||
const auto query = sqlgen::read<std::vector<TestTable>>;
|
||||
|
||||
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
|
||||
26
tests/postgres/test_to_select_from_with_schema_dry.cpp
Normal file
26
tests/postgres/test_to_select_from_with_schema_dry.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/postgres.hpp>
|
||||
|
||||
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<uint32_t> id;
|
||||
std::optional<std::string> nullable;
|
||||
};
|
||||
|
||||
TEST(postgres, test_to_select_from_with_schema_dry) {
|
||||
const auto query = sqlgen::read<std::vector<TestTable>>;
|
||||
|
||||
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
|
||||
27
tests/sqlite/test_to_select_from_with_schema.cpp
Normal file
27
tests/sqlite/test_to_select_from_with_schema.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
|
||||
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<uint32_t> id;
|
||||
std::optional<std::string> nullable;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_to_select_from_with_schema) {
|
||||
const auto select_from_stmt =
|
||||
sqlgen::transpilation::read_to_select_from<TestTable>();
|
||||
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
|
||||
Reference in New Issue
Block a user