~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/long.cc

  • Committer: Monty Taylor
  • Date: 2009-03-06 03:33:24 UTC
  • mfrom: (916.1.2 merge)
  • Revision ID: mordred@inaugust.com-20090306033324-dcedf80g9qzywbvu
Merged Brian's merge... re-rotate the tree.

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