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

« back to all changes in this revision

Viewing changes to drizzled/item/decimal.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/item/decimal.h>
 
23
 
 
24
namespace drizzled
 
25
{
 
26
 
 
27
Item_decimal::Item_decimal(const char *str_arg, uint32_t length,
 
28
                           const CHARSET_INFO * const charset)
 
29
{
 
30
  str2my_decimal(E_DEC_FATAL_ERROR, str_arg, length, charset, &decimal_value);
 
31
  name= (char*) str_arg;
 
32
  decimals= (uint8_t) decimal_value.frac;
 
33
  fixed= 1;
 
34
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
 
35
                                             decimals, unsigned_flag);
 
36
}
 
37
 
 
38
Item_decimal::Item_decimal(int64_t val, bool unsig)
 
39
{
 
40
  int2my_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value);
 
41
  decimals= (uint8_t) decimal_value.frac;
 
42
  fixed= 1;
 
43
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
 
44
                                             decimals, unsigned_flag);
 
45
}
 
46
 
 
47
 
 
48
Item_decimal::Item_decimal(double val, int, int)
 
49
{
 
50
  double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
 
51
  decimals= (uint8_t) decimal_value.frac;
 
52
  fixed= 1;
 
53
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
 
54
                                             decimals, unsigned_flag);
 
55
}
 
56
 
 
57
Item_decimal::Item_decimal(const char *str, const my_decimal *val_arg,
 
58
                           uint32_t decimal_par, uint32_t length)
 
59
{
 
60
  my_decimal2decimal(val_arg, &decimal_value);
 
61
  name= (char*) str;
 
62
  decimals= (uint8_t) decimal_par;
 
63
  max_length= length;
 
64
  fixed= 1;
 
65
}
 
66
 
 
67
 
 
68
Item_decimal::Item_decimal(my_decimal *value_par)
 
69
{
 
70
  my_decimal2decimal(value_par, &decimal_value);
 
71
  decimals= (uint8_t) decimal_value.frac;
 
72
  fixed= 1;
 
73
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
 
74
                                             decimals, unsigned_flag);
 
75
}
 
76
 
 
77
 
 
78
Item_decimal::Item_decimal(const unsigned char *bin, int precision, int scale)
 
79
{
 
80
  binary2my_decimal(E_DEC_FATAL_ERROR, bin,
 
81
                    &decimal_value, precision, scale);
 
82
  decimals= (uint8_t) decimal_value.frac;
 
83
  fixed= 1;
 
84
  max_length= my_decimal_precision_to_length(precision, decimals,
 
85
                                             unsigned_flag);
 
86
}
 
87
 
 
88
int64_t Item_decimal::val_int()
 
89
{
 
90
  int64_t result;
 
91
  my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &result);
 
92
  return result;
 
93
}
 
94
 
 
95
double Item_decimal::val_real()
 
96
{
 
97
  double result;
 
98
  my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
 
99
  return result;
 
100
}
 
101
 
 
102
String *Item_decimal::val_str(String *result)
 
103
{
 
104
  result->set_charset(&my_charset_bin);
 
105
  my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, result);
 
106
  return result;
 
107
}
 
108
 
 
109
void Item_decimal::print(String *str, enum_query_type)
 
110
{
 
111
  my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, &str_value);
 
112
  str->append(str_value);
 
113
}
 
114
 
 
115
bool Item_decimal::eq(const Item *item, bool) const
 
116
{
 
117
  if (type() == item->type() && item->basic_const_item())
 
118
  {
 
119
    /*
 
120
      We need to cast off const to call val_decimal(). This should
 
121
      be OK for a basic constant. Additionally, we can pass 0 as
 
122
      a true decimal constant will return its internal decimal
 
123
      storage and ignore the argument.
 
124
    */
 
125
    Item *arg= (Item*) item;
 
126
    my_decimal *value= arg->val_decimal(0);
 
127
    return !my_decimal_cmp(&decimal_value, value);
 
128
  }
 
129
  return 0;
 
130
}
 
131
 
 
132
 
 
133
void Item_decimal::set_decimal_value(my_decimal *value_par)
 
134
{
 
135
  my_decimal2decimal(value_par, &decimal_value);
 
136
  decimals= (uint8_t) decimal_value.frac;
 
137
  unsigned_flag= !decimal_value.sign();
 
138
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
 
139
                                             decimals, unsigned_flag);
 
140
}
 
141
 
 
142
int Item_decimal::save_in_field(Field *field, bool)
 
143
{
 
144
  field->set_notnull();
 
145
  return field->store_decimal(&decimal_value);
 
146
}
 
147
 
 
148
 
 
149
} /* namespace drizzled */