~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
/** Copyright (C) 2000-2003 MySQL AB
1 by brian
clean slate
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
17
/**
18
  @file
19
20
  @brief
21
  This file defines all numerical functions
22
*/
23
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
24
#include <drizzled/server_includes.h>
212.5.28 by Monty Taylor
Moved my_bit and my_list
25
#include <mysys/my_bit.h>
520.6.7 by Monty Taylor
Moved a bunch of crap out of common_includes.
26
#include <drizzled/slave.h>
549 by Monty Taylor
Took gettext.h out of header files.
27
#include <drizzled/error.h>
1 by brian
clean slate
28
29
bool check_reserved_words(LEX_STRING *name)
30
{
31
  if (!my_strcasecmp(system_charset_info, name->str, "GLOBAL") ||
32
      !my_strcasecmp(system_charset_info, name->str, "LOCAL") ||
33
      !my_strcasecmp(system_charset_info, name->str, "SESSION"))
56 by brian
Next pass of true/false update.
34
    return true;
35
  return false;
1 by brian
clean slate
36
}
37
38
39
/**
40
  @return
56 by brian
Next pass of true/false update.
41
    true if item is a constant
1 by brian
clean slate
42
*/
43
44
bool
45
eval_const_cond(COND *cond)
46
{
56 by brian
Next pass of true/false update.
47
  return ((Item_func*) cond)->val_int() ? true : false;
1 by brian
clean slate
48
}
49
50
51
void Item_func::fix_num_length_and_dec()
52
{
482 by Brian Aker
Remove uint.
53
  uint32_t fl_length= 0;
1 by brian
clean slate
54
  decimals=0;
482 by Brian Aker
Remove uint.
55
  for (uint32_t i=0 ; i < arg_count ; i++)
1 by brian
clean slate
56
  {
57
    set_if_bigger(decimals,args[i]->decimals);
58
    set_if_bigger(fl_length, args[i]->max_length);
59
  }
60
  max_length=float_length(decimals);
61
  if (fl_length > max_length)
62
  {
63
    decimals= NOT_FIXED_DEC;
64
    max_length= float_length(NOT_FIXED_DEC);
65
  }
66
}
67
68
/**
69
  Set max_length/decimals of function if function is fixed point and
70
  result length/precision depends on argument ones.
71
*/
72
73
void Item_func::count_decimal_length()
74
{
75
  int max_int_part= 0;
76
  decimals= 0;
77
  unsigned_flag= 1;
482 by Brian Aker
Remove uint.
78
  for (uint32_t i=0 ; i < arg_count ; i++)
1 by brian
clean slate
79
  {
80
    set_if_bigger(decimals, args[i]->decimals);
81
    set_if_bigger(max_int_part, args[i]->decimal_int_part());
82
    set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
83
  }
398.1.4 by Monty Taylor
Renamed max/min.
84
  int precision= cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
1 by brian
clean slate
85
  max_length= my_decimal_precision_to_length(precision, decimals,
86
                                             unsigned_flag);
87
}
88
89
90
/**
91
  Set max_length of if it is maximum length of its arguments.
92
*/
93
94
void Item_func::count_only_length()
95
{
96
  max_length= 0;
97
  unsigned_flag= 0;
482 by Brian Aker
Remove uint.
98
  for (uint32_t i=0 ; i < arg_count ; i++)
1 by brian
clean slate
99
  {
100
    set_if_bigger(max_length, args[i]->max_length);
101
    set_if_bigger(unsigned_flag, args[i]->unsigned_flag);
102
  }
103
}
104
105
106
/**
107
  Set max_length/decimals of function if function is floating point and
108
  result length/precision depends on argument ones.
109
*/
110
111
void Item_func::count_real_length()
112
{
205 by Brian Aker
uint32 -> uin32_t
113
  uint32_t length= 0;
1 by brian
clean slate
114
  decimals= 0;
115
  max_length= 0;
482 by Brian Aker
Remove uint.
116
  for (uint32_t i=0 ; i < arg_count ; i++)
1 by brian
clean slate
117
  {
118
    if (decimals != NOT_FIXED_DEC)
119
    {
120
      set_if_bigger(decimals, args[i]->decimals);
121
      set_if_bigger(length, (args[i]->max_length - args[i]->decimals));
122
    }
123
    set_if_bigger(max_length, args[i]->max_length);
124
  }
125
  if (decimals != NOT_FIXED_DEC)
126
  {
127
    max_length= length;
128
    length+= decimals;
129
    if (length < max_length)  // If previous operation gave overflow
163 by Brian Aker
Merge Monty's code.
130
      max_length= UINT32_MAX;
1 by brian
clean slate
131
    else
132
      max_length= length;
133
  }
134
}
135
136
137
138
void Item_func::signal_divide_by_null()
139
{
520.1.22 by Brian Aker
Second pass of thd cleanup
140
  Session *session= current_session;
141
  push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
1 by brian
clean slate
142
  null_value= 1;
143
}
144
145
520.1.22 by Brian Aker
Second pass of thd cleanup
146
Item *Item_func::get_tmp_table_item(Session *session)
1 by brian
clean slate
147
{
148
  if (!with_sum_func && !const_item() && functype() != SUSERVAR_FUNC)
149
    return new Item_field(result_field);
520.1.22 by Brian Aker
Second pass of thd cleanup
150
  return copy_or_same(session);
1 by brian
clean slate
151
}
152
153
154
/** Get the value of a variable as a double. */
155
275 by Brian Aker
Full removal of my_bool from central server.
156
double user_var_entry::val_real(bool *null_value)
1 by brian
clean slate
157
{
158
  if ((*null_value= (value == 0)))
159
    return 0.0;
160
161
  switch (type) {
162
  case REAL_RESULT:
163
    return *(double*) value;
164
  case INT_RESULT:
152 by Brian Aker
longlong replacement
165
    return (double) *(int64_t*) value;
1 by brian
clean slate
166
  case DECIMAL_RESULT:
167
  {
168
    double result;
169
    my_decimal2double(E_DEC_FATAL_ERROR, (my_decimal *)value, &result);
170
    return result;
171
  }
172
  case STRING_RESULT:
173
    return my_atof(value);                      // This is null terminated
174
  case ROW_RESULT:
51.1.20 by Jay Pipes
Removed/replaced DBUG symbols and fixed TRUE/FALSEs
175
    assert(1);				// Impossible
1 by brian
clean slate
176
    break;
177
  }
178
  return 0.0;					// Impossible
179
}
180
181
182
/** Get the value of a variable as an integer. */
183
275 by Brian Aker
Full removal of my_bool from central server.
184
int64_t user_var_entry::val_int(bool *null_value) const
1 by brian
clean slate
185
{
186
  if ((*null_value= (value == 0)))
398.1.8 by Monty Taylor
Enabled -Wlong-long.
187
    return 0L;
1 by brian
clean slate
188
189
  switch (type) {
190
  case REAL_RESULT:
152 by Brian Aker
longlong replacement
191
    return (int64_t) *(double*) value;
1 by brian
clean slate
192
  case INT_RESULT:
152 by Brian Aker
longlong replacement
193
    return *(int64_t*) value;
1 by brian
clean slate
194
  case DECIMAL_RESULT:
195
  {
152 by Brian Aker
longlong replacement
196
    int64_t result;
1 by brian
clean slate
197
    my_decimal2int(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, &result);
198
    return result;
199
  }
200
  case STRING_RESULT:
201
  {
202
    int error;
203
    return my_strtoll10(value, (char**) 0, &error);// String is null terminated
204
  }
205
  case ROW_RESULT:
51.1.20 by Jay Pipes
Removed/replaced DBUG symbols and fixed TRUE/FALSEs
206
    assert(1);				// Impossible
1 by brian
clean slate
207
    break;
208
  }
398.1.8 by Monty Taylor
Enabled -Wlong-long.
209
  return 0L;					// Impossible
1 by brian
clean slate
210
}
211
212
213
/** Get the value of a variable as a string. */
214
275 by Brian Aker
Full removal of my_bool from central server.
215
String *user_var_entry::val_str(bool *null_value, String *str,
482 by Brian Aker
Remove uint.
216
				uint32_t decimals)
