1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
20
#include <drizzled/server_includes.h>
25
#include <drizzled/functions/numhybrid.h>
28
#if defined(CMATH_NAMESPACE)
29
using namespace CMATH_NAMESPACE;
32
void Item_func_numhybrid::fix_num_length_and_dec()
35
void Item_func_numhybrid::fix_length_and_dec()
37
fix_num_length_and_dec();
41
String *Item_func_numhybrid::val_str(String *str)
44
switch (hybrid_type) {
47
my_decimal decimal_value, *val;
48
if (!(val= decimal_op(&decimal_value)))
49
return 0; // null is set
50
my_decimal_round(E_DEC_FATAL_ERROR, val, decimals, false, val);
51
my_decimal2string(E_DEC_FATAL_ERROR, val, 0, 0, 0, str);
58
return 0; /* purecov: inspected */
59
str->set_int(nr, unsigned_flag, &my_charset_bin);
66
return 0; /* purecov: inspected */
67
str->set_real(nr,decimals,&my_charset_bin);
71
return str_op(&str_value);
79
double Item_func_numhybrid::val_real()
82
switch (hybrid_type) {
85
my_decimal decimal_value, *val;
87
if (!(val= decimal_op(&decimal_value)))
88
return 0.0; // null is set
89
my_decimal2double(E_DEC_FATAL_ERROR, val, &result);
94
int64_t result= int_op();
95
return unsigned_flag ? (double) ((uint64_t) result) : (double) result;
103
String *res= str_op(&str_value);
104
return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
105
&end_not_used, &err_not_used) : 0.0);
114
int64_t Item_func_numhybrid::val_int()
117
switch (hybrid_type) {
120
my_decimal decimal_value, *val;
121
if (!(val= decimal_op(&decimal_value)))
122
return 0; // null is set
124
my_decimal2int(E_DEC_FATAL_ERROR, val, unsigned_flag, &result);
130
return (int64_t) rint(real_op());
135
if (!(res= str_op(&str_value)))
138
char *end= (char*) res->ptr() + res->length();
139
const CHARSET_INFO * const cs= str_value.charset();
140
return (*(cs->cset->strtoll10))(cs, res->ptr(), &end, &err_not_used);
149
my_decimal *Item_func_numhybrid::val_decimal(my_decimal *decimal_value)
151
my_decimal *val= decimal_value;
153
switch (hybrid_type) {
155
val= decimal_op(decimal_value);
159
int64_t result= int_op();
160
int2my_decimal(E_DEC_FATAL_ERROR, result, unsigned_flag, decimal_value);
165
double result= (double)real_op();
166
double2my_decimal(E_DEC_FATAL_ERROR, result, decimal_value);
172
if (!(res= str_op(&str_value)))
175
str2my_decimal(E_DEC_FATAL_ERROR, (char*) res->ptr(),
176
res->length(), res->charset(), decimal_value);