Made sure that setting to NULL values works

This commit is contained in:
Dr. Patrick Urbanke
2025-12-26 10:55:30 +01:00
parent 3600ae8199
commit 943247cec3
8 changed files with 42 additions and 11 deletions
+3 -1
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;
};
+3 -4
View File
@@ -31,10 +31,9 @@ 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<remove_nullable_t<underlying_t<T, Col<_name>>>,
remove_nullable_t<underlying_t<T, Value<ToType>>>>,
"Must be convertible.");
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 {
return dynamic::Update::Set{
+13 -1
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>) {
+3
View File
@@ -145,6 +145,9 @@ std::string column_or_value_to_sql(
return "INTERVAL '" + std::to_string(_v.val) + " " +
rfl::enum_to_string(_v.unit) + "'";
} else if constexpr (std::is_same_v<Type, dynamic::Null>) {
return "NULL";
} else if constexpr (std::is_same_v<Type, dynamic::Timestamp>) {
return "to_timestamp(" + std::to_string(_v.seconds_since_unix) + ")";
+3
View File
@@ -175,6 +175,9 @@ std::string column_or_value_to_sql(
if constexpr (std::is_same_v<Type, dynamic::String>) {
return "'" + escape_single_quote(_v.val) + "'";
} else if constexpr (std::is_same_v<Type, dynamic::Null>) {
return "NULL";
} else if constexpr (std::is_same_v<Type, dynamic::Duration>) {
const auto unit =
_v.unit == dynamic::TimeUnit::milliseconds
+3
View File
@@ -134,6 +134,9 @@ std::string column_or_value_to_sql(
return "INTERVAL '" + std::to_string(_v.val) + " " +
rfl::enum_to_string(_v.unit) + "'";
} else if constexpr (std::is_same_v<Type, dynamic::Null>) {
return "NULL";
} else if constexpr (std::is_same_v<Type, dynamic::Timestamp>) {
return "to_timestamp(" + std::to_string(_v.seconds_since_unix) + ")";
+3
View File
@@ -136,6 +136,9 @@ std::string column_or_value_to_sql(
rfl::enum_to_string(_v.unit) + "'";
}
} else if constexpr (std::is_same_v<Type, dynamic::Null>) {
return "NULL";
} else if constexpr (std::is_same_v<Type, dynamic::Timestamp>) {
return std::to_string(_v.seconds_since_unix);
+11 -5
View File
@@ -33,16 +33,22 @@ TEST(sqlite, test_update_with_optional) {
using namespace sqlgen;
using namespace sqlgen::literals;
const auto query =
update<Person>("first_name"_c.set("last_name"_c), "age"_c.set(100)) |
where("first_name"_c == "Hugo");
const auto query1 = update<Person>("first_name"_c.set("last_name"_c),
"age"_c.set(std::nullopt)) |
where("first_name"_c == "Hugo");
query(conn).value();
const auto query2 =
update<Person>("age"_c.set(50)) | where("first_name"_c == "Homer");
const auto query3 = update<Person>("age"_c.set(std::optional<int>(11))) |
where("first_name"_c == "Bart");
query1(conn).and_then(query2).and_then(query3).value();
const auto people2 = sqlgen::read<std::vector<Person>>(conn).value();
const std::string expected =
R"([{"id":0,"first_name":"Homer","last_name":"Simpson","age":45},{"id":1,"first_name":"Bart","last_name":"Simpson","age":10},{"id":2,"first_name":"Lisa","last_name":"Simpson","age":8},{"id":3,"first_name":"Maggie","last_name":"Simpson","age":0},{"id":4,"first_name":"Simpson","last_name":"Simpson","age":100}])";
R"([{"id":0,"first_name":"Homer","last_name":"Simpson","age":50},{"id":1,"first_name":"Bart","last_name":"Simpson","age":11},{"id":2,"first_name":"Lisa","last_name":"Simpson","age":8},{"id":3,"first_name":"Maggie","last_name":"Simpson","age":0},{"id":4,"first_name":"Simpson","last_name":"Simpson"}])";
EXPECT_EQ(rfl::json::write(people2), expected);
}