3#include <userver/storages/postgres/exceptions.hpp>
4#include <userver/storages/postgres/io/buffer_io.hpp>
5#include <userver/storages/postgres/io/integral_types.hpp>
6#include <userver/storages/postgres/io/nullable_traits.hpp>
14 const auto* new_buffer_start = buffer + offset;
15 if (offset > length) {
16 throw InvalidInputBufferSize(
17 length,
". Offset requested " + std::to_string(offset));
19 size = size == npos ? length - offset : size;
20 if (offset + size > length) {
21 throw InvalidInputBufferSize(
22 size,
". Buffer remaininig size is " + std::to_string(length - offset));
27 return {is_null, cat, size, new_buffer_start};
32 io::ReadBuffer(GetSubBuffer(0, len, cat), std::forward<T>(value));
39std::size_t
FieldBuffer::Read(T&& value,
const TypeBufferCategory& categories,
41 io::ReadBuffer(GetSubBuffer(0, len, cat), std::forward<T>(value), categories);
49 const TypeBufferCategory& categories,
51 using ValueType = std::decay_t<T>;
52 Integer field_length{0};
56 traits::GetSetNull<ValueType>::SetNull(std::forward<T>(value));
58 }
else if (field_length < 0) {
60 throw InvalidInputBufferSize(0,
"Negative buffer size value");
61 }
else if (field_length == 0) {
62 traits::GetSetNull<ValueType>::SetDefault(std::forward<T>(value));
65 return consumed + Read(value, categories, field_length, cat);
70std::size_t ReadRawBinary(
FieldBuffer buffer, T& value,
71 const TypeBufferCategory& categories) {
72 return buffer.ReadRaw(value, categories);
77template <
typename T,
typename Buffer,
78 typename Enable = USERVER_NAMESPACE::utils::void_t<>>
79struct FormatterAcceptsReplacementOid : std::false_type {};
81template <
typename T,
typename Buffer>
82struct FormatterAcceptsReplacementOid<
84 USERVER_NAMESPACE::utils::void_t<
decltype(std::declval<T&>()(
85 std::declval<
const UserTypes&>(), std::declval<Buffer&>(),
86 std::declval<Oid>()))>> : std::true_type {};
90template <
typename T,
typename Buffer>
91void WriteRawBinary(
const UserTypes& types, Buffer& buffer,
const T& value,
92 [[maybe_unused]] Oid replace_oid = kInvalidOid) {
93 traits::CheckFormatter<T>();
94 static constexpr auto size_len =
sizeof(Integer);
98 using BufferFormatter =
typename traits::
IO<T>::FormatterType;
99 using AcceptsReplacementOid =
100 detail::FormatterAcceptsReplacementOid<BufferFormatter, Buffer>;
101 auto len_start = buffer.size();
102 buffer.resize(buffer.size() + size_len);
103 auto size_before = buffer.size();
104 if constexpr (AcceptsReplacementOid{}) {
105 BufferFormatter{value}(types, buffer, replace_oid);
107 io::WriteBuffer(types, buffer, value);
109 Integer bytes = buffer.size() - size_before;
110 BufferWriter(bytes)(buffer.begin() + len_start);