Lightweight 0.20251202.0
Loading...
Searching...
No Matches
SqlRawColumn.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "Core.hpp"
6
7#include <cstddef>
8#include <span>
9
10#include <sql.h>
11
12namespace Lightweight
13{
14
15/// Metadata of a SQL column for use with highly optimized bulk insert operations.
17{
18 SQLSMALLINT cType; //!< C type of the data
19 SQLSMALLINT sqlType; //!< SQL type of the data
20 SQLULEN size; //!< Size of the data in bytes
21 SQLSMALLINT decimalDigits; //!< Number of digits to the right of the decimal point
22 SQLSMALLINT nullable; //!< Whether the column is nullable
23 SQLULEN bufferLength; //!< Buffer length for variable length types
24};
25
26/// A non-owning reference to a raw column data for batch processing.
28{
29 SqlRawColumnMetadata metadata; //!< Metadata of the column
30 std::span<std::byte const> data; //!< Raw data of the column
31 std::span<SQLLEN const> indicators; //!< Indicators of the column (SQL_NULL_DATA, SQL_DATA_AT_EXEC, etc.)
32};
33
34template <>
35struct SqlDataBinder<SqlRawColumn>
36{
37 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
38 SQLUSMALLINT column,
39 SqlRawColumn const& value,
40 SqlDataBinderCallback& /*cb*/) noexcept
41 {
42 // NOTE: For batch execution (SQL_ATTR_PARAMSET_SIZE > 1), StrLen_or_IndPtr must point to an array of indicators.
43 // The driver uses the SQL_ATTR_PARAM_BIND_TYPE to determine if 'ParameterValuePtr' is an array of values
44 // (Column-Wise) or a pointer to a structure (Row-Wise).
45 // We assume Column-Wise binding here as we control the ExecuteBatch call.
46
47 // For LOB types (SQL_LONGVARCHAR, SQL_LONGVARBINARY, SQL_WLONGVARCHAR), MS SQL Server's ODBC driver
48 // requires the ColumnSize to be the buffer length when not using data-at-execution.
49 // A size of 0 causes HY104 "Invalid precision value" error.
50 SQLULEN columnSize = value.metadata.size;
51 if (columnSize == 0 && (value.metadata.sqlType == SQL_LONGVARCHAR || value.metadata.sqlType == SQL_LONGVARBINARY
52 || value.metadata.sqlType == SQL_WLONGVARCHAR))
53 {
54 columnSize = value.metadata.bufferLength;
55 }
56
57 return SQLBindParameter(stmt,
58 column,
59 SQL_PARAM_INPUT,
60 value.metadata.cType,
61 value.metadata.sqlType,
62 columnSize,
64 const_cast<SQLPOINTER>(static_cast<void const*>(value.data.data())),
65 static_cast<SQLLEN>(value.metadata.bufferLength), // element size for variable length types
66 const_cast<SQLLEN*>(value.indicators.data()));
67 }
68};
69
70} // namespace Lightweight
Metadata of a SQL column for use with highly optimized bulk insert operations.
SQLSMALLINT cType
C type of the data.
SQLULEN size
Size of the data in bytes.
SQLULEN bufferLength
Buffer length for variable length types.
SQLSMALLINT decimalDigits
Number of digits to the right of the decimal point.
SQLSMALLINT nullable
Whether the column is nullable.
SQLSMALLINT sqlType
SQL type of the data.
A non-owning reference to a raw column data for batch processing.
SqlRawColumnMetadata metadata
Metadata of the column.
std::span< SQLLEN const > indicators
Indicators of the column (SQL_NULL_DATA, SQL_DATA_AT_EXEC, etc.)
std::span< std::byte const > data
Raw data of the column.