drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
item/decimal.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
5 *
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.
9 *
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.
14 *
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
18 */
19
20#include <config.h>
21
22#include <drizzled/charset.h>
23#include <drizzled/field.h>
24#include <drizzled/item/decimal.h>
25
26namespace drizzled {
27
28Item_decimal::Item_decimal(const char *str_arg, uint32_t length,
29 const charset_info_st * const charset)
30{
31 decimal_value.store(E_DEC_FATAL_ERROR, str_arg, length, charset);
32 name= (char*) str_arg;
33 decimals= (uint8_t) decimal_value.frac;
34 fixed= 1;
35 max_length= class_decimal_precision_to_length(decimal_value.intg + decimals,
36 decimals, unsigned_flag);
37}
38
39Item_decimal::Item_decimal(int64_t val, bool unsig)
40{
41 int2_class_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value);
42 decimals= (uint8_t) decimal_value.frac;
43 fixed= 1;
44 max_length= class_decimal_precision_to_length(decimal_value.intg + decimals,
45 decimals, unsigned_flag);
46}
47
48
49Item_decimal::Item_decimal(double val, int, int)
50{
51 double2_class_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
52 decimals= (uint8_t) decimal_value.frac;
53 fixed= 1;
54 max_length= class_decimal_precision_to_length(decimal_value.intg + decimals,
55 decimals, unsigned_flag);
56}
57
58Item_decimal::Item_decimal(const char *str, const type::Decimal *val_arg,
59 uint32_t decimal_par, uint32_t length)
60{
61 class_decimal2decimal(val_arg, &decimal_value);
62 name= (char*) str;
63 decimals= (uint8_t) decimal_par;
64 max_length= length;
65 fixed= 1;
66}
67
68
69Item_decimal::Item_decimal(type::Decimal *value_par)
70{
71 class_decimal2decimal(value_par, &decimal_value);
72 decimals= (uint8_t) decimal_value.frac;
73 fixed= 1;
74 max_length= class_decimal_precision_to_length(decimal_value.intg + decimals,
75 decimals, unsigned_flag);
76}
77
78
79Item_decimal::Item_decimal(const unsigned char *bin, int precision, int scale)
80{
81 binary2_class_decimal(E_DEC_FATAL_ERROR, bin,
82 &decimal_value, precision, scale);
83 decimals= (uint8_t) decimal_value.frac;
84 fixed= 1;
85 max_length= class_decimal_precision_to_length(precision, decimals,
86 unsigned_flag);
87}
88
90{
91 int64_t result;
92 decimal_value.val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
93 return result;
94}
95
97{
98 double result;
99 class_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
100 return result;
101}
102
104{
105 result->set_charset(&my_charset_bin);
106 class_decimal2string(&decimal_value, 0, result);
107 return result;
108}
109
111{
112 class_decimal2string(&decimal_value, 0, &str_value);
113 str->append(str_value);
114}
115
116bool Item_decimal::eq(const Item *item, bool) const
117{
118 if (type() == item->type() && item->basic_const_item())
119 {
120 /*
121 We need to cast off const to call val_decimal(). This should
122 be OK for a basic constant. Additionally, we can pass 0 as
123 a true decimal constant will return its internal decimal
124 storage and ignore the argument.
125 */
126 Item *arg= (Item*) item;
127 type::Decimal *value= arg->val_decimal(0);
128 return !class_decimal_cmp(&decimal_value, value);
129 }
130 return 0;
131}
132
133
134void Item_decimal::set_decimal_value(type::Decimal *value_par)
135{
136 class_decimal2decimal(value_par, &decimal_value);
137 decimals= (uint8_t) decimal_value.frac;
138 unsigned_flag= !decimal_value.sign();
139 max_length= class_decimal_precision_to_length(decimal_value.intg + decimals,
140 decimals, unsigned_flag);
141}
142
143int Item_decimal::save_in_field(Field *field, bool)
144{
145 field->set_notnull();
146 return field->store_decimal(&decimal_value);
147}
148
149
150} /* namespace drizzled */
String * val_str(String *)
bool eq(const Item *, bool binary_cmp) const
virtual void print(String *str)
bool fixed
Definition item.h:122
const char * name
Definition item.h:112
virtual type::Decimal * val_decimal(type::Decimal *decimal_buffer)=0
String str_value
Definition item.h:109
TODO: Rename this file - func.h is stupid.
int class_decimal2string(const type::Decimal *d, uint32_t fixed_dec, String *str)
Converting decimal to string.
int class_decimal_cmp(const type::Decimal *a, const type::Decimal *b)