Drizzled Public API Documentation

int32.cc
00001 /* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 MySQL
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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 {
00036 
00037 namespace field
00038 {
00039 
00040 /****************************************************************************
00041  ** Int32
00042  ****************************************************************************/
00043 
00044   int Int32::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
00045   {
00046     long store_tmp;
00047     int error;
00048     int64_t rnd;
00049 
00050     ASSERT_COLUMN_MARKED_FOR_WRITE;
00051 
00052     error= get_int(cs, from, len, &rnd, UINT32_MAX, INT32_MIN, INT32_MAX);
00053     store_tmp= (long) rnd;
00054     longstore(ptr, store_tmp);
00055 
00056     return error;
00057   }
00058 
00059 
00060   int Int32::store(double nr)
00061   {
00062     int error= 0;
00063     int32_t res;
00064     nr=rint(nr);
00065 
00066     ASSERT_COLUMN_MARKED_FOR_WRITE;
00067 
00068     if (nr < (double) INT32_MIN)
00069     {
00070       res=(int32_t) INT32_MIN;
00071       error= 1;
00072     }
00073     else if (nr > (double) INT32_MAX)
00074     {
00075       res=(int32_t) INT32_MAX;
00076       error= 1;
00077     }
00078     else
00079       res=(int32_t) (int64_t) nr;
00080 
00081     if (error)
00082       set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
00083 
00084     longstore(ptr,res);
00085 
00086     return error;
00087   }
00088 
00089 
00090   int Int32::store(int64_t nr, bool unsigned_val)
00091   {
00092     int error= 0;
00093     int32_t res;
00094 
00095     ASSERT_COLUMN_MARKED_FOR_WRITE;
00096 
00097     if (nr < 0 && unsigned_val)
00098       nr= ((int64_t) INT32_MAX) + 1;           // Generate overflow
00099 
00100     if (nr < (int64_t) INT32_MIN)
00101     {
00102       res=(int32_t) INT32_MIN;
00103       error= 1;
00104     }
00105     else if (nr > (int64_t) INT32_MAX)
00106     {
00107       res=(int32_t) INT32_MAX;
00108       error= 1;
00109     }
00110     else
00111     {
00112       res=(int32_t) nr;
00113     }
00114 
00115     if (error)
00116       set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
00117 
00118     longstore(ptr,res);
00119 
00120     return error;
00121   }
00122 
00123 
00124   double Int32::val_real(void) const
00125   {
00126     int32_t j;
00127 
00128     ASSERT_COLUMN_MARKED_FOR_READ;
00129 
00130     longget(j,ptr);
00131 
00132     return (double) j;
00133   }
00134 
00135   int64_t Int32::val_int(void) const
00136   {
00137     int32_t j;
00138 
00139     ASSERT_COLUMN_MARKED_FOR_READ;
00140 
00141     longget(j,ptr);
00142 
00143     return (int64_t) j;
00144   }
00145 
00146   String *Int32::val_str(String *val_buffer, String *) const
00147   {
00148     const CHARSET_INFO * const cs= &my_charset_bin;
00149     uint32_t length;
00150     uint32_t mlength= max(field_length+1,12*cs->mbmaxlen);
00151     val_buffer->alloc(mlength);
00152     char *to=(char*) val_buffer->ptr();
00153     int32_t j;
00154 
00155     ASSERT_COLUMN_MARKED_FOR_READ;
00156 
00157     longget(j,ptr);
00158 
00159     length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
00160     val_buffer->length(length);
00161 
00162     return val_buffer;
00163   }
00164 
00165   int Int32::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
00166   {
00167     int32_t a,b;
00168 
00169     longget(a,a_ptr);
00170     longget(b,b_ptr);
00171 
00172     return (a < b) ? -1 : (a > b) ? 1 : 0;
00173   }
00174 
00175   void Int32::sort_string(unsigned char *to,uint32_t )
00176   {
00177 #ifdef WORDS_BIGENDIAN
00178     {
00179       to[0] = (char) (ptr[0] ^ 128);    /* Revers signbit */
00180       to[1]   = ptr[1];
00181       to[2]   = ptr[2];
00182       to[3]   = ptr[3];
00183     }
00184 #else
00185     {
00186       to[0] = (char) (ptr[3] ^ 128);    /* Revers signbit */
00187       to[1]   = ptr[2];
00188       to[2]   = ptr[1];
00189       to[3]   = ptr[0];
00190     }
00191 #endif
00192   }
00193 
00194 
00195   void Int32::sql_type(String &res) const
00196   {
00197     const CHARSET_INFO * const cs=res.charset();
00198     res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "int"));
00199   }
00200 
00201   unsigned char *Int32::pack(unsigned char* to, const unsigned char *from, uint32_t, bool)
00202   {
00203     int32_t val;
00204     longget(val, from);
00205 
00206     longstore(to, val);
00207     return to + sizeof(val);
00208   }
00209 
00210 
00211   const unsigned char *Int32::unpack(unsigned char* to, const unsigned char *from, uint32_t, bool)
00212   {
00213     int32_t val;
00214     longget(val, from);
00215 
00216     longstore(to, val);
00217 
00218     return from + sizeof(val);
00219   }
00220 
00221 } /* namespace field */
00222 } /* namespace drizzled */