Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/sqlgen/mysql/to_sql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) + ")";
Expand Down
3 changes: 3 additions & 0 deletions src/sqlgen/sqlite/to_sql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) + ")";
Expand Down
26 changes: 26 additions & 0 deletions tests/mysql/test_to_select_from_with_schema_dry.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 26 additions & 0 deletions tests/postgres/test_to_select_from_with_schema_dry.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 27 additions & 0 deletions tests/sqlite/test_to_select_from_with_schema.cpp
Original file line number Diff line number Diff line change
@@ -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
Loading