~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/decimal.cc

  • Committer: Muhammad Umair
  • Date: 2011-08-16 11:47:29 UTC
  • mto: This revision was merged to the branch mainline in revision 2402.
  • Revision ID: umair@remotedesk-20110816114729-w6x88fj0sow4g3z9
mergeĀ lp:~mumair/drizzle/drizzle-IPv6Address

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
 
22
 
#include "config.h"
 
22
#include <config.h>
23
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
 
 
28
 
namespace drizzled
29
 
{
30
 
 
31
 
extern my_decimal decimal_zero;
 
27
#include <drizzled/create_field.h>
 
28
 
 
29
namespace drizzled {
32
30
 
33
31
/****************************************************************************
34
32
 ** File_decimal
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
 
142
138
 
143
139
int Field_decimal::store(const char *from, uint32_t length,
144
 
                         const CHARSET_INFO * const charset_arg)
 
140
                         const charset_info_st * 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
 
double Field_decimal::val_real(void)
 
263
double Field_decimal::val_real(void) const
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
}
279
274
 
280
275
 
281
 
int64_t Field_decimal::val_int(void)
 
276
int64_t Field_decimal::val_int(void) const
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) const
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
}
302
297
 
303
298
 
304
 
String *Field_decimal::val_str(String *val_buffer,
305
 
                               String *)
 
299
String *Field_decimal::val_str(String *val_buffer, String *) const
306
300
{
307
 
  my_decimal decimal_value;
 
301
  type::Decimal decimal_value;
308
302
 
309
303
  ASSERT_COLUMN_MARKED_FOR_READ;
310
304
 
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);
 
305
  class_decimal2string(val_decimal(&decimal_value),
 
306
                       dec, val_buffer);
314
307
  return val_buffer;
315
308
}
316
309
 
321
314
}
322
315
 
323
316
 
324
 
void Field_decimal::sort_string(unsigned char *buff,
325
 
                                uint32_t )
 
317
void Field_decimal::sort_string(unsigned char *buff, uint32_t)
326
318
{
327
319
  memcpy(buff, ptr, bin_size);
328
320
}
329
321
 
330
 
 
331
 
void Field_decimal::sql_type(String &str) const
332
 
{
333
 
  const CHARSET_INFO * const cs= str.charset();
334
 
  str.length(cs->cset->snprintf(cs, (char*) str.ptr(), str.alloced_length(),
335
 
                                "decimal(%d,%d)", precision, (int)dec));
336
 
}
337
 
 
338
 
 
339
322
/**
340
323
  Returns the number of bytes field uses in row-based replication
341
324
  row packed size.
352
335
{
353
336
  uint32_t const source_precision= (field_metadata >> 8U) & 0x00ff;
354
337
  uint32_t const source_decimal= field_metadata & 0x00ff;
355
 
  uint32_t const source_size= my_decimal_get_binary_size(source_precision,
 
338
  uint32_t const source_size= class_decimal_get_binary_size(source_precision,
356
339
                                                         source_decimal);
357
340
  return (source_size);
358
341
}
394
377
  uint32_t from_precision= (param_data & 0xff00) >> 8U;
395
378
  uint32_t from_decimal= param_data & 0x00ff;
396
379
  uint32_t length=pack_length();
397
 
  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);
398
381
  uint32_t len= (param_data && (from_pack_len < length)) ?
399
382
    from_pack_len : length;
400
383
  if ((from_pack_len && (from_pack_len < length)) ||