~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/numhybrid.cc

pandora-build v0.100 - Fixes several bugs found by cb1kenobi. Add several thoughts from folks at LCA.

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