From 1d5caca52366f7db1e84d5e88a2931cb94caa3f1 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sat, 2 Apr 2022 08:27:38 +0200 Subject: [PATCH] Allow value_or_null to be assigned to parameters (#435) --- include/sqlpp11/data_types/parameter_value_base.h | 11 +++++++++++ include/sqlpp11/value_or_null.h | 3 ++- tests/core/usage/Insert.cpp | 8 ++++++++ tests/mysql/usage/Prepared.cpp | 10 ++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/include/sqlpp11/data_types/parameter_value_base.h b/include/sqlpp11/data_types/parameter_value_base.h index c7e103d5..1e45388e 100644 --- a/include/sqlpp11/data_types/parameter_value_base.h +++ b/include/sqlpp11/data_types/parameter_value_base.h @@ -28,6 +28,7 @@ #define SQLPP11_DATA_TYPES_PARAMETER_VALUE_BASE_H #include +#include namespace sqlpp { @@ -53,6 +54,16 @@ namespace sqlpp return *this; } + parameter_value_base& operator=(const value_or_null_t& val) + { + if (val._is_null) + { + set_null(); + return *this; + } + return operator=(val._value); + } + void set_null() { _value = {}; diff --git a/include/sqlpp11/value_or_null.h b/include/sqlpp11/value_or_null.h index 5360dac7..52c3f10d 100644 --- a/include/sqlpp11/value_or_null.h +++ b/include/sqlpp11/value_or_null.h @@ -27,8 +27,9 @@ #ifndef SQLPP11_VALUE_OR_NULL_H #define SQLPP11_VALUE_OR_NULL_H -#include +#include #include +#include namespace sqlpp { diff --git a/tests/core/usage/Insert.cpp b/tests/core/usage/Insert.cpp index 166133bc..f70ff1b3 100644 --- a/tests/core/usage/Insert.cpp +++ b/tests/core/usage/Insert.cpp @@ -95,5 +95,13 @@ int Insert(int, char*[]) db(insert_into(t).set(t.gamma = true, t.delta = 0)); db(insert_into(t).set(values())); + auto prepared_insert = db.prepare(insert_into(t).set(t.gamma = parameter(t.gamma), t.delta = parameter(t.delta))); + prepared_insert.params.gamma = true; + prepared_insert.params.delta = sqlpp::null; + prepared_insert.params.delta = 17; + prepared_insert.params.delta = sqlpp::value_or_null(sqlpp::null); + prepared_insert.params.delta = sqlpp::value_or_null(17); + db(prepared_insert); + return 0; } diff --git a/tests/mysql/usage/Prepared.cpp b/tests/mysql/usage/Prepared.cpp index dab86a69..d277ec81 100644 --- a/tests/mysql/usage/Prepared.cpp +++ b/tests/mysql/usage/Prepared.cpp @@ -44,6 +44,16 @@ const auto tab = TabSample{}; void testPreparedStatementResult(sql::connection& db) { + auto preparedInsert = db.prepare(insert_into(tab).set(tab.beta = parameter(tab.beta))); + preparedInsert.params.beta = sqlpp::null; + db(preparedInsert); + preparedInsert.params.beta = "17"; + db(preparedInsert); + preparedInsert.params.beta = sqlpp::value_or_null(sqlpp::null); + db(preparedInsert); + preparedInsert.params.beta = sqlpp::value_or_null("17"); + db(preparedInsert); + auto preparedSelectAll = db.prepare(sqlpp::select(count(tab.alpha)).from(tab).unconditionally()); auto preparedUpdateAll = db.prepare(sqlpp::update(tab).set(tab.gamma = false).unconditionally());