1
/* Copyright (C) 2005-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
23
It is interface module to fixed precision decimals library.
21
Most functions use 'uint mask' as parameter, if during operation error
25
Most functions use 'uint32_t mask' as parameter, if during operation error
22
26
which fit in this mask is detected then it will be processed automatically
23
27
here. (errors are E_DEC_* constants, see include/decimal.h)
28
32
#ifndef my_decimal_h
29
33
#define my_decimal_h
32
39
#include <mystrings/decimal.h>
40
#include <mysys/my_time.h>
41
#include <drizzled/sql_string.h>
35
48
#define DECIMAL_LONGLONG_DIGITS 22
36
49
#define DECIMAL_LONG_DIGITS 10
65
78
#define DECIMAL_MAX_FIELD_SIZE DECIMAL_MAX_PRECISION
68
inline uint my_decimal_size(uint precision, uint scale)
81
inline uint32_t my_decimal_size(uint32_t precision, uint32_t scale)
71
84
Always allocate more space to allow library to put decimal point
100
113
len= DECIMAL_BUFF_LENGTH;
102
#if !defined (HAVE_purify)
115
#if !defined (HAVE_purify)
103
116
/* Set buffer to 'random' value to find wrong buffer usage */
104
for (uint i= 0; i < DECIMAL_BUFF_LENGTH; i++)
117
for (uint32_t i= 0; i < DECIMAL_BUFF_LENGTH; i++)
114
127
bool sign() const { return decimal_t::sign; }
115
128
void sign(bool s) { decimal_t::sign= s; }
116
uint precision() const { return intg + frac; }
129
uint32_t precision() const { return intg + frac; }
119
#ifndef DRIZZLE_CLIENT
120
132
int decimal_operation_results(int result);
122
inline int decimal_operation_results(int result)
126
#endif /*DRIZZLE_CLIENT*/
129
135
void max_my_decimal(my_decimal *to, int precision, int frac)
138
144
max_my_decimal(to, DECIMAL_MAX_PRECISION, 0);
141
inline int check_result(uint mask, int result)
147
inline int check_result(uint32_t mask, int result)
143
149
if (result & mask)
144
150
decimal_operation_results(result);
148
inline int check_result_and_overflow(uint mask, int result, my_decimal *val)
154
inline int check_result_and_overflow(uint32_t mask, int result, my_decimal *val)
150
156
if (check_result(mask, result) & E_DEC_OVERFLOW)
160
inline uint my_decimal_length_to_precision(uint length, uint scale,
166
inline uint32_t my_decimal_length_to_precision(uint32_t length, uint32_t scale,
161
167
bool unsigned_flag)
163
169
return (uint) (length - (scale>0 ? 1:0) - (unsigned_flag ? 0:1));
166
inline uint32_t my_decimal_precision_to_length(uint precision, uint8_t scale,
172
inline uint32_t my_decimal_precision_to_length(uint32_t precision, uint8_t scale,
167
173
bool unsigned_flag)
169
175
set_if_smaller(precision, DECIMAL_MAX_PRECISION);
189
int my_decimal_get_binary_size(uint precision, uint scale)
195
int my_decimal_get_binary_size(uint32_t precision, uint32_t scale)
191
197
return decimal_bin_size((int)precision, (int)scale);
203
int my_decimal2binary(uint mask, const my_decimal *d, uchar *bin, int prec,
209
int my_decimal2binary(uint32_t mask, const my_decimal *d, unsigned char *bin, int prec,
208
int binary2my_decimal(uint mask, const uchar *bin, my_decimal *d, int prec,
214
int binary2my_decimal(uint32_t mask, const unsigned char *bin, my_decimal *d, int prec,
211
217
return check_result(mask, bin2decimal(bin, (decimal_t*) d, prec, scale));
231
int my_decimal_round(uint mask, const my_decimal *from, int scale,
237
int my_decimal_round(uint32_t mask, const my_decimal *from, int scale,
232
238
bool truncate, my_decimal *to)
234
240
return check_result(mask, decimal_round((decimal_t*) from, to, scale,
235
(truncate ? TRUNCATE : HALF_UP)));
241
(truncate ? TRUNCATE : HALF_UP)));
240
int my_decimal_floor(uint mask, const my_decimal *from, my_decimal *to)
246
int my_decimal_floor(uint32_t mask, const my_decimal *from, my_decimal *to)
242
248
return check_result(mask, decimal_round((decimal_t*) from, to, 0, FLOOR));
247
int my_decimal_ceiling(uint mask, const my_decimal *from, my_decimal *to)
253
int my_decimal_ceiling(uint32_t mask, const my_decimal *from, my_decimal *to)
249
255
return check_result(mask, decimal_round((decimal_t*) from, to, 0, CEILING));
253
#ifndef DRIZZLE_CLIENT
254
int my_decimal2string(uint mask, const my_decimal *d, uint fixed_prec,
255
uint fixed_dec, char filler, String *str);
259
int my_decimal2string(uint32_t mask, const my_decimal *d, uint32_t fixed_prec,
260
uint32_t fixed_dec, char filler, String *str);
259
int my_decimal2int(uint mask, const my_decimal *d, bool unsigned_flag,
263
int my_decimal2int(uint32_t mask, const my_decimal *d, bool unsigned_flag,
262
266
my_decimal rounded;
263
267
/* decimal_round can return only E_DEC_TRUNCATED */
272
int my_decimal2double(uint mask __attribute__((unused)),
273
const my_decimal *d, double *result)
276
int my_decimal2double(uint32_t, const my_decimal *d, double *result)
275
278
/* No need to call check_result as this will always succeed */
276
279
return decimal2double((decimal_t*) d, result);
281
int str2my_decimal(uint mask, char *str, my_decimal *d, char **end)
284
int str2my_decimal(uint32_t mask, char *str, my_decimal *d, char **end)
283
286
return check_result_and_overflow(mask, string2decimal(str,(decimal_t*)d,end),
288
int str2my_decimal(uint mask, const char *from, uint length,
291
int str2my_decimal(uint32_t mask, const char *from, uint32_t length,
289
292
const CHARSET_INFO * charset, my_decimal *decimal_value);
291
#if defined(DRIZZLE_SERVER)
293
int string2my_decimal(uint mask, const String *str, my_decimal *d)
295
int string2my_decimal(uint32_t mask, const String *str, my_decimal *d)
295
297
return str2my_decimal(mask, str->ptr(), str->length(), str->charset(), d);
299
301
my_decimal *date2my_decimal(DRIZZLE_TIME *ltime, my_decimal *dec);
302
#endif /*defined(DRIZZLE_SERVER) */
305
int double2my_decimal(uint mask, double val, my_decimal *d)
305
int double2my_decimal(uint32_t mask, double val, my_decimal *d)
307
307
return check_result_and_overflow(mask, double2decimal(val, (decimal_t*)d), d);
312
int int2my_decimal(uint mask, int64_t i, bool unsigned_flag, my_decimal *d)
312
int int2my_decimal(uint32_t mask, int64_t i, bool unsigned_flag, my_decimal *d)
314
314
return check_result(mask, (unsigned_flag ?
315
315
uint64_t2decimal((uint64_t)i, d) :
333
int my_decimal_add(uint mask, my_decimal *res, const my_decimal *a,
333
int my_decimal_add(uint32_t mask, my_decimal *res, const my_decimal *a,
334
334
const my_decimal *b)
336
336
return check_result_and_overflow(mask,
343
int my_decimal_sub(uint mask, my_decimal *res, const my_decimal *a,
343
int my_decimal_sub(uint32_t mask, my_decimal *res, const my_decimal *a,
344
344
const my_decimal *b)
346
346
return check_result_and_overflow(mask,
353
int my_decimal_mul(uint mask, my_decimal *res, const my_decimal *a,
353
int my_decimal_mul(uint32_t mask, my_decimal *res, const my_decimal *a,
354
354
const my_decimal *b)
356
356
return check_result_and_overflow(mask,
363
int my_decimal_div(uint mask, my_decimal *res, const my_decimal *a,
363
int my_decimal_div(uint32_t mask, my_decimal *res, const my_decimal *a,
364
364
const my_decimal *b, int div_scale_inc)
366
366
return check_result_and_overflow(mask,
374
int my_decimal_mod(uint mask, my_decimal *res, const my_decimal *a,
374
int my_decimal_mod(uint32_t mask, my_decimal *res, const my_decimal *a,
375
375
const my_decimal *b)
377
377
return check_result_and_overflow(mask,