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
27
/****************************************************************************
29
****************************************************************************/
31
int Field_long::store(const char *from,uint len,CHARSET_INFO *cs)
37
error= get_int(cs, from, len, &rnd, UINT32_MAX, INT32_MIN, INT32_MAX);
38
store_tmp= unsigned_flag ? (long) (uint64_t) rnd : (long) rnd;
39
#ifdef WORDS_BIGENDIAN
40
if (table->s->db_low_byte_first)
42
int4store(ptr, store_tmp);
46
longstore(ptr, store_tmp);
51
int Field_long::store(double nr)
63
else if (nr > (double) UINT32_MAX)
66
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
70
res=(int32) (ulong) nr;
74
if (nr < (double) INT32_MIN)
76
res=(int32) INT32_MIN;
79
else if (nr > (double) INT32_MAX)
81
res=(int32) INT32_MAX;
85
res=(int32) (int64_t) nr;
88
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
90
#ifdef WORDS_BIGENDIAN
91
if (table->s->db_low_byte_first)
102
int Field_long::store(int64_t nr, bool unsigned_val)
109
if (nr < 0 && !unsigned_val)
114
else if ((uint64_t) nr >= (1LL << 32))
116
res=(int32) (uint32) ~0L;
120
res=(int32) (uint32) nr;
124
if (nr < 0 && unsigned_val)
125
nr= ((int64_t) INT32_MAX) + 1; // Generate overflow
126
if (nr < (int64_t) INT32_MIN)
128
res=(int32) INT32_MIN;
131
else if (nr > (int64_t) INT32_MAX)
133
res=(int32) INT32_MAX;
140
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
142
#ifdef WORDS_BIGENDIAN
143
if (table->s->db_low_byte_first)
154
double Field_long::val_real(void)
157
#ifdef WORDS_BIGENDIAN
158
if (table->s->db_low_byte_first)
163
return unsigned_flag ? (double) (uint32) j : (double) j;
166
int64_t Field_long::val_int(void)
169
/* See the comment in Field_long::store(long long) */
170
assert(table->in_use == current_thd);
171
#ifdef WORDS_BIGENDIAN
172
if (table->s->db_low_byte_first)
177
return unsigned_flag ? (int64_t) (uint32) j : (int64_t) j;
180
String *Field_long::val_str(String *val_buffer,
181
String *val_ptr __attribute__((unused)))
183
CHARSET_INFO *cs= &my_charset_bin;
185
uint mlength=max(field_length+1,12*cs->mbmaxlen);
186
val_buffer->alloc(mlength);
187
char *to=(char*) val_buffer->ptr();
189
#ifdef WORDS_BIGENDIAN
190
if (table->s->db_low_byte_first)
197
length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32)j);
199
length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
200
val_buffer->length(length);
202
prepend_zeros(val_buffer);
207
bool Field_long::send_binary(Protocol *protocol)
209
return protocol->store_long(Field_long::val_int());
212
int Field_long::cmp(const uchar *a_ptr, const uchar *b_ptr)
215
#ifdef WORDS_BIGENDIAN
216
if (table->s->db_low_byte_first)
228
return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
229
return (a < b) ? -1 : (a > b) ? 1 : 0;
232
void Field_long::sort_string(uchar *to,uint length __attribute__((unused)))
234
#ifdef WORDS_BIGENDIAN
235
if (!table->s->db_low_byte_first)
240
to[0] = (char) (ptr[0] ^ 128); /* Revers signbit */
251
to[0] = (char) (ptr[3] ^ 128); /* Revers signbit */
259
void Field_long::sql_type(String &res) const
261
CHARSET_INFO *cs=res.charset();
262
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
263
"int(%d)",(int) field_length));
264
add_zerofill_and_unsigned(res);