22
#include <drizzled/server_includes.h>
23
#include <drizzled/field/fdecimal.h>
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
#include <drizzled/create_field.h>
28
extern my_decimal decimal_zero;
30
31
/****************************************************************************
32
****************************************************************************/
34
Field_new_decimal::Field_new_decimal(unsigned char *ptr_arg,
35
uint32_t len_arg, unsigned char *null_ptr_arg,
36
unsigned char null_bit_arg,
37
enum utype unireg_check_arg,
38
const char *field_name_arg,
39
uint8_t dec_arg,bool zero_arg,
41
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
42
unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg)
44
precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
45
set_if_smaller(precision, DECIMAL_MAX_PRECISION);
46
assert((precision <= DECIMAL_MAX_PRECISION) &&
47
(dec <= DECIMAL_MAX_SCALE));
48
bin_size= my_decimal_get_binary_size(precision, dec);
52
Field_new_decimal::Field_new_decimal(uint32_t len_arg,
57
:Field_num((unsigned char*) 0, len_arg,
58
maybe_null_arg ? (unsigned char*) "": 0, 0,
59
NONE, name, dec_arg, 0, unsigned_arg)
61
precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
62
set_if_smaller(precision, DECIMAL_MAX_PRECISION);
63
assert((precision <= DECIMAL_MAX_PRECISION) &&
64
(dec <= DECIMAL_MAX_SCALE));
65
bin_size= my_decimal_get_binary_size(precision, dec);
69
int Field_new_decimal::reset(void)
33
****************************************************************************/
35
Field_decimal::Field_decimal(unsigned char *ptr_arg,
37
unsigned char *null_ptr_arg,
38
unsigned char null_bit_arg,
39
enum utype unireg_check_arg,
40
const char *field_name_arg,
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);
57
Field_decimal::Field_decimal(uint32_t len_arg,
62
Field_num((unsigned char*) 0,
64
maybe_null_arg ? (unsigned char*) "": 0,
72
precision= class_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
73
set_if_smaller(precision, (uint32_t)DECIMAL_MAX_PRECISION);
74
assert((precision <= DECIMAL_MAX_PRECISION) &&
75
(dec <= DECIMAL_MAX_SCALE));
76
bin_size= class_decimal_get_binary_size(precision, dec);
80
int Field_decimal::reset(void)
71
82
store_value(&decimal_zero);
98
109
If it does, stores the decimal in the buffer using binary format.
99
110
Otherwise sets maximal number that can be stored in the field.
101
@param decimal_value my_decimal
112
@param decimal_value type::Decimal
109
bool Field_new_decimal::store_value(const my_decimal *decimal_value)
120
bool Field_decimal::store_value(const type::Decimal *decimal_value)
122
int error= decimal_value->val_binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, ptr, precision, dec);
113
if (warn_if_overflow(my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
114
decimal_value, ptr, precision, dec)))
124
if (warn_if_overflow(error))
117
set_value_on_overflow(&buff, decimal_value->sign());
118
my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
126
if (error != E_DEC_TRUNCATED)
129
set_value_on_overflow(&buff, decimal_value->sign());
130
buff.val_binary(E_DEC_FATAL_ERROR, ptr, precision, dec);
125
int Field_new_decimal::store(const char *from, uint32_t length,
126
const CHARSET_INFO * const charset_arg)
139
int Field_decimal::store(const char *from, uint32_t length,
140
const charset_info_st * const charset_arg)
129
my_decimal decimal_value;
131
if ((err= str2my_decimal(E_DEC_FATAL_ERROR &
143
type::Decimal decimal_value;
145
ASSERT_COLUMN_MARKED_FOR_WRITE;
147
if ((err= decimal_value.store(E_DEC_FATAL_ERROR &
132
148
~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
133
from, length, charset_arg,
135
table->in_use->abort_on_warning)
149
from, length, charset_arg)) &&
150
getTable()->in_use->abortOnWarning())
137
152
/* Because "from" is not NUL-terminated and we use %s in the ER() */
138
153
String from_as_str;
139
154
from_as_str.copy(from, length, &my_charset_bin);
141
push_warning_printf(table->in_use, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
156
push_warning_printf(getTable()->in_use, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
142
157
ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
143
158
ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
144
159
"decimal", from_as_str.c_ptr(), field_name,
145
(uint32_t) table->in_use->row_count);
160
(uint32_t) getTable()->in_use->row_count);
151
166
case E_DEC_TRUNCATED:
152
set_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE, ER_WARN_DATA_TRUNCATED, 1);
167
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
168
set_value_on_overflow(&decimal_value, decimal_value.sign());
154
170
case E_DEC_OVERFLOW:
155
171
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
182
Fix following when double2my_decimal when double2decimal
198
Fix following when double2_class_decimal when double2decimal
183
199
will return E_DEC_TRUNCATED always correctly
186
int Field_new_decimal::store(double nr)
202
int Field_decimal::store(double nr)
188
my_decimal decimal_value;
204
type::Decimal decimal_value;
191
err= double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
207
ASSERT_COLUMN_MARKED_FOR_WRITE;
209
err= double2_class_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
195
213
if (check_overflow(err))
196
214
set_value_on_overflow(&decimal_value, decimal_value.sign());
197
215
/* Only issue a warning if store_value doesn't issue an warning */
198
table->in_use->got_warning= 0;
216
getTable()->in_use->got_warning= 0;
200
218
if (store_value(&decimal_value))
202
else if (err && !table->in_use->got_warning)
220
else if (err && !getTable()->in_use->got_warning)
203
221
err= warn_if_overflow(err);
208
int Field_new_decimal::store(int64_t nr, bool unsigned_val)
226
int Field_decimal::store(int64_t nr, bool unsigned_val)
210
my_decimal decimal_value;
228
type::Decimal decimal_value;
213
if ((err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
231
ASSERT_COLUMN_MARKED_FOR_WRITE;
233
if ((err= int2_class_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
214
234
nr, unsigned_val, &decimal_value)))
216
236
if (check_overflow(err))
217
237
set_value_on_overflow(&decimal_value, decimal_value.sign());
218
238
/* Only issue a warning if store_value doesn't issue an warning */
219
table->in_use->got_warning= 0;
239
getTable()->in_use->got_warning= 0;
221
241
if (store_value(&decimal_value))
223
else if (err && !table->in_use->got_warning)
243
else if (err && not getTable()->in_use->got_warning)
224
244
err= warn_if_overflow(err);
229
int Field_new_decimal::store_decimal(const my_decimal *decimal_value)
249
int Field_decimal::store_decimal(const type::Decimal *decimal_value)
231
251
return store_value(decimal_value);
235
int Field_new_decimal::store_time(DRIZZLE_TIME *ltime,
236
enum enum_drizzle_timestamp_type t_type __attribute__((unused)))
255
int Field_decimal::store_time(type::Time <ime,
238
my_decimal decimal_value;
239
return store_value(date2my_decimal(ltime, &decimal_value));
258
type::Decimal decimal_value;
259
return store_value(date2_class_decimal(<ime, &decimal_value));
243
double Field_new_decimal::val_real(void)
263
double Field_decimal::val_real(void) const
246
my_decimal decimal_value;
247
my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
266
type::Decimal decimal_value;
268
ASSERT_COLUMN_MARKED_FOR_READ;
270
class_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
252
int64_t Field_new_decimal::val_int(void)
276
int64_t Field_decimal::val_int(void) const
255
my_decimal decimal_value;
256
my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), false, &i);
279
type::Decimal decimal_value;
281
ASSERT_COLUMN_MARKED_FOR_READ;
283
val_decimal(&decimal_value)->val_int32(E_DEC_FATAL_ERROR, false, &i);
261
my_decimal* Field_new_decimal::val_decimal(my_decimal *decimal_value)
289
type::Decimal* Field_decimal::val_decimal(type::Decimal *decimal_value) const
263
binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
291
ASSERT_COLUMN_MARKED_FOR_READ;
293
binary2_class_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
265
295
return(decimal_value);
269
String *Field_new_decimal::val_str(String *val_buffer,
270
String *val_ptr __attribute__((unused)))
299
String *Field_decimal::val_str(String *val_buffer, String *) const
272
my_decimal decimal_value;
273
uint32_t fixed_precision= decimal_precision ? precision : 0;
274
my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
275
fixed_precision, dec, '0', val_buffer);
301
type::Decimal decimal_value;
303
ASSERT_COLUMN_MARKED_FOR_READ;
305
class_decimal2string(val_decimal(&decimal_value),
276
307
return val_buffer;
280
int Field_new_decimal::cmp(const unsigned char *a,const unsigned char*b)
311
int Field_decimal::cmp(const unsigned char *a,const unsigned char*b)
282
313
return memcmp(a, b, bin_size);
286
void Field_new_decimal::sort_string(unsigned char *buff,
287
uint32_t length __attribute__((unused)))
317
void Field_decimal::sort_string(unsigned char *buff, uint32_t)
289
319
memcpy(buff, ptr, bin_size);
293
void Field_new_decimal::sql_type(String &str) const
295
const CHARSET_INFO * const cs= str.charset();
296
str.length(cs->cset->snprintf(cs, (char*) str.ptr(), str.alloced_length(),
297
"decimal(%d,%d)", precision, (int)dec));
302
Save the field metadata for new decimal fields.
304
Saves the precision in the first byte and decimals() in the second
305
byte of the field metadata array at index of *metadata_ptr and
308
@param metadata_ptr First byte of field metadata
310
@returns number of bytes written to metadata_ptr
312
int Field_new_decimal::do_save_field_metadata(unsigned char *metadata_ptr)
314
*metadata_ptr= precision;
315
*(metadata_ptr + 1)= decimals();
321
Returns the number of bytes field uses in row-based replication
324
This method is used in row-based replication to determine the number
325
of bytes that the field consumes in the row record format. This is
326
used to skip fields in the master that do not exist on the slave.
328
@param field_metadata Encoded size in field metadata
330
@returns The size of the field based on the field metadata.
332
uint32_t Field_new_decimal::pack_length_from_metadata(uint32_t field_metadata)
323
Returns the number of bytes field uses in row-based replication
326
This method is used in row-based replication to determine the number
327
of bytes that the field consumes in the row record format. This is
328
used to skip fields in the master that do not exist on the slave.
330
@param field_metadata Encoded size in field metadata
332
@returns The size of the field based on the field metadata.
334
uint32_t Field_decimal::pack_length_from_metadata(uint32_t field_metadata)
334
336
uint32_t const source_precision= (field_metadata >> 8U) & 0x00ff;
335
uint32_t const source_decimal= field_metadata & 0x00ff;
336
uint32_t const source_size= my_decimal_get_binary_size(source_precision,
337
uint32_t const source_decimal= field_metadata & 0x00ff;
338
uint32_t const source_size= class_decimal_get_binary_size(source_precision,
338
340
return (source_size);
343
Check to see if field size is compatible with destination.
345
This method is used in row-based replication to verify that the slave's
346
field size is less than or equal to the master's field size. The
347
encoded field metadata (from the master or source) is decoded and compared
348
to the size of this field (the slave or destination).
350
@param field_metadata Encoded size in field metadata
352
@retval 0 if this field's size is < the source field's size
353
@retval 1 if this field's size is >= the source field's size
355
int Field_new_decimal::compatible_field_size(uint32_t field_metadata)
358
uint32_t const source_precision= (field_metadata >> 8U) & 0x00ff;
359
uint32_t const source_decimal= field_metadata & 0x00ff;
360
uint32_t const source_size= my_decimal_get_binary_size(source_precision,
362
uint32_t const destination_size= row_pack_length();
363
compatible= (source_size <= destination_size);
365
compatible= (source_precision <= precision) &&
366
(source_decimal <= decimals());
371
uint32_t Field_new_decimal::is_equal(Create_field *new_field)
373
return ((new_field->sql_type == real_type()) &&
374
((new_field->flags & UNSIGNED_FLAG) ==
375
(uint) (flags & UNSIGNED_FLAG)) &&
376
((new_field->flags & AUTO_INCREMENT_FLAG) ==
377
(uint) (flags & AUTO_INCREMENT_FLAG)) &&
378
(new_field->length == max_display_length()) &&
379
(new_field->decimals == dec));
384
Unpack a decimal field from row data.
386
This method is used to unpack a decimal or numeric field from a master
387
whose size of the field is less than that of the slave.
389
@param to Destination of the data
390
@param from Source of the data
391
@param param_data Precision (upper) and decimal (lower) values
393
@return New pointer into memory based on from + length of the data
344
uint32_t Field_decimal::is_equal(CreateField *new_field_ptr)
346
return ((new_field_ptr->sql_type == real_type()) &&
347
((new_field_ptr->flags & UNSIGNED_FLAG) ==
348
(uint32_t) (flags & UNSIGNED_FLAG)) &&
349
((new_field_ptr->flags & AUTO_INCREMENT_FLAG) ==
350
(uint32_t) (flags & AUTO_INCREMENT_FLAG)) &&
351
(new_field_ptr->length == max_display_length()) &&
352
(new_field_ptr->decimals == dec));
357
Unpack a decimal field from row data.
359
This method is used to unpack a decimal or numeric field from a master
360
whose size of the field is less than that of the slave.
362
@param to Destination of the data
363
@param from Source of the data
364
@param param_data Precision (upper) and decimal (lower) values
366
@return New pointer into memory based on from + length of the data
395
368
const unsigned char *
396
Field_new_decimal::unpack(unsigned char* to,
397
const unsigned char *from,
369
Field_decimal::unpack(unsigned char* to,
370
const unsigned char *from,
401
374
if (param_data == 0)
402
375
return Field::unpack(to, from, param_data, low_byte_first);