~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/decimal.cc

  • Committer: Monty Taylor
  • Date: 2010-08-12 20:27:32 UTC
  • mto: (1720.1.5 build)
  • mto: This revision was merged to the branch mainline in revision 1722.
  • Revision ID: mordred@inaugust.com-20100812202732-9kzchbkvkyki4n3u
Merged libdrizzle directly into tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
namespace drizzled
29
29
{
30
30
 
 
31
extern my_decimal decimal_zero;
 
32
 
31
33
/****************************************************************************
32
34
 ** File_decimal
33
35
 ****************************************************************************/
38
40
                             unsigned char null_bit_arg,
39
41
                             enum utype unireg_check_arg,
40
42
                             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
 
  }
 
43
                             uint8_t dec_arg,
 
44
                             bool zero_arg,
 
45
                             bool unsigned_arg)
 
46
:Field_num(ptr_arg,
 
47
           len_arg,
 
48
           null_ptr_arg,
 
49
           null_bit_arg,
 
50
           unireg_check_arg,
 
51
           field_name_arg,
 
52
           dec_arg, zero_arg,
 
53
           unsigned_arg)
 
54
{
 
55
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
 
56
  set_if_smaller(precision, (uint32_t)DECIMAL_MAX_PRECISION);
 
57
  assert((precision <= DECIMAL_MAX_PRECISION) &&
 
58
         (dec <= DECIMAL_MAX_SCALE));
 
59
  bin_size= my_decimal_get_binary_size(precision, dec);
 
60
}
56
61
 
57
62
Field_decimal::Field_decimal(uint32_t len_arg,
58
63
                             bool maybe_null_arg,
59
64
                             const char *name,
60
65
                             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)
 
66
                             bool unsigned_arg)
 
67
:Field_num((unsigned char*) 0,
 
68
           len_arg,
 
69
           maybe_null_arg ? (unsigned char*) "": 0,
 
70
           0,
 
71
           NONE,
 
72
           name,
 
73
           dec_arg,
 
74
           0,
 
75
           unsigned_arg)
71
76
{
72
 
  precision= class_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
 
77
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
73
78
  set_if_smaller(precision, (uint32_t)DECIMAL_MAX_PRECISION);
74
79
  assert((precision <= DECIMAL_MAX_PRECISION) &&
75
80
         (dec <= DECIMAL_MAX_SCALE));
76
 
  bin_size= class_decimal_get_binary_size(precision, dec);
 
81
  bin_size= my_decimal_get_binary_size(precision, dec);
77
82
}
78
83
 
79
84
 
91
96
  @param sign              sign of value which caused overflow
92
97
*/
93
98
 
94
 
void Field_decimal::set_value_on_overflow(type::Decimal *decimal_value,
 
99
void Field_decimal::set_value_on_overflow(my_decimal *decimal_value,
95
100
                                          bool sign)
96
101
{
97
 
  max_Decimal(decimal_value, precision, decimals());
 
102
  max_my_decimal(decimal_value, precision, decimals());
98
103
  if (sign)
99
104
    decimal_value->sign(true);
100
105
 
109
114
  If it does, stores the decimal in the buffer using binary format.
110
115
  Otherwise sets maximal number that can be stored in the field.
111
116
 
112
 
  @param decimal_value   type::Decimal
 
117
  @param decimal_value   my_decimal
113
118
 
114
119
  @retval
115
120
  0 ok
117
122
  1 error
118
123
*/
119
124
 
120
 
bool Field_decimal::store_value(const type::Decimal *decimal_value)
 
125
bool Field_decimal::store_value(const my_decimal *decimal_value)
121
126
{
122
 
  int error= decimal_value->val_binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, ptr, precision, dec);
 
127
  int error= 0;
123
128
 
124
 
  if (warn_if_overflow(error))
 
129
  if (warn_if_overflow(my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
 
130
                                         decimal_value, ptr, precision, dec)))
125
131
  {
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
 
    }
 
132
    my_decimal buff;
 
133
    set_value_on_overflow(&buff, decimal_value->sign());
 
134
    my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
132
135
    error= 1;
133
136
  }
134
 
 
135
137
  return(error);
136
138
}
137
139
 
140
142
                         const CHARSET_INFO * const charset_arg)
141
143
{
142
144
  int err;
143
 
  type::Decimal decimal_value;
 
145
  my_decimal decimal_value;
144
146
 
145
147
  ASSERT_COLUMN_MARKED_FOR_WRITE;
146
148
 
147
 
  if ((err= decimal_value.store(E_DEC_FATAL_ERROR &
 
149
  if ((err= str2my_decimal(E_DEC_FATAL_ERROR &
148
150
                           ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
149
 
                           from, length, charset_arg)) &&
150
 
      getTable()->in_use->abortOnWarning())
 
151
                           from, length, charset_arg,
 
152
                           &decimal_value)) &&
 
153
      getTable()->in_use->abort_on_warning)
151
154
  {
152
155
    /* Because "from" is not NUL-terminated and we use %s in the ER() */
153
156
    String from_as_str;
182
185
                          ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
183
186
                          "decimal", from_as_str.c_ptr(), field_name,
184
187
                          (uint32_t) getTable()->in_use->row_count);
185
 
      decimal_value.set_zero();
 
188
      my_decimal_set_zero(&decimal_value);
186
189
 
187
190
      break;
188
191
    }
