~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/long.cc

  • Committer: Stewart Smith
  • Date: 2009-10-19 03:27:43 UTC
  • mto: This revision was merged to the branch mainline in revision 1188.
  • Revision ID: stewart@flamingspork.com-20091019032743-xajbflq0ktpe9vw2
remove --disable-warnings in func_in_null_scan.test

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)
 
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)
45
49
  {
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;
 
50
    int4store(ptr, store_tmp);
 
51
  }
 
52
  else
 
53
#endif
54
54
    longstore(ptr, store_tmp);
55
 
 
56
 
    return error;
57
 
  }
58
 
 
59
 
 
60
 
  int Int32::store(double nr)
61
 
  {
62
 
    int error= 0;
63
 
    int32_t res;
64
 
    nr=rint(nr);
65
 
 
66
 
    ASSERT_COLUMN_MARKED_FOR_WRITE;
67
 
 
68
 
    if (nr < (double) INT32_MIN)
69
 
    {
70
 
      res=(int32_t) INT32_MIN;
71
 
      error= 1;
72
 
    }
73
 
    else if (nr > (double) INT32_MAX)
74
 
    {
75
 
      res=(int32_t) INT32_MAX;
76
 
      error= 1;
77
 
    }
78
 
    else
79
 
      res=(int32_t) (int64_t) nr;
80
 
 
81
 
    if (error)
82
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
83
 
 
84
 
    longstore(ptr,res);
85
 
 
86
 
    return error;
87
 
  }
88
 
 
89
 
 
90
 
  int Int32::store(int64_t nr, bool unsigned_val)
91
 
  {
92
 
    int error= 0;
93
 
    int32_t res;
94
 
 
95
 
    ASSERT_COLUMN_MARKED_FOR_WRITE;
96
 
 
97
 
    if (nr < 0 && unsigned_val)
98
 
      nr= ((int64_t) INT32_MAX) + 1;           // Generate overflow
99
 
 
100
 
    if (nr < (int64_t) INT32_MIN)
101
 
    {
102
 
      res=(int32_t) INT32_MIN;
103
 
      error= 1;
104
 
    }
105
 
    else if (nr > (int64_t) INT32_MAX)
106
 
    {
107
 
      res=(int32_t) INT32_MAX;
108
 
      error= 1;
109
 
    }
110
 
    else
111
 
    {
112
 
      res=(int32_t) nr;
113
 
    }
114
 
 
115
 
    if (error)
116
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
117
 
 
118
 
    longstore(ptr,res);
119
 
 
120
 
    return error;
121
 
  }
122
 
 
123
 
 
124
 
  double Int32::val_real(void)
125
 
  {
126
 
    int32_t j;
127
 
 
128
 
    ASSERT_COLUMN_MARKED_FOR_READ;
129
 
 
130
 
    longget(j,ptr);
131
 
 
132
 
    return (double) j;
133
 
  }
134
 
 
135
 
  int64_t Int32::val_int(void)
136
 
  {
137
 
    int32_t j;
138
 
 
139
 
    ASSERT_COLUMN_MARKED_FOR_READ;
140
 
 
141
 
    longget(j,ptr);
142
 
 
143
 
    return (int64_t) j;
144
 
  }
145
 
 
146
 
  String *Int32::val_str(String *val_buffer, String *)
147
 
  {
148
 
    const CHARSET_INFO * const cs= &my_charset_bin;
149
 
    uint32_t length;
150
 
    uint32_t mlength= max(field_length+1,12*cs->mbmaxlen);
151
 
    val_buffer->alloc(mlength);
152
 
    char *to=(char*) val_buffer->ptr();
153
 
    int32_t j;
154
 
 
155
 
    ASSERT_COLUMN_MARKED_FOR_READ;
156
 
 
157
 
    longget(j,ptr);
158
 
 
159
 
    length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
160
 
    val_buffer->length(length);
161
 
 
162
 
    return val_buffer;
163
 
  }
164
 
 
165
 
  int Int32::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
166
 
  {
167
 
    int32_t a,b;
168
 
 
 
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
  {
169
201
    longget(a,a_ptr);
170
202
    longget(b,b_ptr);
171
 
 
172
 
    return (a < b) ? -1 : (a > b) ? 1 : 0;
173
 
  }
174
 
 
175
 
  void Int32::sort_string(unsigned char *to,uint32_t )
176
 
  {
177
 
#ifdef WORDS_BIGENDIAN
178
 
    {
179
 
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
180
 
      to[1]   = ptr[1];
181
 
      to[2]   = ptr[2];
182
 
      to[3]   = ptr[3];
183
 
    }
184
 
#else
185
 
    {
186
 
      to[0] = (char) (ptr[3] ^ 128);            /* Revers signbit */
187
 
      to[1]   = ptr[2];
188
 
      to[2]   = ptr[1];
189
 
      to[3]   = ptr[0];
190
 
    }
191
 
#endif
192
 
  }
193
 
 
194
 
 
195
 
  void Int32::sql_type(String &res) const
196
 
  {
197
 
    const CHARSET_INFO * const cs=res.charset();
198
 
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "int"));
199
 
  }
200
 
 
201
 
  unsigned char *Int32::pack(unsigned char* to, const unsigned char *from, uint32_t, bool)
202
 
  {
203
 
    int32_t val;
204
 
    longget(val, from);
205
 
 
206
 
    longstore(to, val);
207
 
    return to + sizeof(val);
208
 
  }
209
 
 
210
 
 
211
 
  const unsigned char *Int32::unpack(unsigned char* to, const unsigned char *from, uint32_t, bool)
212
 
  {
213
 
    int32_t val;
214
 
    longget(val, from);
215
 
 
216
 
    longstore(to, val);
217
 
 
218
 
    return from + sizeof(val);
219
 
  }
220
 
 
221
 
} /* namespace field */
222
 
} /* namespace drizzled */
 
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