~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/decimal.cc

  • Committer: Daniel Nichter
  • Date: 2011-10-23 16:01:37 UTC
  • mto: This revision was merged to the branch mainline in revision 2448.
  • Revision ID: daniel@percona.com-20111023160137-7ac3blgz8z4tf8za
Add Administration Getting Started and Logging.  Capitalize SQL clause keywords.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
 
22
 
#include <drizzled/server_includes.h>
23
 
#include <drizzled/field/fdecimal.h>
 
22
#include <config.h>
 
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
#include <drizzled/create_field.h>
27
28
 
28
 
extern my_decimal decimal_zero;
 
29
namespace drizzled {
29
30
 
30
31
/****************************************************************************
31
 
** Field_new_decimal
32
 
****************************************************************************/
33
 
 
34
 
Field_new_decimal::Field_new_decimal(unsigned char *ptr_arg,
35
 
                                     uint32_t len_arg, unsigned char *null_ptr_arg,
36
 
                                     unsigned char null_bit_arg,
37
 
                                     enum utype unireg_check_arg,
38
 
                                     const char *field_name_arg,
39
 
                                     uint8_t dec_arg,bool zero_arg,
40
 
                                     bool unsigned_arg)
41
 
  :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
42
 
             unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg)
43
 
{
44
 
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
45
 
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
46
 
  assert((precision <= DECIMAL_MAX_PRECISION) &&
47
 
              (dec <= DECIMAL_MAX_SCALE));
48
 
  bin_size= my_decimal_get_binary_size(precision, dec);
49
 
}
50
 
 
51
 
 
52
 
Field_new_decimal::Field_new_decimal(uint32_t len_arg,
53
 
                                     bool maybe_null_arg,
54
 
                                     const char *name,
55
 
                                     uint8_t dec_arg,
56
 
                                     bool unsigned_arg)
57
 
  :Field_num((unsigned char*) 0, len_arg,
58
 
             maybe_null_arg ? (unsigned char*) "": 0, 0,
59
 
             NONE, name, dec_arg, 0, unsigned_arg)
60
 
{
61
 
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
62
 
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
63
 
  assert((precision <= DECIMAL_MAX_PRECISION) &&
64
 
              (dec <= DECIMAL_MAX_SCALE));
65
 
  bin_size= my_decimal_get_binary_size(precision, dec);
66
 
}
67
 
 
68
 
 
69
 
int Field_new_decimal::reset(void)
 
32
 ** File_decimal
 
33
 ****************************************************************************/
 
34
 
 
35
Field_decimal::Field_decimal(unsigned char *ptr_arg,
 
36
                             uint32_t len_arg,
 
37
                             unsigned char *null_ptr_arg,
 
38
                             unsigned char null_bit_arg,
 
39
                             enum utype unireg_check_arg,
 
40
                             const char *field_name_arg,
 
41
                             uint8_t dec_arg) :
 
42
  Field_num(ptr_arg,
 
43
            len_arg,
 
44
            null_ptr_arg,
 
45
            null_bit_arg,
 
46
            unireg_check_arg,
 
47
            field_name_arg,
 
48
            dec_arg, false,
 
49
            false)
 
50
  {
 
51
    precision= class_decimal_length_to_precision(len_arg, dec_arg, false);
 
52
    set_if_smaller(precision, (uint32_t)DECIMAL_MAX_PRECISION);
 
53
    assert((precision <= DECIMAL_MAX_PRECISION) && (dec <= DECIMAL_MAX_SCALE));
 
54
    bin_size= class_decimal_get_binary_size(precision, dec);
 
55
  }
 
56
 
 
57
Field_decimal::Field_decimal(uint32_t len_arg,
 
58
                             bool maybe_null_arg,
 
59
                             const char *name,
 
60
                             uint8_t dec_arg,
 
61
                             bool unsigned_arg) :
 
62
  Field_num((unsigned char*) 0,
 
63
            len_arg,
 
64
            maybe_null_arg ? (unsigned char*) "": 0,
 
65
            0,
 
66
            NONE,
 
67
            name,
 
68
            dec_arg,
 
69
            0,
 
70
            unsigned_arg)
 
