5#include "../DataBinder/UnicodeConverter.hpp"
6#include "../SqlLogger.hpp"
8#include "Primitives.hpp"
10#include "SqlDateTime.hpp"
11#include "SqlFixedString.hpp"
13#include "SqlNullValue.hpp"
16#include "StdString.hpp"
17#include "StdStringView.hpp"
28 template <
class... Ts>
29 struct overloaded: Ts...
31 using Ts::operator()...;
34 template <
class... Ts>
35 overloaded(Ts...) -> overloaded<Ts...>;
94 value(std::move(other))
99 template <std::
size_t N,
typename T =
char, SqlFixedStringMode Mode>
101 value { std::string { other.data(), other.size() } }
106 template <std::
size_t TextSize>
107 constexpr LIGHTWEIGHT_FORCE_INLINE
SqlVariant(
char const (&text)[TextSize]):
108 value { std::string_view { text, TextSize - 1 } }
113 template <std::
size_t TextSize>
114 constexpr LIGHTWEIGHT_FORCE_INLINE
SqlVariant(
char16_t const (&text)[TextSize]):
115 value { std::u16string_view { text, TextSize - 1 } }
120 template <
typename T>
121 LIGHTWEIGHT_FORCE_INLINE
SqlVariant(std::optional<T>
const& other):
136 value = std::move(other);
141 template <detail::HasSqlViewHelper StringViewLike>
142 LIGHTWEIGHT_FORCE_INLINE
explicit SqlVariant(StringViewLike
const* newValue):
143 value { detail::SqlViewHelper<std::remove_cv_t<decltype(*newValue)>>::View(*newValue) }
148 template <detail::HasSqlViewHelper StringViewLike>
151 value = std::string_view(newValue->GetString(), newValue->GetLength());
156 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
bool IsNull() const noexcept
158 return std::holds_alternative<SqlNullType>(
value);
162 template <
typename T>
163 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
bool Is() const noexcept
165 return std::holds_alternative<T>(
value);
169 template <
typename T>
170 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
decltype(
auto)
Get() noexcept
172 if constexpr (IsSpecializationOf<std::optional, T>)
175 return T { std::nullopt };
177 return T { std::get<typename T::value_type>(
value) };
180 return std::get<T>(
value);
184 template <
typename T>
185 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE T
ValueOr(T&& defaultValue)
const noexcept
187 if constexpr (std::is_integral_v<T>)
188 return TryGetIntegral<T>().value_or(std::forward<T>(defaultValue));
191 return std::forward<T>(defaultValue);
193 return std::get<T>(
value);
198 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<bool>
TryGetBool() const noexcept {
return TryGetIntegral<bool>(); }
200 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<int8_t>
TryGetInt8() const noexcept {
return TryGetIntegral<int8_t>(); }
202 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<short>
TryGetShort() const noexcept {
return TryGetIntegral<short>(); }
204 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<unsigned short>
TryGetUShort() const noexcept {
return TryGetIntegral<unsigned short>(); }
206 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<int>
TryGetInt() const noexcept {
return TryGetIntegral<int>(); }
208 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<unsigned int>
TryGetUInt() const noexcept {
return TryGetIntegral<unsigned int>(); }
210 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<long long>
TryGetLongLong() const noexcept {
return TryGetIntegral<long long>(); }
212 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<unsigned long long>
TryGetULongLong() const noexcept {
return TryGetIntegral<unsigned long long>(); }
217 template <
typename ResultType>
218 [[nodiscard]] std::optional<ResultType> TryGetIntegral() const noexcept
224 return std::visit(detail::overloaded {
225 []<
typename T>(T v) -> ResultType
requires(std::is_integral_v<T>) {
return static_cast<ResultType
>(v); },
226 [](
auto) -> ResultType {
throw std::bad_variant_access(); }
239 return std::visit(detail::overloaded {
240 [](std::string_view v) {
return v; },
241 [](std::string
const& v) {
return std::string_view(v.data(), v.size()); },
242 [](
SqlText const& v) {
return std::string_view(v.value.data(), v.value.size()); },
243 [](
auto const&) -> std::string_view {
throw std::bad_variant_access(); }
255 return std::visit(detail::overloaded {
256 [](std::u16string_view v) {
return v; },
257 [](std::u16string
const& v) {
return std::u16string_view(v.data(), v.size()); },
258 [](
auto const&) -> std::u16string_view {
throw std::bad_variant_access(); }
264 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlDate>
TryGetDate()
const
269 if (
auto const* date = std::get_if<SqlDate>(&
value))
272 if (
auto const* datetime = std::get_if<SqlDateTime>(&
value))
274 return SqlDate { std::chrono::year(datetime->sqlValue.year),
275 std::chrono::month(datetime->sqlValue.month),
276 std::chrono::day(datetime->sqlValue.day) };
279 throw std::bad_variant_access();
285 return ToString() == other.ToString();
291 return !(*
this == other);
295 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlTime>
TryGetTime()
const
300 if (
auto const* time = std::get_if<SqlTime>(&
value))
303 if (
auto const* datetime = std::get_if<SqlDateTime>(&
value))
305 return SqlTime { std::chrono::hours(datetime->sqlValue.hour),
306 std::chrono::minutes(datetime->sqlValue.minute),
307 std::chrono::seconds(datetime->sqlValue.second),
308 std::chrono::microseconds(datetime->sqlValue.fraction) };
311 throw std::bad_variant_access();
315 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlDateTime>
TryGetDateTime()
const
320 if (
auto const* dateTime = std::get_if<SqlDateTime>(&
value))
323 throw std::bad_variant_access();
327 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlGuid>
TryGetGuid()
const
332 if (
auto const* guid = std::get_if<SqlGuid>(&
value))
335 throw std::bad_variant_access();
339 [[nodiscard]] LIGHTWEIGHT_API std::string
ToString()
const;
343using SqlVariantRow = std::vector<SqlVariant>;
346struct LIGHTWEIGHT_API SqlDataBinder<
SqlVariant>
348 static SQLRETURN InputParameter(SQLHSTMT stmt,
353 static SQLRETURN GetColumn(
356 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(
SqlVariant const& value)
noexcept
365struct std::formatter<Lightweight::SqlVariant>: formatter<string>
369 return std::formatter<string>::format(value.ToString(), ctx);
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
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 not available.
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 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.