5#if defined(_WIN32) || defined(_WIN64)
10#include "SqlConnectInfo.hpp"
11#include "SqlError.hpp"
12#include "SqlLogger.hpp"
13#include "SqlServerType.hpp"
24#include <system_error>
50 LIGHTWEIGHT_API
explicit SqlConnection(std::optional<SqlConnectionString> connectInfo);
83 return m_connectionId;
87 LIGHTWEIGHT_API
void Close() noexcept;
108 [[nodiscard]] LIGHTWEIGHT_API std::
string UserName() const;
111 [[nodiscard]] LIGHTWEIGHT_API std::
string ServerName() const;
117 [[nodiscard]] SqlServerType
ServerType() const noexcept;
120 [[nodiscard]] std::
string const&
DriverName() const noexcept;
136 std::string_view
const& tableAlias)
const;
148 [[nodiscard]] LIGHTWEIGHT_API
bool IsAlive() const noexcept;
160 [[nodiscard]] LIGHTWEIGHT_API std::chrono::steady_clock::time_point
LastUsed() const noexcept;
163 LIGHTWEIGHT_API
void SetLastUsed(std::chrono::steady_clock::time_point lastUsed) noexcept;
167 std::source_location sourceLocation = std::source_location::current()) const;
175 uint64_t m_connectionId;
176 SqlServerType m_serverType = SqlServerType::UNKNOWN;
178 std::string m_driverName;
196 return *m_queryFormatter;
Represents a connection to a SQL database.
static LIGHTWEIGHT_API void SetDefaultDataSource(SqlConnectionDataSource const &dataSource) noexcept
Sets the default connection information as SqlConnectionDataSource.
LIGHTWEIGHT_API SqlQueryBuilder Query(std::string_view const &table={}) const
SqlServerType ServerType() const noexcept
Retrieves the type of the server.
LIGHTWEIGHT_API SqlConnection()
Constructs a new SQL connection to the default connection.
LIGHTWEIGHT_API void SetLastUsed(std::chrono::steady_clock::time_point lastUsed) noexcept
Sets the last time the connection was used.
uint64_t ConnectionId() const noexcept
Retrieves the connection ID.
LIGHTWEIGHT_API SqlQueryBuilder QueryAs(std::string_view const &table, std::string_view const &tableAlias) const
static LIGHTWEIGHT_API void SetDefaultConnectionString(SqlConnectionString const &connectionString) noexcept
LIGHTWEIGHT_API bool TransactionActive() const noexcept
Tests if a transaction is active.
SQLHDBC NativeHandle() const noexcept
Retrieves the native handle.
static LIGHTWEIGHT_API void ResetPostConnectedHook()
Resets the post connected hook.
LIGHTWEIGHT_API bool TransactionsAllowed() const noexcept
Tests if transactions are allowed.
LIGHTWEIGHT_API std::string ServerName() const
Retrieves the name of the server.
LIGHTWEIGHT_API std::string UserName() const
Retrieves the name of the user.
LIGHTWEIGHT_API bool Connect(SqlConnectionDataSource const &info) noexcept
static LIGHTWEIGHT_API void SetPostConnectedHook(std::function< void(SqlConnection &)> hook)
Sets a callback to be called after each connection being established.
LIGHTWEIGHT_API bool IsAlive() const noexcept
Tests if the connection is still active.
static LIGHTWEIGHT_API SqlConnectionString const & DefaultConnectionString() noexcept
Retrieves the default connection information.
LIGHTWEIGHT_API SqlConnectionString const & ConnectionString() const noexcept
Retrieves the connection information.
std::string const & DriverName() const noexcept
Retrieves the name of the driver used for this connection.
LIGHTWEIGHT_API std::string DatabaseName() const
Retrieves the name of the database in use.
LIGHTWEIGHT_API void Close() noexcept
Closes the connection (attempting to put it back into the connect[[ion pool).
LIGHTWEIGHT_API SqlMigrationQueryBuilder Migration() const
Creates a new migration query builder, compatible the current connection.
SqlQueryFormatter const & QueryFormatter() const noexcept
Retrieves a query formatter suitable for the SQL server being connected.
LIGHTWEIGHT_API SqlConnection(std::optional< SqlConnectionString > connectInfo)
Constructs a new SQL connection to the given connect informaton.
LIGHTWEIGHT_API void RequireSuccess(SQLRETURN sqlResult, std::source_location sourceLocation=std::source_location::current()) const
Checks the result of an SQL operation, and throws an exception if it is not successful.
LIGHTWEIGHT_API SqlErrorInfo LastError() const
Retrieves the last error information with respect to this SQL connection handle.
LIGHTWEIGHT_API std::string ServerVersion() const
Retrieves the reported server version.
LIGHTWEIGHT_API std::chrono::steady_clock::time_point LastUsed() const noexcept
Retrieves the last time the connection was used.
Query builder for building SQL migration queries.
API Entry point for building SQL queries.
Represents a connection data source as a DSN, username, password, and timeout.
Represents an ODBC connection string.
Represents an ODBC SQL error.