~drizzle-trunk/drizzle/development

466 by Monty Taylor
Fixed modelines... these files are c++.
1
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 MySQL
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; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
21
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
22
#include "config.h"
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
23
24
#include <float.h>
1241.9.24 by Monty Taylor
Removed m_string.h from server_includes.h.
25
#include <math.h>
26
27
#include <algorithm>
28
214 by Brian Aker
Rename of fields (fix issue with string and decimal .h clashing).
29
#include <drizzled/field/double.h>
549 by Monty Taylor
Took gettext.h out of header files.
30
#include <drizzled/error.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
31
#include <drizzled/table.h>
32
#include <drizzled/session.h>
1241.9.64 by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal.
33
#include "drizzled/internal/m_string.h"
1067.4.6 by Nathan Williams
Converted all usages of cmax in drizzled/field/ and drizzled/function/ to std::max.
34
35
using namespace std;
36
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
37
namespace drizzled
38
{
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
39
40
/****************************************************************************
41
  double precision floating point numbers
42
****************************************************************************/
43
482 by Brian Aker
Remove uint.
44
int Field_double::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
45
{
46
  int error;
47
  char *end;
48
  double nr= my_strntod(cs,(char*) from, len, &end, &error);
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
49
50
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1660.1.3 by Brian Aker
Encapsulate Table in field
51
  if (error || (!len || (((uint32_t) (end-from) != len) && getTable()->in_use->count_cuted_fields)))
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
52
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
53
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
54
                (error ? ER_WARN_DATA_OUT_OF_RANGE : ER_WARN_DATA_TRUNCATED), 1);
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
55
    error= error ? 1 : 2;
56
  }
57
  Field_double::store(nr);
58
  return error;
59
}
60
61
62
int Field_double::store(double nr)
63
{
64
  int error= truncate(&nr, DBL_MAX);
65
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
66
  ASSERT_COLUMN_MARKED_FOR_WRITE;
67
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
68
#ifdef WORDS_BIGENDIAN
1660.1.3 by Brian Aker
Encapsulate Table in field
69
  if (getTable()->getShare()->db_low_byte_first)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
70
  {
71
    float8store(ptr,nr);
72
  }
73
  else
74
#endif
75
    doublestore(ptr,nr);
76
  return error;
77
}
78
79
80
int Field_double::store(int64_t nr, bool unsigned_val)
81
{
82
  return Field_double::store(unsigned_val ? uint64_t2double((uint64_t) nr) :
83
                             (double) nr);
84
}
85
86
double Field_double::val_real(void)
87
{
88
  double j;
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
89
90
  ASSERT_COLUMN_MARKED_FOR_READ;
91
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
92
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
93
  if (getTable()->getShare()->db_low_byte_first)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
94
  {
95
    float8get(j,ptr);
96
  }
97
  else
98
#endif
99
    doubleget(j,ptr);
100
  return j;
101
}
102
103
int64_t Field_double::val_int(void)
104
{
105
  double j;
106
  int64_t res;
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
107
108
  ASSERT_COLUMN_MARKED_FOR_READ;
109
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
110
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
111
  if (getTable()->getShare()->db_low_byte_first)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
112
  {
113
    float8get(j,ptr);
114
  }
115
  else
116
#endif
117
    doubleget(j,ptr);
118
  /* Check whether we fit into int64_t range */
119
  if (j <= (double) INT64_MIN)
120
  {
121
    res= (int64_t) INT64_MIN;
122
    goto warn;
123
  }
124
  if (j >= (double) (uint64_t) INT64_MAX)
125
  {
126
    res= (int64_t) INT64_MAX;
127
    goto warn;
128
  }
129
  return (int64_t) rint(j);
130
131
warn:
132
  {
133
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
383.1.12 by Brian Aker
Much closer toward UTF8 being around all the time...
134
    String tmp(buf, sizeof(buf), &my_charset_utf8_general_ci), *str;
892.1.2 by Monty Taylor
Fixed solaris warnings.
135
    str= val_str(&tmp, &tmp);
520.1.22 by Brian Aker
Second pass of thd cleanup
136
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
137
                        ER_TRUNCATED_WRONG_VALUE,
138
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
139
                        str->c_ptr());
140
  }
141
  return res;
142
}
143
144
145
String *Field_double::val_str(String *val_buffer,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
146
			      String *)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
147
{
148
  double nr;
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
149
150
  ASSERT_COLUMN_MARKED_FOR_READ;
151
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
152
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
153
  if (getTable()->getShare()->db_low_byte_first)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
154
  {
155
    float8get(nr,ptr);
156
  }
157
  else
158
#endif
159
    doubleget(nr,ptr);
160
1067.4.6 by Nathan Williams
Converted all usages of cmax in drizzled/field/ and drizzled/function/ to std::max.
161
  uint32_t to_length= max(field_length, (uint32_t)DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
162
  val_buffer->alloc(to_length);
163
  char *to=(char*) val_buffer->ptr();
164
  size_t len;
165
166
  if (dec >= NOT_FIXED_DEC)
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
167
    len= internal::my_gcvt(nr, internal::MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
168
  else
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
169
    len= internal::my_fcvt(nr, dec, to, NULL);
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
170
895 by Brian Aker
Completion (?) of uint conversion.
171
  val_buffer->length((uint32_t) len);
216 by Brian Aker
Remove completely ZEROFILL
172
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
173
  return val_buffer;
174
}
175
481 by Brian Aker
Remove all of uchar.
176
int Field_double::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
177
{
178
  double a,b;
179
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
180
  if (getTable()->getShare()->db_low_byte_first)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
181
  {
182
    float8get(a,a_ptr);
183
    float8get(b,b_ptr);
184
  }
185
  else
186
#endif
187
  {
188
    doubleget(a, a_ptr);
189
    doubleget(b, b_ptr);
190
  }
191
  return (a < b) ? -1 : (a > b) ? 1 : 0;
192
}
193
194
195
/* The following should work for IEEE */
196
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
197
void Field_double::sort_string(unsigned char *to,uint32_t )
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
198
{
199
  double nr;
200
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
201
  if (getTable()->getShare()->db_low_byte_first)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
202
  {
203
    float8get(nr,ptr);
204
  }
205
  else
206
#endif
207
    doubleget(nr,ptr);
208
  change_double_for_sort(nr, to);
209
}
210
211
212
void Field_double::sql_type(String &res) const
213
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
214
  const CHARSET_INFO * const cs=res.charset();
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
215
  if (dec == NOT_FIXED_DEC)
216
  {
217
    res.set_ascii(STRING_WITH_LEN("double"));
218
  }
219
  else
220
  {
221
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
222
			    "double(%d,%d)",(int) field_length,dec));
223
  }
224
}
225
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
226
} /* namespace drizzled */