~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/decimal.cc

  • Committer: Monty Taylor
  • Date: 2009-03-04 02:16:28 UTC
  • mto: (917.1.2 mordred)
  • mto: This revision was merged to the branch mainline in revision 912.
  • Revision ID: mordred@inaugust.com-20090304021628-rfq0b16uoi09g8tx
Fix to make VPATH builds work again.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#include <drizzled/field/fdecimal.h>
27
 
#include <drizzled/drizzled_error_messages.h>
 
23
#include <drizzled/field/decimal.h>
 
24
#include <drizzled/error.h>
 
25
#include <drizzled/table.h>
 
26
#include <drizzled/session.h>
28
27
 
 
28
extern my_decimal decimal_zero;
29
29
 
30
30
/****************************************************************************
31
31
** Field_new_decimal
85
85
{
86
86
  max_my_decimal(decimal_value, precision, decimals());
87
87
  if (sign)
88
 
  {
89
 
    if (unsigned_flag)
90
 
      my_decimal_set_zero(decimal_value);
91
 
    else
92
 
      decimal_value->sign(true);
93
 
  }
 
88
    decimal_value->sign(true);
 
89
 
94
90
  return;
95
91
}
96
92
 
114
110
{
115
111
  int error= 0;
116
112
 
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
113
  if (warn_if_overflow(my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
126
114
                                         decimal_value, ptr, precision, dec)))
127
115
  {
245
233
 
246
234
 
247
235
int Field_new_decimal::store_time(DRIZZLE_TIME *ltime,
248
 
                                  enum enum_drizzle_timestamp_type t_type __attribute__((unused)))
 
236
                                  enum enum_drizzle_timestamp_type )
249
237
{
250
238
    my_decimal decimal_value;
251
239
    return store_value(date2my_decimal(ltime, &decimal_value));
265
253
{
266
254
  int64_t i;
267
255
  my_decimal decimal_value;
268
 
  my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
269
 
                 unsigned_flag, &i);
 
256
  my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), false, &i);
270
257
  return i;
271
258
}
272
259
 
280
267
 
281
268
 
282
269
String *Field_new_decimal::val_str(String *val_buffer,
283
 
                                   String *val_ptr __attribute__((unused)))
 
270
                                   String *)
284
271
{
285
272
  my_decimal decimal_value;
286
273
  uint32_t fixed_precision= decimal_precision ? precision : 0;
297
284
 
298
285
 
299
286
void Field_new_decimal::sort_string(unsigned char *buff,
300
 
                                    uint32_t length __attribute__((unused)))
 
287
                                    uint32_t )
301
288
{
302
289
  memcpy(buff, ptr, bin_size);
303
290
}
308
295
  const CHARSET_INFO * const cs= str.charset();
309
296
  str.length(cs->cset->snprintf(cs, (char*) str.ptr(), str.alloced_length(),
310
297
                                "decimal(%d,%d)", precision, (int)dec));
311
 
  add_unsigned(str);
312
298
}
313
299
 
314
300
 
316
302
   Save the field metadata for new decimal fields.
317
303
 
318
304
   Saves the precision in the first byte and decimals() in the second
319
 
   byte of the field metadata array at index of *metadata_ptr and 
 
305
   byte of the field metadata array at index of *metadata_ptr and
320
306
   *(metadata_ptr + 1).
321
307
 
322
308
   @param   metadata_ptr   First byte of field metadata
332
318
 
333
319
 
334
320
/**
335
 
   Returns the number of bytes field uses in row-based replication 
 
321
   Returns the number of bytes field uses in row-based replication
336
322
   row packed size.
337
323
 
338
324
   This method is used in row-based replication to determine the number
346
332
uint32_t Field_new_decimal::pack_length_from_metadata(uint32_t field_metadata)
347
333
{
348
334
  uint32_t const source_precision= (field_metadata >> 8U) & 0x00ff;
349
 
  uint32_t const source_decimal= field_metadata & 0x00ff; 
350
 
  uint32_t const source_size= my_decimal_get_binary_size(source_precision, 
 
335
  uint32_t const source_decimal= field_metadata & 0x00ff;
 
336
  uint32_t const source_size= my_decimal_get_binary_size(source_precision,
351
337
                                                     source_decimal);
352
338
  return (source_size);
353
339
}
357
343
   Check to see if field size is compatible with destination.
358
344
 
359
345
   This method is used in row-based replication to verify that the slave's
360
 
   field size is less than or equal to the master's field size. The 
 
346
   field size is less than or equal to the master's field size. The
361
347
   encoded field metadata (from the master or source) is decoded and compared
362
 
   to the size of this field (the slave or destination). 
 
348
   to the size of this field (the slave or destination).
363
349
 
364
350
   @param   field_metadata   Encoded size in field metadata
365
351
 
370
356
{
371
357
  int compatible= 0;
372
358
  uint32_t const source_precision= (field_metadata >> 8U) & 0x00ff;
373
 
  uint32_t const source_decimal= field_metadata & 0x00ff; 
374
 
  uint32_t const source_size= my_decimal_get_binary_size(source_precision, 
 
359
  uint32_t const source_decimal= field_metadata & 0x00ff;
 
360
  uint32_t const source_size= my_decimal_get_binary_size(source_precision,
375
361
                                                     source_decimal);
376
362
  uint32_t const destination_size= row_pack_length();
377
363
  compatible= (source_size <= destination_size);
382
368
}
383
369
 
384
370
 
385
 
uint32_t Field_new_decimal::is_equal(Create_field *new_field)
 
371
uint32_t Field_new_decimal::is_equal(Create_field *new_field_ptr)
386
372
{
387
 
  return ((new_field->sql_type == real_type()) &&
388
 
          ((new_field->flags & UNSIGNED_FLAG) == 
389
 
           (uint) (flags & UNSIGNED_FLAG)) &&
390
 
          ((new_field->flags & AUTO_INCREMENT_FLAG) ==
391
 
           (uint) (flags & AUTO_INCREMENT_FLAG)) &&
392
 
          (new_field->length == max_display_length()) &&
393
 
          (new_field->decimals == dec));
 
373
  return ((new_field_ptr->sql_type == real_type()) &&
 
374
          ((new_field_ptr->flags & UNSIGNED_FLAG) ==
 
375
           (uint32_t) (flags & UNSIGNED_FLAG)) &&
 
376
          ((new_field_ptr->flags & AUTO_INCREMENT_FLAG) ==
 
377
           (uint32_t) (flags & AUTO_INCREMENT_FLAG)) &&
 
378
          (new_field_ptr->length == max_display_length()) &&
 
379
          (new_field_ptr->decimals == dec));
394
380
}
395
381
 
396
382
 
399
385
 
400
386
   This method is used to unpack a decimal or numeric field from a master
401
387
   whose size of the field is less than that of the slave.
402
 
  
 
388
 
403
389
   @param   to         Destination of the data
404
390
   @param   from       Source of the data
405
391
   @param   param_data Precision (upper) and decimal (lower) values
431
417
      a decimal and write that to the raw data buffer.
432
418
    */
433
419
    decimal_digit_t dec_buf[DECIMAL_MAX_PRECISION];
434
 
    decimal_t dec;
435
 
    dec.len= from_precision;
436
 
    dec.buf= dec_buf;
 
420
    decimal_t conv_dec;
 
421
    conv_dec.len= from_precision;
 
422
    conv_dec.buf= dec_buf;
437
423
    /*
438
424
      Note: bin2decimal does not change the length of the field. So it is
439
425
      just the first step the resizing operation. The second step does the
440
426
      resizing using the precision and decimals from the slave.
441
427
    */
442
 
    bin2decimal((unsigned char *)from, &dec, from_precision, from_decimal);
443
 
    decimal2bin(&dec, to, precision, decimals());
 
428
    bin2decimal((unsigned char *)from, &conv_dec, from_precision, from_decimal);
 
429
    decimal2bin(&conv_dec, to, precision, decimals());
444
430
  }
445
431
  else
446
432
    memcpy(to, from, len); // Sizes are the same, just copy the data.