~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/int64_t.cc

  • Committer: Monty Taylor
  • Date: 2008-10-05 01:41:06 UTC
  • Revision ID: monty@inaugust.com-20081005014106-bulqe4kp7i6ipts1
Moved qsort declarations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
21
24
 
22
25
#include <drizzled/server_includes.h>
23
26
#include <drizzled/field/int64_t.h>
26
29
 Field type int64_t int (8 bytes)
27
30
****************************************************************************/
28
31
 
29
 
int Field_int64_t::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)
30
33
{
31
34
  int error= 0;
32
35
  char *end;
33
36
  uint64_t tmp;
34
37
 
35
 
  tmp= cs->cset->strntoull10rnd(cs, from, len, false, &end,&error);
 
38
  tmp= cs->cset->strntoull10rnd(cs,from,len,unsigned_flag,&end,&error);
36
39
  if (error == MY_ERRNO_ERANGE)
37
40
  {
38
41
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
61
64
  int64_t res;
62
65
 
63
66
  nr= rint(nr);
64
 
 
65
 
  if (nr <= (double) INT64_MIN)
66
 
  {
67
 
    res= INT64_MIN;
68
 
    error= (nr < (double) INT64_MIN);
69
 
  }
70
 
  else if (nr >= (double) (uint64_t) INT64_MAX)
71
 
  {
72
 
    res= INT64_MAX;
73
 
    error= (nr > (double) INT64_MAX);
 
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;
74
81
  }
75
82
  else
76
 
    res=(int64_t) nr;
77
 
 
 
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
  }
78
97
  if (error)
79
98
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
80
99
 
90
109
}
91
110
 
92
111
 
93
 
int Field_int64_t::store(int64_t nr, bool unsigned_val __attribute__((unused)))
 
112
int Field_int64_t::store(int64_t nr, bool unsigned_val)
94
113
{
95
114
  int error= 0;
96
115
 
 
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
 
97
130
#ifdef WORDS_BIGENDIAN
98
131
  if (table->s->db_low_byte_first)
99
132
  {
118
151
#endif
119
152
    int64_tget(j,ptr);
120
153
  /* 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
  }
121
159
  return (double) j;
122
160
}
123
161
 
139
177
                                String *val_ptr __attribute__((unused)))
140
178
{
141
179
  const CHARSET_INFO * const cs= &my_charset_bin;
142
 
  uint32_t length;
143
 
  uint32_t mlength=cmax(field_length+1,22*cs->mbmaxlen);
 
180
  uint length;
 
181
  uint mlength=cmax(field_length+1,22*cs->mbmaxlen);
144
182
  val_buffer->alloc(mlength);
145
183
  char *to=(char*) val_buffer->ptr();
146
184
  int64_t j;
151
189
#endif
152
190
    int64_tget(j,ptr);
153
191
 
154
 
  length=(uint) (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);
155
194
  val_buffer->length(length);
156
195
 
157
196
  return val_buffer;
160
199
 
161
200
bool Field_int64_t::send_binary(Protocol *protocol)
162
201
{
163
 
  return protocol->store_int64_t(Field_int64_t::val_int(), false);
 
202
  return protocol->store_int64_t(Field_int64_t::val_int(), unsigned_flag);
164
203
}
165
204
 
166
205
 
167
 
int Field_int64_t::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
206
int Field_int64_t::cmp(const uchar *a_ptr, const uchar *b_ptr)
168
207
{
169
208
  int64_t a,b;
170
209
#ifdef WORDS_BIGENDIAN
179
218
    int64_tget(a,a_ptr);
180
219
    int64_tget(b,b_ptr);
181
220
  }
 
221
  if (unsigned_flag)
 
222
    return ((uint64_t) a < (uint64_t) b) ? -1 :
 
223
    ((uint64_t) a > (uint64_t) b) ? 1 : 0;
182
224
  return (a < b) ? -1 : (a > b) ? 1 : 0;
183
225
}
184
226
 
185
 
void Field_int64_t::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
 
227
void Field_int64_t::sort_string(uchar *to,uint length __attribute__((unused)))
186
228
{
187
229
#ifdef WORDS_BIGENDIAN
188
230
  if (!table->s->db_low_byte_first)
189
231
  {
190
 
    to[0] = (char) (ptr[0] ^ 128);              /* Revers signbit */
 
232
    if (unsigned_flag)
 
233
      to[0] = ptr[0];
 
234
    else
 
235
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
191
236
    to[1]   = ptr[1];
192
237
    to[2]   = ptr[2];
193
238
    to[3]   = ptr[3];
199
244
  else
200
245
#endif
201
246
  {
202
 
    to[0] = (char) (ptr[7] ^ 128);              /* Revers signbit */
 
247
    if (unsigned_flag)
 
248
      to[0] = ptr[7];
 
249
    else
 
250
      to[0] = (char) (ptr[7] ^ 128);            /* Revers signbit */
203
251
    to[1]   = ptr[6];
204
252
    to[2]   = ptr[5];
205
253
    to[3]   = ptr[4];
215
263
{
216
264
  const CHARSET_INFO * const cs=res.charset();
217
265
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "bigint"));
218
 
}
 
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