5#include "../SqlColumnTypeDefinitions.hpp"
6#include "../SqlError.hpp"
7#include "../SqlLogger.hpp"
8#include "Primitives.hpp"
15#include <source_location>
20#if defined(__SIZEOF_INT128__) && !defined(_MSC_VER)
21 #define LIGHTWEIGHT_INT128_T __int128_t
22 static_assert(
sizeof(__int128_t) ==
sizeof(SQL_NUMERIC_STRUCT::val));
34template <std::
size_t ThePrecision, std::
size_t TheScale>
37 static constexpr auto ColumnType = SqlColumnTypeDefinitions::Decimal { .precision = ThePrecision, .scale = TheScale };
43 static constexpr auto Scale = TheScale;
45 static_assert(TheScale < SQL_MAX_NUMERIC_LEN);
59 constexpr
SqlNumeric(std::floating_point auto value) noexcept
65 constexpr explicit SqlNumeric(SQL_NUMERIC_STRUCT
const& value)
noexcept:
71 static_assert(std::endian::native == std::endian::little);
74 LIGHTWEIGHT_FORCE_INLINE
constexpr void assign(std::floating_point
auto value)
noexcept
76#if defined(LIGHTWEIGHT_INT128_T)
77 auto const num =
static_cast<LIGHTWEIGHT_INT128_T
>(value * std::pow(10,
Scale));
78 *((LIGHTWEIGHT_INT128_T*)
sqlValue.val) = num;
80 auto const num =
static_cast<int64_t
>(value * std::pow(10,
Scale));
98 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
auto ToUnscaledValue() const noexcept
100 auto const sign =
sqlValue.sign ? 1 : -1;
101#if defined(LIGHTWEIGHT_INT128_T)
102 return sign * *
reinterpret_cast<LIGHTWEIGHT_INT128_T const*
>(
sqlValue.val);
104 return sign * *
reinterpret_cast<int64_t const*
>(
sqlValue.val);
109 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
float ToFloat() const noexcept
115 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
double ToDouble() const noexcept
121 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
long double ToLongDouble() const noexcept
127 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
explicit operator float() const noexcept
133 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
explicit operator double() const noexcept
139 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
explicit operator long double() const noexcept
145 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE std::string
ToString()
const
150 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE std::weak_ordering operator<=>(
SqlNumeric const& other)
const noexcept
152 return ToDouble() <=> other.ToDouble();
155 template <std::
size_t OtherPrecision, std::
size_t OtherScale>
156 [[nodiscard]]
constexpr LIGHTWEIGHT_FORCE_INLINE
bool operator==(
159 return ToFloat() == other.ToFloat();
164template <std::
size_t Precision, std::
size_t Scale>
165struct SqlDataBinder<
SqlNumeric<Precision, Scale>>
169 static constexpr auto ColumnType = SqlColumnTypeDefinitions::Decimal { .precision = Precision, .scale = Scale };
171 static void RequireSuccess(SQLHSTMT stmt, SQLRETURN error, std::source_location sourceLocation = std::source_location::current())
173 if (SQL_SUCCEEDED(error))
179 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN InputParameter(SQLHSTMT stmt, SQLUSMALLINT column, ValueType
const& value,
SqlDataBinderCallback& )
noexcept
181 auto* mut =
const_cast<ValueType*
>(&value);
182 mut->sqlValue.precision = Precision;
183 mut->sqlValue.scale = Scale;
184 RequireSuccess(stmt, SQLBindParameter(stmt, column, SQL_PARAM_INPUT, SQL_C_NUMERIC, SQL_NUMERIC, Precision, Scale, (SQLPOINTER) &value.sqlValue, 0,
nullptr));
188 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN OutputColumn(SQLHSTMT stmt, SQLUSMALLINT column, ValueType* result, SQLLEN* indicator,
SqlDataBinderCallback& )
noexcept
191 RequireSuccess(stmt, SQLGetStmtAttr(stmt, SQL_ATTR_APP_ROW_DESC, (SQLPOINTER) &hDesc, 0,
nullptr));
192 RequireSuccess(stmt, SQLSetDescField(hDesc, (SQLSMALLINT) column, SQL_DESC_PRECISION, (SQLPOINTER) Precision, 0));
193 RequireSuccess(stmt, SQLSetDescField(hDesc, (SQLSMALLINT) column, SQL_DESC_SCALE, (SQLPOINTER) Scale, 0));
195 return SQLBindCol(stmt, column, SQL_C_NUMERIC, &result->sqlValue,
sizeof(ValueType), indicator);
198 static LIGHTWEIGHT_FORCE_INLINE SQLRETURN GetColumn(SQLHSTMT stmt, SQLUSMALLINT column, ValueType* result, SQLLEN* indicator,
SqlDataBinderCallback const& )
noexcept
201 RequireSuccess(stmt, SQLGetStmtAttr(stmt, SQL_ATTR_APP_ROW_DESC, (SQLPOINTER) &hDesc, 0,
nullptr));
202 RequireSuccess(stmt, SQLSetDescField(hDesc, (SQLSMALLINT) column, SQL_DESC_PRECISION, (SQLPOINTER) Precision, 0));
203 RequireSuccess(stmt, SQLSetDescField(hDesc, (SQLSMALLINT) column, SQL_DESC_SCALE, (SQLPOINTER) Scale, 0));
205 return SQLGetData(stmt, column, SQL_C_NUMERIC, &result->sqlValue,
sizeof(ValueType), indicator);
208 static LIGHTWEIGHT_FORCE_INLINE std::string Inspect(ValueType
const& value)
noexcept
210 return value.ToString();
216template <std::
size_t Precision, std::
size_t Scale>
217struct std::formatter<
SqlNumeric<Precision, Scale>>: std::formatter<double>
219 template <
typename FormatContext>
222 return formatter<double>::format(value.
ToDouble(), ctx);
static SqlErrorInfo fromStatementHandle(SQLHSTMT hStmt)
Constructs an ODBC error info object from the given ODBC statement handle.
constexpr LIGHTWEIGHT_FORCE_INLINE auto ToUnscaledValue() const noexcept
Converts the numeric to an unscaled integer value.
SQL_NUMERIC_STRUCT sqlValue
The value is stored as a string to avoid floating point precision issues.
constexpr LIGHTWEIGHT_FORCE_INLINE long double ToLongDouble() const noexcept
Converts the numeric to a long double precision floating point value.
static constexpr auto Precision
Number of total digits.
LIGHTWEIGHT_FORCE_INLINE constexpr SqlNumeric & operator=(std::floating_point auto value) noexcept
Assigns a floating point value to the numeric.
static constexpr auto Scale
Number of digits after the decimal point.
constexpr LIGHTWEIGHT_FORCE_INLINE float ToFloat() const noexcept
Converts the numeric to a floating point value.
LIGHTWEIGHT_FORCE_INLINE std::string ToString() const
Converts the numeric to a string.
constexpr SqlNumeric(SQL_NUMERIC_STRUCT const &value) noexcept
Constructs a numeric from a SQL_NUMERIC_STRUCT.
constexpr LIGHTWEIGHT_FORCE_INLINE double ToDouble() const noexcept
Converts the numeric to a double precision floating point value.
LIGHTWEIGHT_FORCE_INLINE constexpr void assign(std::floating_point auto value) noexcept
Assigns a value to the numeric.