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 {
22public:
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, Args&&... args);
45
46 ExecuteResponse Execute(OperationSettings settings, const Query& query, PreparedArgsBuilder&& builder);
47
48 ExecuteResponse Execute(
49 QuerySettings query_settings,
50 OperationSettings settings,
51 const Query& query,
52 PreparedArgsBuilder&& builder
53 );
54 /// @}
55
56 /// Commit the transaction. The options that are missing in `settings` are
57 /// taken from the static config or driver defaults. `settings` can be
58 /// overridden by dynamic config's options for `Commit` "query".
59 void Commit(OperationSettings settings = {});
60
61 /// Rollback the transaction. The operation settings are taken from `Begin`
62 /// settings.
63 void Rollback();
64
65 PreparedArgsBuilder GetBuilder() const;
66
67 /// @cond
68 // For internal use only.
69 Transaction(
70 TableClient& table_client,
71 NYdb::NTable::TTransaction ydb_tx,
72 std::string name,
73 OperationSettings&& rollback_settings
74 ) noexcept;
75 /// @endcond
76
77private:
78 void MarkError() noexcept;
79 auto ErrorGuard();
80
81 void EnsureActive() const;
82
83 TableClient& table_client_;
84 std::string name_;
85 impl::StatsScope stats_scope_;
86 tracing::Span span_;
87 NYdb::NTable::TTransaction ydb_tx_;
88 OperationSettings rollback_settings_;
89 bool is_active_{true};
90};
91
92template <typename... Args>
93ExecuteResponse Transaction::Execute(const Query& query, Args&&... args) {
94 auto builder = GetBuilder();
95 builder.AddParams(std::forward<Args>(args)...);
96 return Execute(OperationSettings{}, query, std::move(builder));
97}
98
99template <typename... Args>
100ExecuteResponse Transaction::Execute(OperationSettings settings, const Query& query, Args&&... args) {
101 auto builder = GetBuilder();
102 builder.AddParams(std::forward<Args>(args)...);
103 return Execute(std::move(settings), query, std::move(builder));
104}
105
106} // namespace ydb
107
108USERVER_NAMESPACE_END