9#include <userver/utils/assert.hpp>
10#include <userver/utils/optional_ref.hpp>
12#include <userver/storages/postgres/io/buffer_io.hpp>
13#include <userver/storages/postgres/io/buffer_io_base.hpp>
14#include <userver/storages/postgres/io/nullable_traits.hpp>
15#include <userver/storages/postgres/io/traits.hpp>
17#include <boost/optional/optional_fwd.hpp>
19USERVER_NAMESPACE_BEGIN
25template <
template <
typename>
class Optional,
typename T,
26 bool Categories =
false>
27struct OptionalValueParser : BufferParserBase<Optional<T>> {
28 using BaseType = BufferParserBase<Optional<T>>;
29 using ValueParser =
typename traits::
IO<T>::ParserType;
31 using BaseType::BaseType;
35 ValueParser{val}(buffer);
36 this->value = std::move(val);
40template <
template <
typename>
class Optional,
typename T>
41struct OptionalValueParser<Optional, T,
true> : BufferParserBase<Optional<T>> {
42 using BaseType = BufferParserBase<Optional<T>>;
43 using ValueParser =
typename traits::IO<T>::ParserType;
45 using BaseType::BaseType;
47 void operator()(
const FieldBuffer& buffer,
48 const TypeBufferCategory& categories) {
50 ValueParser{val}(buffer, categories);
51 this->value = std::move(val);
55template <
template <
typename>
class Optional,
typename T>
56struct OptionalValueFormatter : BufferFormatterBase<Optional<T>> {
57 using BaseType = BufferFormatterBase<Optional<T>>;
58 using ValueFormatter =
typename traits::
IO<T>::FormatterType;
60 using BaseType::BaseType;
62 template <
typename Buffer>
63 void operator()(
const UserTypes& types, Buffer& buffer)
const {
65 ValueFormatter{*
this->value}(types, buffer);
145 using ValueType = boost::optional<T>;
146 inline static bool IsNull(
const ValueType& v) {
return !v; }
147 inline static void SetNull(ValueType& v) { v = ValueType{}; }
148 inline static void SetDefault(ValueType& v) { v.emplace(); }
166 using ValueType = std::optional<T>;
167 inline static bool IsNull(
const ValueType& v) {
return !v; }
168 inline static void SetNull(ValueType& v) { v = std::nullopt; }
169 inline static void SetDefault(ValueType& v) { v.emplace(); }
187 using ValueType = USERVER_NAMESPACE::utils::
OptionalRef<T>;
188 inline static bool IsNull(
const ValueType& v) {
return !v; }
189 inline static void SetNull(ValueType&) {
190 static_assert(!
sizeof(T),
"SetNull not enabled for utils::OptionalRef");
192 inline static void SetDefault(ValueType&) {
193 static_assert(!
sizeof(T),
"SetDefault not enabled for utils::OptionalRef");