~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/int64.cc

  • Committer: Mark Atwood
  • Date: 2008-07-12 11:37:59 UTC
  • mto: This revision was merged to the branch mainline in revision 139.
  • Revision ID: me@mark.atwood.name-20080712113759-nrjn1bq1e0shuory
Add hello_world() UDF

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 MySQL
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; either version 2 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 
 */
20
 
 
21
 
 
22
 
#include "config.h"
23
 
#include <drizzled/field/int64.h>
24
 
#include <drizzled/error.h>
25
 
#include <drizzled/table.h>
26
 
#include <drizzled/session.h>
27
 
#include "drizzled/internal/my_sys.h"
28
 
 
29
 
#include <math.h>
30
 
 
31
 
#include <algorithm>
32
 
 
33
 
using namespace std;
34
 
 
35
 
namespace drizzled
36
 
{
37
 
 
38
 
namespace field
39
 
{
40
 
 
41
 
/****************************************************************************
42
 
  Field type Int64 int (8 bytes)
43
 
 ****************************************************************************/
44
 
 
45
 
int Int64::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
46
 
{
47
 
  int error= 0;
48
 
  char *end;
49
 
  uint64_t tmp;
50
 
 
51
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
52
 
 
53
 
  tmp= cs->cset->strntoull10rnd(cs, from, len, false, &end,&error);
54
 
  if (error == MY_ERRNO_ERANGE)
55
 
  {
56
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
57
 
    error= 1;
58
 
  }
59
 
  else if (getTable()->in_use->count_cuted_fields &&
60
 
           check_int(cs, from, len, end, error))
61
 
    error= 1;
62
 
  else
63
 
    error= 0;
64
 
#ifdef WORDS_BIGENDIAN
65
 
  if (getTable()->getShare()->db_low_byte_first)
66
 
  {
67
 
    int8store(ptr,tmp);
68
 
  }
69
 
  else
70
 
#endif
71
 
    int64_tstore(ptr,tmp);
72
 
  return error;
73
 
}
74
 
 
75
 
 
76
 
int Int64::store(double nr)
77
 
{
78
 
  int error= 0;
79
 
  int64_t res;
80
 
 
81
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
82
 
 
83
 
  nr= rint(nr);
84
 
 
85
 
  if (nr <= (double) INT64_MIN)
86
 
  {
87
 
    res= INT64_MIN;
88
 
    error= (nr < (double) INT64_MIN);
89
 
  }
90
 
  else if (nr >= (double) (uint64_t) INT64_MAX)
91
 
  {
92
 
    res= INT64_MAX;
93
 
    error= (nr > (double) INT64_MAX);
94
 
  }
95
 
  else
96
 
    res=(int64_t) nr;
97
 
 
98
 
  if (error)
99
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
100
 
 
101
 
#ifdef WORDS_BIGENDIAN
102
 
  if (getTable()->getShare()->db_low_byte_first)
103
 
  {
104
 
    int8store(ptr,res);
105
 
  }
106
 
  else
107
 
#endif
108
 
    int64_tstore(ptr,res);
109
 
  return error;
110
 
}
111
 
 
112
 
 
113
 
int Int64::store(int64_t nr, bool )
114
 
{
115
 
  int error= 0;
116
 
 
117
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
118
 
 
119
 
#ifdef WORDS_BIGENDIAN
120
 
  if (getTable()->getShare()->db_low_byte_first)
121
 
  {
122
 
    int8store(ptr,nr);
123
 
  }
124
 
  else
125
 
#endif
126
 
    int64_tstore(ptr,nr);
127
 
  return error;
128
 
}
129
 
 
130
 
 
131
 
double Int64::val_real(void)
132
 
{
133
 
  int64_t j;
134
 
 
135
 
  ASSERT_COLUMN_MARKED_FOR_READ;
136
 
 
137
 
#ifdef WORDS_BIGENDIAN
138
 
  if (getTable()->getShare()->db_low_byte_first)
139
 
  {
140
 
    j=sint8korr(ptr);
141
 
  }
142
 
  else
143
 
#endif
144
 
    int64_tget(j,ptr);
145
 
  /* The following is open coded to avoid a bug in gcc 3.3 */
146
 
  return (double) j;
147
 
}
148
 
 
149
 
 
150
 
int64_t Int64::val_int(void)
151
 
{
152
 
  int64_t j;
153
 
 
154
 
  ASSERT_COLUMN_MARKED_FOR_READ;
155
 
 
156
 
#ifdef WORDS_BIGENDIAN
157
 
  if (getTable()->getShare()->db_low_byte_first)
158
 
    j=sint8korr(ptr);
159
 
  else
160
 
#endif
161
 
    int64_tget(j,ptr);
162
 
  return j;
163
 
}
164
 
 
165
 
 
166
 
String *Int64::val_str(String *val_buffer,
167
 
                       String *)
168
 
{
169
 
  const CHARSET_INFO * const cs= &my_charset_bin;
170
 
  uint32_t length;
171
 
  uint32_t mlength= max(field_length+1,22*cs->mbmaxlen);
172
 
  val_buffer->alloc(mlength);
173
 
  char *to=(char*) val_buffer->ptr();
174
 
  int64_t j;
175
 
 
176
 
  ASSERT_COLUMN_MARKED_FOR_READ;
177
 
 
178
 
#ifdef WORDS_BIGENDIAN
179
 
  if (getTable()->getShare()->db_low_byte_first)
180
 
    j=sint8korr(ptr);
181
 
  else
182
 
#endif
183
 
    int64_tget(j,ptr);
184
 
 
185
 
  length=(uint32_t) (cs->cset->int64_t10_to_str)(cs,to,mlength, -10, j);
186
 
  val_buffer->length(length);
187
 
 
188
 
  return val_buffer;
189
 
}
190
 
 
191
 
int Int64::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
192
 
{
193
 
  int64_t a,b;
194
 
#ifdef WORDS_BIGENDIAN
195
 
  if (getTable()->getShare()->db_low_byte_first)
196
 
  {
197
 
    a=sint8korr(a_ptr);
198
 
    b=sint8korr(b_ptr);
199
 
  }
200
 
  else
201
 
#endif
202
 
  {
203
 
    int64_tget(a,a_ptr);
204
 
    int64_tget(b,b_ptr);
205
 
  }
206
 
  return (a < b) ? -1 : (a > b) ? 1 : 0;
207
 
}
208
 
 
209
 
void Int64::sort_string(unsigned char *to,uint32_t )
210
 
{
211
 
#ifdef WORDS_BIGENDIAN
212
 
  if (!getTable()->getShare()->db_low_byte_first)
213
 
  {
214
 
    to[0] = (char) (ptr[0] ^ 128);              /* Revers signbit */
215
 
    to[1]   = ptr[1];
216
 
    to[2]   = ptr[2];
217
 
    to[3]   = ptr[3];
218
 
    to[4]   = ptr[4];
219
 
    to[5]   = ptr[5];
220
 
    to[6]   = ptr[6];
221
 
    to[7]   = ptr[7];
222
 
  }
223
 
  else
224
 
#endif
225
 
  {
226
 
    to[0] = (char) (ptr[7] ^ 128);              /* Revers signbit */
227
 
    to[1]   = ptr[6];
228
 
    to[2]   = ptr[5];
229
 
    to[3]   = ptr[4];
230
 
    to[4]   = ptr[3];
231
 
    to[5]   = ptr[2];
232
 
    to[6]   = ptr[1];
233
 
    to[7]   = ptr[0];
234
 
  }
235
 
}
236
 
 
237
 
 
238
 
void Int64::sql_type(String &res) const
239
 
{
240
 
  const CHARSET_INFO * const cs=res.charset();
241
 
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "bigint"));
242
 
}
243
 
 
244
 
 
245
 
