18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#include <drizzled/field/int64.h>
24
#include <drizzled/error.h>
25
#include <drizzled/table.h>
26
#include <drizzled/session.h>
27
#include "drizzled/internal/my_sys.h"
21
#ifdef USE_PRAGMA_IMPLEMENTATION
22
#pragma implementation // gcc: Class implementation
25
#include <drizzled/server_includes.h>
26
#include <drizzled/field/int64_t.h>
41
28
/****************************************************************************
42
Field type Int64 int (8 bytes)
43
****************************************************************************/
29
Field type int64_t int (8 bytes)
30
****************************************************************************/
45
int Int64::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
32
int Field_int64_t::store(const char *from,uint len, const CHARSET_INFO * const cs)
51
ASSERT_COLUMN_MARKED_FOR_WRITE;
53
tmp= cs->cset->strntoull10rnd(cs, from, len, false, &end,&error);
38
tmp= cs->cset->strntoull10rnd(cs,from,len,unsigned_flag,&end,&error);
54
39
if (error == MY_ERRNO_ERANGE)
56
41
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
59
else if (getTable()->in_use->count_cuted_fields &&
44
else if (table->in_use->count_cuted_fields &&
60
45
check_int(cs, from, len, end, error))
64
49
#ifdef WORDS_BIGENDIAN
65
if (getTable()->getShare()->db_low_byte_first)
50
if (table->s->db_low_byte_first)
67
52
int8store(ptr,tmp);
76
int Int64::store(double nr)
61
int Field_int64_t::store(double nr)
81
ASSERT_COLUMN_MARKED_FOR_WRITE;
85
if (nr <= (double) INT64_MIN)
88
error= (nr < (double) INT64_MIN);
90
else if (nr >= (double) (uint64_t) INT64_MAX)
93
error= (nr > (double) INT64_MAX);
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);
99
98
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
101
100
#ifdef WORDS_BIGENDIAN
102
if (getTable()->getShare()->db_low_byte_first)
101
if (table->s->db_low_byte_first)
104
103
int8store(ptr,res);
113
int Int64::store(int64_t nr, bool )
112
int Field_int64_t::store(int64_t nr, bool unsigned_val)
117
ASSERT_COLUMN_MARKED_FOR_WRITE;
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);
119
130
#ifdef WORDS_BIGENDIAN
120
if (getTable()->getShare()->db_low_byte_first)
131
if (table->s->db_low_byte_first)
122
133
int8store(ptr,nr);
131
double Int64::val_real(void)
142
double Field_int64_t::val_real(void)
135
ASSERT_COLUMN_MARKED_FOR_READ;
137
145
#ifdef WORDS_BIGENDIAN
138
if (getTable()->getShare()->db_low_byte_first)
146
if (table->s->db_low_byte_first)
140
148
j=sint8korr(ptr);
144
152
int64_tget(j,ptr);
145
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);
146
159
return (double) j;
150
int64_t Int64::val_int(void)
163
int64_t Field_int64_t::val_int(void)
154
ASSERT_COLUMN_MARKED_FOR_READ;
156
166
#ifdef WORDS_BIGENDIAN
157
if (getTable()->getShare()->db_low_byte_first)
167
if (table->s->db_low_byte_first)
158
168
j=sint8korr(ptr);
166
String *Int64::val_str(String *val_buffer,
176
String *Field_int64_t::val_str(String *val_buffer,
177
String *val_ptr __attribute__((unused)))
169
179
const CHARSET_INFO * const cs= &my_charset_bin;
171
uint32_t mlength= max(field_length+1,22*cs->mbmaxlen);
181
uint mlength=max(field_length+1,22*cs->mbmaxlen);
172
182
val_buffer->alloc(mlength);
173
183
char *to=(char*) val_buffer->ptr();
176
ASSERT_COLUMN_MARKED_FOR_READ;
178
185
#ifdef WORDS_BIGENDIAN
179
if (getTable()->getShare()->db_low_byte_first)
186
if (table->s->db_low_byte_first)
180
187
j=sint8korr(ptr);
183
190
int64_tget(j,ptr);
185
length=(uint32_t) (cs->cset->int64_t10_to_str)(cs,to,mlength, -10, j);
192
length=(uint) (cs->cset->int64_t10_to_str)(cs,to,mlength,
193
unsigned_flag ? 10 : -10, j);
186
194
val_buffer->length(length);
188
196
return val_buffer;
191
int Int64::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
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 uchar *a_ptr, const uchar *b_ptr)
194
209
#ifdef WORDS_BIGENDIAN
195
if (getTable()->getShare()->db_low_byte_first)
210
if (table->s->db_low_byte_first)
197
212
a=sint8korr(a_ptr);
198
213
b=sint8korr(b_ptr);
203
218
int64_tget(a,a_ptr);
204
219
int64_tget(b,b_ptr);
222
return ((uint64_t) a < (uint64_t) b) ? -1 :
223
((uint64_t) a > (uint64_t) b) ? 1 : 0;
206
224
return (a < b) ? -1 : (a > b) ? 1 : 0;
209
void Int64::sort_string(unsigned char *to,uint32_t )
227
void Field_int64_t::sort_string(uchar *to,uint length __attribute__((unused)))
211
229
#ifdef WORDS_BIGENDIAN
212
if (!getTable()->getShare()->db_low_byte_first)
230
if (!table->s->db_low_byte_first)
214
to[0] = (char) (ptr[0] ^ 128); /* Revers signbit */
235
to[0] = (char) (ptr[0] ^ 128); /* Revers signbit */
238
void Int64::sql_type(String &res) const
262
void Field_int64_t::sql_type(String &res) const
240
264
const CHARSET_INFO * const cs=res.charset();
241
265
res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "bigint"));
245
unsigned char *Int64::pack(unsigned char* to, const unsigned char *from,
247
#ifdef WORDS_BIGENDIAN
255
#ifdef WORDS_BIGENDIAN
256
if (getTable()->getShare()->db_low_byte_first)
257
val = sint8korr(from);
260
int64_tget(val, from);
262
#ifdef WORDS_BIGENDIAN
267
int64_tstore(to, val);
268
return to + sizeof(val);
272
const unsigned char *Int64::unpack(unsigned char* to, const unsigned char *from, uint32_t,
273
#ifdef WORDS_BIGENDIAN
281
#ifdef WORDS_BIGENDIAN
283
val = sint8korr(from);
286
int64_tget(val, from);
288
#ifdef WORDS_BIGENDIAN
289
if (getTable()->getShare()->db_low_byte_first)
293
int64_tstore(to, val);
294
return from + sizeof(val);
297
} /* namespace field */
298
} /* namespace drizzled */
271
Floating-point numbers
275
Field_real::pack(uchar *to, const uchar *from,
276
uint 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 uchar *dptr= from + pack_length();
283
while (dptr-- > from)
289
return(Field::pack(to, from, max_length, low_byte_first));
293
Field_real::unpack(uchar *to, const uchar *from,
294
uint param_data, bool low_byte_first)
296
#ifdef WORDS_BIGENDIAN
297
if (low_byte_first != table->s->db_low_byte_first)
299
const uchar *dptr= from + pack_length();
300
while (dptr-- > from)
302
return(from + pack_length());
306
return(Field::unpack(to, from, param_data, low_byte_first));