~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mystrings/decimal.h

  • Committer: Mark Atwood
  • Date: 2008-10-03 01:39:40 UTC
  • mto: This revision was merged to the branch mainline in revision 437.
  • Revision ID: mark@fallenpegasus.com-20081003013940-mvefjo725dltz41h
rename logging_noop to logging_query

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
#ifndef DRIZZLED_TYPE_DECIMAL_H
17
 
#define DRIZZLED_TYPE_DECIMAL_H
18
 
#include <assert.h>
19
 
#include <drizzled/sql_string.h>
20
 
#include <drizzled/definitions.h>
21
 
#include <drizzled/type/time.h>
22
 
namespace drizzled
23
 
{
 
16
#ifndef _decimal_h
 
17
#define _decimal_h
24
18
 
25
19
typedef enum
26
 
{
27
 
  TRUNCATE= 0,
28
 
  HALF_EVEN,
29
 
  HALF_UP,
30
 
  CEILING,
31
 
  FLOOR
32
 
} decimal_round_mode;
 
20
{TRUNCATE=0, HALF_EVEN, HALF_UP, CEILING, FLOOR}
 
21
  decimal_round_mode;
33
22
typedef int32_t decimal_digit_t;
34
23
 
35
 
struct decimal_t {
 
24
typedef struct st_decimal_t {
36
25
  int    intg, frac, len;
37
26
  bool sign;
38
27
  decimal_digit_t *buf;
39
 
 
40
 
  /* set a decimal_t to zero */
41
 
  inline void set_zero()
42
 
  {                                                         
43
 
    buf[0]= 0;
44
 
    intg= 1;
45
 
    frac= 0;
46
 
    sign= 0; 
47
 
  }
48
 
 
49
 
  /* negate a decimal */
50
 
  inline void negate()
51
 
  {
52
 
    sign^=1;
53
 
  }
54
 
 
55
 
  int isZero() const;
56
 
 
57
 
};
 
28
} decimal_t;
58
29
 
59
30
int internal_str2dec(char *from, decimal_t *to, char **end,
60
31
                     bool fixed);
61
 
int decimal2string(const decimal_t *from, char *to, int *to_len,
 
32
int decimal2string(decimal_t *from, char *to, int *to_len,
62
33
                   int fixed_precision, int fixed_decimals,
63
34
                   char filler);
64
 
int decimal2uint64_t(const decimal_t *from, uint64_t *to);
65
 
int uint64_t2decimal(const uint64_t from, decimal_t *to);
66
 
int decimal2int64_t(const decimal_t *from, int64_t *to);
67
 
int int64_t2decimal(const int64_t from, decimal_t *to);
68
 
int decimal2double(const decimal_t *from, double *to);
69
 
int double2decimal(const double from, decimal_t *to);
 
35
int decimal2uint64_t(decimal_t *from, uint64_t *to);
 
36
int uint64_t2decimal(uint64_t from, decimal_t *to);
 
37
int decimal2int64_t(decimal_t *from, int64_t *to);
 
38
int int64_t2decimal(int64_t from, decimal_t *to);
 
39
int decimal2double(decimal_t *from, double *to);
 
40
int double2decimal(double from, decimal_t *to);
70
41
int decimal_actual_fraction(decimal_t *from);
71
 
int decimal2bin(const decimal_t *from, unsigned char *to, int precision, int scale);
72
 
int bin2decimal(const unsigned char *from, decimal_t *to, int precision, int scale);
 
42
int decimal2bin(decimal_t *from, uchar *to, int precision, int scale);
 
43
int bin2decimal(const uchar *from, decimal_t *to, int precision, int scale);
73
44
 
 
45
int decimal_size(int precision, int scale);
74
46
int decimal_bin_size(int precision, int scale);
 
47
int decimal_result_size(decimal_t *from1, decimal_t *from2, char op,
 
48
                        int param);
75
49
 
76
 
int decimal_intg(const decimal_t *from);
77
 
int decimal_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
78
 
int decimal_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
79
 
int decimal_cmp(const decimal_t *from1, const decimal_t *from2);
80
 
int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
81
 
int decimal_div(const decimal_t *from1, const decimal_t *from2, decimal_t *to,
 
50
int decimal_intg(decimal_t *from);
 
51
int decimal_add(decimal_t *from1, decimal_t *from2, decimal_t *to);
 
52
int decimal_sub(decimal_t *from1, decimal_t *from2, decimal_t *to);
 
53
int decimal_cmp(decimal_t *from1, decimal_t *from2);
 
54
int decimal_mul(decimal_t *from1, decimal_t *from2, decimal_t *to);
 
55
int decimal_div(decimal_t *from1, decimal_t *from2, decimal_t *to,
82
56
                int scale_incr);
83
 
int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
84
 
int decimal_round(const decimal_t *from, decimal_t *to, int new_scale,
 
57
int decimal_mod(decimal_t *from1, decimal_t *from2, decimal_t *to);
 
58
int decimal_round(decimal_t *from, decimal_t *to, int new_scale,
85
59
                  decimal_round_mode mode);
 
60
int decimal_is_zero(decimal_t *from);
86
61
void max_decimal(int precision, int frac, decimal_t *to);
87
62
 
88
 
inline int string2decimal(char *from, decimal_t *to, char **end)
89
 
{
90
 
  return internal_str2dec(from, to, end, false);
91
 
}
 
63
#define string2decimal(A,B,C) internal_str2dec((A), (B), (C), 0)
 
64
#define string2decimal_fixed(A,B,C) internal_str2dec((A), (B), (C), 1)
 
65
 
 
66
/* set a decimal_t to zero */
 
67
 
 
68
#define decimal_make_zero(dec)        do {                \
 
69
                                        (dec)->buf[0]=0;    \
 
70
                                        (dec)->intg=1;      \
 
71
                                        (dec)->frac=0;      \
 
72
                                        (dec)->sign=0;      \
 
73
                                      } while(0)
92
74
 
93
75
/*
94
76
  returns the length of the buffer to hold string representation
95
77
  of the decimal (including decimal dot, possible sign and \0)
96
78
*/
97
79
 
98
 
inline int decimal_string_size(const decimal_t *dec)
99
 
{
100
 
  return (dec->intg ? dec->intg : 1) + dec->frac + (dec->frac > 0) + 2;
101
 
}
 
80
#define decimal_string_size(dec) (((dec)->intg ? (dec)->intg : 1) + \
 
81
                                  (dec)->frac + ((dec)->frac > 0) + 2)
 
82
 
 
83
/* negate a decimal */
 
84
#define decimal_neg(dec) do { (dec)->sign^=1; } while(0)
102
85
 
103
86
/*
104
87
  conventions:
120
103
#define E_DEC_ERROR            31
121
104
#define E_DEC_FATAL_ERROR      30
122
105
 
123
 
 
124
 
#define DECIMAL_LONGLONG_DIGITS 22
125
 
 
126
 
/** maximum length of buffer in our big digits (uint32_t). */
127
 
#define DECIMAL_BUFF_LENGTH 9
128
 
 
129
 
/* the number of digits that type::Decimal can possibly contain */
130
 
#define DECIMAL_MAX_POSSIBLE_PRECISION (DECIMAL_BUFF_LENGTH * 9)
131
 
 
132
 
 
133
 
/**
134
 
  maximum guaranteed precision of number in decimal digits (number of our
135
 
  digits * number of decimal digits in one our big digit - number of decimal
136
 
  digits in one our big digit decreased by 1 (because we always put decimal
137
 
  point on the border of our big digits))
138
 
*/
139
 
#define DECIMAL_MAX_PRECISION (DECIMAL_MAX_POSSIBLE_PRECISION - 8*2)
140
 
#define DECIMAL_MAX_SCALE 30
141
 
#define DECIMAL_NOT_SPECIFIED 31
142
 
 
143
 
/**
144
 
  maximum length of string representation (number of maximum decimal
145
 
  digits + 1 position for sign + 1 position for decimal point)
146
 
*/
147
 
#define DECIMAL_MAX_STR_LENGTH (DECIMAL_MAX_POSSIBLE_PRECISION + 2)
148
 
 
149
 
namespace type {
150
 
class Decimal;
151
 
}
152
 
 
153
 
inline int class_decimal_int_part(uint32_t precision, uint32_t decimals)
154
 
{
155
 
  return precision - ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : decimals);
156
 
}
157
 
 
158
 
int decimal_operation_results(int result);
159
 
 
160
 
inline void max_Decimal(type::Decimal *to, int precision, int frac)
161
 
{
162
 
  assert((precision <= DECIMAL_MAX_PRECISION)&&
163
 
              (frac <= DECIMAL_MAX_SCALE));
164
 
  max_decimal(precision, frac, (decimal_t*) to);
165
 
}
166
 
 
167
 
inline void max_internal_decimal(type::Decimal *to)
168
 
{
169
 
  max_Decimal(to, DECIMAL_MAX_PRECISION, 0);
170
 
}
171
 
 
172
 
inline int check_result(uint32_t mask, int result)
173
 
{
174
 
  if (result & mask)
175
 
    decimal_operation_results(result);
176
 
  return result;
177
 
}
178
 
 
179
 
namespace type {
180
 
/**
181
 
  type Decimal class limits 'decimal_t' type to what we need in MySQL.
182
 
 
183
 
  It contains internally all necessary space needed by the instance so
184
 
  no extra memory is needed. One should call fix_buffer_pointer() function
185
 
  when he moves type::Decimal objects in memory.
186
 
*/
187
 
 
188
 
class Decimal : public decimal_t
189
 
{
190
 
  decimal_digit_t buffer[DECIMAL_BUFF_LENGTH];
191
 
 
192
 
public:
193
 
 
194
 
  void init()
195
 
  {
196
 
    len= DECIMAL_BUFF_LENGTH;
197
 
    buf= buffer;
198
 
#if !defined (HAVE_VALGRIND)
199
 
    /* Set buffer to 'random' value to find wrong buffer usage */
200
 
    for (uint32_t i= 0; i < DECIMAL_BUFF_LENGTH; i++)
201
 
      buffer[i]= i;
202
106
#endif
203
 
  }
204
 
 
205
 
  Decimal()
206
 
  {
207
 
    init();
208
 
  }
209
 
 
210
 
  void fix_buffer_pointer() { buf= buffer; }
211
 
  bool sign() const { return decimal_t::sign; }
212
 
  void sign(bool s) { decimal_t::sign= s; }
213
 
  uint32_t precision() const { return intg + frac; }
214
 
 
215
 
  int val_int32(uint32_t mask, bool unsigned_flag, int64_t *l) const
216
 
  {
217
 
    type::Decimal rounded;
218
 
    /* decimal_round can return only E_DEC_TRUNCATED */
219
 
    decimal_round(static_cast<const decimal_t*>(this), &rounded, 0, HALF_UP);
220
 
    return check_result(mask, (unsigned_flag ?
221
 
                               decimal2uint64_t(&rounded, reinterpret_cast<uint64_t *>(l)) :
222
 
                               decimal2int64_t(&rounded, l)));
223
 
  }
224
 
 
225
 
  int string_length() const
226
 
  {
227
 
    return decimal_string_size(this);
228
 
  }
229
 
 
230
 
  int val_binary(uint32_t mask, unsigned char *bin, int prec, int scale) const;
231
 
 
232
 
  int store(uint32_t mask, const char *from, uint32_t length, const CHARSET_INFO * charset);
233
 
 
234
 
  int store(uint32_t mask, char *str, char **end)
235
 
  {
236
 
    return check_result_and_overflow(mask, string2decimal(str, static_cast<decimal_t*>(this), end));
237
 
  }
238
 
 
239
 
  int store(uint32_t mask, const String *str)
240
 
  {
241
 
    return store(mask, str->ptr(), str->length(), str->charset());
242
 
  }
243
 
 
244
 
  int check_result_and_overflow(uint32_t mask, int result)
245
 
  {
246
 
    if (check_result(mask, result) & E_DEC_OVERFLOW)
247
 
    {
248
 
      bool _sign= sign();
249
 
      fix_buffer_pointer();
250
 
      max_internal_decimal(this);
251
 
      sign(_sign);
252
 
    }
253
 
    return result;
254
 
  }
255
 
 
256
 
  void convert(double &value) const;
257
 
};
258
 
 
259
 
} // type
260
 
 
261
 
std::ostream& operator<<(std::ostream& output, const type::Decimal &dec);
262
 
 
263
 
inline uint32_t class_decimal_length_to_precision(uint32_t length, uint32_t scale,
264
 
                                                  bool unsigned_flag)
265
 
{
266
 
  return (uint32_t) (length - (scale>0 ? 1:0) - (unsigned_flag ? 0:1));
267
 
}
268
 
 
269
 
inline uint32_t class_decimal_precision_to_length(uint32_t precision, uint8_t scale,
270
 
                                                  bool unsigned_flag)
271
 
{
272
 
  set_if_smaller(precision, (uint32_t)DECIMAL_MAX_PRECISION);
273
 
  return static_cast<uint32_t>(precision + (scale>0 ? 1:0) + (unsigned_flag ? 0:1));
274
 
}
275
 
 
276
 
 
277
 
inline
278
 
int class_decimal_max_length(const type::Decimal *d)
279
 
{
280
 
  /* -1 because we do not count \0 */
281
 
  return decimal_string_size(d) - 1;
282
 
}
283
 
 
284
 
 
285
 
inline
286
 
int class_decimal_get_binary_size(uint32_t precision, uint32_t scale)
287
 
{
288
 
  return decimal_bin_size(static_cast<int>(precision), static_cast<int>(scale));
289
 
}
290
 
 
291
 
 
292
 
inline
293
 
void class_decimal2decimal(const type::Decimal *from, type::Decimal *to)
294
 
{
295
 
  *to= *from;
296
 
  to->fix_buffer_pointer();
297
 
}
298
 
 
299
 
 
300
 
inline
301
 
int binary2_class_decimal(uint32_t mask, const unsigned char *bin, type::Decimal *d, int prec,
302
 
                      int scale)
303
 
{
304
 
  return check_result(mask, bin2decimal(bin, static_cast<decimal_t*>(d), prec, scale));
305
 
}
306
 
 
307
 
 
308
 
inline
309
 
int class_decimal_round(uint32_t mask, const type::Decimal *from, int scale,
310
 
                     bool truncate, type::Decimal *to)
311
 
{
312
 
  return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, scale,
313
 
                                          (truncate ? TRUNCATE : HALF_UP)));