195
198
 
196
199
/**
197
200
  @todo
198
 
  Fix following when double2_class_decimal when double2decimal
 
201
  Fix following when double2my_decimal when double2decimal
199
202
  will return E_DEC_TRUNCATED always correctly
200
203
*/
201
204
 
202
205
int Field_decimal::store(double nr)
203
206
{
204
 
  type::Decimal decimal_value;
 
207
  my_decimal decimal_value;
205
208
  int err;
206
209
 
207
210
  ASSERT_COLUMN_MARKED_FOR_WRITE;
208
211
 
209
 
  err= double2_class_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
 
212
  err= double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
210
213
                         &decimal_value);
211
214
  if (err)
212
215
  {
225
228
 
226
229
int Field_decimal::store(int64_t nr, bool unsigned_val)
227
230
{
228
 
  type::Decimal decimal_value;
 
231
  my_decimal decimal_value;
229
232
  int err;
230
233
 
231
234
  ASSERT_COLUMN_MARKED_FOR_WRITE;
232
235
 
233
 
  if ((err= int2_class_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
 
236
  if ((err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
234
237
                           nr, unsigned_val, &decimal_value)))
235
238
  {
236
239
    if (check_overflow(err))
246
249
}
247
250
 
248
251
 
249
 
int Field_decimal::store_decimal(const type::Decimal *decimal_value)
 
252
int Field_decimal::store_decimal(const my_decimal *decimal_value)
250
253
{
251
254
  return store_value(decimal_value);
252
255
}
253
256
 
254
257
 
255
 
int Field_decimal::store_time(type::Time &ltime,
256
 
                              type::timestamp_t )
 
258
int Field_decimal::store_time(DRIZZLE_TIME *ltime,
 
259
                              enum enum_drizzle_timestamp_type )
257
260
{
258
 
  type::Decimal decimal_value;
259
 
  return store_value(date2_class_decimal(&ltime, &decimal_value));
 
261
  my_decimal decimal_value;
 
262
  return store_value(date2my_decimal(ltime, &decimal_value));
260
263
}
261
264
 
262
265
 
263
266
double Field_decimal::val_real(void)
264
267
{
265
268
  double dbl;
266
 
  type::Decimal decimal_value;
 
269
  my_decimal decimal_value;
267
270
 
268
271
  ASSERT_COLUMN_MARKED_FOR_READ;
269
272
 
270
 
  class_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
 
273
  my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
271
274
 
272
275
  return dbl;
273
276
}
276
279
int64_t Field_decimal::val_int(void)
277
280
{
278
281
  int64_t i;
279
 
  type::Decimal decimal_value;
 
282
  my_decimal decimal_value;
280
283
 
281
284
  ASSERT_COLUMN_MARKED_FOR_READ;
282
285
 
283
 
  val_decimal(&decimal_value)->val_int32(E_DEC_FATAL_ERROR, false, &i);
 
286
  my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), false, &i);
284
287
 
285
288
  return i;
286
289
}
287
290
 
288
291
 
289
 
type::Decimal* Field_decimal::val_decimal(type::Decimal *decimal_value)
 
292
my_decimal* Field_decimal::val_decimal(my_decimal *decimal_value)
290
293
{
291
294
  ASSERT_COLUMN_MARKED_FOR_READ;
292
295
 
293
 
  binary2_class_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
 
296
  binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
294
297
                    precision, dec);
295
298
  return(decimal_value);
296
299
}
299
302
String *Field_decimal::val_str(String *val_buffer,
300
303
                               String *)
301
304
{
302
 
  type::Decimal decimal_value;
 
305
  my_decimal decimal_value;
303
306
 
304
307
  ASSERT_COLUMN_MARKED_FOR_READ;
305
308
 
306
 
  class_decimal2string(val_decimal(&decimal_value),
307
 
                       dec, val_buffer);
 
309
  uint32_t fixed_precision= decimal_precision ? precision : 0;
 
310
  my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
 
311
                    fixed_precision, dec, '0', val_buffer);
308
312
  return val_buffer;
309
313
}
310
314
 
346
350
{
347
351
  uint32_t const source_precision= (field_metadata >> 8U) & 0x00ff;
348
352
  uint32_t const source_decimal= field_metadata & 0x00ff;
349
 
  uint32_t const source_size= class_decimal_get_binary_size(source_precision,
 
353
  uint32_t const source_size= my_decimal_get_binary_size(source_precision,
350
354
                                                         source_decimal);
351
355
  return (source_size);
352
356
}
388
392
  uint32_t from_precision= (param_data & 0xff00) >> 8U;
389
393
  uint32_t from_decimal= param_data & 0x00ff;
390
394
  uint32_t length=pack_length();
391
 
  uint32_t from_pack_len= class_decimal_get_binary_size(from_precision, from_decimal);
 
395
  uint32_t from_pack_len= my_decimal_get_binary_size(from_precision, from_decimal);
392
396
  uint32_t len= (param_data && (from_pack_len < length)) ?
393
397
    from_pack_len : length;
394
398
  if ((from_pack_len && (from_pack_len < length)) ||