userver: userver/ydb/builder.hpp Source File
Loading...
Searching...
No Matches
builder.hpp
1#pragma once
2
3#include <ydb-cpp-sdk/client/params/params.h>
4
5#include <string>
6#include <type_traits>
7#include <utility>
8
9#include <userver/ydb/impl/cast.hpp>
10#include <userver/ydb/io/traits.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace ydb {
15
16class TableClient;
17class Transaction;
18
19class PreparedArgsBuilder final {
20public:
21 PreparedArgsBuilder(PreparedArgsBuilder&&) noexcept = default;
22 PreparedArgsBuilder& operator=(PreparedArgsBuilder&&) = delete;
23
24 /// Supported types and required includes are documented in:
25 /// <userver/ydb/io/supported_types.hpp>
26 template <typename T>
27 void Add(const std::string& name, T&& value);
28
29 /// @cond
30 // For internal use only.
31 explicit PreparedArgsBuilder(NYdb::TParamsBuilder&& builder) : builder_(std::move(builder)) {}
32
33 // For internal use only.
34 template <typename... NamesValues>
35 void AddParams(NamesValues&&... names_values);
36 /// @endcond
37
38private:
39 friend class Transaction;
40 friend class TableClient;
41 struct PreparedArgsWithKey;
42
43 NYdb::TParams Build() && { return std::move(builder_).Build(); }
44
45 PreparedArgsWithKey operator<<(const std::string& key);
46
47 NYdb::TParamsBuilder builder_;
48};
49
50template <typename T>
51void PreparedArgsBuilder::Add(const std::string& name, T&& value) {
52 auto& param_builder = builder_.AddParam(impl::ToString(name));
53 Write(param_builder, std::forward<T>(value));
54 param_builder.Build();
55}
56
57template <typename... NamesValues>
58void PreparedArgsBuilder::AddParams(NamesValues&&... names_values) {
59 [[maybe_unused]] decltype(auto) result = (*this << ... << std::forward<NamesValues>(names_values));
60 static_assert(std::is_same_v<decltype(result), PreparedArgsBuilder&>);
61}
62
63struct PreparedArgsBuilder::PreparedArgsWithKey final {
64 PreparedArgsBuilder& builder;
65 const std::string& key;
66
67 template <typename T>
68 PreparedArgsBuilder& operator<<(T&& value) const {
69 builder.Add(key, std::forward<T>(value));
70 return builder;
71 }
72};
73
74inline auto PreparedArgsBuilder::operator<<(const std::string& key) -> PreparedArgsWithKey {
75 return PreparedArgsWithKey{*this, key};
76}
77
78} // namespace ydb
79
80USERVER_NAMESPACE_END