~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
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
50
  else if (table->in_use->count_cuted_fields &&
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
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
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
102
int Field_int64_t::store(int64_t nr, bool )
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,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
148
				String *)
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
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
895 by Brian Aker
Completion (?) of uint conversion.
163
  length=(uint32_t) (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
481 by Brian Aker
Remove all of uchar.
169
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/
170
{
171
  int64_t a,b;
172
#ifdef WORDS_BIGENDIAN
173
  if (table->s->db_low_byte_first)
174
  {
175
    a=sint8korr(a_ptr);
176
    b=sint8korr(b_ptr);
177
  }
178
  else
179
#endif
180
  {
181
    int64_tget(a,a_ptr);
182
    int64_tget(b,b_ptr);
183
  }
184
  return (a < b) ? -1 : (a > b) ? 1 : 0;
185
}
186
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
187
void Field_int64_t::sort_string(unsigned char *to,uint32_t )
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
188
{
189
#ifdef WORDS_BIGENDIAN
190
  if (!table->s->db_low_byte_first)
191
  {
509 by Brian Aker
Incremental patch for removing unsigned.
192
    to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
193
    to[1]   = ptr[1];
194
    to[2]   = ptr[2];
195
    to[3]   = ptr[3];
196
    to[4]   = ptr[4];
197
    to[5]   = ptr[5];
198
    to[6]   = ptr[6];
199
    to[7]   = ptr[7];
200
  }
201
  else
202
#endif
203
  {
509 by Brian Aker
Incremental patch for removing unsigned.
204
    to[0] = (char) (ptr[7] ^ 128);		/* Revers signbit */
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
205
    to[1]   = ptr[6];
206
    to[2]   = ptr[5];
207
    to[3]   = ptr[4];
208
    to[4]   = ptr[3];
209
    to[5]   = ptr[2];
210
    to[6]   = ptr[1];
211
    to[7]   = ptr[0];
212
  }
213
}
214
215
216
void Field_int64_t::sql_type(String &res) const
217
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
218
  const CHARSET_INFO * const cs=res.charset();
221 by Brian Aker
First pass of removing length types for ints.
219
  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/
220
}
590.3.1 by Lee Bieber
changes to get build working on Solaris
221
222
223
unsigned char *Field_int64_t::pack(unsigned char* to, const unsigned char *from,
224
                                         uint32_t,
225
#ifdef WORDS_BIGENDIAN
226
                                         bool low_byte_first
227
#else
228
                                         bool
229
#endif
230
)
231
{
232
  int64_t val;
233
#ifdef WORDS_BIGENDIAN
234
  if (table->s->db_low_byte_first)
235
     val = sint8korr(from);
236
  else
237
#endif
238
    int64_tget(val, from);
239
240
#ifdef WORDS_BIGENDIAN
241
  if (low_byte_first)
242
    int8store(to, val);
243
  else
244
#endif
245
    int64_tstore(to, val);
246
  return to + sizeof(val);
247
}
248
249
250
const unsigned char *Field_int64_t::unpack(unsigned char* to, const unsigned char *from, uint32_t,
251
#ifdef WORDS_BIGENDIAN
252
                                           bool low_byte_first
253
#else
254
                                           bool
255
#endif
256
)
257
{
258
  int64_t val;
259
#ifdef WORDS_BIGENDIAN
260
  if (low_byte_first)
261
    val = sint8korr(from);
262
  else
263
#endif
264
    int64_tget(val, from);
265
266
#ifdef WORDS_BIGENDIAN
267
  if (table->s->db_low_byte_first)
268
    int8store(to, val);
269
  else
270
#endif
271
    int64_tstore(to, val);
272
  return from + sizeof(val);
273
}
274