userver: userver/storages/mysql/impl/io/params_binder.hpp Source File
Loading...
Searching...
No Matches
params_binder.hpp
1#pragma once
2
3#include <string_view>
4
5#include <userver/storages/mysql/impl/io/binder_declarations.hpp>
6#include <userver/storages/mysql/impl/io/params_binder_base.hpp>
7
8USERVER_NAMESPACE_BEGIN
9
10namespace storages::mysql::impl::io {
11
12class ParamsBinder final : public ParamsBinderBase {
13 public:
14 explicit ParamsBinder(std::size_t size);
15 ~ParamsBinder();
16
17 ParamsBinder(const ParamsBinder& other) = delete;
18 ParamsBinder(ParamsBinder&& other) noexcept;
19
20 template <typename Field>
21 void Bind(std::size_t pos, const Field& field) {
22 storages::mysql::impl::io::BindInput(GetBinds(), pos, field);
23 }
24
25 std::size_t GetRowsCount() const final;
26
27 template <typename... Args>
28 static ParamsBinder BindParams(const Args&... args) {
29 constexpr auto kParamsCount = sizeof...(Args);
30 ParamsBinder binder{kParamsCount};
31
32 if constexpr (kParamsCount > 0) {
33 size_t ind = 0;
34 const auto do_bind = [&binder](std::size_t pos, const auto& arg) {
35 // Workaround for "const char*/const char[N]"
36 if constexpr (std::is_convertible_v<decltype(arg), const char*>) {
37 const auto* const_char_ptr_arg = static_cast<const char*>(arg);
38 // std::basic_string_view cannot be constructed from nullptr
39 if (const_char_ptr_arg == nullptr) {
40 // this is effectively BindNull
41 const std::optional<std::string_view> empty_opt_sw{};
42 binder.Bind(pos, empty_opt_sw);
43 } else {
44 const std::string_view sw{arg};
45 binder.Bind(pos, sw);
46 }
47 } else {
48 binder.Bind(pos, arg);
49 }
50 };
51
52 (..., do_bind(ind++, args));
53 }
54
55 return binder;
56 }
57};
58
59} // namespace storages::mysql::impl::io
60
61USERVER_NAMESPACE_END