Lightweight 0.1.0
Loading...
Searching...
No Matches
SqlTransaction Class Reference

#include <SqlTransaction.hpp>

Public Member Functions

 SqlTransaction (SqlTransaction const &)=delete
 
SqlTransactionoperator= (SqlTransaction const &)=delete
 
 SqlTransaction (SqlTransaction &&)=default
 
SqlTransactionoperator= (SqlTransaction &&)=default
 
LIGHTWEIGHT_API SqlTransaction (SqlConnection &connection, SqlTransactionMode defaultMode=SqlTransactionMode::COMMIT, SqlIsolationMode isolationMode=SqlIsolationMode::DriverDefault, std::source_location location=std::source_location::current())
 
LIGHTWEIGHT_API ~SqlTransaction () noexcept
 Automatically commit the transaction if not done so.
 
SqlConnectionConnection () noexcept
 Get the connection object associated with this transaction.
 
LIGHTWEIGHT_API void Rollback ()
 Rollback the transaction. Throws an exception if the transaction cannot be rolled back.
 
LIGHTWEIGHT_API bool TryRollback () noexcept
 Try to rollback the transaction, and return true if successful, falls otherwise.
 
LIGHTWEIGHT_API void Commit ()
 Commit the transaction. Throws an exception if the transaction cannot be committed.
 
LIGHTWEIGHT_API bool TryCommit () noexcept
 Try to commit the transaction, and return true if successful, falls otherwise.
 

Detailed Description

Represents a transaction to a SQL database.

This class is used to control the transaction manually. It disables the auto-commit mode when constructed, and automatically commits the transaction when destructed if not done so.

This class is designed with RAII in mind, so that the transaction is automatically committed or rolled back when the object goes out of scope.

void doSomeWork(SqlTransaction& transaction)
{
auto stmt = SqlStatement { transaction.Connection() };
stmt.ExecuteDirect("INSERT INTO table (column) VALUES (42)"); // Do some work
}
void main()
{
auto connection = SqlConnection {};
auto transaction = SqlTransaction { connection };
doSomeWork(transaction);
transaction.Commit();
}
Represents a connection to a SQL database.
High level API for (prepared) raw SQL statements.
SqlConnection & Connection() noexcept
Get the connection object associated with this transaction.
LIGHTWEIGHT_API void Commit()
Commit the transaction. Throws an exception if the transaction cannot be committed.

Definition at line 100 of file SqlTransaction.hpp.

Constructor & Destructor Documentation

◆ SqlTransaction()

LIGHTWEIGHT_API SqlTransaction::SqlTransaction ( SqlConnection connection,
SqlTransactionMode  defaultMode = SqlTransactionMode::COMMIT,
SqlIsolationMode  isolationMode = SqlIsolationMode::DriverDefault,
std::source_location  location = std::source_location::current() 
)
explicit

Construct a new SqlTransaction object, and disable the auto-commit mode, so that the transaction can be controlled manually.

Member Function Documentation

◆ Connection()

SqlConnection & SqlTransaction::Connection ( )
inlinenoexcept

Get the connection object associated with this transaction.

Definition at line 148 of file SqlTransaction.hpp.


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