fb-cpp 0.0.1
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
Transaction.h
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Adriano dos Santos Fernandes
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#ifndef FBCPP_TRANSACTION_H
26#define FBCPP_TRANSACTION_H
27
28#include "fb-api.h"
29#include "SmartPtrs.h"
30#include <memory>
31#include <optional>
32#include <stdexcept>
33#include <string>
34#include <string_view>
35#include <vector>
36#include <cstddef>
37
38
42namespace fbcpp
43{
44 class Attachment;
45
50 {
51 CONSISTENCY,
52 READ_COMMITTED,
53 SNAPSHOT
54 };
55
60 {
61 NO_RECORD_VERSION,
62 RECORD_VERSION
63 };
64
69 {
70 READ_ONLY,
71 READ_WRITE
72 };
73
78 {
79 NO_WAIT,
80 WAIT
81 };
82
87 {
88 public:
93 const std::vector<std::uint8_t>& getTpb() const
94 {
95 return tpb;
96 }
97
102 TransactionOptions& setTpb(const std::vector<std::uint8_t>& value)
103 {
104 tpb = value;
105 return *this;
106 }
107
112 TransactionOptions& setTpb(std::vector<std::uint8_t>&& value)
113 {
114 tpb = std::move(value);
115 return *this;
116 }
117
121 const std::optional<TransactionIsolationLevel> getIsolationLevel() const
122 {
123 return isolationLevel;
124 }
125
130 {
131 isolationLevel = value;
132 return *this;
133 }
134
138 const std::optional<TransactionReadCommittedMode> getReadCommittedMode() const
139 {
140 return readCommittedMode;
141 }
142
147 {
148 readCommittedMode = value;
149 return *this;
150 }
151
155 const std::optional<TransactionAccessMode> getAccessMode() const
156 {
157 return accessMode;
158 }
159
164 {
165 accessMode = value;
166 return *this;
167 }
168
172 const std::optional<TransactionWaitMode> getWaitMode() const
173 {
174 return waitMode;
175 }
176
181 {
182 waitMode = value;
183 return *this;
184 }
185
190 bool getNoAutoUndo() const
191 {
192 return noAutoUndo;
193 }
194
200 {
201 noAutoUndo = value;
202 return *this;
203 }
204
208 bool getIgnoreLimbo() const
209 {
210 return ignoreLimbo;
211 }
212
217 {
218 ignoreLimbo = value;
219 return *this;
220 }
221
226 {
227 return restartRequests;
228 }
229
234 {
235 restartRequests = value;
236 return *this;
237 }
238
242 bool getAutoCommit() const
243 {
244 return autoCommit;
245 }
246
251 {
252 autoCommit = value;
253 return *this;
254 }
255
256 private:
257 std::vector<std::uint8_t> tpb;
258 std::optional<TransactionIsolationLevel> isolationLevel;
259 std::optional<TransactionReadCommittedMode> readCommittedMode;
260 std::optional<TransactionAccessMode> accessMode;
261 std::optional<TransactionWaitMode> waitMode;
262 bool noAutoUndo = false;
263 bool ignoreLimbo = false;
264 bool restartRequests = false;
265 bool autoCommit = false;
266 };
267
268 class Client;
269
276 class Transaction final
277 {
278 public:
280
285 explicit Transaction(Attachment& attachment, const TransactionOptions& options = {});
286
291 explicit Transaction(Attachment& attachment, std::string_view setTransactionCmd);
292
298 : client{o.client},
299 handle{std::move(o.handle)}
300 {
301 }
302
303 Transaction& operator=(Transaction&&) = delete;
304
305 Transaction(const Transaction&) = delete;
306 Transaction& operator=(const Transaction&) = delete;
307
311 ~Transaction() noexcept
312 {
313 if (isValid())
314 {
315 try
316 {
317 rollback();
318 }
319 catch (...)
320 {
321 // swallow
322 }
323 }
324 }
325
326 public:
330 bool isValid() noexcept
331 {
332 return handle != nullptr;
333 }
334
339 {
340 return handle;
341 }
342
346 void commit();
347
351 void commitRetaining();
352
356 void rollback();
357
360 //
361 void rollbackRetaining();
362
363 private:
364 Client& client;
366 };
367} // namespace fbcpp
368
369
370#endif // FBCPP_TRANSACTION_H
Represents a connection to a Firebird database.
Definition Attachment.h:177
Represents a Firebird client library instance.
Definition Client.h:53
Represents options used when creating a Transaction object.
Definition Transaction.h:87
bool getAutoCommit() const
Returns whether the transaction will be automatically committed.
const std::vector< std::uint8_t > & getTpb() const
Returns the TPB (Transaction Parameter Block) which will be used to start the transaction.
Definition Transaction.h:93
TransactionOptions & setWaitMode(TransactionWaitMode value)
Sets the transaction wait mode.
TransactionOptions & setTpb(std::vector< std::uint8_t > &&value)
Sets the TPB (Transaction Parameter Block) which will be used to start the transaction.
bool getRestartRequests() const
Returns whether the transaction will restart requests.
const std::optional< TransactionAccessMode > getAccessMode() const
Returns the transaction access mode.
bool getNoAutoUndo() const
Returns whether the transaction will not automatically undo changes in case of a deadlock or update c...
bool getIgnoreLimbo() const
Returns whether the transaction will ignore limbo transactions.
const std::optional< TransactionIsolationLevel > getIsolationLevel() const
Returns the transaction isolation level.
const std::optional< TransactionReadCommittedMode > getReadCommittedMode() const
Returns the read committed mode.
TransactionOptions & setAccessMode(TransactionAccessMode value)
Sets the transaction access mode.
TransactionOptions & setTpb(const std::vector< std::uint8_t > &value)
Sets the TPB (Transaction Parameter Block) which will be used to start the transaction.
TransactionOptions & setReadCommittedMode(TransactionReadCommittedMode value)
Sets the read committed mode.
TransactionOptions & setNoAutoUndo(bool value)
Sets whether the transaction will not automatically undo changes in case of a deadlock or update conf...
TransactionOptions & setIsolationLevel(TransactionIsolationLevel value)
Sets the transaction isolation level.
TransactionOptions & setAutoCommit(bool value)
Sets whether the transaction will be automatically committed.
const std::optional< TransactionWaitMode > getWaitMode() const
Returns the transaction wait mode.
TransactionOptions & setIgnoreLimbo(bool value)
Sets whether the transaction will ignore limbo transactions.
TransactionOptions & setRestartRequests(bool value)
Sets whether the transaction will restart requests.
Represents a transaction in a Firebird database.
void rollback()
Rolls back the transaction.
Transaction(Transaction &&o) noexcept
Move constructor.
~Transaction() noexcept
Rolls back the transaction if it is still active.
void commit()
Commits the transaction.
bool isValid() noexcept
Returns whether the Transaction object is valid.
FbRef< fb::ITransaction > getHandle() noexcept
Returns the internal Firebird ITransaction handle.
void commitRetaining()
Commits the transaction while maintains it active.
void rollbackRetaining()
Rolls back the transaction while maintains it active.
fb-cpp namespace.
Definition Attachment.h:42
TransactionAccessMode
Transaction access mode.
Definition Transaction.h:69
TransactionReadCommittedMode
Transaction read committed mode.
Definition Transaction.h:60
TransactionIsolationLevel
Transaction isolation level.
Definition Transaction.h:50
TransactionWaitMode
Transaction wait mode.
Definition Transaction.h:78