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
29
#include <drizzled/field/double.h>
30
#include <drizzled/error.h>
31
#include <drizzled/table.h>
32
#include <drizzled/session.h>
33
#include <drizzled/current_session.h>
34
#include <drizzled/internal/m_string.h>
41
/****************************************************************************
42
double precision floating point numbers
43
****************************************************************************/
45
int Field_double::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
49
double nr= my_strntod(cs,(char*) from, len, &end, &error);
51
ASSERT_COLUMN_MARKED_FOR_WRITE;
52
if (error || (!len || (((uint32_t) (end-from) != len) && getTable()->in_use->count_cuted_fields)))
54
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
55
(error ? ER_WARN_DATA_OUT_OF_RANGE : ER_WARN_DATA_TRUNCATED), 1);
58
Field_double::store(nr);
63
int Field_double::store(double nr)
65
int error= truncate(&nr, DBL_MAX);
67
ASSERT_COLUMN_MARKED_FOR_WRITE;
69
#ifdef WORDS_BIGENDIAN
70
if (getTable()->getShare()->db_low_byte_first)
81
int Field_double::store(int64_t nr, bool unsigned_val)
83
return Field_double::store(unsigned_val ? uint64_t2double((uint64_t) nr) :
87
double Field_double::val_real(void) const
91
ASSERT_COLUMN_MARKED_FOR_READ;
93
#ifdef WORDS_BIGENDIAN
94
if (getTable()->getShare()->db_low_byte_first)
104
int64_t Field_double::val_int(void) const
109
ASSERT_COLUMN_MARKED_FOR_READ;
111
#ifdef WORDS_BIGENDIAN
112
if (getTable()->getShare()->db_low_byte_first)
119
/* Check whether we fit into int64_t range */
120
if (j <= (double) INT64_MIN)
122
res= (int64_t) INT64_MIN;
125
if (j >= (double) (uint64_t) INT64_MAX)
127
res= (int64_t) INT64_MAX;
130
return (int64_t) rint(j);
134
char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
135
String tmp(buf, sizeof(buf), &my_charset_utf8_general_ci), *str;
136
str= val_str(&tmp, &tmp);
137
Session *session= getTable() ? getTable()->in_use : current_session;
138
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
139
ER_TRUNCATED_WRONG_VALUE,
140
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
147
String *Field_double::val_str(String *val_buffer, String *) const
151
ASSERT_COLUMN_MARKED_FOR_READ;
153
#ifdef WORDS_BIGENDIAN
154
if (getTable()->getShare()->db_low_byte_first)
162
uint32_t to_length= max(field_length, (uint32_t)DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
163
val_buffer->alloc(to_length);
164
char *to=(char*) val_buffer->ptr();
167
if (dec >= NOT_FIXED_DEC)
168
len= internal::my_gcvt(nr, internal::MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
170
len= internal::my_fcvt(nr, dec, to, NULL);
172
val_buffer->length((uint32_t) len);
177
int Field_double::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
180
#ifdef WORDS_BIGENDIAN
181
if (getTable()->getShare()->db_low_byte_first)
192
return (a < b) ? -1 : (a > b) ? 1 : 0;
196
/* The following should work for IEEE */
198
void Field_double::sort_string(unsigned char *to,uint32_t )
201
#ifdef WORDS_BIGENDIAN
202
if (getTable()->getShare()->db_low_byte_first)
209
change_double_for_sort(nr, to);
213
void Field_double::sql_type(String &res) const
215
const CHARSET_INFO * const cs=res.charset();
216
if (dec == NOT_FIXED_DEC)
218
res.set_ascii(STRING_WITH_LEN("double"));
222
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
223
"double(%d,%d)",(int) field_length,dec));
227
} /* namespace drizzled */