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-api.h"
29#include "SmartPtrs.h"
30#include <memory>
31#include <optional>
32#include <span>
33#include <stdexcept>
34#include <string>
35#include <string_view>
36#include <vector>
37#include <cstddef>
38
39
43namespace fbcpp
44{
45 class Attachment;
46
51 {
56
61
66 };
67
83
88 {
93
98 };
99
104 {
108 NO_WAIT,
109
113 WAIT
114 };
115
120 {
121 public:
126 const std::vector<std::uint8_t>& getTpb() const
127 {
128 return tpb;
129 }
130
135 TransactionOptions& setTpb(const std::vector<std::uint8_t>& value)
136 {
137 tpb = value;
138 return *this;
139 }
140
145 TransactionOptions& setTpb(std::vector<std::uint8_t>&& value)
146 {
147 tpb = std::move(value);
148 return *this;
149 }
150
154 const std::optional<TransactionIsolationLevel> getIsolationLevel() const
155 {
156 return isolationLevel;
157 }
158
163 {
164 isolationLevel = value;
165 return *this;
166 }
167
171 const std::optional<TransactionReadCommittedMode> getReadCommittedMode() const
172 {
173 return readCommittedMode;
174 }
175
180 {
181 readCommittedMode = value;
182 return *this;
183 }
184
188 const std::optional<TransactionAccessMode> getAccessMode() const
189 {
190 return accessMode;
191 }
192
197 {
198 accessMode = value;
199 return *this;
200 }
201
205 const std::optional<TransactionWaitMode> getWaitMode() const
206 {
207 return waitMode;
208 }
209
214 {
215 waitMode = value;
216 return *this;
217 }
218
223 bool getNoAutoUndo() const
224 {
225 return noAutoUndo;
226 }
227
233 {
234 noAutoUndo = value;
235 return *this;
236 }
237
241 bool getIgnoreLimbo() const
242 {
243 return ignoreLimbo;
244 }
245
250 {
251 ignoreLimbo = value;
252 return *this;
253 }
254
259 {
260 return restartRequests;
261 }
262
267 {
268 restartRequests = value;
269 return *this;
270 }
271
275 bool getAutoCommit() const
276 {
277 return autoCommit;
278 }
279
284 {
285 autoCommit = value;
286 return *this;
287 }
288
289 private:
290 std::vector<std::uint8_t> tpb;
291 std::optional<TransactionIsolationLevel> isolationLevel;
292 std::optional<TransactionReadCommittedMode> readCommittedMode;
293 std::optional<TransactionAccessMode> accessMode;
294 std::optional<TransactionWaitMode> waitMode;
295 bool noAutoUndo = false;
296 bool ignoreLimbo = false;
297 bool restartRequests = false;
298 bool autoCommit = false;
299 };
300
301 class Client;
302
307 {
311 ACTIVE,
312
316 PREPARED,
317
321 COMMITTED,
322
327 };
328
350 class Transaction final
351 {
352 public:
357 explicit Transaction(Attachment& attachment, const TransactionOptions& options = {});
358
363 explicit Transaction(Attachment& attachment, std::string_view setTransactionCmd);
364
374 explicit Transaction(
375 std::span<std::reference_wrapper<Attachment>> attachments, const TransactionOptions& options = {});
376
382 : client{o.client},
383 handle{std::move(o.handle)},
384 state{o.state},
385 isMultiDatabase{o.isMultiDatabase}
386 {
388 }
389
390 Transaction& operator=(Transaction&&) = delete;
391
392 Transaction(const Transaction&) = delete;
393 Transaction& operator=(const Transaction&) = delete;
394
401 ~Transaction() noexcept
402 {
403 if (isValid())
404 {
405 assert(state != TransactionState::PREPARED &&
406 "Prepared transaction must be explicitly committed or rolled back");
407
408 try
409 {
410 if (state == TransactionState::ACTIVE)
411 rollback();
412 }
413 catch (...)
414 {
415 // swallow
416 }
417 }
418 }
419
420 public:
424 bool isValid() noexcept
425 {
426 return handle != nullptr;
427 }
428
433 {
434 return handle;
435 }
436
440 TransactionState getState() const noexcept
441 {
442 return state;
443 }
444
451 void prepare();
452
458 void prepare(std::string_view message);
459
465 void prepare(std::span<const std::uint8_t> message);
466
472 void commit();
473
479 void commitRetaining();
480
486 void rollback();
487
493 void rollbackRetaining();
494
495 private:
496 Client& client;
499 const bool isMultiDatabase = false;
500 };
501} // namespace fbcpp
502
503
504#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
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.
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.
void prepare()
Prepares the transaction for two-phase commit (2PC phase 1).
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.
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
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:88
@ READ_ONLY
Transaction can only read data; write operations are not permitted.
@ READ_WRITE
Transaction can read and write data.
TransactionReadCommittedMode
Transaction read committed mode.
Definition Transaction.h:72
@ 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.
TransactionIsolationLevel
Transaction isolation level.
Definition Transaction.h:51
@ 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.