MySQL 9.1.0
Source Code Documentation
my_decimal.h File Reference

It is interface module to fixed precision decimals library. More...

#include <assert.h>
#include <stdlib.h>
#include <sys/types.h>
#include <algorithm>
#include <array>
#include "decimal.h"
#include "my_inttypes.h"
#include "my_macros.h"
#include "my_time_t.h"
#include "mysql/strings/dtoa.h"
#include "mysql/strings/m_ctype.h"

Go to the source code of this file.

Classes

class  my_decimal
 my_decimal class limits 'decimal_t' type to what we need in MySQL. More...
 

Functions

int my_decimal_int_part (uint precision, uint decimals)
 
void print_decimal (const my_decimal *dec)
 
void print_decimal_buff (const my_decimal *dec, const uchar *ptr, int length)
 
const char * dbug_decimal_as_string (char *buff, const my_decimal *val)
 
bool str_set_decimal (uint mask, const my_decimal *val, String *str, const CHARSET_INFO *cs, uint decimals)
 Converting decimal to string with character set conversion. More...
 
void max_my_decimal (my_decimal *to, int precision, int frac)
 
void max_internal_decimal (my_decimal *to)
 
int check_result_and_overflow (uint mask, int result, my_decimal *val)
 
uint my_decimal_length_to_precision (uint length, uint scale, bool unsigned_flag)
 
uint32 my_decimal_precision_to_length_no_truncation (uint precision, uint8 scale, bool unsigned_flag)
 
uint32 my_decimal_precision_to_length (uint precision, uint8 scale, bool unsigned_flag)
 
int my_decimal_string_length (const my_decimal *d)
 
int my_decimal_get_binary_size (uint precision, uint scale)
 
void my_decimal2decimal (const my_decimal *from, my_decimal *to)
 
int my_decimal2binary (uint mask, const my_decimal *d, uchar *bin, int prec, int scale)
 
int binary2my_decimal (uint mask, const uchar *bin, my_decimal *d, int prec, int scale)
 
int binary2my_decimal (uint mask, const uchar *bin, my_decimal *d, int prec, int scale, bool keep_prec)
 Decode DECIMAL from binary form. More...
 
int my_decimal_set_zero (my_decimal *d)
 
bool my_decimal_is_zero (const my_decimal *decimal_value)
 
int my_decimal_round (uint mask, const my_decimal *from, int scale, bool truncate, my_decimal *to)
 
int my_decimal_floor (uint mask, const my_decimal *from, my_decimal *to)
 
int my_decimal_ceiling (uint mask, const my_decimal *from, my_decimal *to)
 
int my_decimal2string (uint mask, const my_decimal *d, uint fixed_prec, uint fixed_dec, String *str)
 Converting decimal to string. More...
 
int my_decimal2string (uint mask, const my_decimal *d, String *str)
 
int my_decimal2int (uint mask, const my_decimal *d, bool unsigned_flag, longlong *l)
 
int my_decimal2double (uint, const my_decimal *d, double *result)
 
int my_decimal2lldiv_t (uint mask, const my_decimal *d, lldiv_t *to)
 
int str2my_decimal (uint mask, const char *str, my_decimal *d, const char **end)
 
int str2my_decimal (uint mask, const char *from, size_t length, const CHARSET_INFO *charset, my_decimal *decimal_value)
 
my_decimaldate2my_decimal (const MYSQL_TIME *ltime, my_decimal *dec)
 Convert datetime value to my_decimal in format YYYYMMDDhhmmss.ffffff. More...
 
my_decimaltime2my_decimal (const MYSQL_TIME *ltime, my_decimal *dec)
 Convert time value to my_decimal in format hhmmss.ffffff. More...
 
my_decimaltimeval2my_decimal (const my_timeval *tm, my_decimal *dec)
 Convert timeval value to my_decimal. More...
 
int double2my_decimal (uint mask, double val, my_decimal *d)
 
int int2my_decimal (uint mask, longlong i, bool unsigned_flag, my_decimal *d)
 
