~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/decimal.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-11-28 22:02:19 UTC
  • mfrom: (1228 push)
  • mto: (1228.4.1 push)
  • mto: This revision was merged to the branch mainline in revision 1234.
  • Revision ID: osullivan.padraig@gmail.com-20091128220219-m3x28m8q2unbirke
MergeĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
 
22
 
#include "config.h"
 
22
#include <drizzled/server_includes.h>
23
23
#include <drizzled/field/decimal.h>
24
24
#include <drizzled/error.h>
25
25
#include <drizzled/table.h>
26
26
#include <drizzled/session.h>
27
27
 
28
 
namespace drizzled
29
 
{
30
 
 
31
28
extern my_decimal decimal_zero;
32
29
 
33
30
/****************************************************************************
124
121
 
125
122
bool Field_decimal::store_value(const my_decimal *decimal_value)
126
123
{
127
 
  int error= my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
128
 
                                         decimal_value, ptr, precision, dec);
129
 
  if (warn_if_overflow(error))
 
124
  int error= 0;
 
125
 
 
126
  if (warn_if_overflow(my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
 
127
                                         decimal_value, ptr, precision, dec)))
130
128
  {
131
 
    if (error != E_DEC_TRUNCATED)
132
 
    {
133
 
      my_decimal buff;
134
 
      set_value_on_overflow(&buff, decimal_value->sign());
135
 
      my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
136
 
    }
 
129
    my_decimal buff;
 
130
    set_value_on_overflow(&buff, decimal_value->sign());
 
131
    my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
137
132
    error= 1;
138
133
  }
139
134
  return(error);
152
147
                           ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
153
148
                           from, length, charset_arg,
154
149
                           &decimal_value)) &&
155
 
      getTable()->in_use->abort_on_warning)
 
150
      table->in_use->abort_on_warning)
156
151
  {
157
152
    /* Because "from" is not NUL-terminated and we use %s in the ER() */
158
153
    String from_as_str;
159
154
    from_as_str.copy(from, length, &my_charset_bin);
160
155
 
161
 
    push_warning_printf(getTable()->in_use, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
156
    push_warning_printf(table->in_use, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
162
157
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
163
158
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
164
159
                        "decimal", from_as_str.c_ptr(), field_name,
165
 
                        (uint32_t) getTable()->in_use->row_count);
 
160
                        (uint32_t) table->in_use->row_count);
166
161
 
167
162
    return(err);
168
163
  }
182
177
      String from_as_str;
183
178
      from_as_str.copy(from, length, &my_charset_bin);
184
179
 
185
 
      push_warning_printf(getTable()->in_use, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
180
      push_warning_printf(table->in_use, DRIZZLE_ERROR::WARN_LEVEL_WARN,
186
181
                          ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
187
182
                          ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
188
183
                          "decimal", from_as_str.c_ptr(), field_name,
189
 
                          (uint32_t) getTable()->in_use->row_count);
 
184
                          (uint32_t) table->in_use->row_count);
190
185
      my_decimal_set_zero(&decimal_value);
191
186
 
192
187
      break;
218
213
    if (check_overflow(err))
219
214
      set_value_on_overflow(&decimal_value, decimal_value.sign());
220
215
    /* Only issue a warning if store_value doesn't issue an warning */
221
 
    getTable()->in_use->got_warning= 0;
 
216
    table->in_use->got_warning= 0;
222
217
  }
223
218
  if (store_value(&decimal_value))
224
219
    err= 1;
225
 
  else if (err && !getTable()->in_use->got_warning)
 
220
  else if (err && !table->in_use->got_warning)
226
221
    err= warn_if_overflow(err);
227
222
  return(err);
228
223
}
241
236
    if (check_overflow(err))
242
237
      set_value_on_overflow(&decimal_value, decimal_value.sign());
243
238
    /* Only issue a warning if store_value doesn't issue an warning */
244
 
    getTable()->in_use->got_warning= 0;
 
239
    table->in_use->got_warning= 0;
245
240
  }
246
241
  if (store_value(&decimal_value))
247
242
    err= 1;
248
 
  else if (err && not getTable()->in_use->got_warning)
 
243
  else if (err && !table->in_use->got_warning)
249
244
    err= warn_if_overflow(err);
250
245
  return err;
251
246
}
337
332
 
338
333
 
339
334
/**
 
335
  Save the field metadata for new decimal fields.
 
336
 
 
337
  Saves the precision in the first byte and decimals() in the second
 
338
  byte of the field metadata array at index of *metadata_ptr and
 
339
 *(metadata_ptr + 1).
 
340
 
 
341
 @param   metadata_ptr   First byte of field metadata
 
342
 
 
343
 @returns number of bytes written to metadata_ptr
 
344
*/
 
345
int Field_decimal::do_save_field_metadata(unsigned char *metadata_ptr)
 
346
{
 
347
  *metadata_ptr= precision;
 
348
  *(metadata_ptr + 1)= decimals();
 
349
  return 2;
 
350
}
 
351
 
 
352
 
 
353
/**
340
354
  Returns the number of bytes field uses in row-based replication
341
355
  row packed size.
342
356
 
358
372
}
359
373
 
360
374
 
 
375
/**
 
376
  Check to see if field size is compatible with destination.
 
377
 
 
378
  This method is used in row-based replication to verify that the slave's
 
379
  field size is less than or equal to the master's field size. The
 
380
  encoded field metadata (from the master or source) is decoded and compared
 
381
  to the size of this field (the slave or destination).
 
382
 
 
383
  @param   field_metadata   Encoded size in field metadata
 
384
 
 
385
  @retval 0 if this field's size is < the source field's size
 
386
  @retval 1 if this field's size is >= the source field's size
 
387
*/
 
388
int Field_decimal::compatible_field_size(uint32_t field_metadata)
 
389
{
 
390
  int compatible= 0;
 
391
  uint32_t const source_precision= (field_metadata >> 8U) & 0x00ff;
 
392
  uint32_t const source_decimal= field_metadata & 0x00ff;
 
393
  uint32_t const source_size= my_decimal_get_binary_size(source_precision,
 
394
                                                         source_decimal);
 
395
  uint32_t const destination_size= row_pack_length();
 
396
  compatible= (source_size <= destination_size);
 
397
  if (compatible)
 
398
    compatible= (source_precision <= precision) &&
 
399
      (source_decimal <= decimals());
 
400
  return (compatible);
 
401
}
 
402
 
 
403
 
361
404
uint32_t Field_decimal::is_equal(CreateField *new_field_ptr)
362
405
{
363
406
  return ((new_field_ptr->sql_type == real_type()) &&
423
466
  return from+len;
424
467
}
425
468
 
426
 
} /* namespace drizzled */