~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/functions/numhybrid.cc

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

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