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/short.h>
28
/****************************************************************************
29
Field type short int (2 byte)
30
****************************************************************************/
32
int Field_short::store(const char *from,uint len, const CHARSET_INFO * const cs)
38
error= get_int(cs, from, len, &rnd, UINT16_MAX, INT16_MIN, INT16_MAX);
39
store_tmp= unsigned_flag ? (int) (uint64_t) rnd : (int) rnd;
40
#ifdef WORDS_BIGENDIAN
41
if (table->s->db_low_byte_first)
43
int2store(ptr, store_tmp);
47
shortstore(ptr, (short) store_tmp);
52
int Field_short::store(double nr)
62
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
65
else if (nr > (double) UINT16_MAX)
67
res=(int16_t) UINT16_MAX;
68
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
72
res=(int16_t) (uint16_t) nr;
76
if (nr < (double) INT16_MIN)
79
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
82
else if (nr > (double) INT16_MAX)
85
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
89
res=(int16_t) (int) nr;
91
#ifdef WORDS_BIGENDIAN
92
if (table->s->db_low_byte_first)
103
int Field_short::store(int64_t nr, bool unsigned_val)
110
if (nr < 0L && !unsigned_val)
113
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
116
else if ((uint64_t) nr > (uint64_t) UINT16_MAX)
118
res=(int16_t) UINT16_MAX;
119
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
123
res=(int16_t) (uint16_t) nr;
127
if (nr < 0 && unsigned_val)
128
nr= UINT16_MAX+1; // Generate overflow
133
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
136
else if (nr > (int64_t) INT16_MAX)
139
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
145
#ifdef WORDS_BIGENDIAN
146
if (table->s->db_low_byte_first)
157
double Field_short::val_real(void)
160
#ifdef WORDS_BIGENDIAN
161
if (table->s->db_low_byte_first)
166
return unsigned_flag ? (double) (unsigned short) j : (double) j;
169
int64_t Field_short::val_int(void)
172
#ifdef WORDS_BIGENDIAN
173
if (table->s->db_low_byte_first)
178
return unsigned_flag ? (int64_t) (unsigned short) j : (int64_t) j;
182
String *Field_short::val_str(String *val_buffer,
183
String *val_ptr __attribute__((unused)))
185
const CHARSET_INFO * const cs= &my_charset_bin;
187
uint mlength=max(field_length+1,7*cs->mbmaxlen);
188
val_buffer->alloc(mlength);
189
char *to=(char*) val_buffer->ptr();
191
#ifdef WORDS_BIGENDIAN
192
if (table->s->db_low_byte_first)
199
length=(uint) cs->cset->long10_to_str(cs, to, mlength, 10,
200
(long) (uint16_t) j);
202
length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
203
val_buffer->length(length);
209
bool Field_short::send_binary(Protocol *protocol)
211
return protocol->store_short(Field_short::val_int());
215
int Field_short::cmp(const uchar *a_ptr, const uchar *b_ptr)
218
#ifdef WORDS_BIGENDIAN
219
if (table->s->db_low_byte_first)
232
return ((unsigned short) a < (unsigned short) b) ? -1 :
233
((unsigned short) a > (unsigned short) b) ? 1 : 0;
234
return (a < b) ? -1 : (a > b) ? 1 : 0;
237
void Field_short::sort_string(uchar *to,uint length __attribute__((unused)))
239
#ifdef WORDS_BIGENDIAN
240
if (!table->s->db_low_byte_first)
245
to[0] = (char) (ptr[0] ^ 128); /* Revers signbit */
254
to[0] = (char) (ptr[1] ^ 128); /* Revers signbit */
259
void Field_short::sql_type(String &res) const
261
const CHARSET_INFO * const cs= res.charset();
262
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "smallint"));