5#include "../SqlColumnTypeDefinitions.hpp"
6#include "BasicStringBinder.hpp"
21template <std::
size_t N>
24 using BaseType = std::vector<uint8_t>;
30 static constexpr auto ColumnType = SqlColumnTypeDefinitions::VarBinary { N };
54 template <std::
size_t SourceSize>
56 _base { text, text + SourceSize }
58 static_assert(SourceSize <= N + 1,
"RHS string size must not exceed target string's capacity.");
62 template <std::
size_t SourceSize>
66 static_assert(SourceSize <= N + 1,
"RHS string size must not exceed target string's capacity.");
74#if !defined(LIGHTWEIGHT_CXX26_REFLECTION)
76 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDynamicBinary(std::basic_string_view<value_type> s)
noexcept:
77 _base {
static_cast<uint8_t const*
>(s.data()),
static_cast<uint8_t const*
>(s.data() + s.size()) }
82 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<value_type>
ToStringView() const noexcept
84 return { _base.data(), _base.size() };
92 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::size_t
size() const noexcept
98 void LIGHTWEIGHT_FORCE_INLINE
resize(std::size_t newSize)
100 _base.resize(newSize);
104 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool empty() const noexcept
106 return _base.empty();
110 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr decltype(
auto)
data(
this auto&& self)
noexcept
112 self.EnsureNonNullBuffer();
113 return self._base.data();
117 LIGHTWEIGHT_FORCE_INLINE
void clear() noexcept
123 void EnsureNonNullBuffer()
const
125 if (_base.data() ==
nullptr)
133 mutable SQLLEN _indicator = 0;
142 struct IsSqlDynamicBinaryImpl: std::false_type
147 struct IsSqlDynamicBinaryImpl<SqlDynamicBinary<T>>: std::true_type
152 struct IsSqlDynamicBinaryImpl<std::optional<SqlDynamicBinary<T>>>: std::true_type
159constexpr bool IsSqlDynamicBinary = detail::IsSqlDynamicBinaryImpl<T>::value;
163template <std::
size_t N>
164struct std::formatter<Lightweight::SqlDynamicBinary<N>>: std::formatter<std::string>
168 std::string humanReadableText;
169 for (
auto byte: text)
171 if (
byte >= 0x20 &&
byte <= 0x7E)
172 humanReadableText +=
static_cast<char>(byte);
174 humanReadableText += std::format(
"\\x{:02X}",
byte);
176 return std::formatter<std::string>::format(humanReadableText.data(), ctx);
183template <std::
size_t N>
184struct SqlDataBinder<SqlDynamicBinary<N>>
186 using ValueType = SqlDynamicBinary<N>;
188 static constexpr auto ColumnType = SqlColumnTypeDefinitions::VarBinary { N };
190 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
192 ValueType
const& value,
193 SqlDataBinderCallback& )
noexcept
195 value._indicator =
static_cast<SQLLEN
>(value.size());
197 auto const sqlType =
static_cast<SQLSMALLINT
>(value.size() > 8000 ? SQL_LONGVARBINARY : SQL_VARBINARY);
198 auto const bufferSize =
static_cast<SQLULEN
>(value.size());
199 auto*
const ptr = (SQLPOINTER) value.data();
201 return SQLBindParameter(
202 stmt, column, SQL_PARAM_INPUT, SQL_C_BINARY, sqlType, bufferSize, 0, ptr, 0, &value._indicator);
205 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(
206 SQLHSTMT stmt, SQLUSMALLINT column, ValueType* result, SQLLEN* indicator, SqlDataBinderCallback& cb)
noexcept
211 cb.PlanPostProcessOutputColumn([stmt, column, result, indicator]() {
212 if (*indicator == SQL_NULL_DATA)
215 else if (*indicator == SQL_NO_TOTAL)
217 result->resize(result->size() - 1);
218 else if (*indicator <=
static_cast<SQLLEN
>(result->size()))
219 result->resize(
static_cast<size_t>(*indicator));
225 auto const totalCharsRequired = *indicator;
226 result->resize(
static_cast<size_t>(totalCharsRequired + 1));
227 auto const sqlResult =
228 SQLGetData(stmt, column, SQL_C_BINARY, (SQLPOINTER) result->data(), totalCharsRequired + 1, indicator);
230 assert(SQL_SUCCEEDED(sqlResult));
231 assert(*indicator == totalCharsRequired);
232 result->resize(
static_cast<size_t>(totalCharsRequired));
236 return SQLBindCol(stmt, column, SQL_C_BINARY, (SQLPOINTER) result->data(), 255, indicator);
239 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt,
243 SqlDataBinderCallback
const& )
noexcept
248 return detail::GetRawColumnArrayData<SQL_C_BINARY>(stmt, column, result, indicator);
251 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(ValueType
const& value)
253 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 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.
uint8_t value_type
The element type of the binary data.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicBinary() noexcept=default
Default constructor, creates an empty object.
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.
constexpr auto operator<=>(SqlDynamicBinary< N > const &) const noexcept=default
Defaulted Three-way comparison operator.
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.