71
{
 
72
  precision= class_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
 
73
  set_if_smaller(precision, (uint32_t)DECIMAL_MAX_PRECISION);
 
74
  assert((precision <= DECIMAL_MAX_PRECISION) &&
 
75
         (dec <= DECIMAL_MAX_SCALE));
 
76
  bin_size= class_decimal_get_binary_size(precision, dec);
 
77
}
 
78
 
 
79
 
 
80
int Field_decimal::reset(void)
70
81
{
71
82
  store_value(&decimal_zero);
72
83
  return 0;
80
91
  @param sign              sign of value which caused overflow
81
92
*/
82
93
 
83
 
void Field_new_decimal::set_value_on_overflow(my_decimal *decimal_value,
84
 
                                              bool sign)
 
94
void Field_decimal::set_value_on_overflow(type::Decimal *decimal_value,
 
95
                                          bool sign)
85
96
{
86
 
  max_my_decimal(decimal_value, precision, decimals());
 
97
  max_Decimal(decimal_value, precision, decimals());
87
98
  if (sign)
88
99
    decimal_value->sign(true);
89
100
 
98
109
  If it does, stores the decimal in the buffer using binary format.
99
110
  Otherwise sets maximal number that can be stored in the field.
100
111
 
101
 
  @param decimal_value   my_decimal
 
112
  @param decimal_value   type::Decimal
102
113
 
103
114
  @retval
104
 
    0 ok
 
115
  0 ok
105
116
  @retval
106
 
    1 error
 
117
  1 error
107
118
*/
108
119
 
109
 
bool Field_new_decimal::store_value(const my_decimal *decimal_value)
 
120
bool Field_decimal::store_value(const type::Decimal *decimal_value)
110
121
{
111
 
  int error= 0;
 
122
  int error= decimal_value->val_binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, ptr, precision, dec);
112
123
 
113
 
  if (warn_if_overflow(my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
114
 
                                         decimal_value, ptr, precision, dec)))
 
124
  if (warn_if_overflow(error))
115
125
  {
116
 
    my_decimal buff;
117
 
    set_value_on_overflow(&buff, decimal_value->sign());
118
 
    my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
 
126
    if (error != E_DEC_TRUNCATED)
 
127
    {
 
128
      type::Decimal buff;
 
129
      set_value_on_overflow(&buff, decimal_value->sign());
 
130
      buff.val_binary(E_DEC_FATAL_ERROR, ptr, precision, dec);
 
131
    }
119
132
    error= 1;
120
133
  }
 
134
 
121
135
  return(error);
122
136
}
123
137
 
124
138
 
125
 
int Field_new_decimal::store(const char *from, uint32_t length,
126
 
                             const CHARSET_INFO * const charset_arg)
 
139
int Field_decimal::store(const char *from, uint32_t length,
 
140
                         const charset_info_st * const charset_arg)
127
141
{
128
142
  int err;
129
 
  my_decimal decimal_value;
130
 
 
131
 
  if ((err= str2my_decimal(E_DEC_FATAL_ERROR &
 
143
  type::Decimal decimal_value;
 
144
 
 
145
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
146
 
 
147
  if ((err= decimal_value.store(E_DEC_FATAL_ERROR &
132
148
                           ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
133
 
                           from, length, charset_arg,
134
 
                           &decimal_value)) &&
135
 
      table->in_use->abort_on_warning)
 
149
                           from, length, charset_arg)) &&
 
150
      getTable()->in_use->abortOnWarning())
136
151
  {
137
152
    /* Because "from" is not NUL-terminated and we use %s in the ER() */
138
153
    String from_as_str;
139
154
    from_as_str.copy(from, length, &my_charset_bin);
140
155
 
141
 
    push_warning_printf(table->in_use, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
156
    push_warning_printf(getTable()->in_use, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
142
157
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
143
158
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
144
159
                        "decimal", from_as_str.c_ptr(), field_name,
145
 
                        (uint32_t) table->in_use->row_count);
 
160
                        (uint32_t) getTable()->in_use->row_count);
146
161
 
147
162
    return(err);
148
163
  }
149
164
 
150
165
  switch (err) {
151
166
  case E_DEC_TRUNCATED:
152
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE, ER_WARN_DATA_TRUNCATED, 1);
 
167
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
 
168
    set_value_on_overflow(&decimal_value, decimal_value.sign());
153
169
    break;
154
170
  case E_DEC_OVERFLOW:
155
171
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
161
177
      String from_as_str;
162
178
      from_as_str.copy(from, length, &my_charset_bin);
163
179
 
164
 
    push_warning_printf(table->in_use, DRIZZLE_ERROR::WARN_LEVEL_WARN,
165
 
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
166
 
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
 
180
      push_warning_printf(getTable()->in_use, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
181
                          ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
 
182
                          ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
167
183
                          "decimal", from_as_str.c_ptr(), field_name,
168
 
                        (uint32_t) table->in_use->row_count);
169
 
    my_decimal_set_zero(&decimal_value);
 
184
                          (uint32_t) getTable()->in_use->row_count);
 
185
      decimal_value.set_zero();
170
186
 
171
 
    break;
 
187
      break;
172
188
    }
