5#include "../SqlColumnTypeDefinitions.hpp"
6#include "BasicStringBinder.hpp"
21template <std::
size_t N>
24 using BaseType = std::vector<uint8_t>;
27 using value_type = uint8_t;
28 static constexpr auto ColumnType = SqlColumnTypeDefinitions::VarBinary { N };
41 template <std::
size_t SourceSize>
42 constexpr LIGHTWEIGHT_FORCE_INLINE
SqlDynamicBinary(value_type const (&text)[SourceSize]):
43 _base { text, text + SourceSize }
45 static_assert(SourceSize <= N + 1,
"RHS string size must not exceed target string's capacity.");
49 template <std::
size_t SourceSize>
53 static_assert(SourceSize <= N + 1,
"RHS string size must not exceed target string's capacity.");
57 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDynamicBinary(value_type
const* s, value_type
const* e)
noexcept:
61#if !defined(LIGHTWEIGHT_CXX26_REFLECTION)
63 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDynamicBinary(std::basic_string_view<value_type> s)
noexcept:
64 _base {
static_cast<uint8_t const*
>(s.data()),
static_cast<uint8_t const*
>(s.data() + s.size()) }
69 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<value_type>
ToStringView() const noexcept
71 return { _base.data(), _base.size() };
78 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::size_t
size() const noexcept
84 void LIGHTWEIGHT_FORCE_INLINE
resize(std::size_t newSize)
86 _base.resize(newSize);
90 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool empty() const noexcept
96 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr decltype(
auto)
data(
this auto&& self)
noexcept
98 self.EnsureNonNullBuffer();
99 return self._base.data();
103 LIGHTWEIGHT_FORCE_INLINE
void clear() noexcept
109 void EnsureNonNullBuffer()
const
111 if (_base.data() ==
nullptr)
119 mutable SQLLEN _indicator = 0;
121 friend struct SqlDataBinder<SqlDynamicBinary<N>>;
128 struct IsSqlDynamicBinaryImpl: std::false_type
133 struct IsSqlDynamicBinaryImpl<SqlDynamicBinary<T>>: std::true_type
138 struct IsSqlDynamicBinaryImpl<std::optional<SqlDynamicBinary<T>>>: std::true_type
145constexpr bool IsSqlDynamicBinary = detail::IsSqlDynamicBinaryImpl<T>::value;
149template <std::
size_t N>
150struct std::formatter<Lightweight::SqlDynamicBinary<N>>: std::formatter<std::string>
154 std::string humanReadableText;
155 for (
auto byte: text)
157 if (
byte >= 0x20 &&
byte <= 0x7E)
158 humanReadableText +=
static_cast<char>(byte);
160 humanReadableText += std::format(
"\\x{:02X}",
byte);
162 return std::formatter<std::string>::format(humanReadableText.data(), ctx);
169template <std::
size_t N>
170struct SqlDataBinder<SqlDynamicBinary<N>>
172 using ValueType = SqlDynamicBinary<N>;
174 static constexpr auto ColumnType = SqlColumnTypeDefinitions::VarBinary { N };
176 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
178 ValueType
const& value,
179 SqlDataBinderCallback& )
noexcept
181 value._indicator =
static_cast<SQLLEN
>(value.size());
183 auto const sqlType =
static_cast<SQLSMALLINT
>(value.size() > 8000 ? SQL_LONGVARBINARY : SQL_VARBINARY);
184 auto const bufferSize =
static_cast<SQLULEN
>(value.size());
185 auto*
const ptr = (SQLPOINTER) value.data();
187 return SQLBindParameter(
188 stmt, column, SQL_PARAM_INPUT, SQL_C_BINARY, sqlType, bufferSize, 0, ptr, 0, &value._indicator);
191 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(
192 SQLHSTMT stmt, SQLUSMALLINT column, ValueType* result, SQLLEN* indicator, SqlDataBinderCallback& cb)
noexcept
197 cb.PlanPostProcessOutputColumn([stmt, column, result, indicator]() {
198 if (*indicator == SQL_NULL_DATA)
201 else if (*indicator == SQL_NO_TOTAL)
203 result->resize(result->size() - 1);
204 else if (*indicator <=
static_cast<SQLLEN
>(result->size()))
205 result->resize(
static_cast<size_t>(*indicator));
211 auto const totalCharsRequired = *indicator;
212 result->resize(
static_cast<size_t>(totalCharsRequired + 1));
213 auto const sqlResult =
214 SQLGetData(stmt, column, SQL_C_BINARY, (SQLPOINTER) result->data(), totalCharsRequired + 1, indicator);
216 assert(SQL_SUCCEEDED(sqlResult));
217 assert(*indicator == totalCharsRequired);
218 result->resize(
static_cast<size_t>(totalCharsRequired));
222 return SQLBindCol(stmt, column, SQL_C_BINARY, (SQLPOINTER) result->data(), 255, indicator);
225 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt,
229 SqlDataBinderCallback
const& )
noexcept
234 return detail::GetRawColumnArrayData<SQL_C_BINARY>(stmt, column, result, indicator);
237 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(ValueType
const& value)
239 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.
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 void clear() noexcept
Clears the storad data.
LIGHTWEIGHT_FORCE_INLINE constexpr std::size_t size() const noexcept
Retrieves the size of the string.
void LIGHTWEIGHT_FORCE_INLINE resize(std::size_t newSize)
Resizes the underlying data storage to the given size.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicBinary(std::basic_string_view< value_type > s) noexcept
Constructs a fixed-size string from a string view.
LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string_view< value_type > ToStringView() const noexcept
Retrieves a string view of the string.
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.