userver: userver/storages/postgres/io/string_types.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
string_types.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/postgres/io/string_types.hpp
4/// @brief Strings I/O support
5/// @ingroup userver_postgres_parse_and_format
6
7#include <cstring>
8#include <string>
9#include <string_view>
10
11#include <userver/storages/postgres/exceptions.hpp>
12#include <userver/storages/postgres/io/buffer_io_base.hpp>
13#include <userver/storages/postgres/io/traits.hpp>
14#include <userver/storages/postgres/io/type_mapping.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace storages::postgres::io {
19
20//@{
21/** @name const char* formatting */
22template <>
23struct BufferFormatter<const char*> {
24 const char* value;
25
26 explicit BufferFormatter(const char* val) : value{val} {}
27
28 template <typename Buffer>
29 void operator()(const UserTypes&, Buffer& buf) const {
30 auto sz = std::strlen(value);
31 WriteN(buf, value, sz);
32 }
33
34 template <typename Buffer>
35 static void WriteN(Buffer& buf, const char* c, std::size_t n) {
36 // Don't copy zero-terminator in binary mode
37 while (n > 0 && c[n - 1] == '\0') {
38 --n;
39 }
40 buf.reserve(buf.size() + n);
41 std::copy(c, c + n, std::back_inserter(buf));
42 }
43};
44//@}
45
46//@{
47/** @name char[N] formatting */
48template <std::size_t N>
49struct BufferFormatter<char[N]> {
50 using CharFormatter = BufferFormatter<const char*>;
51 const char* value;
52
53 explicit BufferFormatter(const char* val) : value{val} {}
54
55 template <typename Buffer>
56 void operator()(const UserTypes&, Buffer& buf) const {
58 }
59};
60
61//@}
62
63//@{
64/** @name std::string I/O */
65template <>
67 using CharFormatter = BufferFormatter<const char*>;
68 const std::string& value;
69
70 explicit BufferFormatter(const std::string& val) : value{val} {}
71 template <typename Buffer>
72 void operator()(const UserTypes&, Buffer& buf) const {
74 }
75};
76
77template <>
80
81 explicit BufferParser(std::string& val) : value{val} {}
82
83 void operator()(const FieldBuffer& buffer);
84};
85//@}
86
87//@{
88/** @name string_view I/O */
89template <>
93 using CharFormatter = BufferFormatter<const char*>;
94
95 using BaseType::BaseType;
96
97 template <typename Buffer>
98 void operator()(const UserTypes&, Buffer& buffer) const {
100 }
101};
102
103template <>
107 using BaseType::BaseType;
108
109 void operator()(const FieldBuffer& buffer) {
110 using std::swap;
111 ValueType tmp{reinterpret_cast<const char*>(buffer.buffer), buffer.length};
112 swap(tmp, value);
113 }
114};
115//@}
116
117//@{
118/** @name char I/O */
119template <>
120struct BufferFormatter<char> {
121 char value;
122
123 explicit BufferFormatter(char val) : value{val} {}
124 template <typename Buffer>
125 void operator()(const UserTypes&, Buffer& buf) const {
127 }
128};
129
130template <>
131struct BufferParser<char> {
132 char& value;
133
134 explicit BufferParser(char& val) : value{val} {}
135
136 void operator()(const FieldBuffer& buffer) {
137 if (buffer.length != 1) {
138 throw InvalidInputBufferSize{buffer.length, "for type char"};
139 }
141 }
142};
143//@}
144
145//@{
146/** @name C++ to PostgreSQL mapping for string types */
147template <>
148struct CppToSystemPg<const char*> : PredefinedOid<PredefinedOids::kText> {};
149template <std::size_t N>
150struct CppToSystemPg<char[N]> : PredefinedOid<PredefinedOids::kText> {};
151template <>
152struct CppToSystemPg<std::string> : PredefinedOid<PredefinedOids::kText> {};
153template <>
155};
156template <>
157struct CppToSystemPg<char> : PredefinedOid<PredefinedOids::kChar> {};
158//@}
159
160} // namespace storages::postgres::io
161
162USERVER_NAMESPACE_END