~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/fdecimal.cc

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
1
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
4
 *  Copyright (C) 2008 MySQL
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#ifdef USE_PRAGMA_IMPLEMENTATION
22
 
#pragma implementation                          // gcc: Class implementation
23
 
#endif
24
21
 
25
22
#include <drizzled/server_includes.h>
26
23
#include <drizzled/field/fdecimal.h>
27
 
#include <drizzled/drizzled_error_messages.h>
 
24
#include <drizzled/error.h>
28
25
 
 
26
extern my_decimal decimal_zero;
29
27
 
30
28
/****************************************************************************
31
29
** Field_new_decimal
32
30
****************************************************************************/
33
31
 
34
 
Field_new_decimal::Field_new_decimal(uchar *ptr_arg,
35
 
                                     uint32_t len_arg, uchar *null_ptr_arg,
36
 
                                     uchar null_bit_arg,
 
32
Field_new_decimal::Field_new_decimal(unsigned char *ptr_arg,
 
33
                                     uint32_t len_arg, unsigned char *null_ptr_arg,
 
34
                                     unsigned char null_bit_arg,
37
35
                                     enum utype unireg_check_arg,
38
36
                                     const char *field_name_arg,
39
37
                                     uint8_t dec_arg,bool zero_arg,
54
52
                                     const char *name,
55
53
                                     uint8_t dec_arg,
56
54
                                     bool unsigned_arg)
57
 
  :Field_num((uchar*) 0, len_arg,
58
 
             maybe_null_arg ? (uchar*) "": 0, 0,
 
55
  :Field_num((unsigned char*) 0, len_arg,
 
56
             maybe_null_arg ? (unsigned char*) "": 0, 0,
59
57
             NONE, name, dec_arg, 0, unsigned_arg)
60
58
{
61
59
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
85
83
{
86
84
  max_my_decimal(decimal_value, precision, decimals());
87
85
  if (sign)
88
 
  {
89
 
    if (unsigned_flag)
90
 
      my_decimal_set_zero(decimal_value);
91
 
    else
92
 
      decimal_value->sign(true);
93
 
  }
 
86
    decimal_value->sign(true);
 
87
 
94
88
  return;
95
89
}
96
90
 
114
108
{
115
109
  int error= 0;
116
110
 
117
 
  /* check that we do not try to write negative value in unsigned field */
118
 
  if (unsigned_flag && decimal_value->sign())
119
 
  {
120
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
121
 
    error= 1;
122
 
    decimal_value= &decimal_zero;
123
 
  }
124
 
 
125
111
  if (warn_if_overflow(my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
126
112
                                         decimal_value, ptr, precision, dec)))
127
113
  {
134
120
}
135
121
 
136
122
 
137
 
int Field_new_decimal::store(const char *from, uint length,
 
123
int Field_new_decimal::store(const char *from, uint32_t length,
138
124
                             const CHARSET_INFO * const charset_arg)
139
125
{
140
126
  int err;
245
231
 
246
232
 
247
233
int Field_new_decimal::store_time(DRIZZLE_TIME *ltime,
248
 
                                  timestamp_type t_type __attribute__((unused)))
 
234
                                  enum enum_drizzle_timestamp_type t_type __attribute__((unused)))
249
235
{
250
236
    my_decimal decimal_value;
251
237
    return store_value(date2my_decimal(ltime, &decimal_value));
265
251
{
266
252
  int64_t i;
267
253
  my_decimal decimal_value;
268
 
  my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
269
 
                 unsigned_flag, &i);
 
254
  my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), false, &i);
270
255
  return i;
271
256
}
272
257
 
283
268
                                   String *val_ptr __attribute__((unused)))
284
269
{
285
270
  my_decimal decimal_value;
286
 
  uint fixed_precision= decimal_precision ? precision : 0;
 
271
  uint32_t fixed_precision= decimal_precision ? precision : 0;
287
272
  my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
288
273
                    fixed_precision, dec, '0', val_buffer);
289
274
  return val_buffer;
290
275
}
291
276
 
292
277
 
293
 
int Field_new_decimal::cmp(const uchar *a,const uchar*b)
 
278
int Field_new_decimal::cmp(const unsigned char *a,const unsigned char*b)
294
279
{
295
280
  return memcmp(a, b, bin_size);
296
281
}
297
282
 
298
283
 
299
 
void Field_new_decimal::sort_string(uchar *buff,
300
 
                                    uint length __attribute__((unused)))
 
284
void Field_new_decimal::sort_string(unsigned char *buff,
 
285
                                    uint32_t length __attribute__((unused)))
301
286
{
302
287
  memcpy(buff, ptr, bin_size);
303
288
}
308
293
  const CHARSET_INFO * const cs= str.charset();
309
294
  str.length(cs->cset->snprintf(cs, (char*) str.ptr(), str.alloced_length(),
310
295
                                "decimal(%d,%d)", precision, (int)dec));
