Drizzled Public API Documentation

int32.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/int32.h>
24 #include <drizzled/error.h>
25 #include <drizzled/table.h>
26 #include <drizzled/session.h>
27 
28 #include <math.h>
29 
30 #include <algorithm>
31 
32 using namespace std;
33 
34 namespace drizzled {
35 namespace field {
36 
37 /****************************************************************************
38  ** Int32
39  ****************************************************************************/
40 
41  int Int32::store(const char *from,uint32_t len, const charset_info_st * const cs)
42  {
43  ASSERT_COLUMN_MARKED_FOR_WRITE;
44  int64_t rnd;
45  int error= get_int(cs, from, len, &rnd, UINT32_MAX, INT32_MIN, INT32_MAX);
46  long store_tmp= (long) rnd;
47  longstore(ptr, store_tmp);
48  return error;
49  }
50 
51 
52  int Int32::store(double nr)
53  {
54  int error= 0;
55  int32_t res;
56  nr=rint(nr);
57 
58  ASSERT_COLUMN_MARKED_FOR_WRITE;
59 
60  if (nr < (double) INT32_MIN)
61  {
62  res=(int32_t) INT32_MIN;
63  error= 1;
64  }
65  else if (nr > (double) INT32_MAX)
66  {
67  res=(int32_t) INT32_MAX;
68  error= 1;
69  }
70  else
71  res=(int32_t) (int64_t) nr;
72 
73  if (error)
74  set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
75 
76  longstore(ptr,res);
77 
78  return error;
79  }
80 
81 
82  int Int32::store(int64_t nr, bool unsigned_val)
83  {
84  int error= 0;
85  int32_t res;
86 
87  ASSERT_COLUMN_MARKED_FOR_WRITE;
88 
89  if (nr < 0 && unsigned_val)
90  nr= ((int64_t) INT32_MAX) + 1; // Generate overflow
91 
92  if (nr < (int64_t) INT32_MIN)
93  {
94  res=(int32_t) INT32_MIN;
95  error= 1;
96  }
97  else if (nr > (int64_t) INT32_MAX)
98  {
99  res=(int32_t) INT32_MAX;
100  error= 1;
101  }
102  else
103  {
104  res=(int32_t) nr;
105  }
106 
107  if (error)
108  set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
109 
110  longstore(ptr,res);
111 
112  return error;
113  }
114 
115 
116  double Int32::val_real(void) const
117  {
118  int32_t j;
119 
120  ASSERT_COLUMN_MARKED_FOR_READ;
121 
122  longget(j,ptr);
123 
124  return (double) j;
125  }
126 
127  int64_t Int32::val_int(void) const
128  {
129  int32_t j;
130 
131  ASSERT_COLUMN_MARKED_FOR_READ;
132 
133  longget(j,ptr);
134 
135  return (int64_t) j;
136  }
137 
138  String *Int32::val_str(String *val_buffer, String *) const
139  {
140  const charset_info_st * const cs= &my_charset_bin;
141  uint32_t length;
142  uint32_t mlength= max(field_length+1,12*cs->mbmaxlen);
143  val_buffer->alloc(mlength);
144  char *to=(char*) val_buffer->ptr();
145  int32_t j;
146 
147  ASSERT_COLUMN_MARKED_FOR_READ;
148 
149  longget(j,ptr);
150 
151  length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
152  val_buffer->length(length);
153 
154  return val_buffer;
155  }
156 
157  int Int32::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
158  {
159  int32_t a,b;
160 
161  longget(a,a_ptr);
162  longget(b,b_ptr);
163 
164  return (a < b) ? -1 : (a > b) ? 1 : 0;
165  }
166 
167  void Int32::sort_string(unsigned char *to,uint32_t )
168  {
169 #ifdef WORDS_BIGENDIAN
170  {
171  to[0] = (char) (ptr[0] ^ 128); /* Revers signbit */
172  to[1] = ptr[1];
173  to[2] = ptr[2];
174  to[3] = ptr[3];
175  }
176 #else
177  {
178  to[0] = (char) (ptr[3] ^ 128); /* Revers signbit */
179  to[1] = ptr[2];
180  to[2] = ptr[1];
181  to[3] = ptr[0];
182  }
183 #endif
184  }
185 
186 
187  unsigned char *Int32::pack(unsigned char* to, const unsigned char *from, uint32_t, bool)
188  {
189  int32_t val;
190  longget(val, from);
191 
192  longstore(to, val);
193  return to + sizeof(val);
194  }
195 
196 
197  const unsigned char *Int32::unpack(unsigned char* to, const unsigned char *from, uint32_t, bool)
198  {
199  int32_t val;
200  longget(val, from);
201 
202  longstore(to, val);
203 
204  return from + sizeof(val);
205  }
206 
207 } /* namespace field */
208 } /* namespace drizzled */
TODO: Rename this file - func.h is stupid.