Lightweight 0.20261105.1
Loading...
Searching...
No Matches
BoundOutputColumn.hpp File Reference
#include "../../DataBinder/Core.hpp"
#include "../../SqlBindingTypes.hpp"
#include "../../SqlError.hpp"
#include "../Dispatch.hpp"
#include "Diagnostics.hpp"
#include "Handles.hpp"
#include "SqlOdbcPrelude.hpp"
#include <algorithm>
#include <cstddef>
#include <format>
#include <string>
#include <utility>
#include <sql.h>
#include <sqlext.h>

Go to the source code of this file.

Functions

template<typename Ops , typename Container , std::size_t UnitBytes, std::size_t TerminatorUnits>
SqlResult Lightweight::Driver::Odbc::RestoreBoundBuffer (SqlOutputColumnPreFetch &step, ::Lightweight::Driver::StatementHandle &statement)
 The pre-fetch step of PlanBoundBufferRestore: grows the container back to the bound size and re-binds the column if its storage moved.
 
void Lightweight::Driver::Odbc::ThrowBoundValueNotReadable (SQLHSTMT stmt, SqlColumnIndex column, SQLLEN totalLength)
 Fails the fetch of a bound value longer than its buffer that the driver cannot read again.
 
template<typename Ops , std::size_t UnitBytes, std::size_t TerminatorUnits, typename Container , typename GetData >
std::size_t Lightweight::Driver::Odbc::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 long (SQL_NO_TOTAL), in chunks through getData, doubling result as it goes.
 
template<typename Ops , std::size_t UnitBytes, std::size_t TerminatorUnits, typename Container >
std::size_t Lightweight::Driver::Odbc::ReadBoundValueOfUnknownLength (SQLHSTMT stmt, SqlColumnIndex column, SQLSMALLINT cType, Container *result, SQLLEN *indicator)
 ReadBoundValueOfUnknownLength reading through SQLGetData.
 
template<typename Ops , std::size_t UnitBytes, std::size_t TerminatorUnits, typename Container >
void Lightweight::Driver::Odbc::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 result set.
 

Detailed Description

The ODBC driver's part of binding an output column straight into a growable container: the pre-fetch step that restores the bound buffer before every fetch, the declared-length lookup that sizes it where the driver cannot read a bound column again, and the re-read of a bound value of unknown length. The leaf binders' ODBC overloads (strings, binary) use them; the native SQLite3 driver reads every bound column afresh for each row and needs none of them.

Definition in file BoundOutputColumn.hpp.

Function Documentation

◆ RestoreBoundBuffer()

template<typename Ops , typename Container , std::size_t UnitBytes, std::size_t TerminatorUnits>
SqlResult Lightweight::Driver::Odbc::RestoreBoundBuffer ( SqlOutputColumnPreFetch &  step,
::Lightweight::Driver::StatementHandle &  statement 
)

The pre-fetch step of PlanBoundBufferRestore: grows the container back to the bound size and re-binds the column if its storage moved.

Template Parameters
OpsSqlBasicStringOperations-shaped buffer operations of Container.
UnitBytesBytes per element of the bound buffer.
TerminatorUnitsElements the bound buffer has beyond its size for the driver's terminator.
Parameters
stepThe planned step.
statementThe statement the column is bound on (an ODBC statement: only the ODBC binders plan the step).
Returns
SqlResult::Success, or the result of re-binding the column.
Exceptions
std::bad_allocWhen growing the container back allocates (its storage was moved out); the fetch reports that as HY001.

Definition at line 44 of file BoundOutputColumn.hpp.

References Lightweight::SqlOutputColumnPreFetch::boundData, Lightweight::SqlOutputColumnPreFetch::boundSize, Lightweight::SqlOutputColumnPreFetch::column, Lightweight::SqlOutputColumnPreFetch::cType, Lightweight::Driver::Odbc::Statement::handle, Lightweight::SqlOutputColumnPreFetch::indicator, Lightweight::Driver::Odbc::RestoreBoundBuffer(), Lightweight::SqlOutputColumnPreFetch::target, and Lightweight::Driver::VisitRequired().

Referenced by Lightweight::Driver::Odbc::RestoreBoundBuffer().

◆ ThrowBoundValueNotReadable()

void Lightweight::Driver::Odbc::ThrowBoundValueNotReadable ( SQLHSTMT  stmt,
SqlColumnIndex  column,
SQLLEN  totalLength 
)
inline

Fails the fetch of a bound value longer than its buffer that the driver cannot read again.

The binders' post-processing reads such a value with SQLGetData; a driver without SQL_GD_BOUND (MS SQL Server) refuses that for a bound column. Rather than return the value truncated to the buffer, the fetch fails with this error (SQLSTATE 22001).

