~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/decimal.cc

merged with up to date trunk

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;
 
31
extern type::Decimal decimal_zero;
32
32
 
33
33
/****************************************************************************
34
34
 ** File_decimal
52
52
           dec_arg, zero_arg,
53
53
           unsigned_arg)
54
54
{
55
 
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
 
55
  precision= class_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
56
56
  set_if_smaller(precision, (uint32_t)DECIMAL_MAX_PRECISION);
57
57
  assert((precision <= DECIMAL_MAX_PRECISION) &&
58
58
         (dec <= DECIMAL_MAX_SCALE));
59
 
  bin_size= my_decimal_get_binary_size(precision, dec);
 
59
  bin_size= class_decimal_get_binary_size(precision, dec);
60
60
}
61
61
 
62
62
Field_decimal::Field_decimal(uint32_t len_arg,
74
74
           0,
75
75
           unsigned_arg)
76
76
{
77
 
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
 
77
  precision= class_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
78
78
  set_if_smaller(precision, (uint32_t)DECIMAL_MAX_PRECISION);
79
79
  assert((precision <= DECIMAL_MAX_PRECISION) &&
80
80
         (dec <= DECIMAL_MAX_SCALE));
81
 
  bin_size= my_decimal_get_binary_size(precision, dec);
 
81
  bin_size= class_decimal_get_binary_size(precision, dec);
82
82
}
83
83
 
84
84
 
96
96
  @param sign              sign of value which caused overflow
97
97
*/
98
98
 
99
 
void Field_decimal::set_value_on_overflow(my_decimal *decimal_value,
 
99
void Field_decimal::set_value_on_overflow(type::Decimal *decimal_value,
100
100
                                          bool sign)
101
101
{
102
 
  max_my_decimal(decimal_value, precision, decimals());
 
102
  max_Decimal(decimal_value, precision, decimals());
103
103
  if (sign)
104
104
    decimal_value->sign(true);
105
105
 
114
114
  If it does, stores the decimal in the buffer using binary format.
115
115
  Otherwise sets maximal number that can be stored in the field.
116
116
 
117
 
  @param decimal_value   my_decimal
 
117
  @param decimal_value   type::Decimal
118
118
 
119
119
  @retval
120
120
  0 ok
122
122
  1 error
123
123
*/
124
124
 
125
 
bool Field_decimal::store_value(const my_decimal *decimal_value)
 
125
bool Field_decimal::store_value(const type::Decimal *decimal_value)
126
126
{
127
 
  int error= my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
128
 
                                         decimal_value, ptr, precision, dec);
 
127
  int error= decimal_value->val_binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, ptr, precision, dec);
 
128
 
129
129
  if (warn_if_overflow(error))
130
130
  {
131
131
    if (error != E_DEC_TRUNCATED)
132
132
    {
133
 
      my_decimal buff;
 
133
      type::Decimal buff;
134
134
      set_value_on_overflow(&buff, decimal_value->sign());
135
 
      my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
 
135
      buff.val_binary(E_DEC_FATAL_ERROR, ptr, precision, dec);
136
136
    }
137
137
    error= 1;
138
138
  }
 
139
 
139
140
  return(error);
140
141
}
141
142
 
144
145
                         const CHARSET_INFO * const charset_arg)
