Drizzled Public API Documentation

hybrid_type_traits.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/field.h>
00023 #include <drizzled/hybrid_type.h>
00024 #include <drizzled/hybrid_type_traits.h>
00025 #include <drizzled/item.h>
00026 
00027 #include <math.h>
00028 
00029 namespace drizzled
00030 {
00031 
00032 static const Hybrid_type_traits real_traits_instance;
00033 
00034 Item_result Hybrid_type_traits::type() const
00035 {
00036   return REAL_RESULT;
00037 }
00038 
00039 
00040 void Hybrid_type_traits::fix_length_and_dec(Item *item, Item *arg) const
00041 {
00042   item->decimals= NOT_FIXED_DEC;
00043   item->max_length= item->float_length(arg->decimals);
00044 }
00045 
00046 
00047 /* Hybrid_type operations. */
00048 void Hybrid_type_traits::set_zero(Hybrid_type *val) const
00049 {
00050   val->real= 0.0;
00051 }
00052 
00053 
00054 void Hybrid_type_traits::add(Hybrid_type *val, Field *f) const
00055 {
00056   val->real+= f->val_real();
00057 }
00058 
00059 
00060 void Hybrid_type_traits::div(Hybrid_type *val, uint64_t u) const
00061 {
00062   val->real/= uint64_t2double(u);
00063 }
00064 
00065 
00066 int64_t Hybrid_type_traits::val_int(Hybrid_type *val,
00067                                     bool) const
00068 {
00069   return (int64_t) rint(val->real);
00070 }
00071 
00072 
00073 double Hybrid_type_traits::val_real(Hybrid_type *val) const
00074 {
00075   return val->real;
00076 }
00077 
00078 
00079 type::Decimal *
00080 Hybrid_type_traits::val_decimal(Hybrid_type *val, type::Decimal *) const
00081 {
00082   double2_class_decimal(E_DEC_FATAL_ERROR, val->real, val->dec_buf);
00083   return val->dec_buf;
00084 }
00085 
00086 
00087 String *
00088 Hybrid_type_traits::val_str(Hybrid_type *val, String *to,
00089                             uint8_t decimals) const
00090 {
00091   to->set_real(val->real, decimals, &my_charset_bin);
00092   return to;
00093 }
00094 
00095 
00096 const Hybrid_type_traits *Hybrid_type_traits::instance()
00097 {
00098   return &real_traits_instance;
00099 }
00100 
00101 
00102 Hybrid_type_traits::Hybrid_type_traits()
00103 {}
00104 
00105 Hybrid_type_traits::~Hybrid_type_traits()
00106 {}
00107 
00108 } /* namespace drizzled */