Lightweight 0.20260625.0
Loading...
Searching...
No Matches
Lightweight::SqlNumeric< ThePrecision, TheScale > Struct Template Reference

#include <SqlNumeric.hpp>

Public Member Functions

constexpr SqlNumeric () noexcept=default
 Default constructor.
 
constexpr SqlNumeric (SqlNumeric &&) noexcept=default
 Move constructor.
 
constexpr SqlNumericoperator= (SqlNumeric &&) noexcept=default
 Move assignment operator.
 
constexpr SqlNumeric (SqlNumeric const &) noexcept=default
 Copy constructor.
 
constexpr SqlNumericoperator= (SqlNumeric const &) noexcept=default
 Copy assignment operator.
 
constexpr SqlNumeric (std::floating_point auto value) noexcept
 Constructs a numeric from a floating point value.
 
constexpr SqlNumeric (SQL_NUMERIC_STRUCT const &value) noexcept
 Constructs a numeric from a SQL_NUMERIC_STRUCT.
 
LIGHTWEIGHT_FORCE_INLINE constexpr void assign (std::floating_point auto inputValue) noexcept
 Assigns a value to the numeric.
 
LIGHTWEIGHT_FORCE_INLINE constexpr SqlNumericoperator= (std::floating_point auto value) noexcept
 Assigns a floating point value to the numeric.
 
LIGHTWEIGHT_FORCE_INLINE Int128 ToUnscaledValue () const noexcept
 
LIGHTWEIGHT_FORCE_INLINE float ToFloat () const noexcept
 Converts the numeric to a floating point value.
 
LIGHTWEIGHT_FORCE_INLINE double ToDouble () const noexcept
 Converts the numeric to a double precision floating point value.
 
LIGHTWEIGHT_FORCE_INLINE long double ToLongDouble () const noexcept
 Converts the numeric to a long double precision floating point value.
 
LIGHTWEIGHT_FORCE_INLINE operator float () const noexcept
 Converts the numeric to a floating point value.
 
LIGHTWEIGHT_FORCE_INLINE operator double () const noexcept
 Converts the numeric to a double precision floating point value.
 
LIGHTWEIGHT_FORCE_INLINE operator long double () const noexcept
 Converts the numeric to a long double precision floating point value.
 
LIGHTWEIGHT_FORCE_INLINE std::string ToString () const
 
LIGHTWEIGHT_FORCE_INLINE std::partial_ordering operator<=> (SqlNumeric const &other) const noexcept
 
template<std::size_t OtherPrecision, std::size_t OtherScale>
LIGHTWEIGHT_FORCE_INLINE bool operator== (SqlNumeric< OtherPrecision, OtherScale > const &other) const noexcept
 Equality comparison operator.
 

Public Attributes

SQL_NUMERIC_STRUCT sqlValue {}
 The SQL numeric struct for ODBC binding.
 
double nativeValue {}
 Cached native value for drivers without SQL_NUMERIC_STRUCT support.
 

Static Public Attributes

static constexpr auto Precision = ThePrecision
 Number of total digits.
 
static constexpr auto Scale = TheScale
 Number of digits after the decimal point.
 
static constexpr auto ColumnType = SqlColumnTypeDefinitions::Decimal { .precision = Precision, .scale = TheScale }
 The SQL column type definition for this numeric type.
 

Detailed Description

template<std::size_t ThePrecision, std::size_t TheScale>
struct Lightweight::SqlNumeric< ThePrecision, TheScale >

Represents a fixed-point number with a given precision and scale.

Precision is exactly the total number of digits in the number, including the digits after the decimal point.

Scale is the number of digits after the decimal point, and may be anywhere in [0, Precision]. Scale == Precision denotes a purely fractional number, e.g. SqlNumeric<4, 4> covers [0.0000, 0.9999] — the C++ equivalent of SQL's DECIMAL(4, 4).

Definition at line 94 of file SqlNumeric.hpp.

Constructor & Destructor Documentation

◆ SqlNumeric() [1/2]

template<std::size_t ThePrecision, std::size_t TheScale>
constexpr Lightweight::SqlNumeric< ThePrecision, TheScale >::SqlNumeric ( std::floating_point auto  value)
inlineconstexprnoexcept

Constructs a numeric from a floating point value.