1 by brian
clean slate
217
{
218
  if ((*null_value= (value == 0)))
219
    return (String*) 0;
220
221
  switch (type) {
222
  case REAL_RESULT:
223
    str->set_real(*(double*) value, decimals, &my_charset_bin);
224
    break;
225
  case INT_RESULT:
226
    if (!unsigned_flag)
152 by Brian Aker
longlong replacement
227
      str->set(*(int64_t*) value, &my_charset_bin);
1 by brian
clean slate
228
    else
151 by Brian Aker
Ulonglong to uint64_t
229
      str->set(*(uint64_t*) value, &my_charset_bin);
1 by brian
clean slate
230
    break;
231
  case DECIMAL_RESULT:
232
    my_decimal2string(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, 0, 0, str);
233
    break;
234
  case STRING_RESULT:
235
    if (str->copy(value, length, collation.collation))
236
      str= 0;					// EOM error
237
  case ROW_RESULT:
51.1.20 by Jay Pipes
Removed/replaced DBUG symbols and fixed TRUE/FALSEs
238
    assert(1);				// Impossible
1 by brian
clean slate
239
    break;
240
  }
241
  return(str);
242
}
243
244
/** Get the value of a variable as a decimal. */
245
275 by Brian Aker
Full removal of my_bool from central server.
246
my_decimal *user_var_entry::val_decimal(bool *null_value, my_decimal *val)
1 by brian
clean slate
247
{
248
  if ((*null_value= (value == 0)))
249
    return 0;
250
251
  switch (type) {
252
  case REAL_RESULT:
253
    double2my_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
254
    break;
255
  case INT_RESULT:
152 by Brian Aker
longlong replacement
256
    int2my_decimal(E_DEC_FATAL_ERROR, *(int64_t*) value, 0, val);
1 by brian
clean slate
257
    break;
258
  case DECIMAL_RESULT:
259
    val= (my_decimal *)value;
260
    break;
261
  case STRING_RESULT:
262
    str2my_decimal(E_DEC_FATAL_ERROR, value, length, collation.collation, val);
263
    break;
264
  case ROW_RESULT:
51.1.20 by Jay Pipes
Removed/replaced DBUG symbols and fixed TRUE/FALSEs
265
    assert(1);				// Impossible
1 by brian
clean slate
266
    break;
267
  }
268
  return(val);
269
}