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