userver: userver/storages/mysql/impl/io/common_binders.hpp Source File
Loading...
Searching...
No Matches
common_binders.hpp
1#pragma once
2
3#include <chrono>
4#include <cstdint>
5#include <optional>
6#include <string>
7#include <string_view>
8
9#include <userver/formats/json_fwd.hpp>
10
11#include <userver/storages/mysql/dates.hpp>
12#include <userver/storages/mysql/impl/binder_fwd.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace storages::mysql::impl::io {
17
18template <typename T>
19class ExplicitRef final {
20 public:
21 explicit ExplicitRef(T& ref) : ref_{ref} {}
22 explicit ExplicitRef(T&& ref) = delete;
23
24 T& Get() { return ref_; }
25
26 private:
27 T& ref_;
28};
29
30template <typename T>
31class ExplicitCRef final {
32 public:
33 static_assert(!std::is_const_v<T>);
34
35 explicit ExplicitCRef(const T& ref) : ref_{ref} {}
36 explicit ExplicitCRef(T&& ref) = delete;
37
38 const T& Get() const { return ref_; }
39
40 private:
41 const T& ref_;
42};
43
44// -------------------------- Output Bindings ---------------------------------
45// yes, we do have to define optional<> variants separately,
46// it's not that straightforward for output.
47void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
48 ExplicitRef<std::uint8_t> val);
49void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
50 ExplicitRef<std::optional<std::uint8_t>> val);
51void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
52 ExplicitRef<std::int8_t> val);
53void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
54 ExplicitRef<std::optional<std::int8_t>> val);
55
56void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
57 ExplicitRef<std::uint16_t> val);
58void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
59 ExplicitRef<std::optional<std::uint16_t>> val);
60void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
61 ExplicitRef<std::int16_t> val);
62void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
63 ExplicitRef<std::optional<std::int16_t>> val);
64
65void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
66 ExplicitRef<std::uint32_t> val);
67void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
68 ExplicitRef<std::optional<std::uint32_t>> val);
69void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
70 ExplicitRef<std::int32_t> val);
71void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
72 ExplicitRef<std::optional<std::int32_t>> val);
73
74void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
75 ExplicitRef<std::uint64_t> val);
76void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
77 ExplicitRef<std::optional<std::uint64_t>> val);
78void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
79 ExplicitRef<std::int64_t> val);
80void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
81 ExplicitRef<std::optional<std::int64_t>> val);
82
83void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
84 ExplicitRef<float> val);
85void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
86 ExplicitRef<std::optional<float>> val);
87void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
88 ExplicitRef<double> val);
89void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
90 ExplicitRef<std::optional<double>> val);
91
92void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
93 ExplicitRef<std::string> val);
94void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
95 ExplicitRef<std::optional<std::string>> val);
96
97void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
98 ExplicitRef<formats::json::Value> val);
99void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
100 ExplicitRef<std::optional<formats::json::Value>> val);
101
102// These 2 should never be called, so we just leave them unimplemented
103void FreestandingBind(OutputBindingsFwd&, std::size_t,
104 ExplicitRef<std::string_view>);
105void FreestandingBind(OutputBindingsFwd&, std::size_t,
106 ExplicitRef<std::optional<std::string_view>>);
107
108void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
109 ExplicitRef<std::chrono::system_clock::time_point> val);
110void FreestandingBind(
111 OutputBindingsFwd& binds, std::size_t pos,
112 ExplicitRef<std::optional<std::chrono::system_clock::time_point>> val);
113void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
114 ExplicitRef<Date> val);
115void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
116 ExplicitRef<std::optional<Date>> val);
117void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
118 ExplicitRef<DateTime> val);
119void FreestandingBind(OutputBindingsFwd& binds, std::size_t pos,
120 ExplicitRef<std::optional<DateTime>> val);
121
122template <typename T>
123void FreestandingBind(OutputBindingsFwd&, std::size_t, ExplicitRef<T>) {
124 static_assert(!sizeof(T), "IO support for the type is not implemented.");
125}
126
127// --------------------------- Input Bindings ---------------------------------
128void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
129 ExplicitCRef<std::uint8_t> val);
130void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
131 ExplicitCRef<std::int8_t> val);
132
133void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
134 ExplicitCRef<std::uint16_t> val);
135void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
136 ExplicitCRef<std::int16_t> val);
137
138void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
139 ExplicitCRef<std::uint32_t> val);
140void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
141 ExplicitCRef<std::int32_t> val);
142
143void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
144 ExplicitCRef<std::uint64_t> val);
145void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
146 ExplicitCRef<std::int64_t> val);
147
148void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
149 ExplicitCRef<float> val);
150void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
151 ExplicitCRef<double> val);
152
153void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
154 ExplicitCRef<std::string> val);
155void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
156 ExplicitCRef<std::string_view> val);
157void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
158 ExplicitCRef<formats::json::Value> val);
159
160void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
161 ExplicitCRef<std::chrono::system_clock::time_point> val);
162void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
163 ExplicitCRef<Date> val);
164void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
165 ExplicitCRef<DateTime> val);
166
167void BindNull(InputBindingsFwd& binds, std::size_t pos);
168
169template <typename T>
170void FreestandingBind(InputBindingsFwd& binds, std::size_t pos,
171 ExplicitCRef<std::optional<T>> val) {
172 if (val.Get().has_value()) {
173 storages::mysql::impl::io::FreestandingBind(binds, pos,
174 ExplicitCRef<T>{*val.Get()});
175 } else {
176 storages::mysql::impl::io::BindNull(binds, pos);
177 }
178}
179
180template <typename T>
181void FreestandingBind(InputBindingsFwd&, std::size_t, ExplicitCRef<T>) {
182 static_assert(!sizeof(T), "IO support for the type is not implemented.");
183}
184
185} // namespace storages::mysql::impl::io
186
187USERVER_NAMESPACE_END