~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/decimal.cc

  • Committer: Andrew Hutchings
  • Date: 2011-01-21 11:23:19 UTC
  • mto: (2100.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2101.
  • Revision ID: andrew@linuxjedi.co.uk-20110121112319-nj1cvg0yt3nnf2rr
Add errors page to drizzle client docs
Add link to specific error in migration docs
Minor changes to migration docs

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