Lightweight 0.20260617.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 connection's ODBC driver supports native parameter-array binding
142 /// (`SQL_ATTR_PARAMSET_SIZE` > 1) for batched row-wise execution.
143 ///
144 /// The batched `SqlStatement::ExecuteBatch(rows, accessors...)` consults this to decide whether it
145 /// may submit the whole batch in a single row-wise `SQLExecute`, or must fall back to a single
146 /// prepare followed by consecutive per-row executes. This is a driver/backend capability — not a
147 /// SQL-dialect concern — so it belongs to the connection, which knows both the server type and the
148 /// driver name (either of which a future carve-out can branch on).
149 ///
150 /// @return `true` if the driver honours parameter arrays.
151 [[nodiscard]] bool SupportsNativeRowBatch() const noexcept;
152
153 /// @brief Whether this connection's ODBC driver supports native row-array fetching
154 /// (`SQL_ATTR_ROW_ARRAY_SIZE` > 1 with `SQLFetchScroll`) for block result retrieval.
155 ///
156 /// The fast retrieval path in @c SqlStatement::FetchAllRowWise consults this to decide whether
157 /// it may bind the result columns row-wise over a record block and materialize whole row blocks per
158 /// `SQLFetchScroll` round-trip, or must fall back to the per-row `SQLFetch` path. Like
159 /// @ref SupportsNativeRowBatch this is a driver/backend capability — not a SQL-dialect concern — so
160 /// it lives on the connection. Kept distinct from the parameter-array flag so a backend that honours
161 /// one but not the other can be carved out independently.
162 ///
163 /// @return `true` if the driver honours row-array fetching.
164 [[nodiscard]] bool SupportsNativeRowArrayFetch() const noexcept;
165
166 /// @brief Server-type overload of @ref SupportsNativeRowArrayFetch, for callers that hold only the
167 /// server type (e.g. the DataMapper result reader) and not the connection. Keeps the single source of
168 /// truth for this capability on the connection rather than scattering a `switch (serverType)` into
169 /// business logic.
170 /// @param serverType The backend server type to test.
171 /// @return `true` if that backend honours row-array fetching.
172 [[nodiscard]] static bool SupportsNativeRowArrayFetch(SqlServerType serverType) noexcept;
173
174 /// @brief Whether @p serverType's driver round-trips narrow (@c SQL_C_CHAR) character data
175 /// byte-exact, so a fixed-capacity char string may be array-bound narrow on the row-wise fetch path.
176 ///
177 /// PostgreSQL's psqlODBC transcodes @c SQL_C_CHAR through the client codepage (cp1252 on Windows),
178 /// mangling non-ASCII bytes — its single-row binder therefore reads narrow strings via @c SQL_C_WCHAR.
179 /// That wide round-trip needs an external per-cell buffer + conversion, which cannot be expressed as an
180 /// in-place row-wise array bind, so a record carrying a fixed-capacity string falls back to the per-row
181 /// path on PostgreSQL. MS SQL Server and SQLite read @c SQL_C_CHAR verbatim and stay on the fast path.
182 ///
183 /// @param serverType The backend server type to test.
184 /// @return `true` if narrow character data round-trips byte-exact on that backend.
185 [[nodiscard]] static bool RoundTripsNarrowTextByteExact(SqlServerType serverType) noexcept;
186
187 /// @brief The default block-prefetch depth applied to statements created on this connection.
188 ///
189 /// Classic per-row fetch loops (`while (cursor.FetchRow()) ...`, @c SqlRowIterator,
190 /// @c SqlVariantRowCursor) transparently request this many rows per @c SQLFetchScroll round-trip
191 /// instead of issuing one @c SQLFetch per row. A value <= 1 disables prefetch. Effective only when
192 /// @ref SupportsNativeRowArrayFetch is true; otherwise statements fall back to per-row fetching.
193 ///
194 /// @return The configured default prefetch depth (defaults to @c PrefetchDepthDefault).
195 [[nodiscard]] LIGHTWEIGHT_API std::size_t DefaultPrefetchDepth() const noexcept;
196
197 /// @brief Sets the default block-prefetch depth for statements created on this connection.
198 ///
199 /// @param depth Rows to request per @c SQLFetchScroll round-trip on the transparent prefetch path;
200 /// a value <= 1 disables prefetch (restoring one @c SQLFetch per row).
201 LIGHTWEIGHT_API void SetDefaultPrefetchDepth(std::size_t depth) noexcept;
202
203 /// Creates a new query builder for the given table, compatible with the current connection.
204 ///
205 /// @param table The table to query.
206 /// If not provided, the query will be a generic query builder.
207 [[nodiscard]] LIGHTWEIGHT_API SqlQueryBuilder Query(std::string_view const& table = {}) const;
208
209 /// Creates a new query builder for the given table with an alias, compatible with the current connection.
210 ///
211 /// @param table The table to query.
212 /// @param tableAlias The alias to use for the table.
213 [[nodiscard]] LIGHTWEIGHT_API SqlQueryBuilder QueryAs(std::string_view const& table,
214 std::string_view const& tableAlias) const;
215
216 /// Creates a new migration query builder, compatible the current connection.
217 [[nodiscard]] LIGHTWEIGHT_API SqlMigrationQueryBuilder Migration() const;
218
219 /// Tests if a transaction is active.
220 [[nodiscard]] LIGHTWEIGHT_API bool TransactionActive() const noexcept;
221
222 /// Tests if transactions are allowed.
223 [[nodiscard]] LIGHTWEIGHT_API bool TransactionsAllowed() const noexcept;
224
225 /// Tests if the connection is still active.
226 [[nodiscard]] LIGHTWEIGHT_API bool IsAlive() const noexcept;
227
228 /// Retrieves the connection information.
229 [[nodiscard]] LIGHTWEIGHT_API SqlConnectionString const& ConnectionString() const noexcept;
230
231 /// Retrieves the native handle.
232 [[nodiscard]] SQLHDBC NativeHandle() const noexcept
233 {
234 return m_hDbc;
235 }
236
237 /// Retrieves the last time the connection was used.
238 [[nodiscard]] LIGHTWEIGHT_API std::chrono::steady_clock::time_point LastUsed() const noexcept;
239
240 /// Sets the last time the connection was used.
241 LIGHTWEIGHT_API void SetLastUsed(std::chrono::steady_clock::time_point lastUsed) noexcept;
242
243 /// Checks the result of an SQL operation, and throws an exception if it is not successful.
244 LIGHTWEIGHT_API void RequireSuccess(SQLRETURN sqlResult,
245 std::source_location sourceLocation = std::source_location::current()) const;
246
247 /// Enables coroutine-based asynchronous methods on this connection.
248 ///
249 /// Wires the connection to an injected execution context: blocking ODBC work is offloaded to
250 /// @p dbWorkers (serialized per connection so the ODBC handle is only ever used by one thread
251 /// at a time) and the awaiting coroutine is resumed via @p resume (typically the application's
252 /// run loop). A native driver-async backend is selected when the driver advertises support;
253 /// otherwise the portable thread-offload backend is used.
254 ///
255 /// Both executors must outlive this connection and every coroutine driven through it.
256 /// Must not be called while asynchronous operations are in flight on this connection (it
257 /// replaces the backend); the connection pool calls @ref DisableAsync on return so each
258 /// re-acquire is a fresh enable rather than a live replacement.
259 ///
260 /// @param dbWorkers The worker-thread pool used to run blocking ODBC calls.
261 /// @param resume The scheduler used to resume coroutines after a blocking step completes.
262 LIGHTWEIGHT_API void EnableAsync(Async::IExecutor& dbWorkers, Async::IResumeScheduler& resume);
263
264 /// Enables the asynchronous API on this connection using an explicitly provided backend.
265 ///
266 /// This is the dependency-injection seam behind the convenience overload above: callers and
267 /// tests can supply any @ref Async::IAsyncBackend implementation (a fake/inline backend for
268 /// tests, or a future native event backend) instead of the default thread-offload backend.
269 /// The same in-flight / lifetime constraints as the convenience overload apply.
270 ///
271 /// @param backend The async execution backend to install (consumed); must not be null.
272 LIGHTWEIGHT_API void EnableAsync(std::unique_ptr<Async::IAsyncBackend> backend);
273
274 /// Tears down the asynchronous backend, returning the connection to a non-async state.
275 ///
276 /// Called by the connection pool when a mapper is returned, so a recycled connection never
277 /// carries a stale backend that references executors which may have been destroyed. Safe to
278 /// call when async was never enabled.
279 ///
280 /// @warning Must not be called while async work is in flight on this connection: it destroys the
281 /// backend (and the strand/executors an outstanding offloaded step still references), which would
282 /// race the worker still touching the ODBC handle. Await every async operation on this connection
283 /// to completion before disabling (or before returning the owning pooled @c DataMapper).
284 LIGHTWEIGHT_API void DisableAsync() noexcept;
285
286 /// @return true if @ref EnableAsync has been called on this connection (and not yet disabled).
287 [[nodiscard]] LIGHTWEIGHT_API bool IsAsyncEnabled() const noexcept;
288
289 /// Retrieves the connection's asynchronous execution backend.
290 ///
291 /// Non-const because using the backend schedules and serializes real ODBC work (via its strand and
292 /// resume scheduler), which is an observable mutation of the connection's execution state.
293 ///
294 /// @pre @ref IsAsyncEnabled returns true.
295 /// @throws std::logic_error if async has not been enabled (programmer error, fail-fast).
296 /// @return The async backend used by this connection's async methods.
297 [[nodiscard]] LIGHTWEIGHT_API Async::IAsyncBackend& AsyncBackend();
298
299 private:
300 /// Ensures ODBC handles are allocated. Called by Connect() methods.
301 /// If handles were freed by Close(), this method reallocates them.
302 void EnsureHandlesAllocated();
303
304 void PostConnect();
305
306 // Private data members
307 // Note: move/move assignment operators implemented manually
308 // if adding new data members, make sure to update them accordingly.
309 SQLHENV m_hEnv {};
310 SQLHDBC m_hDbc {};
311 uint64_t m_connectionId;
312 SqlServerType m_serverType = SqlServerType::UNKNOWN;
313 SqlQueryFormatter const* m_queryFormatter {};
314 std::string m_driverName;
315
316 struct Data;
317 Data* m_data {};
318};
319
320inline SqlServerType SqlConnection::ServerType() const noexcept
321{
322 return m_serverType;
323}
324
325inline std::string const& SqlConnection::DriverName() const noexcept
326{
327 return m_driverName;
328}
329
331{
332 return *m_queryFormatter;
333}
334
335inline bool SqlConnection::SupportsNativeRowBatch() const noexcept
336{
337 // Native ODBC parameter-array binding (SQL_ATTR_PARAMSET_SIZE > 1) is a per-driver capability.
338 // Every backend Lightweight supports and tests against honours it; an unverified backend takes the
339 // always-correct per-row path rather than risk a driver that silently ignores the parameter array.
340 switch (ServerType())
341 {
342 case SqlServerType::MICROSOFT_SQL:
343 case SqlServerType::POSTGRESQL:
344 case SqlServerType::SQLITE:
345 return true;
346 case SqlServerType::MYSQL:
347 case SqlServerType::UNKNOWN:
348 return false;
349 }
350 return false;
351}
352
353inline bool SqlConnection::SupportsNativeRowArrayFetch(SqlServerType serverType) noexcept
354{
355 // Native ODBC row-array fetching (SQL_ATTR_ROW_ARRAY_SIZE > 1 + SQLFetchScroll) is a per-driver
356 // capability. Every backend Lightweight supports and tests against honours it; an unverified backend
357 // takes the always-correct per-row SQLFetch path rather than risk a driver that mis-handles the array.
358 switch (serverType)
359 {
360 case SqlServerType::MICROSOFT_SQL:
361 case SqlServerType::POSTGRESQL:
362 case SqlServerType::SQLITE:
363 return true;
364 case SqlServerType::MYSQL:
365 case SqlServerType::UNKNOWN:
366 return false;
367 }
368 return false;
369}
370
372{
374}
375
376inline bool SqlConnection::RoundTripsNarrowTextByteExact(SqlServerType serverType) noexcept
377{
378 switch (serverType)
379 {
380 case SqlServerType::MICROSOFT_SQL:
381 case SqlServerType::SQLITE:
382 return true;
383 case SqlServerType::POSTGRESQL: // psqlODBC transcodes SQL_C_CHAR through the client codepage
384 case SqlServerType::MYSQL:
385 case SqlServerType::UNKNOWN:
386 return false;
387 }
388 return false;
389}
390
391} // 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.
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:469
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