~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/int64_t.cc

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
1
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
4
 *  Copyright (C) 2008 MySQL
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#ifdef USE_PRAGMA_IMPLEMENTATION
22
 
#pragma implementation                          // gcc: Class implementation
23
 
#endif
24
21
 
25
22
#include <drizzled/server_includes.h>
26
23
#include <drizzled/field/int64_t.h>
 
24
#include <drizzled/error.h>
 
25
#include CMATH_H
 
26
 
 
27
#if defined(CMATH_NAMESPACE)
 
28
using namespace CMATH_NAMESPACE;
 
29
#endif
27
30
 
28
31
/****************************************************************************
29
32
 Field type int64_t int (8 bytes)
30
33
****************************************************************************/
31
34
 
32
 
int Field_int64_t::store(const char *from,uint len, const CHARSET_INFO * const cs)
 
35
int Field_int64_t::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
33
36
{
34
37
  int error= 0;
35
38
  char *end;
36
39
  uint64_t tmp;
37
40
 
38
 
  tmp= cs->cset->strntoull10rnd(cs,from,len,unsigned_flag,&end,&error);
 
41
  tmp= cs->cset->strntoull10rnd(cs, from, len, false, &end,&error);
39
42
  if (error == MY_ERRNO_ERANGE)
40
43
  {
41
44
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
64
67
  int64_t res;
65
68
 
66
69
  nr= rint(nr);
67
 
  if (unsigned_flag)
68
 
  {
69
 
    if (nr < 0)
70
 
    {
71
 
      res=0;
72
 
      error= 1;
73
 
    }
74
 
    else if (nr >= (double) UINT64_MAX)
75
 
    {
76
 
      res= ~(int64_t) 0;
77
 
      error= 1;
78
 
    }
79
 
    else
80
 
      res=(int64_t) (uint64_t) nr;
 
70
 
 
71
  if (nr <= (double) INT64_MIN)
 
72
  {
 
73
    res= INT64_MIN;
 
74
    error= (nr < (double) INT64_MIN);
 
75
  }
 
76
  else if (nr >= (double) (uint64_t) INT64_MAX)
 
77
  {
 
78
    res= INT64_MAX;
 
79
    error= (nr > (double) INT64_MAX);
81
80
  }
82
81
  else
83
 
  {
84
 
    if (nr <= (double) INT64_MIN)
85
 
    {
86
 
      res= INT64_MIN;
87
 
      error= (nr < (double) INT64_MIN);
88
 
    }
89
 
    else if (nr >= (double) (uint64_t) INT64_MAX)
90
 
    {
91
 
      res= INT64_MAX;
92
 
      error= (nr > (double) INT64_MAX);
93
 
    }
94
 
    else
95
 
      res=(int64_t) nr;
96
 
  }
 
82
    res=(int64_t) nr;
 
83
 
97
84
  if (error)
98
85
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
99
86
 
109
96
}
110
97
 
111
98
 
112
 
int Field_int64_t::store(int64_t nr, bool unsigned_val)
 
99
int Field_int64_t::store(int64_t nr, bool unsigned_val __attribute__((unused)))
113
100
{
114
101
  int error= 0;
115
102
 
116
 
  if (nr < 0)                                   // Only possible error
117
 
  {
118
 
    /*
119
 
      if field is unsigned and value is signed (< 0) or
120
 
      if field is signed and value is unsigned we have an overflow
121
 
    */
122
 
    if (unsigned_flag != unsigned_val)
123
 
    {
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);
126
 
      error= 1;
127
 
    }
128
 
  }
129
 
 
130
103
#ifdef WORDS_BIGENDIAN
131
104
  if (table->s->db_low_byte_first)
132
105
  {
151
124
#endif
152
125
    int64_tget(j,ptr);
153
126
  /* The following is open coded to avoid a bug in gcc 3.3 */
154
 
  if (unsigned_flag)
155
 
  {
156
 
    uint64_t tmp= (uint64_t) j;
157
 
    return uint64_t2double(tmp);
158
 
  }
159
127
  return (double) j;
160
128
}
161
129
 
177
145
                                String *val_ptr __attribute__((unused)))
178
146
{
179
147
  const CHARSET_INFO * const cs= &my_charset_bin;
180
 
  uint length;
181
 
  uint mlength=max(field_length+1,22*cs->mbmaxlen);
 
148
  uint32_t length;
 
149
  uint32_t mlength=cmax(field_length+1,22*cs->mbmaxlen);
182
150
  val_buffer->alloc(mlength);
183
151
  char *to=(char*) val_buffer->ptr();
184
152
  int64_t j;
189
157
#endif
190
158
    int64_tget(j,ptr);
191
159
 
192
 
  length=(uint) (cs->cset->int64_t10_to_str)(cs,to,mlength,
193
 
                                        unsigned_flag ? 10 : -10, j);
 
160
  length=(uint) (cs->cset->int64_t10_to_str)(cs,to,mlength, -10, j);
194
161
  val_buffer->length(length);
195
162
 
196
163
  return val_buffer;
199
166
 
200
167
bool Field_int64_t::send_binary(Protocol *protocol)
201
168
{
202
 
  return protocol->store_int64_t(Field_int64_t::val_int(), unsigned_flag);
 
169
  return protocol->store_int64_t(Field_int64_t::val_int(), false);
203
170
}
204
171
 
205
172
 
206
 
int Field_int64_t::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
173
int Field_int64_t::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
207
174
{
208
175
  int64_t a,b;
209
176
#ifdef WORDS_BIGENDIAN
218
185
    int64_tget(a,a_ptr);
219
186
    int64_tget(b,b_ptr);
220
187
  }
221
 
  if (unsigned_flag)
222
 
    return ((uint64_t) a < (uint64_t) b) ? -1 :
223
 
    ((uint64_t) a > (uint64_t) b) ? 1 : 0;
224
188
  return (a < b) ? -1 : (a > b) ? 1 : 0;
225
189
}
226
190
 
227
 
void Field_int64_t::sort_string(uchar *to,uint length __attribute__((unused)))
 
191
void Field_int64_t::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
228
192
{
229
193
#ifdef WORDS_BIGENDIAN
230
194
  if (!table->s->db_low_byte_first)
231
195
  {
232
 
    if (unsigned_flag)
233
 
      to[0] = ptr[0];
234
 
    else
235
 
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
 
196
    to[0] = (char) (ptr[0] ^ 128);              /* Revers signbit */
236
197
    to[1]   = ptr[1];
237
198
    to[2]   = ptr[2];
238
199
    to[3]   = ptr[3];
244
205
  else
245
206
#endif
246
207
  {
247
 
    if (unsigned_flag)
248
 
      to[0] = ptr[7];
249
 
    else
250
 
      to[0] = (char) (ptr[7] ^ 128);            /* Revers signbit */
 
208
    to[0] = (char) (ptr[7] ^ 128);              /* Revers signbit */
251
209
    to[1]   = ptr[6];
252
210
    to[2]   = ptr[5];
253
211
    to[3]   = ptr[4];
263
221
{
264
222
  const CHARSET_INFO * const cs=res.charset();
265
223
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "bigint"));
266
 
  add_unsigned(res);
267
 
}
268
 
 
269
 
 
270
 
/*
271
 
  Floating-point numbers
272
 
 */
273
 
 
274
 
uchar *
275
 
Field_real::pack(uchar *to, const uchar *from,
276
 
                 uint max_length, bool low_byte_first)
277
 
{
278
 
  assert(max_length >= pack_length());
279
 
#ifdef WORDS_BIGENDIAN
280
 
  if (low_byte_first != table->s->db_low_byte_first)
281
 
  {
282
 
    const uchar *dptr= from + pack_length();
283
 
    while (dptr-- > from)
284
 
      *to++ = *dptr;
285
 
    return(to);
286
 
  }
287
 
  else
288
 
#endif
289
 
    return(Field::pack(to, from, max_length, low_byte_first));
290
 
}
291
 
 
292
 
const uchar *
293
 
Field_real::unpack(uchar *to, const uchar *from,
294
 
                   uint param_data, bool low_byte_first)
295
 
{
296
 
#ifdef WORDS_BIGENDIAN
297
 
  if (low_byte_first != table->s->db_low_byte_first)
298
 
  {
299
 
    const uchar *dptr= from + pack_length();
300
 
    while (dptr-- > from)
301
 
      *to++ = *dptr;
302
 
    return(from + pack_length());
303
 
  }
304
 
  else
305
 
#endif
306
 
    return(Field::unpack(to, from, param_data, low_byte_first));
307
 
}
308
 
 
 
224
}