~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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
22
#include "config.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>
1241.9.62 by Monty Taylor
Removed plugin/myisam/myisam.h from session.h
27
#include "mysys/my_sys.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.
28
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
29
#include <math.h>
30
1067.4.6 by Nathan Williams
Converted all usages of cmax in drizzled/field/ and drizzled/function/ to std::max.
31
#include <algorithm>
32
33
using namespace std;
34
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
35
36
/****************************************************************************
37
 Field type int64_t int (8 bytes)
38
****************************************************************************/
39
482 by Brian Aker
Remove uint.
40
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/
41
{
42
  int error= 0;
43
  char *end;
44
  uint64_t tmp;
45
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
46
  ASSERT_COLUMN_MARKED_FOR_WRITE;
47
509 by Brian Aker
Incremental patch for removing unsigned.
48
  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/
49
  if (error == MY_ERRNO_ERANGE)
50
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
51
    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/
52
    error= 1;
53
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
54
  else if (table->in_use->count_cuted_fields &&
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
55
           check_int(cs, from, len, end, error))
56
    error= 1;
57
  else
58
    error= 0;
59
#ifdef WORDS_BIGENDIAN
60
  if (table->s->db_low_byte_first)
61
  {
62
    int8store(ptr,tmp);
63
  }
64
  else
65
#endif
66
    int64_tstore(ptr,tmp);
67
  return error;
68
}
69
70
71
int Field_int64_t::store(double nr)
72
{
73
  int error= 0;
74
  int64_t res;
75
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
76
  ASSERT_COLUMN_MARKED_FOR_WRITE;
77
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
78
  nr= rint(nr);
509 by Brian Aker
Incremental patch for removing unsigned.
79
80
  if (nr <= (double) INT64_MIN)
81
  {
82
    res= INT64_MIN;
83
    error= (nr < (double) INT64_MIN);
84
  }
85
  else if (nr >= (double) (uint64_t) INT64_MAX)
86
  {
87
    res= INT64_MAX;
88
    error= (nr > (double) INT64_MAX);
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
89
  }
90
  else
509 by Brian Aker
Incremental patch for removing unsigned.
91
    res=(int64_t) nr;
92
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
93
  if (error)
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
94
    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/
95
96
#ifdef WORDS_BIGENDIAN
97
  if (table->s->db_low_byte_first)
98
  {
99
    int8store(ptr,res);
100
  }
101
  else
102
#endif
103
    int64_tstore(ptr,res);
104
  return error;
105
}
106
107
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
108
int Field_int64_t::store(int64_t nr, bool )
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
109
{
110
  int error= 0;
111
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
112
  ASSERT_COLUMN_MARKED_FOR_WRITE;
113
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
114
#ifdef WORDS_BIGENDIAN
115
  if (table->s->db_low_byte_first)
116
  {
117
    int8store(ptr,nr);
118
  }
119
  else
120
#endif
121
    int64_tstore(ptr,nr);
122
  return error;
123
}
124
125
126
double Field_int64_t::val_real(void)
127
{
128
  int64_t j;
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
129
130
  ASSERT_COLUMN_MARKED_FOR_READ;
131
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
132
#ifdef WORDS_BIGENDIAN
133
  if (table->s->db_low_byte_first)
134
  {
135
    j=sint8korr(ptr);
136
  }
137
  else
138
#endif
139
    int64_tget(j,ptr);
140
  /* The following is open coded to avoid a bug in gcc 3.3 */
141
  return (double) j;
142
}
143
144
145
int64_t Field_int64_t::val_int(void)
146
{
147
  int64_t j;
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
148
149
  ASSERT_COLUMN_MARKED_FOR_READ;
150
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
151
#ifdef WORDS_BIGENDIAN
152
  if (table->s->db_low_byte_first)
153
    j=sint8korr(ptr);
154
  else
155
#endif
156
    int64_tget(j,ptr);
157
  return j;
158
}
159
160
161
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.
162
				String *)
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
163
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
164
  const CHARSET_INFO * const cs= &my_charset_bin;
482 by Brian Aker
Remove uint.
165
  uint32_t length;
