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...>;
93 value(std::move(other))
98 template <std::
size_t N,
typename T =
char, SqlFixedStringMode Mode>
100 value { std::string_view { other.data(), other.size() } }
105 template <std::
size_t TextSize>
106 constexpr LIGHTWEIGHT_FORCE_INLINE
SqlVariant(
char const (&text)[TextSize]):
107 value { std::string_view { text, TextSize - 1 } }
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 } }
119 template <
typename T>
120 LIGHTWEIGHT_FORCE_INLINE
SqlVariant(std::optional<T>
const& other):
135 value = std::move(other);
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) }
147 template <detail::HasSqlViewHelper StringViewLike>
150 value = std::string_view(newValue->GetString(), newValue->GetLength());
155 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
bool IsNull() const noexcept
157 return std::holds_alternative<SqlNullType>(value);
161 template <
typename T>
162 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
bool Is() const noexcept
164 return std::holds_alternative<T>(value);
168 template <
typename T>
169 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE
decltype(
auto)
Get() noexcept
171 if constexpr (IsSpecializationOf<std::optional, T>)
174 return T { std::nullopt };
176 return T { std::get<typename T::value_type>(value) };
179 return std::get<T>(value);
183 template <
typename T>
184 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE T
ValueOr(T&& defaultValue)
const noexcept
186 if constexpr (std::is_integral_v<T>)
187 return TryGetIntegral<T>().value_or(std::forward<T>(defaultValue));
190 return std::forward<T>(defaultValue);
192 return std::get<T>(value);
197 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<bool>
TryGetBool() const noexcept {
return TryGetIntegral<bool>(); }
199 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<int8_t>
TryGetInt8() const noexcept {
return TryGetIntegral<int8_t>(); }
201 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<short>
TryGetShort() const noexcept {
return TryGetIntegral<short>(); }
203 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<unsigned short>
TryGetUShort() const noexcept {
return TryGetIntegral<unsigned short>(); }
205 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<int>
TryGetInt() const noexcept {
return TryGetIntegral<int>(); }
207 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<unsigned int>
TryGetUInt() const noexcept {
return TryGetIntegral<unsigned int>(); }
209 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<long long>
TryGetLongLong() const noexcept {
return TryGetIntegral<long long>(); }
211 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<unsigned long long>
TryGetULongLong() const noexcept {
return TryGetIntegral<unsigned long long>(); }
216 template <
typename ResultType>
217 [[nodiscard]] std::optional<ResultType> TryGetIntegral() const noexcept
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(); }
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(); }
247 [[nodiscard]] std::optional<std::u16string_view> TryGetUtf16StringView() const noexcept
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(); }
262 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlDate>
TryGetDate()
const
267 if (
auto const* date = std::get_if<SqlDate>(&value))
270 if (
auto const* datetime = std::get_if<SqlDateTime>(&value))
272 return SqlDate { std::chrono::year(datetime->sqlValue.year),
273 std::chrono::month(datetime->sqlValue.month),
274 std::chrono::day(datetime->sqlValue.day) };
277 throw std::bad_variant_access();
280 [[nodiscard]]
bool operator==(
SqlVariant const& other)
const noexcept
282 return ToString() == other.ToString();
285 [[nodiscard]]
bool operator!=(
SqlVariant const& other)
const noexcept
287 return !(*
this == other);
291 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlTime>
TryGetTime()
const
296 if (
auto const* time = std::get_if<SqlTime>(&value))
299 if (
auto const* datetime = std::get_if<SqlDateTime>(&value))
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) };
307 throw std::bad_variant_access();
311 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlDateTime>
TryGetDateTime()
const
316 if (
auto const* dateTime = std::get_if<SqlDateTime>(&value))
319 throw std::bad_variant_access();
323 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::optional<SqlGuid>
TryGetGuid()
const
328 if (
auto const* guid = std::get_if<SqlGuid>(&value))
331 throw std::bad_variant_access();
335 [[nodiscard]] LIGHTWEIGHT_API std::string
ToString()
const;
339using SqlVariantRow = std::vector<SqlVariant>;
342struct LIGHTWEIGHT_API SqlDataBinder<
SqlVariant>
344 static SQLRETURN InputParameter(SQLHSTMT stmt,
349 static SQLRETURN GetColumn(
352 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(
SqlVariant const& value)
noexcept
361struct std::formatter<Lightweight::SqlVariant>: formatter<string>
365 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
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.