~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/double.cc

  • Committer: Brian Aker
  • Date: 2010-02-07 01:33:54 UTC
  • Revision ID: brian@gaz-20100207013354-d2pg1n68u5c09pgo
Remove giant include header to its own file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
1
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
4
 *  Copyright (C) 2008 MySQL
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#ifdef USE_PRAGMA_IMPLEMENTATION
22
 
#pragma implementation                          // gcc: Class implementation
23
 
#endif
24
 
 
25
 
#include <drizzled/server_includes.h>
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <float.h>
 
25
#include <math.h>
 
26
 
 
27
#include <algorithm>
 
28
 
26
29
#include <drizzled/field/double.h>
27
 
#include <drizzled/drizzled_error_messages.h>
 
30
#include <drizzled/error.h>
 
31
#include <drizzled/table.h>
 
32
#include <drizzled/session.h>
 
33
#include "drizzled/internal/m_string.h"
 
34
 
 
35
using namespace std;
 
36
 
 
37
namespace drizzled
 
38
{
28
39
 
29
40
/****************************************************************************
30
41
  double precision floating point numbers
31
42
****************************************************************************/
32
43
 
33
 
int Field_double::store(const char *from,uint len, const CHARSET_INFO * const cs)
 
44
int Field_double::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
34
45
{
35
46
  int error;
36
47
  char *end;
37
48
  double nr= my_strntod(cs,(char*) from, len, &end, &error);
38
 
  if (error || (!len || (((uint) (end-from) != len) && table->in_use->count_cuted_fields)))
 
49
 
 
50
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
51
  if (error || (!len || (((uint32_t) (end-from) != len) && table->in_use->count_cuted_fields)))
39
52
  {
40
53
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
41
54
                (error ? ER_WARN_DATA_OUT_OF_RANGE : ER_WARN_DATA_TRUNCATED), 1);
50
63
{
51
64
  int error= truncate(&nr, DBL_MAX);
52
65
 
 
66
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
67
 
53
68
#ifdef WORDS_BIGENDIAN
54
69
  if (table->s->db_low_byte_first)
55
70
  {
68
83
                             (double) nr);
69
84
}
70
85
 
71
 
/*
72
 
  If a field has fixed length, truncate the double argument pointed to by 'nr'
73
 
  appropriately.
74
 
  Also ensure that the argument is within [-max_value; max_value] range.
75
 
*/
76
 
 
77
 
int Field_real::truncate(double *nr, double max_value)
78
 
{
79
 
  int error= 1;
80
 
  double res= *nr;
81
 
  
82
 
  if (isnan(res))
83
 
  {
84
 
    res= 0;
85
 
    set_null();
86
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
87
 
    goto end;
88
 
  }
89
 
  else if (unsigned_flag && res < 0)
90
 
  {
91
 
    res= 0;
92
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
93
 
    goto end;
94
 
  }
95
 
 
96
 
  if (!not_fixed)
97
 
  {
98
 
    uint order= field_length - dec;
99
 
    uint step= array_elements(log_10) - 1;
100
 
    max_value= 1.0;
101
 
    for (; order > step; order-= step)
102
 
      max_value*= log_10[step];
103
 
    max_value*= log_10[order];
104
 
    max_value-= 1.0 / log_10[dec];
105
 
 
106
 
    /* Check for infinity so we don't get NaN in calculations */
107
 
    if (!my_isinf(res))
108
 
    {
109
 
      double tmp= rint((res - floor(res)) * log_10[dec]) / log_10[dec];
110
 
      res= floor(res) + tmp;
111
 
    }
112
 
  }
113
 
  
114
 
  if (res < -max_value)
115
 
  {
116
 
   res= -max_value;
117
 
   set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
118
 
  }
119
 
  else if (res > max_value)
120
 
  {
121
 
    res= max_value;
122
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
123
 
  }
124
 
  else
125
 
    error= 0;
126
 
 
127
 
end:
128
 
  *nr= res;
129
 
  return error;
130
 
}
131
 
 
132
 
 
133
 
int Field_real::store_decimal(const my_decimal *dm)
134
 
{
135
 
  double dbl;
136
 
  my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
137
 
  return store(dbl);
138
 
}
139
 
 
140
86
double Field_double::val_real(void)
141
87
{
142
88
  double j;
 
89
 
 
90
  ASSERT_COLUMN_MARKED_FOR_READ;
 
91
 
143
92
#ifdef WORDS_BIGENDIAN
144
93
  if (table->s->db_low_byte_first)
145
94
  {
155
104
{
156
105
  double j;
157
106
  int64_t res;
 
107
 
 
108
  ASSERT_COLUMN_MARKED_FOR_READ;
 
109
 
158
110
#ifdef WORDS_BIGENDIAN
159
111
  if (table->s->db_low_byte_first)
160
112
  {
179
131
warn:
180
132
  {
181
133
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
182
 
    String tmp(buf, sizeof(buf), &my_charset_latin1), *str;
183
 
    str= val_str(&tmp, 0);
184
 
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
134
    String tmp(buf, sizeof(buf), &my_charset_utf8_general_ci), *str;
 
135
    str= val_str(&tmp, &tmp);
 
136
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
185
137
                        ER_TRUNCATED_WRONG_VALUE,
186
138
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
187
139
                        str->c_ptr());
190
142
}
191
143
 
192
144
 
193
 
my_decimal *Field_real::val_decimal(my_decimal *decimal_value)
194
 
{
195
 
  double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
196
 
  return decimal_value;
197
 
}
198
 
 
199
 
 
200
145
String *Field_double::val_str(String *val_buffer,
201
 
                              String *val_ptr __attribute__((unused)))
 
146
                              String *)
202
147
{
203
148
  double nr;
 
149
 
 
150
  ASSERT_COLUMN_MARKED_FOR_READ;
 
151
 
204
152
#ifdef WORDS_BIGENDIAN
205
153
  if (table->s->db_low_byte_first)
206
154
  {
210
158
#endif
211
159
    doubleget(nr,ptr);
212
160
 
213
 
  uint to_length=max(field_length, (uint32_t)DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
 
161
  uint32_t to_length= max(field_length, (uint32_t)DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
214
162
  val_buffer->alloc(to_length);
215
163
  char *to=(char*) val_buffer->ptr();
216
164
  size_t len;
217
165
 
218
166
  if (dec >= NOT_FIXED_DEC)
219
 
    len= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
 
167
    len= internal::my_gcvt(nr, internal::MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
220
168
  else
221
 
    len= my_fcvt(nr, dec, to, NULL);
 
169
    len= internal::my_fcvt(nr, dec, to, NULL);
222
170
 
223
 
  val_buffer->length((uint) len);
 
171
  val_buffer->length((uint32_t) len);
224
172
 
225
173
  return val_buffer;
226
174
}
227
175
 
228
 
bool Field_double::send_binary(Protocol *protocol)
229
 
{
230
 
  return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
231
 
}
232
 
 
233
 
 
234
 
int Field_double::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
176
int Field_double::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
235
177
{
236
178
  double a,b;
237
179
#ifdef WORDS_BIGENDIAN
250
192
}
251
193
 
252
194
 
253
 
#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
254
 
 
255
195
/* The following should work for IEEE */
256
196
 
257
 
void Field_double::sort_string(uchar *to,uint length __attribute__((unused)))
 
197
void Field_double::sort_string(unsigned char *to,uint32_t )
258
198
{
259
199
  double nr;
260
200
#ifdef WORDS_BIGENDIAN
269
209
}
270
210
 
271
211
 
272
 
/**
273
 
   Save the field metadata for double fields.
274
 
 
275
 
   Saves the pack length in the first byte of the field metadata array
276
 
   at index of *metadata_ptr.
277
 
 
278
 
   @param   metadata_ptr   First byte of field metadata
279
 
 
280
 
   @returns number of bytes written to metadata_ptr
281
 
*/
282
 
int Field_double::do_save_field_metadata(uchar *metadata_ptr)
283
 
{
284
 
  *metadata_ptr= pack_length();
285
 
  return 1;
286
 
}
287
 
 
288
 
 
289
212
void Field_double::sql_type(String &res) const
290
213
{
291
214
  const CHARSET_INFO * const cs=res.charset();
298
221
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
299
222
                            "double(%d,%d)",(int) field_length,dec));
300
223
  }
301
 
  add_unsigned(res);
302
224
}
303
225
 
 
226
} /* namespace drizzled */