Lightweight 0.20250904.0
Loading...
Searching...
No Matches
SqlFixedString.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "../SqlColumnTypeDefinitions.hpp"
6#include "Core.hpp"
7#include "UnicodeConverter.hpp"
8
9#include <format>
10#include <ranges>
11#include <stdexcept>
12#include <utility>
13
14namespace Lightweight
15{
16
17enum class SqlFixedStringMode : uint8_t
18{
19 FIXED_SIZE,
20 FIXED_SIZE_RIGHT_TRIMMED,
21 VARIABLE_SIZE,
22};
23
24/// SQL fixed-capacity string that mimmicks standard library string/string_view with a fixed-size underlying
25/// buffer.
26///
27/// The underlying storage will not be guaranteed to be `\0`-terminated unless
28/// a call to mutable/const c_str() has been performed.
29///
30/// @ingroup DataTypes
31template <std::size_t N, typename T = char, SqlFixedStringMode Mode = SqlFixedStringMode::FIXED_SIZE>
33{
34 private:
35 T _data[N + 1] {};
36 std::size_t _size = 0;
37
38 public:
39 using value_type = T;
40 using iterator = T*;
41 using const_iterator = T const*;
42 using pointer_type = T*;
43 using const_pointer_type = T const*;
44
45 static constexpr std::size_t Capacity = N;
46 static constexpr SqlFixedStringMode PostRetrieveOperation = Mode;
47
48 /// Constructs a fixed-size string from a string literal.
49 template <std::size_t SourceSize>
50 constexpr LIGHTWEIGHT_FORCE_INLINE SqlFixedString(T const (&text)[SourceSize]):
51 _size { SourceSize - 1 }
52 {
53 static_assert(SourceSize <= N + 1, "RHS string size must not exceed target string's capacity.");
54 std::copy_n(text, SourceSize, _data);
55 }
56
57 LIGHTWEIGHT_FORCE_INLINE constexpr SqlFixedString() noexcept = default;
58 LIGHTWEIGHT_FORCE_INLINE constexpr SqlFixedString(SqlFixedString const&) noexcept = default;
59 LIGHTWEIGHT_FORCE_INLINE constexpr SqlFixedString& operator=(SqlFixedString const&) noexcept = default;
60 LIGHTWEIGHT_FORCE_INLINE constexpr SqlFixedString(SqlFixedString&&) noexcept = default;
61 LIGHTWEIGHT_FORCE_INLINE constexpr SqlFixedString& operator=(SqlFixedString&&) noexcept = default;
62 LIGHTWEIGHT_FORCE_INLINE constexpr ~SqlFixedString() noexcept = default;
63
64 /// Constructs a fixed-size string from a string view.
65 LIGHTWEIGHT_FORCE_INLINE constexpr SqlFixedString(std::basic_string_view<T> s) noexcept:
66 _size { (std::min) (N, s.size()) }
67 {
68 std::copy_n(s.data(), _size, _data); // NOLINT(bugprone-suspicious-stringview-data-usage)
69 }
70
71 /// Constructs a fixed-size string from a string.
72 LIGHTWEIGHT_FORCE_INLINE constexpr SqlFixedString(std::basic_string<T> const& s) noexcept:
73 _size { (std::min) (N, s.size()) }
74 {
75 std::copy_n(s.data(), _size, _data);
76 }
77
78 /// Constructs a fixed-size string from a string pointer and length.
79 LIGHTWEIGHT_FORCE_INLINE constexpr SqlFixedString(T const* s, std::size_t len) noexcept:
80 _size { (std::min) (N, len) }
81 {
82 std::copy_n(s, _size, _data);
83 }
84
85 /// Constructs a fixed-size string from a string pointer and end pointer.
86 LIGHTWEIGHT_FORCE_INLINE constexpr SqlFixedString(T const* s, T const* e) noexcept:
87 _size { (std::min) (N, static_cast<std::size_t>(e - s)) }
88 {
89 std::copy(s, e, _data);
90 }
91
92 LIGHTWEIGHT_FORCE_INLINE void reserve(std::size_t capacity)
93 {
94 if (capacity > N)
95 throw std::length_error(std::format("SqlFixedString: capacity {} exceeds maximum capacity {}", capacity, N));
96 }
97
98 /// Tests if the string is empty.
99 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr bool empty() const noexcept
100 {
101 return _size == 0;
102 }
103
104 /// Returns the size of the string.
105 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr std::size_t size() const noexcept
106 {
107 return _size;
108 }
109
110 // NOLINTNEXTLINE(readability-identifier-naming)
111 LIGHTWEIGHT_FORCE_INLINE /*TODO constexpr*/ void setsize(std::size_t n) noexcept
112 {
113 auto const newSize = (std::min) (n, N);
114 _size = newSize;
115 _data[newSize] = '\0';
116 }
117
118 /// Resizes the string.
119 ///
120 /// This sets the size of the string to `n`. If `n` is greater than the current size,
121 /// capped at the maximum capacity.
122 LIGHTWEIGHT_FORCE_INLINE constexpr void resize(std::size_t n) noexcept
123 {
124 auto const newSize = (std::min) (n, N);
125 _size = newSize;
126 _data[newSize] = '\0';
127 }
128
129 /// Returns the maximum capacity of the string.
130 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr std::size_t capacity() const noexcept
131 {
132 return N;
133 }
134
135 /// Clears the string.
136 LIGHTWEIGHT_FORCE_INLINE constexpr void clear() noexcept
137 {
138 _size = 0;
139 }
140
141 /// Assigns a string literal to the string.
142 template <std::size_t SourceSize>
143 LIGHTWEIGHT_FORCE_INLINE constexpr void assign(T const (&source)[SourceSize]) noexcept
144 {
145 static_assert(SourceSize <= N + 1, "Source string must not overflow the target string's capacity.");
146 _size = SourceSize - 1;
147 std::copy_n(source, SourceSize, _data);
148 }
149
150 /// Assigns a string view to the string.
151 LIGHTWEIGHT_FORCE_INLINE constexpr void assign(std::basic_string_view<T> s) noexcept
152 {
153 _size = (std::min) (N, s.size());
154 std::copy_n(s.data(), _size, _data); // NOLINT(bugprone-suspicious-stringview-data-usage)
155 }
156
157 /// Appends a character to the string.
158 LIGHTWEIGHT_FORCE_INLINE constexpr void push_back(T c) noexcept
159 {
160 if (_size < N)
161 {
162 _data[_size] = c;
163 ++_size;
164 }
165 }
166
167 /// Removes the last character from the string.
168 LIGHTWEIGHT_FORCE_INLINE constexpr void pop_back() noexcept
169 {
170 if (_size > 0)
171 --_size;
172 }
173
174 /// Returns a sub string of the string.
175 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string_view<T> substr(
176 std::size_t offset = 0, std::size_t count = (std::numeric_limits<std::size_t>::max)()) const noexcept
177 {
178 if (offset >= _size)
179 return {};
180 if (count == (std::numeric_limits<std::size_t>::max)())
181 return std::basic_string_view<T>(_data + offset, _size - offset);
182 if (offset + count > _size)
183 return std::basic_string_view<T>(_data + offset, _size - offset);
184 return std::basic_string_view<T>(_data + offset, count);
185 }
186
187 /// Returns a string view of the string.
188 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string_view<T> str() const noexcept
189 {
190 return std::basic_string_view<T> { _data, _size };
191 }
192
193 // clang-format off
194 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr pointer_type c_str() noexcept { _data[_size] = '\0'; return _data; }
195 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr pointer_type data() noexcept { return _data; }
196 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr iterator begin() noexcept { return _data; }
197 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr iterator end() noexcept { return _data + size(); }
198 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr T& at(std::size_t i) noexcept { return _data[i]; }
199 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr T& operator[](std::size_t i) noexcept { return _data[i]; }
200
201 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr const_pointer_type c_str() const noexcept { return _data; }
202 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr const_pointer_type data() const noexcept { return _data; }
203 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr const_iterator begin() const noexcept { return _data; }
204 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr const_iterator end() const noexcept { return _data + size(); }
205 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr T const& at(std::size_t i) const noexcept { return _data[i]; }
206 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr T const& operator[](std::size_t i) const noexcept { return _data[i]; }
207 // clang-format on
208
209 /// Returns a std::basic_string<T> from the string.
210 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string<T> ToString() const noexcept
211 {
212 return { _data, _size };
213 }
214
215 /// Returns a std::basic_string<T> from the string.
216 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr explicit operator std::basic_string<T>() const noexcept
217 {
218 return ToString();
219 }
220
221 /// Returns a std::basic_string_view<T> from the string.
222 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string_view<T> ToStringView() const noexcept
223 {
224 return { _data, _size };
225 }
226
227 /// Returns a std::basic_string_view<T> from the string.
228 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE constexpr explicit operator std::basic_string_view<T>() const noexcept
229 {
230 return ToStringView();
231 }
232
233 template <std::size_t OtherSize, SqlFixedStringMode OtherMode>
234 LIGHTWEIGHT_FORCE_INLINE std::weak_ordering operator<=>(
235 SqlFixedString<OtherSize, T, OtherMode> const& other) const noexcept
236 {
237 if ((void*) this == (void*) &other) [[unlikely]]
238 return std::weak_ordering::equivalent;
239
240 for (auto const i: std::views::iota(0U, (std::min) (size(), other.size())))
241 if (auto const cmp = _data[i] <=> other._data[i]; cmp != std::weak_ordering::equivalent) [[unlikely]]
242 return cmp;
243 return size() <=> other.size();
244 }
245
246 template <std::size_t OtherSize, SqlFixedStringMode OtherMode>
247 LIGHTWEIGHT_FORCE_INLINE constexpr bool operator==(SqlFixedString<OtherSize, T, OtherMode> const& other) const noexcept
248 {
249 return (*this <=> other) == std::weak_ordering::equivalent;
250 }
251
252 template <std::size_t OtherSize, SqlFixedStringMode OtherMode>
253 LIGHTWEIGHT_FORCE_INLINE constexpr bool operator!=(SqlFixedString<OtherSize, T, OtherMode> const& other) const noexcept
254 {
255 return !(*this == other);
256 }
257
258 LIGHTWEIGHT_FORCE_INLINE constexpr bool operator==(std::basic_string_view<T> other) const noexcept
259 {
260 return (substr() <=> other) == std::weak_ordering::equivalent;
261 }
262
263 LIGHTWEIGHT_FORCE_INLINE constexpr bool operator!=(std::basic_string_view<T> other) const noexcept
264 {
265 return !(*this == other);
266 }
267};
268
269template <std::size_t N, typename CharT, SqlFixedStringMode Mode>
270struct detail::SqlViewHelper<SqlFixedString<N, CharT, Mode>>
271{
272 static LIGHTWEIGHT_FORCE_INLINE std::basic_string_view<CharT> View(SqlFixedString<N, CharT, Mode> const& str) noexcept
273 {
274 return { str.data(), str.size() };
275 }
276};
277
278namespace detail
279{
280
281 template <typename>
282 struct IsSqlFixedStringTypeImpl: std::false_type
283 {
284 };
285
286 template <std::size_t N, typename T, SqlFixedStringMode Mode>
287 struct IsSqlFixedStringTypeImpl<SqlFixedString<N, T, Mode>>: std::true_type
288 {
289 };
290
291} // namespace detail
292
293template <typename T>
294constexpr bool IsSqlFixedString = detail::IsSqlFixedStringTypeImpl<T>::value;
295
296/// Fixed-size string of element type `char` with a capacity of `N` characters.
297///
298/// @ingroup DataTypes
299template <std::size_t N>
301
302/// Fixed-size string of element type `char16_t` with a capacity of `N` characters.
303///
304/// @ingroup DataTypes
305template <std::size_t N>
307
308/// Fixed-size string of element type `char32_t` with a capacity of `N` characters.
309///
310/// @ingroup DataTypes
311template <std::size_t N>
313
314/// Fixed-size string of element type `wchar_t` with a capacity of `N` characters.
315///
316/// @ingroup DataTypes
317template <std::size_t N>
319
320/// Fixed-size (right-trimmed) string of element type `char` with a capacity of `N` characters.
321///
322/// @ingroup DataTypes
323template <std::size_t N>
325
326/// Fixed-size (right-trimmed) string of element type `wchar_t` with a capacity of `N` characters.
327///
328/// @ingroup DataTypes
329template <std::size_t N>
331
332template <std::size_t N, typename T = char>
334
335template <std::size_t N, typename T, SqlFixedStringMode Mode>
336struct SqlBasicStringOperations<SqlFixedString<N, T, Mode>>
337{
338 using CharType = T;
339 using ValueType = SqlFixedString<N, CharType, Mode>;
340 static constexpr auto ColumnType = []() constexpr -> SqlColumnTypeDefinition {
341 if constexpr (std::same_as<CharType, char>)
342 {
343 if constexpr (Mode == SqlFixedStringMode::VARIABLE_SIZE)
344 return SqlColumnTypeDefinitions::Varchar { N };
345 else
346 return SqlColumnTypeDefinitions::Char { N };
347 }
348 else
349 {
350 if constexpr (Mode == SqlFixedStringMode::VARIABLE_SIZE)
351 return SqlColumnTypeDefinitions::NVarchar { N };
352 else
353 return SqlColumnTypeDefinitions::NChar { N };
354 }
355 }();
356
357 static CharType const* Data(ValueType const* str) noexcept
358 {
359 return str->data();
360 }
361
362 static CharType* Data(ValueType* str) noexcept
363 {
364 return str->data();
365 }
366
367 static SQLULEN Size(ValueType const* str) noexcept
368 {
369 return str->size();
370 }
371
372 static void Clear(ValueType* str) noexcept
373 {
374 str->clear();
375 }
376
377 static void Reserve(ValueType* str, size_t capacity) noexcept
378 {
379 str->reserve((std::min) (N, capacity));
380 str->resize((std::min) (N, capacity));
381 }
382
383 static void Resize(ValueType* str, SQLLEN indicator) noexcept
384 {
385 str->resize(indicator);
386 }
387
388 static void PostProcessOutputColumn(ValueType* result, SQLLEN indicator)
389 {
390 switch (indicator)
391 {
392 case SQL_NULL_DATA:
393 result->clear();
394 break;
395 case SQL_NO_TOTAL:
396 result->resize(N);
397 break;
398 default: {
399 auto const len = (std::min) (N, static_cast<std::size_t>(indicator) / sizeof(CharType));
400 result->setsize(len);
401
402 if constexpr (Mode == SqlFixedStringMode::FIXED_SIZE_RIGHT_TRIMMED)
403 {
404 TrimRight(result, indicator);
405 }
406 break;
407 }
408 }
409 }
410
411 LIGHTWEIGHT_FORCE_INLINE static void TrimRight(ValueType* boundOutputString, SQLLEN indicator) noexcept
412 {
413#if defined(_WIN32)
414 size_t n = (std::min) (static_cast<size_t>(indicator) / sizeof(CharType), N - 1);
415#else
416 size_t n = std::min(static_cast<size_t>(indicator), N - 1);
417#endif
418 while (n > 0 && std::isspace((*boundOutputString)[n - 1]))
419 --n;
420 boundOutputString->setsize(n);
421 }
422};
423
424} // namespace Lightweight
425
426template <std::size_t N, typename T, Lightweight::SqlFixedStringMode P>
427struct std::formatter<Lightweight::SqlFixedString<N, T, P>>: std::formatter<std::string>
428{
429 using value_type = Lightweight::SqlFixedString<N, T, P>;
430 auto format(value_type const& text, format_context& ctx) const -> format_context::iterator
431 {
432 if constexpr (std::same_as<T, wchar_t>)
433 return std::formatter<std::string>::format(ToUtf8(text.ToStringView()), ctx);
434 else
435 return std::formatter<std::string>::format(text.c_str(), ctx);
436 }
437};
LIGHTWEIGHT_FORCE_INLINE constexpr SqlFixedString(T const *s, std::size_t len) noexcept
Constructs a fixed-size string from a string pointer and length.
LIGHTWEIGHT_FORCE_INLINE constexpr std::size_t capacity() const noexcept
Returns the maximum capacity of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlFixedString(T const *s, T const *e) noexcept
Constructs a fixed-size string from a string pointer and end pointer.
LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string_view< T > str() const noexcept
Returns a string view of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr void clear() noexcept
Clears the string.
LIGHTWEIGHT_FORCE_INLINE constexpr void resize(std::size_t n) noexcept
LIGHTWEIGHT_FORCE_INLINE constexpr void push_back(T c) noexcept
Appends a character to the string.
LIGHTWEIGHT_FORCE_INLINE constexpr void assign(T const (&source)[SourceSize]) noexcept
Assigns a string literal to the string.
LIGHTWEIGHT_FORCE_INLINE constexpr void assign(std::basic_string_view< T > s) noexcept
Assigns a string view to the string.
LIGHTWEIGHT_FORCE_INLINE constexpr bool empty() const noexcept
Tests if the string is empty.
constexpr LIGHTWEIGHT_FORCE_INLINE SqlFixedString(T const (&text)[SourceSize])
Constructs a fixed-size string from a string literal.
LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string_view< T > ToStringView() const noexcept
Returns a std::basic_string_view<T> from 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
Returns a sub string of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr std::basic_string< T > ToString() const noexcept
Returns a std::basic_string<T> from the string.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlFixedString(std::basic_string< T > const &s) noexcept
Constructs a fixed-size string from a string.
LIGHTWEIGHT_FORCE_INLINE constexpr std::size_t size() const noexcept
Returns the size of the string.
LIGHTWEIGHT_FORCE_INLINE constexpr void pop_back() noexcept
Removes the last character from the string.
LIGHTWEIGHT_API std::u8string ToUtf8(std::u32string_view u32InputString)