Drizzled Public API Documentation

hybrid_type_traits_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/definitions.h>
00023 #include <drizzled/field.h>
00024 #include <drizzled/hybrid_type.h>
00025 #include <drizzled/hybrid_type_traits_decimal.h>
00026 #include <drizzled/item.h>
00027 
00028 #include <algorithm>
00029 
00030 using namespace std;
00031 namespace drizzled
00032 {
00033 
00034 /* Hybrid_type_traits_decimal */
00035 static const Hybrid_type_traits_decimal decimal_traits_instance;
00036 
00037 
00038 Item_result Hybrid_type_traits_decimal::type() const { return DECIMAL_RESULT; }
00039 
00040 
00041 void
00042 Hybrid_type_traits_decimal::fix_length_and_dec(Item *item, Item *arg) const
00043 {
00044   item->decimals= arg->decimals;
00045   item->max_length= min(arg->max_length + DECIMAL_LONGLONG_DIGITS,
00046                         (unsigned int)DECIMAL_MAX_STR_LENGTH);
00047 }
00048 
00049 
00050 void Hybrid_type_traits_decimal::set_zero(Hybrid_type *val) const
00051 {
00052   val->dec_buf[0].set_zero();
00053   val->used_dec_buf_no= 0;
00054 }
00055 
00056 
00057 void Hybrid_type_traits_decimal::add(Hybrid_type *val, Field *f) const
00058 {
00059   class_decimal_add(E_DEC_FATAL_ERROR,
00060                  &val->dec_buf[val->used_dec_buf_no ^ 1],
00061                  &val->dec_buf[val->used_dec_buf_no],
00062                  f->val_decimal(&val->dec_buf[2]));
00063   val->used_dec_buf_no^= 1;
00064 }
00065 
00066 
00071 void Hybrid_type_traits_decimal::div(Hybrid_type *val, uint64_t u) const
00072 {
00073   int2_class_decimal(E_DEC_FATAL_ERROR, u, true, &val->dec_buf[2]);
00074   /* XXX: what is '4' for scale? */
00075   class_decimal_div(E_DEC_FATAL_ERROR,
00076                  &val->dec_buf[val->used_dec_buf_no ^ 1],
00077                  &val->dec_buf[val->used_dec_buf_no],
00078                  &val->dec_buf[2], 4);
00079   val->used_dec_buf_no^= 1;
00080 }
00081 
00082 
00083 int64_t
00084 Hybrid_type_traits_decimal::val_int(Hybrid_type *val, bool unsigned_flag) const
00085 {
00086   int64_t result;
00087   val->dec_buf[val->used_dec_buf_no].val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
00088 
00089   return result;
00090 }
00091 
00092 
00093 double
00094 Hybrid_type_traits_decimal::val_real(Hybrid_type *val) const
00095 {
00096   class_decimal2double(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
00097                     &val->real);
00098   return val->real;
00099 }
00100 
00101 
00102 type::Decimal *Hybrid_type_traits_decimal::val_decimal(Hybrid_type *val,
00103                                                     type::Decimal *) const
00104 { return &val->dec_buf[val->used_dec_buf_no]; }
00105 
00106 
00107 String *
00108 Hybrid_type_traits_decimal::val_str(Hybrid_type *val, String *to,
00109                                     uint8_t decimals) const
00110 {
00111   class_decimal_round(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
00112                    decimals, false, &val->dec_buf[2]);
00113   class_decimal2string(&val->dec_buf[2], 0, to);
00114   return to;
00115 }
00116 
00117 
00118 const Hybrid_type_traits_decimal *Hybrid_type_traits_decimal::instance()
00119 {
00120   return &decimal_traits_instance;
00121 }
00122 
00123 
00124 Hybrid_type_traits_decimal::Hybrid_type_traits_decimal()
00125 {}
00126 
00127 } /* namespace drizzled */