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