15 :
std::runtime_error(error.message), error_code_(error.
error_code) {}
18 :
std::runtime_error(message), error_code_(0) {}
39 : connection_(connection), committed_(false), rolled_back_(false) {
40 auto result = connection_.get().begin_transaction(isolation_level);
48 if (!committed_ && !rolled_back_ && connection_.get().in_transaction()) {
63 : connection_(other.connection_), committed_(other.committed_),
64 rolled_back_(other.rolled_back_) {
66 other.rolled_back_ =
true;
72 if (!committed_ && !rolled_back_ && connection_.get().in_transaction()) {
80 connection_ = other.connection_;
81 committed_ = other.committed_;
82 rolled_back_ = other.rolled_back_;
85 other.rolled_back_ =
true;
93 if (committed_ || rolled_back_) {
97 auto result = connection_.get().commit_transaction();
108 if (committed_ || rolled_back_) {
112 auto result = connection_.get().rollback_transaction();
132 template <
typename Func>
141 std::reference_wrapper<Connection> connection_;
Abstract base class for database connections.
Exception thrown when transaction operations fail.
int error_code() const noexcept
Get the error code associated with this exception.
TransactionException(const ConnectionError &error)
TransactionException(const std::string &message)
RAII wrapper for database transactions.
TransactionGuard(Connection &connection, IsolationLevel isolation_level=IsolationLevel::ReadCommitted)
Constructor that begins a transaction.
TransactionGuard & operator=(TransactionGuard &&other) noexcept
void rollback()
Roll back the transaction.
~TransactionGuard() noexcept
Destructor that rolls back the transaction if it wasn't committed or rolled back.
TransactionGuard & operator=(const TransactionGuard &)=delete
void commit()
Commit the transaction.
static void with_transaction(Connection &connection, Func &&func, IsolationLevel isolation_level=IsolationLevel::ReadCommitted)
Execute a query within the transaction and commit on success.
bool is_rolled_back() const noexcept
Check if the transaction has been rolled back.
TransactionGuard(TransactionGuard &&other) noexcept
bool is_committed() const noexcept
Check if the transaction has been committed.
TransactionGuard(const TransactionGuard &)=delete
IsolationLevel
Transaction isolation levels.
@ ReadCommitted
Prevents dirty reads.
Error type for database connection operations.