~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/decimal.h

  • Committer: Brian Aker
  • Date: 2010-12-25 21:10:07 UTC
  • mfrom: (2030.1.5 clean)
  • Revision ID: brian@tangent.org-20101225211007-78qgasqv4rvduowe
Merge in modification for naming around types.

Show diffs side-by-side

added added

removed removed

Lines of Context:
120
120
/** maximum length of buffer in our big digits (uint32_t). */
121
121
#define DECIMAL_BUFF_LENGTH 9
122
122
 
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)
125
125
 
126
126
 
145
145
*/
146
146
#define DECIMAL_MAX_FIELD_SIZE DECIMAL_MAX_PRECISION
147
147
 
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)
149
149
{
150
150
  return precision - ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : decimals);
151
151
}
152
152
 
153
153
 
 
154
namespace type {
154
155
/**
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.
156
157
 
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.
160
161
*/
161
162
 
162
 
class my_decimal :public decimal_t
 
163
class Decimal :public decimal_t
163
164
{
164
165
  decimal_digit_t buffer[DECIMAL_BUFF_LENGTH];
165
166
 
176
177
        #endif
177
178
  }
178
179
 
179
 
  my_decimal()
 
180
  Decimal()
180
181
  {
181
182
    init();
182
183
  }
186
187
  uint32_t precision() const { return intg + frac; }
187
188
};
188
189
 
189
 
std::ostream& operator<<(std::ostream& output, const my_decimal &dec);
 
190
} // type
 
191
 
 
192
std::ostream& operator<<(std::ostream& output, const type::Decimal &dec);
190
193
 
191
194
int decimal_operation_results(int result);
192
195
 
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)
194
197
{
195
198
  assert((precision <= DECIMAL_MAX_PRECISION)&&
196
199
              (frac <= DECIMAL_MAX_SCALE));
197
200
  max_decimal(precision, frac, (decimal_t*) to);
198
201
}
199
202
 
200
 
inline void max_internal_decimal(my_decimal *to)
 
203
inline void max_internal_decimal(type::Decimal *to)
201
204
{
202
 
  max_my_decimal(to, DECIMAL_MAX_PRECISION, 0);
 
205
  max_Decimal(to, DECIMAL_MAX_PRECISION, 0);
203
206
}
204
207
 
205
208
inline int check_result(uint32_t mask, int result)
209
212
  return result;
210
213
}
211
214
 
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)
213
216
{
214
217
  if (check_result(mask, result) & E_DEC_OVERFLOW)
215
218
  {
221
224
  return result;
222
225
}
223
226
 
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)
226
229
{
227
230
  return (uint32_t) (length - (scale>0 ? 1:0) - (unsigned_flag ? 0:1));
228
231
}
229
232
 
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)
232
235
{
233
236
  set_if_smaller(precision, (uint32_t)DECIMAL_MAX_PRECISION);
235
238
}
236
239
 
237
240
inline
238
 
int my_decimal_string_length(const my_decimal *d)
 
241
int class_decimal_string_length(const type::Decimal *d)
239
242
{
240
243
  return decimal_string_size(d);
241
244
}
242
245
 
243
246
 
244
247
inline
245
 
int my_decimal_max_length(const my_decimal *d)
 
248
int class_decimal_max_length(const type::Decimal *d)
246
249
{
247
250
  /* -1 because we do not count \0 */
248
251
  return decimal_string_size(d) - 1;
250
253
 
251
254
 
252
255
inline
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)
254
257
{
255
258
  return decimal_bin_size(static_cast<int>(precision), static_cast<int>(scale));
256
259
}
257
260
 
258
261
 
259
262
inline
260
 
void my_decimal2decimal(const my_decimal *from, my_decimal *to)
 
263
void class_decimal2decimal(const type::Decimal *from, type::Decimal *to)
261
264
{
262
265
  *to= *from;
263
266
  to->fix_buffer_pointer();
264
267
}
265
268
 
266
269
 
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,
268
271
                      int scale);
269
272
 
270
273
 
271
274
inline
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,
273
276
                      int scale)
274
277
{
275
278
  return check_result(mask, bin2decimal(bin, static_cast<decimal_t*>(d), prec, scale));
277
280
 
278
281
 
279
282
inline
280
 
int my_decimal_set_zero(my_decimal *d)
 
283
int class_decimal_set_zero(type::Decimal *d)
281
284
{
282
285
  decimal_make_zero(static_cast<decimal_t*> (d));
283
286
  return 0;
285
288
 
286
289
 
287
290
inline
288
 
bool my_decimal_is_zero(const my_decimal *decimal_value)
 
291
bool class_decimal_is_zero(const type::Decimal *decimal_value)
289
292
{
290
293
  return decimal_is_zero(static_cast<const decimal_t*>(decimal_value));
291
294
}
292
295
 
293
296
 
294
297
inline
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)
297
300
{
298
301
  return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, scale,
299
302
                                          (truncate ? TRUNCATE : HALF_UP)));
