~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/long.cc

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

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