Definition at line 138 of file SqlNumeric.hpp.

References Lightweight::SqlNumeric< ThePrecision, TheScale >::assign().

◆ SqlNumeric() [2/2]

template<std::size_t ThePrecision, std::size_t TheScale>
constexpr Lightweight::SqlNumeric< ThePrecision, TheScale >::SqlNumeric ( SQL_NUMERIC_STRUCT const &  value)
inlineexplicitconstexprnoexcept

Constructs a numeric from a SQL_NUMERIC_STRUCT.

Definition at line 144 of file SqlNumeric.hpp.

Member Function Documentation

◆ assign()

template<std::size_t ThePrecision, std::size_t TheScale>
LIGHTWEIGHT_FORCE_INLINE constexpr void Lightweight::SqlNumeric< ThePrecision, TheScale >::assign ( std::floating_point auto  inputValue)
inlineconstexprnoexcept

◆ operator=()

template<std::size_t ThePrecision, std::size_t TheScale>
LIGHTWEIGHT_FORCE_INLINE constexpr SqlNumeric & Lightweight::SqlNumeric< ThePrecision, TheScale >::operator= ( std::floating_point auto  value)
inlineconstexprnoexcept

Assigns a floating point value to the numeric.

Definition at line 172 of file SqlNumeric.hpp.

References Lightweight::SqlNumeric< ThePrecision, TheScale >::assign().

◆ ToUnscaledValue()

template<std::size_t ThePrecision, std::size_t TheScale>
LIGHTWEIGHT_FORCE_INLINE Int128 Lightweight::SqlNumeric< ThePrecision, TheScale >::ToUnscaledValue ( ) const
inlinenoexcept

Converts the numeric to an unscaled integer value.

Along with ToString(), which renders from this value, it is one of the two accessors that do not divide through a floating-point type, and therefore carries every digit up to SqlMaxNumericPrecision on every toolchain. The floating-point accessors do not.

Returns
value * 10^Scale as a signed 128-bit integer.

Definition at line 185 of file SqlNumeric.hpp.

References Lightweight::SqlNumeric< ThePrecision, TheScale >::nativeValue, Lightweight::SqlNumeric< ThePrecision, TheScale >::Scale, and Lightweight::SqlNumeric< ThePrecision, TheScale >::sqlValue.

Referenced by Lightweight::SqlNumeric< ThePrecision, TheScale >::ToDouble(), Lightweight::SqlNumeric< ThePrecision, TheScale >::ToFloat(), Lightweight::SqlNumeric< ThePrecision, TheScale >::ToLongDouble(), and Lightweight::SqlNumeric< ThePrecision, TheScale >::ToString().

◆ ToFloat()

template<std::size_t ThePrecision, std::size_t TheScale>
LIGHTWEIGHT_FORCE_INLINE float Lightweight::SqlNumeric< ThePrecision, TheScale >::ToFloat ( ) const
inlinenoexcept

◆ ToDouble()

template<std::size_t ThePrecision, std::size_t TheScale>
LIGHTWEIGHT_FORCE_INLINE double Lightweight::SqlNumeric< ThePrecision, TheScale >::ToDouble ( ) const
inlinenoexcept

◆ ToLongDouble()

template<std::size_t ThePrecision, std::size_t TheScale>
LIGHTWEIGHT_FORCE_INLINE long double Lightweight::SqlNumeric< ThePrecision, TheScale >::ToLongDouble ( ) const
inlinenoexcept

◆ operator float()

template<std::size_t ThePrecision, std::size_t TheScale>
LIGHTWEIGHT_FORCE_INLINE Lightweight::SqlNumeric< ThePrecision, TheScale >::operator float ( ) const
inlineexplicitnoexcept

Converts the numeric to a floating point value.

Definition at line 218 of file SqlNumeric.hpp.

References Lightweight::SqlNumeric< ThePrecision, TheScale >::ToFloat().

◆ operator double()

template<std::size_t ThePrecision, std::size_t TheScale>
LIGHTWEIGHT_FORCE_INLINE Lightweight::SqlNumeric< ThePrecision, TheScale >::operator double ( ) const
inlineexplicitnoexcept

Converts the numeric to a double precision floating point value.

Definition at line 224 of file SqlNumeric.hpp.

