5#include "../SqlColumnTypeDefinitions.hpp"
6#include "BasicStringBinder.hpp"
21template <std::
size_t N>
24 using BaseType = std::vector<uint8_t>;
28 using value_type = uint8_t;
29 static constexpr auto ColumnType = SqlColumnTypeDefinitions::VarBinary { N };
42 template <std::
size_t SourceSize>
43 constexpr LIGHTWEIGHT_FORCE_INLINE
SqlDynamicBinary(value_type const (&text)[SourceSize]):
44 _base { text, text + SourceSize }
46 static_assert(SourceSize <= N + 1,
"RHS string size must not exceed target string's capacity.");
50 template <std::
size_t SourceSize>
54 static_assert(SourceSize <= N + 1,
"RHS string size must not exceed target string's capacity.");
58 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDynamicBinary(value_type
const* s, value_type
const* e)
noexcept:
62#if !defined(LIGHTWEIGHT_CXX26_REFLECTION)
64 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDynamicBinary(std::basic_string_view<value_type> s)
noexcept:
65 _base {
static_cast<uint8_t const*
>(s.data()),
static_cast<uint8_t const*
>(s.data() + s.size()) }
70 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<value_type>
ToStringView() const noexcept
72 return { _base.data(), _base.size() };
79 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::size_t
size() const noexcept
85 void LIGHTWEIGHT_FORCE_INLINE
resize(std::size_t newSize)
87 _base.resize(newSize);
91 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool empty() const noexcept
97 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr decltype(
auto)
data(
this auto&& self)
noexcept
99 return self._base.data();
103 LIGHTWEIGHT_FORCE_INLINE
void clear() noexcept
109 mutable SQLLEN _indicator = 0;
118 struct IsSqlDynamicBinaryImpl: std::false_type
123 struct IsSqlDynamicBinaryImpl<SqlDynamicBinary<T>>: std::true_type
128 struct IsSqlDynamicBinaryImpl<std::optional<SqlDynamicBinary<T>>>: std::true_type
135constexpr bool IsSqlDynamicBinary = detail::IsSqlDynamicBinaryImpl<T>::value;
139template <std::
size_t N>
140struct std::formatter<Lightweight::SqlDynamicBinary<N>>: std::formatter<std::string>
144 std::string humanReadableText;
145 for (
auto byte: text)
147 if (
byte >= 0x20 &&
byte <= 0x7E)
148 humanReadableText +=
static_cast<char>(byte);
150 humanReadableText += std::format(
"\\x{:02X}",
byte);
152 return std::formatter<std::string>::format(humanReadableText.data(), ctx);
159template <std::
size_t N>
160struct SqlDataBinder<SqlDynamicBinary<N>>
162 using ValueType = SqlDynamicBinary<N>;
164 static constexpr auto ColumnType = SqlColumnTypeDefinitions::VarBinary { N };
166 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
168 ValueType
const& value,
169 SqlDataBinderCallback& )
noexcept
171 value._indicator =
static_cast<SQLLEN
>(value.size());
172 return SQLBindParameter(stmt,
179 (SQLPOINTER) value.data(),
184 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(
185 SQLHSTMT stmt, SQLUSMALLINT column, ValueType* result, SQLLEN* indicator, SqlDataBinderCallback& cb)
noexcept
190 cb.PlanPostProcessOutputColumn([stmt, column, result, indicator]() {
191 if (*indicator == SQL_NULL_DATA)
194 else if (*indicator == SQL_NO_TOTAL)
196 result->resize(result->size() - 1);
197 else if (*indicator <=
static_cast<SQLLEN
>(result->size()))
198 result->resize(*indicator);
204 auto const totalCharsRequired = *indicator;
205 result->resize(totalCharsRequired + 1);
206 auto const sqlResult =
207 SQLGetData(stmt, column, SQL_C_BINARY, (SQLPOINTER) result->data(), totalCharsRequired + 1, indicator);
209 assert(SQL_SUCCEEDED(sqlResult));
210 assert(*indicator == totalCharsRequired);
211 result->resize(totalCharsRequired);
215 return SQLBindCol(stmt, column, SQL_C_BINARY, (SQLPOINTER) result->data(), 255, indicator);
218 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt,
222 SqlDataBinderCallback
const& )
noexcept
227 return detail::GetRawColumnArrayData<SQL_C_BINARY>(stmt, column, result, indicator);
230 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(ValueType
const& value)
232 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.