311
 
  add_unsigned(str);
312
296
}
313
297
 
314
298
 
323
307
 
324
308
   @returns number of bytes written to metadata_ptr
325
309
*/
326
 
int Field_new_decimal::do_save_field_metadata(uchar *metadata_ptr)
 
310
int Field_new_decimal::do_save_field_metadata(unsigned char *metadata_ptr)
327
311
{
328
312
  *metadata_ptr= precision;
329
313
  *(metadata_ptr + 1)= decimals();
343
327
 
344
328
   @returns The size of the field based on the field metadata.
345
329
*/
346
 
uint Field_new_decimal::pack_length_from_metadata(uint field_metadata)
 
330
uint32_t Field_new_decimal::pack_length_from_metadata(uint32_t field_metadata)
347
331
{
348
 
  uint const source_precision= (field_metadata >> 8U) & 0x00ff;
349
 
  uint const source_decimal= field_metadata & 0x00ff; 
350
 
  uint const source_size= my_decimal_get_binary_size(source_precision, 
 
332
  uint32_t const source_precision= (field_metadata >> 8U) & 0x00ff;
 
333
  uint32_t const source_decimal= field_metadata & 0x00ff; 
 
334
  uint32_t const source_size= my_decimal_get_binary_size(source_precision, 
351
335
                                                     source_decimal);
352
336
  return (source_size);
353
337
}
366
350
   @retval 0 if this field's size is < the source field's size
367
351
   @retval 1 if this field's size is >= the source field's size
368
352
*/
369
 
int Field_new_decimal::compatible_field_size(uint field_metadata)
 
353
int Field_new_decimal::compatible_field_size(uint32_t field_metadata)
370
354
{
371
355
  int compatible= 0;
372
 
  uint const source_precision= (field_metadata >> 8U) & 0x00ff;
373
 
  uint const source_decimal= field_metadata & 0x00ff; 
374
 
  uint const source_size= my_decimal_get_binary_size(source_precision, 
 
356
  uint32_t const source_precision= (field_metadata >> 8U) & 0x00ff;
 
357
  uint32_t const source_decimal= field_metadata & 0x00ff; 
 
358
  uint32_t const source_size= my_decimal_get_binary_size(source_precision, 
375
359
                                                     source_decimal);
376
 
  uint const destination_size= row_pack_length();
 
360
  uint32_t const destination_size= row_pack_length();
377
361
  compatible= (source_size <= destination_size);
378
362
  if (compatible)
379
363
    compatible= (source_precision <= precision) &&
382
366
}
383
367
 
384
368
 
385
 
uint Field_new_decimal::is_equal(Create_field *new_field)
 
369
uint32_t Field_new_decimal::is_equal(Create_field *new_field)
386
370
{
387
371
  return ((new_field->sql_type == real_type()) &&
388
372
          ((new_field->flags & UNSIGNED_FLAG) == 
406
390
 
407
391
   @return  New pointer into memory based on from + length of the data
408
392
*/
409
 
const uchar *
410
 
Field_new_decimal::unpack(uchar* to,
411
 
                          const uchar *from,
412
 
                          uint param_data,
 
393
const unsigned char *
 
394
Field_new_decimal::unpack(unsigned char* to,
 
395
                          const unsigned char *from,
 
396
                          uint32_t param_data,
413
397
                          bool low_byte_first)
414
398
{
415
399
  if (param_data == 0)
416
400
    return Field::unpack(to, from, param_data, low_byte_first);
417
401
 
418
 
  uint from_precision= (param_data & 0xff00) >> 8U;
419
 
  uint from_decimal= param_data & 0x00ff;
420
 
  uint length=pack_length();
421
 
  uint from_pack_len= my_decimal_get_binary_size(from_precision, from_decimal);
422
 
  uint len= (param_data && (from_pack_len < length)) ?
 
402
  uint32_t from_precision= (param_data & 0xff00) >> 8U;
 
403
  uint32_t from_decimal= param_data & 0x00ff;
 
404
  uint32_t length=pack_length();
 
405
  uint32_t from_pack_len= my_decimal_get_binary_size(from_precision, from_decimal);
 
406
  uint32_t len= (param_data && (from_pack_len < length)) ?
423
407
            from_pack_len : length;
424
408
  if ((from_pack_len && (from_pack_len < length)) ||
425
409
      (from_precision < precision) ||
439
423
      just the first step the resizing operation. The second step does the
440
424
      resizing using the precision and decimals from the slave.
441
425
    */
442
 
    bin2decimal((uchar *)from, &dec, from_precision, from_decimal);
 
426
    bin2decimal((unsigned char *)from, &dec, from_precision, from_decimal);
443
427
    decimal2bin(&dec, to, precision, decimals());
444
428
  }
445
429
  else