173
189
  }
174
190
 
179
195
 
180
196
/**
181
197
  @todo
182
 
  Fix following when double2my_decimal when double2decimal
 
198
  Fix following when double2_class_decimal when double2decimal
183
199
  will return E_DEC_TRUNCATED always correctly
184
200
*/
185
201
 
186
 
int Field_new_decimal::store(double nr)
 
202
int Field_decimal::store(double nr)
187
203
{
188
 
  my_decimal decimal_value;
 
204
  type::Decimal decimal_value;
189
205
  int err;
190
206
 
191
 
  err= double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
 
207
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
208
 
 
209
  err= double2_class_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
192
210
                         &decimal_value);
193
211
  if (err)
194
212
  {
195
213
    if (check_overflow(err))
196
214
      set_value_on_overflow(&decimal_value, decimal_value.sign());
197
215
    /* Only issue a warning if store_value doesn't issue an warning */
198
 
    table->in_use->got_warning= 0;
 
216
    getTable()->in_use->got_warning= 0;
199
217
  }
200
218
  if (store_value(&decimal_value))
201
219
    err= 1;
202
 
  else if (err && !table->in_use->got_warning)
 
220
  else if (err && !getTable()->in_use->got_warning)
203
221
    err= warn_if_overflow(err);
204
222
  return(err);
205
223
}
206
224
 
207
225
 
208
 
int Field_new_decimal::store(int64_t nr, bool unsigned_val)
 
226
int Field_decimal::store(int64_t nr, bool unsigned_val)
209
227
{
210
 
  my_decimal decimal_value;
 
228
  type::Decimal decimal_value;
211
229
  int err;
212
230
 
213
 
  if ((err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
 
231
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
232
 
 
233
  if ((err= int2_class_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
214
234
                           nr, unsigned_val, &decimal_value)))
215
235
  {
216
236
    if (check_overflow(err))
217
237
      set_value_on_overflow(&decimal_value, decimal_value.sign());
218
238
    /* Only issue a warning if store_value doesn't issue an warning */
219
 
    table->in_use->got_warning= 0;
 
239
    getTable()->in_use->got_warning= 0;
220
240
  }
221
241
  if (store_value(&decimal_value))
222
242
    err= 1;
223
 
  else if (err && !table->in_use->got_warning)
 
243
  else if (err && not getTable()->in_use->got_warning)
224
244
    err= warn_if_overflow(err);
225
245
  return err;
226
246
}
227
247
 
228
248
 
229
 
int Field_new_decimal::store_decimal(const my_decimal *decimal_value)
 
249
int Field_decimal::store_decimal(const type::Decimal *decimal_value)
230
250
{
231
251
  return store_value(decimal_value);
232
252
}
233
253
 
234
254
 
235
 
int Field_new_decimal::store_time(DRIZZLE_TIME *ltime,
236
 
                                  enum enum_drizzle_timestamp_type t_type __attribute__((unused)))
 
255
int Field_decimal::store_time(type::Time &ltime,
 
256
                              type::timestamp_t )
237
257
{
238
 
    my_decimal decimal_value;
239
 
    return store_value(date2my_decimal(ltime, &decimal_value));
 
258
  type::Decimal decimal_value;
 
259
  return store_value(date2_class_decimal(&ltime, &decimal_value));
240
260
}
241
261
 
242
262
 
243
 
double Field_new_decimal::val_real(void)
 
263
double Field_decimal::val_real(void) const
244
264
{
245
265
  double dbl;
246
 
  my_decimal decimal_value;
247
 
  my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
 
266
  type::Decimal decimal_value;
 
267
 
 
268
  ASSERT_COLUMN_MARKED_FOR_READ;
 
269
 
 
270
  class_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
 
271
 
248
272
  return dbl;
249
273
}
250
274
 
251
275
 
252
 
int64_t Field_new_decimal::val_int(void)
 
276
int64_t Field_decimal::val_int(void) const
253
277
{
254
278
  int64_t i;
255
 
  my_decimal decimal_value;
256
 
  my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), false, &i);
 
