drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
decimal_typecast.cc
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#include <drizzled/function/math/decimal_typecast.h>
22#include <drizzled/error.h>
23#include <drizzled/session.h>
24#include <drizzled/internal/m_string.h>
25
26namespace drizzled
27{
28
30{
31 type::Decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
32 if (null_value)
33 return NULL;
34 class_decimal2string(tmp, 0, str);
35 return str;
36}
37
38
40{
41 type::Decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
42 double res;
43 if (null_value)
44 return 0.0;
45 class_decimal2double(E_DEC_FATAL_ERROR, tmp, &res);
46 return res;
47}
48
49
51{
52 type::Decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
53 int64_t res;
54 if (null_value)
55 return 0;
56
57 tmp->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &res);
58
59 return res;
60}
61
62
64{
65 type::Decimal tmp_buf, *tmp= args[0]->val_decimal(&tmp_buf);
66 bool sign;
67 uint32_t precision;
68
69 if ((null_value= args[0]->null_value))
70 return NULL;
71 class_decimal_round(E_DEC_FATAL_ERROR, tmp, decimals, false, dec);
72 sign= dec->sign();
73 if (unsigned_flag)
74 {
75 if (sign)
76 {
77 dec->set_zero();
78 goto err;
79 }
80 }
81 precision= class_decimal_length_to_precision(max_length,
82 decimals, unsigned_flag);
83 if (precision - decimals < (uint) class_decimal_intg(dec))
84 {
85 max_Decimal(dec, precision, decimals);
86 dec->sign(sign);
87 goto err;
88 }
89 return dec;
90
91err:
92 push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_ERROR,
93 ER_WARN_DATA_OUT_OF_RANGE,
94 ER(ER_WARN_DATA_OUT_OF_RANGE),
95 name, 1);
96 return dec;
97}
98
99
101{
102 char len_buf[20*3 + 1];
103 char *end;
104
105 uint32_t precision= class_decimal_length_to_precision(max_length, decimals,
106 unsigned_flag);
107 str->append(STRING_WITH_LEN("cast("));
108 args[0]->print(str);
109 str->append(STRING_WITH_LEN(" as decimal("));
110
111 end=internal::int10_to_str(precision, len_buf,10);
112 str->append(len_buf, (uint32_t) (end - len_buf));
113
114 str->append(',');
115
116 end=internal::int10_to_str(decimals, len_buf,10);
117 str->append(len_buf, (uint32_t) (end - len_buf));
118
119 str->append(')');
120 str->append(')');
121}
122
123} /* namespace drizzled */
type::Decimal * val_decimal(type::Decimal *)
virtual void print(String *str)
bool null_value
Definition item.h:124
const char * name
Definition item.h:112
TODO: Rename this file - func.h is stupid.
int class_decimal2string(const type::Decimal *d, uint32_t fixed_dec, String *str)
Converting decimal to string.