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#include <cstdint>
40
41
45namespace fbcpp
46{
47 class Attachment;
48
53 {
58
63
68 };
69
85
90 {
95
100 };
101
106 {
110 NO_WAIT,
111
115 WAIT
116 };
117
122 {
123 public:
128 const std::vector<std::uint8_t>& getTpb() const
129 {
130 return tpb;
131 }
132
137 TransactionOptions& setTpb(const std::vector<std::uint8_t>& value)
138 {
139 tpb = value;
140 return *this;
141 }
142
147 TransactionOptions& setTpb(std::vector<std::uint8_t>&& value)
148 {
149 tpb = std::move(value);
150 return *this;
151 }
152
156 const std::optional<TransactionIsolationLevel> getIsolationLevel() const
157 {
158 return isolationLevel;
159 }
160
165 {
166 isolationLevel = value;
167 return *this;
168 }
169
173 const std::optional<TransactionReadCommittedMode> getReadCommittedMode() const
174 {
175 return readCommittedMode;
176 }
177
182 {
183 readCommittedMode = value;
184 return *this;
185 }
186
190 const std::optional<TransactionAccessMode> getAccessMode() const
191 {
192 return accessMode;
193 }
194
199 {
200 accessMode = value;
201 return *this;
202 }
203
207 const std::optional<TransactionWaitMode> getWaitMode() const
208 {
209 return waitMode;
210 }
211
216 {
217 waitMode = value;
218 return *this;
219 }
220
225 bool getNoAutoUndo() const
226 {
227 return noAutoUndo;
228 }
229
235 {
236 noAutoUndo = value;
237 return *this;
238 }
239
243 bool getIgnoreLimbo() const
244 {
245 return ignoreLimbo;
246 }
247
252 {
253 ignoreLimbo = value;
254 return *this;
255 }
256
261 {
262 return restartRequests;
263 }
264
269 {
270 restartRequests = value;
271 return *this;
272 }
273
277 bool getAutoCommit() const
278 {
279 return autoCommit;
280 }
281
286 {
287 autoCommit = value;
288 return *this;
289 }
290
291 private:
292 std::vector<std::uint8_t> tpb;
293 std::optional<TransactionIsolationLevel> isolationLevel;
294 std::optional<TransactionReadCommittedMode> readCommittedMode;
295 std::optional<TransactionAccessMode> accessMode;
296 std::optional<TransactionWaitMode> waitMode;
297 bool noAutoUndo = false;
298 bool ignoreLimbo = false;
299 bool restartRequests = false;
300 bool autoCommit = false;
301 };
302
303 class Client;
304
309 {
313 ACTIVE,
314
318 PREPARED,
319
323 COMMITTED,
324
329 };
330
352 class FB_CPP_EXPORT Transaction final
353 {
354 public:
359 explicit Transaction(Attachment& attachment, const TransactionOptions& options = {});
360
365 explicit Transaction(Attachment& attachment, std::string_view setTransactionCmd);
366
376 explicit Transaction(
377 std::span<std::reference_wrapper<Attachment>> attachments, const TransactionOptions& options = {});
378
384 : client{o.client},
385 handle{std::move(o.handle)},
386 state{o.state},
387 isMultiDatabase{o.isMultiDatabase}
388 {
389 o.state = TransactionState::ROLLED_BACK;
390 }
391
392 Transaction& operator=(Transaction&&) = delete;
393
394 Transaction(const Transaction&) = delete;
395 Transaction& operator=(const Transaction&) = delete;
396
403 ~Transaction() noexcept
404 {
405 if (isValid())
406 {
407 assert(state != TransactionState::PREPARED &&
408 "Prepared transaction must be explicitly committed or rolled back");
409
410 try
411 {
412 if (state == TransactionState::ACTIVE)
413 rollback();
414 }
415 catch (...)
416 {
417 // swallow
418 }
419 }
420 }
421
422 public:
426 bool isValid() noexcept
427 {
428 return handle != nullptr;
429 }
430
435 {
436 return handle;
437 }
438
442 TransactionState getState() const noexcept
443 {
444 return state;
445 }
446
453 void prepare();
454
460 void prepare(std::string_view message);
461
467 void prepare(std::span<const std::uint8_t> message);
468
474 void commit();
475
481 void commitRetaining();
482
488 void rollback();
489
495 void rollbackRetaining();
496
497 private:
498 Client& client;
500 TransactionState state = TransactionState::ACTIVE;
501 const bool isMultiDatabase = false;
502 };
503} // namespace fbcpp
504
505
506#endif // FBCPP_TRANSACTION_H
Represents a connection to a Firebird database.
Definition Attachment.h:220
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 Array.h:43
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:90
TransactionReadCommittedMode
Transaction read committed mode.
Definition Transaction.h:74
@ 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:53
@ 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.