~drizzle-trunk/drizzle/development

466 by Monty Taylor
Fixed modelines... these files are c++.
1
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
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
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
22
#include <drizzled/server_includes.h>
214 by Brian Aker
Rename of fields (fix issue with string and decimal .h clashing).
23
#include <drizzled/field/int64_t.h>
550 by Monty Taylor
Moved error.h into just the files that need it.
24
#include <drizzled/error.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
25
#include <drizzled/table.h>
26
#include <drizzled/session.h>
27
572.1.4 by Monty Taylor
Removed a bunch of unusued tests and defines from autoconf.
28
#include CMATH_H
29
30
#if defined(CMATH_NAMESPACE)
31
using namespace CMATH_NAMESPACE;
32
#endif
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
33
34
/****************************************************************************
35
 Field type int64_t int (8 bytes)
36
****************************************************************************/
37
482 by Brian Aker
Remove uint.
38
int Field_int64_t::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
39
{
40
  int error= 0;
41
  char *end;
42
  uint64_t tmp;
43
509 by Brian Aker
Incremental patch for removing unsigned.
44
  tmp= cs->cset->strntoull10rnd(cs, from, len, false, &end,&error);
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
45
  if (error == MY_ERRNO_ERANGE)
46
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
47
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
48
    error= 1;
49
  }
50
  else if (table->in_use->count_cuted_fields && 
51
           check_int(cs, from, len, end, error))
52
    error= 1;
53
  else
54
    error= 0;
55
#ifdef WORDS_BIGENDIAN
56
  if (table->s->db_low_byte_first)
57
  {
58
    int8store(ptr,tmp);
59
  }
60
  else
61
#endif
62
    int64_tstore(ptr,tmp);
63
  return error;
64
}
65
66
67
int Field_int64_t::store(double nr)
68
{
69
  int error= 0;
70
  int64_t res;
71
72
  nr= rint(nr);
509 by Brian Aker
Incremental patch for removing unsigned.
73
74
  if (nr <= (double) INT64_MIN)
75
  {
76
    res= INT64_MIN;
77
    error= (nr < (double) INT64_MIN);
78
  }
79
  else if (nr >= (double) (uint64_t) INT64_MAX)
80
  {
81
    res= INT64_MAX;
82
    error= (nr > (double) INT64_MAX);
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
83
  }
84
  else
509 by Brian Aker
Incremental patch for removing unsigned.
85
    res=(int64_t) nr;
86
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
87
  if (error)
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
88
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
89
90
#ifdef WORDS_BIGENDIAN
91
  if (table->s->db_low_byte_first)
92
  {
93
    int8store(ptr,res);
94
  }
95
  else
96
#endif
97
    int64_tstore(ptr,res);
98
  return error;
99
}
100
101
509 by Brian Aker
Incremental patch for removing unsigned.
102
int Field_int64_t::store(int64_t nr, bool unsigned_val __attribute__((unused)))
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
103
{
104
  int error= 0;
105
106
#ifdef WORDS_BIGENDIAN
107
  if (table->s->db_low_byte_first)
108
  {
109
    int8store(ptr,nr);
110
  }
111
  else
112
#endif
113
    int64_tstore(ptr,nr);
114
  return error;
115
}
116
117
118
double Field_int64_t::val_real(void)
119
{
120
  int64_t j;
121
#ifdef WORDS_BIGENDIAN
122
  if (table->s->db_low_byte_first)
123
  {
124
    j=sint8korr(ptr);
125
  }
126
  else
127
#endif
128
    int64_tget(j,ptr);
129
  /* The following is open coded to avoid a bug in gcc 3.3 */
130
  return (double) j;
131
}
132
133
134
int64_t Field_int64_t::val_int(void)
135
{
136
  int64_t j;
137
#ifdef WORDS_BIGENDIAN
138
  if (table->s->db_low_byte_first)
139
    j=sint8korr(ptr);
140
  else
141
#endif
142
    int64_tget(j,ptr);
143
  return j;
144
}
145
146
147
String *Field_int64_t::val_str(String *val_buffer,
148
				String *val_ptr __attribute__((unused)))
149
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
150
  const CHARSET_INFO * const cs= &my_charset_bin;
482 by Brian Aker
Remove uint.
151
  uint32_t length;
152
  uint32_t mlength=cmax(field_length+1,22*cs->mbmaxlen);
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
153
  val_buffer->alloc(mlength);
