~drizzle-trunk/drizzle/development

173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
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
#ifdef USE_PRAGMA_IMPLEMENTATION
22
#pragma implementation				// gcc: Class implementation
23
#endif
24
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
25
#include <drizzled/server_includes.h>
214 by Brian Aker
Rename of fields (fix issue with string and decimal .h clashing).
26
#include <drizzled/field/long.h>
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
27
28
/****************************************************************************
29
** long int
30
****************************************************************************/
31
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
32
int Field_long::store(const char *from,uint len, const CHARSET_INFO * const cs)
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
33
{
34
  long store_tmp;
35
  int error;
36
  int64_t rnd;
37
  
38
  error= get_int(cs, from, len, &rnd, UINT32_MAX, INT32_MIN, INT32_MAX);
39
  store_tmp= unsigned_flag ? (long) (uint64_t) rnd : (long) rnd;
40
#ifdef WORDS_BIGENDIAN
41
  if (table->s->db_low_byte_first)
42
  {
43
    int4store(ptr, store_tmp);
44
  }
45
  else
46
#endif
47
    longstore(ptr, store_tmp);
48
  return error;
49
}
50
51
52
int Field_long::store(double nr)
53
{
54
  int error= 0;
205 by Brian Aker
uint32 -> uin32_t
55
  int32_t res;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
56
  nr=rint(nr);
57
  if (unsigned_flag)
58
  {
59
    if (nr < 0)
60
    {
61
      res=0;
62
      error= 1;
63
    }
64
    else if (nr > (double) UINT32_MAX)
65
    {
66
      res= INT32_MAX;
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
67
      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/
68
      error= 1;
69
    }
70
    else
290 by Brian Aker
Update for ulong change over.
71
      res=(int32_t) (uint32_t) nr;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
72
  }
73
  else
74
  {
75
    if (nr < (double) INT32_MIN)
76
    {
205 by Brian Aker
uint32 -> uin32_t
77
      res=(int32_t) INT32_MIN;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
78
      error= 1;
79
    }
80
    else if (nr > (double) INT32_MAX)
81
    {
205 by Brian Aker
uint32 -> uin32_t
82
      res=(int32_t) INT32_MAX;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
83
      error= 1;
84
    }
85
    else
205 by Brian Aker
uint32 -> uin32_t
86
      res=(int32_t) (int64_t) nr;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
87
  }
88
  if (error)
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
89
    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/
90
91
#ifdef WORDS_BIGENDIAN
92
  if (table->s->db_low_byte_first)
93
  {
94
    int4store(ptr,res);
95
  }
96
  else
97
#endif
98
    longstore(ptr,res);
99
  return error;
100
}
101
102
103
int Field_long::store(int64_t nr, bool unsigned_val)
104
{
105
  int error= 0;
205 by Brian Aker
uint32 -> uin32_t
106
  int32_t res;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
107
108
  if (unsigned_flag)
109
  {
110
    if (nr < 0 && !unsigned_val)
111
    {
112
      res=0;
113
      error= 1;
114
    }
365.2.5 by Monty Taylor
More ~0 removal.
115
    else if (nr > INT32_MAX)
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
116
    {
365.2.5 by Monty Taylor
More ~0 removal.
117
      res= INT32_MAX;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
118
      error= 1;
119
    }
120
    else
205 by Brian Aker
uint32 -> uin32_t
121
      res=(int32_t) (uint32_t) nr;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
122
  }
123
  else
124
  {
125
    if (nr < 0 && unsigned_val)
126
      nr= ((int64_t) INT32_MAX) + 1;           // Generate overflow
127
    if (nr < (int64_t) INT32_MIN) 
128
    {
205 by Brian Aker
uint32 -> uin32_t
129
      res=(int32_t) INT32_MIN;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
130
      error= 1;
131
    }
132
    else if (nr > (int64_t) INT32_MAX)
133
    {
205 by Brian Aker
uint32 -> uin32_t
134
      res=(int32_t) INT32_MAX;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
135
      error= 1;
136
    }
137
    else
205 by Brian Aker
uint32 -> uin32_t
138
      res=(int32_t) nr;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
139
  }
140
  if (error)
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
141
    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/
142
143
#ifdef WORDS_BIGENDIAN
144
  if (table->s->db_low_byte_first)
145
  {
146
    int4store(ptr,res);
147
  }
148
  else
149
#endif
150
    longstore(ptr,res);
151
  return error;
152
}
153
154
155
double Field_long::val_real(void)
156
{
205 by Brian Aker
uint32 -> uin32_t
157
  int32_t j;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
158
#ifdef WORDS_BIGENDIAN
159
  if (table->s->db_low_byte_first)
160
    j=sint4korr(ptr);
161
  else
162
#endif
163
    longget(j,ptr);
205 by Brian Aker
uint32 -> uin32_t
164
  return unsigned_flag ? (double) (uint32_t) j : (double) j;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
165
}
166
167
int64_t Field_long::val_int(void)
168
{
205 by Brian Aker
uint32 -> uin32_t
169
  int32_t j;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
170
  /* See the comment in Field_long::store(long long) */
171
  assert(table->in_use == current_thd);
172
#ifdef WORDS_BIGENDIAN
173
  if (table->s->db_low_byte_first)
174
    j=sint4korr(ptr);
175
  else
176
#endif
177
    longget(j,ptr);
205 by Brian Aker
uint32 -> uin32_t
178
  return unsigned_flag ? (int64_t) (uint32_t) j : (int64_t) j;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
179
}
180
181
String *Field_long::val_str(String *val_buffer,
182
			    String *val_ptr __attribute__((unused)))
