Drizzled Public API Documentation

user_var_entry.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 Sun Microsystems, Inc.
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; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 
00022 #include <drizzled/session.h>
00023 #include <drizzled/internal/m_string.h>
00024 #include <drizzled/user_var_entry.h>
00025 
00026 namespace drizzled
00027 {
00028 
00031 double user_var_entry::val_real(bool *null_value)
00032 {
00033   if ((*null_value= (value == 0)))
00034     return 0.0;
00035 
00036   switch (type) {
00037   case REAL_RESULT:
00038     return *(double*) value;
00039 
00040   case INT_RESULT:
00041     return (double) *(int64_t*) value;
00042 
00043   case DECIMAL_RESULT:
00044     {
00045       double result;
00046       class_decimal2double(E_DEC_FATAL_ERROR, (type::Decimal *)value, &result);
00047       return result;
00048     }
00049 
00050   case STRING_RESULT:
00051     return internal::my_atof(value);                      // This is null terminated
00052 
00053   case ROW_RESULT:
00054     assert(1);        // Impossible
00055     break;
00056   }
00057   return 0.0;         // Impossible
00058 }
00059 
00060 
00063 int64_t user_var_entry::val_int(bool *null_value) const
00064 {
00065   if ((*null_value= (value == 0)))
00066     return 0L;
00067 
00068   switch (type) {
00069   case REAL_RESULT:
00070     return (int64_t) *(double*) value;
00071 
00072   case INT_RESULT:
00073     return *(int64_t*) value;
00074 
00075   case DECIMAL_RESULT:
00076     {
00077       int64_t result;
00078       ((type::Decimal *)(value))->val_int32(E_DEC_FATAL_ERROR, 0, &result);
00079       return result;
00080     }
00081 
00082   case STRING_RESULT:
00083     {
00084       int error;
00085       return internal::my_strtoll10(value, (char**) 0, &error);// String is null terminated
00086     }
00087 
00088   case ROW_RESULT:
00089     assert(1);        // Impossible
00090     break;
00091   }
00092 
00093   return 0L;          // Impossible
00094 }
00095 
00096 
00099 String *user_var_entry::val_str(bool *null_value, String *str,
00100                                 uint32_t decimals)
00101 {
00102   if ((*null_value= (value == 0)))
00103     return (String*) 0;
00104 
00105   switch (type) {
00106   case REAL_RESULT:
00107     str->set_real(*(double*) value, decimals, &my_charset_bin);
00108     break;
00109 
00110   case INT_RESULT:
00111     if (!unsigned_flag)
00112       str->set(*(int64_t*) value, &my_charset_bin);
00113     else
00114       str->set(*(uint64_t*) value, &my_charset_bin);
00115     break;
00116 
00117   case DECIMAL_RESULT:
00118     class_decimal2string((type::Decimal *)value, 0, str);
00119     break;
00120 
00121   case STRING_RESULT:
00122     if (str->copy(value, length, collation.collation))
00123       str= 0;         // EOM error
00124 
00125   case ROW_RESULT:
00126     assert(1);        // Impossible
00127     break;
00128   }
00129 
00130   return(str);
00131 }
00132 
00135 type::Decimal *user_var_entry::val_decimal(bool *null_value, type::Decimal *val)
00136 {
00137   if ((*null_value= (value == 0)))
00138     return 0;
00139 
00140   switch (type) {
00141   case REAL_RESULT:
00142     double2_class_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
00143     break;
00144 
00145   case INT_RESULT:
00146     int2_class_decimal(E_DEC_FATAL_ERROR, *(int64_t*) value, 0, val);
00147     break;
00148 
00149   case DECIMAL_RESULT:
00150     val= (type::Decimal *)value;
00151     break;
00152 
00153   case STRING_RESULT:
00154     val->store(E_DEC_FATAL_ERROR, value, length, collation.collation);
00155     break;
00156 
00157   case ROW_RESULT:
00158     assert(1);        // Impossible
00159     break;
00160   }
00161 
00162   return(val);
00163 }
00164 
00185 bool user_var_entry::update_hash(bool set_null, void *ptr, uint32_t arg_length,
00186                                  Item_result arg_type, const CHARSET_INFO * const cs, Derivation dv,
00187                                  bool unsigned_arg)
00188 {
00189   if (set_null)
00190   {
00191     if (value)
00192     {
00193       assert(length && size);
00194       free(value);
00195       value= NULL;
00196       length= 0;
00197       size= 0;
00198     }
00199   }
00200   else
00201   {
00202     size_t needed_size= arg_length + ((arg_type == STRING_RESULT) ? 1 : 0);
00203 
00204     if (needed_size > size)
00205     {
00206       char *new_ptr;
00207 
00208       new_ptr= (char *)realloc(value, needed_size);
00209 
00210       if (new_ptr == NULL)
00211         return true;
00212 
00213       value= new_ptr;
00214       size= needed_size;
00215     }
00216 
00217     if (arg_type == STRING_RESULT)
00218       value[arg_length]= 0;     // Store end \0
00219 
00220     memcpy(value, ptr, arg_length);
00221     if (arg_type == DECIMAL_RESULT)
00222       ((type::Decimal*)value)->fix_buffer_pointer();
00223     length= arg_length;
00224     collation.set(cs, dv);
00225     unsigned_flag= unsigned_arg;
00226   }
00227   type= arg_type;
00228 
00229   return false;
00230 }
00231 
00232 } /* namespace drizzled */