5#include "../SqlColumnTypeDefinitions.hpp"
8#include "UnicodeConverter.hpp"
17constexpr size_t SqlMaxColumnSize = (std::numeric_limits<uint32_t>::max)();
24template <std::
size_t N,
typename T =
char>
28 static constexpr std::size_t DynamicCapacity = N;
30 using string_type = std::basic_string<T>;
33 template <std::
size_t SourceSize>
35 _value { text, SourceSize - 1 }
37 static_assert(SourceSize <= N + 1,
"RHS string size must not exceed target string's capacity.");
46 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDynamicString() noexcept = default;
47 LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicString(SqlDynamicString const&) noexcept = default;
48 LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicString& operator=(SqlDynamicString const&) noexcept = default;
49 LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicString(SqlDynamicString&&) noexcept = default;
50 LIGHTWEIGHT_FORCE_INLINE constexpr SqlDynamicString& operator=(SqlDynamicString&&) noexcept = default;
51 LIGHTWEIGHT_FORCE_INLINE constexpr ~SqlDynamicString() noexcept = default;
54 LIGHTWEIGHT_FORCE_INLINE constexpr
SqlDynamicString(std::basic_string_view<T> s) noexcept:
60 LIGHTWEIGHT_FORCE_INLINE
constexpr SqlDynamicString(std::basic_string<T>
const& s)
noexcept:
66 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<T>
str() const noexcept
68 return std::basic_string_view<T> {
data(),
size() };
72 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE string_type
const&
value() const noexcept
78 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE string_type&
value() noexcept
84 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE T
const*
data() const noexcept
90 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE T*
data() noexcept
96 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE T
const*
c_str() const noexcept
98 return _value.c_str();
102 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::size_t
capacity() const noexcept
108 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::size_t
size() const noexcept
110 return _value.size();
114 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr bool empty() const noexcept
116 return _value.empty();
120 LIGHTWEIGHT_FORCE_INLINE
constexpr void clear() noexcept
126 LIGHTWEIGHT_FORCE_INLINE
constexpr void resize(std::size_t n)
noexcept
132 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr std::basic_string_view<T>
ToStringView() const noexcept
134 return { _value.data(), _value.size() };
137 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr T& operator[](std::size_t index)
noexcept
139 return _value[index];
142 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr T
const& operator[](std::size_t index)
const noexcept
144 return _value[index];
147 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
constexpr explicit operator std::basic_string_view<T>() const noexcept
152 template <std::
size_t OtherSize>
153 LIGHTWEIGHT_FORCE_INLINE std::weak_ordering operator<=>(SqlDynamicString<OtherSize, T>
const& other)
const noexcept
155 return _value <=> other._value;
158 template <std::
size_t OtherSize>
159 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator==(SqlDynamicString<OtherSize, T>
const& other)
const noexcept
161 return (*this <=> other) == std::weak_ordering::equivalent;
164 template <std::
size_t OtherSize>
165 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator!=(SqlDynamicString<OtherSize, T>
const& other)
const noexcept
167 return !(*
this == other);
170 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator==(std::basic_string_view<T> other)
const noexcept
172 return (
ToStringView() <=> other) == std::weak_ordering::equivalent;
175 LIGHTWEIGHT_FORCE_INLINE
constexpr bool operator!=(std::basic_string_view<T> other)
const noexcept
177 return !(*
this == other);
184template <std::
size_t N,
typename CharT>
185struct detail::SqlViewHelper<SqlDynamicString<N, CharT>>
187 static LIGHTWEIGHT_FORCE_INLINE std::basic_string_view<CharT> View(SqlDynamicString<N, CharT>
const& str)
noexcept
189 return { str.data(), str.size() };
197 struct IsSqlDynamicStringImpl: std::false_type
201 template <std::
size_t N,
typename T>
202 struct IsSqlDynamicStringImpl<SqlDynamicString<N, T>>: std::true_type
206 template <std::
size_t N,
typename T>
207 struct IsSqlDynamicStringImpl<std::optional<SqlDynamicString<N, T>>>: std::true_type
214constexpr bool IsSqlDynamicString = detail::IsSqlDynamicStringImpl<T>::value;
219template <std::
size_t N>
225template <std::
size_t N>
231template <std::
size_t N>
237template <std::
size_t N>
242template <std::
size_t N,
typename T>
243struct std::formatter<Lightweight::SqlDynamicString<N, T>>: std::formatter<std::string>
247 auto format(value_type
const& text, format_context& ctx)
const
249 if constexpr (Lightweight::detail::OneOf<T, wchar_t, char32_t, char16_t>)
250 return std::formatter<std::string>::format((
char const*)
Lightweight::ToUtf8(text.ToStringView()).c_str(), ctx);
252 return std::formatter<std::string>::format((
char const*) text.data(), ctx);
256template <std::
size_t N,
typename T>
257struct std::formatter<std::optional<Lightweight::SqlDynamicString<N, T>>>: std::formatter<string>
259 using value_type = std::optional<Lightweight::SqlDynamicString<N, T>>;
261 auto format(value_type
const& text, format_context& ctx)
const
263 if (!text.has_value())
264 return std::formatter<std::string>::format(
"nullopt", ctx);
265 return std::formatter<std::string>::format(std::format(
"{}", text.value()), ctx);
272template <std::
size_t N,
typename T>
273struct SqlBasicStringOperations<SqlDynamicString<N, T>>
276 using ValueType = SqlDynamicString<N, CharType>;
278 static constexpr SqlColumnTypeDefinition ColumnType = []()
constexpr {
279 if constexpr (std::same_as<CharType, char>)
280 return SqlColumnTypeDefinitions::Varchar { N };
282 return SqlColumnTypeDefinitions::NVarchar { N };
285 static CharType
const* Data(ValueType
const* str)
noexcept
290 static CharType* Data(ValueType* str)
noexcept
295 static SQLULEN Size(ValueType
const* str)
noexcept
300 static void Clear(ValueType* str)
noexcept
302 str->value().clear();
305 static void Reserve(ValueType* str,
size_t capacity)
noexcept
307 str->value().resize((std::min) (N, capacity));
310 static void Resize(ValueType* str, SQLLEN indicator)
noexcept
312 str->value().resize(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 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)