314
 
}
315
 
 
316
 
 
317
 
inline
318
 
int class_decimal_floor(uint32_t mask, const type::Decimal *from, type::Decimal *to)
319
 
{
320
 
  return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, 0, FLOOR));
321
 
}
322
 
 
323
 
 
324
 
inline
325
 
int class_decimal_ceiling(uint32_t mask, const type::Decimal *from, type::Decimal *to)
326
 
{
327
 
  return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, 0, CEILING));
328
 
}
329
 
 
330
 
 
331
 
int class_decimal2string(const type::Decimal *d,
332
 
                         uint32_t fixed_dec, String *str);
333
 
 
334
 
 
335
 
inline
336
 
int class_decimal2double(uint32_t, const type::Decimal *d, double *result)
337
 
{
338
 
  /* No need to call check_result as this will always succeed */
339
 
  return decimal2double(static_cast<const decimal_t*>(d), result);
340
 
}
341
 
 
342
 
 
343
 
type::Decimal *date2_class_decimal(type::Time *ltime, type::Decimal *dec);
344
 
 
345
 
 
346
 
inline
347
 
int double2_class_decimal(uint32_t mask, double val, type::Decimal *d)
348
 
{
349
 
  return d->check_result_and_overflow(mask, double2decimal(val, static_cast<decimal_t*>(d)));
350
 
}
351
 
 
352
 
 
353
 
