mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-05 17:09:50 -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};
|
||||
|
||||
Reference in New Issue
Block a user