7#include <boost/pfr/core.hpp>
10#include <userver/compiler/demangle.hpp>
12#include <userver/storages/postgres/exceptions.hpp>
13#include <userver/storages/postgres/io/buffer_io_base.hpp>
14#include <userver/storages/postgres/io/field_buffer.hpp>
15#include <userver/storages/postgres/io/row_types.hpp>
16#include <userver/storages/postgres/io/traits.hpp>
17#include <userver/storages/postgres/io/type_mapping.hpp>
18#include <userver/storages/postgres/io/type_traits.hpp>
19#include <userver/storages/postgres/io/user_types.hpp>
21USERVER_NAMESPACE_BEGIN
27void InitRecordParser();
30struct CompositeBinaryParser : BufferParserBase<T> {
31 using BaseType = BufferParserBase<T>;
33 using IndexSequence =
typename RowType::IndexSequence;
35 using BaseType::BaseType;
37 void operator()(
FieldBuffer buffer,
const TypeBufferCategory& categories) {
38 if constexpr (!
traits::kIsMappedToUserType<T>) {
42 Integer field_count{0};
45 if (field_count != RowType::size) {
49 ReadTuple(buffer, categories, RowType::GetTuple(
this->value), IndexSequence{});
53 static constexpr std::size_t int_size =
sizeof(Integer);
56 void ReadField(
FieldBuffer& buffer,
const TypeBufferCategory& categories, U& val)
const {
57 Integer field_type = 0;
59 auto elem_category = GetTypeBufferCategory(categories, field_type);
60 if (elem_category == BufferCategory::kNoParser) {
62 static_cast<Oid>(field_type),
compiler::GetTypeName<U>(),
compiler::GetTypeName<T>()};
64 buffer.ReadRaw(val, categories, elem_category);
67 template <
typename Tuple, std::size_t... Indexes>
69 ReadTuple(
FieldBuffer& buffer,
const TypeBufferCategory& categories, Tuple&& tuple, std::index_sequence<Indexes...>)
71 (ReadField(buffer, categories, std::get<Indexes>(std::forward<Tuple>(tuple))), ...);
76struct CompositeBinaryFormatter : BufferFormatterBase<T> {
77 using BaseType = BufferFormatterBase<T>;
78 using PgMapping = CppToPg<T>;
80 using IndexSequence =
typename RowType::IndexSequence;
81 static constexpr std::size_t size = RowType::size;
83 using BaseType::BaseType;
85 template <
typename Buffer>
86 void operator()(
const UserTypes& types, Buffer& buffer)
const {
87 const auto oid = PgMapping::GetOid(types);
89 auto msg =
"Type '" + kPgUserTypeName<T>.ToString() +
90 "' was not created in database and because of that the '" +
compiler::GetTypeName<T>() +
91 "' could not be serialized. Forgot a migration or rolled it "
92 "after the service started?";
96 const auto& type_desc = types.GetCompositeDescription(oid);
97 if (type_desc.Size() != size) {
98 throw CompositeSizeMismatch{type_desc.Size(), size, compiler::GetTypeName<T>()};
101 io::WriteBuffer(types, buffer,
static_cast<Integer>(size));
102 WriteTuple(types, type_desc, buffer, RowType::GetTuple(
this->value), IndexSequence{});
106 template <
typename Buffer,
typename U>
114 auto field_type = CppToPg<U>::GetOid(types);
115 const auto& field_desc = type_desc[index];
116 if (field_type != field_desc.type) {
117 if (io::MappedToSameType(
118 static_cast<PredefinedOids>(field_type),
119 static_cast<PredefinedOids>(types.FindDomainBaseOid(field_desc.type))
121 field_type = field_desc.type;
123 throw CompositeMemberTypeMismatch(
124 PgMapping::postgres_name.schema,
125 PgMapping::postgres_name.name,
132 io::WriteBuffer(types, buffer,
static_cast<Integer>(field_type));
133 io::WriteRawBinary(types, buffer, val, field_type);
135 template <
typename Buffer,
typename Tuple, std::size_t... Indexes>
139 (WriteField(types, type_desc, Indexes, buffer, std::get<Indexes>(std::forward<Tuple>(tuple))), ...);
149template <
typename Tuple>
150struct AssertTupleHasParsers;
152template <
typename... Members>
153struct AssertTupleHasParsers<std::tuple<Members...>> : std::true_type {
155 (HasParser<std::decay_t<Members>>::value && ...),
156 "No parser for member. Probably you forgot to include "
157 "file with parser or to define your own. Please see page "
158 "`uPg: Supported data types` for more information"
162template <
typename Tuple>
163struct AssertTupleHasFormatters;
165template <
typename... Members>
166struct AssertTupleHasFormatters<std::tuple<Members...>> : std::true_type {
168 (HasFormatter<std::decay_t<Members>>::value && ...),
169 "No formatter for member. Probably you forgot to "
170 "include file with formatter or to define your own. Please see page "
171 "`uPg: Supported data types` for more information"
176constexpr bool AssertHasCompositeParsers() {
177 io::
traits::AssertIsValidRowType<T>();
178 return AssertTupleHasParsers<
typename io::
RowType<T>::TupleType>::value;
182constexpr bool AssertHasCompositeFormatters() {
183 io::
traits::AssertIsValidRowType<T>();
184 return AssertTupleHasFormatters<
typename io::
RowType<T>::TupleType>::value;