~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/size.cc

  • Committer: Brian Aker
  • Date: 2010-12-20 10:21:10 UTC
  • mto: This revision was merged to the branch mainline in revision 2016.
  • Revision ID: brian@tangent.org-20101220102110-60oh6zarw5n9nhah
Fixing up a, somewhat, hidden unsigned type to solve a few issues around
numbers.

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
int Size::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
 
46
{
 
47
  int error= 0;
 
48
  char *end;
 
49
  uint64_t tmp;
 
50
 
 
51
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
52
 
 
53
  tmp= cs->cset->strntoull10rnd(cs, from, len, false, &end,&error);
 
54
  if (error == MY_ERRNO_ERANGE)
 
55
  {
 
56
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
57
    error= 1;
 
58
  }
 
59
  else if (getTable()->in_use->count_cuted_fields && check_int(cs, from, len, end, error))
 
60
  {
 
61
    error= 1;
 
62
  }
 
63
  else
 
64
  {
 
65
    error= 0;
 
66
  }
 
67
 
 
68
  int64_tstore(ptr,tmp);
 
69
 
 
70
  return error;
 
71
}
 
72
 
 
73
 
 
74
int Size::store(double nr)
 
75
{
 
76
  int error= 0;
 
77
  int64_t res;
 
78
 
 
79
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
80
 
 
81
  nr= rint(nr);
 
82
 
 
83
  if (nr <= (double) INT64_MIN)
 
84
  {
 
85
    res= INT64_MIN;
 
86
    error= (nr < (double) INT64_MIN);
 
87
  }
 
88
  else if (nr >= (double) (uint64_t) INT64_MAX)
 
89
  {
 
90
    res= INT64_MAX;
 
91
    error= (nr > (double) INT64_MAX);
 
92
  }
 
93
  else
 
94
  {
 
95
    res=(int64_t) nr;
 
96
  }
 
97
 
 
98
  if (error)
 
99
  {
 
100
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
101
    return 0;
 
102
  }
 
103
 
 
104
  int64_tstore(ptr, res);
 
105
 
 
106
  return error;
 
107
}
 
108
 
 
109
 
 
110
int Size::store(int64_t nr, bool arg)
 
111
{
 
112
  int error= 0;
 
113
 
 
114
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
115
 
 
116
  if (not arg and nr < 0)
 
117
  {
 
118
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
119
    return 0;
 
120
  }
 
121
 
 
122
  int64_tstore(ptr,nr);
 
123
 
 
124
  return error;
 
125
}
 
126
 
 
127
 
 
128
double Size::val_real(void)
 
129
{
 
130
  int64_t j;
 
131
 
 
132
  ASSERT_COLUMN_MARKED_FOR_READ;
 
133
 
 
134
  int64_tget(j,ptr);
 
135
 
 
136
  return (double) j;
 
137
}
 
138
 
 
139
 
 
140
int64_t Size::val_int(void)
 
141
{
 
142
  int64_t j;
 
143
 
 
144
  ASSERT_COLUMN_MARKED_FOR_READ;
 
145
 
 
146
  int64_tget(j,ptr);
 
147
 
 
148
  return j;
 
149
}
 
150
 
 
151
 
 
152
String *Size::val_str(String *val_buffer, String *)
 
153
{
 
154
  const CHARSET_INFO * const cs= &my_charset_bin;
 
155
  uint32_t length;
 
156
  uint32_t mlength= max(field_length+1,22*cs->mbmaxlen);
 
157
  val_buffer->alloc(mlength);
 
158
  char *to=(char*) val_buffer->ptr();
 
159
  int64_t j;
 
160
 
 
161
  ASSERT_COLUMN_MARKED_FOR_READ;
 
162
 
 
163
  int64_tget(j,ptr);
 
164
 
 
165
  length=(uint32_t) (cs->cset->int64_t10_to_str)(cs,to,mlength, -10, j);
 
166
  val_buffer->length(length);
 
167
 
 
168
  return val_buffer;
 
169
}
 
170
 
 
171
int Size::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
172
{
 
173
  int64_t a,b;
 
174
 
 
175
  int64_tget(a,a_ptr);
 
176
  int64_tget(b,b_ptr);
 
177
 
 
178
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
179
}
 
180
 
 
181
void Size::sort_string(unsigned char *to,uint32_t )
 
182
{
 
183
#ifdef WORDS_BIGENDIAN
 
184
  {
 
185
    to[0] = (char) (ptr[0] ^ 128);              /* Revers signbit */
 
186
    to[1]   = ptr[1];
 
187
    to[2]   = ptr[2];
 
188
    to[3]   = ptr[3];
 
189
    to[4]   = ptr[4];
 
190
    to[5]   = ptr[5];
 
191
    to[6]   = ptr[6];
 
192
    to[7]   = ptr[7];
 
193
  }
 
194
#else
 
195
  {
 
196
    to[0] = (char) (ptr[7] ^ 128);              /* Revers signbit */
 
197
    to[1]   = ptr[6];
 
198
    to[2]   = ptr[5];
 
199
    to[3]   = ptr[4];
 
200
    to[4]   = ptr[3];
 
201
    to[5]   = ptr[2];
 
202
    to[6]   = ptr[1];
 
203
    to[7]   = ptr[0];
 
204
  }
 
205
#endif
 
206
}
 
207
 
 
208
 
 
209
void Size::sql_type(String &res) const
 
210
{
 
211
  const CHARSET_INFO * const cs=res.charset();
 
212
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "unsigned integer"));
 
213
}
 
214
 
 
215
 
 
216
unsigned char *Size::pack(unsigned char* to, const unsigned char *from, uint32_t, bool)
 
217
{
 
218
  int64_t val;
 
219
 
 
220
  int64_tget(val, from);
 
221
  int64_tstore(to, val);
 
222
 
 
223
  return to + sizeof(val);
 
224
}
 
225
 
 
226
 
 
227
const unsigned char *Size::unpack(unsigned char* to, const unsigned char *from, uint32_t, bool)
 
228
{
 
229
  int64_t val;
 
230
 
 
231
  int64_tget(val, from);
 
232
  int64_tstore(to, val);
 
233
 
 
234
  return from + sizeof(val);
 
235
}
 
236
 
 
237
} /* namespace field */
 
238
} /* namespace drizzled */