Lightweight 0.20261105.1
Loading...
Searching...
No Matches
Handles.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5// <sql.h> below needs a few symbols resolved first — see SqlOdbcPrelude.hpp's header comment.
6#include "../../Api.hpp"
7#include "../../SqlBindingTypes.hpp"
8#include "../../SqlError.hpp"
9#include "../Concept.hpp"
10#include "SqlOdbcPrelude.hpp"
11
12#include <concepts>
13#include <cstddef>
14#include <cstdint>
15#include <string_view>
16#include <utility>
17
18#include <sql.h>
19#include <sqlext.h>
20#include <sqlspi.h>
21#include <sqltypes.h>
22
23namespace Lightweight
24{
25class SqlConnection;
26class SqlStatement;
27} // namespace Lightweight
28
29namespace Lightweight::Driver::Odbc
30{
31
32/// @brief The native ODBC statement handle type (@c SQLHSTMT), for ODBC-only API outside this directory.
33using NativeStatementHandle = SQLHSTMT;
34
35/// @brief The native ODBC connection handle type (@c SQLHDBC), for ODBC-only API outside this directory.
36using NativeConnectionHandle = SQLHDBC;
37
38/// @brief The native ODBC return code type (@c SQLRETURN), for ODBC-only API outside this directory.
39using NativeResult = SQLRETURN;
40
41/// @brief The ODBC driver's statement handle struct.
43{
44 /// The native ODBC statement handle.
45 SQLHSTMT handle {};
46
47 /// Compares the native handles.
48 bool operator==(Statement const&) const = default;
49};
50
51/// @brief The ODBC driver's connection handle struct.
53{
54 /// The native ODBC environment handle; null for a borrowed connection.
55 SQLHENV env {};
56
57 /// The native ODBC connection handle.
58 SQLHDBC dbc {};
59
60 /// Compares the native handles.
61 bool operator==(Connection const&) const = default;
62};
63
64/// @brief The ODBC driver: names its handle structs and its driver id.
65struct Driver
66{
67 /// The statement handle struct.
69
70 /// The connection handle struct.
72
73 /// The id the tagged handles carry for this driver.
74 static constexpr SqlDriverId Id = SqlDriverId::Odbc;
75
76 /// The driver's name, as error messages show it.
77 static constexpr std::string_view Name = "ODBC";
78
79 /// Parameter arrays (@c SQL_ATTR_PARAMSET_SIZE > 1) execute a whole batch in one @c SQLExecute.
80 static constexpr bool SupportsParameterArrays = true;
81
82 /// Row arrays (@c SQL_ATTR_ROW_ARRAY_SIZE > 1 with @c SQLFetchScroll) fetch a block of rows per call.
83 static constexpr bool SupportsRowArrayFetch = true;
84
85 /// Whether closing a connection destroys its statement handles, so that they must not be freed afterwards.
86 /// True for ODBC: @c SQLFreeHandle on the connection frees the statements allocated on it.
87 static constexpr bool StatementsDieWithConnection = true;
88
89 /// Narrow (@c SQL_C_CHAR) text may be transcoded through the client codepage (psqlODBC does), so whether it
90 /// round-trips byte-exact depends on the backend.
91 static constexpr bool TranscodesNarrowText = true;
92
93 /// The catalog is read with the ODBC catalog functions (@c SQLTables, @c SQLColumns, @c SQLStatistics, ...).
94 static constexpr bool HasCatalogFunctions = true;
95};
96
97// static_assert(SqlDriver<Driver>) is in Connection.hpp, after the operations the concept requires.
98
99/// @brief The native ODBC statement handle of @p stmt.
100/// @param stmt Any statement, including a moved-from one.
101/// @return The native handle, or @c SQL_NULL_HSTMT when the statement's handle is empty or belongs to
102/// another driver.
103[[nodiscard]] inline LIGHTWEIGHT_FORCE_INLINE NativeStatementHandle NativeHandle(SqlStatement const& stmt) noexcept;
104
105/// @brief The native ODBC connection handle of @p connection.
106/// @param connection Any connection, including a closed or moved-from one.
107/// @return The native handle, or @c SQL_NULL_HDBC when the connection's handle is empty or belongs to
108/// another driver.
109[[nodiscard]] inline LIGHTWEIGHT_FORCE_INLINE NativeConnectionHandle NativeHandle(SqlConnection const& connection) noexcept;
110
111// The neutral vocabulary must be bit-identical to ODBC's, so the ODBC driver converts for free.
112static_assert(std::same_as<SqlColumnIndex, SQLUSMALLINT>, "SqlColumnIndex must be SQLUSMALLINT");
113static_assert(std::same_as<SqlIndicator, SQLLEN>,
114 "SqlIndicator must be the same type as SQLLEN on this platform; indicators are passed to ODBC by pointer");
115static_assert(SqlNullData == SQL_NULL_DATA);
116static_assert(SqlUnknownParameterType == SQL_UNKNOWN_TYPE);
117// Driver-neutral code spells these ODBC integer types with the fixed-width standard types they are.
118static_assert(std::same_as<std::int16_t, SQLSMALLINT>, "std::int16_t must be SQLSMALLINT");
119static_assert(std::same_as<std::uint16_t, SQLUSMALLINT>, "std::uint16_t must be SQLUSMALLINT");
120static_assert(std::same_as<std::size_t, SQLULEN>, "std::size_t must be SQLULEN; string binder sizes are passed to ODBC");
121static_assert(std::to_underlying(SqlResult::Success) == SQL_SUCCESS);
122static_assert(std::to_underlying(SqlResult::SuccessWithInfo) == SQL_SUCCESS_WITH_INFO);
123static_assert(std::to_underlying(SqlResult::StillExecuting) == SQL_STILL_EXECUTING);
124static_assert(std::to_underlying(SqlResult::NeedData) == SQL_NEED_DATA);
125static_assert(std::to_underlying(SqlResult::NoData) == SQL_NO_DATA);
126static_assert(std::to_underlying(SqlResult::ParamDataAvailable) == SQL_PARAM_DATA_AVAILABLE);
127static_assert(std::to_underlying(SqlResult::Error) == SQL_ERROR);
128static_assert(std::to_underlying(SqlResult::InvalidHandle) == SQL_INVALID_HANDLE);
129static_assert(sizeof(SqlResult) == sizeof(SQLRETURN));
130static_assert(std::to_underlying(SqlIsolationMode::ReadUncommitted) == SQL_TXN_READ_UNCOMMITTED);
131static_assert(std::to_underlying(SqlIsolationMode::ReadCommitted) == SQL_TXN_READ_COMMITTED);
132static_assert(std::to_underlying(SqlIsolationMode::RepeatableRead) == SQL_TXN_REPEATABLE_READ);
133static_assert(std::to_underlying(SqlIsolationMode::Serializable) == SQL_TXN_SERIALIZABLE);
134
135// SqlError's return-code enumerators carry ODBC's values, so an error_code built from either compares equal.
136static_assert(std::to_underlying(SqlError::SUCCESS) == SQL_SUCCESS);
137static_assert(std::to_underlying(SqlError::SUCCESS_WITH_INFO) == SQL_SUCCESS_WITH_INFO);
138static_assert(std::to_underlying(SqlError::NODATA) == SQL_NO_DATA);
139static_assert(std::to_underlying(SqlError::FAILURE) == SQL_ERROR);
140static_assert(std::to_underlying(SqlError::INVALID_HANDLE) == SQL_INVALID_HANDLE);
141static_assert(std::to_underlying(SqlError::STILL_EXECUTING) == SQL_STILL_EXECUTING);
142static_assert(std::to_underlying(SqlError::NEED_DATA) == SQL_NEED_DATA);
143static_assert(std::to_underlying(SqlError::PARAM_DATA_AVAILABLE) == SQL_PARAM_DATA_AVAILABLE);
144static_assert(std::to_underlying(SqlError::NO_DATA_FOUND) == SQL_NO_DATA_FOUND);
145
146} // namespace Lightweight::Driver::Odbc
Represents a connection to a SQL database.
High level API for (prepared) raw SQL statements.
The ODBC driver's connection handle struct.
Definition Handles.hpp:53
bool operator==(Connection const &) const =default
Compares the native handles.
SQLHENV env
The native ODBC environment handle; null for a borrowed connection.
Definition Handles.hpp:55
SQLHDBC dbc
The native ODBC connection handle.
Definition Handles.hpp:58
The ODBC driver: names its handle structs and its driver id.
Definition Handles.hpp:66
static constexpr std::string_view Name
The driver's name, as error messages show it.
Definition Handles.hpp:77
static constexpr bool StatementsDieWithConnection
Definition Handles.hpp:87
static constexpr bool TranscodesNarrowText
Definition Handles.hpp:91
static constexpr SqlDriverId Id
The id the tagged handles carry for this driver.
Definition Handles.hpp:74
static constexpr bool SupportsParameterArrays
Parameter arrays (SQL_ATTR_PARAMSET_SIZE > 1) execute a whole batch in one SQLExecute.
Definition Handles.hpp:80
static constexpr bool SupportsRowArrayFetch
Row arrays (SQL_ATTR_ROW_ARRAY_SIZE > 1 with SQLFetchScroll) fetch a block of rows per call.
Definition Handles.hpp:83
static constexpr bool HasCatalogFunctions
The catalog is read with the ODBC catalog functions (SQLTables, SQLColumns, SQLStatistics,...
Definition Handles.hpp:94
The ODBC driver's statement handle struct.
Definition Handles.hpp:43
SQLHSTMT handle
The native ODBC statement handle.
Definition Handles.hpp:45
bool operator==(Statement const &) const =default
Compares the native handles.