Parameters
stmtThe statement the column is bound on (its diagnostic names the driver's reason).
columnThe 1-based column index.
totalLengthThe value's length as the driver reported it, in bytes, or SQL_NO_TOTAL.

Definition at line 89 of file BoundOutputColumn.hpp.

References Lightweight::Driver::Diagnose(), Lightweight::Driver::Odbc::Statement::handle, Lightweight::SqlErrorInfo::sqlState, and Lightweight::Driver::Odbc::ThrowBoundValueNotReadable().

Referenced by Lightweight::Driver::Odbc::ReadBoundValueOfUnknownLength(), and Lightweight::Driver::Odbc::ThrowBoundValueNotReadable().

◆ ReadBoundValueOfUnknownLength() [1/2]

template<typename Ops , std::size_t UnitBytes, std::size_t TerminatorUnits, typename Container , typename GetData >
std::size_t Lightweight::Driver::Odbc::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 long (SQL_NO_TOTAL), in chunks through getData, doubling result as it goes.

Only a row whose value overflowed its bound buffer gets here. The container keeps its grown size, so the caller decides what the value's length means for it, and re-binds the column if the container's storage moved (see PlanBoundBufferRestore), also when this throws.

Template Parameters
OpsSqlBasicStringOperations-shaped buffer operations of Container.
UnitBytesBytes per element of the buffer.
TerminatorUnitsElements the bound buffer has beyond its size for the driver's terminator.
Parameters
stmtThe statement the column is bound on (for the driver's diagnostic).
columnThe 1-based column index.
cTypeThe C data type the column is bound as; a character type takes one element of each chunk for its terminator.
resultThe bound container.
indicatorThe column's length/indicator buffer; set to the value's length in bytes.
getDataReads the next chunk: SQLGetData of the column as cType, given the target, its length in bytes and the indicator.
maxBytesThe most bytes the container grows to.
Returns
The value's length in elements; 0 for a NULL, which leaves indicator at SQL_NULL_DATA (a column unbound after a failed re-bind is read through here, NULL rows included).
Exceptions
SqlException22001 when the driver cannot read the bound column again (see ThrowBoundValueNotReadable) or the value exceeds maxBytes; HY000 when the driver reports a length that contradicts the call.

Definition at line 132 of file BoundOutputColumn.hpp.

References Lightweight::Driver::Odbc::ReadBoundValueOfUnknownLength(), and Lightweight::Driver::Odbc::ThrowBoundValueNotReadable().

Referenced by Lightweight::Driver::Odbc::ReadBoundValueOfUnknownLength(), and Lightweight::Driver::Odbc::ReadBoundValueOfUnknownLength().

◆ ReadBoundValueOfUnknownLength() [2/2]

template<typename Ops , std::size_t UnitBytes, std::size_t TerminatorUnits, typename Container >
std::size_t Lightweight::Driver::Odbc::ReadBoundValueOfUnknownLength ( SQLHSTMT  stmt,
SqlColumnIndex  column,
SQLSMALLINT  cType,
Container *  result,
SQLLEN *  indicator 
)

ReadBoundValueOfUnknownLength reading through SQLGetData.

Returns
The value's length in elements.

Definition at line 185 of file BoundOutputColumn.hpp.

References Lightweight::Driver::Odbc::ReadBoundValueOfUnknownLength().

◆ PlanBoundBufferRestore()

template<typename Ops , std::size_t UnitBytes, std::size_t TerminatorUnits, typename Container >
void Lightweight::Driver::Odbc::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 result set.

The binder's post-processing shrinks the container to each row's length, so before the next fetch it is grown back to the bound size. When its storage moved — a truncation re-fetch grew it, or the caller moved the value out — the column is re-bound to the new storage, at the larger of the old bound size and the container's current size. Per row this is a size compare and a pointer compare when nothing changed; growing back within the retained capacity allocates nothing, and planning the step allocates nothing either (see SqlOutputColumnPreFetch).

Template Parameters
OpsSqlBasicStringOperations-shaped buffer operations of Container.
UnitBytesBytes per element of the bound buffer.
TerminatorUnitsElements the bound buffer has beyond its size for the driver's terminator.
Parameters
columnThe 1-based column index.
cTypeThe C data type the column is bound as.
resultThe bound container.
indicatorThe column's length/indicator buffer.
cbThe binder callback the step is planned on.

Definition at line 213 of file BoundOutputColumn.hpp.

References Lightweight::Driver::Odbc::PlanBoundBufferRestore(), Lightweight::SqlDataBinderCallback::PlanPreFetchOutputColumn(), and Lightweight::SqlOutputColumnPreFetch::run.

Referenced by Lightweight::Driver::Odbc::PlanBoundBufferRestore().