References Lightweight::SqlNumeric< ThePrecision, TheScale >::ToDouble().

◆ operator long double()

template<std::size_t ThePrecision, std::size_t TheScale>
LIGHTWEIGHT_FORCE_INLINE Lightweight::SqlNumeric< ThePrecision, TheScale >::operator long double ( ) const
inlineexplicitnoexcept

Converts the numeric to a long double precision floating point value.

Definition at line 230 of file SqlNumeric.hpp.

References Lightweight::SqlNumeric< ThePrecision, TheScale >::ToLongDouble().

◆ ToString()

template<std::size_t ThePrecision, std::size_t TheScale>
LIGHTWEIGHT_FORCE_INLINE std::string Lightweight::SqlNumeric< ThePrecision, TheScale >::ToString ( ) const
inline

Converts the numeric to a string, exactly.

Rendered from the unscaled integer rather than by formatting ToLongDouble(), so every digit the carrier holds survives on every toolchain. Formatting through long double would drop the low digits wherever that type is narrow (53 bits on MSVC and on Clang for Apple Silicon) or wherever the standard library's formatter narrows to double regardless.

Returns
The value in plain decimal notation with exactly Scale fractional digits.

Definition at line 243 of file SqlNumeric.hpp.

References Lightweight::SqlNumeric< ThePrecision, TheScale >::Scale, and Lightweight::SqlNumeric< ThePrecision, TheScale >::ToUnscaledValue().

◆ operator<=>()

template<std::size_t ThePrecision, std::size_t TheScale>
LIGHTWEIGHT_FORCE_INLINE std::partial_ordering Lightweight::SqlNumeric< ThePrecision, TheScale >::operator<=> ( SqlNumeric< ThePrecision, TheScale > const &  other) const
inlinenoexcept

Three-way comparison operator.

Comparing two SqlNumeric values yields std::partial_ordering because the underlying conversion to double admits NaN inputs. In practice every value produced by this type is finite, so the result is totally ordered.

Definition at line 270 of file SqlNumeric.hpp.

References Lightweight::SqlNumeric< ThePrecision, TheScale >::ToDouble().

◆ operator==()

template<std::size_t ThePrecision, std::size_t TheScale>
template<std::size_t OtherPrecision, std::size_t OtherScale>
LIGHTWEIGHT_FORCE_INLINE bool Lightweight::SqlNumeric< ThePrecision, TheScale >::operator== ( SqlNumeric< OtherPrecision, OtherScale > const &  other) const
inlinenoexcept

Equality comparison operator.

Definition at line 277 of file SqlNumeric.hpp.

References Lightweight::SqlNumeric< ThePrecision, TheScale >::ToFloat().

Member Data Documentation

◆ Precision

template<std::size_t ThePrecision, std::size_t TheScale>
constexpr auto Lightweight::SqlNumeric< ThePrecision, TheScale >::Precision = ThePrecision
staticconstexpr

Number of total digits.

Definition at line 97 of file SqlNumeric.hpp.

Referenced by Lightweight::SqlNumeric< ThePrecision, TheScale >::assign().

◆ Scale

template<std::size_t ThePrecision, std::size_t TheScale>
constexpr auto Lightweight::SqlNumeric< ThePrecision, TheScale >::Scale = TheScale
staticconstexpr

◆ ColumnType

template<std::size_t ThePrecision, std::size_t TheScale>
constexpr auto Lightweight::SqlNumeric< ThePrecision, TheScale >::ColumnType = SqlColumnTypeDefinitions::Decimal { .precision = Precision, .scale = TheScale }
staticconstexpr

The SQL column type definition for this numeric type.

Definition at line 103 of file SqlNumeric.hpp.

◆ sqlValue

◆ nativeValue

template<std::size_t ThePrecision, std::size_t TheScale>
double Lightweight::SqlNumeric< ThePrecision, TheScale >::nativeValue {}

Cached native value for drivers without SQL_NUMERIC_STRUCT support.

Definition at line 123 of file SqlNumeric.hpp.

Referenced by Lightweight::SqlNumeric< ThePrecision, TheScale >::assign(), and Lightweight::SqlNumeric< ThePrecision, TheScale >::ToUnscaledValue().


The documentation for this struct was generated from the following file: