userver: userver/ydb/transaction.hpp Source File
Loading...
Searching...
No Matches
transaction.hpp
1#pragma once
2
3#include <string>
4
5#include <userver/tracing/span.hpp>
6
7#include <userver/ydb/builder.hpp>
8#include <userver/ydb/exceptions.hpp>
9#include <userver/ydb/impl/stats_scope.hpp>
10#include <userver/ydb/query.hpp>
11#include <userver/ydb/response.hpp>
12#include <userver/ydb/settings.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace ydb {
17
18/// @brief YDB Transaction
19///
20/// https://ydb.tech/docs/en/concepts/transactions
21class Transaction final {
22 public:
23 Transaction(Transaction&&) noexcept = default;
24 Transaction(const Transaction&) = delete;
25 Transaction& operator=(Transaction&&) = delete;
26 Transaction& operator=(const Transaction&) = delete;
27 ~Transaction();
28
29 /// Execute a single data query as a part of the transaction. Query parameters
30 /// are passed in `Args` as "string key - value" pairs:
31 ///
32 /// @code
33 /// client.ExecuteDataQuery(query, "name1", value1, "name2", value2, ...);
34 /// @endcode
35 ///
36 /// Use ydb::PreparedArgsBuilder for storing a generic buffer of query params
37 /// if needed.
38 ///
39 /// @{
40 template <typename... Args>
41 ExecuteResponse Execute(const Query& query, Args&&... args);
42
43 template <typename... Args>
44 ExecuteResponse Execute(OperationSettings settings, const Query& query,
45 Args&&... args);
46
47 ExecuteResponse Execute(OperationSettings settings, const Query& query,
48 PreparedArgsBuilder&& builder);
49
50 ExecuteResponse Execute(QuerySettings query_settings,
51 OperationSettings settings, const Query& query,
52 PreparedArgsBuilder&& builder);
53 /// @}
54
55 /// Commit the transaction. The options that are missing in `settings` are
56 /// taken from the static config or driver defaults. `settings` can be
57 /// overridden by dynamic config's options for `Commit` "query".
58 void Commit(OperationSettings settings = {});
59
60 /// Rollback the transaction. The operation settings are taken from `Begin`
61 /// settings.
62 void Rollback();
63
64 PreparedArgsBuilder GetBuilder() const;
65
66 /// @cond
67 // For internal use only.
68 Transaction(TableClient& table_client, NYdb::NTable::TTransaction ydb_tx,
69 std::string name, OperationSettings&& rollback_settings) noexcept;
70 /// @endcond
71
72 private:
73 void MarkError() noexcept;
74 auto ErrorGuard();
75
76 void EnsureActive() const;
77
78 TableClient& table_client_;
79 std::string name_;
80 impl::StatsScope stats_scope_;
81 tracing::Span span_;
82 NYdb::NTable::TTransaction ydb_tx_;
83 OperationSettings rollback_settings_;
84 bool is_active_{true};
85};
86
87template <typename... Args>
88ExecuteResponse Transaction::Execute(const Query& query, Args&&... args) {
89 auto builder = GetBuilder();
90 builder.AddParams(std::forward<Args>(args)...);
91 return Execute(OperationSettings{}, query, std::move(builder));
92}
93
94template <typename... Args>
95ExecuteResponse Transaction::Execute(OperationSettings settings,
96 const Query& query, Args&&... args) {
97 auto builder = GetBuilder();
98 builder.AddParams(std::forward<Args>(args)...);
99 return Execute(std::move(settings), query, std::move(builder));
100}
101
102} // namespace ydb
103
104USERVER_NAMESPACE_END