Drizzled Public API Documentation

neg.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/function/math/neg.h>
00023 
00024 namespace drizzled
00025 {
00026 
00027 double Item_func_neg::real_op()
00028 {
00029   double value= args[0]->val_real();
00030   null_value= args[0]->null_value;
00031   return -value;
00032 }
00033 
00034 
00035 int64_t Item_func_neg::int_op()
00036 {
00037   int64_t value= args[0]->val_int();
00038   null_value= args[0]->null_value;
00039 
00040   return -value;
00041 }
00042 
00043 
00044 type::Decimal *Item_func_neg::decimal_op(type::Decimal *decimal_value)
00045 {
00046   type::Decimal val, *value= args[0]->val_decimal(&val);
00047   if (!(null_value= args[0]->null_value))
00048   {
00049     class_decimal2decimal(value, decimal_value);
00050     class_decimal_neg(decimal_value);
00051     return decimal_value;
00052   }
00053   return 0;
00054 }
00055 
00056 
00057 void Item_func_neg::fix_num_length_and_dec()
00058 {
00059   decimals= args[0]->decimals;
00060   /* 1 add because sign can appear */
00061   max_length= args[0]->max_length + 1;
00062 }
00063 
00064 
00065 void Item_func_neg::fix_length_and_dec()
00066 {
00067   Item_func_num1::fix_length_and_dec();
00068 
00069   /*
00070     If this is in integer context keep the context as integer if possible
00071     (This is how multiplication and other integer functions works)
00072     Use val() to get value as arg_type doesn't mean that item is
00073     Item_int or Item_real due to existence of Item_param.
00074   */
00075   if (hybrid_type == INT_RESULT && args[0]->const_item())
00076   {
00077     int64_t val= args[0]->val_int();
00078     if ((uint64_t) val >= (uint64_t) INT64_MIN &&
00079         ((uint64_t) val != (uint64_t) INT64_MIN ||
00080           args[0]->type() != INT_ITEM))
00081     {
00082       /*
00083         Ensure that result is converted to DECIMAL, as int64_t can't hold
00084         the negated number
00085       */
00086       hybrid_type= DECIMAL_RESULT;
00087     }
00088   }
00089   unsigned_flag= false;
00090 }
00091 
00092 } /* namespace drizzled */