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>
31
extern my_decimal decimal_zero;
27
#include <drizzled/create_field.h>
33
31
/****************************************************************************
40
38
unsigned char null_bit_arg,
41
39
enum utype unireg_check_arg,
42
40
const char *field_name_arg,
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);
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);
62
57
Field_decimal::Field_decimal(uint32_t len_arg,
63
58
bool maybe_null_arg,
67
:Field_num((unsigned char*) 0,
69
maybe_null_arg ? (unsigned char*) "": 0,
62
Field_num((unsigned char*) 0,
64
maybe_null_arg ? (unsigned char*) "": 0,
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);
96
91
@param sign sign of value which caused overflow
99
void Field_decimal::set_value_on_overflow(my_decimal *decimal_value,
94
void Field_decimal::set_value_on_overflow(type::Decimal *decimal_value,
102
max_my_decimal(decimal_value, precision, decimals());
97
max_Decimal(decimal_value, precision, decimals());
104
99
decimal_value->sign(true);
125
bool Field_decimal::store_value(const my_decimal *decimal_value)
120
bool Field_decimal::store_value(const type::Decimal *decimal_value)
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);
129
124
if (warn_if_overflow(error))
131
126
if (error != E_DEC_TRUNCATED)
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);
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)
147
my_decimal decimal_value;
143
type::Decimal decimal_value;
149
145
ASSERT_COLUMN_MARKED_FOR_WRITE;
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,
155
getTable()->in_use->abort_on_warning)
149
from, length, charset_arg)) &&
150
getTable()->in_use->abortOnWarning())
157
152
/* Because "from" is not NUL-terminated and we use %s in the ER() */
158
153
String from_as_str;
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
207
202
int Field_decimal::store(double nr)
209
my_decimal decimal_value;
204
type::Decimal decimal_value;
212
207
ASSERT_COLUMN_MARKED_FOR_WRITE;
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,
231
226
int Field_decimal::store(int64_t nr, bool unsigned_val)
233
my_decimal decimal_value;
228
type::Decimal decimal_value;
236
231
ASSERT_COLUMN_MARKED_FOR_WRITE;
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)))
241
236
if (check_overflow(err))
254
int Field_decimal::store_decimal(const my_decimal *decimal_value)
249
int Field_decimal::store_decimal(const type::Decimal *decimal_value)
256
251
return store_value(decimal_value);
260
int Field_decimal::store_time(DRIZZLE_TIME *ltime,
261
enum enum_drizzle_timestamp_type )
255
int Field_decimal::store_time(type::Time <ime,
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(<ime, &decimal_value));
268
double Field_decimal::val_real(void)
263
double Field_decimal::val_real(void) const
271
my_decimal decimal_value;
266
type::Decimal decimal_value;
273
268
ASSERT_COLUMN_MARKED_FOR_READ;
275
my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
270
class_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
281
int64_t Field_decimal::val_int(void)
276
int64_t Field_decimal::val_int(void) const
284
my_decimal decimal_value;
279
type::Decimal decimal_value;
286
281
ASSERT_COLUMN_MARKED_FOR_READ;
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);
294
my_decimal* Field_decimal::val_decimal(my_decimal *decimal_value)
289
type::Decimal* Field_decimal::val_decimal(type::Decimal *decimal_value) const
296
291
ASSERT_COLUMN_MARKED_FOR_READ;
298
binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
293
binary2_class_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
300
295
return(decimal_value);
304
String *Field_decimal::val_str(String *val_buffer,
299
String *Field_decimal::val_str(String *val_buffer, String *) const
307
my_decimal decimal_value;
301
type::Decimal decimal_value;
309
303
ASSERT_COLUMN_MARKED_FOR_READ;
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),
314
307
return val_buffer;
324
void Field_decimal::sort_string(unsigned char *buff,
317
void Field_decimal::sort_string(unsigned char *buff, uint32_t)
327
319
memcpy(buff, ptr, bin_size);
331
void Field_decimal::sql_type(String &str) const
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));
340
323
Returns the number of bytes field uses in row-based replication
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)) ||