5#include "../DataBinder/UnicodeConverter.hpp"
6#include "../SqlLogger.hpp"
8#include "Primitives.hpp"
9#include "SqlBinary.hpp"
11#include "SqlDateTime.hpp"
12#include "SqlDynamicNumeric.hpp"
13#include "SqlFixedString.hpp"
15#include "SqlNullValue.hpp"
18#include "StdString.hpp"
19#include "StdStringView.hpp"
30 template <
class... Ts>
31 struct overloaded: Ts...
33 using Ts::operator()...;
36 template <
class... Ts>
37 overloaded(Ts...) -> overloaded<Ts...>;
98 value(std::move(other))
103 template <std::
size_t N,
typename T =
char, SqlFixedStringMode Mode>
105 value { std::string { other.data(), other.size() } }
110 template <std::
size_t TextSize>
111 constexpr LIGHTWEIGHT_FORCE_INLINE
SqlVariant(
char const (&text)[TextSize]):
112 value { std::string_view { text, TextSize - 1 } }
117 template <std::
size_t TextSize>
118 constexpr LIGHTWEIGHT_FORCE_INLINE
SqlVariant(
char16_t const (&text)[TextSize]):
119 value { std::u16string_view { text, TextSize - 1 } }
124 template <
typename T>
125 LIGHTWEIGHT_FORCE_INLINE
SqlVariant(std::optional<T>
const& other):
140 value = std::move(other);
145 template <detail::HasSqlViewHelper StringViewLike>
146 LIGHTWEIGHT_FORCE_INLINE
explicit SqlVariant(StringViewLike
const* newValue):
147 value { detail::SqlViewHelper<std::remove_cv_t<decltype(*newValue)>>::View(*newValue) }
152 template <detail::HasSqlViewHelper StringViewLike>
155 value = std::string_view(newValue->GetString(), newValue->GetLength());
160 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
bool IsNull() const noexcept
162 return std::holds_alternative<SqlNullType>(
value);
166 template <
typename T>
167 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
bool Is() const noexcept
169 return std::holds_alternative<T>(
value);
178 template <
typename T>
179 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
decltype(
auto)
Get() noexcept
181 if constexpr (IsSpecializationOf<std::optional, T>)
184 return T { std::nullopt };
186 return T { std::get<typename T::value_type>(
value) };
188 else if constexpr (std::is_floating_point_v<T>)
190 if (
auto const* numeric = std::get_if<SqlDynamicNumeric>(&
value))
191 return static_cast<T
>(numeric->ToDouble());
192 return static_cast<T
>(std::get<T>(
value));
194 else if constexpr (std::is_same_v<T, std::string>)
196 if (
auto const* binary = std::get_if<SqlBinary>(&
value))
197 return std::string(
reinterpret_cast<char const*
>(binary->data()), binary->size());
198 if (
auto const* numeric = std::get_if<SqlDynamicNumeric>(&
value))
199 return numeric->ToString();
200 return std::string(std::get<std::string>(
value));
203 return std::get<T>(
value);
212 template <
typename T>
213 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE T
ValueOr(T&& defaultValue)
const noexcept
215 if constexpr (std::is_integral_v<T>)
216 return TryGetIntegral<T>().value_or(std::forward<T>(defaultValue));
219 return std::forward<T>(defaultValue);
221 if constexpr (std::is_floating_point_v<T>)
223 if (
auto const* numeric = std::get_if<SqlDynamicNumeric>(&
value))
224 return static_cast<T
>(numeric->ToDouble());
226 else if constexpr (std::is_same_v<std::remove_cvref_t<T>, std::string>)
228 if (
auto const* binary = std::get_if<SqlBinary>(&
value))
229 return T(
reinterpret_cast<char const*
>(binary->data()), binary->size());
230 if (
auto const* numeric = std::get_if<SqlDynamicNumeric>(&
value))
231 return T(numeric->ToString());
237 if (
auto const* held = std::get_if<std::remove_cvref_t<T>>(&
value))
240 return std::forward<T>(defaultValue);
245 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<bool>
TryGetBool() const noexcept {
return TryGetIntegral<bool>(); }
247 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<int8_t>
TryGetInt8() const noexcept {
return TryGetIntegral<int8_t>(); }
249 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<short>
TryGetShort() const noexcept {
return TryGetIntegral<short>(); }
251 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<unsigned short>
TryGetUShort() const noexcept {
return TryGetIntegral<unsigned short>(); }
253 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<int>
TryGetInt() const noexcept {
return TryGetIntegral<int>(); }
255 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<unsigned int>
TryGetUInt() const noexcept {
return TryGetIntegral<unsigned int>(); }
257 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<long long>
TryGetLongLong() const noexcept {
return TryGetIntegral<long long>(); }
259 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<unsigned long long>
TryGetULongLong() const noexcept {
return TryGetIntegral<unsigned long long>(); }
267 template <
typename ResultType>
268 [[nodiscard]] std::optional<ResultType> TryGetIntegral() const noexcept
274 return std::visit(detail::overloaded {
275 []<
typename T>(T v) -> std::optional<ResultType>
requires(std::is_integral_v<T>) {
return static_cast<ResultType
>(v); },
278 [](SqlDynamicNumeric
const& v) -> std::optional<ResultType> {
281 return static_cast<ResultType
>(v.unscaledValue);
283 [](
auto) -> std::optional<ResultType> {
return std::nullopt; }
297 using Result = std::optional<std::string_view>;
299 return std::visit(detail::overloaded {
300 [](std::string_view v) -> Result {
return v; },
301 [](std::string
const& v) -> Result {
return std::string_view(v.data(), v.size()); },
302 [](
SqlText const& v) -> Result {
return std::string_view(v.value.data(), v.value.size()); },
305 [](
SqlBinary const& v) -> Result {
return std::string_view(
reinterpret_cast<char const*
>(v.data()), v.size()); },
306 [](
auto const&) -> Result {
return std::nullopt; }
319 using Result = std::optional<std::u16string_view>;
321 return std::visit(detail::overloaded {
322 [](std::u16string_view v) -> Result {
return v; },
323 [](std::u16string
const& v) -> Result {
return std::u16string_view(v.data(), v.size()); },
324 [](
auto const&) -> Result {
return std::nullopt; }
330 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlDate>
TryGetDate()
const
335 if (
auto const* date = std::get_if<SqlDate>(&
value))
338 if (
auto const* datetime = std::get_if<SqlDateTime>(&
value))
340 return SqlDate { std::chrono::year(datetime->sqlValue.year),
341 std::chrono::month(datetime->sqlValue.month),
342 std::chrono::day(datetime->sqlValue.day) };
345 throw std::bad_variant_access();
351 return ToString() == other.ToString();
357 return !(*
this == other);
361 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlTime>
TryGetTime()
const
366 if (
auto const* time = std::get_if<SqlTime>(&
value))
369 if (
auto const* datetime = std::get_if<SqlDateTime>(&
value))
371 return SqlTime { std::chrono::hours(datetime->sqlValue.hour),
372 std::chrono::minutes(datetime->sqlValue.minute),
373 std::chrono::seconds(datetime->sqlValue.second),
374 std::chrono::microseconds(datetime->sqlValue.fraction) };
377 throw std::bad_variant_access();
381 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlDateTime>
TryGetDateTime()
const
386 if (
auto const* dateTime = std::get_if<SqlDateTime>(&
value))
389 throw std::bad_variant_access();
393 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlGuid>
TryGetGuid()
const
398 if (
auto const* guid = std::get_if<SqlGuid>(&
value))
401 throw std::bad_variant_access();
410 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlDynamicNumeric>
TryGetNumeric()
const
415 if (
auto const* numeric = std::get_if<SqlDynamicNumeric>(&
value))
418 throw std::bad_variant_access();
427 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlBinary>
TryGetBinary()
const
432 if (
auto const* binary = std::get_if<SqlBinary>(&
value))
435 throw std::bad_variant_access();
439 [[nodiscard]] LIGHTWEIGHT_API std::string
ToString()
const;
443using SqlVariantRow = std::vector<SqlVariant>;
446struct LIGHTWEIGHT_API SqlDataBinder<
SqlVariant>
448 static SQLRETURN InputParameter(SQLHSTMT stmt,
453 static SQLRETURN GetColumn(
456 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(
SqlVariant const& value)
noexcept
465struct std::formatter<Lightweight::SqlVariant>: formatter<string>
469 return std::formatter<string>::format(value.ToString(), ctx);
Represents a binary data type.
constexpr auto SqlNullValue
A fixed-point decimal whose precision and scale are known only at run time.
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< SqlDynamicNumeric > TryGetNumeric() const
Retrieve the exact fixed-point decimal from the variant, or std::nullopt if NULL.
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.
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
Retrieves a string view from the variant, or std::nullopt if the variant is NULL or holds a non-strin...
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, SqlBinary, SqlDynamicNumeric, SqlDate, SqlTime, SqlDateTime > InnerType
The inner type of the variant.
bool operator!=(SqlVariant const &other) const noexcept
Inequality comparison operator.
std::optional< std::u16string_view > TryGetUtf16StringView() const noexcept
Retrieves a UTF-16 string view from the variant, or std::nullopt if the variant is NULL or holds a no...
LIGHTWEIGHT_FORCE_INLINE SqlVariant & operator=(InnerType &&other) noexcept
Assignment operator of a SqlVariant from one of the supported types.
InnerType value
The variant value.
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 std::optional< SqlBinary > TryGetBinary() const
Retrieve the binary payload from the variant, or std::nullopt if the value is NULL.
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<>.
bool operator==(SqlVariant const &other) const noexcept
Equality comparison operator.
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.