~drizzle-trunk/drizzle/development

642.1.6 by Lee
move functions from item.cc/item.h to item directory
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
642.1.6 by Lee
move functions from item.cc/item.h to item directory
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
20
#include <config.h>
2154.2.4 by Brian Aker
This fixes 716459
21
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
22
#include <math.h>
2154.2.4 by Brian Aker
This fixes 716459
23
642.1.6 by Lee
move functions from item.cc/item.h to item directory
24
#include <drizzled/error.h>
2154.2.4 by Brian Aker
This fixes 716459
25
#include <drizzled/field.h>
26
#include <drizzled/item/float.h>
642.1.25 by Lee
more header file cleanup
27
#include <drizzled/item/num.h>
642.1.63 by Lee
more header file cleanup
28
#include <drizzled/item/string.h>
642.1.6 by Lee
move functions from item.cc/item.h to item directory
29
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
30
namespace drizzled
31
{
32
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
33
extern const CHARSET_INFO *system_charset_info;
34
642.1.6 by Lee
move functions from item.cc/item.h to item directory
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;
895 by Brian Aker
Completion (?) of uint conversion.
54
  return (uint32_t) (str - decimal_point);
642.1.6 by Lee
move functions from item.cc/item.h to item directory
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
2030.1.4 by Brian Aker
Change my_decimal to Decimal
80
type::Decimal *Item_float::val_decimal(type::Decimal *decimal_value)
642.1.6 by Lee
move functions from item.cc/item.h to item directory
81
{
82
  // following assert is redundant, because fixed=1 assigned in constructor
83
  assert(fixed == 1);
2030.1.3 by Brian Aker
Second pass through function names.
84
  double2_class_decimal(E_DEC_FATAL_ERROR, value, decimal_value);
642.1.6 by Lee
move functions from item.cc/item.h to item directory
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
2215.2.1 by Stewart Smith
remove enum_query_type which was effectively unused. It was set to one value once, compared to it once (i.e. always true) and passed around everywhere doing nothing.
124
void Item_float::print(String *str)
642.1.6 by Lee
move functions from item.cc/item.h to item directory
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
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
172
} /* namespace drizzled */