00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <config.h>
00023 #include <drizzled/field/int32.h>
00024 #include <drizzled/error.h>
00025 #include <drizzled/table.h>
00026 #include <drizzled/session.h>
00027
00028 #include <math.h>
00029
00030 #include <algorithm>
00031
00032 using namespace std;
00033
00034 namespace drizzled {
00035 namespace field {
00036
00037
00038
00039
00040
00041 int Int32::store(const char *from,uint32_t len, const charset_info_st * const cs)
00042 {
00043 ASSERT_COLUMN_MARKED_FOR_WRITE;
00044 int64_t rnd;
00045 int error= get_int(cs, from, len, &rnd, UINT32_MAX, INT32_MIN, INT32_MAX);
00046 long store_tmp= (long) rnd;
00047 longstore(ptr, store_tmp);
00048 return error;
00049 }
00050
00051
00052 int Int32::store(double nr)
00053 {
00054 int error= 0;
00055 int32_t res;
00056 nr=rint(nr);
00057
00058 ASSERT_COLUMN_MARKED_FOR_WRITE;
00059
00060 if (nr < (double) INT32_MIN)
00061 {
00062 res=(int32_t) INT32_MIN;
00063 error= 1;
00064 }
00065 else if (nr > (double) INT32_MAX)
00066 {
00067 res=(int32_t) INT32_MAX;
00068 error= 1;
00069 }
00070 else
00071 res=(int32_t) (int64_t) nr;
00072
00073 if (error)
00074 set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
00075
00076 longstore(ptr,res);
00077
00078 return error;
00079 }
00080
00081
00082 int Int32::store(int64_t nr, bool unsigned_val)
00083 {
00084 int error= 0;
00085 int32_t res;
00086
00087 ASSERT_COLUMN_MARKED_FOR_WRITE;
00088
00089 if (nr < 0 && unsigned_val)
00090 nr= ((int64_t) INT32_MAX) + 1;
00091
00092 if (nr < (int64_t) INT32_MIN)
00093 {
00094 res=(int32_t) INT32_MIN;
00095 error= 1;
00096 }
00097 else if (nr > (int64_t) INT32_MAX)
00098 {
00099 res=(int32_t) INT32_MAX;
00100 error= 1;
00101 }
00102 else
00103 {
00104 res=(int32_t) nr;
00105 }
00106
00107 if (error)
00108 set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
00109
00110 longstore(ptr,res);
00111
00112 return error;
00113 }
00114
00115
00116 double Int32::val_real(void) const
00117 {
00118 int32_t j;
00119
00120 ASSERT_COLUMN_MARKED_FOR_READ;
00121
00122 longget(j,ptr);
00123
00124 return (double) j;
00125 }
00126
00127 int64_t Int32::val_int(void) const
00128 {
00129 int32_t j;
00130
00131 ASSERT_COLUMN_MARKED_FOR_READ;
00132
00133 longget(j,ptr);
00134
00135 return (int64_t) j;
00136 }
00137
00138 String *Int32::val_str(String *val_buffer, String *) const
00139 {
00140 const charset_info_st * const cs= &my_charset_bin;
00141 uint32_t length;
00142 uint32_t mlength= max(field_length+1,12*cs->mbmaxlen);
00143 val_buffer->alloc(mlength);
00144 char *to=(char*) val_buffer->ptr();
00145 int32_t j;
00146
00147 ASSERT_COLUMN_MARKED_FOR_READ;
00148
00149 longget(j,ptr);
00150
00151 length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
00152 val_buffer->length(length);
00153
00154 return val_buffer;
00155 }
00156
00157 int Int32::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
00158 {
00159 int32_t a,b;
00160
00161 longget(a,a_ptr);
00162 longget(b,b_ptr);
00163
00164 return (a < b) ? -1 : (a > b) ? 1 : 0;
00165 }
00166
00167 void Int32::sort_string(unsigned char *to,uint32_t )
00168 {
00169 #ifdef WORDS_BIGENDIAN
00170 {
00171 to[0] = (char) (ptr[0] ^ 128);
00172 to[1] = ptr[1];
00173 to[2] = ptr[2];
00174 to[3] = ptr[3];
00175 }
00176 #else
00177 {
00178 to[0] = (char) (ptr[3] ^ 128);
00179 to[1] = ptr[2];
00180 to[2] = ptr[1];
00181 to[3] = ptr[0];
00182 }
00183 #endif
00184 }
00185
00186
00187 unsigned char *Int32::pack(unsigned char* to, const unsigned char *from, uint32_t, bool)
00188 {
00189 int32_t val;
00190 longget(val, from);
00191
00192 longstore(to, val);
00193 return to + sizeof(val);
00194 }
00195
00196
00197 const unsigned char *Int32::unpack(unsigned char* to, const unsigned char *from, uint32_t, bool)
00198 {
00199 int32_t val;
00200 longget(val, from);
00201
00202 longstore(to, val);
00203
00204 return from + sizeof(val);
00205 }
00206
00207 }
00208 }