6#include "../../Api.hpp"
7#include "../../SqlBindingTypes.hpp"
8#include "../../SqlError.hpp"
11#include "SqlOdbcPrelude.hpp"
20#include <source_location>
39namespace Lightweight::Driver::Odbc
83 [[nodiscard]]
inline SQLPOINTER OdbcIntAttr(SQLULEN value)
noexcept
85 static_assert(
sizeof(SQLPOINTER) >=
sizeof(SQLULEN),
86 "SQLPOINTER must be wide enough to carry an integral ODBC attribute value");
87 SQLPOINTER ptr =
nullptr;
88 std::memcpy(
static_cast<void*
>(&ptr), &value,
sizeof(value));
118[[nodiscard]] LIGHTWEIGHT_API SqlResult
Prepare(
Statement& stmt, std::string_view query,
bool wideQueryText);
125 return ToResult(SQLExecute(stmt.handle));
138[[nodiscard]]
inline LIGHTWEIGHT_FORCE_INLINE SqlResult
Fetch(
Statement& stmt)
noexcept
140 return ToResult(SQLFetch(stmt.handle));
148 return ToResult(SQLMoreResults(stmt.handle));
171 if (rc == SqlResult::NoData || !Succeeded(rc))
217 SqlColumnIndex column);
224[[nodiscard]] LIGHTWEIGHT_API std::expected<SqlResultColumnType, SqlErrorInfo>
ColumnType(
Statement const& stmt,
225 SqlColumnIndex column);
258 SQLULEN columnSize {};
259 SQLSMALLINT decimalDigits {};
260 SQLSMALLINT nullable {};
261 auto columnName = std::array<SQLWCHAR, 64> {};
262 SQLSMALLINT columnNameLength {};
263 return SQLDescribeColW(stmt,
266 static_cast<SQLSMALLINT
>(columnName.size()),
280 SQLSMALLINT columnType {};
292 SqlColumnIndex column)
noexcept
313 std::size_t valueSize,
314 std::size_t columnWiseRowCount)
noexcept
316 auto bindType = SQLULEN { SQL_PARAM_BIND_BY_COLUMN };
317 if (!SQL_SUCCEEDED(SQLGetStmtAttr(stmt.handle, SQL_ATTR_PARAM_BIND_TYPE, &bindType, 0,
nullptr))
318 || bindType == SQL_PARAM_BIND_BY_COLUMN)
319 return { .
rowStride = valueSize, .rowCount = columnWiseRowCount };
321 auto parameterSetSize = SQLULEN { 1 };
322 if (!SQL_SUCCEEDED(SQLGetStmtAttr(stmt.handle, SQL_ATTR_PARAMSET_SIZE, ¶meterSetSize, 0,
nullptr)))
323 parameterSetSize = 1;
324 return { .rowStride =
static_cast<std::size_t
>(bindType), .rowCount =
static_cast<std::size_t
>(parameterSetSize) };
337 std::source_location sourceLocation)
339 return detail::CheckOdbcCall(std::to_underlying(result), stmt.
handle, sourceLocation);
350 if (!std::in_range<std::int16_t>(dataType))
351 return SqlResultColumnType::Unknown;
365 SqlColumnIndex column,
366 SqlOutputColumnLength length)
noexcept;
375 SqlColumnIndex column,
398template <
typename Check>
401 check(ToResult(SQLSetStmtAttr(stmt.
handle, SQL_ATTR_PARAMSET_SIZE, detail::OdbcIntAttr(rowCount), 0)));
402 check(ToResult(SQLSetStmtAttr(stmt.
handle, SQL_ATTR_PARAM_BIND_OFFSET_PTR, bindOffset, 0)));
403 check(ToResult(SQLSetStmtAttr(stmt.
handle, SQL_ATTR_PARAM_BIND_TYPE, SQL_PARAM_BIND_BY_COLUMN, 0)));
404 check(ToResult(SQLSetStmtAttr(stmt.
handle, SQL_ATTR_PARAM_OPERATION_PTR, SQL_PARAM_PROCEED, 0)));
413template <
typename Check>
415 Statement& stmt, std::size_t rowCount, std::size_t rowStride,
ProcessedCount* processedCount, Check
const& check)
417 check(ToResult(SQLSetStmtAttr(stmt.
handle, SQL_ATTR_PARAMSET_SIZE, detail::OdbcIntAttr(rowCount), 0)));
418 check(ToResult(SQLSetStmtAttr(stmt.
handle, SQL_ATTR_PARAM_BIND_TYPE, detail::OdbcIntAttr(rowStride), 0)));
419 check(ToResult(SQLSetStmtAttr(stmt.
handle, SQL_ATTR_PARAM_BIND_OFFSET_PTR,
nullptr, 0)));
420 check(ToResult(SQLSetStmtAttr(stmt.
handle, SQL_ATTR_PARAM_OPERATION_PTR, SQL_PARAM_PROCEED, 0)));
421 check(ToResult(SQLSetStmtAttr(stmt.
handle, SQL_ATTR_PARAMS_PROCESSED_PTR, processedCount, 0)));
429template <
typename Check>
432 check(ToResult(SQLSetStmtAttr(stmt.
handle, SQL_ATTR_PARAM_STATUS_PTR, paramStatus, 0)));
433 check(ToResult(SQLSetStmtAttr(stmt.
handle, SQL_ATTR_PARAMS_PROCESSED_PTR, processedCount, 0)));
444template <
typename Check>
446 std::size_t rowBindType,
447 std::size_t arrayDepth,
452 check(ToResult(SQLSetStmtAttr(stmt.
handle, SQL_ATTR_ROW_BIND_TYPE, detail::OdbcIntAttr(rowBindType), 0)));
453 check(ToResult(SQLSetStmtAttr(stmt.
handle, SQL_ATTR_ROW_ARRAY_SIZE, detail::OdbcIntAttr(arrayDepth), 0)));
454 check(ToResult(SQLSetStmtAttr(stmt.
handle, SQL_ATTR_ROW_STATUS_PTR, rowStatus, 0)));
455 check(ToResult(SQLSetStmtAttr(stmt.
handle, SQL_ATTR_ROWS_FETCHED_PTR, rowsFetched, 0)));
467 SqlColumnIndex column,
470 SqlIndicator bufferLength,
471 SqlIndicator* indicators)
noexcept
473 return ToResult(SQLBindCol(stmt.handle, column, cType, buffer, bufferLength, indicators));
484 Statement& stmt, SqlColumnIndex column,
char* buffer, SqlIndicator bufferLength, SqlIndicator* indicators)
noexcept
486 return BindColumn(stmt, column, SQL_C_CHAR, buffer, bufferLength, indicators);
502 Statement& stmt, std::uint16_t parameter,
char16_t* text, std::size_t columnSize, SqlIndicator& lengthIndicator)
noexcept
504 return ToResult(SQLBindParameter(stmt.handle,
509 static_cast<SQLULEN
>(columnSize),
521 return ToResult(SQLFetchScroll(stmt.handle, SQL_FETCH_NEXT, 0));
530 SqlColumnIndex column,
536 auto const result =
DescribeColumn(stmt, column, odbcDescription);
538 .columnSize = odbcDescription.columnSize };
void ResetParameterArrays(StatementHandle &statement) noexcept
Restores single-row parameter binding after a parameter-array execution. Not reported.
void CloseCursor(StatementHandle &statement) noexcept
Closes the cursor of a result set that was read to its end. Pending result sets are left alone.
SqlResult UnbindColumns(StatementHandle &statement) noexcept
Unbinds every bound result column.
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...
void Allocate(ConnectionHandle &connection) noexcept
Allocates the driver's connection resources, unless the connection already holds them.
SqlResult NumResultColumns(StatementHandle const &statement, std::int16_t &count) noexcept
Reports the number of columns of the current result set.
SqlResult SetQueryTimeout(StatementHandle &statement, std::chrono::seconds timeout) noexcept
Sets the query timeout of the statement.
void DiscardResults(StatementHandle &statement) noexcept
Drains every pending result set and closes the cursor, so the handle can run another query....
void Free(StatementHandle &statement) noexcept
Frees the statement's driver handle and leaves statement empty, bound to the same driver.
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...
SqlResult NumParameters(StatementHandle const &statement, std::int16_t &count) noexcept
Reports the number of parameters of the prepared statement.
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.
SqlResult QueryTimeout(StatementHandle const &statement, std::chrono::seconds &timeout) noexcept
Reports the query timeout of the statement.
void ResetRowArrayAttributes(StatementHandle &statement) noexcept
Restores single-row fetching after a row-array fetch. Not reported; nothing for a driver without row ...
SqlResult NumRowsAffected(StatementHandle const &statement, std::ptrdiff_t &count) noexcept
Reports the number of rows the last executed statement affected.
void Abandon(StatementHandle &statement) noexcept
Gives up the statement's driver handle after its connection session ended, and leaves statement empty...
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...
std::int16_t BoundParameterCount(StatementHandle const &statement) noexcept
The number of parameters currently bound on the statement.
SqlResult DescribeColumn(StatementHandle const &statement, SqlColumnIndex column, SqlColumnDescription &description)
Describes a result column: its type and its size.
SqlResult ResetParameters(StatementHandle &statement) noexcept
Drops every parameter binding.
std::expected< SqlResultColumnType, SqlErrorInfo > ColumnType(StatementHandle const &statement, SqlColumnIndex column)
The SQL data type of a result column, as the driver describes it.
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.
SqlResult MoreResults(StatementHandle &statement) noexcept
Advances to the next pending result set.
CallOutcome CheckResult(StatementHandle const &statement, SqlResult result, std::source_location location)
The non-throwing, fault-seam-aware verdict on result.
void SetRowWiseParameterArray(Statement &stmt, std::size_t rowCount, std::size_t rowStride, ProcessedCount *processedCount, Check const &check)
Sets up row-wise parameter-array binding of rowCount parameter sets, rowStride bytes apart.
LIGHTWEIGHT_API void ReleaseCursor(Statement &stmt) noexcept
Closes any open cursor and discards its pending rows (SQLFreeStmt with SQL_CLOSE),...
LIGHTWEIGHT_FORCE_INLINE SqlResult BindUtf16TextParameter(Statement &stmt, std::uint16_t parameter, char16_t *text, std::size_t columnSize, SqlIndicator &lengthIndicator) noexcept
Binds UTF-16 text as an input parameter of SQL type SQL_WVARCHAR (SQLBindParameter with SQL_C_WCHAR)....
SQLULEN ProcessedCount
The count a driver writes back through a parameter-array or row-array attribute pointer (SQL_ATTR_PAR...
SQLUSMALLINT RowStatus
One element of a parameter-status or row-status array (SQL_ATTR_PARAM_STATUS_PTR, SQL_ATTR_ROW_STATUS...
constexpr RowStatus ParamStatusUnused
The parameter status of a parameter set the driver did not process (SQL_PARAM_UNUSED).
LIGHTWEIGHT_API std::expected< std::string, SqlErrorInfo > ColumnName(Statement const &stmt, SqlColumnIndex column)
The name of a result column, as the driver describes it.
constexpr RowStatus RowStatusError
The row status of a fetched row the driver could not convert (SQL_ROW_ERROR).
void SetColumnWiseParameterArray(Statement &stmt, std::size_t rowCount, void *bindOffset, Check const &check)
Sets up column-wise parameter-array binding of rowCount parameter sets.
LIGHTWEIGHT_API SqlResultColumnType ToResultColumnType(std::int16_t sqlType) noexcept
Maps an ODBC SQL_* type code onto the driver-neutral SqlResultColumnType.
constexpr RowStatus ParamStatusError
The parameter status of a parameter set that failed (SQL_PARAM_ERROR).
LIGHTWEIGHT_FORCE_INLINE SqlResult FetchNextBlock(Statement &stmt) noexcept
Fetches the next block of rows into the row-array bound columns (SQLFetchScroll, SQL_FETCH_NEXT).
ParameterArrayLayout CurrentParameterArrayLayout(Statement const &stmt, std::size_t valueSize, std::size_t columnWiseRowCount) noexcept
constexpr std::int16_t UnknownParameterType
The declared SQL type DescribeParameterTypes reports for a parameter it could not describe.
void SetRowArray(Statement &stmt, std::size_t rowBindType, std::size_t arrayDepth, RowStatus *rowStatus, ProcessedCount *rowsFetched, Check const &check)
Sets up row-array fetching of up to arrayDepth rows per FetchNextBlock.
LIGHTWEIGHT_FORCE_INLINE SqlResult BindColumn(Statement &stmt, SqlColumnIndex column, std::int16_t cType, void *buffer, SqlIndicator bufferLength, SqlIndicator *indicators) noexcept
Binds a result column to a buffer (SQLBindCol).
LIGHTWEIGHT_FORCE_INLINE SqlResult BindTextColumn(Statement &stmt, SqlColumnIndex column, char *buffer, SqlIndicator bufferLength, SqlIndicator *indicators) noexcept
Binds a result column to a narrow character buffer (SQL_C_CHAR).
void SetParameterStatusArrays(Statement &stmt, RowStatus *paramStatus, ProcessedCount *processedCount, Check const &check)
Binds the arrays the driver reports per-parameter-set status and the processed count into.
@ 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...
What DescribeColumn reports about a result column.
std::int16_t sqlType
The SQL_* type code of the column.
std::size_t columnSize
The column size (maximum character count for character columns); 0 when the driver cannot bound it.
The ODBC driver's connection handle struct.
The layout of the parameter array that is being bound.
std::size_t rowStride
The byte distance from one row's value to the next.
std::size_t rowCount
The number of rows.
The ODBC driver's statement handle struct.
SQLHSTMT handle
The native ODBC statement handle.
What a driver reports about a result column (see DescribeColumn).
SqlResultColumnType type
The column's type, SqlResultColumnType::Unknown for one without an enumerator.