145
146
{
146
147
  int err;
147
 
  my_decimal decimal_value;
 
148
  type::Decimal decimal_value;
148
149
 
149
150
  ASSERT_COLUMN_MARKED_FOR_WRITE;
150
151
 
151
 
  if ((err= str2my_decimal(E_DEC_FATAL_ERROR &
 
152
  if ((err= decimal_value.store(E_DEC_FATAL_ERROR &
152
153
                           ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
153
 
                           from, length, charset_arg,
154
 
                           &decimal_value)) &&
 
154
                           from, length, charset_arg)) &&
155
155
      getTable()->in_use->abort_on_warning)
156
156
  {
157
157
    /* Because "from" is not NUL-terminated and we use %s in the ER() */
187
187
                          ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
188
188
                          "decimal", from_as_str.c_ptr(), field_name,
189
189
                          (uint32_t) getTable()->in_use->row_count);
190
 
      my_decimal_set_zero(&decimal_value);
 
190
      decimal_value.set_zero();
191
191
 
192
192
      break;
193
193
    }
200
200
 
201
201
/**
202
202
  @todo
203
 
  Fix following when double2my_decimal when double2decimal
 
203
  Fix following when double2_class_decimal when double2decimal
204
204
  will return E_DEC_TRUNCATED always correctly
205
205
*/
206
206
 
207
207
int Field_decimal::store(double nr)
208
208
{
209
 
  my_decimal decimal_value;
 
209
  type::Decimal decimal_value;
210
210
  int err;
211
211
 
212
212
  ASSERT_COLUMN_MARKED_FOR_WRITE;
213
213
 
214
 
  err= double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
 
214
  err= double2_class_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
215
215
                         &decimal_value);
216
216
  if (err)
217
217
  {
230
230
 
231
231
int Field_decimal::store(int64_t nr, bool unsigned_val)
232
232
{
233
 
  my_decimal decimal_value;
 
233
  type::Decimal decimal_value;
234
234
  int err;
235
235
 
236
236
  ASSERT_COLUMN_MARKED_FOR_WRITE;
237
237
 
238
 
  if ((err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
 
238
  if ((err= int2_class_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
239
239
                           nr, unsigned_val, &decimal_value)))
240
240
  {
241
241
    if (check_overflow(err))
251
251
}
252
252
 
253
253
 
254
 
int Field_decimal::store_decimal(const my_decimal *decimal_value)
 
254
int Field_decimal::store_decimal(const type::Decimal *decimal_value)
255
255
{
256
256
  return store_value(decimal_value);
257
257
}
258
258
 
259
259
 
260
 
int Field_decimal::store_time(DRIZZLE_TIME *ltime,
 
260
int Field_decimal::store_time(type::Time *ltime,
261
261
                              enum enum_drizzle_timestamp_type )
262
262
{
263
 
  my_decimal decimal_value;
264
 
  return store_value(date2my_decimal(ltime, &decimal_value));
 
263
  type::Decimal decimal_value;
 
264
  return store_value(date2_class_decimal(ltime, &decimal_value));
265
265
}
266
266
 
267
267
 
268
268
double Field_decimal::val_real(void)
269
269
{
270
270
  double dbl;
271
 
  my_decimal decimal_value;
 
271
  type::Decimal decimal_value;
272
272
 
273
273
  ASSERT_COLUMN_MARKED_FOR_READ;
274
274
 
275
 
  my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
 
275
  class_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
276
276
 
277
277
  return dbl;
278
278
}
281
281
int64_t Field_decimal::val_int(void)
282
282
{
283
283
  int64_t i;
284
 
  my_decimal decimal_value;
 
284
  type::Decimal decimal_value;
285
285
 
286
286
  ASSERT_COLUMN_MARKED_FOR_READ;
287
287
 
288
 
  my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), false, &i);
 
288
  val_decimal(&decimal_value)->val_int32(E_DEC_FATAL_ERROR, false, &i);
289
289
 
290
290
  return i;
291
291
}
292
292
 
293
293
 
294
 
my_decimal* Field_decimal::val_decimal(my_decimal *decimal_value)
 
294
type::Decimal* Field_decimal::val_decimal(type::Decimal *decimal_value)
295
295
{
296
296
  ASSERT_COLUMN_MARKED_FOR_READ;
297
297
 
298
 
  binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
 
298
  binary2_class_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
299
299
                    precision, dec);
300
300
  return(decimal_value);
301
301
}
304
304
String *Field_decimal::val_str(String *val_buffer,
305
305
                               String *)
306
306
{
307
 
  my_decimal decimal_value;
 
307
  type::Decimal decimal_value;
308
308
 
309
309
  ASSERT_COLUMN_MARKED_FOR_READ;
310
310
 
311
311
  uint32_t fixed_precision= decimal_precision ? precision : 0;
312
 
  my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
 
312
  class_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
313
313
                    fixed_precision, dec, '0', val_buffer);
314
314
  return val_buffer;
315
315
}
352
352
{
353
353
  uint32_t const source_precision= (field_metadata >> 8U) & 0x00ff;
354
354
  uint32_t const source_decimal= field_metadata & 0x00ff;
355
 
  uint32_t const source_size= my_decimal_get_binary_size(source_precision,
 
355
  uint32_t const source_size= class_decimal_get_binary_size(source_precision,
356
356
                                                         source_decimal);
357
357
  return (source_size);
358
358
}
394
394
  uint32_t from_precision= (param_data & 0xff00) >> 8U;
395
395
  uint32_t from_decimal= param_data & 0x00ff;
396
396
  uint32_t length=pack_length();
397
 
  uint32_t from_pack_len= my_decimal_get_binary_size(from_precision, from_decimal);
 
397
  uint32_t from_pack_len= class_decimal_get_binary_size(from_precision, from_decimal);
398
398
  uint32_t len= (param_data && (from_pack_len < length)) ?
399
399
    from_pack_len : length;
400
400
  if ((from_pack_len && (from_pack_len < length)) ||