Drizzled Public API Documentation

int64.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/int64.h>
00024 #include <drizzled/error.h>
00025 #include <drizzled/table.h>
00026 #include <drizzled/session.h>
00027 #include <drizzled/internal/my_sys.h>
00028 
00029 #include <math.h>
00030 
00031 #include <algorithm>
00032 
00033 using namespace std;
00034 
00035 namespace drizzled
00036 {
00037 
00038 namespace field
00039 {
00040 
00041 /****************************************************************************
00042   Field type Int64 int (8 bytes)
00043  ****************************************************************************/
00044 
00045 int Int64::store(const char *from,uint32_t len, const CHARSET_INFO * const cs)
00046 {
00047   int error= 0;
00048   char *end;
00049   uint64_t tmp;
00050 
00051   ASSERT_COLUMN_MARKED_FOR_WRITE;
00052 
00053   tmp= cs->cset->strntoull10rnd(cs, from, len, false, &end,&error);
00054   if (error == MY_ERRNO_ERANGE)
00055   {
00056     set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
00057     error= 1;
00058   }
00059   else if (getTable()->in_use->count_cuted_fields && check_int(cs, from, len, end, error))
00060   {
00061     error= 1;
00062   }
00063   else
00064   {
00065     error= 0;
00066   }
00067 
00068   int64_tstore(ptr,tmp);
00069 
00070   return error;
00071 }
00072 
00073 
00074 int Int64::store(double nr)
00075 {
00076   int error= 0;
00077   int64_t res;
00078 
00079   ASSERT_COLUMN_MARKED_FOR_WRITE;
00080 
00081   nr= rint(nr);
00082 
00083   if (nr <= (double) INT64_MIN)
00084   {
00085     res= INT64_MIN;
00086     error= (nr < (double) INT64_MIN);
00087   }
00088   else if (nr >= (double) (uint64_t) INT64_MAX)
00089   {
00090     res= INT64_MAX;
00091     error= (nr > (double) INT64_MAX);
00092   }
00093   else
00094   {
00095     res=(int64_t) nr;
00096   }
00097 
00098   if (error)
00099     set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
00100 
00101   int64_tstore(ptr, res);
00102 
00103   return error;
00104 }
00105 
00106 
00107 int Int64::store(int64_t nr, bool arg)
00108 {
00109   int error= 0;
00110 
00111   ASSERT_COLUMN_MARKED_FOR_WRITE;
00112   if (arg and (nr < 0)) // Only a partial fix for overflow
00113   {
00114     set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
00115     error= 1;
00116   }
00117 
00118   int64_tstore(ptr,nr);
00119 
00120   return error;
00121 }
00122 
00123 
00124 double Int64::val_real(void) const
00125 {
00126   int64_t j;
00127 
00128   ASSERT_COLUMN_MARKED_FOR_READ;
00129 
00130   int64_tget(j,ptr);
00131   /* The following is open coded to avoid a bug in gcc 3.3 */
00132 
00133   return (double) j;
00134 }
00135 
00136 
00137 int64_t Int64::val_int(void) const
00138 {
00139   int64_t j;
00140 
00141   ASSERT_COLUMN_MARKED_FOR_READ;
00142 
00143   int64_tget(j,ptr);
00144 
00145   return j;
00146 }
00147 
00148 
00149 String *Int64::val_str(String *val_buffer, String *) const
00150 {
00151   const CHARSET_INFO * const cs= &my_charset_bin;
00152   uint32_t length;
00153   uint32_t mlength= max(field_length+1,22*cs->mbmaxlen);
00154   val_buffer->alloc(mlength);
00155   char *to=(char*) val_buffer->ptr();
00156   int64_t j;
00157 
00158   ASSERT_COLUMN_MARKED_FOR_READ;
00159 
00160   int64_tget(j,ptr);
00161 
00162   length=(uint32_t) (cs->cset->int64_t10_to_str)(cs,to,mlength, -10, j);
00163   val_buffer->length(length);
00164 
00165   return val_buffer;
00166 }
00167 
00168 int Int64::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
00169 {
00170   int64_t a,b;
00171 
00172   int64_tget(a,a_ptr);
00173   int64_tget(b,b_ptr);
00174 
00175   return (a < b) ? -1 : (a > b) ? 1 : 0;
00176 }
00177 
00178 void Int64::sort_string(unsigned char *to,uint32_t )
00179 {
00180 #ifdef WORDS_BIGENDIAN
00181   {
00182     to[0] = (char) (ptr[0] ^ 128);    /* Revers signbit */
00183     to[1]   = ptr[1];
00184     to[2]   = ptr[2];
00185     to[3]   = ptr[3];
00186     to[4]   = ptr[4];
00187     to[5]   = ptr[5];
00188     to[6]   = ptr[6];
00189     to[7]   = ptr[7];
00190   }
00191 #else
00192   {
00193     to[0] = (char) (ptr[7] ^ 128);    /* Revers signbit */
00194     to[1]   = ptr[6];
00195     to[2]   = ptr[5];
00196     to[3]   = ptr[4];
00197     to[4]   = ptr[3];
00198     to[5]   = ptr[2];
00199     to[6]   = ptr[1];
00200     to[7]   = ptr[0];
00201   }
00202 #endif
00203 }
00204 
00205 
00206 void Int64::sql_type(String &res) const
00207 {
00208   const CHARSET_INFO * const cs=res.charset();
00209   res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), "bigint"));
00210 }
00211 
00212 
00213 unsigned char *Int64::pack(unsigned char* to, const unsigned char *from, uint32_t, bool)
00214 {
00215   int64_t val;
00216 
00217   int64_tget(val, from);
00218   int64_tstore(to, val);
00219 
00220   return to + sizeof(val);
00221 }
00222 
00223 
00224 const unsigned char *Int64::unpack(unsigned char* to, const unsigned char *from, uint32_t, bool)
00225 {
00226   int64_t val;
00227 
00228   int64_tget(val, from);
00229   int64_tstore(to, val);
00230 
00231   return from + sizeof(val);
00232 }
00233 
00234 } /* namespace field */
00235 } /* namespace drizzled */