~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/decimal.cc

  • Committer: Brian Aker
  • Date: 2010-10-28 17:12:01 UTC
  • mfrom: (1887.1.3 merge)
  • Revision ID: brian@tangent.org-20101028171201-baj6l1bnntn1s4ad
Merge in POTFILES changes.

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);
123
 
 
 
127
  int error= my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
 
128
                                         decimal_value, ptr, precision, dec);
124
129
  if (warn_if_overflow(error))
125
130
  {
126
131
    if (error != E_DEC_TRUNCATED)
127
132
    {
128
 
      type::Decimal buff;
 
133
      my_decimal buff;
129
134
      set_value_on_overflow(&buff, decimal_value->sign());
130
 
      buff.val_binary(E_DEC_FATAL_ERROR, ptr, precision, dec);
 
135
      my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
131
136
    }
132
137
    error= 1;
133
138
  }
134
 
 
135
139
  return(error);
136
140
}
137
141
 
140
144
                         const CHARSET_INFO * const charset_arg)
141
145
{
142
146
  int err;
143
 
  type::Decimal decimal_value;
 
147
  my_decimal decimal_value;
144
148
 
145
149
  ASSERT_COLUMN_MARKED_FOR_WRITE;
146
150
 
147
 
  if ((err= decimal_value.store(E_DEC_FATAL_ERROR &
 
151
  if ((err= str2my_decimal(E_DEC_FATAL_ERROR &
148
152
                           ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
149
 
                           from, length, charset_arg)) &&
150
 
      getTable()->in_use->abortOnWarning())
 
153
                           from, length, charset_arg,
 
154
                           &decimal_value)) &&
 
155
      getTable()->in_use->abort_on_warning)
151
156
  {
152
157
    /* Because "from" is not NUL-terminated and we use %s in the ER() */
153
158
    String from_as_str;
182
187
                          ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
183
188
                          "decimal", from_as_str.c_ptr(), field_name,
184
189
                          (uint32_t) getTable()->in_use->row_count);
185
 
      decimal_value.set_zero();
 
190
      my_decimal_set_zero(&decimal_value);
186
191
 
187
192
      break;
188
193
    }
195
200
 
196
201
/**
197
202
  @todo
198
 
  Fix following when double2_class_decimal when double2decimal
 
203
  Fix following when double2my_decimal when double2decimal
199
204
  will return E_DEC_TRUNCATED always correctly
200
205
*/
201
206
 
202
207
int Field_decimal::store(double nr)
203
208
{
204
 
  type::Decimal decimal_value;
 
209
  my_decimal decimal_value;
205
210
  int err;
206
211
 
207
212
  ASSERT_COLUMN_MARKED_FOR_WRITE;
208
213
 
209
 
  err= double2_class_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
 
214
  err= double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
210
215
                         &decimal_value);
211
216
  if (err)
212
217
  {
225
230
 
226
231
int Field_decimal::store(int64_t nr, bool unsigned_val)
227
232
{
228
 
  type::Decimal decimal_value;
 
233
  my_decimal decimal_value;
229
234
  int err;
230
235
 
231
236
  ASSERT_COLUMN_MARKED_FOR_WRITE;
232
237
 
233
 
  if ((err= int2_class_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
 
238
  if ((err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
234
239
                           nr, unsigned_val, &decimal_value)))
235
240
  {
236
241
    if (check_overflow(err))
246
251
}
247
252
 
248
253
 
249
 
int Field_decimal::store_decimal(const type::Decimal *decimal_value)
 
254
int Field_decimal::store_decimal(const my_decimal *decimal_value)
250
255
{
251
256
  return store_value(decimal_value);
252
257
}
253
258
 
254
259
 
255
 
int Field_decimal::store_time(type::Time &ltime,
256
 
                              type::timestamp_t )
 
260
int Field_decimal::store_time(DRIZZLE_TIME *ltime,
 
261
                              enum enum_drizzle_timestamp_type )
257
262
{
258
 
  type::Decimal decimal_value;
259
 
  return store_value(date2_class_decimal(&ltime, &decimal_value));
 
263
  my_decimal decimal_value;
 
264
  return store_value(date2my_decimal(ltime, &decimal_value));
260
265
}
261
266
 
262
267
 
263
268
double Field_decimal::val_real(void)
264
269
{
265
270
  double dbl;
266
 
  type::Decimal decimal_value;
 
271
  my_decimal decimal_value;
267
272
 
268
273
  ASSERT_COLUMN_MARKED_FOR_READ;
269
274
 
270
 
  class_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
 
275
  my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
271
276
 
272
277
  return dbl;
273
278
}
276
281
int64_t Field_decimal::val_int(void)
277
282
{
278
283
  int64_t i;
279
 
  type::Decimal decimal_value;
 
284
  my_decimal decimal_value;
280
285
 
281
286
  ASSERT_COLUMN_MARKED_FOR_READ;
282
287
 
283
 
  val_decimal(&decimal_value)->val_int32(E_DEC_FATAL_ERROR, false, &i);
 
288
  my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), false, &i);
284
289
 
285
290
  return i;
286
291
}
287
292
 
288
293
 
289
 
type::Decimal* Field_decimal::val_decimal(type::Decimal *decimal_value)
 
294
my_decimal* Field_decimal::val_decimal(my_decimal *decimal_value)
290
295
{
291
296
  ASSERT_COLUMN_MARKED_FOR_READ;
292
297
 
293
 
  binary2_class_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
 
298
  binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
294
299
                    precision, dec);
295
300
  return(decimal_value);
296
301
}
299
304
String *Field_decimal::val_str(String *val_buffer,
300
305
                               String *)
301
306
{
302
 
  type::Decimal decimal_value;
 
307
  my_decimal decimal_value;
303
308
 
304
309
  ASSERT_COLUMN_MARKED_FOR_READ;
305
310
 
306
 
  class_decimal2string(val_decimal(&decimal_value),
307
 
                       dec, val_buffer);
 
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);
308
314
  return val_buffer;
309
315
}
310
316
 
346
352
{
347
353
  uint32_t const source_precision= (field_metadata >> 8U) & 0x00ff;
348
354
  uint32_t const source_decimal= field_metadata & 0x00ff;
349
 
  uint32_t const source_size= class_decimal_get_binary_size(source_precision,
 
355
  uint32_t const source_size= my_decimal_get_binary_size(source_precision,
350
356
                                                         source_decimal);
351
357
  return (source_size);
352
358
}
388
394
  uint32_t from_precision= (param_data & 0xff00) >> 8U;
389
395
  uint32_t from_decimal= param_data & 0x00ff;
390
396
  uint32_t length=pack_length();
391
 
  uint32_t from_pack_len= class_decimal_get_binary_size(from_precision, from_decimal);
 
397
  uint32_t from_pack_len= my_decimal_get_binary_size(from_precision, from_decimal);
392
398
  uint32_t len= (param_data && (from_pack_len < length)) ?
393
399
    from_pack_len : length;
394
400
  if ((from_pack_len && (from_pack_len < length)) ||