~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/long.cc

  • Committer: Brian Aker
  • Date: 2008-10-12 01:59:02 UTC
  • Revision ID: brian@tangent.org-20081012015902-prhy6wsimdqr28om
Dead code around unsigned (first pass)

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