userver: userver/storages/sqlite/transaction.hpp Source File
Loading...
Searching...
No Matches
transaction.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/sqlite/transaction.hpp
4
5#include <userver/utils/fast_pimpl.hpp>
6#include <userver/utils/trx_tracker.hpp>
7
8#include <userver/storages/sqlite/cursor_result_set.hpp>
9#include <userver/storages/sqlite/impl/binder_help.hpp>
10#include <userver/storages/sqlite/options.hpp>
11#include <userver/storages/sqlite/query.hpp>
12#include <userver/storages/sqlite/result_set.hpp>
13#include <userver/storages/sqlite/savepoint.hpp>
14#include <userver/storages/sqlite/sqlite_fwd.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace storages::sqlite {
19
20/// @brief RAII transaction wrapper, auto-<b>ROLLBACK</b>s on destruction if no
21/// prior `Commit`/`Rollback` call was made.
22///
23/// This type can't be constructed in user code and is always retrieved from
24/// storages::sqlite::Client
25class Transaction final {
26public:
27 Transaction(std::shared_ptr<infra::ConnectionPtr> connection, const settings::TransactionOptions& options);
28 ~Transaction();
29 Transaction(const Transaction& other) = delete;
30 Transaction(Transaction&& other) noexcept;
31 Transaction& operator=(Transaction&&) noexcept;
32
33 /// @brief Executes a statement.
34 ///
35 /// Fills placeholders of the statement with args..., `Args` are expected to
36 /// be of supported types.
37 /// See @ref scripts/docs/en/userver/sqlite/supported_types.md for better understanding of `Args`
38 /// requirements.
39 ///
40 /// @tparam Args Types of parameters to bind
41 /// @param query SQL query to execute
42 /// @param args Parameters to bind to the query
43 /// @return ResultSet containing the results of the query
44 template <typename... Args>
45 ResultSet Execute(const Query& query, const Args&... args) const;
46
47 /// @brief Executes a statement, decomposing the row.
48 ///
49 /// Decomposes the fields of the row and binds them as parameters to the query.
50 /// See @ref scripts/docs/en/userver/sqlite/supported_types.md for better understanding of `T` requirements.
51 ///
52 /// @tparam T Type of the row to decompose
53 /// @param query SQL query to execute
54 /// @param row Row object to decompose and bind
55 /// @return ResultSet containing the results of the query
56 template <typename T>
57 ResultSet ExecuteDecompose(const Query& query, const T& row) const;
58
59 /// @brief Executes a statement multiple times.
60 ///
61 /// Iterates over the container and executes the query for each element.
62 /// Container is expected to be a std::Container, Container::value_type is
63 /// expected to be an aggregate of supported types.
64 /// See @ref scripts/docs/en/userver/mysql/supported_types.md for better understanding of
65 /// `Container::value_type` requirements.
66 ///
67 /// @tparam Container Type of the container holding rows
68 /// @param query SQL query to execute
69 /// @param params Container of rows to bind and execute
70 template <typename Container>
71 void ExecuteMany(const Query& query, const Container& params) const;
72
73 /// @brief Executes a statement and returns a cursor for iterating over results.
74 ///
75 /// Fills placeholders of the statement with args..., `Args` are expected to
76 /// be of supported types.
77 /// See @ref scripts/docs/en/userver/sqlite/supported_types.md for better understanding of `Args`
78 /// requirements.
79 ///
80 /// @tparam T Type of the result rows
81 /// @tparam Args Types of parameters to bind
82 /// @param batch_size Number of rows to fetch per batch
83 /// @param query SQL query to execute
84 /// @param args Parameters to bind to the query
85 /// @return CursorResultSet for iterating over the results
86 template <typename T, typename... Args>
87 CursorResultSet<T> GetCursor(std::size_t batch_size, const Query& query, const Args&... args) const;
88
89 /// @brief Creates a savepoint with specified name.
90 ///
91 /// @param name Name of the savepoint
92 /// @return Savepoint object representing the created savepoint
93 Savepoint Save(std::string name) const;
94
95 /// @brief Commit the transaction. On failure, attempts a rollback and rethrows the commit error.
96 /// The transaction handle is consumed in either case.
97 void Commit();
98
99 /// @brief Rollback the transaction
100 void Rollback();
101
102private:
103 ResultSet DoExecute(impl::io::ParamsBinderBase& params) const;
104 void AssertValid() const;
105
106 void RollbackNoThrow() noexcept;
107
108 void AccountQueryExecute() const noexcept;
109 void AccountQueryFailed() const noexcept;
110
111 std::shared_ptr<infra::ConnectionPtr> connection_;
112 utils::trx_tracker::TransactionLock trx_lock_;
113};
114
115template <typename... Args>
116ResultSet Transaction::Execute(const Query& query, const Args&... args) const {
117 AssertValid();
118 AccountQueryExecute();
119 try {
120 auto params_binder = impl::BindHelper::UpdateParamsBindings(query, *connection_, args...);
121 return DoExecute(params_binder);
122 } catch (const std::exception& err) {
123 AccountQueryFailed();
124 throw;
125 }
126}
127
128template <typename T>
129ResultSet Transaction::ExecuteDecompose(const Query& query, const T& row) const {
130 AssertValid();
131 AccountQueryExecute();
132 try {
133 auto params_binder = impl::BindHelper::UpdateRowAsParamsBindings(query, *connection_, row);
134 return DoExecute(params_binder);
135 } catch (const std::exception& err) {
136 AccountQueryFailed();
137 throw;
138 }
139}
140
141template <typename Container>
142void Transaction::ExecuteMany(const Query& query, const Container& params) const {
143 AssertValid();
144 AccountQueryExecute();
145 for (const auto& row : params) {
146 try {
147 auto params_binder = impl::BindHelper::UpdateRowAsParamsBindings(query, *connection_, row);
148 DoExecute(params_binder);
149 } catch (const std::exception& err) {
150 AccountQueryFailed();
151 throw;
152 }
153 }
154}
155
156template <typename T, typename... Args>
157CursorResultSet<T> Transaction::GetCursor(std::size_t batch_size, const Query& query, const Args&... args) const {
158 AssertValid();
159 AccountQueryExecute();
160 try {
161 auto params_binder = impl::BindHelper::UpdateParamsBindings(query, *connection_, args...);
162 return CursorResultSet<T>{DoExecute(params_binder, connection_), batch_size};
163 } catch (const std::exception& err) {
164 AccountQueryFailed();
165 throw;
166 }
167}
168
169} // namespace storages::sqlite
170
171USERVER_NAMESPACE_END