279
  type::Decimal decimal_value;
 
280
 
 
281
  ASSERT_COLUMN_MARKED_FOR_READ;
 
282
 
 
283
  val_decimal(&decimal_value)->val_int32(E_DEC_FATAL_ERROR, false, &i);
 
284
 
257
285
  return i;
258
286
}
259
287
 
260
288
 
261
 
my_decimal* Field_new_decimal::val_decimal(my_decimal *decimal_value)
 
289
type::Decimal* Field_decimal::val_decimal(type::Decimal *decimal_value) const
262
290
{
263
 
  binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
 
291
  ASSERT_COLUMN_MARKED_FOR_READ;
 
292
 
 
293
  binary2_class_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
264
294
                    precision, dec);
265
295
  return(decimal_value);
266
296
}
267
297
 
268
298
 
269
 
String *Field_new_decimal::val_str(String *val_buffer,
270
 
                                   String *val_ptr __attribute__((unused)))
 
299
String *Field_decimal::val_str(String *val_buffer, String *) const
271
300
{
272
 
  my_decimal decimal_value;
273
 
  uint32_t fixed_precision= decimal_precision ? precision : 0;
274
 
  my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
275
 
                    fixed_precision, dec, '0', val_buffer);
 
301
  type::Decimal decimal_value;
 
302
 
 
303
  ASSERT_COLUMN_MARKED_FOR_READ;
 
304
 
 
305
  class_decimal2string(val_decimal(&decimal_value),
 
306
                       dec, val_buffer);
276
307
  return val_buffer;
277
308
}
278
309
 
279
310
 
280
 
int Field_new_decimal::cmp(const unsigned char *a,const unsigned char*b)
 
311
int Field_decimal::cmp(const unsigned char *a,const unsigned char*b)
281
312
{
282
313
  return memcmp(a, b, bin_size);
283
314
}
284
315
 
285
316
 
286
 
void Field_new_decimal::sort_string(unsigned char *buff,
287
 
                                    uint32_t length __attribute__((unused)))
 
317
void Field_decimal::sort_string(unsigned char *buff, uint32_t)
288
318
{
289
319
  memcpy(buff, ptr, bin_size);
290
320
}
291
321
 
292
 
 
293
 
void Field_new_decimal::sql_type(String &str) const
294
 
{
295
 
  const CHARSET_INFO * const cs= str.charset();
296
 
  str.length(cs->cset->snprintf(cs, (char*) str.ptr(), str.alloced_length(),
297
 
                                "decimal(%d,%d)", precision, (int)dec));
298
 
}
299
 
 
300
 
 
301
 
/**
302
 
   Save the field metadata for new decimal fields.
303
 
 
304
 
   Saves the precision in the first byte and decimals() in the second
305
 
   byte of the field metadata array at index of *metadata_ptr and 
306
 
   *(metadata_ptr + 1).
307
 
 
308
 
   @param   metadata_ptr   First byte of field metadata
309
 
 
310
 
   @returns number of bytes written to metadata_ptr
311
 
*/
312
 
