0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Allow value_or_null to be assigned to parameters (#435)

This commit is contained in:
Roland Bock 2022-04-02 08:27:38 +02:00
parent 052c8a9e3c
commit 1d5caca523
4 changed files with 31 additions and 1 deletions

View File

@ -28,6 +28,7 @@
#define SQLPP11_DATA_TYPES_PARAMETER_VALUE_BASE_H #define SQLPP11_DATA_TYPES_PARAMETER_VALUE_BASE_H
#include <sqlpp11/data_types/parameter_value.h> #include <sqlpp11/data_types/parameter_value.h>
#include <sqlpp11/value_or_null.h>
namespace sqlpp namespace sqlpp
{ {
@ -53,6 +54,16 @@ namespace sqlpp
return *this; return *this;
} }
parameter_value_base& operator=(const value_or_null_t<DataType>& val)
{
if (val._is_null)
{
set_null();
return *this;
}
return operator=(val._value);
}
void set_null() void set_null()
{ {
_value = {}; _value = {};

View File

@ -27,8 +27,9 @@
#ifndef SQLPP11_VALUE_OR_NULL_H #ifndef SQLPP11_VALUE_OR_NULL_H
#define SQLPP11_VALUE_OR_NULL_H #define SQLPP11_VALUE_OR_NULL_H
#include <sqlpp11/wrap_operand.h> #include <sqlpp11/null.h>
#include <sqlpp11/type_traits.h> #include <sqlpp11/type_traits.h>
#include <sqlpp11/wrap_operand.h>
namespace sqlpp namespace sqlpp
{ {

View File

@ -95,5 +95,13 @@ int Insert(int, char*[])
db(insert_into(t).set(t.gamma = true, t.delta = 0)); db(insert_into(t).set(t.gamma = true, t.delta = 0));
db(insert_into(t).set(values())); 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::integer>(sqlpp::null);
prepared_insert.params.delta = sqlpp::value_or_null(17);
db(prepared_insert);
return 0; return 0;
} }

View File

@ -44,6 +44,16 @@ const auto tab = TabSample{};
void testPreparedStatementResult(sql::connection& db) 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::text>(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 preparedSelectAll = db.prepare(sqlpp::select(count(tab.alpha)).from(tab).unconditionally());
auto preparedUpdateAll = db.prepare(sqlpp::update(tab).set(tab.gamma = false).unconditionally()); auto preparedUpdateAll = db.prepare(sqlpp::update(tab).set(tab.gamma = false).unconditionally());