~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/long.cc

  • Committer: Mark Atwood
  • Date: 2009-01-05 04:37:22 UTC
  • mto: (758.1.1 devel)
  • mto: This revision was merged to the branch mainline in revision 759.
  • Revision ID: me@mark.atwood.name-20090105043722-03l4mzcxi4yjjjih
replace sql_print_error etc with errmsg_print

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 <drizzled/table.h>
 
26
#include <drizzled/session.h>
 
27
#include CMATH_H
 
28
 
 
29
#if defined(CMATH_NAMESPACE)
 
30
using namespace CMATH_NAMESPACE;
 
31
#endif
27
32
 
28
33
/****************************************************************************
29
34
** long int
34
39
  long store_tmp;
35
40
  int error;
36
41
  int64_t rnd;
37
 
  
 
42
 
38
43
  error= get_int(cs, from, len, &rnd, UINT32_MAX, INT32_MIN, INT32_MAX);
39
 
  store_tmp= unsigned_flag ? (long) (uint64_t) rnd : (long) rnd;
 
44
  store_tmp= (long) rnd;
40
45
#ifdef WORDS_BIGENDIAN
41
46
  if (table->s->db_low_byte_first)
42
47
  {
54
59
  int error= 0;
55
60
  int32_t res;
56
61
  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;
 
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
72
  }
73
73
  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
 
  }
 
74
    res=(int32_t) (int64_t) nr;
 
75
 
88
76
  if (error)
89
77
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
90
78
 
105
93
  int error= 0;
106
94
  int32_t res;
107
95
 
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;
 
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;
122
107
  }
123
108
  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
 
  }
 
109
    res=(int32_t) nr;
 
110
 
140
111
  if (error)
141
112
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
142
113
 
161
132
  else
162
133
#endif
163
134
    longget(j,ptr);
164
 
  return unsigned_flag ? (double) (uint32_t) j : (double) j;
 
135
  return (double) j;
165
136
}
166
137
 
167
138
int64_t Field_long::val_int(void)
168
139
{
169
140
  int32_t j;
170
 
  /* See the comment in Field_long::store(long long) */
171
 
  assert(table->in_use == current_thd);
 
141
  /* See the comment in Field_long::store(int64_t) */
 
142
  assert(table->in_use == current_session);
172
143
#ifdef WORDS_BIGENDIAN
173
144
  if (table->s->db_low_byte_first)
174
145
    j=sint4korr(ptr);
175
146
  else
176
147
#endif
177
148
    longget(j,ptr);
178
 
  return unsigned_flag ? (int64_t) (uint32_t) j : (int64_t) j;
 
149
  return (int64_t) j;
179
150
}
180
151
 
181
152
String *Field_long::val_str(String *val_buffer,
194
165
#endif
195
166
    longget(j,ptr);
196
167
 
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);
 
168
  length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
201
169
  val_buffer->length(length);
202
170
 
203
171
  return val_buffer;
224
192
    longget(a,a_ptr);
225
193
    longget(b,b_ptr);
226
194
  }
227
 
  if (unsigned_flag)
228
 
    return ((uint32_t) a < (uint32_t) b) ? -1 : ((uint32_t) a > (uint32_t) b) ? 1 : 0;
 
195
 
229
196
  return (a < b) ? -1 : (a > b) ? 1 : 0;
230
197
}
231
198
 
234
201
#ifdef WORDS_BIGENDIAN
235
202
  if (!table->s->db_low_byte_first)
236
203
  {
237
 
    if (unsigned_flag)
238
 
      to[0] = ptr[0];
239
 
    else
240
 
      to[0] = (char) (ptr[0] ^ 128);            /* Revers signbit */
 
204
    to[0] = (char) (ptr[0] ^ 128);              /* Revers signbit */
241
205
    to[1]   = ptr[1];
242
206
    to[2]   = ptr[2];
243
207
    to[3]   = ptr[3];
245
209
  else
246
210
#endif
247
211
  {
248
 
    if (unsigned_flag)
249
 
      to[0] = ptr[3];
250
 
    else
251
 
      to[0] = (char) (ptr[3] ^ 128);            /* Revers signbit */
 
212
    to[0] = (char) (ptr[3] ^ 128);              /* Revers signbit */
252
213
    to[1]   = ptr[2];
253
214
    to[2]   = ptr[1];
254
215
    to[3]   = ptr[0];
260
221
{
261
222
  const CHARSET_INFO * const cs=res.charset();
262
223
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "int"));
263
 
  add_unsigned(res);
 
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);
264
276
}
265
277