int Field_new_decimal::do_save_field_metadata(unsigned char *metadata_ptr)
313
 
{
314
 
  *metadata_ptr= precision;
315
 
  *(metadata_ptr + 1)= decimals();
316
 
  return 2;
317
 
}
318
 
 
319
 
 
320
 
/**
321
 
   Returns the number of bytes field uses in row-based replication 
322
 
   row packed size.
323
 
 
324
 
   This method is used in row-based replication to determine the number
325
 
   of bytes that the field consumes in the row record format. This is
326
 
   used to skip fields in the master that do not exist on the slave.
327
 
 
328
 
   @param   field_metadata   Encoded size in field metadata
329
 
 
330
 
   @returns The size of the field based on the field metadata.
331
 
*/
332
 
uint32_t Field_new_decimal::pack_length_from_metadata(uint32_t field_metadata)
 
322
/**
 
323
  Returns the number of bytes field uses in row-based replication
 
324
  row packed size.
 
325
 
 
326
  This method is used in row-based replication to determine the number
 
327
  of bytes that the field consumes in the row record format. This is
 
328
  used to skip fields in the master that do not exist on the slave.
 
329
 
 
330
  @param   field_metadata   Encoded size in field metadata
 
331
 
 
332
  @returns The size of the field based on the field metadata.
 
333
*/
 
334
uint32_t Field_decimal::pack_length_from_metadata(uint32_t field_metadata)
333
335
{
334
336
  uint32_t const source_precision= (field_metadata >> 8U) & 0x00ff;
335
 
  uint32_t const source_decimal= field_metadata & 0x00ff; 
336
 
  uint32_t const source_size= my_decimal_get_binary_size(source_precision, 
337
 
                                                     source_decimal);
 
337
  uint32_t const source_decimal= field_metadata & 0x00ff;
 
338
  uint32_t const source_size= class_decimal_get_binary_size(source_precision,
 
339
                                                         source_decimal);
338
340
  return (source_size);
339
341
}
340
342
 
341
343
 
342
 
/**
343
 
   Check to see if field size is compatible with destination.
344
 
 
345
 
   This method is used in row-based replication to verify that the slave's
346
 
   field size is less than or equal to the master's field size. The 
347
 
   encoded field metadata (from the master or source) is decoded and compared
348
 
   to the size of this field (the slave or destination). 
349
 
 
350
 
   @param   field_metadata   Encoded size in field metadata
351
 
 
352
 
   @retval 0 if this field's size is < the source field's size
353
 
   @retval 1 if this field's size is >= the source field's size
354
 
*/
355
 
int Field_new_decimal::compatible_field_size(uint32_t field_metadata)
356
 
{
357
 
  int compatible= 0;
358
 
  uint32_t const source_precision= (field_metadata >> 8U) & 0x00ff;
359
 
  uint32_t const source_decimal= field_metadata & 0x00ff; 
360
 
  uint32_t const source_size= my_decimal_get_binary_size(source_precision, 
361
 
                                                     source_decimal);
362
 
  uint32_t const destination_size= row_pack_length();
363
 
  compatible= (source_size <= destination_size);
364
 
  if (compatible)
365
 
    compatible= (source_precision <= precision) &&
366
 
                (source_decimal <= decimals());
367
 
  return (compatible);
368
 
}
369
 
 
370
 
 
371
 
uint32_t Field_new_decimal::is_equal(Create_field *new_field)
372
 
{
373
 
  return ((new_field->sql_type == real_type()) &&
374
 
          ((new_field->flags & UNSIGNED_FLAG) == 
375
 
           (uint) (flags & UNSIGNED_FLAG)) &&
376
 
          ((new_field->flags & AUTO_INCREMENT_FLAG) ==
377
 
           (uint) (flags & AUTO_INCREMENT_FLAG)) &&
378
 
          (new_field->length == max_display_length()) &&
379
 
          (new_field->decimals == dec));
380
 
}
381
 
 
382
 
 
383
 
