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