fb-cpp 0.0.2
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-cpp-export.h"
29#include "fb-api.h"
30#include "SmartPtrs.h"
31#include <memory>
32#include <optional>
33#include <span>
34#include <stdexcept>
35#include <string>
36#include <string_view>
37#include <vector>
38#include <cstddef>
39
40
44namespace fbcpp
45{
46 class Attachment;
47
52 {
57
62
67 };
68
84
89 {
94
99 };
100
105 {
109 NO_WAIT,
110
114 WAIT
115 };
116
121 {
122 public:
127 const std::vector<std::uint8_t>& getTpb() const
128 {
129 return tpb;
130 }
131
136 TransactionOptions& setTpb(const std::vector<std::uint8_t>& value)
137 {
138 tpb = value;
139 return *this;
140 }
141
146 TransactionOptions& setTpb(std::vector<std::uint8_t>&& value)
147 {
148 tpb = std::move(value);
149 return *this;
150 }
151
155 const std::optional<TransactionIsolationLevel> getIsolationLevel() const
156 {
157 return isolationLevel;
158 }
159
164 {
165 isolationLevel = value;
166 return *this;
167 }
168
172 const std::optional<TransactionReadCommittedMode> getReadCommittedMode() const
173 {
174 return readCommittedMode;
175 }
176
181 {
182 readCommittedMode = value;
183 return *this;
184 }
185
189 const std::optional<TransactionAccessMode> getAccessMode() const
190 {
191 return accessMode;
192 }
193
198 {
199 accessMode = value;
200 return *this;
201 }
202
206 const std::optional<TransactionWaitMode> getWaitMode() const
207 {
208 return waitMode;
209 }
210
215 {
216 waitMode = value;
217 return *this;
218 }
219
224 bool getNoAutoUndo() const
225 {
226 return noAutoUndo;
227 }
228
234 {
235 noAutoUndo = value;
236 return *this;
237 }
238
242 bool getIgnoreLimbo() const
243 {
244 return ignoreLimbo;
245 }
246
251 {
252 ignoreLimbo = value;
253 return *this;
254 }
255
260 {
261 return restartRequests;
262 }
263
268 {
269 restartRequests = value;
270 return *this;
271 }
272
276 bool getAutoCommit() const
277 {
278 return autoCommit;
279 }
280
285 {
286 autoCommit = value;
287 return *this;
288 }
289
290 private:
291 std::vector<std::uint8_t> tpb;
292 std::optional<TransactionIsolationLevel> isolationLevel;
293 std::optional<TransactionReadCommittedMode> readCommittedMode;
294 std::optional<TransactionAccessMode> accessMode;
295 std::optional<TransactionWaitMode> waitMode;
296 bool noAutoUndo = false;
297 bool ignoreLimbo = false;
298 bool restartRequests = false;
299 bool autoCommit = false;
300 };
301
302 class Client;
303
308 {
312 ACTIVE,
313
317 PREPARED,
318
322 COMMITTED,
323
328 };
329
351 class FB_CPP_EXPORT Transaction final
352 {
353 public:
358 explicit Transaction(Attachment& attachment, const TransactionOptions& options = {});
359
364 explicit Transaction(Attachment& attachment, std::string_view setTransactionCmd);
365
375 explicit Transaction(
376 std::span<std::reference_wrapper<Attachment>> attachments, const TransactionOptions& options = {});
377
383 : client{o.client},
384 handle{std::move(o.handle)},
385 state{o.state},
386 isMultiDatabase{o.isMultiDatabase}
387 {
388 o.state = TransactionState::ROLLED_BACK;
389 }
390
391 Transaction& operator=(Transaction&&) = delete;
392
393 Transaction(const Transaction&) = delete;
394 Transaction& operator=(const Transaction&) = delete;
395
402 ~Transaction() noexcept
403 {
404 if (isValid())
405 {
406 assert(state != TransactionState::PREPARED &&
407 "Prepared transaction must be explicitly committed or rolled back");
408
409 try
410 {
411 if (state == TransactionState::ACTIVE)
412 rollback();
413 }
414 catch (...)
415 {
416 // swallow
417 }
418 }
419 }
420
421 public:
425 bool isValid() noexcept
426 {
427 return handle != nullptr;
428 }
429
434 {
435 return handle;
436 }
437
441 TransactionState getState() const noexcept
442 {
443 return state;
444 }
445
452 void prepare();
453
459 void prepare(std::string_view message);
460
466 void prepare(std::span<const std::uint8_t> message);
467
473 void commit();
474
480 void commitRetaining();
481
487 void rollback();
488
494 void rollbackRetaining();
495
496 private:
497 Client& client;
499 TransactionState state = TransactionState::ACTIVE;
500 const bool isMultiDatabase = false;
501 };
502} // namespace fbcpp
503
504
505#endif // FBCPP_TRANSACTION_H
Represents a connection to a Firebird database.
Definition Attachment.h:219
Represents a Firebird client library instance.
Definition Client.h:54
Reference-counted smart pointer for Firebird objects using addRef/release semantics.
Definition SmartPtrs.h:70
Represents options used when creating a Transaction object.
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.
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 one or more Firebird databases.
Transaction(Transaction &&o) noexcept
Move constructor.
~Transaction() noexcept
Rolls back the transaction if it is still active.
TransactionState getState() const noexcept
Returns the current transaction state.
bool isValid() noexcept
Returns whether the Transaction object is valid.
FbRef< fb::ITransaction > getHandle() noexcept
Returns the internal Firebird ITransaction handle.
fb-cpp namespace.
Definition Attachment.h:46
TransactionState
Transaction state for tracking two-phase commit lifecycle.
@ ACTIVE
Transaction is active and can execute statements.
@ COMMITTED
Transaction has been committed.
@ PREPARED
Transaction has been prepared (2PC phase 1).
@ ROLLED_BACK
Transaction has been rolled back.
TransactionAccessMode
Transaction access mode.
Definition Transaction.h:89
TransactionReadCommittedMode
Transaction read committed mode.
Definition Transaction.h:73
@ NO_RECORD_VERSION
Does not allow reading of record versions; waits for or errors on uncommitted changes.
@ RECORD_VERSION
Allows reading of the latest committed version of a record.
@ READ_ONLY
Read-only replica.
@ READ_WRITE
Read-write replica.
TransactionIsolationLevel
Transaction isolation level.
Definition Transaction.h:52
@ READ_COMMITTED
Allows reading of committed changes from other transactions.
@ SNAPSHOT
Provides a stable snapshot of the database at transaction start time.
@ CONSISTENCY
Ensures full transaction consistency at the expense of concurrency.
TransactionWaitMode
Transaction wait mode.
@ WAIT
Transaction waits until a conflicting lock is released.
@ NO_WAIT
Transaction returns an error immediately if a lock conflict occurs.