~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
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
22
#include <drizzled/server_includes.h>
214 by Brian Aker
Rename of fields (fix issue with string and decimal .h clashing).
23
#include <drizzled/field/double.h>
549 by Monty Taylor
Took gettext.h out of header files.
24
#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.
25
#include <drizzled/table.h>
26
#include <drizzled/session.h>
27
#include <drizzled/protocol.h>
28
572.1.4 by Monty Taylor
Removed a bunch of unusued tests and defines from autoconf.
29
#include CMATH_H
30
31
#if defined(CMATH_NAMESPACE)
32
using namespace CMATH_NAMESPACE;
33
#endif
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
34
35
/****************************************************************************
36
  double precision floating point numbers
37
****************************************************************************/
38
482 by Brian Aker
Remove uint.
39
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
40
{
41
  int error;
42
  char *end;
43
  double nr= my_strntod(cs,(char*) from, len, &end, &error);
44
  if (error || (!len || (((uint) (end-from) != len) && table->in_use->count_cuted_fields)))
45
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
46
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
47
                (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
48
    error= error ? 1 : 2;
49
  }
50
  Field_double::store(nr);
51
  return error;
52
}
53
54
55
int Field_double::store(double nr)
56
{
57
  int error= truncate(&nr, DBL_MAX);
58
59
#ifdef WORDS_BIGENDIAN
60
  if (table->s->db_low_byte_first)
61
  {
62
    float8store(ptr,nr);
63
  }
64
  else
65
#endif
66
    doublestore(ptr,nr);
67
  return error;
68
}
69
70
71
int Field_double::store(int64_t nr, bool unsigned_val)
72
{
73
  return Field_double::store(unsigned_val ? uint64_t2double((uint64_t) nr) :
74
                             (double) nr);
75
}
76
77
double Field_double::val_real(void)
78
{
79
  double j;
80
#ifdef WORDS_BIGENDIAN
81
  if (table->s->db_low_byte_first)
82
  {
83
    float8get(j,ptr);
84
  }
85
  else
86
#endif
87
    doubleget(j,ptr);
88
  return j;
89
}
90
91
int64_t Field_double::val_int(void)
92
{
93
  double j;
94
  int64_t res;
95
#ifdef WORDS_BIGENDIAN
96
  if (table->s->db_low_byte_first)
97
  {
98
    float8get(j,ptr);
99
  }
100
  else
101
#endif
102
    doubleget(j,ptr);
103
  /* Check whether we fit into int64_t range */
104
  if (j <= (double) INT64_MIN)
105
  {
106
    res= (int64_t) INT64_MIN;
107
    goto warn;
108
  }
109
  if (j >= (double) (uint64_t) INT64_MAX)
110
  {
111
    res= (int64_t) INT64_MAX;
112
    goto warn;
113
  }
114
  return (int64_t) rint(j);
115
116
warn:
117
  {
118
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
383.1.12 by Brian Aker
Much closer toward UTF8 being around all the time...
119
    String tmp(buf, sizeof(buf), &my_charset_utf8_general_ci), *str;
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
120
    str= val_str(&tmp, 0);
520.1.22 by Brian Aker
Second pass of thd cleanup
121
    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
122
                        ER_TRUNCATED_WRONG_VALUE,
123
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
124
                        str->c_ptr());
125
  }
126
  return res;
127
}
128
129
130
String *Field_double::val_str(String *val_buffer,
131
			      String *val_ptr __attribute__((unused)))
132
{
133
  double nr;
134
#ifdef WORDS_BIGENDIAN
135
  if (table->s->db_low_byte_first)
136
  {
137
    float8get(nr,ptr);
138
  }
139
  else
140
#endif
141
    doubleget(nr,ptr);
142
482 by Brian Aker
Remove uint.
143
  uint32_t to_length=cmax(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
144
  val_buffer->alloc(to_length);
145
  char *to=(char*) val_buffer->ptr();
146
  size_t len;
147
148
  if (dec >= NOT_FIXED_DEC)
149
    len= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
150
  else
151
    len= my_fcvt(nr, dec, to, NULL);
152
153
  val_buffer->length((uint) len);
216 by Brian Aker
Remove completely ZEROFILL
154
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
155
  return val_buffer;
156
}
157
158
bool Field_double::send_binary(Protocol *protocol)
159
{
160
  return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
161
}
162
163
481 by Brian Aker
Remove all of uchar.
164
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
165
{
166
  double a,b;
167
#ifdef WORDS_BIGENDIAN
168
  if (table->s->db_low_byte_first)
169
  {
170
    float8get(a,a_ptr);
171
    float8get(b,b_ptr);
172
  }
173
  else
174
#endif
175
  {
176
    doubleget(a, a_ptr);
177
    doubleget(b, b_ptr);
178
  }
179
  return (a < b) ? -1 : (a > b) ? 1 : 0;
180
}
181
182
183
/* The following should work for IEEE */
184
482 by Brian Aker
Remove uint.
185
void Field_double::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
186
{
187
  double nr;
188
#ifdef WORDS_BIGENDIAN
189
  if (table->s->db_low_byte_first)
190
  {
191
    float8get(nr,ptr);
192
  }
193
  else
194
#endif
195
    doubleget(nr,ptr);
196
  change_double_for_sort(nr, to);
197
}
198
199
200
/**
201
   Save the field metadata for double fields.
202
203
   Saves the pack length in the first byte of the field metadata array
204
   at index of *metadata_ptr.
205
206
   @param   metadata_ptr   First byte of field metadata
207
208
   @returns number of bytes written to metadata_ptr
209
*/
481 by Brian Aker
Remove all of uchar.
210
int Field_double::do_save_field_metadata(unsigned char *metadata_ptr)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
211
{
212
  *metadata_ptr= pack_length();
213
  return 1;
214
}
215
216
217
void Field_double::sql_type(String &res) const
218
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
219
  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
220
  if (dec == NOT_FIXED_DEC)
221
  {
222
    res.set_ascii(STRING_WITH_LEN("double"));
223
  }
224
  else
225
  {
226
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
227
			    "double(%d,%d)",(int) field_length,dec));
228
  }
229
}
230