154
  char *to=(char*) val_buffer->ptr();
155
  int64_t j;
156
#ifdef WORDS_BIGENDIAN
157
  if (table->s->db_low_byte_first)
158
    j=sint8korr(ptr);
159
  else
160
#endif
161
    int64_tget(j,ptr);
162
509 by Brian Aker
Incremental patch for removing unsigned.
163
  length=(uint) (cs->cset->int64_t10_to_str)(cs,to,mlength, -10, j);
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
164
  val_buffer->length(length);
216 by Brian Aker
Remove completely ZEROFILL
165
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
166
  return val_buffer;
167
}
168
169
170
bool Field_int64_t::send_binary(Protocol *protocol)
171
{
509 by Brian Aker
Incremental patch for removing unsigned.
172
  return protocol->store_int64_t(Field_int64_t::val_int(), false);
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
173
}
174
175
481 by Brian Aker
Remove all of uchar.
176
int Field_int64_t::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
177
{
178
  int64_t a,b;
179
#ifdef WORDS_BIGENDIAN
180
  if (table->s->db_low_byte_first)
181
  {
182
    a=sint8korr(a_ptr);
183
    b=sint8korr(b_ptr);
184
  }
185
  else
186
#endif
187
  {
188
    int64_tget(a,a_ptr);
189
    int64_tget(b,b_ptr);
190
  }
191
  return (a < b) ? -1 : (a > b) ? 1 : 0;
192
}
193
482 by Brian Aker
Remove uint.
194
void Field_int64_t::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
195
{
196
#ifdef WORDS_BIGENDIAN
197
  if (!table->s->db_low_byte_first)
198
  {
509 by Brian Aker
Incremental patch for removing unsigned.
199
    to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
200
    to[1]   = ptr[1];
201
    to[2]   = ptr[2];
202
    to[3]   = ptr[3];
203
    to[4]   = ptr[4];
204
    to[5]   = ptr[5];
205
    to[6]   = ptr[6];
206
    to[7]   = ptr[7];
207
  }
208
  else
209
#endif
210
  {
509 by Brian Aker
Incremental patch for removing unsigned.
211
    to[0] = (char) (ptr[7] ^ 128);		/* Revers signbit */
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
212
    to[1]   = ptr[6];
213
    to[2]   = ptr[5];
214
    to[3]   = ptr[4];
215
    to[4]   = ptr[3];
216
    to[5]   = ptr[2];
217
    to[6]   = ptr[1];
218
    to[7]   = ptr[0];
219
  }
220
}
221
222
223
void Field_int64_t::sql_type(String &res) const
224
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
225
  const CHARSET_INFO * const cs=res.charset();
221 by Brian Aker
First pass of removing length types for ints.
226
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "bigint"));
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
227
}
590.3.1 by Lee Bieber
changes to get build working on Solaris
228
229
230
unsigned char *Field_int64_t::pack(unsigned char* to, const unsigned char *from,
231
                                         uint32_t,
232
#ifdef WORDS_BIGENDIAN
233
                                         bool low_byte_first
234
#else
235
                                         bool
236
#endif
237
)
238
{
239
  int64_t val;
240
#ifdef WORDS_BIGENDIAN
241
  if (table->s->db_low_byte_first)
242
     val = sint8korr(from);
243
  else
244
#endif
245
    int64_tget(val, from);
246
247
#ifdef WORDS_BIGENDIAN
248
  if (low_byte_first)
249
    int8store(to, val);
250
  else
251
#endif
252
    int64_tstore(to, val);
253
  return to + sizeof(val);
254
}
255
256
257
const unsigned char *Field_int64_t::unpack(unsigned char* to, const unsigned char *from, uint32_t,
258
#ifdef WORDS_BIGENDIAN
259
                                           bool low_byte_first
260
#else
261
                                           bool
262
#endif
263
)
264
{
265
  int64_t val;
266
#ifdef WORDS_BIGENDIAN
267
  if (low_byte_first)
268
    val = sint8korr(from);
269
  else
270
#endif
271
    int64_tget(val, from);
272
273
#ifdef WORDS_BIGENDIAN
274
  if (table->s->db_low_byte_first)
275
    int8store(to, val);
276
  else
277
#endif
278
    int64_tstore(to, val);
279
  return from + sizeof(val);
280
}
281