~drizzle-trunk/drizzle/development

492.3.8 by Lee
code clean move Item_func_mod, Item_func_int_div, Item_func_neg to functions directory
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
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 <drizzled/server_includes.h>
21
#include CSTDINT_H
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
22
#include <drizzled/function/neg.h>
492.3.8 by Lee
code clean move Item_func_mod, Item_func_int_div, Item_func_neg to functions directory
23
24
double Item_func_neg::real_op()
25
{
26
  double value= args[0]->val_real();
27
  null_value= args[0]->null_value;
28
  return -value;
29
}
30
31
32
int64_t Item_func_neg::int_op()
33
{
34
  int64_t value= args[0]->val_int();
35
  null_value= args[0]->null_value;
36
  return -value;
37
}
38
39
40
my_decimal *Item_func_neg::decimal_op(my_decimal *decimal_value)
41
{
42
  my_decimal val, *value= args[0]->val_decimal(&val);
43
  if (!(null_value= args[0]->null_value))
44
  {
45
    my_decimal2decimal(value, decimal_value);
46
    my_decimal_neg(decimal_value);
47
    return decimal_value;
48
  }
49
  return 0;
50
}
51
52
53
void Item_func_neg::fix_num_length_and_dec()
54
{
55
  decimals= args[0]->decimals;
56
  /* 1 add because sign can appear */
57
  max_length= args[0]->max_length + 1;
58
}
59
60
61
void Item_func_neg::fix_length_and_dec()
62
{
63
  Item_func_num1::fix_length_and_dec();
64
65
  /*
66
    If this is in integer context keep the context as integer if possible
67
    (This is how multiplication and other integer functions works)
68
    Use val() to get value as arg_type doesn't mean that item is
69
    Item_int or Item_real due to existence of Item_param.
70
  */
71
  if (hybrid_type == INT_RESULT && args[0]->const_item())
72
  {
73
    int64_t val= args[0]->val_int();
74
    if ((uint64_t) val >= (uint64_t) INT64_MIN &&
75
        ((uint64_t) val != (uint64_t) INT64_MIN ||
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
76
          args[0]->type() != INT_ITEM))
492.3.8 by Lee
code clean move Item_func_mod, Item_func_int_div, Item_func_neg to functions directory
77
    {
78
      /*
79
        Ensure that result is converted to DECIMAL, as int64_t can't hold
80
        the negated number
81
      */
82
      hybrid_type= DECIMAL_RESULT;
83
    }
84
  }
85
  unsigned_flag= 0;
86
  return;
87
}