~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/long.cc

  • Committer: Monty Taylor
  • Date: 2009-03-20 04:49:49 UTC
  • mto: (950.1.1 mordred)
  • mto: This revision was merged to the branch mainline in revision 958.
  • Revision ID: mordred@inaugust.com-20090320044949-nfx7ygyy89ojl6v5
RemovedĀ unusedĀ code.

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