~ubuntu-branches/ubuntu/saucy/drizzle/saucy-proposed

« back to all changes in this revision

Viewing changes to drizzled/function/math/neg.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

Show diffs side-by-side

added added

removed removed

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