~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/int64_t.cc

  • Committer: Brian Aker
  • Date: 2008-10-12 01:59:02 UTC
  • Revision ID: brian@tangent.org-20081012015902-prhy6wsimdqr28om
Dead code around unsigned (first pass)

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
 
22
 
#include "config.h"
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"
28
 
 
29
 
#include <math.h>
30
 
 
31
 
#include <algorithm>
32
 
 
33
 
using namespace std;
34
 
 
35
 
namespace drizzled
36
 
{
37
 
 
38
 
namespace field
39
 
{
 
22
#include <drizzled/server_includes.h>
 
23
#include <drizzled/field/int64_t.h>
40
24
 
41
25
/****************************************************************************
42
 
  Field type Int64 int (8 bytes)
43
 
 ****************************************************************************/
 
26
 Field type int64_t int (8 bytes)
 
27
****************************************************************************/
44
28
 
45
 
int Int64::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
 
29
int Field_int64_t::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
46
30
{
47
31
  int error= 0;
48
32
  char *end;
49
33
  uint64_t tmp;
50
34
 
51
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
52
 
 
53
 
  tmp= cs->cset->strntoull10rnd(cs, from, len, false, &end,&error);
 
35
  tmp= cs->cset->strntoull10rnd(cs,from,len,unsigned_flag,&end,&error);
54
36
  if (error == MY_ERRNO_ERANGE)
55
37
  {
56
38
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
57
39
    error= 1;
58
40
  }
59
 
  else if (getTable()->in_use->count_cuted_fields &&
 
41
  else if (table->in_use->count_cuted_fields && 
60
42
           check_int(cs, from, len, end, error))
61
43
    error= 1;
62
44
  else
63
45
    error= 0;
64
46
#ifdef WORDS_BIGENDIAN
65
 
  if (getTable()->getShare()->db_low_byte_first)
 
47
  if (table->s->db_low_byte_first)
66
48
  {
67
49
    int8store(ptr,tmp);
68
50
  }
73
55
}
74
56
 
75
57
 
76
 
int Int64::store(double nr)
 
58
int Field_int64_t::store(double nr)
77
59
{
78
60
  int error= 0;
79
61
  int64_t res;
80
62
 
81
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
82
 
 
83
63
  nr= rint(nr);
84
 
 
85
 
  if (nr <= (double) INT64_MIN)
86
 
  {
87
 
    res= INT64_MIN;
88
 
    error= (nr < (double) INT64_MIN);
89
 
  }
90
 
  else if (nr >= (double) (uint64_t) INT64_MAX)
91
 
  {
92
 
    res= INT64_MAX;
93
 
    error= (nr > (double) INT64_MAX);
 
64
  if (unsigned_flag)
 
65
  {
 
66
    if (nr < 0)
 
67
    {
 
68
      res=0;
 
69
      error= 1;
 
70
    }
 
71
    else if (nr >= (double) UINT64_MAX)
 
72
    {
 
73
      res= ~(int64_t) 0;
 
74
      error= 1;
 
75
    }
 
76
    else
 
77
      res=(int64_t) (uint64_t) nr;
94
78
  }
95
79
  else
96
 
    res=(int64_t) nr;
97
 
 
 
80
  {
 
81
    if (nr <= (double) INT64_MIN)
 
82
    {
 
83
      res= INT64_MIN;
 
84
      error= (nr < (double) INT64_MIN);
 
85
    }
 
86
    else if (nr >= (double) (uint64_t) INT64_MAX)
 
87
    {
 
88
      res= INT64_MAX;
 
89
      error= (nr > (double) INT64_MAX);
 
90
    }
 
91
    else
 
92
      res=(int64_t) nr;
 
93
  }
98
94
  if (error)
99
95
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
100
96
 
101
97
#ifdef WORDS_BIGENDIAN
102
 
  if (getTable()->getShare()->db_low_byte_first)
 
98
  if (table->s->db_low_byte_first)
103
99
  {
104
100
    int8store(ptr,res);
105
101
  }
110
106
}
111
107
 
112
108
 
113
 
int Int64::store(int64_t nr, bool )
 
109
int Field_int64_t::store(int64_t nr, bool unsigned_val)
114
110
{
115
111
  int error= 0;
116
112
 
117
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
113
  if (nr < 0)                                   // Only possible error
 
114
  {
 
115
    /*
 
116
      if field is unsigned and value is signed (< 0) or
 
117
      if field is signed and value is unsigned we have an overflow
 
118
    */
 
119
    if (unsigned_flag != unsigned_val)
 
120
    {
 
121
      nr= unsigned_flag ? (uint64_t) 0 : (uint64_t) INT64_MAX;
 
122
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
123
      error= 1;
 
124
    }
 
125
  }
118
126
 
119
127
#ifdef WORDS_BIGENDIAN
120
 
  if (getTable()->getShare()->db_low_byte_first)
 
128
  if (table->s->db_low_byte_first)
121
129
  {
122
130
    int8store(ptr,nr);
123
131
  }
128
136
}
129
137
 
130
138
 
131
 
double Int64::val_real(void)
 
139
double Field_int64_t::val_real(void)
132
140
{
133
141
  int64_t j;
134
 
 
135
 
  ASSERT_COLUMN_MARKED_FOR_READ;
136
 
 
137
142
#ifdef WORDS_BIGENDIAN
138
 
  if (getTable()->getShare()->db_low_byte_first)
 
143
  if (table->s->db_low_byte_first)
139
144
  {
140
145
    j=sint8korr(ptr);
141
146
  }
143
148
#endif
144
149
    int64_tget(j,ptr);
145
150
  /* The following is open coded to avoid a bug in gcc 3.3 */
 
151
  if (unsigned_flag)
 
152
  {
 
153
    uint64_t tmp= (uint64_t) j;
 
154
    return uint64_t2double(tmp);
 
155
  }
146
156
  return (double) j;
147
157
}
148
158
 
149
159
 
150
 
int64_t Int64::val_int(void)
 
160
int64_t Field_int64_t::val_int(void)
151
161
{
152
162
  int64_t j;
153
 
 
154
 
  ASSERT_COLUMN_MARKED_FOR_READ;
155
 
 
156
163
#ifdef WORDS_BIGENDIAN
157
 
  if (getTable()->getShare()->db_low_byte_first)
 
164
  if (table->s->db_low_byte_first)
158
165
    j=sint8korr(ptr);
159
166
  else
160
167
#endif
163
170
}
164
171
 
165
172
 
166
 
String *Int64::val_str(String *val_buffer,
167
 
                       String *)
 
173
String *Field_int64_t::val_str(String *val_buffer,
 
174
                                String *val_ptr __attribute__((unused)))
168
175
{
169
176
  const CHARSET_INFO * const cs= &my_charset_bin;
170
177
  uint32_t length;
171
 
  uint32_t mlength= max(field_length+1,22*cs->mbmaxlen);
 
178
  uint32_t mlength=cmax(field_length+1,22*cs->mbmaxlen);
172
179
  val_buffer->alloc(mlength);
173
180
  char *to=(char*) val_buffer->ptr();
174
181
  int64_t j;
175
 
 
176
 
  ASSERT_COLUMN_MARKED_FOR_READ;
177
 
 
178
182
#ifdef WORDS_BIGENDIAN
179
 
  if (getTable()->getShare()->db_low_byte_first)
 
183
  if (table->s->db_low_byte_first)
180
184
    j=sint8korr(ptr);
181
185
  else
182
186
#endif
183
187
    int64_tget(j,ptr);
184
188
 
185
 
  length=(uint32_t) (cs->cset->int64_t10_to_str)(cs,to,mlength, -10, j);
 
189
  length=(uint) (cs->cset->int64_t10_to_str)(cs,to,mlength,
 
190
                                        unsigned_flag ? 10 : -10, j);
186
191
  val_buffer->length(length);
187
192
 
188
193
  return val_buffer;
189
194
}
190
195
 
191
 
int Int64::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
196
 
 
197
bool Field_int64_t::send_binary(Protocol *protocol)
 
198
{
 
199
  return protocol->store_int64_t(Field_int64_t::val_int(), unsigned_flag);
 
200
}
 
201
 
 
202
 
 
203
int Field_int64_t::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
192
204
{
193
205
  int64_t a,b;
194
206
#ifdef WORDS_BIGENDIAN
195
 
  if (getTable()->getShare()->db_low_byte_first)
 
207
  if (table->s->db_low_byte_first)
196
208
  {
197
209
    a=sint8korr(a_ptr);
198
210
    b=sint8korr(b_ptr);
203
215
    int64_tget(a,a_ptr);
204
216
    int64_tget(b,b_ptr);
205
217
  }
 
218
  if (unsigned_flag)
 
219
    return ((uint64_t) a < (uint64_t) b) ? -1 :
 
220
    ((uint64_t) a > (uint64_t) b) ? 1 : 0;
206
221
  return (a < b) ? -1 : (a > b) ? 1 : 0;
207
222
}
208
223
 
209
 
void Int64::sort_string(unsigned char *to,uint32_t )
 
224
void Field_int64_t::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
210
225
{
211
226
#ifdef WORDS_BIGENDIAN
212
 
  if (!getTable()->getShare()->db_low_byte_first)
 
227
  if (!table->s->db_low_byte_first)
213
228
  {
214
 
    to[0] = (char) (ptr[0] ^ 128);              /* Revers signbit */
 
229
    if (unsigned_flag)
 
230
      to[0] = ptr[0];
 
231
    else
 
232
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
215
233
    to[1]   = ptr[1];
216
234
    to[2]   = ptr[2];
217
235
    to[3]   = ptr[3];
223
241
  else
224
242
#endif
225
243
  {
226
 
    to[0] = (char) (ptr[7] ^ 128);              /* Revers signbit */
 
244
    if (unsigned_flag)
 
245
      to[0] = ptr[7];
 
246
    else
 
247
      to[0] = (char) (ptr[7] ^ 128);            /* Revers signbit */
227
248
    to[1]   = ptr[6];
228
249
    to[2]   = ptr[5];
229
250
    to[3]   = ptr[4];
235
256
}
236
257
 
237
258
 
238
 
void Int64::sql_type(String &res) const
 
259
void Field_int64_t::sql_type(String &res) const
239
260
{
240
261
  const CHARSET_INFO * const cs=res.charset();
241
262
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "bigint"));
242
263
}
243
 
 
244
 
 
245
 
unsigned char *Int64::pack(unsigned char* to, const unsigned char *from,
246
 
                           uint32_t,
247
 
#ifdef WORDS_BIGENDIAN
248
 
                           bool low_byte_first
249
 
#else
250
 
                           bool
251
 
#endif
252
 
                          )
253
 
{
254
 
  int64_t val;
255
 
#ifdef WORDS_BIGENDIAN
256
 
  if (getTable()->getShare()->db_low_byte_first)
257
 
    val = sint8korr(from);
258
 
  else
259
 
#endif
260
 
    int64_tget(val, from);
261
 
 
262
 
#ifdef WORDS_BIGENDIAN
263
 
  if (low_byte_first)
264
 
    int8store(to, val);
265
 
  else
266
 
#endif
267
 
    int64_tstore(to, val);
268
 
  return to + sizeof(val);
269
 
}
270
 
 
271
 
 
272
 
const unsigned char *Int64::unpack(unsigned char* to, const unsigned char *from, uint32_t,
273
 
#ifdef WORDS_BIGENDIAN
274
 
                                   bool low_byte_first
275
 
#else
276
 
                                   bool
277
 
#endif
278
 
                                  )
279
 
{
280
 
  int64_t val;
281
 
#ifdef WORDS_BIGENDIAN
282
 
  if (low_byte_first)
283
 
    val = sint8korr(from);
284
 
  else
285
 
#endif
286
 
    int64_tget(val, from);
287
 
 
288
 
#ifdef WORDS_BIGENDIAN
289
 
  if (getTable()->getShare()->db_low_byte_first)
290
 
    int8store(to, val);
291
 
  else
292
 
#endif
293
 
    int64_tstore(to, val);
294
 
  return from + sizeof(val);
295
 
}
296
 
 
297
 
} /* namespace field */
298
 
} /* namespace drizzled */