5#include "../SqlColumnTypeDefinitions.hpp"
6#include "BasicStringBinder.hpp"
18template <std::
size_t N>
21 using BaseType = std::vector<uint8_t>;
25 using value_type = uint8_t;
26 static constexpr auto ColumnType = SqlColumnTypeDefinitions::VarBinary { N };
39 template <std::
size_t SourceSize>
40 constexpr LIGHTWEIGHT_FORCE_INLINE
SqlDynamicBinary(value_type const (&text)[SourceSize]):
41 _base { text, text + SourceSize }
43 static_assert(SourceSize <= N + 1,
"RHS string size must not exceed target string's capacity.");
47 template <std::
size_t SourceSize>
51 static_assert(SourceSize <= N + 1,
"RHS string size must not exceed target string's capacity.");
55 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDynamicBinary(value_type
const* s, value_type
const* e)
noexcept:
61 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDynamicBinary(std::basic_string_view<value_type> s)
noexcept:
62 _base {
static_cast<uint8_t const*
>(s.data()),
static_cast<uint8_t const*
>(s.data() + s.size()) }
67 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<value_type>
ToStringView() const noexcept
69 return { _base.data(), _base.size() };
76 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::size_t
size() const noexcept
82 void LIGHTWEIGHT_FORCE_INLINE
resize(std::size_t newSize)
84 _base.resize(newSize);
88 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool empty() const noexcept
95 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr decltype(
auto)
data(
this auto&& self)
noexcept
97 return self._base.data();
101 LIGHTWEIGHT_FORCE_INLINE
void clear() noexcept
107 mutable SQLLEN _indicator = 0;
116struct IsSqlDynamicBinaryImpl: std::false_type
126struct IsSqlDynamicBinaryImpl<std::optional<SqlDynamicBinary<T>>>: std::true_type
133constexpr bool IsSqlDynamicBinary = detail::IsSqlDynamicBinaryImpl<T>::value;
135template <std::
size_t N>
140 std::string humanReadableText;
141 for (
auto byte: text)
143 if (
byte >= 0x20 &&
byte <= 0x7E)
144 humanReadableText +=
static_cast<char>(byte);
146 humanReadableText += std::format(
"\\x{:02X}",
byte);
148 return std::formatter<std::string>::format(humanReadableText.data(), ctx);
152template <std::
size_t N>
157 static constexpr auto ColumnType = SqlColumnTypeDefinitions::VarBinary { N };
159 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
161 ValueType
const& value,
164 value._indicator =
static_cast<SQLLEN
>(value.size());
165 return SQLBindParameter(stmt,
172 (SQLPOINTER) value.data(),
177 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(
178 SQLHSTMT stmt, SQLUSMALLINT column, ValueType* result, SQLLEN* indicator,
SqlDataBinderCallback& cb)
noexcept
183 cb.PlanPostProcessOutputColumn([stmt, column, result, indicator]() {
184 if (*indicator == SQL_NULL_DATA)
187 else if (*indicator == SQL_NO_TOTAL)
189 result->resize(result->size() - 1);
190 else if (*indicator <=
static_cast<SQLLEN
>(result->size()))
191 result->resize(*indicator);
197 auto const totalCharsRequired = *indicator;
198 result->resize(totalCharsRequired + 1);
199 auto const sqlResult =
200 SQLGetData(stmt, column, SQL_C_BINARY, (SQLPOINTER) result->data(), totalCharsRequired + 1, indicator);
202 assert(SQL_SUCCEEDED(sqlResult));
203 assert(*indicator == totalCharsRequired);
204 result->resize(totalCharsRequired);
208 return SQLBindCol(stmt, column, SQL_C_BINARY, (SQLPOINTER) result->data(), 255, indicator);
211 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt,
220 return detail::GetArrayData<SQL_C_BINARY>(stmt, column, result, indicator);
223 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(ValueType
const& value)
225 return std::format(
"SqlBinary<{}>(size={})", N, value.size());
LIGHTWEIGHT_FORCE_INLINE constexpr decltype(auto) data(this auto &&self) noexcept
Retrieves the pointer to the string data.
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.
LIGHTWEIGHT_FORCE_INLINE void clear() noexcept
Clears the storad data.
static constexpr std::size_t DynamicCapacity
The maximum size of the underlying data storage.
LIGHTWEIGHT_FORCE_INLINE constexpr bool empty() const noexcept
Tests if the stored data is empty.
constexpr LIGHTWEIGHT_FORCE_INLINE SqlDynamicBinary(std::initializer_list< value_type > data)
Constructs the object from an initializer list of bytes.
void LIGHTWEIGHT_FORCE_INLINE resize(std::size_t newSize)
Resizes the underlying data storage to the given 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 std::size_t size() const noexcept
Retrieves the size of the string.