relx 0.1.0
A Modern C++23 Type-Safe SQL Query Builder
Loading...
Searching...
No Matches
relx::connection::TransactionGuard Class Reference

RAII wrapper for database transactions. More...

#include <relx/connection/transaction_guard.hpp>

Public Member Functions

 TransactionGuard (Connection &connection, IsolationLevel isolation_level=IsolationLevel::ReadCommitted)
 Constructor that begins a transaction.
 
 ~TransactionGuard () noexcept
 Destructor that rolls back the transaction if it wasn't committed or rolled back.
 
 TransactionGuard (const TransactionGuard &)=delete
 
TransactionGuardoperator= (const TransactionGuard &)=delete
 
 TransactionGuard (TransactionGuard &&other) noexcept
 
TransactionGuardoperator= (TransactionGuard &&other) noexcept
 
void commit ()
 Commit the transaction.
 
void rollback ()
 Roll back the transaction.
 
bool is_committed () const noexcept
 Check if the transaction has been committed.
 
bool is_rolled_back () const noexcept
 Check if the transaction has been rolled back.
 

Static Public Member Functions

template<typename Func >
static void with_transaction (Connection &connection, Func &&func, IsolationLevel isolation_level=IsolationLevel::ReadCommitted)
 Execute a query within the transaction and commit on success.
 

Detailed Description

RAII wrapper for database transactions.

Automatically begins a transaction on construction and either commits or rolls back on destruction, depending on whether commit() was called or an error occurred.

Definition at line 31 of file transaction_guard.hpp.

Constructor & Destructor Documentation

◆ TransactionGuard() [1/3]

relx::connection::TransactionGuard::TransactionGuard ( Connection connection,
IsolationLevel  isolation_level = IsolationLevel::ReadCommitted 
)
inlineexplicit

Constructor that begins a transaction.

Parameters
connectionThe database connection to use
isolation_levelThe isolation level for the transaction
Exceptions
TransactionExceptionif the transaction cannot be started

Definition at line 37 of file transaction_guard.hpp.

◆ ~TransactionGuard()

relx::connection::TransactionGuard::~TransactionGuard ( )
inlinenoexcept

Destructor that rolls back the transaction if it wasn't committed or rolled back.

Definition at line 47 of file transaction_guard.hpp.

◆ TransactionGuard() [2/3]

relx::connection::TransactionGuard::TransactionGuard ( const TransactionGuard )
delete

◆ TransactionGuard() [3/3]

relx::connection::TransactionGuard::TransactionGuard ( TransactionGuard &&  other)
inlinenoexcept

Definition at line 62 of file transaction_guard.hpp.

Member Function Documentation

◆ commit()

void relx::connection::TransactionGuard::commit ( )
inline

Commit the transaction.

Exceptions
TransactionExceptionif the commit fails

Definition at line 92 of file transaction_guard.hpp.

◆ is_committed()

bool relx::connection::TransactionGuard::is_committed ( ) const
inlinenoexcept

Check if the transaction has been committed.

Returns
True if the transaction was committed

Definition at line 122 of file transaction_guard.hpp.

◆ is_rolled_back()

bool relx::connection::TransactionGuard::is_rolled_back ( ) const
inlinenoexcept

Check if the transaction has been rolled back.

Returns
True if the transaction was rolled back

Definition at line 126 of file transaction_guard.hpp.

◆ operator=() [1/2]

TransactionGuard & relx::connection::TransactionGuard::operator= ( const TransactionGuard )
delete

◆ operator=() [2/2]

TransactionGuard & relx::connection::TransactionGuard::operator= ( TransactionGuard &&  other)
inlinenoexcept

Definition at line 69 of file transaction_guard.hpp.

◆ rollback()

void relx::connection::TransactionGuard::rollback ( )
inline

Roll back the transaction.

Exceptions
TransactionExceptionif the rollback fails

Definition at line 107 of file transaction_guard.hpp.

◆ with_transaction()

template<typename Func >
static void relx::connection::TransactionGuard::with_transaction ( Connection connection,
Func &&  func,
IsolationLevel  isolation_level = IsolationLevel::ReadCommitted 
)
inlinestatic

Execute a query within the transaction and commit on success.

Template Parameters
FuncA callable that executes database operations
Parameters
funcThe function to execute within the transaction
Exceptions
Anyexception thrown by func, or TransactionException for transaction errors

Definition at line 133 of file transaction_guard.hpp.


The documentation for this class was generated from the following file: