~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2005-2006 MySQL AB
2
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.
6
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.
11
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 */
15
16
/**
17
  @file
18
19
  It is interface module to fixed precision decimals library.
20
21
  Most functions use 'uint mask' as parameter, if during operation error
22
  which fit in this mask is detected then it will be processed automatically
23
  here. (errors are E_DEC_* constants, see include/decimal.h)
24
25
  Most function are just inline wrappers around library calls
26
*/
27
28
#ifndef my_decimal_h
29
#define my_decimal_h
30
31
C_MODE_START
32
#include <decimal.h>
33
C_MODE_END
34
35
#define DECIMAL_LONGLONG_DIGITS 22
36
#define DECIMAL_LONG_DIGITS 10
37
#define DECIMAL_LONG3_DIGITS 8
38
39
/** maximum length of buffer in our big digits (uint32). */
40
#define DECIMAL_BUFF_LENGTH 9
41
42
/* the number of digits that my_decimal can possibly contain */
43
#define DECIMAL_MAX_POSSIBLE_PRECISION (DECIMAL_BUFF_LENGTH * 9)
44
45
46
/**
47
  maximum guaranteed precision of number in decimal digits (number of our
48
  digits * number of decimal digits in one our big digit - number of decimal
49
  digits in one our big digit decreased by 1 (because we always put decimal
50
  point on the border of our big digits))
51
*/
52
#define DECIMAL_MAX_PRECISION (DECIMAL_MAX_POSSIBLE_PRECISION - 8*2)
53
#define DECIMAL_MAX_SCALE 30
54
#define DECIMAL_NOT_SPECIFIED 31
55
56
/**
57
  maximum length of string representation (number of maximum decimal
58
  digits + 1 position for sign + 1 position for decimal point)
59
*/
60
#define DECIMAL_MAX_STR_LENGTH (DECIMAL_MAX_POSSIBLE_PRECISION + 2)
61
62
/**
63
  maximum size of packet length.
64
*/
65
#define DECIMAL_MAX_FIELD_SIZE DECIMAL_MAX_PRECISION
66
67
68
inline uint my_decimal_size(uint precision, uint scale)
69
{
70
  /*
71
    Always allocate more space to allow library to put decimal point
72
    where it want
73
  */
74
  return decimal_size(precision, scale) + 1;
75
}
76
77
78
inline int my_decimal_int_part(uint precision, uint decimals)
79
{
80
  return precision - ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : decimals);
81
}
82
83
84
/**
85
  my_decimal class limits 'decimal_t' type to what we need in MySQL.
86
87
  It contains internally all necessary space needed by the instance so
88
  no extra memory is needed. One should call fix_buffer_pointer() function
89
  when he moves my_decimal objects in memory.
90
*/
91
92
class my_decimal :public decimal_t
93
{
94
  decimal_digit_t buffer[DECIMAL_BUFF_LENGTH];
95
96
public:
97
98
  void init()
99
  {
100
    len= DECIMAL_BUFF_LENGTH;
101
    buf= buffer;
102
#if !defined (HAVE_purify) && !defined(DBUG_OFF)
103
    /* Set buffer to 'random' value to find wrong buffer usage */
104
    for (uint i= 0; i < DECIMAL_BUFF_LENGTH; i++)
105
      buffer[i]= i;
106
#endif
107
  }
108
  my_decimal()
109
  {
110
    init();
111
  }
112
  void fix_buffer_pointer() { buf= buffer; }
113
114
  bool sign() const { return decimal_t::sign; }
115
  void sign(bool s) { decimal_t::sign= s; }
116
  uint precision() const { return intg + frac; }
117
};
118
119
120
#ifndef DBUG_OFF
121
void print_decimal(const my_decimal *dec);
122
void print_decimal_buff(const my_decimal *dec, const uchar* ptr, int length);
123
const char *dbug_decimal_as_string(char *buff, const my_decimal *val);
124
#else
125
#define dbug_decimal_as_string(A) NULL
126
#endif
127
128
#ifndef MYSQL_CLIENT
129
int decimal_operation_results(int result);
130
#else
131
inline int decimal_operation_results(int result)
132
{
133
  return result;
134
}
135
#endif /*MYSQL_CLIENT*/
136
137
inline
138
void max_my_decimal(my_decimal *to, int precision, int frac)
139
{
140
  DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION)&&
141
              (frac <= DECIMAL_MAX_SCALE));
142
  max_decimal(precision, frac, (decimal_t*) to);
143
}
144
145
inline void max_internal_decimal(my_decimal *to)
146
{
147
  max_my_decimal(to, DECIMAL_MAX_PRECISION, 0);
148
}
149
150
inline int check_result(uint mask, int result)
151
{
152
  if (result & mask)
153
    decimal_operation_results(result);
154
  return result;
155
}
156
157
inline int check_result_and_overflow(uint mask, int result, my_decimal *val)
158
{
159
  if (check_result(mask, result) & E_DEC_OVERFLOW)
160
  {
161
    bool sign= val->sign();
162
    val->fix_buffer_pointer();
163
    max_internal_decimal(val);
164
    val->sign(sign);
165
  }
166
  return result;
167
}
168
169
inline uint my_decimal_length_to_precision(uint length, uint scale,
170
                                           bool unsigned_flag)
171
{
172
  return (uint) (length - (scale>0 ? 1:0) - (unsigned_flag ? 0:1));
173
}
174
175
inline uint32 my_decimal_precision_to_length(uint precision, uint8 scale,
176
                                             bool unsigned_flag)
177
{
178
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
179
  return (uint32)(precision + (scale>0 ? 1:0) + (unsigned_flag ? 0:1));
180
}
181
182
inline
183
int my_decimal_string_length(const my_decimal *d)
184
{
185
  return decimal_string_size(d);
186
}
187
188
189
inline
190
int my_decimal_max_length(const my_decimal *d)
191
{
192
  /* -1 because we do not count \0 */
193
  return decimal_string_size(d) - 1;
194
}
195
196
197
inline
198
int my_decimal_get_binary_size(uint precision, uint scale)
199
{
200
  return decimal_bin_size((int)precision, (int)scale);
201
}
202
203
204
inline
205
void my_decimal2decimal(const my_decimal *from, my_decimal *to)
206
{
207
  *to= *from;
208
  to->fix_buffer_pointer();
209
}
210
211
212
int my_decimal2binary(uint mask, const my_decimal *d, uchar *bin, int prec,
213
		      int scale);
214
215
216
inline
217
int binary2my_decimal(uint mask, const uchar *bin, my_decimal *d, int prec,
218
		      int scale)
219
{
220
  return check_result(mask, bin2decimal(bin, (decimal_t*) d, prec, scale));
221
}
222
223
224
inline
225
int my_decimal_set_zero(my_decimal *d)
226
{
227
  decimal_make_zero(((decimal_t*) d));
228
  return 0;
229
}
230
231
232
inline
233
bool my_decimal_is_zero(const my_decimal *decimal_value)
234
{
235
  return decimal_is_zero((decimal_t*) decimal_value);
236
}
237
238
239
inline
240
int my_decimal_round(uint mask, const my_decimal *from, int scale,
241
                     bool truncate, my_decimal *to)
242
{
243
  return check_result(mask, decimal_round((decimal_t*) from, to, scale,
244
					  (truncate ? TRUNCATE : HALF_UP)));
245
}
246
247
248
inline
249
int my_decimal_floor(uint mask, const my_decimal *from, my_decimal *to)
250
{
251
  return check_result(mask, decimal_round((decimal_t*) from, to, 0, FLOOR));
252
}
253
254
255
inline
256
int my_decimal_ceiling(uint mask, const my_decimal *from, my_decimal *to)
257
{
258
  return check_result(mask, decimal_round((decimal_t*) from, to, 0, CEILING));
259
}
260
261
262
#ifndef MYSQL_CLIENT
263
int my_decimal2string(uint mask, const my_decimal *d, uint fixed_prec,
264
		      uint fixed_dec, char filler, String *str);
265
#endif
266
267
inline
268
int my_decimal2int(uint mask, const my_decimal *d, my_bool unsigned_flag,
269
		   longlong *l)
270
{
271
  my_decimal rounded;
272
  /* decimal_round can return only E_DEC_TRUNCATED */
273
  decimal_round((decimal_t*)d, &rounded, 0, HALF_UP);
274
  return check_result(mask, (unsigned_flag ?
275
			     decimal2ulonglong(&rounded, (ulonglong *)l) :
276
			     decimal2longlong(&rounded, l)));
277
}
278
279
280
inline
53.2.4 by Monty Taylor
Changes so that client/ builds cleanly with no warnings.
281
int my_decimal2double(uint mask __attribute__((__unused__)), 
282
                      const my_decimal *d, double *result)
1 by brian
clean slate
283
{
284
  /* No need to call check_result as this will always succeed */
285
  return decimal2double((decimal_t*) d, result);
286
}
287
288
289
inline
290
int str2my_decimal(uint mask, const char *str, my_decimal *d, char **end)
291
{
292
  return check_result_and_overflow(mask, string2decimal(str,(decimal_t*)d,end),
293
                                   d);
294
}
295
296
297
int str2my_decimal(uint mask, const char *from, uint length,
298
                   CHARSET_INFO *charset, my_decimal *decimal_value);
299
300
#if defined(MYSQL_SERVER)
301
inline
302
int string2my_decimal(uint mask, const String *str, my_decimal *d)
303
{
304
  return str2my_decimal(mask, str->ptr(), str->length(), str->charset(), d);
305
}
306
307
308
my_decimal *date2my_decimal(MYSQL_TIME *ltime, my_decimal *dec);
309
310
311
#endif /*defined(MYSQL_SERVER) */
312
313
inline
314
int double2my_decimal(uint mask, double val, my_decimal *d)
315
{
316
  return check_result_and_overflow(mask, double2decimal(val, (decimal_t*)d), d);
317
}
318
319
320
inline
321
int int2my_decimal(uint mask, longlong i, my_bool unsigned_flag, my_decimal *d)
322
{
323
  return check_result(mask, (unsigned_flag ?
324
			     ulonglong2decimal((ulonglong)i, d) :
325
			     longlong2decimal(i, d)));
326
}
327
328
329
inline
330
void my_decimal_neg(decimal_t *arg)
331
{
332
  if (decimal_is_zero(arg))
333
  {
334
    arg->sign= 0;
335
    return;
336
  }
337
  decimal_neg(arg);
338
}
339
340
341
inline
342
int my_decimal_add(uint mask, my_decimal *res, const my_decimal *a,
343
		   const my_decimal *b)
344
{
345
  return check_result_and_overflow(mask,
346
                                   decimal_add((decimal_t*)a,(decimal_t*)b,res),
347
                                   res);
348
}
349
350
351
inline
352
int my_decimal_sub(uint mask, my_decimal *res, const my_decimal *a,
353
		   const my_decimal *b)
354
{
355
  return check_result_and_overflow(mask,
356
                                   decimal_sub((decimal_t*)a,(decimal_t*)b,res),
357
                                   res);
358
}
359
360
361
inline
362
int my_decimal_mul(uint mask, my_decimal *res, const my_decimal *a,
363
		   const my_decimal *b)
364
{
365
  return check_result_and_overflow(mask,
366
                                   decimal_mul((decimal_t*)a,(decimal_t*)b,res),
367
                                   res);
368
}
369
370
371
inline
372
int my_decimal_div(uint mask, my_decimal *res, const my_decimal *a,
373
		   const my_decimal *b, int div_scale_inc)
374
{
375
  return check_result_and_overflow(mask,
376
                                   decimal_div((decimal_t*)a,(decimal_t*)b,res,
377
                                               div_scale_inc),
378
                                   res);
379
}
380
381
382
inline
383
int my_decimal_mod(uint mask, my_decimal *res, const my_decimal *a,
384
		   const my_decimal *b)
385
{
386
  return check_result_and_overflow(mask,
387
                                   decimal_mod((decimal_t*)a,(decimal_t*)b,res),
388
                                   res);
389
}
390
391
392
/**
393
  @return
394
    -1 if a<b, 1 if a>b and 0 if a==b
395
*/
396
inline
397
int my_decimal_cmp(const my_decimal *a, const my_decimal *b)
398
{
399
  return decimal_cmp((decimal_t*) a, (decimal_t*) b);
400
}
401
402
403
inline
404
int my_decimal_intg(const my_decimal *a)
405
{
406
  return decimal_intg((decimal_t*) a);
407
}
408
409
410
void my_decimal_trim(ulong *precision, uint *scale);
411
412
413
#endif /*my_decimal_h*/
414