120
120
/** maximum length of buffer in our big digits (uint32_t). */
121
121
#define DECIMAL_BUFF_LENGTH 9
123
/* the number of digits that my_decimal can possibly contain */
123
/* the number of digits that type::Decimal can possibly contain */
124
124
#define DECIMAL_MAX_POSSIBLE_PRECISION (DECIMAL_BUFF_LENGTH * 9)
146
146
#define DECIMAL_MAX_FIELD_SIZE DECIMAL_MAX_PRECISION
148
inline int my_decimal_int_part(uint32_t precision, uint32_t decimals)
148
inline int class_decimal_int_part(uint32_t precision, uint32_t decimals)
150
150
return precision - ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : decimals);
155
my_decimal class limits 'decimal_t' type to what we need in MySQL.
156
type::Decimal class limits 'decimal_t' type to what we need in MySQL.
157
158
It contains internally all necessary space needed by the instance so
158
159
no extra memory is needed. One should call fix_buffer_pointer() function
159
when he moves my_decimal objects in memory.
160
when he moves type::Decimal objects in memory.
162
class my_decimal :public decimal_t
163
class Decimal :public decimal_t
164
165
decimal_digit_t buffer[DECIMAL_BUFF_LENGTH];
186
187
uint32_t precision() const { return intg + frac; }
189
std::ostream& operator<<(std::ostream& output, const my_decimal &dec);
192
std::ostream& operator<<(std::ostream& output, const type::Decimal &dec);
191
194
int decimal_operation_results(int result);
193
inline void max_my_decimal(my_decimal *to, int precision, int frac)
196
inline void max_Decimal(type::Decimal *to, int precision, int frac)
195
198
assert((precision <= DECIMAL_MAX_PRECISION)&&
196
199
(frac <= DECIMAL_MAX_SCALE));
197
200
max_decimal(precision, frac, (decimal_t*) to);
200
inline void max_internal_decimal(my_decimal *to)
203
inline void max_internal_decimal(type::Decimal *to)
202
max_my_decimal(to, DECIMAL_MAX_PRECISION, 0);
205
max_Decimal(to, DECIMAL_MAX_PRECISION, 0);
205
208
inline int check_result(uint32_t mask, int result)
212
inline int check_result_and_overflow(uint32_t mask, int result, my_decimal *val)
215
inline int check_result_and_overflow(uint32_t mask, int result, type::Decimal *val)
214
217
if (check_result(mask, result) & E_DEC_OVERFLOW)
224
inline uint32_t my_decimal_length_to_precision(uint32_t length, uint32_t scale,
227
inline uint32_t class_decimal_length_to_precision(uint32_t length, uint32_t scale,
225
228
bool unsigned_flag)
227
230
return (uint32_t) (length - (scale>0 ? 1:0) - (unsigned_flag ? 0:1));
230
inline uint32_t my_decimal_precision_to_length(uint32_t precision, uint8_t scale,
233
inline uint32_t class_decimal_precision_to_length(uint32_t precision, uint8_t scale,
231
234
bool unsigned_flag)
233
236
set_if_smaller(precision, (uint32_t)DECIMAL_MAX_PRECISION);
238
int my_decimal_string_length(const my_decimal *d)
241
int class_decimal_string_length(const type::Decimal *d)
240
243
return decimal_string_size(d);
245
int my_decimal_max_length(const my_decimal *d)
248
int class_decimal_max_length(const type::Decimal *d)
247
250
/* -1 because we do not count \0 */
248
251
return decimal_string_size(d) - 1;
253
int my_decimal_get_binary_size(uint32_t precision, uint32_t scale)
256
int class_decimal_get_binary_size(uint32_t precision, uint32_t scale)
255
258
return decimal_bin_size(static_cast<int>(precision), static_cast<int>(scale));
260
void my_decimal2decimal(const my_decimal *from, my_decimal *to)
263
void class_decimal2decimal(const type::Decimal *from, type::Decimal *to)
263
266
to->fix_buffer_pointer();
267
int my_decimal2binary(uint32_t mask, const my_decimal *d, unsigned char *bin, int prec,
270
int class_decimal2binary(uint32_t mask, const type::Decimal *d, unsigned char *bin, int prec,
272
int binary2my_decimal(uint32_t mask, const unsigned char *bin, my_decimal *d, int prec,
275
int binary2_class_decimal(uint32_t mask, const unsigned char *bin, type::Decimal *d, int prec,
275
278
return check_result(mask, bin2decimal(bin, static_cast<decimal_t*>(d), prec, scale));
288
bool my_decimal_is_zero(const my_decimal *decimal_value)
291
bool class_decimal_is_zero(const type::Decimal *decimal_value)
290
293
return decimal_is_zero(static_cast<const decimal_t*>(decimal_value));
295
int my_decimal_round(uint32_t mask, const my_decimal *from, int scale,
296
bool truncate, my_decimal *to)
298
int class_decimal_round(uint32_t mask, const type::Decimal *from, int scale,
299
bool truncate, type::Decimal *to)
298
301
return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, scale,
299
302
(truncate ? TRUNCATE : HALF_UP)));
304
int my_decimal_floor(uint32_t mask, const my_decimal *from, my_decimal *to)
307
int class_decimal_floor(uint32_t mask, const type::Decimal *from, type::Decimal *to)
306
309
return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, 0, FLOOR));
311
int my_decimal_ceiling(uint32_t mask, const my_decimal *from, my_decimal *to)
314
int class_decimal_ceiling(uint32_t mask, const type::Decimal *from, type::Decimal *to)
313
316
return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, 0, CEILING));
317
int my_decimal2string(uint32_t mask, const my_decimal *d, uint32_t fixed_prec,
320
int class_decimal2string(uint32_t mask, const type::Decimal *d, uint32_t fixed_prec,
318
321
uint32_t fixed_dec, char filler, String *str);
321
int my_decimal2int(uint32_t mask, const my_decimal *d, bool unsigned_flag,
324
int class_decimal2int(uint32_t mask, const type::Decimal *d, bool unsigned_flag,
327
type::Decimal rounded;
325
328
/* decimal_round can return only E_DEC_TRUNCATED */
326
329
decimal_round(static_cast<const decimal_t*>(d), &rounded, 0, HALF_UP);
327
330
return check_result(mask, (unsigned_flag ?
334
int my_decimal2double(uint32_t, const my_decimal *d, double *result)
337
int class_decimal2double(uint32_t, const type::Decimal *d, double *result)
336
339
/* No need to call check_result as this will always succeed */
337
340
return decimal2double(static_cast<const decimal_t*>(d), result);
342
int str2my_decimal(uint32_t mask, char *str, my_decimal *d, char **end)
345
int str2_class_decimal(uint32_t mask, char *str, type::Decimal *d, char **end)
344
347
return check_result_and_overflow(mask, string2decimal(str, static_cast<decimal_t*>(d),end),
349
int str2my_decimal(uint32_t mask, const char *from, uint32_t length,
350
const CHARSET_INFO * charset, my_decimal *decimal_value);
352
int str2_class_decimal(uint32_t mask, const char *from, uint32_t length,
353
const CHARSET_INFO * charset, type::Decimal *decimal_value);
353
int string2my_decimal(uint32_t mask, const String *str, my_decimal *d)
356
int string2_class_decimal(uint32_t mask, const String *str, type::Decimal *d)
355
return str2my_decimal(mask, str->ptr(), str->length(), str->charset(), d);
358
return str2_class_decimal(mask, str->ptr(), str->length(), str->charset(), d);
359
my_decimal *date2my_decimal(DRIZZLE_TIME *ltime, my_decimal *dec);
362
type::Decimal *date2_class_decimal(type::Time *ltime, type::Decimal *dec);
363
int double2my_decimal(uint32_t mask, double val, my_decimal *d)
366
int double2_class_decimal(uint32_t mask, double val, type::Decimal *d)
365
368
return check_result_and_overflow(mask, double2decimal(val, static_cast<decimal_t*>(d)), d);
370
int int2my_decimal(uint32_t mask, int64_t i, bool unsigned_flag, my_decimal *d)
373
int int2_class_decimal(uint32_t mask, int64_t i, bool unsigned_flag, type::Decimal *d)
372
375
return check_result(mask, (unsigned_flag ?
373
376
uint64_t2decimal(static_cast<uint64_t>(i), d) :
391
int my_decimal_add(uint32_t mask, my_decimal *res, const my_decimal *a,
394
int class_decimal_add(uint32_t mask, type::Decimal *res, const type::Decimal *a,
395
const type::Decimal *b)
394
397
return check_result_and_overflow(mask,
395
398
decimal_add(static_cast<const decimal_t*>(a),
402
int my_decimal_sub(uint32_t mask, my_decimal *res, const my_decimal *a,
405
int class_decimal_sub(uint32_t mask, type::Decimal *res, const type::Decimal *a,
406
const type::Decimal *b)
405
408
return check_result_and_overflow(mask,
406
409
decimal_sub(static_cast<const decimal_t*>(a),
413
int my_decimal_mul(uint32_t mask, my_decimal *res, const my_decimal *a,
416
int class_decimal_mul(uint32_t mask, type::Decimal *res, const type::Decimal *a,
417
const type::Decimal *b)
416
419
return check_result_and_overflow(mask,
417
420
decimal_mul(static_cast<const decimal_t*>(a),
424
int my_decimal_div(uint32_t mask, my_decimal *res, const my_decimal *a,
425
const my_decimal *b, int div_scale_inc)
427
int class_decimal_div(uint32_t mask, type::Decimal *res, const type::Decimal *a,
428
const type::Decimal *b, int div_scale_inc)
427
430
return check_result_and_overflow(mask,
428
431
decimal_div(static_cast<const decimal_t*>(a),
436
int my_decimal_mod(uint32_t mask, my_decimal *res, const my_decimal *a,
439
int class_decimal_mod(uint32_t mask, type::Decimal *res, const type::Decimal *a,
440
const type::Decimal *b)
439
442
return check_result_and_overflow(mask,
440
443
decimal_mod(static_cast<const decimal_t*>(a),
448
451
-1 if a<b, 1 if a>b and 0 if a==b
451
int my_decimal_cmp(const my_decimal *a, const my_decimal *b)
454
int class_decimal_cmp(const type::Decimal *a, const type::Decimal *b)
453
456
return decimal_cmp(static_cast<const decimal_t*>(a),
454
457
static_cast<const decimal_t*>(b));
459
int my_decimal_intg(const my_decimal *a)
462
int class_decimal_intg(const type::Decimal *a)
461
464
return decimal_intg(static_cast<const decimal_t*>(a));
465
void my_decimal_trim(uint32_t *precision, uint32_t *scale);
468
void class_decimal_trim(uint32_t *precision, uint32_t *scale);
467
470
} /* namespace drizzled */