~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/long.cc

  • Committer: Brian Aker
  • Date: 2010-09-12 01:42:27 UTC
  • mto: (1759.2.1 build)
  • mto: This revision was merged to the branch mainline in revision 1762.
  • Revision ID: brian@tangent.org-20100912014227-krt6d9z5ohqrokhb
Add two plugins to handle the string and math functions.

Show diffs side-by-side

added added

removed removed

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