Drizzled Public API Documentation

neg.cc
1 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2008 Sun Microsystems, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include <config.h>
21 
22 #include <drizzled/function/math/neg.h>
23 
24 namespace drizzled
25 {
26 
28 {
29  double value= args[0]->val_real();
30  null_value= args[0]->null_value;
31  return -value;
32 }
33 
34 
36 {
37  int64_t value= args[0]->val_int();
38  null_value= args[0]->null_value;
39 
40  return -value;
41 }
42 
43 
45 {
46  type::Decimal val, *value= args[0]->val_decimal(&val);
47  if (!(null_value= args[0]->null_value))
48  {
49  class_decimal2decimal(value, decimal_value);
50  class_decimal_neg(decimal_value);
51  return decimal_value;
52  }
53  return 0;
54 }
55 
56 
57 void Item_func_neg::fix_num_length_and_dec()
58 {
59  decimals= args[0]->decimals;
60  /* 1 add because sign can appear */
61  max_length= args[0]->max_length + 1;
62 }
63 
64 
65 void Item_func_neg::fix_length_and_dec()
66 {
67  Item_func_num1::fix_length_and_dec();
68 
69  /*
70  If this is in integer context keep the context as integer if possible
71  (This is how multiplication and other integer functions works)
72  Use val() to get value as arg_type doesn't mean that item is
73  Item_int or Item_real due to existence of Item_param.
74  */
75  if (hybrid_type == INT_RESULT && args[0]->const_item())
76  {
77  int64_t val= args[0]->val_int();
78  if ((uint64_t) val >= (uint64_t) INT64_MIN &&
79  ((uint64_t) val != (uint64_t) INT64_MIN ||
80  args[0]->type() != INT_ITEM))
81  {
82  /*
83  Ensure that result is converted to DECIMAL, as int64_t can't hold
84  the negated number
85  */
86  hybrid_type= DECIMAL_RESULT;
87  }
88  }
89  unsigned_flag= false;
90 }
91 
92 } /* namespace drizzled */