~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/long.cc

  • Committer: Brian Aker
  • Date: 2009-10-15 00:22:33 UTC
  • mto: (1183.1.11 merge)
  • mto: This revision was merged to the branch mainline in revision 1198.
  • Revision ID: brian@gaz-20091015002233-fa4ao2mbc67wls91
First pass of information engine. OMG, ponies... is it so much easier to
deal with creating and engine.

The list table iterator though... its ass, needs to go. We should also
abstract out share. Very few engines need a custom one. Just say'in

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
27
 
28
 
#include <math.h>
29
 
 
30
28
#include <algorithm>
31
29
 
32
30
using namespace std;
33
31
 
34
 
namespace drizzled
35
 
{
36
 
 
37
 
namespace field
38
 
{
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
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
44
 
 
45
  error= get_int(cs, from, len, &rnd, UINT32_MAX, INT32_MIN, INT32_MAX);
 
46
  store_tmp= (long) rnd;
 
47
#ifdef WORDS_BIGENDIAN
 
48
  if (table->s->db_low_byte_first)
 
49
  {
 
50
    int4store(ptr, store_tmp);
 
51
  }
 
52
  else
 
53
#endif
 
54
    longstore(ptr, store_tmp);
 
55
  return error;
 
56
}
 
57
 
 
58
 
 
59
int Field_long::store(double nr)
 
60
{
 
61
  int error= 0;
 
62
  int32_t res;
 
63
  nr=rint(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;
 
76
  }
 
77
  else
 
78
    res=(int32_t) (int64_t) nr;
 
79
 
 
80
  if (error)
 
81
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
82
 
 
83
#ifdef WORDS_BIGENDIAN
 
84
  if (table->s->db_low_byte_first)
 
85
  {
 
86
    int4store(ptr,res);
 
87
  }
 
88
  else
 
89
#endif
 
90
    longstore(ptr,res);
 
91
  return error;
 
92
}
 
93
 
 
94
 
 
95
int Field_long::store(int64_t nr, bool unsigned_val)
 
96
{
 
97
  int error= 0;
 
98
  int32_t res;
 
99
 
 
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;
 
113
  }
 
114
  else
 
115
    res=(int32_t) nr;
 
116
 
 
117
  if (error)
 
118
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
119
 
 
120
#ifdef WORDS_BIGENDIAN
 
121
  if (table->s->db_low_byte_first)
 
122
  {
 
123
    int4store(ptr,res);
 
124
  }
 
125
  else
 
126
#endif
 
127
    longstore(ptr,res);
 
128
  return error;
 
129
}
 
130
 
 
131
 
 
132
double Field_long::val_real(void)
 
133
{
 
134
  int32_t j;
 
135
 
 
136
  ASSERT_COLUMN_MARKED_FOR_READ;
 
137
 
 
138
#ifdef WORDS_BIGENDIAN
 
139
  if (table->s->db_low_byte_first)
 
140
    j=sint4korr(ptr);
 
141
  else
 
142
#endif
 
143
    longget(j,ptr);
 
144
  return (double) j;
 
145
}
 
146
 
 
147
int64_t Field_long::val_int(void)
 
148
{
 
149
  int32_t j;
 
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);
 
155
#ifdef WORDS_BIGENDIAN
 
156
  if (table->s->db_low_byte_first)
 
157
    j=sint4korr(ptr);
 
158
  else
 
159
#endif
 
160
    longget(j,ptr);
 
161
  return (int64_t) j;
 
162
}
 
163
 
 
164
String *Field_long::val_str(String *val_buffer,
 
165
                            String *)
 
166
{
 
167
  const CHARSET_INFO * const cs= &my_charset_bin;
 
168
  uint32_t length;
 
169
  uint32_t mlength= max(field_length+1,12*cs->mbmaxlen);
 
170
  val_buffer->alloc(mlength);
 
171
  char *to=(char*) val_buffer->ptr();
 
172
  int32_t j;
 
173
 
 
174
  ASSERT_COLUMN_MARKED_FOR_READ;
 
175
 
 
176
#ifdef WORDS_BIGENDIAN
 
177
  if (table->s->db_low_byte_first)
 
178
    j=sint4korr(ptr);
 
179
  else
 
180
#endif
 
181
    longget(j,ptr);
 
182
 
 
183
  length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
 
184
  val_buffer->length(length);
 
185
 
 
186
  return val_buffer;
 
187
}
 
188
 
 
189
int Field_long::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
190
{
 
191
  int32_t a,b;
 
192
#ifdef WORDS_BIGENDIAN
 
193
  if (table->s->db_low_byte_first)
 
194
  {
 
195
    a=sint4korr(a_ptr);
 
196
    b=sint4korr(b_ptr);
 
197
  }
 
198
  else
 
199
#endif
 
200
  {
 
201
    longget(a,a_ptr);
 
202
    longget(b,b_ptr);
 
203
  }
 
204
 
 
205
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
206
}
 
207
 
 
208
void Field_long::sort_string(unsigned char *to,uint32_t )
 
209
{
 
210
#ifdef WORDS_BIGENDIAN
 
211
  if (!table->s->db_low_byte_first)
 
212
  {
 
213
    to[0] = (char) (ptr[0] ^ 128);              /* Revers signbit */
 
214
    to[1]   = ptr[1];
 
215
    to[2]   = ptr[2];
 
216
    to[3]   = ptr[3];
 
217
  }
 
218
  else
 
219
#endif
 
220
  {
 
221
    to[0] = (char) (ptr[3] ^ 128);              /* Revers signbit */
 
222
    to[1]   = ptr[2];
 
223
    to[2]   = ptr[1];
 
224
    to[3]   = ptr[0];
 
225
  }
 
226
}
 
227
 
 
228
 
 
229
void Field_long::sql_type(String &res) const
 
230
{
 
231
  const CHARSET_INFO * const cs=res.charset();
 
232
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "int"));
 
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);
 
285
}
 
286