~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/double.cc

Removed dead variable, sorted authors file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
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
 
 
22
 
#include <drizzled/server_includes.h>
23
 
#include <drizzled/field/double.h>
24
 
#include <drizzled/drizzled_error_messages.h>
25
 
 
26
 
/****************************************************************************
27
 
  double precision floating point numbers
28
 
****************************************************************************/
29
 
 
30
 
int Field_double::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
31
 
{
32
 
  int error;
33
 
  char *end;
34
 
  double nr= my_strntod(cs,(char*) from, len, &end, &error);
35
 
  if (error || (!len || (((uint) (end-from) != len) && table->in_use->count_cuted_fields)))
36
 
  {
37
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
38
 
                (error ? ER_WARN_DATA_OUT_OF_RANGE : ER_WARN_DATA_TRUNCATED), 1);
39
 
    error= error ? 1 : 2;
40
 
  }
41
 
  Field_double::store(nr);
42
 
  return error;
43
 
}
44
 
 
45
 
 
46
 
int Field_double::store(double nr)
47
 
{
48
 
  int error= truncate(&nr, DBL_MAX);
49
 
 
50
 
#ifdef WORDS_BIGENDIAN
51
 
  if (table->s->db_low_byte_first)
52
 
  {
53
 
    float8store(ptr,nr);
54
 
  }
55
 
  else
56
 
#endif
57
 
    doublestore(ptr,nr);
58
 
  return error;
59
 
}
60
 
 
61
 
 
62
 
int Field_double::store(int64_t nr, bool unsigned_val)
63
 
{
64
 
  return Field_double::store(unsigned_val ? uint64_t2double((uint64_t) nr) :
65
 
                             (double) nr);
66
 
}
67
 
 
68
 
double Field_double::val_real(void)
69
 
{
70
 
  double j;
71
 
#ifdef WORDS_BIGENDIAN
72
 
  if (table->s->db_low_byte_first)
73
 
  {
74
 
    float8get(j,ptr);
75
 
  }
76
 
  else
77
 
#endif
78
 
    doubleget(j,ptr);
79
 
  return j;
80
 
}
81
 
 
82
 
int64_t Field_double::val_int(void)
83
 
{
84
 
  double j;
85
 
  int64_t res;
86
 
#ifdef WORDS_BIGENDIAN
87
 
  if (table->s->db_low_byte_first)
88
 
  {
89
 
    float8get(j,ptr);
90
 
  }
91
 
  else
92
 
#endif
93
 
    doubleget(j,ptr);
94
 
  /* Check whether we fit into int64_t range */
95
 
  if (j <= (double) INT64_MIN)
96
 
  {
97
 
    res= (int64_t) INT64_MIN;
98
 
    goto warn;
99
 
  }
100
 
  if (j >= (double) (uint64_t) INT64_MAX)
101
 
  {
102
 
    res= (int64_t) INT64_MAX;
103
 
    goto warn;
104
 
  }
105
 
  return (int64_t) rint(j);
106
 
 
107
 
warn:
108
 
  {
109
 
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
110
 
    String tmp(buf, sizeof(buf), &my_charset_utf8_general_ci), *str;
111
 
    str= val_str(&tmp, 0);
112
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
113
 
                        ER_TRUNCATED_WRONG_VALUE,
114
 
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
115
 
                        str->c_ptr());
116
 
  }
117
 
  return res;
118
 
}
119
 
 
120
 
 
121
 
String *Field_double::val_str(String *val_buffer,
122
 
                              String *val_ptr __attribute__((unused)))
123
 
{
124
 
  double nr;
125
 
#ifdef WORDS_BIGENDIAN
126
 
  if (table->s->db_low_byte_first)
127
 
  {
128
 
    float8get(nr,ptr);
129
 
  }
130
 
  else
131
 
#endif
132
 
    doubleget(nr,ptr);
133
 
 
134
 
  uint32_t to_length=cmax(field_length, (uint32_t)DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
135
 
  val_buffer->alloc(to_length);
136
 
  char *to=(char*) val_buffer->ptr();
137
 
  size_t len;
138
 
 
139
 
  if (dec >= NOT_FIXED_DEC)
140
 
    len= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
141
 
  else
142
 
    len= my_fcvt(nr, dec, to, NULL);
143
 
 
144
 
  val_buffer->length((uint) len);
145
 
 
146
 
  return val_buffer;
147
 
}
148
 
 
149
 
bool Field_double::send_binary(Protocol *protocol)
150
 
{
151
 
  return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
152
 
}
153
 
 
154
 
 
155
 
int Field_double::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
156
 
{
157
 
  double a,b;
158
 
#ifdef WORDS_BIGENDIAN
159
 
  if (table->s->db_low_byte_first)
160
 
  {
161
 
    float8get(a,a_ptr);
162
 
    float8get(b,b_ptr);
163
 
  }
164
 
  else
165
 
#endif
166
 
  {
167
 
    doubleget(a, a_ptr);
168
 
    doubleget(b, b_ptr);
169
 
  }
170
 
  return (a < b) ? -1 : (a > b) ? 1 : 0;
171
 
}
172
 
 
173
 
 
174
 
#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
175
 
 
176
 
/* The following should work for IEEE */
177
 
 
178
 
void Field_double::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
179
 
{
180
 
  double nr;
181
 
#ifdef WORDS_BIGENDIAN
182
 
  if (table->s->db_low_byte_first)
183
 
  {
184
 
    float8get(nr,ptr);
185
 
  }
186
 
  else
187
 
#endif
188
 
    doubleget(nr,ptr);
189
 
  change_double_for_sort(nr, to);
190
 
}
191
 
 
192
 
 
193
 
/**
194
 
   Save the field metadata for double fields.
195
 
 
196
 
   Saves the pack length in the first byte of the field metadata array
197
 
   at index of *metadata_ptr.
198
 
 
199
 
   @param   metadata_ptr   First byte of field metadata
200
 
 
201
 
   @returns number of bytes written to metadata_ptr
202
 
*/
203
 
int Field_double::do_save_field_metadata(unsigned char *metadata_ptr)
204
 
{
205
 
  *metadata_ptr= pack_length();
206
 
  return 1;
207
 
}
208
 
 
209
 
 
210
 
void Field_double::sql_type(String &res) const
211
 
{
212
 
  const CHARSET_INFO * const cs=res.charset();
213
 
  if (dec == NOT_FIXED_DEC)
214
 
  {
215
 
    res.set_ascii(STRING_WITH_LEN("double"));
216
 
  }
217
 
  else
218
 
  {
219
 
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
220
 
                            "double(%d,%d)",(int) field_length,dec));
221
 
  }
222
 
}
223