Lightweight 0.20261105.1
Loading...
Searching...
No Matches
Dispatch.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "../Api.hpp"
6#include "../SqlBindingTypes.hpp"
7#include "../SqlConnectInfo.hpp"
8#include "../SqlError.hpp"
9#include "../SqlServerType.hpp"
10#include "Concept.hpp"
11#include "DriverSet.hpp"
12
13#include <chrono>
14#include <concepts>
15#include <cstddef>
16#include <cstdint>
17#include <expected>
18#include <format>
19#include <functional>
20#include <optional>
21#include <source_location>
22#include <string>
23#include <string_view>
24#include <tuple>
25#include <type_traits>
26#include <utility>
27#include <vector>
28
29/// @file
30/// The driver-neutral face of the @ref Lightweight::Driver::SqlDriver concept: one function per concept
31/// operation, taking the tagged @c Lightweight::Driver::StatementHandle /
32/// @c Lightweight::Driver::ConnectionHandle instead of a driver's handle struct, plus the optional
33/// operations only some drivers provide and the typed visits for code that exists for one driver only.
34///
35/// Each function visits the handle and calls the driver's function unqualified inside the visit, so that
36/// it is found by argument-dependent lookup on the driver's handle struct. A dispatch function itself
37/// never matches a driver struct (the handles' constructors are explicit), so a driver lacking a required
38/// operation is a compile error rather than a recursion into the dispatch function. The layer exists
39/// because inside @c SqlStatement / @c SqlConnection members argument-dependent lookup is suppressed by
40/// the same-named members.
41
42namespace Lightweight::Driver
43{
44
45namespace detail
46{
47 /// @brief Whether @p H is one of the tagged handle types the dispatch functions take.
48 template <typename H>
49 concept TaggedHandle = std::same_as<H, StatementHandle> || std::same_as<H, ConnectionHandle>;
50
51 // Like the driver-list traits in DriverSet.hpp, the traits below are function templates deduced from their
52 // arguments rather than class templates with partial specializations, so that an application importing the
53 // Lightweight module can instantiate them (GCC 14 does not find such a partial specialization there).
54
55 /// @brief What invoking @p F returns with the first of no arguments it is invocable with: none.
56 /// @return @c std::type_identity<void>.
57 template <typename F>
58 constexpr auto FindFirstInvokeResult() noexcept -> std::type_identity<void>
59 {
60 return {};
61 }
62
63 /// @brief What invoking @p F returns with the first of @p Arg and @p Rest it is invocable with.
64 /// @param rest The remaining argument types, tried in order when @p F is not invocable with @p Arg.
65 /// @return @c std::type_identity of that result, or of @c void if @p F is invocable with none of them.
66 template <typename F, typename Arg, typename... Rest>
67 constexpr auto FindFirstInvokeResult(std::type_identity<Arg> /*argument*/, std::type_identity<Rest>... rest) noexcept
68 {
69 if constexpr (std::is_invocable_v<F, Arg>)
70 return std::type_identity<std::invoke_result_t<F, Arg>> {};
71 else
72 return FindFirstInvokeResult<F>(rest...);
73 }
74
75 /// @brief The result of @ref VisitRequired with @p F on a mutable handle of this type.
76 /// @return @c std::type_identity of what @p F returns with the first driver struct it takes; of @c void if none.
77 template <typename F, typename Role, typename... Drivers>
78 constexpr auto FindRequiredVisitResult(BasicHandle<Role, Drivers...>* /*handle*/) noexcept
79 {
80 return FindFirstInvokeResult<F>(std::type_identity<typename Role::template Type<Drivers>&> {}...);
81 }
82
83 /// @brief The result of @ref VisitRequired with @p F on a const handle of this type.
84 /// @return @c std::type_identity of what @p F returns with the first driver struct it takes; of @c void if none.
85 template <typename F, typename Role, typename... Drivers>
86 constexpr auto FindRequiredVisitResult(BasicHandle<Role, Drivers...> const* /*handle*/) noexcept
87 {
88 return FindFirstInvokeResult<F>(std::type_identity<typename Role::template Type<Drivers> const&> {}...);
89 }
90
91 /// @brief The result of @ref VisitRequired with @p F on a handle of type @p Handle (possibly const).
92 template <typename F, typename Handle>
93 using RequiredVisitResult = decltype(FindRequiredVisitResult<F>(static_cast<Handle*>(nullptr)))::type;
94
95 /// @brief Whether driver @p D connects from @p Info: its @c Connect(D::Connection&, Info const&,
96 /// std::source_location) is well-formed, found by argument-dependent lookup.
97 template <typename D, typename Info>
98 concept DriverConnectsFrom = requires(D::Connection& connection, Info const& info, std::source_location location) {
99 { Connect(connection, info, location) } -> std::same_as<std::expected<void, SqlErrorInfo>>;
100 };
101
102 /// @brief The first driver among an empty list that connects from @p Info: none.
103 /// @return @c std::type_identity<void>.
104 template <typename Info>
105 constexpr auto FindDriverConnectingFrom(DriverList<> /*drivers*/) noexcept -> std::type_identity<void>
106 {
107 return {};
108 }
109
110 /// @brief The first driver among @p D and @p Rest that connects from @p Info.
111 /// @return @c std::type_identity of that driver, or of @c void if there is none.
112 template <typename Info, typename D, typename... Rest>
113 constexpr auto FindDriverConnectingFrom(DriverList<D, Rest...> /*drivers*/) noexcept
114 {
115 if constexpr (DriverConnectsFrom<D, Info>)
116 return std::type_identity<D> {};
117 else
118 return FindDriverConnectingFrom<Info>(DriverList<Rest...> {});
119 }
120
121 /// @brief The enabled driver that connects from @p Info; @c void if there is none.
122 template <typename Info>
123 using EnabledDriverConnectingFrom = decltype(FindDriverConnectingFrom<Info>(EnabledDrivers {}))::type;
124
125 /// @brief Whether an enabled driver connects from @p Info (see @ref Lightweight::Driver::Connect).
126 template <typename Info>
127 inline constexpr bool CanConnectFrom = !std::is_void_v<EnabledDriverConnectingFrom<Info>>;
128
129 /// @brief The name of a connect-info type, as the error of a connect no enabled driver performs shows it.
130 /// @return The unqualified type name.
131 template <typename Info>
132 constexpr std::string_view ConnectInfoName() noexcept
133 {
134 static_assert(std::same_as<Info, SqliteConnectInfo>, "Info must be one of the SqlConnectInfo alternatives.");
135 return "SqliteConnectInfo";
136 }
137
138#if defined(LIGHTWEIGHT_DRIVER_ODBC)
139 /// @brief The name of the ODBC connection-string alternative.
140 /// @return "SqlConnectionString".
141 template <>
142 constexpr std::string_view ConnectInfoName<SqlConnectionString>() noexcept
143 {
144 return "SqlConnectionString";
145 }
146
147 /// @brief The name of the ODBC data-source alternative.
148 /// @return "SqlConnectionDataSource".
149 template <>
150 constexpr std::string_view ConnectInfoName<SqlConnectionDataSource>() noexcept
151 {
152 return "SqlConnectionDataSource";
153 }
154#endif
155
156 /// @brief The error of connecting from @p Info through a driver that does not connect from it.
157 /// @param driver The driver the handle is bound to.
158 /// @return SQLSTATE @c IM001 with the message "connecting from a <Info> is not supported by the <driver> driver".
159 template <typename Info>
160 [[nodiscard]] SqlErrorInfo ConnectNotSupported(SqlDriverId driver)
161 {
162 return NotSupportedByDriver(driver, std::format("connecting from a {}", ConnectInfoName<Info>()));
163 }
164
165 /// @brief Whether one of @p Drivers has the id @p id.
166 template <SqlDriver... Drivers>
167 constexpr bool IsEnabled(SqlDriverId id, DriverList<Drivers...> /*drivers*/) noexcept
168 {
169 return ((Drivers::Id == id) || ...);
170 }
171
172 /// @brief The name of the driver among @p Drivers whose id is @p id; "unknown" if there is none.
173 template <SqlDriver... Drivers>
174 constexpr std::string_view NameOf(SqlDriverId id, DriverList<Drivers...> /*drivers*/) noexcept
175 {
176 auto name = std::string_view { "unknown" };
177 (void) ((Drivers::Id == id ? (name = Drivers::Name, true) : false) || ...);
178 return name;
179 }
180
181 /// @brief A capability of the driver among @p Drivers whose id is @p id; @c false if there is none.
182 /// @param id The driver's id.
183 /// @param select Returns the capability of driver @c D when called with a @c std::type_identity<D>.
184 /// @return What @p select returns for that driver.
185 template <typename Select, SqlDriver... Drivers>
186 constexpr bool CapabilityOf(SqlDriverId id, Select const& select, DriverList<Drivers...> /*drivers*/) noexcept
187 {
188 auto supported = false;
189 (void) ((Drivers::Id == id ? (supported = select(std::type_identity<Drivers> {}), true) : false) || ...);
190 return supported;
191 }
192} // namespace detail
193
194/// @brief Whether the library was built with driver @p id (a fold over the enabled drivers).
195/// @param id The driver's id; every known driver has one, whether or not the build contains it.
196/// @return @c true when the driver is one of the enabled drivers.
197[[nodiscard]] constexpr bool IsEnabled(SqlDriverId id) noexcept
198{
199 return detail::IsEnabled(id, detail::EnabledDrivers {});
200}
201
202/// @brief Whether a connection opened from the default connect info refuses to connect while no default was set
203/// (see SqlConnection::SetDefaultConnectInfo): with SQLSTATE @c 08001 instead of opening what the unset default
204/// names. Holds in a build without the ODBC driver, whose unset default would be a @c SqliteConnectInfo naming a
205/// private temporary database. With ODBC the unset default is an empty connection string, which the driver manager
206/// refuses itself.
207inline constexpr bool RefusesUnsetDefaultConnectInfo = !IsEnabled(SqlDriverId::Odbc);
208
209/// @brief The display name of an enabled driver, as error messages show it.
210/// @param id The driver's id.
211/// @return The driver's @c Name (e.g. "ODBC"); "unknown" for an id no enabled driver has.
212[[nodiscard]] constexpr std::string_view NameOf(SqlDriverId id) noexcept
213{
214 return detail::NameOf(id, detail::EnabledDrivers {});
215}
216
217/// @brief Whether an enabled driver executes parameter arrays natively (one call per batch).
218/// @param id The driver's id.
219/// @return The driver's @c SupportsParameterArrays; @c false for an id no enabled driver has.
220[[nodiscard]] constexpr bool SupportsParameterArrays(SqlDriverId id) noexcept
221{
222 return detail::CapabilityOf(
223 id, []<typename D>(std::type_identity<D>) { return D::SupportsParameterArrays; }, detail::EnabledDrivers {});
224}
225
226/// @brief Whether an enabled driver fetches blocks of rows into array-bound columns natively.
227/// @param id The driver's id.
228/// @return The driver's @c SupportsRowArrayFetch; @c false for an id no enabled driver has.
229[[nodiscard]] constexpr bool SupportsRowArrayFetch(SqlDriverId id) noexcept
230{
231 return detail::CapabilityOf(
232 id, []<typename D>(std::type_identity<D>) { return D::SupportsRowArrayFetch; }, detail::EnabledDrivers {});
233}
234
235/// @brief Whether closing a connection of an enabled driver destroys its statement handles, so that they must
236/// not be freed afterwards.
237/// @param id The driver's id.
238/// @return The driver's @c StatementsDieWithConnection; @c false for an id no enabled driver has.
239[[nodiscard]] constexpr bool StatementsDieWithConnection(SqlDriverId id) noexcept
240{
241 return detail::CapabilityOf(
242 id, []<typename D>(std::type_identity<D>) { return D::StatementsDieWithConnection; }, detail::EnabledDrivers {});
243}
244
245/// @brief Whether an enabled driver may pass narrow text through a client codepage, so that whether narrow
246/// text round-trips byte-exact depends on the backend.
247/// @param id The driver's id.
248/// @return The driver's @c TranscodesNarrowText; @c false for an id no enabled driver has.
249[[nodiscard]] constexpr bool TranscodesNarrowText(SqlDriverId id) noexcept
250{
251 return detail::CapabilityOf(
252 id, []<typename D>(std::type_identity<D>) { return D::TranscodesNarrowText; }, detail::EnabledDrivers {});
253}
254
255/// @brief Whether an enabled driver reads a database's catalog with catalog functions of its own (ODBC's SQLTables,
256/// SQLColumns, ...); @c false when SqlSchema reads it with the SQL of SqlQueryFormatter::CatalogQueries().
257/// @param id The driver's id.
258/// @return The driver's @c HasCatalogFunctions; @c false for an id no enabled driver has.
259[[nodiscard]] constexpr bool HasCatalogFunctions(SqlDriverId id) noexcept
260{
261 return detail::CapabilityOf(
262 id, []<typename D>(std::type_identity<D>) { return D::HasCatalogFunctions; }, detail::EnabledDrivers {});
263}
264
265// {{{ The statement half of the SqlDriver concept
266
267/// @brief Allocates a statement of the connection's driver and binds @p statement to that driver.
268/// @param connection The connection to allocate on.
269/// @param statement Receives the statement; whatever it held before is forgotten, not freed. It is bound to
270/// the connection's driver whatever the result, and empty on failure.
271/// @return The driver's result of allocating.
272[[nodiscard]] inline SqlResult AllocateStatement(ConnectionHandle const& connection, StatementHandle& statement) noexcept
273{
274 return Visit(connection, [&statement](auto const& held) {
275 using D = detail::DriverOfConnection<std::remove_cvref_t<decltype(held)>, detail::EnabledDrivers>;
276 auto driverStatement = typename D::Statement {};
277 auto const result = Allocate(held, driverStatement);
278 statement = StatementHandle { driverStatement };
279 return result;
280 });
281}
282
283/// @brief Frees the statement's driver handle and leaves @p statement empty, bound to the same driver.
284/// @param statement The statement; may be empty.
285inline void Free(StatementHandle& statement) noexcept
286{
287 Visit(statement, [](auto& held) { Free(held); });
288}
289
290/// @brief Gives up the statement's driver handle after its connection session ended, and leaves @p statement
291/// empty, bound to the same driver: what died with the session is not touched again, what the driver still
292/// holds for the statement is released (see each driver's @c Abandon).
293/// @param statement The statement; may be empty.
294inline void Abandon(StatementHandle& statement) noexcept
295{
296 Visit(statement, [](auto& held) { Abandon(held); });
297}
298
299/// @brief A second handle naming the same driver statement as @p statement (its driver struct copied, see
300/// @c SqlDriverHandleStruct): it owns nothing more, so only one of the two may free it.
301/// @param statement The statement.
302/// @return The alias, bound to the same driver.
303[[nodiscard]] inline StatementHandle Alias(StatementHandle const& statement) noexcept
304{
305 return Visit(statement, [](auto const& held) { return StatementHandle { held }; });
306}
307
308/// @brief Tells whether two handles name the same driver statement.
309/// @param first One handle.
310/// @param second The other handle.
311/// @return @c true when both are bound to the same driver and hold equal structs.
312[[nodiscard]] inline bool SameStatement(StatementHandle const& first, StatementHandle const& second) noexcept
313{
314 return Visit(first, [&second](auto const& heldFirst) {
315 return Visit(second, [&heldFirst](auto const& heldSecond) {
316 if constexpr (std::same_as<decltype(heldFirst), decltype(heldSecond)>)
317 return heldFirst == heldSecond;
318 else
319 return false;
320 });
321 });
322}
323
324/// @brief Prepares @p query on the statement.
325/// @param statement The statement.
326/// @param query The SQL text, UTF-8.
327/// @param wideQueryText Whether the driver is to receive the text as UTF-16 (see
328/// SqlConnection::AcceptsWideQueryText()); a driver taking UTF-8 only ignores it.
329/// @return The driver's result.
330[[nodiscard]] inline SqlResult Prepare(StatementHandle& statement, std::string_view query, bool wideQueryText)
331{
332 return Visit(statement, [&](auto& held) { return Prepare(held, query, wideQueryText); });
333}
334
335/// @brief Executes the prepared statement with the currently bound parameters.
336/// @param statement The statement.
337/// @return The driver's result; @c SqlResult::NoData for a searched UPDATE/DELETE that matched no rows.
338[[nodiscard]] inline LIGHTWEIGHT_FORCE_INLINE SqlResult Execute(StatementHandle& statement) noexcept
339{
340 return Visit(statement, [](auto& held) { return Execute(held); });
341}
342
343/// @brief Executes @p query directly, without preparing it.
344/// @param statement The statement.
345/// @param query The SQL text, UTF-8.
346/// @param wideQueryText As for @c Prepare.
347/// @return The driver's result.
348[[nodiscard]] inline SqlResult ExecuteDirect(StatementHandle& statement, std::string_view query, bool wideQueryText)
349{
350 return Visit(statement, [&](auto& held) { return ExecuteDirect(held, query, wideQueryText); });
351}
352
353/// @brief Fetches the next row of the result set into the bound columns.
354/// @param statement The statement.
355/// @return The driver's result; @c SqlResult::NoData past the last row.
356[[nodiscard]] inline LIGHTWEIGHT_FORCE_INLINE SqlResult Fetch(StatementHandle& statement) noexcept
357{
358 return Visit(statement, [](auto& held) { return Fetch(held); });
359}
360
361/// @brief Advances to the next pending result set.
362/// @param statement The statement.
363/// @return The driver's result; @c SqlResult::NoData when none is pending.
364[[nodiscard]] inline SqlResult MoreResults(StatementHandle& statement) noexcept
365{
366 return Visit(statement, [](auto& held) { return MoreResults(held); });
367}
368
369/// @brief Closes the cursor of a result set that was read to its end. Pending result sets are left alone.
370/// @param statement The statement.
371inline void CloseCursor(StatementHandle& statement) noexcept
372{
373 Visit(statement, [](auto& held) { CloseCursor(held); });
374}
375
376/// @brief Drains every pending result set and closes the cursor, so the handle can run another query.
377/// An error met while draining is not reported.
378/// @param statement The statement.
379inline void DiscardResults(StatementHandle& statement) noexcept
380{
381 Visit(statement, [](auto& held) { DiscardResults(held); });
382}
383
384/// @brief Unbinds every bound result column.
385/// @param statement The statement.
386/// @return The driver's result.
387[[nodiscard]] inline SqlResult UnbindColumns(StatementHandle& statement) noexcept
388{
389 return Visit(statement, [](auto& held) { return UnbindColumns(held); });
390}
391
392/// @brief Drops every parameter binding.
393/// @param statement The statement.
394/// @return The driver's result.
395[[nodiscard]] inline SqlResult ResetParameters(StatementHandle& statement) noexcept
396{
397 return Visit(statement, [](auto& held) { return ResetParameters(held); });
398}
399
400/// @brief Restores single-row parameter binding after a parameter-array execution. Not reported.
401/// @param statement The statement.
402inline void ResetParameterArrays(StatementHandle& statement) noexcept
403{
404 Visit(statement, [](auto& held) { ResetParameterArrays(held); });
405}
406
407/// @brief Reports the number of columns of the current result set.
408/// @param statement The statement.
409/// @param count Receives the column count (0 for a statement that produced no result set).
410/// @return The driver's result.
411[[nodiscard]] inline SqlResult NumResultColumns(StatementHandle const& statement, std::int16_t& count) noexcept
412{
413 return Visit(statement, [&count](auto const& held) { return NumResultColumns(held, count); });
414}
415
416/// @brief Reports the number of parameters of the prepared statement.
417/// @param statement The statement.
418/// @param count Receives the parameter count.
419/// @return The driver's result.
420[[nodiscard]] inline SqlResult NumParameters(StatementHandle const& statement, std::int16_t& count) noexcept
421{
422 return Visit(statement, [&count](auto const& held) { return NumParameters(held, count); });
423}
424
425/// @brief Reports the number of rows the last executed statement affected.
426/// @param statement The statement.
427/// @param count Receives the row count; -1 when the driver cannot tell.
428/// @return The driver's result.
429[[nodiscard]] inline SqlResult NumRowsAffected(StatementHandle const& statement, std::ptrdiff_t& count) noexcept
430{
431 return Visit(statement, [&count](auto const& held) { return NumRowsAffected(held, count); });
432}
433
434/// @brief The SQL data type of a result column, as the driver describes it.
435/// @param statement The statement, with a result set.
436/// @param column The 1-based result column.
437/// @return The type, @c SqlResultColumnType::Unknown for a type without an enumerator, or the statement's
438/// diagnostics if the column cannot be described.
439[[nodiscard]] inline std::expected<SqlResultColumnType, SqlErrorInfo> ColumnType(StatementHandle const& statement,
440 SqlColumnIndex column)
441{
442 return Visit(statement, [column](auto const& held) { return ColumnType(held, column); });
443}
444
445/// @brief Sets the query timeout of the statement.
446/// @param statement The statement.
447/// @param timeout The timeout; zero means none.
448/// @return The driver's result.
449[[nodiscard]] inline SqlResult SetQueryTimeout(StatementHandle& statement, std::chrono::seconds timeout) noexcept
450{
451 return Visit(statement, [timeout](auto& held) { return SetQueryTimeout(held, timeout); });
452}
453
454/// @brief Reports the query timeout of the statement.
455/// @param statement The statement.
456/// @param timeout Receives the timeout; zero means none.
457/// @return The driver's result.
458[[nodiscard]] inline SqlResult QueryTimeout(StatementHandle const& statement, std::chrono::seconds& timeout) noexcept
459{
460 return Visit(statement, [&timeout](auto const& held) { return QueryTimeout(held, timeout); });
461}
462
463/// @brief Reads the statement's diagnostics: the first record its last failed call left.
464/// @param statement The statement.
465/// @return The diagnostics; value-initialized fields when the driver has none to report.
466[[nodiscard]] inline SqlErrorInfo Diagnose(StatementHandle const& statement)
467{
468 return Visit(statement, [](auto const& held) { return Diagnose(held); });
469}
470
471/// @brief Throws unless @p result indicates success: @c std::invalid_argument for a soft failure the
472/// caller is expected to recover from, @c SqlException otherwise (see @c ClassifyResult).
473/// @param statement The statement @p result came from.
474/// @param result The result to check.
475/// @param location Where the check originated; the driver's fault seam sees it. Defaults to the caller's
476/// location.
477inline void RequireSuccess(StatementHandle const& statement,
478 SqlResult result,
479 std::source_location location = std::source_location::current())
480{
481 Visit(statement, [&](auto const& held) { RequireSuccess(held, result, location); });
482}
483
484/// @brief The non-throwing, fault-seam-aware verdict on @p result.
485/// @param statement The statement @p result came from.
486/// @param result The result to check.
487/// @param location Where the check originated; the driver's fault seam sees it.
488/// @return The verdict, plus the injected diagnostic when one was injected.
489[[nodiscard]] inline CallOutcome CheckResult(StatementHandle const& statement,
490 SqlResult result,
491 std::source_location location)
492{
493 return Visit(statement, [&](auto const& held) { return CheckResult(held, result, location); });
494}
495
496/// @brief Opens the catalog result set @p request names on @p statement, in the layout the ODBC specification
497/// gives that catalog function.
498/// @param statement The statement to open the result set on.
499/// @param request The catalog function and its arguments.
500/// @return The driver's result; on a driver without catalog functions @c SqlResult::Error, with the statement's
501/// diagnostics @c NotSupportedByDriver(id, "catalog access") (SQLSTATE @c IM001).
502[[nodiscard]] inline SqlResult OpenCatalog(StatementHandle const& statement, SqlCatalogRequest const& request)
503{
504 return Visit(statement, [&request](auto const& held) { return OpenCatalog(held, request); });
505}
506
507/// @brief The type a @c DATA_TYPE code of the driver's catalog result sets (see @c OpenCatalog) stands for.
508/// @param statement The statement holding the catalog result set.
509/// @param dataType The code, as read from the result set.
510/// @return The type; @c SqlResultColumnType::Unknown for a code without an enumerator, and on a driver without
511/// catalog functions.
512[[nodiscard]] inline SqlResultColumnType CatalogColumnType(StatementHandle const& statement, int dataType) noexcept
513{
514 return Visit(statement, [dataType](auto const& held) { return CatalogColumnType(held, dataType); });
515}
516
517/// @brief Describes a result column: its type and its size.
518/// @param statement The statement, with a result set.
519/// @param column The 1-based result column.
520/// @param description Receives the description.
521/// @return The driver's result.
522[[nodiscard]] inline SqlResult DescribeColumn(StatementHandle const& statement,
523 SqlColumnIndex column,
524 SqlColumnDescription& description)
525{
526 return Visit(statement, [&](auto const& held) { return DescribeColumn(held, column, description); });
527}
528
529/// @brief The driver's own SQL type code of a result column, as the driver's leaf binders read it (see
530/// @c SqlDataBinderCallback::DescribeResultColumnType).
531/// @param statement The statement, with a result set.
532/// @param column The 1-based result column.
533/// @return The code; @c std::nullopt when the driver cannot describe the column, or has no such codes (its binders
534/// then never ask).
535[[nodiscard]] inline std::optional<std::int16_t> DescribeResultColumnSqlType(StatementHandle const& statement,
536 SqlColumnIndex column) noexcept
537{
538 return Visit(statement, [column](auto const& held) { return DescribeResultColumnSqlType(held, column); });
539}
540
541/// @brief The declared length of a result column as a bound buffer size (see
542/// @c SqlDataBinderCallback::MinimumOutputColumnSize).
543/// @param statement The statement holding the result set.
544/// @param column The 1-based result column.
545/// @param length Which declared length to ask for.
546/// @return The number of elements to bind, without a terminator; @c detail::DefaultBoundColumnSize where the
547/// driver reports no usable length.
548[[nodiscard]] inline std::size_t DeclaredBoundColumnSize(StatementHandle const& statement,
549 SqlColumnIndex column,
550 SqlOutputColumnLength length) noexcept
551{
552 return Visit(statement, [&](auto const& held) { return DeclaredBoundColumnSize(held, column, length); });
553}
554
555/// @brief Restores single-row fetching after a row-array fetch. Not reported; nothing for a driver without row
556/// arrays.
557/// @param statement The statement.
558inline void ResetRowArrayAttributes(StatementHandle& statement) noexcept
559{
560 Visit(statement, [](auto& held) { ResetRowArrayAttributes(held); });
561}
562
563// }}}
564
565// {{{ The connection half of the SqlDriver concept
566
567/// @brief Allocates the driver's connection resources, unless the connection already holds them.
568/// @param connection The connection, bound to its driver.
569inline void Allocate(ConnectionHandle& connection) noexcept
570{
571 Visit(connection, [](auto& held) { Allocate(held); });
572}
573
574/// @brief Closes the connection: disconnects it, frees its driver resources and leaves @p connection
575/// empty, bound to the same driver. Not reported.
576/// @param connection The connection; it must own its resources (not be borrowed).
577/// @param diagnostic Whether a refused disconnect's diagnostic is read (@c Skip for a connection never opened).
578/// @return Nothing, or the driver's diagnostic of a disconnect it refused, when read (the resources are freed
579/// regardless).
580[[nodiscard]] inline std::expected<void, SqlErrorInfo> Close(ConnectionHandle& connection,
581 CloseDiagnostic diagnostic) noexcept
582{
583 return Visit(connection, [diagnostic](auto& held) { return Close(held, diagnostic); });
584}
585
586/// @brief Ends the connection's session, keeping its driver resources for the next connect.
587/// @param connection The connection; one that holds no session has nothing to end.
588/// @return The driver's result; a failure means the driver refused, and the session goes on.
589[[nodiscard]] inline SqlResult Disconnect(ConnectionHandle& connection) noexcept
590{
591 return Visit(connection, [](auto& held) { return Disconnect(held); });
592}
593
594/// @brief Tells whether @p connection holds a driver connection (connected or not).
595/// @param connection The connection.
596/// @return Whether the driver connection is allocated.
597[[nodiscard]] inline bool IsAllocated(ConnectionHandle const& connection) noexcept
598{
599 return Visit(connection, [](auto const& held) { return IsAllocated(held); });
600}
601
602/// @brief Tells whether the driver still considers the connection alive.
603/// @param connection The connection.
604/// @return Whether the connection is alive.
605[[nodiscard]] inline bool IsAlive(ConnectionHandle const& connection) noexcept
606{
607 return Visit(connection, [](auto const& held) { return IsAlive(held); });
608}
609
610/// @brief Reads the connection's diagnostics: the first record its last failed call left.
611/// @param connection The connection.
612/// @return The diagnostics; value-initialized fields when the driver has none to report.
613[[nodiscard]] inline SqlErrorInfo LastError(ConnectionHandle const& connection)
614{
615 return Visit(connection, [](auto const& held) { return LastError(held); });
616}
617
618/// @brief The non-throwing, fault-seam-aware verdict on @p result of a call on @p connection.
619/// @param connection The connection @p result came from.
620/// @param result The result to check.
621/// @param location Where the check originated; the driver's fault seam sees it.
622/// @return The verdict, plus the injected diagnostic when one was injected.
623[[nodiscard]] inline CallOutcome CheckResult(ConnectionHandle const& connection,
624 SqlResult result,
625 std::source_location location)
626{
627 return Visit(connection, [&](auto const& held) { return CheckResult(held, result, location); });
628}
629
630/// @brief The name of the DBMS the connection is connected to.
631/// @param connection The connected connection.
632/// @return The name in UTF-8, or an empty string if the driver cannot tell.
633[[nodiscard]] inline std::string ServerName(ConnectionHandle const& connection)
634{
635 return Visit(connection, [](auto const& held) { return ServerName(held); });
636}
637
638/// @brief The version of the DBMS the connection is connected to.
639/// @param connection The connected connection.
640/// @return The version in UTF-8, or an empty string if the driver cannot tell.
641[[nodiscard]] inline std::string ServerVersion(ConnectionHandle const& connection)
642{
643 return Visit(connection, [](auto const& held) { return ServerVersion(held); });
644}
645
646/// @brief The name of the driver (library) the connection runs on.
647/// @param connection The connected connection.
648/// @return The name in UTF-8, or an empty string if the driver cannot tell.
649[[nodiscard]] inline std::string DriverName(ConnectionHandle const& connection)
650{
651 return Visit(connection, [](auto const& held) { return DriverName(held); });
652}
653
654/// @brief The name of the database in use.
655/// @param connection The connected connection.
656/// @return The name in UTF-8, or an empty string if the driver cannot tell.
657[[nodiscard]] inline std::string DatabaseName(ConnectionHandle const& connection)
658{
659 return Visit(connection, [](auto const& held) { return DatabaseName(held); });
660}
661
662/// @brief The name of the user the connection is logged in as.
663/// @param connection The connected connection.
664/// @return The name in UTF-8, or an empty string if the driver cannot tell.
665[[nodiscard]] inline std::string UserName(ConnectionHandle const& connection)
666{
667 return Visit(connection, [](auto const& held) { return UserName(held); });
668}
669
670/// @brief Tells whether a transaction is open, i.e. autocommit is off.
671/// @param connection The connection.
672/// @return Whether a transaction is open.
673[[nodiscard]] inline bool TransactionActive(ConnectionHandle const& connection) noexcept
674{
675 return Visit(connection, [](auto const& held) { return TransactionActive(held); });
676}
677
678/// @brief Tells whether the data source supports transactions.
679/// @param connection The connected connection.
680/// @return Whether transactions are supported.
681[[nodiscard]] inline bool TransactionsAllowed(ConnectionHandle const& connection) noexcept
682{
683 return Visit(connection, [](auto const& held) { return TransactionsAllowed(held); });
684}
685
686/// @brief Switches autocommit mode on or off.
687/// @param connection The connection.
688/// @param enabled Whether every statement commits on its own.
689/// @return The driver's result.
690[[nodiscard]] inline SqlResult SetAutoCommit(ConnectionHandle& connection, bool enabled) noexcept
691{
692 return Visit(connection, [enabled](auto& held) { return SetAutoCommit(held, enabled); });
693}
694
695/// @brief Commits the open transaction.
696/// @param connection The connection.
697/// @return The driver's result.
698[[nodiscard]] inline SqlResult Commit(ConnectionHandle& connection) noexcept
699{
700 return Visit(connection, [](auto& held) { return Commit(held); });
701}
702
703/// @brief Rolls back the open transaction.
704/// @param connection The connection.
705/// @return The driver's result.
706[[nodiscard]] inline SqlResult Rollback(ConnectionHandle& connection) noexcept
707{
708 return Visit(connection, [](auto& held) { return Rollback(held); });
709}
710
711/// @brief Sets the transaction isolation level of the connection.
712/// @param connection The connection.
713/// @param mode The isolation level; not @c SqlIsolationMode::DriverDefault.
714/// @return The driver's result.
715[[nodiscard]] inline SqlResult SetIsolation(ConnectionHandle& connection, SqlIsolationMode mode) noexcept
716{
717 return Visit(connection, [mode](auto& held) { return SetIsolation(held, mode); });
718}
719
720/// @brief Rolls back the open transaction and switches autocommit back on.
721/// @param connection The connection, with a transaction open.
722/// @return Whether both steps succeeded.
723[[nodiscard]] inline bool RollbackOpenTransaction(ConnectionHandle& connection) noexcept
724{
725 return Visit(connection, [](auto& held) { return RollbackOpenTransaction(held); });
726}
727
728/// @brief The string the driver escapes the wildcards of a catalog search pattern with (see @c OpenCatalog).
729/// @param connection The connected connection.
730/// @return The escape, empty when the driver reports none; on a driver without catalog functions
731/// @c NotSupportedByDriver(id, "catalog access") (SQLSTATE @c IM001).
732[[nodiscard]] inline std::expected<std::string, SqlErrorInfo> SearchPatternEscape(ConnectionHandle const& connection)
733{
734 return Visit(connection, [](auto const& held) { return SearchPatternEscape(held); });
735}
736
737// }}}
738
739// {{{ Connecting: what a connection is opened from differs per driver
740
741/// @brief Tells whether connecting from @p Info binds @p handle to another driver than the one it is bound to.
742/// @param handle The connection's handle.
743/// @return @c true when an enabled driver connects from @p Info and it is not the handle's; @c false otherwise,
744/// also when no enabled driver connects from @p Info.
745template <typename Info>
746[[nodiscard]] bool RebindsDriverFor(ConnectionHandle const& handle) noexcept
747{
748 using D = detail::EnabledDriverConnectingFrom<Info>;
749 if constexpr (std::is_void_v<D>)
750 return false;
751 else
752 return handle.DriverId() != D::Id;
753}
754
755/// @brief Binds @p handle to the enabled driver D for which `Connect(D::Connection&, Info const&,
756/// std::source_location)` is well-formed (found by argument-dependent lookup), closing what the handle held
757/// if that was another driver's. No such driver: no change.
758/// @param handle The connection's handle; it must own what it holds (not be borrowed).
759template <typename Info>
760void BindDriverFor(ConnectionHandle& handle) noexcept
761{
762 using D = detail::EnabledDriverConnectingFrom<Info>;
763 if constexpr (!std::is_void_v<D>)
764 {
765 if (handle.DriverId() == D::Id)
766 return;
767 if (!handle.IsEmpty())
768 std::ignore = Close(handle, CloseDiagnostic::Skip); // SqlConnection closes a connected handle first
769 handle = ConnectionHandle { typename D::Connection {} };
770 }
771}
772
773/// @brief Connects @p handle from @p info through the enabled driver that connects from it.
774/// @param handle The connection's handle, bound by @c BindDriverFor and allocated by @c Allocate.
775/// @param info What to connect from.
776/// @param location Where the connect originated; the driver's fault seam sees it as the location of its
777/// connection checks.
778/// @return Nothing on success, else the driver's diagnostics of the failed step. When no enabled driver
779/// connects from @p Info, or @p handle is bound to another driver:
780/// std::unexpected(NotSupportedByDriver(handle.DriverId(), "connecting from a <Info>")).
781template <typename Info>
782[[nodiscard]] std::expected<void, SqlErrorInfo> Connect(ConnectionHandle& handle,
783 Info const& info,
784 std::source_location location)
785{
786 using D = detail::EnabledDriverConnectingFrom<Info>;
787 if constexpr (std::is_void_v<D>)
788 return std::unexpected(detail::ConnectNotSupported<Info>(handle.DriverId()));
789 else
790 return Visit(handle, [&](auto& held) -> std::expected<void, SqlErrorInfo> {
791 if constexpr (std::same_as<std::remove_cvref_t<decltype(held)>, typename D::Connection>)
792 return Connect(held, info, location);
793 else
794 return std::unexpected(detail::ConnectNotSupported<Info>(handle.DriverId()));
795 });
796}
797
798// }}}
799
800// {{{ Optional operations: the driver's function when it has one, else the stated fallback
801
802/// @brief Tells whether the connection's driver receives wide (UTF-16) query text unchanged.
803/// @param connection The connected connection.
804/// @return The driver's answer; @c true for a driver without the operation.
805[[nodiscard]] inline bool AcceptsWideQueryText(ConnectionHandle const& connection) noexcept
806{
807 return Visit(connection, [](auto const& held) {
808 if constexpr (requires { AcceptsWideQueryText(held); })
809 return AcceptsWideQueryText(held);
810 else
811 return true;
812 });
813}
814
815/// @brief Tells whether the connection's driver can read a bound output column again, so that a bound value
816/// longer than its buffer is read in full (see @c SqlConnection::ReadsBoundColumnsAgain).
817/// @param connection The connected connection.
818/// @return The driver's answer; @c true for a driver without the operation, which fills a bound column afresh for
819/// every row and so never cuts a value off.
820[[nodiscard]] inline bool ReadsBoundColumnsAgain(ConnectionHandle const& connection) noexcept
821{
822 return Visit(connection, [](auto const& held) {
823 if constexpr (requires { ReadsBoundColumnsAgain(held); })
824 return ReadsBoundColumnsAgain(held);
825 else
826 return true;
827 });
828}
829
830/// @brief Tells how the narrow string binders exchange text with the connection's driver (see
831/// @c SqlConnection::NarrowTextBindingMode).
832/// @param connection The connected connection.
833/// @param serverType The connected backend.
834/// @param connectInfo What the connection was connected from.
835/// @return The driver's answer; @c detail::NarrowTextBindingOf(serverType) for a driver without the operation.
836[[nodiscard]] inline ::Lightweight::detail::NarrowTextBinding NarrowTextBinding(ConnectionHandle const& connection,
837 SqlServerType serverType,
838 SqlConnectInfo const& connectInfo)
839{
840 return Visit(connection, [&](auto const& held) {
841 if constexpr (requires { NarrowTextBinding(held, serverType, connectInfo); })
842 return NarrowTextBinding(held, serverType, connectInfo);
843 else
844 return ::Lightweight::detail::NarrowTextBindingOf(serverType);
845 });
846}
847
848#if defined(LIGHTWEIGHT_DRIVER_ODBC)
849/// @brief Names the driver a connect from @p connectionString reaches, without connecting (see
850/// @c ServerTypeFromDriverName): the connection string's own @c DRIVER, else what the driver manager lists for its
851/// @c DSN.
852/// @param connection The connection, allocated.
853/// @param connectionString The connection string to connect from.
854/// @return The driver's answer; empty for a driver without the operation, or when nothing names the driver.
855[[nodiscard]] inline std::string DriverNameOf(ConnectionHandle const& connection,
856 SqlConnectionString const& connectionString)
857{
858 return Visit(connection, [&](auto const& held) -> std::string {
859 if constexpr (requires { DriverNameOf(held, connectionString); })
860 return DriverNameOf(held, connectionString);
861 else
862 return {};
863 });
864}
865#endif
866
867/// @brief Records whether the connection's driver takes text through its wide entry points, where the
868/// driver has such a choice; does nothing otherwise.
869/// @param connection The connected connection.
870/// @param acceptsWideQueryText What @c AcceptsWideQueryText found.
871inline void RegisterTextEntry(ConnectionHandle const& connection, bool acceptsWideQueryText) noexcept
872{
873 Visit(connection, [acceptsWideQueryText](auto const& held) {
874 if constexpr (requires { RegisterTextEntry(held, acceptsWideQueryText); })
875 RegisterTextEntry(held, acceptsWideQueryText);
876 });
877}
878
879/// @brief Records @p statement as belonging to a connection whose driver takes text only through its
880/// ANSI entry points, where the driver has such a choice; does nothing otherwise.
881/// @param statement The freshly allocated statement.
882/// @param connection The connection it was allocated on.
883inline void RegisterAnsiOnly(StatementHandle const& statement, ConnectionHandle const& connection) noexcept
884{
885 Visit(statement, [&connection](auto const& heldStatement) {
886 Visit(connection, [&heldStatement](auto const& heldConnection) {
887 if constexpr (requires { RegisterAnsiOnly(heldStatement, heldConnection); })
888 RegisterAnsiOnly(heldStatement, heldConnection);
889 });
890 });
891}
892
893/// @brief Describes the declared SQL type of each of the first @p count parameters.
894/// @param statement The prepared statement.
895/// @param count The number of parameters to describe.
896/// @return The driver's type codes; empty for a driver without the operation.
897[[nodiscard]] inline std::vector<std::int16_t> DescribeParameterTypes(StatementHandle const& statement, std::size_t count)
898{
899 return Visit(statement, [count](auto const& held) {
900 if constexpr (requires { DescribeParameterTypes(held, count); })
901 return DescribeParameterTypes(held, count);
902 else
903 return std::vector<std::int16_t> {};
904 });
905}
906
907/// @brief The number of parameters currently bound on the statement.
908/// @param statement The statement.
909/// @return The driver's count; -1 for a driver without the operation, or when it cannot tell.
910[[nodiscard]] inline std::int16_t BoundParameterCount(StatementHandle const& statement) noexcept
911{
912 return Visit(statement, [](auto const& held) {
913 if constexpr (requires { BoundParameterCount(held); })
914 return BoundParameterCount(held);
915 else
916 return std::int16_t { -1 };
917 });
918}
919
920// }}}
921
922// {{{ Typed visits for code that exists for one driver only
923
924/// @brief Calls @p f with the held struct when @p f is invocable with it; otherwise does nothing.
925/// @param handle The statement or connection handle.
926/// @param f The callable, e.g. taking one driver's struct only.
927template <typename Handle, typename F>
928 requires detail::TaggedHandle<std::remove_cvref_t<Handle>>
929void VisitIfSupported(Handle&& handle, F&& f)
930{
931 Visit(std::forward<Handle>(handle), [&f](auto& held) {
932 if constexpr (std::is_invocable_v<F, decltype(held)>)
933 std::invoke(std::forward<F>(f), held);
934 });
935}
936
937/// @brief Calls @p f with the held struct when invocable; otherwise throws
938/// SqlException(NotSupportedByDriver(id, operation)).
939/// @param handle The statement or connection handle.
940/// @param operation The name of the operation @p f performs, for the error message.
941/// @param f The callable, e.g. taking one driver's struct only.
942/// @return What @p f returns.
943template <typename Handle, typename F>
944 requires detail::TaggedHandle<std::remove_cvref_t<Handle>>
945decltype(auto) VisitRequired(Handle&& handle, std::string_view operation, F&& f)
946{
947 // An rvalue handle is visited through the const overload, as Visit binds it.
948 using Visited = std::conditional_t<std::is_lvalue_reference_v<Handle>,
949 std::remove_reference_t<Handle>,
950 std::remove_reference_t<Handle> const>;
951 using Result = detail::RequiredVisitResult<F, Visited>;
952 auto const driverId = handle.DriverId();
953 return Visit(std::forward<Handle>(handle), [&](auto& held) -> Result {
954 if constexpr (std::is_invocable_v<F, decltype(held)>)
955 return std::invoke(std::forward<F>(f), held);
956 else
957 throw SqlException(NotSupportedByDriver(driverId, operation));
958 });
959}
960
961// }}}
962
963} // namespace Lightweight::Driver
bool IsAllocated(ConnectionHandle const &connection) noexcept
Tells whether connection holds a driver connection (connected or not).
Definition Dispatch.hpp:597
std::string ServerVersion(ConnectionHandle const &connection)
The version of the DBMS the connection is connected to.
Definition Dispatch.hpp:641
void ResetParameterArrays(StatementHandle &statement) noexcept
Restores single-row parameter binding after a parameter-array execution. Not reported.
Definition Dispatch.hpp:402
void CloseCursor(StatementHandle &statement) noexcept
Closes the cursor of a result set that was read to its end. Pending result sets are left alone.
Definition Dispatch.hpp:371
std::expected< void, SqlErrorInfo > Connect(ConnectionHandle &handle, Info const &info, std::source_location location)
Connects handle from info through the enabled driver that connects from it.
Definition Dispatch.hpp:782
SqlResult UnbindColumns(StatementHandle &statement) noexcept
Unbinds every bound result column.
Definition Dispatch.hpp:387
bool TransactionActive(ConnectionHandle const &connection) noexcept
Tells whether a transaction is open, i.e. autocommit is off.
Definition Dispatch.hpp:673
constexpr bool StatementsDieWithConnection(SqlDriverId id) noexcept
Whether closing a connection of an enabled driver destroys its statement handles, so that they must n...
Definition Dispatch.hpp:239
std::optional< std::int16_t > DescribeResultColumnSqlType(StatementHandle const &statement, SqlColumnIndex column) noexcept
The driver's own SQL type code of a result column, as the driver's leaf binders read it (see SqlDataB...
Definition Dispatch.hpp:535
decltype(auto) VisitRequired(Handle &&handle, std::string_view operation, F &&f)
Calls f with the held struct when invocable; otherwise throws SqlException(NotSupportedByDriver(id,...
Definition Dispatch.hpp:945
void Allocate(ConnectionHandle &connection) noexcept
Allocates the driver's connection resources, unless the connection already holds them.
Definition Dispatch.hpp:569
void RequireSuccess(StatementHandle const &statement, SqlResult result, std::source_location location=std::source_location::current())
Throws unless result indicates success: std::invalid_argument for a soft failure the caller is expect...
Definition Dispatch.hpp:477
SqlResult SetAutoCommit(ConnectionHandle &connection, bool enabled) noexcept
Switches autocommit mode on or off.
Definition Dispatch.hpp:690
bool SameStatement(StatementHandle const &first, StatementHandle const &second) noexcept
Tells whether two handles name the same driver statement.
Definition Dispatch.hpp:312
constexpr bool SupportsParameterArrays(SqlDriverId id) noexcept
Whether an enabled driver executes parameter arrays natively (one call per batch).
Definition Dispatch.hpp:220
SqlResult NumResultColumns(StatementHandle const &statement, std::int16_t &count) noexcept
Reports the number of columns of the current result set.
Definition Dispatch.hpp:411
bool ReadsBoundColumnsAgain(ConnectionHandle const &connection) noexcept
Tells whether the connection's driver can read a bound output column again, so that a bound value lon...
Definition Dispatch.hpp:820
std::string ServerName(ConnectionHandle const &connection)
The name of the DBMS the connection is connected to.
Definition Dispatch.hpp:633
SqlResult SetQueryTimeout(StatementHandle &statement, std::chrono::seconds timeout) noexcept
Sets the query timeout of the statement.
Definition Dispatch.hpp:449
constexpr bool SupportsRowArrayFetch(SqlDriverId id) noexcept
Whether an enabled driver fetches blocks of rows into array-bound columns natively.
Definition Dispatch.hpp:229
constexpr bool HasCatalogFunctions(SqlDriverId id) noexcept
Whether an enabled driver reads a database's catalog with catalog functions of its own (ODBC's SQLTab...
Definition Dispatch.hpp:259
void DiscardResults(StatementHandle &statement) noexcept
Drains every pending result set and closes the cursor, so the handle can run another query....
Definition Dispatch.hpp:379
void Free(StatementHandle &statement) noexcept
Frees the statement's driver handle and leaves statement empty, bound to the same driver.
Definition Dispatch.hpp:285
std::string DriverNameOf(ConnectionHandle const &connection, SqlConnectionString const &connectionString)
Names the driver a connect from connectionString reaches, without connecting (see ServerTypeFromDrive...
Definition Dispatch.hpp:855
SqlResult OpenCatalog(StatementHandle const &statement, SqlCatalogRequest const &request)
Opens the catalog result set request names on statement, in the layout the ODBC specification gives t...
Definition Dispatch.hpp:502
void RegisterAnsiOnly(StatementHandle const &statement, ConnectionHandle const &connection) noexcept
Records statement as belonging to a connection whose driver takes text only through its ANSI entry po...
Definition Dispatch.hpp:883
SqlResult NumParameters(StatementHandle const &statement, std::int16_t &count) noexcept
Reports the number of parameters of the prepared statement.
Definition Dispatch.hpp:420
SqlResultColumnType CatalogColumnType(StatementHandle const &statement, int dataType) noexcept
The type a DATA_TYPE code of the driver's catalog result sets (see OpenCatalog) stands for.
Definition Dispatch.hpp:512
SqlResult SetIsolation(ConnectionHandle &connection, SqlIsolationMode mode) noexcept
Sets the transaction isolation level of the connection.
Definition Dispatch.hpp:715
constexpr bool RefusesUnsetDefaultConnectInfo
Whether a connection opened from the default connect info refuses to connect while no default was set...
Definition Dispatch.hpp:207
SqlResult QueryTimeout(StatementHandle const &statement, std::chrono::seconds &timeout) noexcept
Reports the query timeout of the statement.
Definition Dispatch.hpp:458
void ResetRowArrayAttributes(StatementHandle &statement) noexcept
Restores single-row fetching after a row-array fetch. Not reported; nothing for a driver without row ...
Definition Dispatch.hpp:558
SqlResult NumRowsAffected(StatementHandle const &statement, std::ptrdiff_t &count) noexcept
Reports the number of rows the last executed statement affected.
Definition Dispatch.hpp:429
bool RebindsDriverFor(ConnectionHandle const &handle) noexcept
Tells whether connecting from Info binds handle to another driver than the one it is bound to.
Definition Dispatch.hpp:746
void Abandon(StatementHandle &statement) noexcept
Gives up the statement's driver handle after its connection session ended, and leaves statement empty...
Definition Dispatch.hpp:294
bool RollbackOpenTransaction(ConnectionHandle &connection) noexcept
Rolls back the open transaction and switches autocommit back on.
Definition Dispatch.hpp:723
SqlErrorInfo LastError(ConnectionHandle const &connection)
Reads the connection's diagnostics: the first record its last failed call left.
Definition Dispatch.hpp:613
std::size_t DeclaredBoundColumnSize(StatementHandle const &statement, SqlColumnIndex column, SqlOutputColumnLength length) noexcept
The declared length of a result column as a bound buffer size (see SqlDataBinderCallback::MinimumOutp...
Definition Dispatch.hpp:548
SqlResult Disconnect(ConnectionHandle &connection) noexcept
Ends the connection's session, keeping its driver resources for the next connect.
Definition Dispatch.hpp:589
StatementHandle Alias(StatementHandle const &statement) noexcept
A second handle naming the same driver statement as statement (its driver struct copied,...
Definition Dispatch.hpp:303
SqlResult Commit(ConnectionHandle &connection) noexcept
Commits the open transaction.
Definition Dispatch.hpp:698
void VisitIfSupported(Handle &&handle, F &&f)
Calls f with the held struct when f is invocable with it; otherwise does nothing.
Definition Dispatch.hpp:929
SqlErrorInfo Diagnose(StatementHandle const &statement)
Reads the statement's diagnostics: the first record its last failed call left.
Definition Dispatch.hpp:466
std::int16_t BoundParameterCount(StatementHandle const &statement) noexcept
The number of parameters currently bound on the statement.
Definition Dispatch.hpp:910
std::string DriverName(ConnectionHandle const &connection)
The name of the driver (library) the connection runs on.
Definition Dispatch.hpp:649
void RegisterTextEntry(ConnectionHandle const &connection, bool acceptsWideQueryText) noexcept
Records whether the connection's driver takes text through its wide entry points, where the driver ha...
Definition Dispatch.hpp:871
SqlResult DescribeColumn(StatementHandle const &statement, SqlColumnIndex column, SqlColumnDescription &description)
Describes a result column: its type and its size.
Definition Dispatch.hpp:522
SqlResult ResetParameters(StatementHandle &statement) noexcept
Drops every parameter binding.
Definition Dispatch.hpp:395
std::expected< SqlResultColumnType, SqlErrorInfo > ColumnType(StatementHandle const &statement, SqlColumnIndex column)
The SQL data type of a result column, as the driver describes it.
Definition Dispatch.hpp:439
void BindDriverFor(ConnectionHandle &handle) noexcept
Binds handle to the enabled driver D for which Connect(D::Connection&, Info const&,...
Definition Dispatch.hpp:760
std::string UserName(ConnectionHandle const &connection)
The name of the user the connection is logged in as.
Definition Dispatch.hpp:665
std::vector< std::int16_t > DescribeParameterTypes(StatementHandle const &statement, std::size_t count)
Describes the declared SQL type of each of the first count parameters.
Definition Dispatch.hpp:897
std::expected< void, SqlErrorInfo > Close(ConnectionHandle &connection, CloseDiagnostic diagnostic) noexcept
Closes the connection: disconnects it, frees its driver resources and leaves connection empty,...
Definition Dispatch.hpp:580
constexpr bool TranscodesNarrowText(SqlDriverId id) noexcept
Whether an enabled driver may pass narrow text through a client codepage, so that whether narrow text...
Definition Dispatch.hpp:249
SqlResult Rollback(ConnectionHandle &connection) noexcept
Rolls back the open transaction.
Definition Dispatch.hpp:706
bool AcceptsWideQueryText(ConnectionHandle const &connection) noexcept
Tells whether the connection's driver receives wide (UTF-16) query text unchanged.
Definition Dispatch.hpp:805
SqlResult MoreResults(StatementHandle &statement) noexcept
Advances to the next pending result set.
Definition Dispatch.hpp:364
bool TransactionsAllowed(ConnectionHandle const &connection) noexcept
Tells whether the data source supports transactions.
Definition Dispatch.hpp:681
std::string DatabaseName(ConnectionHandle const &connection)
The name of the database in use.
Definition Dispatch.hpp:657
CallOutcome CheckResult(StatementHandle const &statement, SqlResult result, std::source_location location)
The non-throwing, fault-seam-aware verdict on result.
Definition Dispatch.hpp:489
bool IsAlive(ConnectionHandle const &connection) noexcept
Tells whether the driver still considers the connection alive.
Definition Dispatch.hpp:605
SqlResult AllocateStatement(ConnectionHandle const &connection, StatementHandle &statement) noexcept
Allocates a statement of the connection's driver and binds statement to that driver.
Definition Dispatch.hpp:272
std::variant< SqlConnectionDataSource, SqlConnectionString, SqliteConnectInfo > SqlConnectInfo
What a connection is opened from: an ODBC data source, an ODBC connection string, or a SQLite databas...
@ Execute
The execution of a prepared statement (SqlStatement::Execute).
@ ExecuteDirect
The execution of a one-shot statement (SqlStatement::ExecuteDirect).
@ Prepare
The preparation of a statement (SqlStatement::Prepare).
Verdict of a non-throwing checked driver call, carrying the diagnostic that justifies a failure verdi...
Definition Concept.hpp:76
What a catalog call asks for: the ODBC catalog function and its arguments, in driver-neutral terms.
Definition Concept.hpp:136
What a driver reports about a result column (see DescribeColumn).
Definition Concept.hpp:172
Represents a SQL error as reported by the database driver.
Definition SqlError.hpp:29