1067.4.6 by Nathan Williams
Converted all usages of cmax in drizzled/field/ and drizzled/function/ to std::max.
166
  uint32_t mlength= max(field_length+1,22*cs->mbmaxlen);
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
167
  val_buffer->alloc(mlength);
168
  char *to=(char*) val_buffer->ptr();
169
  int64_t j;
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
170
171
  ASSERT_COLUMN_MARKED_FOR_READ;
172
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
173
#ifdef WORDS_BIGENDIAN
174
  if (table->s->db_low_byte_first)
175
    j=sint8korr(ptr);
176
  else
177
#endif
178
    int64_tget(j,ptr);
179
895 by Brian Aker
Completion (?) of uint conversion.
180
  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/
181
  val_buffer->length(length);
216 by Brian Aker
Remove completely ZEROFILL
182
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
183
  return val_buffer;
184
}
185
481 by Brian Aker
Remove all of uchar.
186
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/
187
{
188
  int64_t a,b;
189
#ifdef WORDS_BIGENDIAN
190
  if (table->s->db_low_byte_first)
191
  {
192
    a=sint8korr(a_ptr);
193
    b=sint8korr(b_ptr);
194
  }
195
  else
196
#endif
197
  {
198
    int64_tget(a,a_ptr);
199
    int64_tget(b,b_ptr);
200
  }
201
  return (a < b) ? -1 : (a > b) ? 1 : 0;
202
}
203
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
204
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/
205
{
206
#ifdef WORDS_BIGENDIAN
207
  if (!table->s->db_low_byte_first)
208
  {
509 by Brian Aker
Incremental patch for removing unsigned.
209
    to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
210
    to[1]   = ptr[1];
211
    to[2]   = ptr[2];
212
    to[3]   = ptr[3];
213
    to[4]   = ptr[4];
214
    to[5]   = ptr[5];
215
    to[6]   = ptr[6];
216
    to[7]   = ptr[7];
217
  }
218
  else
219
#endif
220
  {
509 by Brian Aker
Incremental patch for removing unsigned.
221
    to[0] = (char) (ptr[7] ^ 128);		/* Revers signbit */
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
222
    to[1]   = ptr[6];
223
    to[2]   = ptr[5];
224
    to[3]   = ptr[4];
225
    to[4]   = ptr[3];
226
    to[5]   = ptr[2];
227
    to[6]   = ptr[1];
228
    to[7]   = ptr[0];
229
  }
230
}
231
232
233
void Field_int64_t::sql_type(String &res) const
234
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
235
  const CHARSET_INFO * const cs=res.charset();
221 by Brian Aker
First pass of removing length types for ints.
236
  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/
237
}
590.3.1 by Lee Bieber
changes to get build working on Solaris
238
239
240
unsigned char *Field_int64_t::pack(unsigned char* to, const unsigned char *from,
241
                                         uint32_t,
242
#ifdef WORDS_BIGENDIAN
243
                                         bool low_byte_first
244
#else
245
                                         bool
246
#endif
247
)
248
{
249
  int64_t val;
250
#ifdef WORDS_BIGENDIAN
251
  if (table->s->db_low_byte_first)
252
     val = sint8korr(from);
253
  else
254
#endif
255
    int64_tget(val, from);
256
257
#ifdef WORDS_BIGENDIAN
258
  if (low_byte_first)
259
    int8store(to, val);
260
  else
261
#endif
262
    int64_tstore(to, val);
263
  return to + sizeof(val);
264
}
265
266
267
const unsigned char *Field_int64_t::unpack(unsigned char* to, const unsigned char *from, uint32_t,
268
#ifdef WORDS_BIGENDIAN
269
                                           bool low_byte_first
270
#else
271
                                           bool
272
#endif
273
)
274
{
275
  int64_t val;
276
#ifdef WORDS_BIGENDIAN
277
  if (low_byte_first)
278
    val = sint8korr(from);
279
  else
280
#endif
281
    int64_tget(val, from);
282
283
#ifdef WORDS_BIGENDIAN
284
  if (table->s->db_low_byte_first)
285
    int8store(to, val);
286
  else
287
#endif
288
    int64_tstore(to, val);
289
  return from + sizeof(val);
290
}
291