Drizzled Public API Documentation

mod.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 #include <math.h>
00022 #include <drizzled/function/math/mod.h>
00023 #include <drizzled/type/decimal.h>
00024 
00025 #include <algorithm>
00026 
00027 using namespace std;
00028 
00029 namespace drizzled
00030 {
00031 
00032 int64_t Item_func_mod::int_op()
00033 {
00034   assert(fixed == 1);
00035   int64_t value=  args[0]->val_int();
00036   int64_t val2= args[1]->val_int();
00037   int64_t result;
00038 
00039   if ((null_value= args[0]->null_value || args[1]->null_value))
00040     return 0;
00041   if (val2 == 0)
00042   {
00043     signal_divide_by_null();
00044     return 0;
00045   }
00046 
00047   if (args[0]->unsigned_flag)
00048     result= args[1]->unsigned_flag ?
00049       ((uint64_t) value) % ((uint64_t) val2) : ((uint64_t) value) % val2;
00050   else
00051     result= args[1]->unsigned_flag ?
00052       (int64_t)(value % ((uint64_t) val2)) : (int64_t)(value % val2);
00053 
00054   return result;
00055 }
00056 
00057 double Item_func_mod::real_op()
00058 {
00059   assert(fixed == 1);
00060   double value= args[0]->val_real();
00061   double val2=  args[1]->val_real();
00062   if ((null_value= args[0]->null_value || args[1]->null_value))
00063     return 0.0;
00064   if (val2 == 0.0)
00065   {
00066     signal_divide_by_null();
00067     return 0.0;
00068   }
00069   return fmod(value,val2);
00070 }
00071 
00072 
00073 type::Decimal *Item_func_mod::decimal_op(type::Decimal *decimal_value)
00074 {
00075   type::Decimal value1, *val1;
00076   type::Decimal value2, *val2;
00077 
00078   val1= args[0]->val_decimal(&value1);
00079   if ((null_value= args[0]->null_value))
00080     return 0;
00081   val2= args[1]->val_decimal(&value2);
00082   if ((null_value= args[1]->null_value))
00083     return 0;
00084   switch (class_decimal_mod(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
00085                          val1, val2)) {
00086   case E_DEC_TRUNCATED:
00087   case E_DEC_OK:
00088     return decimal_value;
00089   case E_DEC_DIV_ZERO:
00090     signal_divide_by_null();
00091   default:
00092     null_value= 1;
00093     return 0;
00094   }
00095 }
00096 
00097 
00098 void Item_func_mod::result_precision()
00099 {
00100   decimals= max(args[0]->decimals, args[1]->decimals);
00101   max_length= max(args[0]->max_length, args[1]->max_length);
00102 }
00103 
00104 
00105 void Item_func_mod::fix_length_and_dec()
00106 {
00107   Item_num_op::fix_length_and_dec();
00108   maybe_null= 1;
00109   unsigned_flag= args[0]->unsigned_flag;
00110 }
00111 
00112 } /* namespace drizzled */