Lightweight 0.20250904.0
Loading...
Searching...
No Matches
SqlVariant.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "../DataBinder/UnicodeConverter.hpp"
6#include "../SqlLogger.hpp"
7#include "Core.hpp"
8#include "Primitives.hpp"
9#include "SqlDate.hpp"
10#include "SqlDateTime.hpp"
11#include "SqlFixedString.hpp"
12#include "SqlGuid.hpp"
13#include "SqlNullValue.hpp"
14#include "SqlText.hpp"
15#include "SqlTime.hpp"
16#include "StdString.hpp"
17#include "StdStringView.hpp"
18
19#include <format>
20#include <print>
21#include <variant>
22
23namespace Lightweight
24{
25
26namespace detail
27{
28 template <class... Ts>
29 struct overloaded: Ts... // NOLINT(readability-identifier-naming)
30 {
31 using Ts::operator()...;
32 };
33
34 template <class... Ts>
35 overloaded(Ts...) -> overloaded<Ts...>;
36
37} // namespace detail
38
39/// @brief Represents a value that can be any of the supported SQL data types.
40///
41/// Use this class with care. Always prefer native types when possible, in order to avoid any unnecessary overhead.
42///
43/// @ingroup DataTypes
45{
46 /// @brief The inner type of the variant.
47 ///
48 /// This type is a variant of all the supported SQL data types.
49 using InnerType = std::variant<SqlNullType,
50 SqlGuid,
51 bool,
52 int8_t,
53 short,
54 unsigned short,
55 int,
56 unsigned int,
57 long long,
58 unsigned long long,
59 float,
60 double,
61 std::string,
62 std::string_view,
63 std::u16string,
64 std::u16string_view,
65 SqlText,
66 SqlDate,
67 SqlTime,
69
70 InnerType value;
71
72 /// @brief Default construct a new SqlVariant.
73 SqlVariant() = default;
74 /// @brief Copy construct a new SqlVariant from another.
75 SqlVariant(SqlVariant const&) = default;
76 /// @brief Move construct a new SqlVariant from another.
77 SqlVariant(SqlVariant&&) noexcept = default;
78 /// @brief Copy assign a new SqlVariant from another.
79 SqlVariant& operator=(SqlVariant const&) = default;
80 /// @brief Move assign a new SqlVariant from another.
81 SqlVariant& operator=(SqlVariant&&) noexcept = default;
82 /// @brief Destructor for SqlVariant.
83 ~SqlVariant() = default;
84
85 /// @brief Copy constructor of a SqlVariant from one of the supported types.
86 LIGHTWEIGHT_FORCE_INLINE SqlVariant(InnerType const& other):
87 value(other)
88 {
89 }
90
91 /// @brief Move constructor of a SqlVariant from one of the supported types.
92 LIGHTWEIGHT_FORCE_INLINE SqlVariant(InnerType&& other) noexcept:
93 value(std::move(other))
94 {
95 }
96
97 /// @brief Construct a new SqlVariant from a SqlFixedString.
98 template <std::size_t N, typename T = char, SqlFixedStringMode Mode>
99 constexpr LIGHTWEIGHT_FORCE_INLINE SqlVariant(SqlFixedString<N, T, Mode> const& other):
100 value { std::string_view { other.data(), other.size() } }
101 {
102 }
103
104 /// @brief Copy constructor of a SqlVariant from a char array.
105 template <std::size_t TextSize>
106 constexpr LIGHTWEIGHT_FORCE_INLINE SqlVariant(char const (&text)[TextSize]):
107 value { std::string_view { text, TextSize - 1 } }
108 {
109 }
110
111 /// @brief Copy constructor of a SqlVariant from a char16_t array.
112 template <std::size_t TextSize>
113 constexpr LIGHTWEIGHT_FORCE_INLINE SqlVariant(char16_t const (&text)[TextSize]):
114 value { std::u16string_view { text, TextSize - 1 } }
115 {
116 }
117
118 /// @brief Copy constructor of a SqlVariant from an optional of one of the supported types.
119 template <typename T>
120 LIGHTWEIGHT_FORCE_INLINE SqlVariant(std::optional<T> const& other):
121 value { other ? InnerType { *other } : InnerType { SqlNullValue } }
122 {
123 }
124
125 /// @brief Assignment operator of a SqlVariant from one of the supported types.
126 LIGHTWEIGHT_FORCE_INLINE SqlVariant& operator=(InnerType const& other)
127 {
128 value = other;
129 return *this;
130 }
131
132 /// @brief Assignment operator of a SqlVariant from one of the supported types.
133 LIGHTWEIGHT_FORCE_INLINE SqlVariant& operator=(InnerType&& other) noexcept
134 {
135 value = std::move(other);
136 return *this;
137 }
138
139 /// @brief Construct from an string-like object that implements an SqlViewHelper<>.
140 template <detail::HasSqlViewHelper StringViewLike>
141 LIGHTWEIGHT_FORCE_INLINE explicit SqlVariant(StringViewLike const* newValue):
142 value { detail::SqlViewHelper<std::remove_cv_t<decltype(*newValue)>>::View(*newValue) }
143 {
144 }
145
146 /// @brief Assign from an string-like object that implements an SqlViewHelper<>.
147 template <detail::HasSqlViewHelper StringViewLike>
148 LIGHTWEIGHT_FORCE_INLINE SqlVariant& operator=(StringViewLike const* newValue) noexcept
149 {
150 value = std::string_view(newValue->GetString(), newValue->GetLength());
151 return *this;
152 }
153
154 /// @brief Check if the value is NULL.
155 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE bool IsNull() const noexcept
156 {
157 return std::holds_alternative<SqlNullType>(value);
158 }
159
160 /// @brief Check if the value is of the specified type.
161 template <typename T>
162 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE bool Is() const noexcept
163 {
164 return std::holds_alternative<T>(value);
165 }
166
167 /// @brief Retrieve the value as the specified type.
168 template <typename T>
169 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE decltype(auto) Get() noexcept
170 {
171 if constexpr (IsSpecializationOf<std::optional, T>)
172 {
173 if (IsNull())
174 return T { std::nullopt };
175 else
176 return T { std::get<typename T::value_type>(value) };
177 }
178 else
179 return std::get<T>(value);
180 }
181
182 /// @brief Retrieve the value as the specified type, or return the default value if the value is NULL.
183 template <typename T>
184 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE T ValueOr(T&& defaultValue) const noexcept
185 {
186 if constexpr (std::is_integral_v<T>)
187 return TryGetIntegral<T>().value_or(std::forward<T>(defaultValue));
188
189 if (IsNull())
190 return std::forward<T>(defaultValue);
191
192 return std::get<T>(value);
193 }
194
195 // clang-format off
196 /// @brief Retrieve the bool from the variant or std::nullopt
197 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<bool> TryGetBool() const noexcept { return TryGetIntegral<bool>(); }
198 /// @brief Retrieve the int8_t from the variant or std::nullopt
199 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<int8_t> TryGetInt8() const noexcept { return TryGetIntegral<int8_t>(); }
200 /// @brief Retrieve the unsigned short from the variant or std::nullopt
201 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<short> TryGetShort() const noexcept { return TryGetIntegral<short>(); }
202 /// @brief Retrieve the unsigned short from the variant or std::nullopt
203 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<unsigned short> TryGetUShort() const noexcept { return TryGetIntegral<unsigned short>(); }
204 /// @brief Retrieve the int from the variant or std::nullopt
205 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<int> TryGetInt() const noexcept { return TryGetIntegral<int>(); }
206 /// @brief Retrieve the unsigned int from the variant or std::nullopt
207 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<unsigned int> TryGetUInt() const noexcept { return TryGetIntegral<unsigned int>(); }
208 /// @brief Retrieve the long long from the variant or std::nullopt
209 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<long long> TryGetLongLong() const noexcept { return TryGetIntegral<long long>(); }
210 /// @brief Retrieve the unsigned long long from the variant or std::nullopt
211 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<unsigned long long> TryGetULongLong() const noexcept { return TryGetIntegral<unsigned long long>(); }
212 // clang-format on
213
214 private:
215 /// @brief template that is used to get integral types
216 template <typename ResultType>
217 [[nodiscard]] std::optional<ResultType> TryGetIntegral() const noexcept
218 {
219 if (IsNull())
220 return std::nullopt;
221
222 // clang-format off
223 return std::visit(detail::overloaded {
224 []<typename T>(T v) -> ResultType requires(std::is_integral_v<T>) { return static_cast<ResultType>(v); },
225 [](auto) -> ResultType { throw std::bad_variant_access(); } // NOLINT(performance-unnecessary-value-param)
226 }, value);
227 // clang-format on
228 }
229
230 public:
231 /// @brief function to get string_view from SqlVariant or std::nullopt
232 [[nodiscard]] std::optional<std::string_view> TryGetStringView() const noexcept
233 {
234 if (IsNull())
235 return std::nullopt;
236
237 // clang-format off
238 return std::visit(detail::overloaded {
239 [](std::string_view v) { return v; },
240 [](std::string const& v) { return std::string_view(v.data(), v.size()); },
241 [](SqlText const& v) { return std::string_view(v.value.data(), v.value.size()); },
242 [](auto const&) -> std::string_view { throw std::bad_variant_access(); }
243 }, value);
244 // clang-format on
245 }
246
247 [[nodiscard]] std::optional<std::u16string_view> TryGetUtf16StringView() const noexcept
248 {
249 if (IsNull())
250 return std::nullopt;
251
252 // clang-format off
253 return std::visit(detail::overloaded {
254 [](std::u16string_view v) { return v; },
255 [](std::u16string const& v) { return std::u16string_view(v.data(), v.size()); },
256 [](auto const&) -> std::u16string_view { throw std::bad_variant_access(); }
257 }, value);
258 // clang-format on
259 }
260
261 /// @brief function to get SqlDate from SqlVariant or std::nullopt
262 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlDate> TryGetDate() const
263 {
264 if (IsNull())
265 return std::nullopt;
266
267 if (auto const* date = std::get_if<SqlDate>(&value))
268 return *date;
269
270 if (auto const* datetime = std::get_if<SqlDateTime>(&value))
271 {
272 return SqlDate { std::chrono::year(datetime->sqlValue.year),
273 std::chrono::month(datetime->sqlValue.month),
274 std::chrono::day(datetime->sqlValue.day) };
275 }
276
277 throw std::bad_variant_access();
278 }
279
280 [[nodiscard]] bool operator==(SqlVariant const& other) const noexcept
281 {
282 return ToString() == other.ToString();
283 }
284
285 [[nodiscard]] bool operator!=(SqlVariant const& other) const noexcept
286 {
287 return !(*this == other);
288 }
289
290 /// @brief function to get SqlTime from SqlVariant or std::nullopt
291 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlTime> TryGetTime() const
292 {
293 if (IsNull())
294 return std::nullopt;
295
296 if (auto const* time = std::get_if<SqlTime>(&value))
297 return *time;
298
299 if (auto const* datetime = std::get_if<SqlDateTime>(&value))
300 {
301 return SqlTime { std::chrono::hours(datetime->sqlValue.hour),
302 std::chrono::minutes(datetime->sqlValue.minute),
303 std::chrono::seconds(datetime->sqlValue.second),
304 std::chrono::microseconds(datetime->sqlValue.fraction) };
305 }
306
307 throw std::bad_variant_access();
308 }
309
310 /// @brief function to get SqlDateTime from SqlVariant or std::nullopt
311 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlDateTime> TryGetDateTime() const
312 {
313 if (IsNull())
314 return std::nullopt;
315
316 if (auto const* dateTime = std::get_if<SqlDateTime>(&value))
317 return *dateTime;
318
319 throw std::bad_variant_access();
320 }
321
322 /// @brief Retrieve the GUID from the variant or std::nullopt if the value is NULL.
323 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlGuid> TryGetGuid() const
324 {
325 if (IsNull())
326 return std::nullopt;
327
328 if (auto const* guid = std::get_if<SqlGuid>(&value))
329 return *guid;
330
331 throw std::bad_variant_access();
332 }
333
334 /// @brief Create string representation of the variant. Can be used for debug purposes
335 [[nodiscard]] LIGHTWEIGHT_API std::string ToString() const;
336};
337
338/// @brief Represents a row of data from the database using SqlVariant as the column data type.
339using SqlVariantRow = std::vector<SqlVariant>;
340
341template <>
342struct LIGHTWEIGHT_API SqlDataBinder<SqlVariant>
343{
344 static SQLRETURN InputParameter(SQLHSTMT stmt,
345 SQLUSMALLINT column,
346 SqlVariant const& variantValue,
347 SqlDataBinderCallback& cb) noexcept;
348
349 static SQLRETURN GetColumn(
350 SQLHSTMT stmt, SQLUSMALLINT column, SqlVariant* result, SQLLEN* indicator, SqlDataBinderCallback const& cb) noexcept;
351
352 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(SqlVariant const& value) noexcept
353 {
354 return value.ToString();
355 }
356};
357
358} // namespace Lightweight
359
360template <>
361struct std::formatter<Lightweight::SqlVariant>: formatter<string>
362{
363 auto format(Lightweight::SqlVariant const& value, format_context& ctx) const -> format_context::iterator
364 {
365 return std::formatter<string>::format(value.ToString(), ctx);
366 }
367};
constexpr auto SqlNullValue
Represents a value that can be any of the supported SQL data types.
LIGHTWEIGHT_FORCE_INLINE SqlVariant(InnerType &&other) noexcept
Move constructor of a SqlVariant from one of the supported types.
LIGHTWEIGHT_API std::string ToString() const
Create string representation of the variant. Can be used for debug purposes.
LIGHTWEIGHT_FORCE_INLINE std::optional< unsigned long long > TryGetULongLong() const noexcept
Retrieve the unsigned long long from the variant or std::nullopt.
LIGHTWEIGHT_FORCE_INLINE SqlVariant(std::optional< T > const &other)
Copy constructor of a SqlVariant from an optional of one of the supported types.
LIGHTWEIGHT_FORCE_INLINE std::optional< SqlDateTime > TryGetDateTime() const
function to get SqlDateTime from SqlVariant or std::nullopt
constexpr LIGHTWEIGHT_FORCE_INLINE SqlVariant(SqlFixedString< N, T, Mode > const &other)
Construct a new SqlVariant from a SqlFixedString.
LIGHTWEIGHT_FORCE_INLINE bool Is() const noexcept
Check if the value is of the specified type.
LIGHTWEIGHT_FORCE_INLINE T ValueOr(T &&defaultValue) const noexcept
Retrieve the value as the specified type, or return the default value if the value is NULL.
std::variant< SqlNullType, SqlGuid, bool, int8_t, short, unsigned short, int, unsigned int, long long, unsigned long long, float, double, std::string, std::string_view, std::u16string, std::u16string_view, SqlText, SqlDate, SqlTime, SqlDateTime > InnerType
The inner type of the variant.
SqlVariant(SqlVariant const &)=default
Copy construct a new SqlVariant from another.
constexpr LIGHTWEIGHT_FORCE_INLINE SqlVariant(char16_t const (&text)[TextSize])
Copy constructor of a SqlVariant from a char16_t array.
LIGHTWEIGHT_FORCE_INLINE std::optional< SqlGuid > TryGetGuid() const
Retrieve the GUID from the variant or std::nullopt if the value is NULL.
std::optional< std::string_view > TryGetStringView() const noexcept
function to get string_view from SqlVariant or std::nullopt
LIGHTWEIGHT_FORCE_INLINE SqlVariant & operator=(InnerType &&other) noexcept
Assignment operator of a SqlVariant from one of the supported types.
SqlVariant()=default
Default construct a new SqlVariant.
LIGHTWEIGHT_FORCE_INLINE std::optional< int > TryGetInt() const noexcept
Retrieve the int from the variant or std::nullopt.
LIGHTWEIGHT_FORCE_INLINE SqlVariant(StringViewLike const *newValue)
Construct from an string-like object that implements an SqlViewHelper<>.
LIGHTWEIGHT_FORCE_INLINE bool IsNull() const noexcept
Check if the value is NULL.
LIGHTWEIGHT_FORCE_INLINE std::optional< SqlTime > TryGetTime() const
function to get SqlTime from SqlVariant or std::nullopt
constexpr LIGHTWEIGHT_FORCE_INLINE SqlVariant(char const (&text)[TextSize])
Copy constructor of a SqlVariant from a char array.
LIGHTWEIGHT_FORCE_INLINE SqlVariant & operator=(StringViewLike const *newValue) noexcept
Assign from an string-like object that implements an SqlViewHelper<>.
LIGHTWEIGHT_FORCE_INLINE std::optional< SqlDate > TryGetDate() const
function to get SqlDate from SqlVariant or std::nullopt
LIGHTWEIGHT_FORCE_INLINE decltype(auto) Get() noexcept
Retrieve the value as the specified type.
LIGHTWEIGHT_FORCE_INLINE std::optional< short > TryGetShort() const noexcept
Retrieve the unsigned short from the variant or std::nullopt.
LIGHTWEIGHT_FORCE_INLINE std::optional< unsigned short > TryGetUShort() const noexcept
Retrieve the unsigned short from the variant or std::nullopt.
LIGHTWEIGHT_FORCE_INLINE std::optional< bool > TryGetBool() const noexcept
Retrieve the bool from the variant or std::nullopt.
LIGHTWEIGHT_FORCE_INLINE std::optional< int8_t > TryGetInt8() const noexcept
Retrieve the int8_t from the variant or std::nullopt.
SqlVariant(SqlVariant &&) noexcept=default
Move construct a new SqlVariant from another.
LIGHTWEIGHT_FORCE_INLINE std::optional< long long > TryGetLongLong() const noexcept
Retrieve the long long from the variant or std::nullopt.
LIGHTWEIGHT_FORCE_INLINE SqlVariant & operator=(InnerType const &other)
Assignment operator of a SqlVariant from one of the supported types.
LIGHTWEIGHT_FORCE_INLINE std::optional< unsigned int > TryGetUInt() const noexcept
Retrieve the unsigned int from the variant or std::nullopt.