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()}
143 StatusWrapper statusWrapper{client, status.get()};
145 auto tpbBuilder = buildTpb(master, statusWrapper, options);
146 const auto tpbBuffer = tpbBuilder->getBuffer(&statusWrapper);
147 const auto tpbBufferLen = tpbBuilder->getBufferLength(&statusWrapper);
149 handle.reset(attachment.
getHandle()->startTransaction(&statusWrapper, tpbBufferLen, tpbBuffer));
153 : client{attachment.getClient()}
158 StatusWrapper statusWrapper{client, status.get()};
161 attachment.
getHandle()->execute(&statusWrapper,
nullptr,
static_cast<unsigned>(setTransactionCmd.length()),
162 setTransactionCmd.data(), SQL_DIALECT_V6,
nullptr,
nullptr,
nullptr,
nullptr));
166 : client{attachments[0].get().getClient()},
167 isMultiDatabase{true}
169 assert(!attachments.empty());
172 for (
const auto& attachment : attachments)
174 assert(attachment.get().isValid());
176 if (&attachment.get().getClient() != &client)
177 throw std::invalid_argument(
"All attachments must use the same Client for multi-database transactions");
183 StatusWrapper statusWrapper{client, status.get()};
185 auto tpbBuilder = buildTpb(master, statusWrapper, options);
186 const auto tpbBuffer = tpbBuilder->getBuffer(&statusWrapper);
187 const auto tpbBufferLen = tpbBuilder->getBufferLength(&statusWrapper);
189 auto dtcInterface = master->getDtc();
190 auto dtcStart =
fbUnique(dtcInterface->startBuilder(&statusWrapper));
193 for (
const auto& attachment : attachments)
194 dtcStart->addWithTpb(&statusWrapper, attachment.get().getHandle().get(), tpbBufferLen, tpbBuffer);
197 handle.reset(dtcStart->start(&statusWrapper));
207 StatusWrapper statusWrapper{client, status.get()};
209 handle->rollback(&statusWrapper);
220 StatusWrapper statusWrapper{client, status.get()};
222 handle->commit(&statusWrapper);
233 StatusWrapper statusWrapper{client, status.get()};
235 handle->commitRetaining(&statusWrapper);
244 StatusWrapper statusWrapper{client, status.get()};
246 handle->rollbackRetaining(&statusWrapper);
251 prepare(std::span<const std::uint8_t>{});
260 StatusWrapper statusWrapper{client, status.get()};
262 handle->prepare(&statusWrapper,
static_cast<unsigned>(message.size()), message.data());
268 const auto messageBytes =
reinterpret_cast<const std::uint8_t*
>(message.data());
269 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.
FbUniquePtr< fb::IStatus > newStatus()
Creates and returns a Firebird IStatus instance.
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.