5#if defined(_WIN32) || defined(_WIN64)
9#include "SqlConnection.hpp"
10#include "SqlError.hpp"
13#include <source_location>
30enum class SqlIsolationMode : std::uint8_t
36 ReadUncommitted = SQL_TXN_READ_UNCOMMITTED,
42 ReadCommitted = SQL_TXN_READ_COMMITTED,
48 RepeatableRead = SQL_TXN_REPEATABLE_READ,
54 Serializable = SQL_TXN_SERIALIZABLE,
58enum class SqlTransactionMode : std::uint8_t
73 std::runtime_error(message)
119 SqlTransactionMode defaultMode = SqlTransactionMode::COMMIT,
120 SqlIsolationMode isolationMode = SqlIsolationMode::DriverDefault,
121 std::source_location location = std::source_location::current());
143 [[nodiscard]] SQLHDBC NativeHandle() const noexcept
150 SqlTransactionMode m_defaultMode {};
151 std::source_location m_location {};
156 return *m_connection;
162struct std::formatter<Lightweight::SqlTransactionMode>: std::formatter<std::string_view>
164 using SqlTransactionMode = Lightweight::SqlTransactionMode;
165 auto format(SqlTransactionMode value, format_context& ctx)
const -> format_context::iterator
167 using namespace std::string_view_literals;
171 case SqlTransactionMode::COMMIT:
174 case SqlTransactionMode::ROLLBACK:
177 case SqlTransactionMode::NONE:
181 return std::formatter<string_view>::format(name, ctx);
186struct std::formatter<Lightweight::SqlIsolationMode>: std::formatter<std::string_view>
188 using SqlIsolationMode = Lightweight::SqlIsolationMode;
189 auto format(SqlIsolationMode value, format_context& ctx)
const -> format_context::iterator
191 using namespace std::string_view_literals;
195 case SqlIsolationMode::DriverDefault:
196 name =
"DriverDefault";
198 case SqlIsolationMode::ReadUncommitted:
199 name =
"ReadUncommitted";
201 case SqlIsolationMode::ReadCommitted:
202 name =
"ReadCommitted";
204 case SqlIsolationMode::RepeatableRead:
205 name =
"RepeatableRead";
207 case SqlIsolationMode::Serializable:
208 name =
"Serializable";
211 return std::formatter<string_view>::format(name, ctx);
Represents a connection to a SQL database.
SQLHDBC NativeHandle() const noexcept
Retrieves the native handle.
SqlTransactionException(std::string const &message) noexcept
Constructs a transaction exception with the given error message.
SqlTransaction(SqlTransaction &&)=default
Default move constructor.
LIGHTWEIGHT_API ~SqlTransaction() noexcept
Automatically commit the transaction if not done so.
LIGHTWEIGHT_API bool TryCommit() noexcept
Try to commit the transaction, and return true if successful, falls otherwise.
LIGHTWEIGHT_API SqlTransaction(SqlConnection &connection, SqlTransactionMode defaultMode=SqlTransactionMode::COMMIT, SqlIsolationMode isolationMode=SqlIsolationMode::DriverDefault, std::source_location location=std::source_location::current())
SqlConnection & Connection() 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 void Commit()
Commit the transaction. Throws an exception if the transaction cannot be committed.
LIGHTWEIGHT_API bool TryRollback() noexcept
Try to rollback the transaction, and return true if successful, falls otherwise.
SqlTransaction & operator=(SqlTransaction &&)=default
Default move assignment operator.