~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/decimal.cc

  • Committer: Brian Aker
  • Date: 2011-02-14 05:47:07 UTC
  • mto: This revision was merged to the branch mainline in revision 2167.
  • Revision ID: brian@tangent.org-20110214054707-61nsqgg1g4w1zhx1
Merge in all changes for current_session, etc.

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