~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/int32.cc

  • Committer: Brian Aker
  • Date: 2010-12-18 18:24:57 UTC
  • mfrom: (1999.6.3 trunk)
  • Revision ID: brian@tangent.org-20101218182457-yi1wd0so2hml1k1w
Merge in Lee's copyright header fix

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
1
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
4
 *  Copyright (C) 2008 MySQL
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#ifdef USE_PRAGMA_IMPLEMENTATION
22
 
#pragma implementation                          // gcc: Class implementation
23
 
#endif
24
 
 
25
 
#include <drizzled/server_includes.h>
26
 
#include <drizzled/field/long.h>
 
21
 
 
22
#include "config.h"
 
23
#include <drizzled/field/int32.h>
 
24
#include <drizzled/error.h>
 
25
#include <drizzled/table.h>
 
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
39
 
28
40
/****************************************************************************
29
 
** long int
30
 
****************************************************************************/
31
 
 
32
 
int Field_long::store(const char *from,uint 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= unsigned_flag ? (long) (uint64_t) rnd : (long) rnd;
 
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;
40
54
#ifdef WORDS_BIGENDIAN
41
 
  if (table->s->db_low_byte_first)
42
 
  {
43
 
    int4store(ptr, store_tmp);
44
 
  }
45
 
  else
 
55
    if (getTable()->getShare()->db_low_byte_first)
 
56
    {
 
57
      int4store(ptr, store_tmp);
 
58
    }
 
59
    else
46
60
#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
 
  if (unsigned_flag)
58
 
  {
59
 
    if (nr < 0)
60
 
    {
61
 
      res=0;
62
 
      error= 1;
63
 
    }
64
 
    else if (nr > (double) UINT32_MAX)
65
 
    {
66
 
      res= INT32_MAX;
67
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
68
 
      error= 1;
69
 
    }
70
 
    else
71
 
      res=(int32_t) (uint32_t) nr;
 
61
      longstore(ptr, store_tmp);
 
62
    return error;
72
63
  }
73
 
  else
 
64
 
 
65
 
 
66
  int Int32::store(double nr)
74
67
  {
 
68
    int error= 0;
 
69
    int32_t res;
 
70
    nr=rint(nr);
 
71
 
 
72
    ASSERT_COLUMN_MARKED_FOR_WRITE;
 
73
 
75
74
    if (nr < (double) INT32_MIN)
76
75
    {
77
76
      res=(int32_t) INT32_MIN;
84
83
    }
85
84
    else
86
85
      res=(int32_t) (int64_t) nr;
87
 
  }
88
 
  if (error)
89
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
86
 
 
87
    if (error)
 
88
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
90
89
 
91
90
#ifdef WORDS_BIGENDIAN
92
 
  if (table->s->db_low_byte_first)
93
 
  {
94
 
    int4store(ptr,res);
95
 
  }
96
 
  else
 
91
    if (getTable()->getShare()->db_low_byte_first)
 
92
    {
 
93
      int4store(ptr,res);
 
94
    }
 
95
    else
97
96
#endif
98
 
    longstore(ptr,res);
99
 
  return error;
100
 
}
101
 
 
102
 
 
103
 
int Field_long::store(int64_t nr, bool unsigned_val)
104
 
{
105
 
  int error= 0;
106
 
  int32_t res;
107
 
 
108
 
  if (unsigned_flag)
109
 
  {
110
 
    if (nr < 0 && !unsigned_val)
111
 
    {
112
 
      res=0;
113
 
      error= 1;
114
 
    }
115
 
    else if ((uint64_t) nr >= (1LL << 32))
116
 
    {
117
 
      res=(int32_t) (uint32_t) ~0L;
118
 
      error= 1;
119
 
    }
120
 
    else
121
 
      res=(int32_t) (uint32_t) nr;
 
97
      longstore(ptr,res);
 
98
    return error;
122
99
  }
123
 
  else
 
100
 
 
101
 
 
102
  int Int32::store(int64_t nr, bool unsigned_val)
124
103
  {
 
104
    int error= 0;
 
105
    int32_t res;
 
106
 
 
107
    ASSERT_COLUMN_MARKED_FOR_WRITE;
 
108
 
125
109
    if (nr < 0 && unsigned_val)
126
110
      nr= ((int64_t) INT32_MAX) + 1;           // Generate overflow
127
 
    if (nr < (int64_t) INT32_MIN) 
 
111
    if (nr < (int64_t) INT32_MIN)
128
112
    {
129
113
      res=(int32_t) INT32_MIN;
130
114
      error= 1;
136
120
    }
137
121
    else
138
122
      res=(int32_t) nr;
139
 
  }
140
 
  if (error)
141
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
142
 
 
143
 
#ifdef WORDS_BIGENDIAN
144
 
  if (table->s->db_low_byte_first)
145
 
  {
146
 
    int4store(ptr,res);
147
 
  }
148
 
  else
149
 
#endif
150
 
    longstore(ptr,res);
151
 
  return error;
152
 
}
153
 
 
154
 
 
155
 
