~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/long.cc

  • Committer: Mark Atwood
  • Date: 2008-10-03 01:39:40 UTC
  • mto: This revision was merged to the branch mainline in revision 437.
  • Revision ID: mark@fallenpegasus.com-20081003013940-mvefjo725dltz41h
rename logging_noop to logging_query

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
 
 
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
 
{
 
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>
39
27
 
40
28
/****************************************************************************
41
 
 ** Int32
42
 
 ****************************************************************************/
 
29
** long int
 
30
****************************************************************************/
43
31
 
44
 
  int Int32::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
 
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;
 
40
#ifdef WORDS_BIGENDIAN
 
41
  if (table->s->db_low_byte_first)
45
42
  {
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;
 
43
    int4store(ptr, store_tmp);
 
44
  }
 
45
  else
 
46
#endif
54
47
    longstore(ptr, store_tmp);
55
 
 
56
 
    return error;
 
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;
57
72
  }
58
 
 
59
 
 
60
 
  int Int32::store(double nr)
 
73
  else
61
74
  {
62
 
    int error= 0;
63
 
    int32_t res;
64
 
    nr=rint(nr);
65
 
 
66
 
    ASSERT_COLUMN_MARKED_FOR_WRITE;
67
 
 
68
75
    if (nr < (double) INT32_MIN)
69
76
    {
70
77
      res=(int32_t) INT32_MIN;
77
84
    }
78
85
    else
79
86
      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
 
 
 
87
  }
 
88
  if (error)
 
89
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
90
 
 
91
#ifdef WORDS_BIGENDIAN
 
92
  if (table->s->db_low_byte_first)
 
93
  {
 
94
    int4store(ptr,res);
 
95
  }
 
96
  else
 
97
#endif
84
98
    longstore(ptr,res);
85
 
 
86
 
    return error;
 
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 (nr > INT32_MAX)
 
116
    {
 
117
      res= INT32_MAX;
 
118
      error= 1;
 
119
    }
 
120
    else
 
121
      res=(int32_t) (uint32_t) nr;
87
122
  }
88
 
 
89
 
 
90
 
  int Int32::store(int64_t nr, bool unsigned_val)
 
123
  else
91
124
  {
92
 
    int error= 0;
93
 
    int32_t res;
94
 
 
95
 
    ASSERT_COLUMN_MARKED_FOR_WRITE;
96
 
 
97
125
    if (nr < 0 && unsigned_val)
98
126
      nr= ((int64_t) INT32_MAX) + 1;           // Generate overflow
99
 
 
100
 
    if (nr < (int64_t) INT32_MIN)
 
127
    if (nr < (int64_t) INT32_MIN) 
101
128
    {
102
129
      res=(int32_t) INT32_MIN;
103
130
      error= 1;
108
135
      error= 1;
109
136
    }
110
137
    else
111
 
    {
112
138
      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
 
 
 
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
118
150
    longstore(ptr,res);
119
 
 
120
 
    return error;
121
 
  }
122
 
 
123
 
 
124
 
  double Int32::val_real(void) const
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) const
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 *) const
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
 
 
 
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
159
200
    length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
160
 
    val_buffer->length(length);
161
 
 
162
 
    return val_buffer;
 
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;
 
215
#ifdef WORDS_BIGENDIAN
 
216
  if (table->s->db_low_byte_first)
 
217
  {
 
218
    a=sint4korr(a_ptr);
 
219
    b=sint4korr(b_ptr);
163
220
  }
164
 
 
165
 
  int Int32::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
221
  else
 
222
#endif
166
223
  {
167
 
    int32_t a,b;
168
 
 
169
224
    longget(a,a_ptr);
170
225
    longget(b,b_ptr);
171
 
 
172
 
    return (a < b) ? -1 : (a > b) ? 1 : 0;
173
226
  }
 
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
}
174
231
 
175
 
  void Int32::sort_string(unsigned char *to,uint32_t )
 
232
void Field_long::sort_string(uchar *to,uint length __attribute__((unused)))
 
233
{
 
234
#ifdef WORDS_BIGENDIAN
 
235
  if (!table->s->db_low_byte_first)
176
236
  {
177
 
#ifdef WORDS_BIGENDIAN
178
 
    {
 
237
    if (unsigned_flag)
 
238
      to[0] = ptr[0];
 
239
    else
179
240
      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
 
    {
 
241
    to[1]   = ptr[1];
 
242
    to[2]   = ptr[2];
 
243
    to[3]   = ptr[3];
 
244
  }
 
245
  else
 
246
#endif
 
247
  {
 
248
    if (unsigned_flag)
 
249
      to[0] = ptr[3];
 
250
    else
186
251
      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 */
 
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