inline
354
 
int int2_class_decimal(uint32_t mask, int64_t i, bool unsigned_flag, type::Decimal *d)
355
 
{
356
 
  return check_result(mask, (unsigned_flag ?
357
 
                             uint64_t2decimal(static_cast<uint64_t>(i), d) :
358
 
                             int64_t2decimal(i, d)));
359
 
}
360
 
 
361
 
 
362
 
inline
363
 
void class_decimal_neg(decimal_t *arg)
364
 
{
365
 
  if (arg->isZero())
366
 
  {
367
 
    arg->sign= 0;
368
 
    return;
369
 
  }
370
 
  arg->negate();
371
 
}
372
 
 
373
 
 
374
 
inline
375
 
int class_decimal_add(uint32_t mask, type::Decimal *res, const type::Decimal *a,
376
 
                   const type::Decimal *b)
377
 
{
378
 
  return res->check_result_and_overflow(mask,
379
 
                                        decimal_add(static_cast<const decimal_t*>(a),
380
 
                                                    static_cast<const decimal_t*>(b), res));
381
 
}
382
 
 
383
 
 
384
 
inline
385
 
int class_decimal_sub(uint32_t mask, type::Decimal *res, const type::Decimal *a,
386
 
                   const type::Decimal *b)
387
 
{
388
 
  return res->check_result_and_overflow(mask,
389
 
                                        decimal_sub(static_cast<const decimal_t*>(a),
390
 
                                                    static_cast<const decimal_t*>(b), res));
391
 
}
392
 
 
393
 
 
394
 
inline
395
 
int class_decimal_mul(uint32_t mask, type::Decimal *res, const type::Decimal *a,
396
 
                   const type::Decimal *b)
397
 
{
398
 
  return res->check_result_and_overflow(mask,
399
 
                                        decimal_mul(static_cast<const decimal_t*>(a),
400
 
                                                    static_cast<const decimal_t*>(b),res));
401
 
}
402
 
 
403
 
 
404
 
inline
405
 
int class_decimal_div(uint32_t mask, type::Decimal *res, const type::Decimal *a,
406
 
                   const type::Decimal *b, int div_scale_inc)
407
 
{
408
 
  return res->check_result_and_overflow(mask,
409
 
                                        decimal_div(static_cast<const decimal_t*>(a),
410
 
                                                    static_cast<const decimal_t*>(b),res,
411
 
                                                    div_scale_inc));
412
 
}
413
 
 
414
 
 
415
 
inline
416
 
int class_decimal_mod(uint32_t mask, type::Decimal *res, const type::Decimal *a,
417
 
                   const type::Decimal *b)
418
 
{
419
 
  return res->check_result_and_overflow(mask,
420
 
                                        decimal_mod(static_cast<const decimal_t*>(a),
421
 
                                                    static_cast<const decimal_t*>(b),res));
422
 
}
423
 
 
424
 
 
425
 
