Better support for nullable values in UPDATE...SET, resolves #108 (#111)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2025-12-26 12:51:23 +01:00
committed by GitHub
parent 0ea4d461d4
commit c8f4ad8d84
12 changed files with 310 additions and 4 deletions

View File

@@ -25,6 +25,8 @@ struct Integer {
int64_t val;
};
struct Null {};
struct String {
std::string val;
};
@@ -35,7 +37,7 @@ struct Timestamp {
struct Value {
using ReflectionType = rfl::TaggedUnion<"type", Duration, Boolean, Float,
Integer, String, Timestamp>;
Integer, Null, String, Timestamp>;
const auto& reflection() const { return val; }
ReflectionType val;
};

View File

@@ -15,6 +15,7 @@
#include "all_columns_exist.hpp"
#include "get_schema.hpp"
#include "get_tablename.hpp"
#include "remove_nullable_t.hpp"
#include "to_condition.hpp"
#include "to_sets.hpp"
#include "to_value.hpp"
@@ -30,8 +31,8 @@ struct ToSet<T, Set<transpilation::Col<_name>, ToType>> {
static_assert(
all_columns_exist<T, transpilation::Col<_name>>(),
"At least one column referenced in your SET query does not exist.");
static_assert(std::is_convertible_v<underlying_t<T, Col<_name>>,
underlying_t<T, Value<ToType>>>,
static_assert(std::is_convertible_v<underlying_t<T, Value<ToType>>,
underlying_t<T, Col<_name>>>,
"Must be convertible.");
dynamic::Update::Set operator()(const auto& _set) const {

View File

@@ -4,9 +4,11 @@
#include <rfl.hpp>
#include <type_traits>
#include "../dynamic/Type.hpp"
#include "../dynamic/Value.hpp"
#include "Value.hpp"
#include "has_reflection_method.hpp"
#include "is_nullable.hpp"
namespace sqlgen::transpilation {
@@ -17,7 +19,17 @@ template <class T>
struct ToValue {
dynamic::Value operator()(const T& _t) const {
using Type = std::remove_cvref_t<T>;
if constexpr (std::is_floating_point_v<Type>) {
if constexpr (is_nullable_v<Type>) {
if (!_t) {
return dynamic::Value{dynamic::Null{}};
}
return ToValue<std::remove_cvref_t<decltype(*_t)>>{}(*_t);
} else if constexpr (std::is_same_v<Type, std::nullopt_t> ||
std::is_same_v<Type, std::nullptr_t>) {
return dynamic::Value{dynamic::Null{}};
} else if constexpr (std::is_floating_point_v<Type>) {
return dynamic::Value{dynamic::Float{.val = static_cast<double>(_t)}};
} else if constexpr (std::is_same_v<Type, bool>) {