183
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
184
  const CHARSET_INFO * const cs= &my_charset_bin;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
185
  uint length;
186
  uint mlength=max(field_length+1,12*cs->mbmaxlen);
187
  val_buffer->alloc(mlength);
188
  char *to=(char*) val_buffer->ptr();
205 by Brian Aker
uint32 -> uin32_t
189
  int32_t j;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
190
#ifdef WORDS_BIGENDIAN
191
  if (table->s->db_low_byte_first)
192
    j=sint4korr(ptr);
193
  else
194
#endif
195
    longget(j,ptr);
196
197
  if (unsigned_flag)
205 by Brian Aker
uint32 -> uin32_t
198
    length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32_t)j);
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
199
  else
200
    length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
201
  val_buffer->length(length);
216 by Brian Aker
Remove completely ZEROFILL
202
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
203
  return val_buffer;
204
}
205
206
207
bool Field_long::send_binary(Protocol *protocol)
208
{
209
  return protocol->store_long(Field_long::val_int());
210
}
211
212
int Field_long::cmp(const uchar *a_ptr, const uchar *b_ptr)
213
{
205 by Brian Aker
uint32 -> uin32_t
214
  int32_t a,b;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
215
#ifdef WORDS_BIGENDIAN
216
  if (table->s->db_low_byte_first)
217
  {
218
    a=sint4korr(a_ptr);
219
    b=sint4korr(b_ptr);
220
  }
221
  else
222
#endif
223
  {
224
    longget(a,a_ptr);
225
    longget(b,b_ptr);
226
  }
227
  if (unsigned_flag)
205 by Brian Aker
uint32 -> uin32_t
228
    return ((uint32_t) a < (uint32_t) b) ? -1 : ((uint32_t) a > (uint32_t) b) ? 1 : 0;
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
229
  return (a < b) ? -1 : (a > b) ? 1 : 0;
230
}
231
232
void Field_long::sort_string(uchar *to,uint length __attribute__((unused)))
233
{
234
#ifdef WORDS_BIGENDIAN
235
  if (!table->s->db_low_byte_first)
236
  {
237
    if (unsigned_flag)
238
      to[0] = ptr[0];
239
    else
240
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
241
    to[1]   = ptr[1];
242
    to[2]   = ptr[2];
243
    to[3]   = ptr[3];
244
  }
245
  else
246
#endif
247
  {
248
    if (unsigned_flag)
249
      to[0] = ptr[3];
250
    else
251
      to[0] = (char) (ptr[3] ^ 128);		/* Revers signbit */
252
    to[1]   = ptr[2];
253
    to[2]   = ptr[1];
254
    to[3]   = ptr[0];
255
  }
256
}
257
258
259
void Field_long::sql_type(String &res) const
260
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
261
  const CHARSET_INFO * const cs=res.charset();
221 by Brian Aker
First pass of removing length types for ints.
262
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "int"));
216 by Brian Aker
Remove completely ZEROFILL
263
  add_unsigned(res);
173.1.12 by Toru Maesaka
Moved out SHORT, LONG and INT64_T to field/
264
}
265