6#include "../../DataBinder/Core.hpp"
7#include "../../SqlBindingTypes.hpp"
8#include "../../SqlError.hpp"
9#include "../Dispatch.hpp"
12#include "SqlOdbcPrelude.hpp"
30namespace Lightweight::Driver::Odbc
43template <
typename Ops,
typename Container, std::
size_t UnitBytes, std::
size_t TerminatorUnits>
47 auto*
const result =
static_cast<Container*
>(step.
target);
48 auto const size =
static_cast<std::size_t
>(Ops::Size(result));
49 if (
static_cast<void const*
>(Ops::Data(result)) != step.
boundData)
56 if constexpr (
requires { Ops::ResizeForOverwrite(result, step.
boundSize); })
57 Ops::ResizeForOverwrite(result, step.
boundSize);
59 Ops::Resize(result,
static_cast<SQLLEN
>(step.
boundSize));
61 auto*
const data = Ops::Data(result);
62 if (
static_cast<void const*
>(data) == step.
boundData)
63 return SqlResult::Success;
64 auto const rc = SQLBindCol(stmt.
handle,
67 static_cast<SQLPOINTER
>(data),
68 static_cast<SQLLEN
>((step.
boundSize + TerminatorUnits) * UnitBytes),
72 if (SQL_SUCCEEDED(rc))
95 info.nativeErrorCode = 0;
97 totalLength == SQL_NO_TOTAL ? std::string {
"length unknown" } : std::format(
"{} bytes", totalLength);
98 info.message = std::format(
"Column {}: the value ({}) is longer than its bound buffer, and the driver cannot "
99 "read a bound column again (no SQL_GD_BOUND). Read the column with GetColumn(), or "
100 "size the bound variable to the longest value before binding it. ({})",
104 throw SqlException(std::move(info));
131template <
typename Ops, std::
size_t UnitBytes, std::
size_t TerminatorUnits,
typename Container,
typename GetData>
133 SqlColumnIndex column,
137 GetData
const& getData,
138 std::size_t maxBytes = ::Lightweight::detail::MaxUnknownLengthValueBytes)
140 using ::Lightweight::detail::DefaultBoundColumnSize;
141 using ::Lightweight::detail::ThrowBoundValueMisread;
142 auto const terminatorUnits = std::size_t { cType == SQL_C_BINARY ? 0U : 1U };
143 if (Ops::Size(result) < DefaultBoundColumnSize)
144 Ops::Resize(result,
static_cast<SQLLEN
>(DefaultBoundColumnSize));
145 auto read = std::size_t { 0 };
149 auto const units = Ops::Size(result) - read + TerminatorUnits;
151 getData(
static_cast<SQLPOINTER
>(Ops::Data(result) + read),
static_cast<SQLLEN
>(units * UnitBytes), indicator);
152 if (rv == SQL_NO_DATA)
160 if (!SQL_SUCCEEDED(rv))
162 if (first && *indicator == SQL_NULL_DATA)
165 if (rv == SQL_SUCCESS)
168 if (*indicator < 0 || std::cmp_greater(*indicator, (units - terminatorUnits) * UnitBytes))
169 ThrowBoundValueMisread(column,
"HY000", std::format(
"the driver reported {} bytes", *indicator));
170 read +=
static_cast<std::size_t
>(*indicator) / UnitBytes;
173 read += units - terminatorUnits;
174 if (Ops::Size(result) * 2 * UnitBytes > maxBytes)
175 ThrowBoundValueMisread(column,
"22001", std::format(
"it is longer than {} bytes", maxBytes));
176 Ops::Resize(result,
static_cast<SQLLEN
>(Ops::Size(result) * 2));
178 *indicator =
static_cast<SQLLEN
>(read * UnitBytes);
184template <
typename Ops, std::
size_t UnitBytes, std::
size_t TerminatorUnits,
typename Container>
186 SQLHSTMT stmt, SqlColumnIndex column, SQLSMALLINT cType, Container* result, SQLLEN* indicator)
188 return ReadBoundValueOfUnknownLength<Ops, UnitBytes, TerminatorUnits>(
189 stmt, column, cType, result, indicator, [&](SQLPOINTER target, SQLLEN length, SQLLEN* lengthIndicator) {
190 return SQLGetData(stmt, column, cType, target, length, lengthIndicator);
212template <
typename Ops, std::
size_t UnitBytes, std::
size_t TerminatorUnits,
typename Container>
214 SqlColumnIndex column, SQLSMALLINT cType, Container* result, SQLLEN* indicator,
SqlDataBinderCallback& cb)
217 .
run = &RestoreBoundBuffer<Ops, Container, UnitBytes, TerminatorUnits>,
219 .boundData =
static_cast<void const*
>(Ops::Data(result)),
220 .boundSize =
static_cast<std::size_t
>(Ops::Size(result)),
224 step.indicator = indicator;
void ThrowBoundValueNotReadable(SQLHSTMT stmt, SqlColumnIndex column, SQLLEN totalLength)
Fails the fetch of a bound value longer than its buffer that the driver cannot read again.
void PlanBoundBufferRestore(SqlColumnIndex column, SQLSMALLINT cType, Container *result, SQLLEN *indicator, SqlDataBinderCallback &cb)
Keeps an output column that is bound straight into a growable container bound for every fetch of the ...
SqlResult RestoreBoundBuffer(SqlOutputColumnPreFetch &step, ::Lightweight::Driver::StatementHandle &statement)
The pre-fetch step of PlanBoundBufferRestore: grows the container back to the bound size and re-binds...
std::size_t ReadBoundValueOfUnknownLength(SQLHSTMT stmt, SqlColumnIndex column, SQLSMALLINT cType, Container *result, SQLLEN *indicator, GetData const &getData, std::size_t maxBytes=::Lightweight::detail::MaxUnknownLengthValueBytes)
Reads a bound value again in full that the driver reported longer than its buffer without saying how ...
decltype(auto) VisitRequired(Handle &&handle, std::string_view operation, F &&f)
Calls f with the held struct when invocable; otherwise throws SqlException(NotSupportedByDriver(id,...
SqlErrorInfo Diagnose(StatementHandle const &statement)
Reads the statement's diagnostics: the first record its last failed call left.
virtual void PlanPreFetchOutputColumn(SqlOutputColumnPreFetch const &step)=0
The ODBC driver's statement handle struct.
SQLHSTMT handle
The native ODBC statement handle.
std::string sqlState
The SQLSTATE diagnostic code (5 characters).
A step a binder plans to run before every fetch into the output column it binds.
std::size_t boundSize
The element count the column is currently bound with.
SqlIndicator * indicator
The column's length/indicator buffer.
void * target
The bound object the step works on.
void const * boundData
The storage the column is currently bound to.
std::int16_t cType
The driver's C type code the column is bound as.
SqlResult(* run)(SqlOutputColumnPreFetch &step, Driver::StatementHandle &statement)
Runs the step. Returns SqlResult::Success, or the failing result (the fetch then fails with it).
SqlColumnIndex column
The 1-based column index.