31 #if defined(MYSQL_SERVER) || defined(EMBEDDED_LIBRARY)
32 #include "sql_string.h"
42 #define DECIMAL_LONGLONG_DIGITS 22
43 #define DECIMAL_LONG_DIGITS 10
44 #define DECIMAL_LONG3_DIGITS 8
47 #define DECIMAL_BUFF_LENGTH 9
50 #define DECIMAL_MAX_POSSIBLE_PRECISION (DECIMAL_BUFF_LENGTH * 9)
59 #define DECIMAL_MAX_PRECISION (DECIMAL_MAX_POSSIBLE_PRECISION - 8*2)
60 #define DECIMAL_MAX_SCALE 30
61 #define DECIMAL_NOT_SPECIFIED 31
67 #define DECIMAL_MAX_STR_LENGTH (DECIMAL_MAX_POSSIBLE_PRECISION + 2)
72 #define DECIMAL_MAX_FIELD_SIZE DECIMAL_MAX_PRECISION
75 inline uint my_decimal_size(uint precision, uint scale)
81 return decimal_size(precision, scale) + 1;
85 inline int my_decimal_int_part(uint precision, uint decimals)
87 return precision - ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : decimals);
107 #if !defined(DBUG_OFF)
113 #if !defined(DBUG_OFF)
115 static const int test_value= 123;
122 #if !defined(DBUG_OFF)
127 buffer[
i]= rhs.buffer[
i];
128 fix_buffer_pointer();
133 #if !defined(DBUG_OFF)
139 decimal_t::operator=(rhs);
141 buffer[
i]= rhs.buffer[
i];
142 fix_buffer_pointer();
148 #if !defined(DBUG_OFF)
167 DBUG_ASSERT(foo1 == test_value);
168 DBUG_ASSERT(foo2 == test_value);
171 void fix_buffer_pointer() { buf= buffer; }
173 bool sign()
const {
return decimal_t::sign; }
174 void sign(
bool s) { decimal_t::sign= s; }
175 uint precision()
const {
return intg + frac; }
187 void print_decimal_buff(
const my_decimal *dec,
const uchar* ptr,
int length);
188 const char *dbug_decimal_as_string(
char *buff,
const my_decimal *val);
190 #define dbug_decimal_as_string(A) NULL
193 bool str_set_decimal(uint mask,
const my_decimal *val, uint fixed_prec,
194 uint fixed_dec,
char filler,
String *str,
200 int decimal_operation_results(
int result);
202 inline int decimal_operation_results(
int result)
209 void max_my_decimal(
my_decimal *
to,
int precision,
int frac)
212 (frac <= DECIMAL_MAX_SCALE));
213 max_decimal(precision, frac, to);
216 inline void max_internal_decimal(
my_decimal *to)
221 inline int check_result(uint mask,
int result)
224 decimal_operation_results(result);
228 inline int check_result_and_overflow(uint mask,
int result,
my_decimal *val)
230 if (check_result(mask, result) & E_DEC_OVERFLOW)
232 bool sign= val->sign();
233 val->fix_buffer_pointer();
234 max_internal_decimal(val);
240 inline uint my_decimal_length_to_precision(uint length, uint scale,
244 DBUG_ASSERT(length || !scale);
245 return (uint) (length - (scale>0 ? 1:0) -
246 (unsigned_flag || !length ? 0:1));
249 inline uint32 my_decimal_precision_to_length_no_truncation(uint precision,
257 DBUG_ASSERT(precision || !scale);
258 return (uint32)(precision + (scale > 0 ? 1 : 0) +
259 (unsigned_flag || !precision ? 0 : 1));
262 inline uint32 my_decimal_precision_to_length(uint precision, uint8 scale,
269 DBUG_ASSERT(precision || !scale);
271 return my_decimal_precision_to_length_no_truncation(precision, scale,
276 int my_decimal_string_length(
const my_decimal *d)
279 return decimal_string_size(d);
284 int my_decimal_max_length(
const my_decimal *d)
287 return decimal_string_size(d) - 1;
292 int my_decimal_get_binary_size(uint precision, uint scale)
294 return decimal_bin_size((
int)precision, (
int)scale);
305 int my_decimal2binary(uint mask,
const my_decimal *d, uchar *bin,
int prec,
310 int binary2my_decimal(uint mask,
const uchar *bin,
my_decimal *d,
int prec,
313 return check_result(mask, bin2decimal(bin, d, prec, scale));
325 decimal_make_zero(static_cast<decimal_t*>(d));
331 bool my_decimal_is_zero(
const my_decimal *decimal_value)
333 return decimal_is_zero(decimal_value);
338 int my_decimal_round(uint mask,
const my_decimal *from,
int scale,
341 return check_result(mask, decimal_round(from, to, scale,
342 (truncate ? TRUNCATE : HALF_UP)));
349 return check_result(mask, decimal_round(from, to, 0, FLOOR));
356 return check_result(mask, decimal_round(from, to, 0, CEILING));
363 return str_set_decimal(E_DEC_FATAL_ERROR, val, 0, 0, 0, str, cs);
368 int my_decimal2string(uint mask,
const my_decimal *d, uint fixed_prec,
369 uint fixed_dec,
char filler,
String *str);
373 int my_decimal2int(uint mask,
const my_decimal *d, my_bool unsigned_flag,
378 decimal_round(d, &rounded, 0, HALF_UP);
379 return check_result(mask, (unsigned_flag ?
380 decimal2ulonglong(&rounded, (ulonglong *)l) :
381 decimal2longlong(&rounded, l)));
386 int my_decimal2double(uint,
const my_decimal *d,
double *result)
389 return decimal2double(d, result);
393 inline int my_decimal2lldiv_t(uint mask,
const my_decimal *d, lldiv_t *to)
395 return check_result(mask, decimal2lldiv_t(d, to));
399 inline int str2my_decimal(uint mask,
const char *str,
402 return check_result_and_overflow(mask, string2decimal(str, d, end), d);
406 int str2my_decimal(uint mask,
const char *from, uint length,
409 #if defined(MYSQL_SERVER) || defined(EMBEDDED_LIBRARY)
413 return str2my_decimal(mask, str->ptr(), str->length(), str->charset(), d);
424 int double2my_decimal(uint mask,
double val,
my_decimal *d)
426 return check_result_and_overflow(mask, double2decimal(val, d), d);
431 int int2my_decimal(uint mask, longlong
i, my_bool unsigned_flag,
my_decimal *d)
433 return check_result(mask, (unsigned_flag ?
434 ulonglong2decimal((ulonglong)i, d) :
435 longlong2decimal(i, d)));
442 if (decimal_is_zero(arg))
455 return check_result_and_overflow(mask,
456 decimal_add(a, b, res),
465 return check_result_and_overflow(mask,
466 decimal_sub(a, b, res),
475 return check_result_and_overflow(mask,
476 decimal_mul(a, b, res),
485 return check_result_and_overflow(mask,
486 decimal_div(a, b, res, div_scale_inc),
495 return check_result_and_overflow(mask,
496 decimal_mod(a, b, res),
508 return decimal_cmp(a, b);
515 return decimal_intg(a);
519 void my_decimal_trim(ulong *precision, uint *scale);