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/internal/m_string.h"
40
/****************************************************************************
41
double precision floating point numbers
42
****************************************************************************/
44
int Field_double::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
48
double nr= my_strntod(cs,(char*) from, len, &end, &error);
50
ASSERT_COLUMN_MARKED_FOR_WRITE;
51
if (error || (!len || (((uint32_t) (end-from) != len) && getTable()->in_use->count_cuted_fields)))
53
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
54
(error ? ER_WARN_DATA_OUT_OF_RANGE : ER_WARN_DATA_TRUNCATED), 1);
57
Field_double::store(nr);
62
int Field_double::store(double nr)
64
int error= truncate(&nr, DBL_MAX);
66
ASSERT_COLUMN_MARKED_FOR_WRITE;
68
#ifdef WORDS_BIGENDIAN
69
if (getTable()->getShare()->db_low_byte_first)
80
int Field_double::store(int64_t nr, bool unsigned_val)
82
return Field_double::store(unsigned_val ? uint64_t2double((uint64_t) nr) :
86
double Field_double::val_real(void)
90
ASSERT_COLUMN_MARKED_FOR_READ;
92
#ifdef WORDS_BIGENDIAN
93
if (getTable()->s->db_low_byte_first)
103
int64_t Field_double::val_int(void)
108
ASSERT_COLUMN_MARKED_FOR_READ;
110
#ifdef WORDS_BIGENDIAN
111
if (getTable()->s->db_low_byte_first)
118
/* Check whether we fit into int64_t range */
119
if (j <= (double) INT64_MIN)
121
res= (int64_t) INT64_MIN;
124
if (j >= (double) (uint64_t) INT64_MAX)
126
res= (int64_t) INT64_MAX;
129
return (int64_t) rint(j);
133
char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
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,
137
ER_TRUNCATED_WRONG_VALUE,
138
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
145
String *Field_double::val_str(String *val_buffer,
150
ASSERT_COLUMN_MARKED_FOR_READ;
152
#ifdef WORDS_BIGENDIAN
153
if (getTable()->s->db_low_byte_first)
161
uint32_t to_length= max(field_length, (uint32_t)DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
162
val_buffer->alloc(to_length);
163
char *to=(char*) val_buffer->ptr();
166
if (dec >= NOT_FIXED_DEC)
167
len= internal::my_gcvt(nr, internal::MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
169
len= internal::my_fcvt(nr, dec, to, NULL);
171
val_buffer->length((uint32_t) len);
176
int Field_double::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
179
#ifdef WORDS_BIGENDIAN
180
if (getTable()->s->db_low_byte_first)
191
return (a < b) ? -1 : (a > b) ? 1 : 0;
195
/* The following should work for IEEE */
197
void Field_double::sort_string(unsigned char *to,uint32_t )
200
#ifdef WORDS_BIGENDIAN
201
if (getTable()->s->db_low_byte_first)
208
change_double_for_sort(nr, to);
212
void Field_double::sql_type(String &res) const
214
const CHARSET_INFO * const cs=res.charset();
215
if (dec == NOT_FIXED_DEC)
217
res.set_ascii(STRING_WITH_LEN("double"));
221
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
222
"double(%d,%d)",(int) field_length,dec));
226
} /* namespace drizzled */