1
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 MySQL
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.
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.
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
22
#include <drizzled/server_includes.h>
23
#include <drizzled/field/double.h>
24
#include <drizzled/drizzled_error_messages.h>
26
/****************************************************************************
27
double precision floating point numbers
28
****************************************************************************/
30
int Field_double::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
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)))
37
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
38
(error ? ER_WARN_DATA_OUT_OF_RANGE : ER_WARN_DATA_TRUNCATED), 1);
41
Field_double::store(nr);
46
int Field_double::store(double nr)
48
int error= truncate(&nr, DBL_MAX);
50
#ifdef WORDS_BIGENDIAN
51
if (table->s->db_low_byte_first)
62
int Field_double::store(int64_t nr, bool unsigned_val)
64
return Field_double::store(unsigned_val ? uint64_t2double((uint64_t) nr) :
68
double Field_double::val_real(void)
71
#ifdef WORDS_BIGENDIAN
72
if (table->s->db_low_byte_first)
82
int64_t Field_double::val_int(void)
86
#ifdef WORDS_BIGENDIAN
87
if (table->s->db_low_byte_first)
94
/* Check whether we fit into int64_t range */
95
if (j <= (double) INT64_MIN)
97
res= (int64_t) INT64_MIN;
100
if (j >= (double) (uint64_t) INT64_MAX)
102
res= (int64_t) INT64_MAX;
105
return (int64_t) rint(j);
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",
121
String *Field_double::val_str(String *val_buffer,
122
String *val_ptr __attribute__((unused)))
125
#ifdef WORDS_BIGENDIAN
126
if (table->s->db_low_byte_first)
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();
139
if (dec >= NOT_FIXED_DEC)
140
len= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
142
len= my_fcvt(nr, dec, to, NULL);
144
val_buffer->length((uint) len);
149
bool Field_double::send_binary(Protocol *protocol)
151
return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
155
int Field_double::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
158
#ifdef WORDS_BIGENDIAN
159
if (table->s->db_low_byte_first)
170
return (a < b) ? -1 : (a > b) ? 1 : 0;
174
#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
176
/* The following should work for IEEE */
178
void Field_double::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
181
#ifdef WORDS_BIGENDIAN
182
if (table->s->db_low_byte_first)
189
change_double_for_sort(nr, to);
194
Save the field metadata for double fields.
196
Saves the pack length in the first byte of the field metadata array
197
at index of *metadata_ptr.
199
@param metadata_ptr First byte of field metadata
201
@returns number of bytes written to metadata_ptr
203
int Field_double::do_save_field_metadata(unsigned char *metadata_ptr)
205
*metadata_ptr= pack_length();
210
void Field_double::sql_type(String &res) const
212
const CHARSET_INFO * const cs=res.charset();
213
if (dec == NOT_FIXED_DEC)
215
res.set_ascii(STRING_WITH_LEN("double"));
219
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
220
"double(%d,%d)",(int) field_length,dec));