Lightweight 0.20260625.0
Loading...
Searching...
No Matches
SqlConnection.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#if defined(_WIN32) || defined(_WIN64)
6 #include <Windows.h>
7#endif
8
9#include "Api.hpp"
10#include "Async/Fwd.hpp"
11#include "SqlConnectInfo.hpp"
12#include "SqlError.hpp"
13#include "SqlLogger.hpp"
14#include "SqlServerType.hpp"
15
16#include <atomic>
17#include <chrono>
18#include <expected>
19#include <format>
20#include <functional>
21#include <memory>
22#include <optional>
23#include <string>
24#include <string_view>
25#include <system_error>
26
27#include <sql.h>
28#include <sqlext.h>
29#include <sqlspi.h>
30#include <sqltypes.h>
31
32namespace Lightweight
33{
34
35class SqlQueryBuilder;
36class SqlMigrationQueryBuilder;
37class SqlQueryFormatter;
38
39/// @brief Represents a connection to a SQL database.
40class SqlConnection final
41{
42 public:
43 /// @brief Constructs a new SQL connection to the default connection.
44 ///
45 /// The default connection is set via SetDefaultConnectInfo.
46 /// In case the default connection is not set, the connection will fail.
47 /// And in case the connection fails, the last error will be set.
48 LIGHTWEIGHT_API SqlConnection();
49
50 /// @brief Constructs a new SQL connection to the given connect informaton.
51 ///
52 /// @param connectInfo The connection information to use. If `std::nullopt`,
53 /// no connection is established and the object is left
54 /// in an unconnected state — use `Connect(...)` later.
55 /// If a value is provided and the connection fails,
56 /// `SqlException` is thrown carrying the ODBC diagnostic
57 /// read from the DBC handle.
58 LIGHTWEIGHT_API explicit SqlConnection(std::optional<SqlConnectionString> connectInfo);
59
60 /// Move constructor.
61 LIGHTWEIGHT_API SqlConnection(SqlConnection&& /*other*/) noexcept;
62 /// Move assignment operator.
63 LIGHTWEIGHT_API SqlConnection& operator=(SqlConnection&& /*other*/) noexcept;
64 SqlConnection(SqlConnection const& /*other*/) = delete;
65 SqlConnection& operator=(SqlConnection const& /*other*/) = delete;
66
67 /// Destructs this SQL connection object,
68 LIGHTWEIGHT_API ~SqlConnection() noexcept;
69
70 /// Retrieves the default connection information.
71 LIGHTWEIGHT_API static SqlConnectionString const& DefaultConnectionString() noexcept;
72
73 /// Sets the default connection information.
74 ///
75 /// @param connectionString The connection information to use.
76 LIGHTWEIGHT_API static void SetDefaultConnectionString(SqlConnectionString const& connectionString) noexcept;
77
78 /// Sets the default connection information as SqlConnectionDataSource.
79 LIGHTWEIGHT_API static void SetDefaultDataSource(SqlConnectionDataSource const& dataSource) noexcept;
80
81 /// Sets a callback to be called after each connection being established.
82 LIGHTWEIGHT_API static void SetPostConnectedHook(std::function<void(SqlConnection&)> hook);
83
84 /// Resets the post connected hook.
85 LIGHTWEIGHT_API static void ResetPostConnectedHook();
86
87 /// @brief Retrieves the connection ID.
88 ///
89 /// This is a unique identifier for the connection, which is useful for debugging purposes.
90 /// Note, this ID will not change if the connection is moved nor when it is reused via the connection pool.
91 [[nodiscard]] uint64_t ConnectionId() const noexcept
92 {
93 return m_connectionId;
94 }
95
96 /// Closes the connection (attempting to put it back into the connect[[ion pool).
97 LIGHTWEIGHT_API void Close() noexcept;
98
99 /// Connects to the given database with the given username and password.
100 ///
101 /// This method can be called on a connection that has been closed via Close().
102 /// If the ODBC handles have been freed, they will be automatically reallocated.
103 ///
104 /// @retval true if the connection was successful.
105 /// @retval false if the connection failed. Use LastError() to retrieve the error information.
106 LIGHTWEIGHT_API bool Connect(SqlConnectionDataSource const& info) noexcept;
107
108 /// Connects to the given database with the given connection string.
109 ///
110 /// This method can be called on a connection that has been closed via Close().
111 /// If the ODBC handles have been freed, they will be automatically reallocated.
112 ///
113 /// @retval true if the connection was successful.
114 /// @retval false if the connection failed. Use LastError() to retrieve the error information.
115 LIGHTWEIGHT_API bool Connect(SqlConnectionString sqlConnectionString) noexcept;
116
117 /// Retrieves the last error information with respect to this SQL connection handle.
118 [[nodiscard]] LIGHTWEIGHT_API SqlErrorInfo LastError() const;
119
120 /// Retrieves the name of the database in use.
121 [[nodiscard]] LIGHTWEIGHT_API std::string DatabaseName() const;
122
123 /// Retrieves the name of the user.
124 [[nodiscard]] LIGHTWEIGHT_API std::string UserName() const;
125
126 /// Retrieves the name of the server.
127 [[nodiscard]] LIGHTWEIGHT_API std::string ServerName() const;
128
129 /// Retrieves the reported server version.
130 [[nodiscard]] LIGHTWEIGHT_API std::string ServerVersion() const;
131
132 /// Retrieves the type of the server.
133 [[nodiscard]] SqlServerType ServerType() const noexcept;
134
135 /// Retrieves the name of the driver used for this connection.
136 [[nodiscard]] std::string const& DriverName() const noexcept;
137
138 /// Retrieves a query formatter suitable for the SQL server being connected.
139 [[nodiscard]] SqlQueryFormatter const& QueryFormatter() const noexcept;
140
141 /// @brief Whether this backend must rebuild a table to apply an `ALTER TABLE` schema change it
142 /// cannot express in place (foreign-key add/drop, or column type/nullability change).
143 ///
144 /// The migration executor consults this to decide whether a `-- LIGHTWEIGHT_SQLITE_GUARD:` sentinel
145 /// must be turned into a table rebuild (`true` for SQLite) or executed directly. Exposed on the
146 /// connection so capability decisions go through the backend, delegating the dialect knowledge to
147 /// the query formatter.
148 [[nodiscard]] LIGHTWEIGHT_API bool RequiresTableRebuildForSchemaChange() const noexcept;
149
150 /// @brief Whether this connection's ODBC driver supports native parameter-array binding
151 /// (`SQL_ATTR_PARAMSET_SIZE` > 1) for batched row-wise execution.
152 ///
153 /// The batched `SqlStatement::ExecuteBatch(rows, accessors...)` consults this to decide whether it
154 /// may submit the whole batch in a single row-wise `SQLExecute`, or must fall back to a single
155 /// prepare followed by consecutive per-row executes. This is a driver/backend capability — not a
156 /// SQL-dialect concern — so it belongs to the connection, which knows both the server type and the
157 /// driver name (either of which a future carve-out can branch on).
158 ///
159 /// @return `true` if the driver honours parameter arrays.
160 [[nodiscard]] bool SupportsNativeRowBatch() const noexcept;
161
162 /// @brief Whether this connection's ODBC driver supports native row-array fetching
163 /// (`SQL_ATTR_ROW_ARRAY_SIZE` > 1 with `SQLFetchScroll`) for block result retrieval.
164 ///
165 /// The fast retrieval path in @c SqlStatement::FetchAllRowWise consults this to decide whether
166 /// it may bind the result columns row-wise over a record block and materialize whole row blocks per
167 /// `SQLFetchScroll` round-trip, or must fall back to the per-row `SQLFetch` path. Like
168 /// @ref SupportsNativeRowBatch this is a driver/backend capability — not a SQL-dialect concern — so
169 /// it lives on the connection. Kept distinct from the parameter-array flag so a backend that honours
170 /// one but not the other can be carved out independently.
171 ///
172 /// @return `true` if the driver honours row-array fetching.
173 [[nodiscard]] bool SupportsNativeRowArrayFetch() const noexcept;
174
175 /// @brief Server-type overload of @ref SupportsNativeRowArrayFetch, for callers that hold only the
176 /// server type (e.g. the DataMapper result reader) and not the connection. Keeps the single source of
177 /// truth for this capability on the connection rather than scattering a `switch (serverType)` into
178 /// business logic.
179 /// @param serverType The backend server type to test.
180 /// @return `true` if that backend honours row-array fetching.
181 [[nodiscard]] static bool SupportsNativeRowArrayFetch(SqlServerType serverType) noexcept;
182
183 /// @brief Whether @p serverType's driver round-trips narrow (@c SQL_C_CHAR) character data
184 /// byte-exact, so a fixed-capacity char string may be array-bound narrow on the row-wise fetch path.
185 ///
186 /// PostgreSQL's psqlODBC transcodes @c SQL_C_CHAR through the client codepage (cp1252 on Windows),
187 /// mangling non-ASCII bytes — its single-row binder therefore reads narrow strings via @c SQL_C_WCHAR.
188 /// That wide round-trip needs an external per-cell buffer + conversion, which cannot be expressed as an
189 /// in-place row-wise array bind, so a record carrying a fixed-capacity string falls back to the per-row
190 /// path on PostgreSQL. MS SQL Server and SQLite read @c SQL_C_CHAR verbatim and stay on the fast path.
191 ///
192 /// @param serverType The backend server type to test.
193 /// @return `true` if narrow character data round-trips byte-exact on that backend.
194 [[nodiscard]] static bool RoundTripsNarrowTextByteExact(SqlServerType serverType) noexcept;
195
196 /// @brief The default block-prefetch depth applied to statements created on this connection.
197 ///
198 /// Classic per-row fetch loops (`while (cursor.FetchRow()) ...`, @c SqlRowIterator,
199 /// @c SqlVariantRowCursor) transparently request this many rows per @c SQLFetchScroll round-trip
200 /// instead of issuing one @c SQLFetch per row. A value <= 1 disables prefetch. Effective only when
201 /// @ref SupportsNativeRowArrayFetch is true; otherwise statements fall back to per-row fetching.
202 ///
203 /// @return The configured default prefetch depth (defaults to @c PrefetchDepthDefault).
204 [[nodiscard]] LIGHTWEIGHT_API std::size_t DefaultPrefetchDepth() const noexcept;
205
206 /// @brief Sets the default block-prefetch depth for statements created on this connection.
207 ///
208 /// @param depth Rows to request per @c SQLFetchScroll round-trip on the transparent prefetch path;
209 /// a value <= 1 disables prefetch (restoring one @c SQLFetch per row).
210 LIGHTWEIGHT_API void SetDefaultPrefetchDepth(std::size_t depth) noexcept;
211
212 /// Creates a new query builder for the given table, compatible with the current connection.
213 ///
214 /// @param table The table to query.
215 /// If not provided, the query will be a generic query builder.
216 [[nodiscard]] LIGHTWEIGHT_API SqlQueryBuilder Query(std::string_view const& table = {}) const;
217
218 /// Creates a new query builder for the given table with an alias, compatible with the current connection.
219 ///
220 /// @param table The table to query.
221 /// @param tableAlias The alias to use for the table.
222 [[nodiscard]] LIGHTWEIGHT_API SqlQueryBuilder QueryAs(std::string_view const& table,
223 std::string_view const& tableAlias) const;
224
225 /// Creates a new migration query builder, compatible the current connection.
226 [[nodiscard]] LIGHTWEIGHT_API SqlMigrationQueryBuilder Migration() const;
227
228 /// Tests if a transaction is active.
229 [[nodiscard]] LIGHTWEIGHT_API bool TransactionActive() const noexcept;
230
231 /// Tests if transactions are allowed.
232 [[nodiscard]] LIGHTWEIGHT_API bool TransactionsAllowed() const noexcept;
233
234 /// Tests if the connection is still active.
235 [[nodiscard]] LIGHTWEIGHT_API bool IsAlive() const noexcept;
236
237 /// Retrieves the connection information.
238 [[nodiscard]] LIGHTWEIGHT_API SqlConnectionString const& ConnectionString() const noexcept;
239
240 /// Retrieves the native handle.
241 [[nodiscard]] SQLHDBC NativeHandle() const noexcept
242 {
243 return m_hDbc;
244 }
245
246 /// Retrieves the last time the connection was used.
247 [[nodiscard]] LIGHTWEIGHT_API std::chrono::steady_clock::time_point LastUsed() const noexcept;
248
249 /// Sets the last time the connection was used.
250 LIGHTWEIGHT_API void SetLastUsed(std::chrono::steady_clock::time_point lastUsed) noexcept;
251
252 /// Checks the result of an SQL operation, and throws an exception if it is not successful.
253 LIGHTWEIGHT_API void RequireSuccess(SQLRETURN sqlResult,
254 std::source_location sourceLocation = std::source_location::current()) const;
255
256 /// Enables coroutine-based asynchronous methods on this connection.
257 ///
258 /// Wires the connection to an injected execution context: blocking ODBC work is offloaded to
259 /// @p dbWorkers (serialized per connection so the ODBC handle is only ever used by one thread
260 /// at a time) and the awaiting coroutine is resumed via @p resume (typically the application's
261 /// run loop). A native driver-async backend is selected when the driver advertises support;
262 /// otherwise the portable thread-offload backend is used.
263 ///
264 /// Both executors must outlive this connection and every coroutine driven through it.
265 /// Must not be called while asynchronous operations are in flight on this connection (it
266 /// replaces the backend); the connection pool calls @ref DisableAsync on return so each
267 /// re-acquire is a fresh enable rather than a live replacement.
268 ///
269 /// @param dbWorkers The worker-thread pool used to run blocking ODBC calls.
270 /// @param resume The scheduler used to resume coroutines after a blocking step completes.
271 LIGHTWEIGHT_API void EnableAsync(Async::IExecutor& dbWorkers, Async::IResumeScheduler& resume);
272
273 /// Enables the asynchronous API on this connection using an explicitly provided backend.
274 ///
275 /// This is the dependency-injection seam behind the convenience overload above: callers and
276 /// tests can supply any @ref Async::IAsyncBackend implementation (a fake/inline backend for
277 /// tests, or a future native event backend) instead of the default thread-offload backend.
278 /// The same in-flight / lifetime constraints as the convenience overload apply.
279 ///
280 /// @param backend The async execution backend to install (consumed); must not be null.
281 LIGHTWEIGHT_API void EnableAsync(std::unique_ptr<Async::IAsyncBackend> backend);
282
283 /// Tears down the asynchronous backend, returning the connection to a non-async state.
284 ///
285 /// Called by the connection pool when a mapper is returned, so a recycled connection never
286 /// carries a stale backend that references executors which may have been destroyed. Safe to
287 /// call when async was never enabled.
288 ///
289 /// @warning Must not be called while async work is in flight on this connection: it destroys the
290 /// backend (and the strand/executors an outstanding offloaded step still references), which would
291 /// race the worker still touching the ODBC handle. Await every async operation on this connection
292 /// to completion before disabling (or before returning the owning pooled @c DataMapper).
293 LIGHTWEIGHT_API void DisableAsync() noexcept;
294
295 /// @return true if @ref EnableAsync has been called on this connection (and not yet disabled).
296 [[nodiscard]] LIGHTWEIGHT_API bool IsAsyncEnabled() const noexcept;
297
298 /// Retrieves the connection's asynchronous execution backend.
299 ///
300 /// Non-const because using the backend schedules and serializes real ODBC work (via its strand and
301 /// resume scheduler), which is an observable mutation of the connection's execution state.
302 ///
303 /// @pre @ref IsAsyncEnabled returns true.
304 /// @throws std::logic_error if async has not been enabled (programmer error, fail-fast).
305 /// @return The async backend used by this connection's async methods.
306 [[nodiscard]] LIGHTWEIGHT_API Async::IAsyncBackend& AsyncBackend();
307
308 private:
309 /// Ensures ODBC handles are allocated. Called by Connect() methods.
310 /// If handles were freed by Close(), this method reallocates them.
311 void EnsureHandlesAllocated();
312
313 void PostConnect();
314
315 // Private data members
316 // Note: move/move assignment operators implemented manually
317 // if adding new data members, make sure to update them accordingly.
318 SQLHENV m_hEnv {};
319 SQLHDBC m_hDbc {};
320 uint64_t m_connectionId;
321 SqlServerType m_serverType = SqlServerType::UNKNOWN;
322 SqlQueryFormatter const* m_queryFormatter {};
323 std::string m_driverName;
324
325 struct Data;
326 Data* m_data {};
327};
328
329inline SqlServerType SqlConnection::ServerType() const noexcept
330{
331 return m_serverType;
332}
333
334inline std::string const& SqlConnection::DriverName() const noexcept
335{
336 return m_driverName;
337}
338
340{
341 return *m_queryFormatter;
342}
343
344inline bool SqlConnection::SupportsNativeRowBatch() const noexcept
345{
346 // Native ODBC parameter-array binding (SQL_ATTR_PARAMSET_SIZE > 1) is a per-driver capability.
347 // Every backend Lightweight supports and tests against honours it; an unverified backend takes the
348 // always-correct per-row path rather than risk a driver that silently ignores the parameter array.
349 switch (ServerType())
350 {
351 case SqlServerType::MICROSOFT_SQL:
352 case SqlServerType::POSTGRESQL:
353 case SqlServerType::SQLITE:
354 return true;
355 case SqlServerType::MYSQL:
356 case SqlServerType::UNKNOWN:
357 return false;
358 }
359 return false;
360}
361
362inline bool SqlConnection::SupportsNativeRowArrayFetch(SqlServerType serverType) noexcept
363{
364 // Native ODBC row-array fetching (SQL_ATTR_ROW_ARRAY_SIZE > 1 + SQLFetchScroll) is a per-driver
365 // capability. Every backend Lightweight supports and tests against honours it; an unverified backend
366 // takes the always-correct per-row SQLFetch path rather than risk a driver that mis-handles the array.
367 switch (serverType)
368 {
369 case SqlServerType::MICROSOFT_SQL:
370 case SqlServerType::POSTGRESQL:
371 case SqlServerType::SQLITE:
372 return true;
373 case SqlServerType::MYSQL:
374 case SqlServerType::UNKNOWN:
375 return false;
376 }
377 return false;
378}
379
381{
383}
384
385inline bool SqlConnection::RoundTripsNarrowTextByteExact(SqlServerType serverType) noexcept
386{
387 switch (serverType)
388 {
389 case SqlServerType::MICROSOFT_SQL:
390 case SqlServerType::SQLITE:
391 return true;
392 case SqlServerType::POSTGRESQL: // psqlODBC transcodes SQL_C_CHAR through the client codepage
393 case SqlServerType::MYSQL:
394 case SqlServerType::UNKNOWN:
395 return false;
396 }
397 return false;
398}
399
400} // namespace Lightweight
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.
LIGHTWEIGHT_API bool RequiresTableRebuildForSchemaChange() const noexcept
Whether this backend must rebuild a table to apply an ALTER TABLE schema change it cannot express in ...
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.
Definition Migrate.hpp:477
API Entry point for building SQL queries.
Definition SqlQuery.hpp:32
API to format SQL queries for different SQL dialects.
Represents a connection data source as a DSN, username, password, and timeout.
Represents an ODBC connection string.
Represents an ODBC SQL error.
Definition SqlError.hpp:33