1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include <drizzled/item/decimal.h>
27
Item_decimal::Item_decimal(const char *str_arg, uint32_t length,
28
const CHARSET_INFO * const charset)
30
str2my_decimal(E_DEC_FATAL_ERROR, str_arg, length, charset, &decimal_value);
31
name= (char*) str_arg;
32
decimals= (uint8_t) decimal_value.frac;
34
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
35
decimals, unsigned_flag);
38
Item_decimal::Item_decimal(int64_t val, bool unsig)
40
int2my_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value);
41
decimals= (uint8_t) decimal_value.frac;
43
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
44
decimals, unsigned_flag);
48
Item_decimal::Item_decimal(double val, int, int)
50
double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
51
decimals= (uint8_t) decimal_value.frac;
53
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
54
decimals, unsigned_flag);
57
Item_decimal::Item_decimal(const char *str, const my_decimal *val_arg,
58
uint32_t decimal_par, uint32_t length)
60
my_decimal2decimal(val_arg, &decimal_value);
62
decimals= (uint8_t) decimal_par;
68
Item_decimal::Item_decimal(my_decimal *value_par)
70
my_decimal2decimal(value_par, &decimal_value);
71
decimals= (uint8_t) decimal_value.frac;
73
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
74
decimals, unsigned_flag);
78
Item_decimal::Item_decimal(const unsigned char *bin, int precision, int scale)
80
binary2my_decimal(E_DEC_FATAL_ERROR, bin,
81
&decimal_value, precision, scale);
82
decimals= (uint8_t) decimal_value.frac;
84
max_length= my_decimal_precision_to_length(precision, decimals,
88
int64_t Item_decimal::val_int()
91
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &result);
95
double Item_decimal::val_real()
98
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
102
String *Item_decimal::val_str(String *result)
104
result->set_charset(&my_charset_bin);
105
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, result);
109
void Item_decimal::print(String *str, enum_query_type)
111
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, &str_value);
112
str->append(str_value);
115
bool Item_decimal::eq(const Item *item, bool) const
117
if (type() == item->type() && item->basic_const_item())
120
We need to cast off const to call val_decimal(). This should
121
be OK for a basic constant. Additionally, we can pass 0 as
122
a true decimal constant will return its internal decimal
123
storage and ignore the argument.
125
Item *arg= (Item*) item;
126
my_decimal *value= arg->val_decimal(0);
127
return !my_decimal_cmp(&decimal_value, value);
133
void Item_decimal::set_decimal_value(my_decimal *value_par)
135
my_decimal2decimal(value_par, &decimal_value);
136
decimals= (uint8_t) decimal_value.frac;
137
unsigned_flag= !decimal_value.sign();
138
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
139
decimals, unsigned_flag);
142
int Item_decimal::save_in_field(Field *field, bool)
144
field->set_notnull();
145
return field->store_decimal(&decimal_value);
149
} /* namespace drizzled */