void my_decimal_neg (decimal_t *arg)
 
int my_decimal_add (uint mask, my_decimal *res, const my_decimal *a, const my_decimal *b)
 
int my_decimal_sub (uint mask, my_decimal *res, const my_decimal *a, const my_decimal *b)
 
int my_decimal_mul (uint mask, my_decimal *res, const my_decimal *a, const my_decimal *b)
 
int my_decimal_div (uint mask, my_decimal *res, const my_decimal *a, const my_decimal *b, int div_scale_inc)
 
int my_decimal_mod (uint mask, my_decimal *res, const my_decimal *a, const my_decimal *b)
 
int my_decimal_cmp (const my_decimal *a, const my_decimal *b)
 
bool operator< (const my_decimal &lhs, const my_decimal &rhs)
 
bool operator!= (const my_decimal &lhs, const my_decimal &rhs)
 
int my_decimal_intg (const my_decimal *a)
 
void my_decimal_trim (ulong *precision, uint *scale)
 

Variables

static constexpr int DECIMAL_LONGLONG_DIGITS {22}
 
static constexpr int DECIMAL_BUFF_LENGTH {9}
 maximum length of buffer in our big digits (uint32). More...
 
static constexpr int DECIMAL_MAX_POSSIBLE_PRECISION {DECIMAL_BUFF_LENGTH * 9}
 the number of digits that my_decimal can possibly contain More...
 
static constexpr int DECIMAL_MAX_PRECISION
 maximum guaranteed precision of number in decimal digits (number of our digits * number of decimal digits in one our big digit - number of decimal digits in one our big digit decreased by 1 (because we always put decimal point on the border of our big digits)) More...
 
static constexpr int DECIMAL_MAX_STR_LENGTH {DECIMAL_MAX_POSSIBLE_PRECISION + 2}
 maximum length of string representation (number of maximum decimal digits + 1 position for sign + 1 position for decimal point, no terminator) More...
 
static constexpr int DECIMAL_MAX_FIELD_SIZE {DECIMAL_MAX_PRECISION}
 maximum size of packet length. More...
 

Detailed Description

It is interface module to fixed precision decimals library.

Most functions use 'uint mask' as parameter, if during operation error which fit in this mask is detected then it will be processed automatically here. (errors are E_DEC_* constants, see include/decimal.h)

Most function are just inline wrappers around library calls

Function Documentation

◆ binary2my_decimal() [1/2]

int binary2my_decimal ( uint  mask,
const uchar bin,
my_decimal d,
int  prec,
int  scale 
)
inline

◆ binary2my_decimal() [2/2]

int binary2my_decimal ( uint  mask,
const uchar bin,
my_decimal d,
int  prec,
int  scale,
bool  keep_prec 
)
inline

Decode DECIMAL from binary form.

Parameters
maskError mask
binBinary string to decode
d[out] DECIMAL buffer
precPrecision of stored value
scaleScale of stored value
keep_precWhether to keep stored value's precision
Returns
conversion error

◆ check_result_and_overflow()

int check_result_and_overflow ( uint  mask,
int  result,
my_decimal val 
)
inline

◆ date2my_decimal()

my_decimal * date2my_decimal ( const MYSQL_TIME ltime,
my_decimal dec 
)

Convert datetime value to my_decimal in format YYYYMMDDhhmmss.ffffff.

Parameters
ltimeDate value to convert from.
decDecimal value to convert to.

◆ dbug_decimal_as_string()

const char * dbug_decimal_as_string ( char *  buff,
const my_decimal val 
)

◆ double2my_decimal()

int double2my_decimal ( uint  mask,
double  val,
my_decimal d 
)
inline

◆ int2my_decimal()

int int2my_decimal ( uint  mask,
longlong  i,
bool  unsigned_flag,
my_decimal d 
)
inline

◆ max_internal_decimal()

void max_internal_decimal ( my_decimal to)
inline

◆ max_my_decimal()

void max_my_decimal ( my_decimal to,
int  precision,
int  frac 
)
inline

◆ my_decimal2binary()

