~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
2318.7.19 by Olaf van der Spek
Refactor Items
30
namespace drizzled {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
31
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
32
extern const charset_info_st *system_charset_info;
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
33
642.1.6 by Lee
move functions from item.cc/item.h to item directory
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;
2445.1.3 by Olaf van der Spek
Refactor
49
  for (; system_charset_info->isdigit(*str) ; str++)
642.1.6 by Lee
move functions from item.cc/item.h to item directory
50
    ;
51
  if (*str == 'e' || *str == 'E')
52
    return NOT_FIXED_DEC;
895 by Brian Aker
Completion (?) of uint conversion.
53
  return (uint32_t) (str - decimal_point);
642.1.6 by Lee
move functions from item.cc/item.h to item directory
54
}
55
56
String *Item_float::val_str(String *str)
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
65
int64_t Item_float::val_int()
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
2030.1.4 by Brian Aker
Change my_decimal to Decimal
79
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
80
{
81
  // following assert is redundant, because fixed=1 assigned in constructor
82
  assert(fixed == 1);
2030.1.3 by Brian Aker
Second pass through function names.
83
  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
84
  return (decimal_value);
85
}
86
87
/**
88
  This function is only called during parsing. We will signal an error if
89
  value is not a true double value (overflow)
90
*/
91
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
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.
123
void Item_float::print(String *str)
642.1.6 by Lee
move functions from item.cc/item.h to item directory
124
{
125
  if (presentation)
126
  {
127
    str->append(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
2318.7.19 by Olaf van der Spek
Refactor Items
156
Item *Item_static_float_func::safe_charset_converter(const charset_info_st*)
642.1.6 by Lee
move functions from item.cc/item.h to item directory
157
{
158
  char buf[64];
2318.7.14 by Olaf van der Spek
Refactor
159
  String tmp(buf, sizeof(buf), &my_charset_bin);
160
  String* s= val_str(&tmp);
2440.2.16 by Olaf van der Spek
Use str_ref
161
  Item_string* conv= new Item_static_string_func(func_name, *s, s->charset());
2318.6.90 by Olaf van der Spek
Refactor
162
  conv->str_value.copy();
163
  conv->str_value.mark_as_const();
642.1.6 by Lee
move functions from item.cc/item.h to item directory
164
  return conv;
165
}
166
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
167
} /* namespace drizzled */