5#include "../SqlColumnTypeDefinitions.hpp"
6#include "../SqlError.hpp"
7#include "Primitives.hpp"
15#include <source_location>
23#if defined(__SIZEOF_INT128__) && !defined(_MSC_VER)
24 #define LIGHTWEIGHT_INT128_T __int128_t
25 static_assert(
sizeof(__int128_t) ==
sizeof(SQL_NUMERIC_STRUCT::val));
37template <std::
size_t ThePrecision, std::
size_t TheScale>
44 static constexpr auto Scale = TheScale;
47 static constexpr auto ColumnType = SqlColumnTypeDefinitions::Decimal { .precision =
Precision, .scale = TheScale };
49 static_assert(
Precision <= SQL_MAX_NUMERIC_LEN);
71 constexpr
SqlNumeric(std::floating_point auto value) noexcept
77 constexpr explicit SqlNumeric(SQL_NUMERIC_STRUCT
const& value)
noexcept:
83 static_assert(std::endian::native == std::endian::little);
86 LIGHTWEIGHT_FORCE_INLINE
constexpr void assign(std::floating_point
auto inputValue)
noexcept
91 sqlValue.sign = std::signbit(inputValue) ? 0 : 1;
95 auto const unscaledValue = std::roundl(
static_cast<long double>(std::abs(inputValue) * std::powl(10.0L,
Scale)));
96#if defined(LIGHTWEIGHT_INT128_T)
97 auto const num =
static_cast<LIGHTWEIGHT_INT128_T
>(unscaledValue);
98 std::memcpy(
sqlValue.val, &num,
sizeof(num));
100 auto const num =
static_cast<int64_t
>(unscaledValue);
101 std::memcpy(
sqlValue.val, &num,
sizeof(num));
113 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
auto ToUnscaledValue() const noexcept
117 auto const unscaledValue = std::roundl(
nativeValue * std::powl(10.0L,
Scale));
118#if defined(LIGHTWEIGHT_INT128_T)
119 return static_cast<LIGHTWEIGHT_INT128_T
>(unscaledValue);
121 return static_cast<int64_t
>(unscaledValue);
125 auto const sign =
sqlValue.sign ? 1 : -1;
126#if defined(LIGHTWEIGHT_INT128_T)
127 return sign * *
reinterpret_cast<LIGHTWEIGHT_INT128_T const*
>(
sqlValue.val);
129 return sign * *
reinterpret_cast<int64_t const*
>(
sqlValue.val);
134 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
float ToFloat() const noexcept
140 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
double ToDouble() const noexcept
146 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
long double ToLongDouble() const noexcept
152 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
explicit operator float() const noexcept
158 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
explicit operator double() const noexcept
164 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
explicit operator long double() const noexcept
170 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::string
ToString()
const
176 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::weak_ordering
operator<=>(
SqlNumeric const& other)
const noexcept
178 return ToDouble() <=> other.ToDouble();
182 template <std::
size_t OtherPrecision, std::
size_t OtherScale>
183 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
bool operator==(
186 return ToFloat() == other.ToFloat();
191concept SqlNumericType =
requires(T t) {
192 { T::Precision } -> std::convertible_to<std::size_t>;
193 { T::Scale } -> std::convertible_to<std::size_t>;
194} && std::same_as<T, SqlNumeric<T::Precision, T::Scale>>;
197template <std::
size_t Precision, std::
size_t Scale>
198struct SqlDataBinder<SqlNumeric<Precision, Scale>>
200 using ValueType = SqlNumeric<Precision, Scale>;
202 static constexpr auto ColumnType = SqlColumnTypeDefinitions::Decimal { .precision = Precision, .scale = Scale };
204 static void RequireSuccess(SQLHSTMT stmt, SQLRETURN error, std::source_location sourceLocation = std::source_location::current())
206 if (SQL_SUCCEEDED(error))
212 static constexpr bool NativeNumericSupportIsBroken(SqlServerType serverType)
noexcept
216 return serverType == SqlServerType::SQLITE || serverType == SqlServerType::MICROSOFT_SQL;
219 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt,
221 ValueType
const& value,
222 SqlDataBinderCallback& cb)
noexcept
224 if (NativeNumericSupportIsBroken(cb.ServerType()))
226 return SQLBindParameter(stmt,
233 (SQLPOINTER) &value.nativeValue,
234 sizeof(value.nativeValue),
238 return SQLBindParameter(stmt,
243 value.sqlValue.precision,
244 value.sqlValue.scale,
251 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(
252 SQLHSTMT stmt, SQLUSMALLINT column, ValueType* result, SQLLEN* indicator, SqlDataBinderCallback& cb)
noexcept
254 if (NativeNumericSupportIsBroken(cb.ServerType()))
256 result->sqlValue = { .precision = Precision, .scale = Scale, .sign = 0, .val = {} };
257 return SQLBindCol(stmt, column, SQL_C_DOUBLE, &result->nativeValue,
sizeof(result->nativeValue), indicator);
261 RequireSuccess(stmt, SQLGetStmtAttr(stmt, SQL_ATTR_APP_ROW_DESC, (SQLPOINTER) &hDesc, 0,
nullptr));
262 RequireSuccess(stmt, SQLSetDescField(hDesc, (SQLSMALLINT) column, SQL_DESC_PRECISION, (SQLPOINTER) Precision, 0));
263 RequireSuccess(stmt, SQLSetDescField(hDesc, (SQLSMALLINT) column, SQL_DESC_SCALE, (SQLPOINTER) Scale, 0));
265 return SQLBindCol(stmt, column, SQL_C_NUMERIC, &result->sqlValue,
sizeof(ValueType), indicator);
268 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt, SQLUSMALLINT column, ValueType* result, SQLLEN* indicator, SqlDataBinderCallback
const& cb)
noexcept
270 if (NativeNumericSupportIsBroken(cb.ServerType()))
272 result->sqlValue = { .precision = Precision, .scale = Scale, .sign = 0, .val = {} };
273 return SQLGetData(stmt, column, SQL_C_DOUBLE, &result->nativeValue,
sizeof(result->nativeValue), indicator);
277 RequireSuccess(stmt, SQLGetStmtAttr(stmt, SQL_ATTR_APP_ROW_DESC, (SQLPOINTER) &hDesc, 0,
nullptr));
278 RequireSuccess(stmt, SQLSetDescField(hDesc, (SQLSMALLINT) column, SQL_DESC_PRECISION, (SQLPOINTER) Precision, 0));
279 RequireSuccess(stmt, SQLSetDescField(hDesc, (SQLSMALLINT) column, SQL_DESC_SCALE, (SQLPOINTER) Scale, 0));
281 return SQLGetData(stmt, column, SQL_C_NUMERIC, &result->sqlValue,
sizeof(ValueType), indicator);
284 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(ValueType
const& value)
noexcept
286 return value.ToString();
293template <Lightweight::SqlNumericType Type>
294struct std::formatter<Type>: std::formatter<std::string>
296 template <
typename FormatContext>
297 auto format(Type
const& value, FormatContext& ctx)
299 return formatter<std::string>::format(value.ToString(), ctx);
static SqlErrorInfo FromStatementHandle(SQLHSTMT hStmt)
Constructs an ODBC error info object from the given ODBC statement handle.
constexpr LIGHTWEIGHT_FORCE_INLINE float ToFloat() const noexcept
Converts the numeric to a floating point value.
LIGHTWEIGHT_FORCE_INLINE constexpr void assign(std::floating_point auto inputValue) noexcept
Assigns a value to the numeric.
double nativeValue
Cached native value for drivers without SQL_NUMERIC_STRUCT support.
constexpr LIGHTWEIGHT_FORCE_INLINE bool operator==(SqlNumeric< OtherPrecision, OtherScale > const &other) const noexcept
Equality comparison operator.
constexpr LIGHTWEIGHT_FORCE_INLINE double ToDouble() const noexcept
Converts the numeric to a double precision floating point value.
constexpr SqlNumeric(SQL_NUMERIC_STRUCT const &value) noexcept
Constructs a numeric from a SQL_NUMERIC_STRUCT.
LIGHTWEIGHT_FORCE_INLINE std::string ToString() const
Converts the numeric to a string.
static constexpr auto Precision
Number of total digits.
constexpr SqlNumeric() noexcept=default
Default constructor.
SQL_NUMERIC_STRUCT sqlValue
The SQL numeric struct for ODBC binding.
constexpr LIGHTWEIGHT_FORCE_INLINE auto ToUnscaledValue() const noexcept
Converts the numeric to an unscaled integer value.
constexpr LIGHTWEIGHT_FORCE_INLINE std::weak_ordering operator<=>(SqlNumeric const &other) const noexcept
Three-way comparison operator.
static constexpr auto Scale
Number of digits after the decimal point.
constexpr LIGHTWEIGHT_FORCE_INLINE long double ToLongDouble() const noexcept
Converts the numeric to a long double precision floating point value.
static constexpr auto ColumnType
The SQL column type definition for this numeric type.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlNumeric & operator=(std::floating_point auto value) noexcept
Assigns a floating point value to the numeric.