~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/size.cc

Merge Joe, plus I updated the tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
22
 
#include "config.h"
23
 
#include <drizzled/field/size.h>
24
 
#include <drizzled/error.h>
25
 
#include <drizzled/table.h>
26
 
#include <drizzled/session.h>
27
 
#include "drizzled/internal/my_sys.h"
28
 
 
29
 
#include <math.h>
30
 
 
31
 
#include <algorithm>
32
 
 
33
 
using namespace std;
34
 
 
35
 
namespace drizzled
36
 
{
37
 
 
38
 
namespace field
39
 
{
40
 
 
41
 
/****************************************************************************
42
 
  Field type Size int (8 bytes)
43
 
 ****************************************************************************/
44
 
 
45
 
Size::Size(unsigned char *ptr_arg, uint32_t len_arg,
46
 
           unsigned char *null_ptr_arg,
47
 
           unsigned char null_bit_arg,
48
 
           enum utype unireg_check_arg,
49
 
           const char *field_name_arg) :
50
 
  Field_num(ptr_arg,
51
 
            len_arg,
52
 
            null_ptr_arg,
53
 
            null_bit_arg,
54
 
            unireg_check_arg,
55
 
            field_name_arg,
56
 
            0, false, true)
57
 
{
58
 
  flags|= UNSIGNED_FLAG;
59
 
}
60
 
 
61
 
Size::Size(uint32_t len_arg,bool maybe_null_arg,
62
 
           const char *field_name_arg,
63
 
           bool unsigned_arg) :
64
 
  Field_num((unsigned char*) 0,
65
 
            len_arg, maybe_null_arg ? (unsigned char*) "": 0,
66
 
            0,
67
 
            NONE,
68
 
            field_name_arg,
69
 
            0,
70
 
            0,
71
 
            unsigned_arg)
72
 
{
73
 
  flags|= UNSIGNED_FLAG;
74
 
  assert(unsigned_arg);
75
 
}
76
 
 
77
 
int Size::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
78
 
{
79
 
  int error= 0;
80
 
  char *end;
81
 
  uint64_t tmp;
82
 
 
83
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
84
 
 
85
 
  tmp= cs->cset->strntoull10rnd(cs, from, len, false, &end,&error);
86
 
  if (error == MY_ERRNO_ERANGE)
87
 
  {
88
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
89
 
    error= 1;
90
 
  }
91
 
  else if (getTable()->in_use->count_cuted_fields && check_int(cs, from, len, end, error))
92
 
  {
93
 
    error= 1;
94
 
  }
95
 
  else
96
 
  {
97
 
    error= 0;
98
 
  }
99
 
 
100
 
  int64_tstore(ptr,tmp);
101
 
 
102
 
  return error;
103
 
}
104
 
 
105
 
 
106
 
int Size::store(double nr)
107
 
{
108
 
  int error= 0;
109
 
  int64_t res;
110
 
 
111
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
112
 
 
113
 
  nr= rint(nr);
114
 
 
115
 
  if (nr <= (double) INT64_MIN)
116
 
  {
117
 
    res= INT64_MIN;
118
 
    error= (nr < (double) INT64_MIN);
119
 
  }
120
 
  else if (nr >= (double) (uint64_t) INT64_MAX)
121
 
  {
122
 
    res= INT64_MAX;
123
 
    error= (nr > (double) INT64_MAX);
124
 
  }
125
 
  else
126
 
  {
127
 
    res=(int64_t) nr;
128
 
  }
129
 
 
130
 
  if (error)
131
 
  {
132
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
133
 
    return 0;
134
 
  }
135
 
 
136
 
  int64_tstore(ptr, res);
137
 
 
138
 
  return error;
139
 
}
140
 
 
141
 
 
142
 
int Size::store(int64_t nr, bool arg)
143
 
{
144
 
  int error= 0;
145
 
 
146
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
147
 
 
148
 
  if (not arg and nr < 0)
149
 
  {
150
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
151
 
    return 0;
152
 
  }
153
 
 
154
 
  int64_tstore(ptr,nr);
155
 
 
156
 
  return error;
157
 
}
158
 
 
159
 
 
160
 
double Size::val_real(void)
161
 
{
162
 
  int64_t j;
163
 
 
164
 
  ASSERT_COLUMN_MARKED_FOR_READ;
165
 
 
166
 
  int64_tget(j,ptr);
167
 
 
168
 
  return (double) j;
169
 
}
170
 
 
171
 
 
172
 
int64_t Size::val_int(void)
173
 
{
174
 
  int64_t j;
175
 
 
176
 
  ASSERT_COLUMN_MARKED_FOR_READ;
177
 
 
178
 
  int64_tget(j,ptr);
179
 
 
180
 
  return j;
181
 
}
182
 
 
183
 
 
184
 
String *Size::val_str(String *val_buffer, String *)
185
 
{
186
 
  const CHARSET_INFO * const cs= &my_charset_bin;
187
 
  uint32_t length;
188
 
  uint32_t mlength= max(field_length+1,22*cs->mbmaxlen);
189
 
  val_buffer->alloc(mlength);
190
 
  char *to=(char*) val_buffer->ptr();
191
 
  int64_t j;
192
 
 
193
 
  ASSERT_COLUMN_MARKED_FOR_READ;
194
 
 
195
 
  int64_tget(j,ptr);
196
 
 
197
 
  length=(uint32_t) (cs->cset->int64_t10_to_str)(cs,to,mlength, -10, j);
198
 
  val_buffer->length(length);
199
 
 
200
 
  return val_buffer;
201
 
}
202
 
 
203
 
int Size::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
204
 
{
205
 
  int64_t a,b;
206
 
 
207
 
  int64_tget(a,a_ptr);
208
 
  int64_tget(b,b_ptr);
209
 
 
210
 
  return (a < b) ? -1 : (a > b) ? 1 : 0;
211
 
}
212
 
 
213
 
void Size::sort_string(unsigned char *to,uint32_t )
214
 
{
215
 
#ifdef WORDS_BIGENDIAN
216
 
  {
217
 
    to[0] = (char) (ptr[0] ^ 128);              /* Revers signbit */
218
 
    to[1]   = ptr[1];
219
 
    to[2]   = ptr[2];
220
 
    to[3]   = ptr[3];
221
 
    to[4]   = ptr[4];
222
 
    to[5]   = ptr[5];
223
 
    to[6]   = ptr[6];
224
 
    to[7]   = ptr[7];
225
 
  }
226
 
#else
227
 
  {
228
 
    to[0] = (char) (ptr[7] ^ 128);              /* Revers signbit */
229
 
    to[1]   = ptr[6];
230
 
    to[2]   = ptr[5];
231
 
    to[3]   = ptr[4];
232
 
    to[4]   = ptr[3];
233
 
    to[5]   = ptr[2];
234
 
    to[6]   = ptr[1];
235
 
    to[7]   = ptr[0];
236
 
  }
237
 
#endif
238
 
}
239
 
 
240
 
 
241
 
void Size::sql_type(String &res) const
242
 
{
243
 
  const CHARSET_INFO * const cs=res.charset();
244
 
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "unsigned integer"));
245
 
}
246
 
 
247
 
 
248
 
unsigned char *Size::pack(unsigned char* to, const unsigned char *from, uint32_t, bool)
249
 
{
250
 
  int64_t val;
251
 
 
252
 
  int64_tget(val, from);
253
 
  int64_tstore(to, val);
254
 
 
255
 
  return to + sizeof(val);
256
 
}
257
 
 
258
 
 
259
 
const unsigned char *Size::unpack(unsigned char* to, const unsigned char *from, uint32_t, bool)
260
 
{
261
 
  int64_t val;
262
 
 
263
 
  int64_tget(val, from);
264
 
  int64_tstore(to, val);
265
 
 
266
 
  return from + sizeof(val);
267
 
}
268
 
 
269
 
} /* namespace field */
270
 
} /* namespace drizzled */