5#include "../SqlColumnTypeDefinitions.hpp"
6#include "BasicStringBinder.hpp"
22template <std::
size_t N>
25 using BaseType = std::vector<uint8_t>;
38#if defined(_MSVC_STL_VERSION)
39 using ByteView = std::basic_string_view<value_type>;
45 static constexpr auto ColumnType = SqlColumnTypeDefinitions::VarBinary { N };
69 template <std::
size_t SourceSize>
71 _base { text, text + SourceSize }
73 static_assert(SourceSize <= N + 1,
"RHS string size must not exceed target string's capacity.");
77 template <std::
size_t SourceSize>
81 static_assert(SourceSize <= N + 1,
"RHS string size must not exceed target string's capacity.");
89#if !defined(LIGHTWEIGHT_CXX26_REFLECTION)
92 _base { s.data(), s.data() + s.size() }
97 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr ByteView Bytes() const noexcept
99 return { _base.data(), _base.size() };
104 [[deprecated(
"use Bytes(); SqlDynamicBinary stores raw bytes, not text")]] [[nodiscard]]
115 return _base <=> other._base;
121 return _base == other._base;
125 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::size_t
size() const noexcept
131 void LIGHTWEIGHT_FORCE_INLINE
resize(std::size_t newSize)
133 _base.resize(newSize);
137 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool empty() const noexcept
139 return _base.empty();
143 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr decltype(
auto)
data(
this auto&& self)
noexcept
145 self.EnsureNonNullBuffer();
146 return self._base.data();
150 LIGHTWEIGHT_FORCE_INLINE
void clear() noexcept
156 void EnsureNonNullBuffer()
const
158 if (_base.data() ==
nullptr)
166 mutable SQLLEN _indicator = 0;
175 struct IsSqlDynamicBinaryImpl: std::false_type
180 struct IsSqlDynamicBinaryImpl<SqlDynamicBinary<T>>: std::true_type
185 struct IsSqlDynamicBinaryImpl<std::optional<SqlDynamicBinary<T>>>: std::true_type
192constexpr bool IsSqlDynamicBinary = detail::IsSqlDynamicBinaryImpl<T>::value;
196template <std::
size_t N>
197struct std::formatter<Lightweight::SqlDynamicBinary<N>>: std::formatter<std::string>
201 std::string humanReadableText;
202 for (
auto byte: text)
204 if (
byte >= 0x20 &&
byte <= 0x7E)
205 humanReadableText +=
static_cast<char>(byte);
207 humanReadableText += std::format(
"\\x{:02X}",
byte);
209 return std::formatter<std::string>::format(humanReadableText.data(), ctx);
216template <std::
size_t N>
217struct SqlDataBinder<SqlDynamicBinary<N>>
219 using ValueType = SqlDynamicBinary<N>;
221 static constexpr auto ColumnType = SqlColumnTypeDefinitions::VarBinary { N };
223 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
225 ValueType
const& value,
226 SqlDataBinderCallback& )
noexcept
228 value._indicator =
static_cast<SQLLEN
>(value.size());
230 auto const sqlType =
static_cast<SQLSMALLINT
>(value.size() > 8000 ? SQL_LONGVARBINARY : SQL_VARBINARY);
231 auto const bufferSize =
static_cast<SQLULEN
>(value.size());
232 auto*
const ptr = (SQLPOINTER) value.data();
234 return SQLBindParameter(
235 stmt, column, SQL_PARAM_INPUT, SQL_C_BINARY, sqlType, bufferSize, 0, ptr, 0, &value._indicator);
238 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(
239 SQLHSTMT stmt, SQLUSMALLINT column, ValueType* result, SQLLEN* indicator, SqlDataBinderCallback& cb)
noexcept
244 cb.PlanPostProcessOutputColumn([stmt, column, result, indicator]() {
245 if (*indicator == SQL_NULL_DATA)
248 else if (*indicator == SQL_NO_TOTAL)
250 result->resize(result->size() - 1);
251 else if (*indicator <=
static_cast<SQLLEN
>(result->size()))
252 result->resize(
static_cast<size_t>(*indicator));
258 auto const totalCharsRequired = *indicator;
259 result->resize(
static_cast<size_t>(totalCharsRequired + 1));
260 auto const sqlResult =
261 SQLGetData(stmt, column, SQL_C_BINARY, (SQLPOINTER) result->data(), totalCharsRequired + 1, indicator);
263 assert(SQL_SUCCEEDED(sqlResult));
264 assert(*indicator == totalCharsRequired);
265 result->resize(
static_cast<size_t>(totalCharsRequired));
269 return SQLBindCol(stmt, column, SQL_C_BINARY, (SQLPOINTER) result->data(), 255, indicator);
272 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt,
276 SqlDataBinderCallback
const& )
noexcept
281 return detail::GetRawColumnArrayData<SQL_C_BINARY>(stmt, column, result, indicator);
284 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(ValueType
const& value)
286 return std::format(
"SqlBinary<{}>(size={})", N, value.size());
LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicBinary(value_type const *s, value_type const *e) noexcept
Constructs a fixed-size string from a string pointer and end pointer.
static constexpr auto ColumnType
The SQL column type definition for this binary type.
LIGHTWEIGHT_FORCE_INLINE constexpr decltype(auto) data(this auto &&self) noexcept
Retrieves the pointer to the string data.
LIGHTWEIGHT_FORCE_INLINE constexpr bool empty() const noexcept
Tests if the stored data is empty.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicBinary(ByteView s) noexcept
Constructs the binary payload from a contiguous view of bytes.
LIGHTWEIGHT_FORCE_INLINE void clear() noexcept
Clears the storad data.
LIGHTWEIGHT_FORCE_INLINE constexpr std::size_t size() const noexcept
Retrieves the size of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr ByteView Bytes() const noexcept
Retrieves a read-only view over the stored bytes.
LIGHTWEIGHT_FORCE_INLINE constexpr ByteView ToStringView() const noexcept
void LIGHTWEIGHT_FORCE_INLINE resize(std::size_t newSize)
Resizes the underlying data storage to the given size.
uint8_t value_type
The element type of the binary data.
constexpr bool operator==(SqlDynamicBinary< N > const &other) const noexcept
Equality operator derived from the spaceship comparison above.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicBinary() noexcept=default
Default constructor, creates an empty object.
static constexpr std::size_t DynamicCapacity
The maximum size of the underlying data storage.
constexpr LIGHTWEIGHT_FORCE_INLINE SqlDynamicBinary(std::initializer_list< value_type > data)
Constructs the object from an initializer list of bytes.
std::span< value_type const > ByteView
constexpr auto operator<=>(SqlDynamicBinary< N > const &other) const noexcept