~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/float.cc

  • Committer: Lee
  • Date: 2008-10-30 22:02:01 UTC
  • mto: (572.1.2 devel)
  • mto: This revision was merged to the branch mainline in revision 573.
  • Revision ID: lbieber@lbieber-desktop-20081030220201-elb6qprbzpn7c5a4
add my name to the AUTHORS file

Show diffs side-by-side

added added

removed removed

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