Drizzled Public API Documentation

float.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 <math.h>
23 
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>
29 
30 namespace drizzled {
31 
32 extern const charset_info_st *system_charset_info;
33 
34 static uint32_t nr_of_decimals(const char *str, const char *end)
35 {
36  const char *decimal_point;
37 
38  /* Find position for '.' */
39  for (;;)
40  {
41  if (str == end)
42  return 0;
43  if (*str == 'e' || *str == 'E')
44  return NOT_FIXED_DEC;
45  if (*str++ == '.')
46  break;
47  }
48  decimal_point= str;
49  for (; system_charset_info->isdigit(*str) ; str++)
50  ;
51  if (*str == 'e' || *str == 'E')
52  return NOT_FIXED_DEC;
53  return (uint32_t) (str - decimal_point);
54 }
55 
57 {
58  // following assert is redundant, because fixed=1 assigned in constructor
59  assert(fixed == 1);
60  str->set_real(value,decimals,&my_charset_bin);
61  return str;
62 }
63 
64 
66 {
67  assert(fixed == 1);
68  if (value <= (double) INT64_MIN)
69  {
70  return INT64_MIN;
71  }
72  else if (value >= (double) (uint64_t) INT64_MAX)
73  {
74  return INT64_MAX;
75  }
76  return (int64_t) rint(value);
77 }
78 
80 {
81  // following assert is redundant, because fixed=1 assigned in constructor
82  assert(fixed == 1);
83  double2_class_decimal(E_DEC_FATAL_ERROR, value, decimal_value);
84  return (decimal_value);
85 }
86 
92 Item_float::Item_float(const char *str_arg, uint32_t length)
93 {
94  int error;
95  char *end_not_used;
96  value= my_strntod(&my_charset_bin, (char*) str_arg, length, &end_not_used,
97  &error);
98  if (error)
99  {
100  /*
101  Note that we depend on that str_arg is null terminated, which is true
102  when we are in the parser
103  */
104  assert(str_arg[length] == 0);
105  my_error(ER_ILLEGAL_VALUE_FOR_TYPE, MYF(0), "double", (char*) str_arg);
106  }
107  presentation= name=(char*) str_arg;
108  decimals=(uint8_t) nr_of_decimals(str_arg, str_arg+length);
109  max_length=length;
110  fixed= 1;
111 }
112 
113 int Item_float::save_in_field(Field *field, bool)
114 {
115  double nr= val_real();
116  if (null_value)
117  return set_field_to_null(field);
118  field->set_notnull();
119  return field->store(nr);
120 }
121 
122 
124 {
125  if (presentation)
126  {
127  str->append(presentation, strlen(presentation));
128  return;
129  }
130  char buffer[20];
131  String num(buffer, sizeof(buffer), &my_charset_bin);
132  num.set_real(value, decimals, &my_charset_bin);
133  str->append(num);
134 }
135 
136 /*
137  hex item
138  In string context this is a binary string.
139  In number context this is a int64_t value.
140 */
141 
142 bool Item_float::eq(const Item *arg, bool) const
143 {
144  if (arg->basic_const_item() && arg->type() == type())
145  {
146  /*
147  We need to cast off const to call val_int(). This should be OK for
148  a basic constant.
149  */
150  Item *item= (Item*) arg;
151  return item->val_real() == value;
152  }
153  return false;
154 }
155 
156 Item *Item_static_float_func::safe_charset_converter(const charset_info_st*)
157 {
158  char buf[64];
159  String tmp(buf, sizeof(buf), &my_charset_bin);
160  String* s= val_str(&tmp);
161  Item_string* conv= new Item_static_string_func(func_name, *s, s->charset());
162  conv->str_value.copy();
163  conv->str_value.mark_as_const();
164  return conv;
165 }
166 
167 } /* namespace drizzled */
const char * name
Definition: item.h:110
virtual bool basic_const_item() const
Definition: item.h:474
Item_float(const char *str_arg, uint32_t length)
Definition: float.cc:92
bool eq(const Item *, bool binary_cmp) const
Definition: float.cc:142
bool fixed
Definition: item.h:120
TODO: Rename this file - func.h is stupid.
double val_real()
Definition: float.h:48
bool null_value
Definition: item.h:122
String * val_str(String *)
Definition: float.cc:56
int64_t val_int()
Definition: float.cc:65
virtual double val_real()=0
type::Decimal * val_decimal(type::Decimal *)
Definition: float.cc:79
virtual void print(String *str)
Definition: float.cc:123
String str_value
Definition: item.h:107