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/long.h>
25
/****************************************************************************
27
****************************************************************************/
29
int Field_long::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
35
error= get_int(cs, from, len, &rnd, UINT32_MAX, INT32_MIN, INT32_MAX);
36
store_tmp= (long) rnd;
37
#ifdef WORDS_BIGENDIAN
38
if (table->s->db_low_byte_first)
40
int4store(ptr, store_tmp);
44
longstore(ptr, store_tmp);
49
int Field_long::store(double nr)
55
if (nr < (double) INT32_MIN)
57
res=(int32_t) INT32_MIN;
60
else if (nr > (double) INT32_MAX)
62
res=(int32_t) INT32_MAX;
66
res=(int32_t) (int64_t) nr;
69
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
71
#ifdef WORDS_BIGENDIAN
72
if (table->s->db_low_byte_first)
83
int Field_long::store(int64_t nr, bool unsigned_val)
88
if (nr < 0 && unsigned_val)
89
nr= ((int64_t) INT32_MAX) + 1; // Generate overflow
90
if (nr < (int64_t) INT32_MIN)
92
res=(int32_t) INT32_MIN;
95
else if (nr > (int64_t) INT32_MAX)
97
res=(int32_t) INT32_MAX;
104
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
106
#ifdef WORDS_BIGENDIAN
107
if (table->s->db_low_byte_first)
118
double Field_long::val_real(void)
121
#ifdef WORDS_BIGENDIAN
122
if (table->s->db_low_byte_first)
130
int64_t Field_long::val_int(void)
133
/* See the comment in Field_long::store(int64_t) */
134
assert(table->in_use == current_thd);
135
#ifdef WORDS_BIGENDIAN
136
if (table->s->db_low_byte_first)
144
String *Field_long::val_str(String *val_buffer,
145
String *val_ptr __attribute__((unused)))
147
const CHARSET_INFO * const cs= &my_charset_bin;
149
uint32_t mlength=cmax(field_length+1,12*cs->mbmaxlen);
150
val_buffer->alloc(mlength);
151
char *to=(char*) val_buffer->ptr();
153
#ifdef WORDS_BIGENDIAN
154
if (table->s->db_low_byte_first)
160
length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
161
val_buffer->length(length);
167
bool Field_long::send_binary(Protocol *protocol)
169
return protocol->store_long(Field_long::val_int());
172
int Field_long::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
175
#ifdef WORDS_BIGENDIAN
176
if (table->s->db_low_byte_first)
188
return (a < b) ? -1 : (a > b) ? 1 : 0;
191
void Field_long::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
193
#ifdef WORDS_BIGENDIAN
194
if (!table->s->db_low_byte_first)
196
to[0] = (char) (ptr[0] ^ 128); /* Revers signbit */
204
to[0] = (char) (ptr[3] ^ 128); /* Revers signbit */
212
void Field_long::sql_type(String &res) const
214
const CHARSET_INFO * const cs=res.charset();
215
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "int"));