~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
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
20
#include "config.h"
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
21
#include <math.h>
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
22
#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
23
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
24
namespace drizzled
25
{
26
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
27
void Item_func_numhybrid::fix_num_length_and_dec()
28
{}
29
30
void Item_func_numhybrid::fix_length_and_dec()
31
{
32
  fix_num_length_and_dec();
33
  find_num_type();
34
}
35
36
String *Item_func_numhybrid::val_str(String *str)
37
{
38
  assert(fixed == 1);
39
  switch (hybrid_type) {
40
  case DECIMAL_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
41
    {
2030.1.4 by Brian Aker
Change my_decimal to Decimal
42
      type::Decimal decimal_value, *val;
2008 by Brian Aker
Formatting + remove default from switch/case.
43
      if (!(val= decimal_op(&decimal_value)))
44
        return 0;                                 // null is set
2030.1.2 by Brian Aker
First pass in refactoring of the name of my_decimal.
45
      class_decimal_round(E_DEC_FATAL_ERROR, val, decimals, false, val);
2030.1.3 by Brian Aker
Second pass through function names.
46
      class_decimal2string(E_DEC_FATAL_ERROR, val, 0, 0, 0, str);
2008 by Brian Aker
Formatting + remove default from switch/case.
47
      break;
48
    }
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
49
  case INT_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
50
    {
51
      int64_t nr= int_op();
52
      if (null_value)
53
        return 0;
54
      str->set_int(nr, unsigned_flag, &my_charset_bin);
55
      break;
56
    }
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
57
  case REAL_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
58
    {
59
      double nr= real_op();
60
      if (null_value)
61
        return 0;
62
      str->set_real(nr,decimals,&my_charset_bin);
63
      break;
64
    }
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
65
  case STRING_RESULT:
66
    return str_op(&str_value);
2008 by Brian Aker
Formatting + remove default from switch/case.
67
  case ROW_RESULT:
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
68
    assert(0);
69
  }
70
  return str;
71
}
72
73
74
double Item_func_numhybrid::val_real()
75
{
76
  assert(fixed == 1);
77
  switch (hybrid_type) {
78
  case DECIMAL_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
79
    {
2030.1.4 by Brian Aker
Change my_decimal to Decimal
80
      type::Decimal decimal_value, *val;
2008 by Brian Aker
Formatting + remove default from switch/case.
81
      double result;
82
      if (!(val= decimal_op(&decimal_value)))
83
        return 0.0;                               // null is set
2030.1.3 by Brian Aker
Second pass through function names.
84
      class_decimal2double(E_DEC_FATAL_ERROR, val, &result);
2008 by Brian Aker
Formatting + remove default from switch/case.
85
      return result;
86
    }
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
87
  case INT_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
88
    {
89
      int64_t result= int_op();
90
      return unsigned_flag ? (double) ((uint64_t) result) : (double) result;
91
    }
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
92
  case REAL_RESULT:
93
    return real_op();
94
  case STRING_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
95
    {
96
      char *end_not_used;
97
      int err_not_used;
98
      String *res= str_op(&str_value);
99
      return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
100
                               &end_not_used, &err_not_used) : 0.0);
101
    }
102
  case ROW_RESULT:
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
103
    assert(0);
104
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
105
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
106
  return 0.0;
107
}
108
109
110
int64_t Item_func_numhybrid::val_int()
111
{
112
  assert(fixed == 1);
113
  switch (hybrid_type) {
114
  case DECIMAL_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
115
    {
2030.1.4 by Brian Aker
Change my_decimal to Decimal
116
      type::Decimal decimal_value, *val;
2008 by Brian Aker
Formatting + remove default from switch/case.
117
      if (!(val= decimal_op(&decimal_value)))
118
        return 0;                                 // null is set
119
      int64_t result;
2034.2.1 by Brian Aker
Move class_decimal2int to a method on Decimal.
120
      val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
2008 by Brian Aker
Formatting + remove default from switch/case.
121
      return result;
122
    }
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
123
  case INT_RESULT:
124
    return int_op();
125
  case REAL_RESULT:
126
    return (int64_t) rint(real_op());
127
  case STRING_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
128
    {
129
      int err_not_used;
130
      String *res;
131
      if (!(res= str_op(&str_value)))
132
        return 0;
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
133
2008 by Brian Aker
Formatting + remove default from switch/case.
134
      char *end= (char*) res->ptr() + res->length();
135
      const CHARSET_INFO * const cs= str_value.charset();
136
      return (*(cs->cset->strtoll10))(cs, res->ptr(), &end, &err_not_used);
137
    }
138
  case ROW_RESULT:
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
139
    assert(0);
140
  }
141
  return 0;
142
}
143
144
2030.1.4 by Brian Aker
Change my_decimal to Decimal
145
type::Decimal *Item_func_numhybrid::val_decimal(type::Decimal *decimal_value)
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
146
{
2030.1.4 by Brian Aker
Change my_decimal to Decimal
147
  type::Decimal *val= decimal_value;
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
148
  assert(fixed == 1);
2008 by Brian Aker
Formatting + remove default from switch/case.
149
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
150
  switch (hybrid_type) {
151
  case DECIMAL_RESULT:
152
    val= decimal_op(decimal_value);
153
    break;
154
  case INT_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
155
    {
156
      int64_t result= int_op();
2030.1.3 by Brian Aker
Second pass through function names.
157
      int2_class_decimal(E_DEC_FATAL_ERROR, result, unsigned_flag, decimal_value);
2008 by Brian Aker
Formatting + remove default from switch/case.
158
      break;
159
    }
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
160
  case REAL_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
161
    {
162
      double result= (double)real_op();
2030.1.3 by Brian Aker
Second pass through function names.
163
      double2_class_decimal(E_DEC_FATAL_ERROR, result, decimal_value);
2008 by Brian Aker
Formatting + remove default from switch/case.
164
      break;
165
    }
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
166
  case STRING_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
167
    {
168
      String *res;
169
      if (!(res= str_op(&str_value)))
170
        return NULL;
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
171
2034.2.5 by Brian Aker
Improvement for decimal in encapsulation.
172
      decimal_value->store(E_DEC_FATAL_ERROR, (char*) res->ptr(),
173
                           res->length(), res->charset());
2008 by Brian Aker
Formatting + remove default from switch/case.
174
      break;
175
    }
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
176
  case ROW_RESULT:
177
    assert(0);
178
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
179
492.3.2 by Lee
code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid
180
  return val;
181
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
182
183
} /* namespace drizzled */