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,
18 bool Categories>
19struct TransformParserBase : BufferParserBase<UserType> {
20 using BaseType = BufferParserBase<UserType>;
21 using BaseType::BaseType;
22
23 void operator()(const FieldBuffer& buffer) {
24 WireType tmp;
25 io::ReadBuffer(buffer, tmp);
26 this->value = Converter{}(tmp);
27 }
28};
29
30template <typename UserType, typename WireType, typename Converter>
31struct TransformParserBase<UserType, WireType, Converter, true>
32 : BufferParserBase<UserType> {
33 using BaseType = BufferParserBase<UserType>;
34 using BaseType::BaseType;
35
36 void operator()(const FieldBuffer& buffer,
37 const TypeBufferCategory& categories) {
38 WireType tmp;
39 io::ReadBuffer(buffer, tmp, categories);
40 this->value = Converter{}(tmp);
41 }
42};
43
44} // namespace detail
45
46template <typename UserType, typename WireType, typename Converter>
47struct TransformParser : detail::TransformParserBase<
48 UserType, WireType, Converter,
49 detail::kParserRequiresTypeCategories<WireType>> {
50 using BaseType = detail::TransformParserBase<
51 UserType, WireType, Converter,
52 detail::kParserRequiresTypeCategories<WireType>>;
53 using BaseType::BaseType;
54};
55
56template <typename UserType, typename WireType, typename Converter>
57struct TransformFormatter : detail::BufferFormatterBase<UserType> {
58 using BaseType = detail::BufferFormatterBase<UserType>;
59 using BaseType::BaseType;
60
61 template <typename Buffer>
62 void operator()(const UserTypes& types, Buffer& buffer) const {
63 if constexpr (traits::kIsMappedToSystemType<UserType> &&
64 traits::kIsMappedToSystemType<WireType>) {
65 static_assert(CppToPg<UserType>::type_oid == CppToPg<WireType>::type_oid,
66 "Database type mismatch in TransformFormatter");
67 }
69 CppToPg<UserType>::GetOid(types) == CppToPg<WireType>::GetOid(types),
70 "Database type mismatch in TransformFormatter");
71
72 WireType tmp = Converter{}(this->value);
73 io::WriteBuffer(types, buffer, tmp);
74 }
75};
76} // namespace storages::postgres::io
77
78USERVER_NAMESPACE_END