mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-06 09:30:07 -06:00
Added support for schemata
This commit is contained in:
@@ -11,7 +11,7 @@ namespace sqlgen::dynamic {
|
||||
struct Table {
|
||||
std::string alias;
|
||||
std::string name;
|
||||
std::string schema = "public";
|
||||
std::string schema;
|
||||
};
|
||||
|
||||
} // namespace sqlgen::dynamic
|
||||
|
||||
24
include/sqlgen/parsing/get_schema.hpp
Normal file
24
include/sqlgen/parsing/get_schema.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef SQLGEN_PARSING_GET_SCHEMA_HPP_
|
||||
#define SQLGEN_PARSING_GET_SCHEMA_HPP_
|
||||
|
||||
#include <rfl.hpp>
|
||||
#include <type_traits>
|
||||
|
||||
#include "has_schema.hpp"
|
||||
|
||||
namespace sqlgen::parsing {
|
||||
|
||||
template <class T>
|
||||
std::string get_schema() noexcept {
|
||||
using Type = std::remove_cvref_t<T>;
|
||||
if constexpr (has_schema<Type>) {
|
||||
using LiteralType = typename Type::schema;
|
||||
return LiteralType().str();
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sqlgen::parsing
|
||||
|
||||
#endif
|
||||
@@ -25,7 +25,7 @@ std::string get_tablename() noexcept {
|
||||
using Type = std::remove_cvref_t<T>;
|
||||
if constexpr (has_tablename<Type>) {
|
||||
using LiteralType = typename Type::tablename;
|
||||
return internal::remove_namespaces(LiteralType().str());
|
||||
return LiteralType().str();
|
||||
} else {
|
||||
return internal::remove_namespaces(rfl::type_name_t<Type>().str());
|
||||
}
|
||||
|
||||
15
include/sqlgen/parsing/has_schema.hpp
Normal file
15
include/sqlgen/parsing/has_schema.hpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef SQLGEN_PARSING_HAS_SCHEMA_HPP_
|
||||
#define SQLGEN_PARSING_HAS_SCHEMA_HPP_
|
||||
|
||||
#include <concepts>
|
||||
|
||||
namespace sqlgen::parsing {
|
||||
|
||||
template <typename T>
|
||||
concept has_schema = requires() {
|
||||
{ typename T::schema() } -> std::same_as<typename T::schema>;
|
||||
};
|
||||
|
||||
} // namespace sqlgen::parsing
|
||||
|
||||
#endif
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "../dynamic/Table.hpp"
|
||||
#include "../dynamic/Type.hpp"
|
||||
#include "../dynamic/types.hpp"
|
||||
#include "get_schema.hpp"
|
||||
#include "get_tablename.hpp"
|
||||
#include "has_reflection_method.hpp"
|
||||
#include "is_nullable.hpp"
|
||||
@@ -122,7 +123,8 @@ dynamic::CreateTable to_create_table() {
|
||||
using NamedTupleType = rfl::named_tuple_t<std::remove_cvref_t<T>>;
|
||||
using Fields = typename NamedTupleType::Fields;
|
||||
return dynamic::CreateTable{
|
||||
.table = dynamic::Table{.name = get_tablename<T>()},
|
||||
.table =
|
||||
dynamic::Table{.name = get_tablename<T>(), .schema = get_schema<T>()},
|
||||
.columns = internal::make_columns<Fields>(
|
||||
std::make_integer_sequence<int, rfl::tuple_size_v<Fields>>()),
|
||||
.if_not_exists = true};
|
||||
|
||||
22
tests/test_schema.cpp
Normal file
22
tests/test_schema.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/parsing/to_create_table.hpp>
|
||||
|
||||
namespace test_schema {
|
||||
|
||||
struct TestTable {
|
||||
using schema = sqlgen::Literal<"test">;
|
||||
|
||||
std::string field1;
|
||||
int32_t field2;
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::optional<std::string> nullable;
|
||||
};
|
||||
|
||||
TEST(general, test_schema) {
|
||||
const auto create_table_stmt = sqlgen::parsing::to_create_table<TestTable>();
|
||||
|
||||
EXPECT_EQ(create_table_stmt.table.schema, "test");
|
||||
}
|
||||
} // namespace test_schema
|
||||
@@ -3,19 +3,22 @@
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/parsing/to_create_table.hpp>
|
||||
|
||||
TEST(general, test_to_create_table) {
|
||||
struct TestTable {
|
||||
std::string field1;
|
||||
int32_t field2;
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::optional<std::string> nullable;
|
||||
};
|
||||
namespace test_to_create_table {
|
||||
|
||||
struct TestTable {
|
||||
std::string field1;
|
||||
int32_t field2;
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::optional<std::string> nullable;
|
||||
};
|
||||
|
||||
TEST(general, test_to_create_table) {
|
||||
const auto create_table_stmt = sqlgen::parsing::to_create_table<TestTable>();
|
||||
|
||||
const auto get_properties = [](const auto& _t) { return _t.properties; };
|
||||
|
||||
EXPECT_EQ(create_table_stmt.table.name, "TestTable");
|
||||
EXPECT_EQ(create_table_stmt.table.schema, "");
|
||||
|
||||
EXPECT_EQ(create_table_stmt.columns.size(), 4);
|
||||
|
||||
@@ -33,3 +36,4 @@ TEST(general, test_to_create_table) {
|
||||
EXPECT_EQ(create_table_stmt.columns.at(3).type.visit(get_properties).nullable,
|
||||
true);
|
||||
}
|
||||
} // namespace test_to_create_table
|
||||
|
||||
Reference in New Issue
Block a user