~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/decimal.cc

  • Committer: Brian Aker
  • Date: 2008-12-09 17:33:02 UTC
  • mfrom: (656.1.54 devel)
  • Revision ID: brian@tangent.org-20081209173302-aptngvc7efxnatnt
Merge from Monty.

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