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