/**
384
 
   Unpack a decimal field from row data.
385
 
 
386
 
   This method is used to unpack a decimal or numeric field from a master
387
 
   whose size of the field is less than that of the slave.
388
 
  
389
 
   @param   to         Destination of the data
390
 
   @param   from       Source of the data
391
 
   @param   param_data Precision (upper) and decimal (lower) values
392
 
 
393
 
   @return  New pointer into memory based on from + length of the data
 
344
uint32_t Field_decimal::is_equal(CreateField *new_field_ptr)
 
345
{
 
346
  return ((new_field_ptr->sql_type == real_type()) &&
 
347
          ((new_field_ptr->flags & UNSIGNED_FLAG) ==
 
348
           (uint32_t) (flags & UNSIGNED_FLAG)) &&
 
349
          ((new_field_ptr->flags & AUTO_INCREMENT_FLAG) ==
 
350
           (uint32_t) (flags & AUTO_INCREMENT_FLAG)) &&
 
351
          (new_field_ptr->length == max_display_length()) &&
 
352
          (new_field_ptr->decimals == dec));
 
353
}
 
354
 
 
355
 
 
356
/**
 
357
  Unpack a decimal field from row data.
 
358
 
 
359
  This method is used to unpack a decimal or numeric field from a master
 
360
  whose size of the field is less than that of the slave.
 
361
 
 
362
  @param   to         Destination of the data
 
363
  @param   from       Source of the data
 
364
  @param   param_data Precision (upper) and decimal (lower) values
 
365
 
 
366
  @return  New pointer into memory based on from + length of the data
394
367
*/
395
368
const unsigned char *
396
 
Field_new_decimal::unpack(unsigned char* to,
397
 
                          const unsigned char *from,
398
 
                          uint32_t param_data,
399
 
                          bool low_byte_first)
 
369
Field_decimal::unpack(unsigned char* to,
 
370
                      const unsigned char *from,
 
371
                      uint32_t param_data,
 
372
                      bool low_byte_first)
400
373
{
401
374
  if (param_data == 0)
402
375
    return Field::unpack(to, from, param_data, low_byte_first);
404
377
  uint32_t from_precision= (param_data & 0xff00) >> 8U;
405
378
  uint32_t from_decimal= param_data & 0x00ff;
406
379
  uint32_t length=pack_length();
407
 
  uint32_t from_pack_len= my_decimal_get_binary_size(from_precision, from_decimal);
 
380
  uint32_t from_pack_len= class_decimal_get_binary_size(from_precision, from_decimal);
408
381
  uint32_t len= (param_data && (from_pack_len < length)) ?
409
 
            from_pack_len : length;
 
382
    from_pack_len : length;
410
383
  if ((from_pack_len && (from_pack_len < length)) ||
411
384
      (from_precision < precision) ||
412
385
      (from_decimal < decimals()))
417
390
      a decimal and write that to the raw data buffer.
418
391
    */
419
392
    decimal_digit_t dec_buf[DECIMAL_MAX_PRECISION];
420
 
    decimal_t dec;
421
 
    dec.len= from_precision;
422
 
    dec.buf= dec_buf;
 
393
    decimal_t conv_dec;
 
394
    conv_dec.len= from_precision;
 
395
    conv_dec.buf= dec_buf;
423
396
    /*
424
 
      Note: bin2decimal does not change the length of the field. So it is
425
 
      just the first step the resizing operation. The second step does the
426
 
      resizing using the precision and decimals from the slave.
 
397
Note: bin2decimal does not change the length of the field. So it is
 
398
just the first step the resizing operation. The second step does the
 
399
resizing using the precision and decimals from the slave.
427
400
    */
428
 
    bin2decimal((unsigned char *)from, &dec, from_precision, from_decimal);
429
 
    decimal2bin(&dec, to, precision, decimals());
 
401
    bin2decimal((unsigned char *)from, &conv_dec, from_precision, from_decimal);
 
402
    decimal2bin(&conv_dec, to, precision, decimals());
430
403
  }
431
404
  else
432
405
    memcpy(to, from, len); // Sizes are the same, just copy the data.
433
406
  return from+len;
434
407
}
435
408
 
 
409
} /* namespace drizzled */