~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/long.cc

pandora-build v0.100 - Fixes several bugs found by cb1kenobi. Add several thoughts from folks at LCA.

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
 
#include <drizzled/server_includes.h>
 
22
#include "config.h"
26
23
#include <drizzled/field/long.h>
 
24
#include <drizzled/error.h>
 
25
#include <drizzled/table.h>
 
26
#include <drizzled/session.h>
 
27
 
 
28
#include <math.h>
 
29
 
 
30
#include <algorithm>
 
31
 
 
32
using namespace std;
 
33
 
27
34
 
28
35
/****************************************************************************
29
36
** long int
30
37
****************************************************************************/
31
38
 
32
 
int Field_long::store(const char *from,uint len, const CHARSET_INFO * const cs)
 
39
int Field_long::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
33
40
{
34
41
  long store_tmp;
35
42
  int error;
36
43
  int64_t rnd;
37
 
  
 
44
 
 
45
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
46
 
38
47
  error= get_int(cs, from, len, &rnd, UINT32_MAX, INT32_MIN, INT32_MAX);
39
 
  store_tmp= unsigned_flag ? (long) (uint64_t) rnd : (long) rnd;
 
48
  store_tmp= (long) rnd;
40
49
#ifdef WORDS_BIGENDIAN
41
50
  if (table->s->db_low_byte_first)
42
51
  {
54
63
  int error= 0;
55
64
  int32_t res;
56
65
  nr=rint(nr);
57
 
  if (unsigned_flag)
58
 
  {
59
 
    if (nr < 0)
60
 
    {
61
 
      res=0;
62
 
      error= 1;
63
 
    }
64
 
    else if (nr > (double) UINT32_MAX)
65
 
    {
66
 
      res= INT32_MAX;
67
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
68
 
      error= 1;
69
 
    }
70
 
    else
71
 
      res=(int32_t) (uint32_t) nr;
 
66
 
 
67
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
68
 
 
69
  if (nr < (double) INT32_MIN)
 
70
  {
 
71
    res=(int32_t) INT32_MIN;
 
72
    error= 1;
 
73
  }
 
74
  else if (nr > (double) INT32_MAX)
 
75
  {
 
76
    res=(int32_t) INT32_MAX;
 
77
    error= 1;
72
78
  }
73
79
  else
74
 
  {
75
 
    if (nr < (double) INT32_MIN)
76
 
    {
77
 
      res=(int32_t) INT32_MIN;
78
 
      error= 1;
79
 
    }
80
 
    else if (nr > (double) INT32_MAX)
81
 
    {
82
 
      res=(int32_t) INT32_MAX;
83
 
      error= 1;
84
 
    }
85
 
    else
86
 
      res=(int32_t) (int64_t) nr;
87
 
  }
 
80
    res=(int32_t) (int64_t) nr;
 
81
 
88
82
  if (error)
89
83
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
90
84
 
105
99
  int error= 0;
106
100
  int32_t res;
107
101
 
108
 
  if (unsigned_flag)
109
 
  {
110
 
    if (nr < 0 && !unsigned_val)
111
 
    {
112
 
      res=0;
113
 
      error= 1;
114
 
    }
115
 
    else if ((uint64_t) nr >= (1LL << 32))
116
 
    {
117
 
      res=(int32_t) (uint32_t) ~0L;
118
 
      error= 1;
119
 
    }
120
 
    else
121
 
      res=(int32_t) (uint32_t) nr;
 
102
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
103
 
 
104
  if (nr < 0 && unsigned_val)
 
105
    nr= ((int64_t) INT32_MAX) + 1;           // Generate overflow
 
106
  if (nr < (int64_t) INT32_MIN)
 
107
  {
 
108
    res=(int32_t) INT32_MIN;
 
109
    error= 1;
 
110
  }
 
111
  else if (nr > (int64_t) INT32_MAX)
 
112
  {
 
113
    res=(int32_t) INT32_MAX;
 
114
    error= 1;
122
115
  }
123
116
  else
124
 
  {
125
 
    if (nr < 0 && unsigned_val)
126
 
      nr= ((int64_t) INT32_MAX) + 1;           // Generate overflow
127
 
    if (nr < (int64_t) INT32_MIN) 
128
 
    {
129
 
      res=(int32_t) INT32_MIN;
130
 
      error= 1;
131
 
    }
132
 
    else if (nr > (int64_t) INT32_MAX)
133
 
    {
134
 
      res=(int32_t) INT32_MAX;
135
 
      error= 1;
136
 
    }
137
 
    else
138
 
      res=(int32_t) nr;
139
 
  }
 
117
    res=(int32_t) nr;
 
118
 
140
119
  if (error)
141
120
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
142
121
 
155
134
double Field_long::val_real(void)
156
135
{
157
136
  int32_t j;
 
137
 
 
138
  ASSERT_COLUMN_MARKED_FOR_READ;
 
139
 
158
140
#ifdef WORDS_BIGENDIAN
159
141
  if (table->s->db_low_byte_first)
160
142
    j=sint4korr(ptr);
161
143
  else
162
144
#endif
163
145
    longget(j,ptr);
164
 
  return unsigned_flag ? (double) (uint32_t) j : (double) j;
 
146
  return (double) j;
165
147
}
166
148
 
167
149
int64_t Field_long::val_int(void)
168
150
{
169
151
  int32_t j;
170
 
  /* See the comment in Field_long::store(long long) */
171
 
  assert(table->in_use == current_thd);
 
152
 
 
153
  ASSERT_COLUMN_MARKED_FOR_READ;
 
154
 
 
155
  /* See the comment in Field_long::store(int64_t) */
 
156
  assert(table->in_use == current_session);
172
157
#ifdef WORDS_BIGENDIAN
173
158
  if (table->s->db_low_byte_first)
174
159
    j=sint4korr(ptr);
175
160
  else
176
161
#endif
177
162
    longget(j,ptr);
178
 
  return unsigned_flag ? (int64_t) (uint32_t) j : (int64_t) j;
 
163
  return (int64_t) j;
179
164
}
180
165
 
181
166
String *Field_long::val_str(String *val_buffer,
182
 
                            String *val_ptr __attribute__((unused)))
 
167
                            String *)
183
168
{
184
169
  const CHARSET_INFO * const cs= &my_charset_bin;
185
 
  uint length;
186
 
  uint mlength=max(field_length+1,12*cs->mbmaxlen);
 
170
  uint32_t length;
 
171
  uint32_t mlength= max(field_length+1,12*cs->mbmaxlen);
187
172
  val_buffer->alloc(mlength);
188
173
  char *to=(char*) val_buffer->ptr();
189
174
  int32_t j;
 
175
 
 
176
  ASSERT_COLUMN_MARKED_FOR_READ;
 
177
 
190
178
#ifdef WORDS_BIGENDIAN
191
179
  if (table->s->db_low_byte_first)
192
180
    j=sint4korr(ptr);
194
182
#endif
195
183
    longget(j,ptr);
196
184
 
197
 
  if (unsigned_flag)
198
 
    length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32_t)j);
199
 
  else
200
 
    length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
 
185
  length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
201
186
  val_buffer->length(length);
202
187
 
203
188
  return val_buffer;
204
189
}
205
190
 
206
 
 
207
 
bool Field_long::send_binary(Protocol *protocol)
208
 
{
209
 
  return protocol->store_long(Field_long::val_int());
210
 
}
211
 
 
212
 
int Field_long::cmp(const uchar *a_ptr, const uchar *b_ptr)
 
191
int Field_long::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
213
192
{
214
193
  int32_t a,b;
215
194
#ifdef WORDS_BIGENDIAN
224
203
    longget(a,a_ptr);
225
204
    longget(b,b_ptr);
226
205
  }
227
 
  if (unsigned_flag)
228
 
    return ((uint32_t) a < (uint32_t) b) ? -1 : ((uint32_t) a > (uint32_t) b) ? 1 : 0;
 
206
 
229
207
  return (a < b) ? -1 : (a > b) ? 1 : 0;
230
208
}
231
209
 
