Drizzled Public API Documentation

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