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>
48 template <std::
size_t SourceSize>
50 _value { text, SourceSize - 1 }
52 static_assert(SourceSize <= N + 1,
"RHS string size must not exceed target string's capacity.");
80 LIGHTWEIGHT_FORCE_INLINE constexpr
SqlDynamicString(std::basic_string_view<T> s) noexcept:
86 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDynamicString(std::basic_string<T>
const& s)
noexcept:
92 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<T>
str() const noexcept
94 return std::basic_string_view<T> {
data(),
size() };
110 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE T
const*
data() const noexcept
112 return _value.data();
116 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE T*
data() noexcept
118 return _value.data();
122 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE T
const*
c_str() const noexcept
124 return _value.c_str();
128 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::size_t
capacity() const noexcept
134 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::size_t
size() const noexcept
136 return _value.size();
140 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool empty() const noexcept
142 return _value.empty();
146 LIGHTWEIGHT_FORCE_INLINE
constexpr void clear() noexcept
158 LIGHTWEIGHT_FORCE_INLINE
constexpr void push_back(T c)
noexcept
164 LIGHTWEIGHT_FORCE_INLINE
constexpr void pop_back() noexcept
173 LIGHTWEIGHT_FORCE_INLINE
void setsize(std::size_t n)
noexcept
175 auto const newSize = (std::min) (n, N);
176 _value.resize(newSize);
180 LIGHTWEIGHT_FORCE_INLINE
constexpr void resize(std::size_t n)
noexcept
186 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<T>
substr(
187 std::size_t offset = 0, std::size_t count = (std::numeric_limits<std::size_t>::max)()) const noexcept
189 if (count != (std::numeric_limits<std::size_t>::max)())
191 return _value.substr(offset, count);
193 return _value.substr(offset);
203 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr explicit operator std::basic_string<T>() const noexcept
209 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<T>
ToStringView() const noexcept
211 return { _value.data(), _value.size() };
215 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr T&
operator[](std::size_t index)
noexcept
217 return _value[index];
221 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr T
const&
operator[](std::size_t index)
const noexcept
223 return _value[index];
227 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr explicit operator std::basic_string_view<T>() const noexcept
233 template <std::
size_t OtherSize>
236 return _value <=> other._value;
240 template <std::
size_t OtherSize>
243 return (*this <=> other) == std::weak_ordering::equivalent;
247 template <std::
size_t OtherSize>
250 return !(*
this == other);
254 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator==(std::basic_string_view<T> other)
const noexcept
256 return (
ToStringView() <=> other) == std::weak_ordering::equivalent;
260 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator!=(std::basic_string_view<T> other)
const noexcept
262 return !(*
this == other);
268 return _value.begin();
272 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr iterator end() noexcept
280 return _value.begin();
293static_assert(SqlStringInterface<SqlDynamicString<10>>);
295template <std::
size_t N,
typename CharT>
296struct detail::SqlViewHelper<SqlDynamicString<N, CharT>>
298 static LIGHTWEIGHT_FORCE_INLINE std::basic_string_view<CharT> View(SqlDynamicString<N, CharT>
const& str)
noexcept
300 return { str.data(), str.size() };
308 struct IsSqlDynamicStringImpl: std::false_type
312 template <std::
size_t N,
typename T>
313 struct IsSqlDynamicStringImpl<SqlDynamicString<N, T>>: std::true_type
317 template <std::
size_t N,
typename T>
318 struct IsSqlDynamicStringImpl<std::optional<SqlDynamicString<N, T>>>: std::true_type
322 template <
typename T>
323 consteval size_t SqlMaxNumberOfChars()
329 constexpr size_t numberOfBytes = 2147483647;
330 return numberOfBytes /
sizeof(T);
336constexpr bool IsSqlDynamicString = detail::IsSqlDynamicStringImpl<T>::value;
341template <std::
size_t N>
347template <std::
size_t N>
353template <std::
size_t N>
359template <std::
size_t N>
372template <std::
size_t N,
typename T>
373struct std::formatter<Lightweight::SqlDynamicString<N, T>>: std::formatter<std::string>
377 auto format(value_type
const& text, format_context& ctx)
const
379 if constexpr (Lightweight::detail::OneOf<T, wchar_t, char32_t, char16_t>)
380 return std::formatter<std::string>::format((
char const*)
Lightweight::ToUtf8(text.ToStringView()).c_str(), ctx);
382 return std::formatter<std::string>::format((
char const*) text.data(), ctx);
386template <std::
size_t N,
typename T>
387struct std::formatter<std::optional<Lightweight::SqlDynamicString<N, T>>>: std::formatter<string>
389 using value_type = std::optional<Lightweight::SqlDynamicString<N, T>>;
391 auto format(value_type
const& text, format_context& ctx)
const
393 if (!text.has_value())
394 return std::formatter<std::string>::format(
"nullopt", ctx);
395 return std::formatter<std::string>::format(std::format(
"{}", text.value()), ctx);
402template <std::
size_t N,
typename T>
403struct SqlBasicStringOperations<SqlDynamicString<N, T>>
406 using ValueType = SqlDynamicString<N, CharType>;
408 static constexpr SqlColumnTypeDefinition ColumnType = []()
constexpr {
409 if constexpr (std::same_as<CharType, char>)
410 return SqlColumnTypeDefinitions::Varchar { N };
412 return SqlColumnTypeDefinitions::NVarchar { N };
415 static CharType
const* Data(ValueType
const* str)
noexcept
420 static CharType* Data(ValueType* str)
noexcept
425 static SQLULEN Size(ValueType
const* str)
noexcept
430 static void Clear(ValueType* str)
noexcept
432 str->value().clear();
435 static void Reserve(ValueType* str,
size_t capacity)
noexcept
437 str->value().resize((std::min) (N, capacity));
440 static void Resize(ValueType* str, SQLLEN indicator)
noexcept
442 str->value().resize(
static_cast<size_t>(indicator));
LIGHTWEIGHT_FORCE_INLINE void setsize(std::size_t n) noexcept
static constexpr std::size_t DynamicCapacity
constexpr variable with the capacity of the string.
LIGHTWEIGHT_FORCE_INLINE std::weak_ordering operator<=>(SqlDynamicString< OtherSize, T > const &other) const noexcept
Three-way comparison operator for comparing with another SqlDynamicString of possibly different capac...
T value_type
The element type of the string.
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 constexpr const_iterator end() const noexcept
Retrieves a const iterator to the end of the string.
string_type::iterator iterator
Iterator type for the string.
string_type::const_iterator const_iterator
Const iterator type for the string.
LIGHTWEIGHT_FORCE_INLINE constexpr T const & operator[](std::size_t index) const noexcept
Const qulified element access operator.
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 void reserve(std::size_t capacity)
Reserves capacity for at least the given number of characters.
LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string_view< T > str() const noexcept
Returns a string view of the string.
T * pointer_type
Pointer type for the string.
LIGHTWEIGHT_FORCE_INLINE constexpr bool empty() const noexcept
Tests if the string is empty.
T const * const_pointer_type
Const pointer type for the string.
LIGHTWEIGHT_FORCE_INLINE constexpr void pop_back() noexcept
Removes the last character from the string.
LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string_view< T > ToStringView() const noexcept
Retrieves a string view of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr iterator end() noexcept
Retrieves an iterator to the end of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr bool operator!=(SqlDynamicString< OtherSize, T > const &other) const noexcept
Inequality comparison operator for comparing with another SqlDynamicString of possibly different capa...
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 constexpr bool operator==(std::basic_string_view< T > other) const noexcept
Equality comparison operator for comparing with a string view.
LIGHTWEIGHT_FORCE_INLINE string_type & value() noexcept
Retrieves the string's inner value (std::basic_string<T>).
LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicString() noexcept=default
Defaulted default constructor.
LIGHTWEIGHT_FORCE_INLINE constexpr string_type ToString() const noexcept
Retrieves the string as a string_type.
std::basic_string< T > string_type
String type used for the internal storage of the string.
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 bool operator!=(std::basic_string_view< T > other) const noexcept
Inequality comparison operator for comparing with a string view.
LIGHTWEIGHT_FORCE_INLINE constexpr iterator begin() noexcept
Retrieves an iterator to the beginning of the string.
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 push_back(T c) noexcept
Appends a character to the end of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr void clear() noexcept
Clears the string.
LIGHTWEIGHT_FORCE_INLINE constexpr const_iterator begin() const noexcept
Retrieves a const iterator to the beginning of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr void resize(std::size_t n) noexcept
Resizes the string.
LIGHTWEIGHT_FORCE_INLINE constexpr T & operator[](std::size_t index) noexcept
Element access operator.
LIGHTWEIGHT_FORCE_INLINE constexpr bool operator==(SqlDynamicString< OtherSize, T > const &other) const noexcept
Equality comparison operator for comparing with another SqlDynamicString of possibly different capaci...
LIGHTWEIGHT_API std::u8string ToUtf8(std::u32string_view u32InputString)