5#include "../SqlColumnTypeDefinitions.hpp"
8#include "StringInterface.hpp"
9#include "UnicodeConverter.hpp"
18constexpr size_t SqlMaxColumnSize = (std::numeric_limits<uint32_t>::max)();
25template <std::
size_t N,
typename T =
char>
29 static constexpr std::size_t DynamicCapacity = N;
31 using string_type = std::basic_string<T>;
32 using iterator = string_type::iterator;
33 using const_iterator = string_type::const_iterator;
34 using pointer_type = T*;
35 using const_pointer_type = T
const*;
38 template <std::
size_t SourceSize>
40 _value { text, SourceSize - 1 }
42 static_assert(SourceSize <= N + 1,
"RHS string size must not exceed target string's capacity.");
51 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDynamicString() noexcept = default;
52 LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicString(SqlDynamicString const&) noexcept = default;
53 LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicString& operator=(SqlDynamicString const&) noexcept = default;
54 LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicString(SqlDynamicString&&) noexcept = default;
55 LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicString& operator=(SqlDynamicString&&) noexcept = default;
56 LIGHTWEIGHT_FORCE_INLINE constexpr ~SqlDynamicString() noexcept = default;
59 LIGHTWEIGHT_FORCE_INLINE constexpr
SqlDynamicString(std::basic_string_view<T> s) noexcept:
65 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDynamicString(std::basic_string<T>
const& s)
noexcept:
71 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<T>
str() const noexcept
73 return std::basic_string_view<T> {
data(),
size() };
77 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE string_type
const&
value() const noexcept
83 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE string_type&
value() noexcept
89 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE T
const*
data() const noexcept
95 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE T*
data() noexcept
101 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE T
const*
c_str() const noexcept
103 return _value.c_str();
107 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::size_t
capacity() const noexcept
113 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::size_t
size() const noexcept
115 return _value.size();
119 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool empty() const noexcept
121 return _value.empty();
125 LIGHTWEIGHT_FORCE_INLINE
constexpr void clear() noexcept
130 LIGHTWEIGHT_FORCE_INLINE
void reserve(std::size_t
capacity)
135 LIGHTWEIGHT_FORCE_INLINE
constexpr void push_back(T c)
noexcept
140 LIGHTWEIGHT_FORCE_INLINE
constexpr void pop_back() noexcept
146 LIGHTWEIGHT_FORCE_INLINE
void setsize(std::size_t n)
noexcept
148 auto const newSize = (std::min) (n, N);
149 _value.resize(newSize);
153 LIGHTWEIGHT_FORCE_INLINE
constexpr void resize(std::size_t n)
noexcept
159 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<T>
substr(
160 std::size_t offset = 0, std::size_t count = (std::numeric_limits<std::size_t>::max)()) const noexcept
162 if (count != (std::numeric_limits<std::size_t>::max)())
164 return _value.substr(offset, count);
166 return _value.substr(offset);
169 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr string_type ToString() const noexcept
174 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr explicit operator std::basic_string<T>() const noexcept
180 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<T>
ToStringView() const noexcept
182 return { _value.data(), _value.size() };
185 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr T& operator[](std::size_t index)
noexcept
187 return _value[index];
190 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr T
const& operator[](std::size_t index)
const noexcept
192 return _value[index];
195 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr explicit operator std::basic_string_view<T>() const noexcept
200 template <std::
size_t OtherSize>
201 LIGHTWEIGHT_FORCE_INLINE std::weak_ordering operator<=>(SqlDynamicString<OtherSize, T>
const& other)
const noexcept
203 return _value <=> other._value;
206 template <std::
size_t OtherSize>
207 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator==(SqlDynamicString<OtherSize, T>
const& other)
const noexcept
209 return (*this <=> other) == std::weak_ordering::equivalent;
212 template <std::
size_t OtherSize>
213 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator!=(SqlDynamicString<OtherSize, T>
const& other)
const noexcept
215 return !(*
this == other);
218 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator==(std::basic_string_view<T> other)
const noexcept
220 return (
ToStringView() <=> other) == std::weak_ordering::equivalent;
223 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator!=(std::basic_string_view<T> other)
const noexcept
225 return !(*
this == other);
228 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr iterator begin() noexcept
230 return _value.begin();
233 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr iterator end() noexcept
238 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr const_iterator begin() const noexcept
240 return _value.begin();
243 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr const_iterator end() const noexcept
252static_assert(SqlStringInterface<SqlDynamicString<10>>);
254template <std::
size_t N,
typename CharT>
255struct detail::SqlViewHelper<SqlDynamicString<N, CharT>>
257 static LIGHTWEIGHT_FORCE_INLINE std::basic_string_view<CharT> View(SqlDynamicString<N, CharT>
const& str)
noexcept
259 return { str.data(), str.size() };
267 struct IsSqlDynamicStringImpl: std::false_type
271 template <std::
size_t N,
typename T>
272 struct IsSqlDynamicStringImpl<SqlDynamicString<N, T>>: std::true_type
276 template <std::
size_t N,
typename T>
277 struct IsSqlDynamicStringImpl<std::optional<SqlDynamicString<N, T>>>: std::true_type
281 template <
typename T>
282 consteval size_t SqlMaxNumberOfChars()
288 constexpr size_t numberOfBytes = 2147483647;
289 return numberOfBytes /
sizeof(T);
295constexpr bool IsSqlDynamicString = detail::IsSqlDynamicStringImpl<T>::value;
300template <std::
size_t N>
306template <std::
size_t N>
312template <std::
size_t N>
318template <std::
size_t N>
331template <std::
size_t N,
typename T>
332struct std::formatter<Lightweight::SqlDynamicString<N, T>>: std::formatter<std::string>
336 auto format(value_type
const& text, format_context& ctx)
const
338 if constexpr (Lightweight::detail::OneOf<T, wchar_t, char32_t, char16_t>)
339 return std::formatter<std::string>::format((
char const*)
Lightweight::ToUtf8(text.ToStringView()).c_str(), ctx);
341 return std::formatter<std::string>::format((
char const*) text.data(), ctx);
345template <std::
size_t N,
typename T>
346struct std::formatter<std::optional<Lightweight::SqlDynamicString<N, T>>>: std::formatter<string>
348 using value_type = std::optional<Lightweight::SqlDynamicString<N, T>>;
350 auto format(value_type
const& text, format_context& ctx)
const
352 if (!text.has_value())
353 return std::formatter<std::string>::format(
"nullopt", ctx);
354 return std::formatter<std::string>::format(std::format(
"{}", text.value()), ctx);
361template <std::
size_t N,
typename T>
362struct SqlBasicStringOperations<SqlDynamicString<N, T>>
365 using ValueType = SqlDynamicString<N, CharType>;
367 static constexpr SqlColumnTypeDefinition ColumnType = []()
constexpr {
368 if constexpr (std::same_as<CharType, char>)
369 return SqlColumnTypeDefinitions::Varchar { N };
371 return SqlColumnTypeDefinitions::NVarchar { N };
374 static CharType
const* Data(ValueType
const* str)
noexcept
379 static CharType* Data(ValueType* str)
noexcept
384 static SQLULEN Size(ValueType
const* str)
noexcept
389 static void Clear(ValueType* str)
noexcept
391 str->value().clear();
394 static void Reserve(ValueType* str,
size_t capacity)
noexcept
396 str->value().resize((std::min) (N, capacity));
399 static void Resize(ValueType* str, SQLLEN indicator)
noexcept
401 str->value().resize(
static_cast<size_t>(indicator));
LIGHTWEIGHT_FORCE_INLINE std::size_t size() const noexcept
Retrieves the string's size.
LIGHTWEIGHT_FORCE_INLINE std::size_t capacity() const noexcept
Retrieves the string's capacity.
LIGHTWEIGHT_FORCE_INLINE T const * c_str() const noexcept
Retrieves the string's inner value (as T const*).
LIGHTWEIGHT_FORCE_INLINE T const * data() const noexcept
Retrieves the string's inner value (as T const*).
LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string_view< T > str() const noexcept
Returns a string view of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr bool empty() const noexcept
Tests if the string is empty.
LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string_view< T > ToStringView() const noexcept
Retrieves a string view of the string.
constexpr LIGHTWEIGHT_FORCE_INLINE SqlDynamicString(T const (&text)[SourceSize])
Constructs a fixed-size string from a string literal.
LIGHTWEIGHT_FORCE_INLINE T * data() noexcept
Retrieves the string's inner value (as T*).
LIGHTWEIGHT_FORCE_INLINE string_type & value() noexcept
Retrieves the string's inner value (std::basic_string<T>).
LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string_view< T > substr(std::size_t offset=0, std::size_t count=(std::numeric_limits< std::size_t >::max)()) const noexcept
Retrieves a string view of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicString(std::basic_string< T > const &s) noexcept
Constructs a fixed-size string from a string.
LIGHTWEIGHT_FORCE_INLINE string_type const & value() const noexcept
Retrieves the string's inner value (std::basic_string<T>).
LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicString(T const *s, T const *e) noexcept
Constructs a fixed-size string from a string pointer and end pointer.
LIGHTWEIGHT_FORCE_INLINE constexpr void clear() noexcept
Clears the string.
LIGHTWEIGHT_FORCE_INLINE constexpr void resize(std::size_t n) noexcept
Resizes the string.
LIGHTWEIGHT_API std::u8string ToUtf8(std::u32string_view u32InputString)