~drizzle-trunk/drizzle/development

492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
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>
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
21
#include <drizzled/function/numhybrid.h>
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
22
23
void Item_func_numhybrid::fix_num_length_and_dec()
24
{}
25
26
void Item_func_numhybrid::fix_length_and_dec()
27
{
28
  fix_num_length_and_dec();
29
  find_num_type();
30
}
31
32
String *Item_func_numhybrid::val_str(String *str)
33
{
34
  assert(fixed == 1);
35
  switch (hybrid_type) {
36
  case DECIMAL_RESULT:
37
  {
38
    my_decimal decimal_value, *val;
39
    if (!(val= decimal_op(&decimal_value)))
40
      return 0;                                 // null is set
41
    my_decimal_round(E_DEC_FATAL_ERROR, val, decimals, false, val);
42
    my_decimal2string(E_DEC_FATAL_ERROR, val, 0, 0, 0, str);
43
    break;
44
  }
45
  case INT_RESULT:
46
  {
47
    int64_t nr= int_op();
48
    if (null_value)
49
      return 0; /* purecov: inspected */
50
    str->set_int(nr, unsigned_flag, &my_charset_bin);
51
    break;
52
  }
53
  case REAL_RESULT:
54
  {
55
    double nr= real_op();
56
    if (null_value)
57
      return 0; /* purecov: inspected */
58
    str->set_real(nr,decimals,&my_charset_bin);
59
    break;
60
  }
61
  case STRING_RESULT:
62
    return str_op(&str_value);
63
  default:
64
    assert(0);
65
  }
66
  return str;
67
}
68
69
70
double Item_func_numhybrid::val_real()
71
{
72
  assert(fixed == 1);
73
  switch (hybrid_type) {
74
  case DECIMAL_RESULT:
75
  {
76
    my_decimal decimal_value, *val;
77
    double result;
78
    if (!(val= decimal_op(&decimal_value)))
79
      return 0.0;                               // null is set
80
    my_decimal2double(E_DEC_FATAL_ERROR, val, &result);
81
    return result;
82
  }
83
  case INT_RESULT:
84
  {
85
    int64_t result= int_op();
86
    return unsigned_flag ? (double) ((uint64_t) result) : (double) result;
87
  }
88
  case REAL_RESULT:
89
    return real_op();
90
  case STRING_RESULT:
91
  {
92
    char *end_not_used;
93
    int err_not_used;
94
    String *res= str_op(&str_value);
95
    return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
96
			     &end_not_used, &err_not_used) : 0.0);
97
  }
98
  default:
99
    assert(0);
100
  }
101
  return 0.0;
102
}
103
104
105
int64_t Item_func_numhybrid::val_int()
106
{
107
  assert(fixed == 1);
108
  switch (hybrid_type) {
109
  case DECIMAL_RESULT:
110
  {
111
    my_decimal decimal_value, *val;
112
    if (!(val= decimal_op(&decimal_value)))
113
      return 0;                                 // null is set
114
    int64_t result;
115
    my_decimal2int(E_DEC_FATAL_ERROR, val, unsigned_flag, &result);
116
    return result;
117
  }
118
  case INT_RESULT:
119
    return int_op();
120
  case REAL_RESULT:
121
    return (int64_t) rint(real_op());
122
  case STRING_RESULT:
123
  {
124
    int err_not_used;
125
    String *res;
126
    if (!(res= str_op(&str_value)))
127
      return 0;
128
129
    char *end= (char*) res->ptr() + res->length();
130
    const CHARSET_INFO * const cs= str_value.charset();
131
    return (*(cs->cset->strtoll10))(cs, res->ptr(), &end, &err_not_used);
132
  }
133
  default:
134
    assert(0);
135
  }
136
  return 0;
137
}
138
139
140
my_decimal *Item_func_numhybrid::val_decimal(my_decimal *decimal_value)
141
{
142
  my_decimal *val= decimal_value;
143
  assert(fixed == 1);
144
  switch (hybrid_type) {
145
  case DECIMAL_RESULT:
146
    val= decimal_op(decimal_value);
147
    break;
148
  case INT_RESULT:
149
  {
150
    int64_t result= int_op();
151
    int2my_decimal(E_DEC_FATAL_ERROR, result, unsigned_flag, decimal_value);
152
    break;
153
  }
154
  case REAL_RESULT:
155
  {
156
    double result= (double)real_op();
157
    double2my_decimal(E_DEC_FATAL_ERROR, result, decimal_value);
158
    break;
159
  }
160
  case STRING_RESULT:
161
  {
162
    String *res;
163
    if (!(res= str_op(&str_value)))
164
      return NULL;
165
166
    str2my_decimal(E_DEC_FATAL_ERROR, (char*) res->ptr(),
167
                   res->length(), res->charset(), decimal_value);
168
    break;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
169
  }
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
170
  case ROW_RESULT:
171
  default:
172
    assert(0);
173
  }
174
  return val;
175
}