~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>
#include <math.h>
#include <drizzled/function/numhybrid.h>

namespace drizzled
{

void Item_func_numhybrid::fix_num_length_and_dec()
{}

void Item_func_numhybrid::fix_length_and_dec()
{
  fix_num_length_and_dec();
  find_num_type();
}

String *Item_func_numhybrid::val_str(String *str)
{
  assert(fixed == 1);
  switch (hybrid_type) {
  case DECIMAL_RESULT:
    {
      type::Decimal decimal_value, *val;
      if (!(val= decimal_op(&decimal_value)))
        return 0;                                 // null is set
      class_decimal_round(E_DEC_FATAL_ERROR, val, decimals, false, val);
      class_decimal2string(val, 0, str);
      break;
    }
  case INT_RESULT:
    {
      int64_t nr= int_op();
      if (null_value)
        return 0;
      str->set_int(nr, unsigned_flag, &my_charset_bin);
      break;
    }
  case REAL_RESULT:
    {
      double nr= real_op();
      if (null_value)
        return 0;
      str->set_real(nr,decimals,&my_charset_bin);
      break;
    }
  case STRING_RESULT:
    return str_op(&str_value);
  case ROW_RESULT:
    assert(0);
  }
  return str;
}


double Item_func_numhybrid::val_real()
{
  assert(fixed == 1);
  switch (hybrid_type) {
  case DECIMAL_RESULT:
    {
      type::Decimal decimal_value, *val;
      double result;
      if (!(val= decimal_op(&decimal_value)))
        return 0.0;                               // null is set
      class_decimal2double(E_DEC_FATAL_ERROR, val, &result);
      return result;
    }
  case INT_RESULT:
    {
      int64_t result= int_op();
      return unsigned_flag ? (double) ((uint64_t) result) : (double) result;
    }
  case REAL_RESULT:
    return real_op();
  case STRING_RESULT:
    {
      char *end_not_used;
      int err_not_used;
      String *res= str_op(&str_value);
      return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
                               &end_not_used, &err_not_used) : 0.0);
    }
  case ROW_RESULT:
    assert(0);
  }

  return 0.0;
}


int64_t Item_func_numhybrid::val_int()
{
  assert(fixed == 1);
  switch (hybrid_type) {
  case DECIMAL_RESULT:
    {
      type::Decimal decimal_value, *val;
      if (!(val= decimal_op(&decimal_value)))
        return 0;                                 // null is set
      int64_t result;
      val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
      return result;
    }
  case INT_RESULT:
    return int_op();
  case REAL_RESULT:
    return (int64_t) rint(real_op());
  case STRING_RESULT:
    {
      int err_not_used;
      String *res;
      if (!(res= str_op(&str_value)))
        return 0;

      char *end= (char*) res->ptr() + res->length();
      const charset_info_st * const cs= str_value.charset();
      return (*(cs->cset->strtoll10))(cs, res->ptr(), &end, &err_not_used);
    }
  case ROW_RESULT:
    assert(0);
  }
  return 0;
}


type::Decimal *Item_func_numhybrid::val_decimal(type::Decimal *decimal_value)
{
  type::Decimal *val= decimal_value;
  assert(fixed == 1);

  switch (hybrid_type) {
  case DECIMAL_RESULT:
    val= decimal_op(decimal_value);
    break;
  case INT_RESULT:
    {
      int64_t result= int_op();
      int2_class_decimal(E_DEC_FATAL_ERROR, result, unsigned_flag, decimal_value);
      break;
    }
  case REAL_RESULT:
    {
      double result= (double)real_op();
      double2_class_decimal(E_DEC_FATAL_ERROR, result, decimal_value);
      break;
    }
  case STRING_RESULT:
    {
      String *res;
      if (!(res= str_op(&str_value)))
        return NULL;

      decimal_value->store(E_DEC_FATAL_ERROR, (char*) res->ptr(),
                           res->length(), res->charset());
      break;
    }
  case ROW_RESULT:
    assert(0);
  }

  return val;
}

} /* namespace drizzled */