301
304
 
302
305
 
303
306
inline
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)
305
308
{
306
309
  return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, 0, FLOOR));
307
310
}
308
311
 
309
312
 
310
313
inline
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)
312
315
{
313
316
  return check_result(mask, decimal_round(static_cast<const decimal_t*>(from), to, 0, CEILING));
314
317
}
315
318
 
316
319
 
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);
319
322
 
320
323
inline
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,
322
325
                   int64_t *l)
323
326
{
324
 
  my_decimal rounded;
 
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 ?
331
334
 
332
335
 
333
336
inline
334
 
int my_decimal2double(uint32_t, const my_decimal *d, double *result)
 
337
int class_decimal2double(uint32_t, const type::Decimal *d, double *result)
335
338
{
336
339
  /* No need to call check_result as this will always succeed */
337
340
  return decimal2double(static_cast<const decimal_t*>(d), result);
339
342
 
340
343
 
341
344
inline
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)
343
346
{
344
347
  return check_result_and_overflow(mask, string2decimal(str, static_cast<decimal_t*>(d),end),
345
348
                                   d);
346
349
}
347
350
 
348
351
 
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);
351
354
 
352
355
inline
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)
354
357
{
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);
356
359
}
357
360
 
358
361
 
359
 
my_decimal *date2my_decimal(DRIZZLE_TIME *ltime, my_decimal *dec);
 
362
type::Decimal *date2_class_decimal(type::Time *ltime, type::Decimal *dec);
360
363
 
361
364
 
362
365
inline
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)
364
367
{
365
368
  return check_result_and_overflow(mask, double2decimal(val, static_cast<decimal_t*>(d)), d);
366
369
}
367
370
 
368
371
 
369
372
inline
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)
371
374
{
372
375
  return check_result(mask, (unsigned_flag ?
373
376
                             uint64_t2decimal(static_cast<uint64_t>(i), d) :
376
379
 
377
380
 
378
381
inline
379
 
void my_decimal_neg(decimal_t *arg)
 
382
void class_decimal_neg(decimal_t *arg)
380
383
{
381
384
  if (decimal_is_zero(arg))
382
385
  {
388
391
 
389
392
 
390
393
inline
391
 
int my_decimal_add(uint32_t mask, my_decimal *res, const my_decimal *a,
392
 
                   const my_decimal *b)
 
394
int class_decimal_add(uint32_t mask, type::Decimal *res, const type::Decimal *a,
 
395
                   const type::Decimal *b)
393
396
{
394
397
  return check_result_and_overflow(mask,
395
398
                                   decimal_add(static_cast<const decimal_t*>(a),
399
402
 
400
403
 
401
404
inline
402
 
int my_decimal_sub(uint32_t mask, my_decimal *res, const my_decimal *a,
403
 
                   const my_decimal *b)
 
405
int class_decimal_sub(uint32_t mask, type::Decimal *res, const type::Decimal *a,
 
406
                   const type::Decimal *b)
404
407
{
405
408
  return check_result_and_overflow(mask,
406
409
                                   decimal_sub(static_cast<const decimal_t*>(a),
410
413
 
411
414
 
412
415
inline
413
 
int my_decimal_mul(uint32_t mask, my_decimal *res, const my_decimal *a,
414
 
                   const my_decimal *b)
 
416
int class_decimal_mul(uint32_t mask, type::Decimal *res, const type::Decimal *a,
 
417
                   const type::Decimal *b)
415
418
{
416
419
  return check_result_and_overflow(mask,
417
420
                                   decimal_mul(static_cast<const decimal_t*>(a),
421
424
 
422
425
 
423
426
inline
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)
426
429
{
427
430
  return check_result_and_overflow(mask,
428
431
                                   decimal_div(static_cast<const decimal_t*>(a),
433
436
 
434
437
 
435
438
inline
436
 
int my_decimal_mod(uint32_t mask, my_decimal *res, const my_decimal *a,
437
 
                   const my_decimal *b)
 
439
int class_decimal_mod(uint32_t mask, type::Decimal *res, const type::Decimal *a,
 
440
                   const type::Decimal *b)
438
441
{
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
449
452
*/
450
453
inline
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)
452
455
{
453
456
  return decimal_cmp(static_cast<const decimal_t*>(a),
454
457
                     static_cast<const decimal_t*>(b));
456
459
 
457
460
 
458
461
inline
459
 
int my_decimal_intg(const my_decimal *a)
 
462
int class_decimal_intg(const type::Decimal *a)
460
463
{
461
464
  return decimal_intg(static_cast<const decimal_t*>(a));
462
465
}
463
466
 
464
467
 
465
 
void my_decimal_trim(uint32_t *precision, uint32_t *scale);
 
468
void class_decimal_trim(uint32_t *precision, uint32_t *scale);
466
469
 
467
470
} /* namespace drizzled */
468
471