userver: userver/storages/postgres/io/transform_io.hpp Source File
Loading...
Searching...
No Matches
transform_io.hpp
1#pragma once
2
3#include <userver/storages/postgres/io/buffer_io.hpp>
4#include <userver/storages/postgres/io/buffer_io_base.hpp>
5#include <userver/storages/postgres/io/field_buffer.hpp>
6#include <userver/storages/postgres/io/type_traits.hpp>
7#include <userver/storages/postgres/io/user_types.hpp>
8
9#include <userver/utils/assert.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace storages::postgres::io {
14
15namespace detail {
16
17template <typename UserType, typename WireType, typename Converter, bool Categories>
18struct TransformParserBase : BufferParserBase<UserType> {
19 using BaseType = BufferParserBase<UserType>;
20 using BaseType::BaseType;
21
22 void operator()(const FieldBuffer& buffer) {
23 WireType tmp;
24 io::ReadBuffer(buffer, tmp);
25 this->value = Converter{}(tmp);
26 }
27};
28
29template <typename UserType, typename WireType, typename Converter>
30struct TransformParserBase<UserType, WireType, Converter, true> : BufferParserBase<UserType> {
31 using BaseType = BufferParserBase<UserType>;
32 using BaseType::BaseType;
33
34 void operator()(const FieldBuffer& buffer, const TypeBufferCategory& categories) {
35 WireType tmp;
36 io::ReadBuffer(buffer, tmp, categories);
37 this->value = Converter{}(tmp);
38 }
39};
40
41} // namespace detail
42
43template <typename UserType, typename WireType, typename Converter>
44struct TransformParser
45 : detail::TransformParserBase<UserType, WireType, Converter, detail::kParserRequiresTypeCategories<WireType>> {
46 using BaseType =
47 detail::TransformParserBase<UserType, WireType, Converter, detail::kParserRequiresTypeCategories<WireType>>;
48 using BaseType::BaseType;
49};
50
51template <typename UserType, typename WireType, typename Converter>
52struct TransformFormatter : detail::BufferFormatterBase<UserType> {
53 using BaseType = detail::BufferFormatterBase<UserType>;
54 using BaseType::BaseType;
55
56 template <typename Buffer>
57 void operator()(const UserTypes& types, Buffer& buffer) const {
58 if constexpr (traits::kIsMappedToSystemType<UserType> && traits::kIsMappedToSystemType<WireType>) {
59 static_assert(
60 CppToPg<UserType>::type_oid == CppToPg<WireType>::type_oid,
61 "Database type mismatch in TransformFormatter"
62 );
63 }
65 CppToPg<UserType>::GetOid(types) == CppToPg<WireType>::GetOid(types),
66 "Database type mismatch in TransformFormatter"
67 );
68
69 WireType tmp = Converter{}(this->value);
70 io::WriteBuffer(types, buffer, tmp);
71 }
72};
73} // namespace storages::postgres::io
74
75USERVER_NAMESPACE_END