Added to_type()

This commit is contained in:
Dr. Patrick Urbanke
2025-03-28 06:49:29 +01:00
parent 4fa34b94ec
commit 98e11993f0
2 changed files with 41 additions and 2 deletions

View File

@@ -11,7 +11,8 @@ namespace sqlgen::dynamic {
using Type =
rfl::TaggedUnion<"type", types::Unknown, types::Boolean, types::Float32,
types::Float64, types::Int8, types::Int16, types::Int32,
types::Int64, types::Text, types::Timestamp,
types::Int64, types::UInt8, types::UInt16, types::UInt32,
types::UInt64, types::Text, types::Timestamp,
types::TimestampWithTZ, types::VarChar>;
} // namespace sqlgen::dynamic

View File

@@ -24,7 +24,45 @@ std::string to_name() {
template <class FieldType>
dynamic::Type to_type() {
// TODO
using T = std::remove_cvref_t<typename FieldType::Type>;
if constexpr (std::is_same_v<T, bool>) {
return types::Boolean{};
} else if constexpr (std::is_integral_v<T> && std::is_signed_v<T>) {
if constexpr (sizeof(T) == 1) {
return types::Int8{};
} else if constexpr (sizeof(T) == 2) {
return types::Int16{};
} else if constexpr (sizeof(T) == 4) {
return types::Int32{};
} else if constexpr (sizeof(T) == 8) {
return types::Int64{};
} else {
static_assert(rfl::always_false_v<T>, "Unsupported signed integer.");
}
} else if constexpr (std::is_integral_v<T> && !std::is_signed_v<T>) {
if constexpr (sizeof(T) == 1) {
return types::UInt8{};
} else if constexpr (sizeof(T) == 2) {
return types::UInt16{};
} else if constexpr (sizeof(T) == 4) {
return types::UInt32{};
} else if constexpr (sizeof(T) == 8) {
return types::UInt64{};
} else {
static_assert(rfl::always_false_v<T>, "Unsupported unsigned integer.");
}
} else if constexpr (std::is_floating_point_v<T>) {
if constexpr (sizeof(T) == 4) {
return types::Float32{};
} else if constexpr (sizeof(T) == 8) {
return types::Float64{};
} else {
static_assert(rfl::always_false_v<T>,
"Unsupported floating point value.");
}
} else if constexpr (std::is_same_v<T, std::string>) {
return types::Text{};
}
}
template <class FieldType>