~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/long.cc

Merge of Jay

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