~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);
895 by Brian Aker
Completion (?) of uint conversion.
44
  if (error || (!len || (((uint32_t) (end-from) != len) && table->in_use->count_cuted_fields)))
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
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;
892.1.2 by Monty Taylor
Fixed solaris warnings.
120
    str= val_str(&tmp, &tmp);
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,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
131
			      String *)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
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
895 by Brian Aker
Completion (?) of uint conversion.
153
  val_buffer->length((uint32_t) 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
481 by Brian Aker
Remove all of uchar.
158
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
159
{
160
  double a,b;
161
#ifdef WORDS_BIGENDIAN
162
  if (table->s->db_low_byte_first)
163
  {
164
    float8get(a,a_ptr);
165
    float8get(b,b_ptr);
166
  }
167
  else
168
#endif
169
  {
170
    doubleget(a, a_ptr);
171
    doubleget(b, b_ptr);
172
  }
173
  return (a < b) ? -1 : (a > b) ? 1 : 0;
174
}
175
176
177
/* The following should work for IEEE */
178
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
179
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
180
{
181
  double nr;
182
#ifdef WORDS_BIGENDIAN
183
  if (table->s->db_low_byte_first)
184
  {
185
    float8get(nr,ptr);
186
  }
187
  else
188
#endif
189
    doubleget(nr,ptr);
190
  change_double_for_sort(nr, to);
191
}
192
193
194
/**
195
   Save the field metadata for double fields.
196
197
   Saves the pack length in the first byte of the field metadata array
198
   at index of *metadata_ptr.
199
200
   @param   metadata_ptr   First byte of field metadata
201
202
   @returns number of bytes written to metadata_ptr
203
*/
481 by Brian Aker
Remove all of uchar.
204
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
205
{
206
  *metadata_ptr= pack_length();
207
  return 1;
208
}
209
210
211
void Field_double::sql_type(String &res) const
212
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
213
  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
214
  if (dec == NOT_FIXED_DEC)
215
  {
216
    res.set_ascii(STRING_WITH_LEN("double"));
217
  }
218
  else
219
  {
220
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
221
			    "double(%d,%d)",(int) field_length,dec));
222
  }
223
}
224