232
 
void Field_long::sort_string(uchar *to,uint length __attribute__((unused)))
 
210
void Field_long::sort_string(unsigned char *to,uint32_t )
233
211
{
234
212
#ifdef WORDS_BIGENDIAN
235
213
  if (!table->s->db_low_byte_first)
236
214
  {
237
 
    if (unsigned_flag)
238
 
      to[0] = ptr[0];
239
 
    else
240
 
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
 
215
    to[0] = (char) (ptr[0] ^ 128);              /* Revers signbit */
241
216
    to[1]   = ptr[1];
242
217
    to[2]   = ptr[2];
243
218
    to[3]   = ptr[3];
245
220
  else
246
221
#endif
247
222
  {
248
 
    if (unsigned_flag)
249
 
      to[0] = ptr[3];
250
 
    else
251
 
      to[0] = (char) (ptr[3] ^ 128);            /* Revers signbit */
 
223
    to[0] = (char) (ptr[3] ^ 128);              /* Revers signbit */
252
224
    to[1]   = ptr[2];
253
225
    to[2]   = ptr[1];
254
226
    to[3]   = ptr[0];
260
232
{
261
233
  const CHARSET_INFO * const cs=res.charset();
262
234
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "int"));
263
 
  add_unsigned(res);
 
235
}
 
236
 
 
237
unsigned char *Field_long::pack(unsigned char* to, const unsigned char *from,
 
238
                                         uint32_t,
 
239
#ifdef WORDS_BIGENDIAN
 
240
                                         bool low_byte_first
 
241
#else
 
242
                                         bool
 
243
#endif
 
244
)
 
245
{
 
246
  int32_t val;
 
247
#ifdef WORDS_BIGENDIAN
 
248
  if (table->s->db_low_byte_first)
 
249
    val = sint4korr(from);
 
250
  else
 
251
#endif
 
252
    longget(val, from);
 
253
 
 
254
#ifdef WORDS_BIGENDIAN
 
255
  if (low_byte_first)
 
256
    int4store(to, val);
 
257
  else
 
258
#endif
 
259
    longstore(to, val);
 
260
  return to + sizeof(val);
 
261
}
 
262
 
 
263
 
 
264
const unsigned char *Field_long::unpack(unsigned char* to, const unsigned char *from, uint32_t,
 
265
#ifdef WORDS_BIGENDIAN
 
266
                                           bool low_byte_first
 
267
#else
 
268
                                           bool
 
269
#endif
 
270
)
 
271
{
 
272
  int32_t val;
 
273
#ifdef WORDS_BIGENDIAN
 
274
  if (low_byte_first)
 
275
    val = sint4korr(from);
 
276
  else
 
277
#endif
 
278
    longget(val, from);
 
279
 
 
280
#ifdef WORDS_BIGENDIAN
 
281
  if (table->s->db_low_byte_first)
 
282
    int4store(to, val);
 
283
  else
 
284
#endif
 
285
    longstore(to, val);
 
286
  return from + sizeof(val);
264
287
}
265
288