/**
426
 
  @return
427
 
    -1 if a<b, 1 if a>b and 0 if a==b
428
 
*/
429
 
inline
430
 
int class_decimal_cmp(const type::Decimal *a, const type::Decimal *b)
431
 
{
432
 
  return decimal_cmp(static_cast<const decimal_t*>(a),
433
 
                     static_cast<const decimal_t*>(b));
434
 
}
435
 
 
436
 
 
437
 
inline
438
 
int class_decimal_intg(const type::Decimal *a)
439
 
{
440
 
  return decimal_intg(static_cast<const decimal_t*>(a));
441
 
}
442
 
 
443
 
 
444
 
void class_decimal_trim(uint32_t *precision, uint32_t *scale);
445
 
 
446
 
inline type::Decimal &decimal_zero_const()
447
 
{
448
 
  static type::Decimal _decimal_zero;
449
 
  return _decimal_zero;
450
 
}
451
 
 
452
 
double my_double_round(double value, int64_t dec, bool dec_unsigned,
453
 
                       bool truncate);
454
 
 
455
 
 
456
 
#define decimal_zero decimal_zero_const()
457
 
 
458
 
} /* namespace drizzled */
459
 
 
460
 
#endif /* DRIZZLED_TYPE_DECIMAL_H */
461
107