int my_decimal2binary ( uint  mask,
const my_decimal d,
uchar bin,
int  prec,
int  scale 
)

◆ my_decimal2decimal()

void my_decimal2decimal ( const my_decimal from,
my_decimal to 
)
inline

◆ my_decimal2double()

int my_decimal2double ( uint  ,
const my_decimal d,
double *  result 
)
inline

◆ my_decimal2int()

int my_decimal2int ( uint  mask,
const my_decimal d,
bool  unsigned_flag,
longlong l 
)
inline

◆ my_decimal2lldiv_t()

int my_decimal2lldiv_t ( uint  mask,
const my_decimal d,
lldiv_t *  to 
)
inline

◆ my_decimal2string() [1/2]

int my_decimal2string ( uint  mask,
const my_decimal d,
String str 
)
inline

◆ my_decimal2string() [2/2]

int my_decimal2string ( uint  mask,
const my_decimal d,
uint  fixed_prec,
uint  fixed_dec,
String str 
)

Converting decimal to string.

Convert given my_decimal to String; allocate buffer as needed.

Parameters
[in]maskwhat problems to warn on (mask of E_DEC_* values)
[in]dthe decimal to print
[in]fixed_precoverall number of digits if ZEROFILL, 0 otherwise
[in]fixed_decnumber of decimal places (if fixed_prec != 0)
[out]strwhere to store the resulting string
Returns
error code
Return values
E_DEC_OKok
E_DEC_TRUNCATEDtruncated
E_DEC_OVERFLOWoverflow
E_DEC_OOMout-of-memory

◆ my_decimal_add()

int my_decimal_add ( uint  mask,
my_decimal res,
const my_decimal a,
const my_decimal b 
)
inline

◆ my_decimal_ceiling()

int my_decimal_ceiling ( uint  mask,
const my_decimal from,
my_decimal to 
)
inline

◆ my_decimal_cmp()

int my_decimal_cmp ( const my_decimal a,
const my_decimal b 
)
inline
Return values
-1if a < b
1if a > b
0if a == b

◆ my_decimal_div()

int my_decimal_div ( uint  mask,
my_decimal res,
const my_decimal a,
const my_decimal b,
int  div_scale_inc 
)
inline

◆ my_decimal_floor()

int my_decimal_floor ( uint  mask,
const my_decimal from,
my_decimal to 
)
inline

◆ my_decimal_get_binary_size()

int my_decimal_get_binary_size ( uint  precision,
uint  scale 
)
inline

◆ my_decimal_int_part()

int my_decimal_int_part ( uint  precision,
uint  decimals 
)
inline

◆ my_decimal_intg()

int my_decimal_intg ( const my_decimal a)
inline

◆ my_decimal_is_zero()

bool my_decimal_is_zero ( const my_decimal decimal_value)
inline

◆ my_decimal_length_to_precision()

uint my_decimal_length_to_precision ( uint  length,
uint  scale,
bool  unsigned_flag 
)
inline

◆ my_decimal_mod()

int my_decimal_mod ( uint  mask,
my_decimal res,
const my_decimal a,
const my_decimal b 
)
inline

◆ my_decimal_mul()

int my_decimal_mul ( uint  mask,
my_decimal res,
const my_decimal a,
const my_decimal b 
)
inline

◆ my_decimal_neg()

void my_decimal_neg ( decimal_t arg)
inline

◆ my_decimal_precision_to_length()

uint32 my_decimal_precision_to_length ( uint  precision,
uint8  scale,
bool  unsigned_flag 
)
inline

◆ my_decimal_precision_to_length_no_truncation()

uint32 my_decimal_precision_to_length_no_truncation ( uint  precision,
uint8  scale,
bool  unsigned_flag 
)
inline

◆ my_decimal_round()

int my_decimal_round ( uint  mask,
const my_decimal from,
int  scale,
bool  truncate,
my_decimal to 
)
inline

◆ my_decimal_set_zero()

int my_decimal_set_zero ( my_decimal d)
inline

