25#include "Transaction.h"
26#include "Attachment.h"
32using namespace fbcpp::impl;
38 auto tpbBuilder =
fbUnique(master->getUtilInterface()->getXpbBuilder(&statusWrapper, fb::IXpbBuilder::TPB,
39 reinterpret_cast<const std::uint8_t*
>(options.
getTpb().data()),
40 static_cast<unsigned>(options.
getTpb().size())));
44 switch (accessMode.value())
46 case TransactionAccessMode::READ_ONLY:
47 tpbBuilder->insertTag(&statusWrapper, isc_tpb_read);
50 case TransactionAccessMode::READ_WRITE:
51 tpbBuilder->insertTag(&statusWrapper, isc_tpb_write);
62 switch (waitMode.value())
64 case TransactionWaitMode::NO_WAIT:
65 tpbBuilder->insertTag(&statusWrapper, isc_tpb_nowait);
68 case TransactionWaitMode::WAIT:
69 tpbBuilder->insertTag(&statusWrapper, isc_tpb_wait);
80 switch (isolationLevel.value())
82 case TransactionIsolationLevel::CONSISTENCY:
83 tpbBuilder->insertTag(&statusWrapper, isc_tpb_consistency);
86 case TransactionIsolationLevel::SNAPSHOT:
87 tpbBuilder->insertTag(&statusWrapper, isc_tpb_concurrency);
90 case TransactionIsolationLevel::READ_COMMITTED:
91 tpbBuilder->insertTag(&statusWrapper, isc_tpb_read_committed);
95 switch (readCommittedMode.value())
97 case TransactionReadCommittedMode::NO_RECORD_VERSION:
98 tpbBuilder->insertTag(&statusWrapper, isc_tpb_no_rec_version);
101 case TransactionReadCommittedMode::RECORD_VERSION:
102 tpbBuilder->insertTag(&statusWrapper, isc_tpb_rec_version);
120 tpbBuilder->insertTag(&statusWrapper, isc_tpb_no_auto_undo);
123 tpbBuilder->insertTag(&statusWrapper, isc_tpb_ignore_limbo);
126 tpbBuilder->insertTag(&statusWrapper, isc_tpb_restart_requests);
129 tpbBuilder->insertTag(&statusWrapper, isc_tpb_autocommit);
136 : client{attachment.getClient()}
142 StatusWrapper statusWrapper{client};
144 auto tpbBuilder = buildTpb(master, statusWrapper, options);
145 const auto tpbBuffer = tpbBuilder->getBuffer(&statusWrapper);
146 const auto tpbBufferLen = tpbBuilder->getBufferLength(&statusWrapper);
148 handle.reset(attachment.
getHandle()->startTransaction(&statusWrapper, tpbBufferLen, tpbBuffer));
152 : client{attachment.getClient()}
156 StatusWrapper statusWrapper{client};
159 attachment.
getHandle()->execute(&statusWrapper,
nullptr,
static_cast<unsigned>(setTransactionCmd.length()),
160 setTransactionCmd.data(), SQL_DIALECT_V6,
nullptr,
nullptr,
nullptr,
nullptr));
164 : client{attachments[0].get().getClient()},
165 isMultiDatabase{true}
167 assert(!attachments.empty());
170 for (
const auto& attachment : attachments)
172 assert(attachment.get().isValid());
174 if (&attachment.get().getClient() != &client)
175 throw std::invalid_argument(
"All attachments must use the same Client for multi-database transactions");
180 StatusWrapper statusWrapper{client};
182 auto tpbBuilder = buildTpb(master, statusWrapper, options);
183 const auto tpbBuffer = tpbBuilder->getBuffer(&statusWrapper);
184 const auto tpbBufferLen = tpbBuilder->getBufferLength(&statusWrapper);
186 auto dtcInterface = master->getDtc();
187 auto dtcStart =
fbUnique(dtcInterface->startBuilder(&statusWrapper));
190 for (
const auto& attachment : attachments)
191 dtcStart->addWithTpb(&statusWrapper, attachment.get().getHandle().get(), tpbBufferLen, tpbBuffer);
194 handle.reset(dtcStart->start(&statusWrapper));
203 StatusWrapper statusWrapper{client};
205 handle->rollback(&statusWrapper);
215 StatusWrapper statusWrapper{client};
217 handle->commit(&statusWrapper);
227 StatusWrapper statusWrapper{client};
229 handle->commitRetaining(&statusWrapper);
237 StatusWrapper statusWrapper{client};
239 handle->rollbackRetaining(&statusWrapper);
244 prepare(std::span<const std::uint8_t>{});
252 StatusWrapper statusWrapper{client};
254 handle->prepare(&statusWrapper,
static_cast<unsigned>(message.size()), message.data());
260 const auto messageBytes =
reinterpret_cast<const std::uint8_t*
>(message.data());
261 prepare(std::span<const std::uint8_t>{messageBytes, message.size()});
Represents a connection to a Firebird database.
FbRef< fb::IAttachment > getHandle() noexcept
Returns the internal Firebird IAttachment handle.
bool isValid() noexcept
Returns whether the Attachment object is valid.
fb::IMaster * getMaster() noexcept
Returns the Firebird IMaster interface.
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.
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.
const std::optional< TransactionWaitMode > getWaitMode() const
Returns the transaction wait mode.
void rollback()
Rolls back the transaction.
void commit()
Commits the transaction.
void prepare()
Prepares the transaction for two-phase commit (2PC phase 1).
bool isValid() noexcept
Returns whether the Transaction object is valid.
void commitRetaining()
Commits the transaction while maintains it active.
void rollbackRetaining()
Rolls back the transaction while maintains it active.
Transaction(Attachment &attachment, const TransactionOptions &options={})
Constructs a Transaction object that starts a transaction in the specified Attachment using the speci...
@ 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.
FbUniquePtr< T > fbUnique(T *obj) noexcept
Creates a unique pointer for a Firebird disposable object.
std::unique_ptr< T, impl::FbDisposeDeleter > FbUniquePtr
Unique pointer type for Firebird disposable objects.