~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/int32.cc

  • Committer: Daniel Nichter
  • Date: 2011-10-23 16:01:37 UTC
  • mto: This revision was merged to the branch mainline in revision 2448.
  • Revision ID: daniel@percona.com-20111023160137-7ac3blgz8z4tf8za
Add Administration Getting Started and Logging.  Capitalize SQL clause keywords.

Show diffs side-by-side

added added

removed removed

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