unsigned char *Int64::pack(unsigned char* to, const unsigned char *from,
246
 
                           uint32_t,
247
 
#ifdef WORDS_BIGENDIAN
248
 
                           bool low_byte_first
249
 
#else
250
 
                           bool
251
 
#endif
252
 
                          )
253
 
{
254
 
  int64_t val;
255
 
#ifdef WORDS_BIGENDIAN
256
 
  if (getTable()->getShare()->db_low_byte_first)
257
 
    val = sint8korr(from);
258
 
  else
259
 
#endif
260
 
    int64_tget(val, from);
261
 
 
262
 
#ifdef WORDS_BIGENDIAN
263
 
  if (low_byte_first)
264
 
    int8store(to, val);
265
 
  else
266
 
#endif
267
 
    int64_tstore(to, val);
268
 
  return to + sizeof(val);
269
 
}
270
 
 
271
 
 
272
 
const unsigned char *Int64::unpack(unsigned char* to, const unsigned char *from, uint32_t,
273
 
#ifdef WORDS_BIGENDIAN
274
 
                                   bool low_byte_first
275
 
#else
276
 
                                   bool
277
 
#endif
278
 
                                  )
279
 
{
280
 
  int64_t val;
281
 
#ifdef WORDS_BIGENDIAN
282
 
  if (low_byte_first)
283
 
    val = sint8korr(from);
284
 
  else
285
 
#endif
286
 
    int64_tget(val, from);
287
 
 
288
 
#ifdef WORDS_BIGENDIAN
289
 
  if (getTable()->getShare()->db_low_byte_first)
290
 
    int8store(to, val);
291
 
  else
292
 
#endif
293
 
    int64_tstore(to, val);
294
 
  return from + sizeof(val);
295
 
}
296
 
 
297
 
} /* namespace field */
298
 
} /* namespace drizzled */