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>
16USERVER_NAMESPACE_BEGIN
23struct BufferFormatter<
const char*> {
26 explicit BufferFormatter(
const char* val) : value{val} {}
28 template <
typename Buffer>
29 void operator()(
const UserTypes&, Buffer& buf)
const {
30 auto sz = std::strlen(value);
31 WriteN(buf, value, sz);
34 template <
typename Buffer>
35 static void WriteN(Buffer& buf,
const char* c, std::size_t n) {
37 while (n > 0 && c[n - 1] ==
'\0') {
40 buf.reserve(buf.size() + n);
41 std::copy(c, c + n, std::back_inserter(buf));
48template <std::size_t N>
49struct BufferFormatter<
char[N]> {
50 using CharFormatter = BufferFormatter<
const char*>;
53 explicit BufferFormatter(
const char* val) : value{val} {}
55 template <
typename Buffer>
56 void operator()(
const UserTypes&, Buffer& buf)
const {
57 CharFormatter::WriteN(buf, value, N);
66struct BufferFormatter<std::string> {
67 using CharFormatter = BufferFormatter<
const char*>;
68 const std::string& value;
70 explicit BufferFormatter(
const std::string& val) : value{val} {}
71 template <
typename Buffer>
72 void operator()(
const UserTypes&, Buffer& buf)
const {
73 CharFormatter::WriteN(buf, value.data(), value.size());
78struct BufferParser<std::string> {
81 explicit BufferParser(std::string& val) : value{val} {}
83 void operator()(
const FieldBuffer& buffer);
90struct BufferFormatter<std::string_view> : detail::BufferFormatterBase<std::string_view> {
91 using BaseType = detail::BufferFormatterBase<std::string_view>;
92 using CharFormatter = BufferFormatter<
const char*>;
94 using BaseType::BaseType;
96 template <
typename Buffer>
97 void operator()(
const UserTypes&, Buffer& buffer)
const {
98 CharFormatter::WriteN(buffer, value.data(), value.size());
103struct BufferParser<std::string_view> : detail::BufferParserBase<std::string_view> {
104 using BaseType = detail::BufferParserBase<std::string_view>;
105 using BaseType::BaseType;
107 void operator()(
const FieldBuffer& buffer) {
109 ValueType tmp{
reinterpret_cast<
const char*>(buffer.buffer), buffer.length};
118struct BufferFormatter<
char> {
121 explicit BufferFormatter(
char val) : value{val} {}
122 template <
typename Buffer>
123 void operator()(
const UserTypes&, Buffer& buf)
const {
124 buf.push_back(value);
129struct BufferParser<
char> {
132 explicit BufferParser(
char& val) : value{val} {}
134 void operator()(
const FieldBuffer& buffer) {
135 if (buffer.length != 1) {
136 throw InvalidInputBufferSize{fmt::format(
"Buffer size {} is invalid for type char", buffer.length)};
138 value = *buffer.buffer;
146struct CppToSystemPg<
const char*> : PredefinedOid<PredefinedOids::kText> {};
147template <std::size_t N>
148struct CppToSystemPg<
char[N]> : PredefinedOid<PredefinedOids::kText> {};
150struct CppToSystemPg<std::string> : PredefinedOid<PredefinedOids::kText> {};
152struct CppToSystemPg<std::string_view> : PredefinedOid<PredefinedOids::kText> {};
154struct CppToSystemPg<
char> : PredefinedOid<PredefinedOids::kChar> {};