~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/decimal.cc

  • Committer: Monty Taylor
  • Date: 2008-12-06 22:41:03 UTC
  • mto: (656.1.7 devel)
  • mto: This revision was merged to the branch mainline in revision 665.
  • Revision ID: monty@inaugust.com-20081206224103-jdouqwt9hb0f01y1
Moved non-working tests into broken suite for easier running of working tests.

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