Drizzled Public API Documentation

decimal.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/charset.h>
00023 #include <drizzled/field.h>
00024 #include <drizzled/item/decimal.h>
00025 
00026 namespace drizzled {
00027 
00028 Item_decimal::Item_decimal(const char *str_arg, uint32_t length,
00029                            const charset_info_st * const charset)
00030 {
00031   decimal_value.store(E_DEC_FATAL_ERROR, str_arg, length, charset);
00032   name= (char*) str_arg;
00033   decimals= (uint8_t) decimal_value.frac;
00034   fixed= 1;
00035   max_length= class_decimal_precision_to_length(decimal_value.intg + decimals,
00036                                              decimals, unsigned_flag);
00037 }
00038 
00039 Item_decimal::Item_decimal(int64_t val, bool unsig)
00040 {
00041   int2_class_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value);
00042   decimals= (uint8_t) decimal_value.frac;
00043   fixed= 1;
00044   max_length= class_decimal_precision_to_length(decimal_value.intg + decimals,
00045                                              decimals, unsigned_flag);
00046 }
00047 
00048 
00049 Item_decimal::Item_decimal(double val, int, int)
00050 {
00051   double2_class_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
00052   decimals= (uint8_t) decimal_value.frac;
00053   fixed= 1;
00054   max_length= class_decimal_precision_to_length(decimal_value.intg + decimals,
00055                                              decimals, unsigned_flag);
00056 }
00057 
00058 Item_decimal::Item_decimal(const char *str, const type::Decimal *val_arg,
00059                            uint32_t decimal_par, uint32_t length)
00060 {
00061   class_decimal2decimal(val_arg, &decimal_value);
00062   name= (char*) str;
00063   decimals= (uint8_t) decimal_par;
00064   max_length= length;
00065   fixed= 1;
00066 }
00067 
00068 
00069 Item_decimal::Item_decimal(type::Decimal *value_par)
00070 {
00071   class_decimal2decimal(value_par, &decimal_value);
00072   decimals= (uint8_t) decimal_value.frac;
00073   fixed= 1;
00074   max_length= class_decimal_precision_to_length(decimal_value.intg + decimals,
00075                                              decimals, unsigned_flag);
00076 }
00077 
00078 
00079 Item_decimal::Item_decimal(const unsigned char *bin, int precision, int scale)
00080 {
00081   binary2_class_decimal(E_DEC_FATAL_ERROR, bin,
00082                     &decimal_value, precision, scale);
00083   decimals= (uint8_t) decimal_value.frac;
00084   fixed= 1;
00085   max_length= class_decimal_precision_to_length(precision, decimals,
00086                                              unsigned_flag);
00087 }
00088 
00089 int64_t Item_decimal::val_int()
00090 {
00091   int64_t result;
00092   decimal_value.val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
00093   return result;
00094 }
00095 
00096 double Item_decimal::val_real()
00097 {
00098   double result;
00099   class_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
00100   return result;
00101 }
00102 
00103 String *Item_decimal::val_str(String *result)
00104 {
00105   result->set_charset(&my_charset_bin);
00106   class_decimal2string(&decimal_value, 0, result);
00107   return result;
00108 }
00109 
00110 void Item_decimal::print(String *str)
00111 {
00112   class_decimal2string(&decimal_value, 0, &str_value);
00113   str->append(str_value);
00114 }
00115 
00116 bool Item_decimal::eq(const Item *item, bool) const
00117 {
00118   if (type() == item->type() && item->basic_const_item())
00119   {
00120     /*
00121       We need to cast off const to call val_decimal(). This should
00122       be OK for a basic constant. Additionally, we can pass 0 as
00123       a true decimal constant will return its internal decimal
00124       storage and ignore the argument.
00125     */
00126     Item *arg= (Item*) item;
00127     type::Decimal *value= arg->val_decimal(0);
00128     return !class_decimal_cmp(&decimal_value, value);
00129   }
00130   return 0;
00131 }
00132 
00133 
00134 void Item_decimal::set_decimal_value(type::Decimal *value_par)
00135 {
00136   class_decimal2decimal(value_par, &decimal_value);
00137   decimals= (uint8_t) decimal_value.frac;
00138   unsigned_flag= !decimal_value.sign();
00139   max_length= class_decimal_precision_to_length(decimal_value.intg + decimals,
00140                                              decimals, unsigned_flag);
00141 }
00142 
00143 int Item_decimal::save_in_field(Field *field, bool)
00144 {
00145   field->set_notnull();
00146   return field->store_decimal(&decimal_value);
00147 }
00148 
00149 
00150 } /* namespace drizzled */