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
21
#ifdef USE_PRAGMA_IMPLEMENTATION
22
#pragma implementation // gcc: Class implementation
25
#include <drizzled/server_includes.h>
26
#include <drizzled/field/int64_t.h>
28
/****************************************************************************
29
Field type int64_t int (8 bytes)
30
****************************************************************************/
32
int Field_int64_t::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
38
tmp= cs->cset->strntoull10rnd(cs,from,len,unsigned_flag,&end,&error);
39
if (error == MY_ERRNO_ERANGE)
41
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
44
else if (table->in_use->count_cuted_fields &&
45
check_int(cs, from, len, end, error))
49
#ifdef WORDS_BIGENDIAN
50
if (table->s->db_low_byte_first)
56
int64_tstore(ptr,tmp);
61
int Field_int64_t::store(double nr)
74
else if (nr >= (double) UINT64_MAX)
80
res=(int64_t) (uint64_t) nr;
84
if (nr <= (double) INT64_MIN)
87
error= (nr < (double) INT64_MIN);
89
else if (nr >= (double) (uint64_t) INT64_MAX)
92
error= (nr > (double) INT64_MAX);
98
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
100
#ifdef WORDS_BIGENDIAN
101
if (table->s->db_low_byte_first)
107
int64_tstore(ptr,res);
112
int Field_int64_t::store(int64_t nr, bool unsigned_val)
116
if (nr < 0) // Only possible error
119
if field is unsigned and value is signed (< 0) or
120
if field is signed and value is unsigned we have an overflow
122
if (unsigned_flag != unsigned_val)
124
nr= unsigned_flag ? (uint64_t) 0 : (uint64_t) INT64_MAX;
125
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
130
#ifdef WORDS_BIGENDIAN
131
if (table->s->db_low_byte_first)
137
int64_tstore(ptr,nr);
142
double Field_int64_t::val_real(void)
145
#ifdef WORDS_BIGENDIAN
146
if (table->s->db_low_byte_first)
153
/* The following is open coded to avoid a bug in gcc 3.3 */
156
uint64_t tmp= (uint64_t) j;
157
return uint64_t2double(tmp);
163
int64_t Field_int64_t::val_int(void)
166
#ifdef WORDS_BIGENDIAN
167
if (table->s->db_low_byte_first)
176
String *Field_int64_t::val_str(String *val_buffer,
177
String *val_ptr __attribute__((unused)))
179
const CHARSET_INFO * const cs= &my_charset_bin;
181
uint32_t mlength=cmax(field_length+1,22*cs->mbmaxlen);
182
val_buffer->alloc(mlength);
183
char *to=(char*) val_buffer->ptr();
185
#ifdef WORDS_BIGENDIAN
186
if (table->s->db_low_byte_first)
192
length=(uint) (cs->cset->int64_t10_to_str)(cs,to,mlength,
193
unsigned_flag ? 10 : -10, j);
194
val_buffer->length(length);
200
bool Field_int64_t::send_binary(Protocol *protocol)
202
return protocol->store_int64_t(Field_int64_t::val_int(), unsigned_flag);
206
int Field_int64_t::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
209
#ifdef WORDS_BIGENDIAN
210
if (table->s->db_low_byte_first)
222
return ((uint64_t) a < (uint64_t) b) ? -1 :
223
((uint64_t) a > (uint64_t) b) ? 1 : 0;
224
return (a < b) ? -1 : (a > b) ? 1 : 0;
227
void Field_int64_t::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
229
#ifdef WORDS_BIGENDIAN
230
if (!table->s->db_low_byte_first)
235
to[0] = (char) (ptr[0] ^ 128); /* Revers signbit */
250
to[0] = (char) (ptr[7] ^ 128); /* Revers signbit */
262
void Field_int64_t::sql_type(String &res) const
264
const CHARSET_INFO * const cs=res.charset();
265
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "bigint"));
271
Floating-point numbers
275
Field_real::pack(unsigned char *to, const unsigned char *from,
276
uint32_t max_length, bool low_byte_first)
278
assert(max_length >= pack_length());
279
#ifdef WORDS_BIGENDIAN
280
if (low_byte_first != table->s->db_low_byte_first)
282
const unsigned char *dptr= from + pack_length();
283
while (dptr-- > from)
289
return(Field::pack(to, from, max_length, low_byte_first));
292
const unsigned char *
293
Field_real::unpack(unsigned char *to, const unsigned char *from,
294
uint32_t param_data, bool low_byte_first)
296
#ifdef WORDS_BIGENDIAN
297
if (low_byte_first != table->s->db_low_byte_first)
299
const unsigned char *dptr= from + pack_length();
300
while (dptr-- > from)
302
return(from + pack_length());
306
return(Field::unpack(to, from, param_data, low_byte_first));