double Field_long::val_real(void)
156
 
{
157
 
  int32_t j;
158
 
#ifdef WORDS_BIGENDIAN
159
 
  if (table->s->db_low_byte_first)
160
 
    j=sint4korr(ptr);
161
 
  else
162
 
#endif
163
 
    longget(j,ptr);
164
 
  return unsigned_flag ? (double) (uint32_t) j : (double) j;
165
 
}
166
 
 
167
 
int64_t Field_long::val_int(void)
168
 
{
169
 
  int32_t j;
170
 
  /* See the comment in Field_long::store(long long) */
171
 
  assert(table->in_use == current_thd);
172
 
#ifdef WORDS_BIGENDIAN
173
 
  if (table->s->db_low_byte_first)
174
 
    j=sint4korr(ptr);
175
 
  else
176
 
#endif
177
 
    longget(j,ptr);
178
 
  return unsigned_flag ? (int64_t) (uint32_t) j : (int64_t) j;
179
 
}
180
 
 
181
 
String *Field_long::val_str(String *val_buffer,
182
 
                            String *val_ptr __attribute__((unused)))
183
 
{
184
 
  const CHARSET_INFO * const cs= &my_charset_bin;
185
 
  uint length;
186
 
  uint mlength=max(field_length+1,12*cs->mbmaxlen);
187
 
  val_buffer->alloc(mlength);
188
 
  char *to=(char*) val_buffer->ptr();
189
 
  int32_t j;
190
 
#ifdef WORDS_BIGENDIAN
191
 
  if (table->s->db_low_byte_first)
192
 
    j=sint4korr(ptr);
193
 
  else
194
 
#endif
195
 
    longget(j,ptr);
196
 
 
197
 
  if (unsigned_flag)
198
 
    length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32_t)j);
199
 
  else
 
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
 
200
189
    length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
201
 
  val_buffer->length(length);
202
 
 
203
 
  return val_buffer;
204
 
}
205
 
 
206
 
 
207
 
bool Field_long::send_binary(Protocol *protocol)
208
 
{
209
 
  return protocol->store_long(Field_long::val_int());
210
 
}
211
 
 
212
 
int Field_long::cmp(const uchar *a_ptr, const uchar *b_ptr)
213
 
{
214
 
  int32_t a,b;
 
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;
215
198
#ifdef WORDS_BIGENDIAN
216
 
  if (table->s->db_low_byte_first)
217
 
  {
218
 
    a=sint4korr(a_ptr);
219
 
    b=sint4korr(b_ptr);
220
 
  }
221
 
  else
 
199
    if (getTable()->getShare()->db_low_byte_first)
 
200
    {
 
201
      a=sint4korr(a_ptr);
 
202
      b=sint4korr(b_ptr);
 
203
    }
 
204
    else
222
205
#endif
223
 
  {
224
 
    longget(a,a_ptr);
225
 
    longget(b,b_ptr);
 
206
    {
 
207
      longget(a,a_ptr);
 
208
      longget(b,b_ptr);
 
209
    }
 
210
 
 
211
    return (a < b) ? -1 : (a > b) ? 1 : 0;
226
212
  }
227
 
  if (unsigned_flag)
228
 
    return ((uint32_t) a < (uint32_t) b) ? -1 : ((uint32_t) a > (uint32_t) b) ? 1 : 0;
229
 
  return (a < b) ? -1 : (a > b) ? 1 : 0;
230
 
}
231
213
 
232
 
void Field_long::sort_string(uchar *to,uint length __attribute__((unused)))
233
 
{
 
214
  void Int32::sort_string(unsigned char *to,uint32_t )
 
215
  {
234
216
#ifdef WORDS_BIGENDIAN
235
 
  if (!table->s->db_low_byte_first)
236
 
  {
237
 
    if (unsigned_flag)
238
 
      to[0] = ptr[0];
239
 
    else
 
217
    if (!getTable()->getShare()->db_low_byte_first)
 
218
    {
240
219
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
241
 
    to[1]   = ptr[1];
242
 
    to[2]   = ptr[2];
243
 
    to[3]   = ptr[3];
244
 
  }
245
 
  else
 
220
      to[1]   = ptr[1];
 
221
      to[2]   = ptr[2];
 
222
      to[3]   = ptr[3];
 
223
    }
 
224
    else
246
225
#endif
247
 
  {
248
 
    if (unsigned_flag)
249
 
      to[0] = ptr[3];
250
 
    else
 
226
    {
251
227
      to[0] = (char) (ptr[3] ^ 128);            /* Revers signbit */
252
 
    to[1]   = ptr[2];
253
 
    to[2]   = ptr[1];
254
 
    to[3]   = ptr[0];
255
 
  }
256
 
}
257
 
 
258
 
 
259
 
void Field_long::sql_type(String &res) const
260
 
{
261
 
  const CHARSET_INFO * const cs=res.charset();
262
 
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "int"));
263
 
  add_unsigned(res);
264
 
}
265
 
 
 
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 */