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, Inc.
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
24
#include <drizzled/error.h>
25
#include <drizzled/field.h>
26
#include <drizzled/item/float.h>
27
#include <drizzled/item/num.h>
28
#include <drizzled/item/string.h>
33
extern const CHARSET_INFO *system_charset_info;
35
static uint32_t nr_of_decimals(const char *str, const char *end)
37
const char *decimal_point;
39
/* Find position for '.' */
44
if (*str == 'e' || *str == 'E')
50
for (; my_isdigit(system_charset_info, *str) ; str++)
52
if (*str == 'e' || *str == 'E')
54
return (uint32_t) (str - decimal_point);
57
String *Item_float::val_str(String *str)
59
// following assert is redundant, because fixed=1 assigned in constructor
61
str->set_real(value,decimals,&my_charset_bin);
66
int64_t Item_float::val_int()
69
if (value <= (double) INT64_MIN)
73
else if (value >= (double) (uint64_t) INT64_MAX)
77
return (int64_t) rint(value);
80
type::Decimal *Item_float::val_decimal(type::Decimal *decimal_value)
82
// following assert is redundant, because fixed=1 assigned in constructor
84
double2_class_decimal(E_DEC_FATAL_ERROR, value, decimal_value);
85
return (decimal_value);
89
This function is only called during parsing. We will signal an error if
90
value is not a true double value (overflow)
93
Item_float::Item_float(const char *str_arg, uint32_t length)
97
value= my_strntod(&my_charset_bin, (char*) str_arg, length, &end_not_used,
102
Note that we depend on that str_arg is null terminated, which is true
103
when we are in the parser
105
assert(str_arg[length] == 0);
106
my_error(ER_ILLEGAL_VALUE_FOR_TYPE, MYF(0), "double", (char*) str_arg);
108
presentation= name=(char*) str_arg;
109
decimals=(uint8_t) nr_of_decimals(str_arg, str_arg+length);
114
int Item_float::save_in_field(Field *field, bool)
116
double nr= val_real();
118
return set_field_to_null(field);
119
field->set_notnull();
120
return field->store(nr);
124
void Item_float::print(String *str, enum_query_type)
128
str->append(presentation);
132
String num(buffer, sizeof(buffer), &my_charset_bin);
133
num.set_real(value, decimals, &my_charset_bin);
139
In string context this is a binary string.
140
In number context this is a int64_t value.
143
bool Item_float::eq(const Item *arg, bool) const
145
if (arg->basic_const_item() && arg->type() == type())
148
We need to cast off const to call val_int(). This should be OK for
151
Item *item= (Item*) arg;
152
return item->val_real() == value;
157
Item *Item_static_float_func::safe_charset_converter(const CHARSET_INFO * const)
161
String *s, tmp(buf, sizeof(buf), &my_charset_bin);
163
if ((conv= new Item_static_string_func(func_name, s->ptr(), s->length(),
166
conv->str_value.copy();
167
conv->str_value.mark_as_const();
172
} /* namespace drizzled */