◆ my_decimal_string_length()

int my_decimal_string_length ( const my_decimal d)
inline

◆ my_decimal_sub()

int my_decimal_sub ( uint  mask,
my_decimal res,
const my_decimal a,
const my_decimal b 
)
inline

◆ my_decimal_trim()

void my_decimal_trim ( ulong *  precision,
uint *  scale 
)

◆ operator!=()

bool operator!= ( const my_decimal lhs,
const my_decimal rhs 
)
inline

◆ operator<()

bool operator< ( const my_decimal lhs,
const my_decimal rhs 
)
inline

◆ print_decimal()

void print_decimal ( const my_decimal dec)

◆ print_decimal_buff()

void print_decimal_buff ( const my_decimal dec,
const uchar ptr,
int  length 
)

◆ str2my_decimal() [1/2]

int str2my_decimal ( uint  mask,
const char *  from,
size_t  length,
const CHARSET_INFO charset,
my_decimal decimal_value 
)

◆ str2my_decimal() [2/2]

int str2my_decimal ( uint  mask,
const char *  str,
my_decimal d,
const char **  end 
)
inline

◆ str_set_decimal()

bool str_set_decimal ( uint  mask,
const my_decimal val,
String str,
const CHARSET_INFO cs,
uint  decimals 
)

Converting decimal to string with character set conversion.

Convert given my_decimal to String; allocate buffer as needed.

Parameters
[in]maskwhat problems to warn on (mask of E_DEC_* values)
[in]valthe decimal to print
[out]strwhere to store the resulting string
[in]cscharacter set
[in]decimalsround to desired number of decimals
Returns
error code
Return values
E_DEC_OKok
E_DEC_TRUNCATEDtruncated
E_DEC_OVERFLOWoverflow
E_DEC_OOMout-of-memory

Would be great to make it a method of the String class, but this would need to include my_decimal.h from sql_string.h and sql_string.cc, which is not desirable.

◆ time2my_decimal()

my_decimal * time2my_decimal ( const MYSQL_TIME ltime,
my_decimal dec 
)

Convert time value to my_decimal in format hhmmss.ffffff.

Parameters
ltimeDate value to convert from.
decDecimal value to convert to.

◆ timeval2my_decimal()

my_decimal * timeval2my_decimal ( const my_timeval tm,
my_decimal dec 
)

Convert timeval value to my_decimal.

Variable Documentation

◆ DECIMAL_BUFF_LENGTH

constexpr int DECIMAL_BUFF_LENGTH {9}
staticconstexpr

maximum length of buffer in our big digits (uint32).

◆ DECIMAL_LONGLONG_DIGITS

constexpr int DECIMAL_LONGLONG_DIGITS {22}
staticconstexpr

◆ DECIMAL_MAX_FIELD_SIZE

constexpr int DECIMAL_MAX_FIELD_SIZE {DECIMAL_MAX_PRECISION}
staticconstexpr

maximum size of packet length.

◆ DECIMAL_MAX_POSSIBLE_PRECISION

constexpr int DECIMAL_MAX_POSSIBLE_PRECISION {DECIMAL_BUFF_LENGTH * 9}
staticconstexpr

the number of digits that my_decimal can possibly contain

◆ DECIMAL_MAX_PRECISION

constexpr int DECIMAL_MAX_PRECISION
staticconstexpr
Initial value:
8 * 2}
static constexpr int DECIMAL_MAX_POSSIBLE_PRECISION
the number of digits that my_decimal can possibly contain
Definition: my_decimal.h:62

maximum guaranteed precision of number in decimal digits (number of our digits * number of decimal digits in one our big digit - number of decimal digits in one our big digit decreased by 1 (because we always put decimal point on the border of our big digits))

◆ DECIMAL_MAX_STR_LENGTH

constexpr int DECIMAL_MAX_STR_LENGTH {DECIMAL_MAX_POSSIBLE_PRECISION + 2}
staticconstexpr

maximum length of string representation (number of maximum decimal digits + 1 position for sign + 1 position for decimal point, no terminator)