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/field/short.h>
27
/****************************************************************************
28
Field type short int (2 byte)
29
****************************************************************************/
31
int Field_short::store(const char *from,uint len,CHARSET_INFO *cs)
37
error= get_int(cs, from, len, &rnd, UINT16_MAX, INT16_MIN, INT16_MAX);
38
store_tmp= unsigned_flag ? (int) (uint64_t) rnd : (int) rnd;
39
#ifdef WORDS_BIGENDIAN
40
if (table->s->db_low_byte_first)
42
int2store(ptr, store_tmp);
46
shortstore(ptr, (short) store_tmp);
51
int Field_short::store(double nr)
61
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
64
else if (nr > (double) UINT16_MAX)
66
res=(int16_t) UINT16_MAX;
67
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
71
res=(int16_t) (uint16_t) nr;
75
if (nr < (double) INT16_MIN)
78
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
81
else if (nr > (double) INT16_MAX)
84
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
88
res=(int16_t) (int) nr;
90
#ifdef WORDS_BIGENDIAN
91
if (table->s->db_low_byte_first)
102
int Field_short::store(int64_t nr, bool unsigned_val)
109
if (nr < 0L && !unsigned_val)
112
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
115
else if ((uint64_t) nr > (uint64_t) UINT16_MAX)
117
res=(int16_t) UINT16_MAX;
118
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
122
res=(int16_t) (uint16_t) nr;
126
if (nr < 0 && unsigned_val)
127
nr= UINT16_MAX+1; // Generate overflow
132
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
135
else if (nr > (int64_t) INT16_MAX)
138
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
144
#ifdef WORDS_BIGENDIAN
145
if (table->s->db_low_byte_first)
156
double Field_short::val_real(void)
159
#ifdef WORDS_BIGENDIAN
160
if (table->s->db_low_byte_first)
165
return unsigned_flag ? (double) (unsigned short) j : (double) j;
168
int64_t Field_short::val_int(void)
171
#ifdef WORDS_BIGENDIAN
172
if (table->s->db_low_byte_first)
177
return unsigned_flag ? (int64_t) (unsigned short) j : (int64_t) j;
181
String *Field_short::val_str(String *val_buffer,
182
String *val_ptr __attribute__((unused)))
184
CHARSET_INFO *cs= &my_charset_bin;
186
uint mlength=max(field_length+1,7*cs->mbmaxlen);
187
val_buffer->alloc(mlength);
188
char *to=(char*) val_buffer->ptr();
190
#ifdef WORDS_BIGENDIAN
191
if (table->s->db_low_byte_first)
198
length=(uint) cs->cset->long10_to_str(cs, to, mlength, 10,
199
(long) (uint16_t) j);
201
length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
202
val_buffer->length(length);
208
bool Field_short::send_binary(Protocol *protocol)
210
return protocol->store_short(Field_short::val_int());
214
int Field_short::cmp(const uchar *a_ptr, const uchar *b_ptr)
217
#ifdef WORDS_BIGENDIAN
218
if (table->s->db_low_byte_first)
231
return ((unsigned short) a < (unsigned short) b) ? -1 :
232
((unsigned short) a > (unsigned short) b) ? 1 : 0;
233
return (a < b) ? -1 : (a > b) ? 1 : 0;
236
void Field_short::sort_string(uchar *to,uint length __attribute__((unused)))
238
#ifdef WORDS_BIGENDIAN
239
if (!table->s->db_low_byte_first)
244
to[0] = (char) (ptr[0] ^ 128); /* Revers signbit */
253
to[0] = (char) (ptr[1] ^ 128); /* Revers signbit */
258
void Field_short::sql_type(String &res) const
260
CHARSET_INFO *cs=res.charset();
261
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "smallint"));