5#if defined(_WIN32) || defined(_WIN64)
10#include "Async/Fwd.hpp"
11#include "SqlConnectInfo.hpp"
12#include "SqlError.hpp"
13#include "SqlLogger.hpp"
14#include "SqlServerType.hpp"
25#include <system_error>
36class SqlMigrationQueryBuilder;
37class SqlQueryFormatter;
58 LIGHTWEIGHT_API
explicit SqlConnection(std::optional<SqlConnectionString> connectInfo);
93 return m_connectionId;
97 LIGHTWEIGHT_API
void Close() noexcept;
124 [[nodiscard]] LIGHTWEIGHT_API std::
string UserName() const;
127 [[nodiscard]] LIGHTWEIGHT_API std::
string ServerName() const;
133 [[nodiscard]] SqlServerType
ServerType() const noexcept;
136 [[nodiscard]] std::
string const&
DriverName() const noexcept;
214 std::string_view
const& tableAlias)
const;
226 [[nodiscard]] LIGHTWEIGHT_API
bool IsAlive() const noexcept;
238 [[nodiscard]] LIGHTWEIGHT_API std::chrono::steady_clock::time_point
LastUsed() const noexcept;
241 LIGHTWEIGHT_API
void SetLastUsed(std::chrono::steady_clock::time_point lastUsed) noexcept;
245 std::source_location sourceLocation = std::source_location::current()) const;
262 LIGHTWEIGHT_API
void EnableAsync(Async::IExecutor& dbWorkers, Async::IResumeScheduler& resume);
272 LIGHTWEIGHT_API
void EnableAsync(std::unique_ptr<Async::IAsyncBackend> backend);
302 void EnsureHandlesAllocated();
311 uint64_t m_connectionId;
312 SqlServerType m_serverType = SqlServerType::UNKNOWN;
313 SqlQueryFormatter
const* m_queryFormatter {};
314 std::string m_driverName;
332 return *m_queryFormatter;
342 case SqlServerType::MICROSOFT_SQL:
343 case SqlServerType::POSTGRESQL:
344 case SqlServerType::SQLITE:
346 case SqlServerType::MYSQL:
347 case SqlServerType::UNKNOWN:
360 case SqlServerType::MICROSOFT_SQL:
361 case SqlServerType::POSTGRESQL:
362 case SqlServerType::SQLITE:
364 case SqlServerType::MYSQL:
365 case SqlServerType::UNKNOWN:
380 case SqlServerType::MICROSOFT_SQL:
381 case SqlServerType::SQLITE:
383 case SqlServerType::POSTGRESQL:
384 case SqlServerType::MYSQL:
385 case SqlServerType::UNKNOWN:
Represents a connection to a SQL database.
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.
static LIGHTWEIGHT_API void ResetPostConnectedHook()
Resets the post connected hook.
LIGHTWEIGHT_API void DisableAsync() noexcept
LIGHTWEIGHT_API std::string ServerName() const
Retrieves the name of the server.
SqlServerType ServerType() const noexcept
Retrieves the type of the server.
LIGHTWEIGHT_API SqlMigrationQueryBuilder Migration() const
Creates a new migration query builder, compatible the current connection.
LIGHTWEIGHT_API bool TransactionActive() const noexcept
Tests if a transaction is active.
LIGHTWEIGHT_API bool IsAlive() const noexcept
Tests if the connection is still active.
LIGHTWEIGHT_API SqlQueryBuilder Query(std::string_view const &table={}) const
LIGHTWEIGHT_API SqlQueryBuilder QueryAs(std::string_view const &table, std::string_view const &tableAlias) const
LIGHTWEIGHT_API SqlConnection(std::optional< SqlConnectionString > connectInfo)
Constructs a new SQL connection to the given connect informaton.
LIGHTWEIGHT_API bool TransactionsAllowed() const noexcept
Tests if transactions are allowed.
uint64_t ConnectionId() const noexcept
Retrieves the connection ID.
static bool RoundTripsNarrowTextByteExact(SqlServerType serverType) noexcept
Whether serverType's driver round-trips narrow (SQL_C_CHAR) character data byte-exact,...
SqlQueryFormatter const & QueryFormatter() const noexcept
Retrieves a query formatter suitable for the SQL server being connected.
bool SupportsNativeRowArrayFetch() const noexcept
Whether this connection's ODBC driver supports native row-array fetching (SQL_ATTR_ROW_ARRAY_SIZE > 1...
LIGHTWEIGHT_API void SetLastUsed(std::chrono::steady_clock::time_point lastUsed) noexcept
Sets the last time the connection was used.
LIGHTWEIGHT_API void Close() noexcept
Closes the connection (attempting to put it back into the connect[[ion pool).
LIGHTWEIGHT_API bool IsAsyncEnabled() const noexcept
LIGHTWEIGHT_API Async::IAsyncBackend & AsyncBackend()
static LIGHTWEIGHT_API void SetDefaultDataSource(SqlConnectionDataSource const &dataSource) noexcept
Sets the default connection information as SqlConnectionDataSource.
static LIGHTWEIGHT_API SqlConnectionString const & DefaultConnectionString() noexcept
Retrieves the default connection information.
LIGHTWEIGHT_API void SetDefaultPrefetchDepth(std::size_t depth) noexcept
Sets the default block-prefetch depth for statements created on this connection.
LIGHTWEIGHT_API SqlErrorInfo LastError() const
Retrieves the last error information with respect to this SQL connection handle.
LIGHTWEIGHT_API SqlConnection()
Constructs a new SQL connection to the default connection.
LIGHTWEIGHT_API SqlConnection(SqlConnection &&) noexcept
Move constructor.
static LIGHTWEIGHT_API void SetDefaultConnectionString(SqlConnectionString const &connectionString) noexcept
LIGHTWEIGHT_API std::string DatabaseName() const
Retrieves the name of the database in use.
bool SupportsNativeRowBatch() const noexcept
Whether this connection's ODBC driver supports native parameter-array binding (SQL_ATTR_PARAMSET_SIZE...
LIGHTWEIGHT_API std::size_t DefaultPrefetchDepth() const noexcept
The default block-prefetch depth applied to statements created on this connection.
LIGHTWEIGHT_API std::chrono::steady_clock::time_point LastUsed() const noexcept
Retrieves the last time the connection was used.
SQLHDBC NativeHandle() const noexcept
Retrieves the native handle.
LIGHTWEIGHT_API std::string ServerVersion() const
Retrieves the reported server version.
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 UserName() const
Retrieves the name of the user.
static LIGHTWEIGHT_API void SetPostConnectedHook(std::function< void(SqlConnection &)> hook)
Sets a callback to be called after each connection being established.
LIGHTWEIGHT_API void EnableAsync(Async::IExecutor &dbWorkers, Async::IResumeScheduler &resume)
LIGHTWEIGHT_API bool Connect(SqlConnectionDataSource const &info) noexcept
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.