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
72 std::runtime_error(message)
116 SqlTransactionMode defaultMode = SqlTransactionMode::COMMIT,
117 SqlIsolationMode isolationMode = SqlIsolationMode::DriverDefault,
118 std::source_location location = std::source_location::current());
140 [[nodiscard]] SQLHDBC NativeHandle() const noexcept
147 SqlTransactionMode m_defaultMode {};
148 std::source_location m_location {};
153 return *m_connection;
159struct std::formatter<Lightweight::SqlTransactionMode>: std::formatter<std::string_view>
161 using SqlTransactionMode = Lightweight::SqlTransactionMode;
162 auto format(SqlTransactionMode value, format_context& ctx)
const -> format_context::iterator
164 using namespace std::string_view_literals;
168 case SqlTransactionMode::COMMIT:
171 case SqlTransactionMode::ROLLBACK:
174 case SqlTransactionMode::NONE:
178 return std::formatter<string_view>::format(name, ctx);
183struct std::formatter<Lightweight::SqlIsolationMode>: std::formatter<std::string_view>
185 using SqlIsolationMode = Lightweight::SqlIsolationMode;
186 auto format(SqlIsolationMode value, format_context& ctx)
const -> format_context::iterator
188 using namespace std::string_view_literals;
192 case SqlIsolationMode::DriverDefault:
193 name =
"DriverDefault";
195 case SqlIsolationMode::ReadUncommitted:
196 name =
"ReadUncommitted";
198 case SqlIsolationMode::ReadCommitted:
199 name =
"ReadCommitted";
201 case SqlIsolationMode::RepeatableRead:
202 name =
"RepeatableRead";
204 case SqlIsolationMode::Serializable:
205 name =
"Serializable";
208 return std::formatter<string_view>::format(name, ctx);
Represents a connection to a SQL database.
SQLHDBC NativeHandle() const noexcept
Retrieves the native handle.
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.