~drizzle-trunk/drizzle/development

584.4.5 by Monty Taylor
Split out cache_row and type_holder.
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-2009 Sun Microsystems, Inc.
584.4.5 by Monty Taylor
Split out cache_row and type_holder.
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
 */
1 by brian
clean slate
19
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
20
#include <config.h>
21
#include <drizzled/sql_select.h>
22
#include <drizzled/error.h>
23
#include <drizzled/show.h>
24
#include <drizzled/item/cmpfunc.h>
25
#include <drizzled/item/cache_row.h>
26
#include <drizzled/item/type_holder.h>
27
#include <drizzled/item/sum.h>
28
#include <drizzled/item/copy_string.h>
29
#include <drizzled/function/str/conv_charset.h>
30
#include <drizzled/sql_base.h>
31
#include <drizzled/util/convert.h>
32
#include <drizzled/plugin/client.h>
33
#include <drizzled/time_functions.h>
34
#include <drizzled/field/str.h>
35
#include <drizzled/field/num.h>
36
#include <drizzled/field/blob.h>
37
#include <drizzled/field/date.h>
38
#include <drizzled/field/datetime.h>
39
#include <drizzled/field/decimal.h>
40
#include <drizzled/field/double.h>
41
#include <drizzled/field/enum.h>
42
#include <drizzled/field/epoch.h>
43
#include <drizzled/field/int32.h>
44
#include <drizzled/field/int64.h>
45
#include <drizzled/field/microtime.h>
46
#include <drizzled/field/null.h>
47
#include <drizzled/field/real.h>
48
#include <drizzled/field/size.h>
49
#include <drizzled/field/time.h>
50
#include <drizzled/field/varstring.h>
2154.2.24 by Brian Aker
Merge in all changes for current_session, etc.
51
#include <drizzled/current_session.h>
2154.2.18 by Brian Aker
Merge in all changes for include files.
52
#include <drizzled/session.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
53
#include <drizzled/internal/m_string.h>
2198.1.2 by Olaf van der Spek
Refactor includes
54
#include <drizzled/item/ref.h>
55
#include <drizzled/item/subselect.h>
2234.1.3 by Olaf van der Spek
Refactor includes
56
#include <drizzled/sql_lex.h>
2241.3.2 by Olaf van der Spek
Refactor Session::variables
57
#include <drizzled/system_variables.h>
584.5.1 by Monty Taylor
Removed field includes from field.h.
58
1502.3.1 by iwamatsu at nigauri
Add cstdio include to files needing it. Fixes the build on some debian
59
#include <cstdio>
919.2.11 by Monty Taylor
Removed C99 isnan() usage, which allows us to remove the util/math.{cc,h} workarounds. Yay for standards!
60
#include <math.h>
1067.4.4 by Nathan Williams
The rest of the files in the drizzled directory were purged of the cmin macro and replace with std::min (except for the definition in globals.h and 1 usage in stacktrace.cc).
61
#include <algorithm>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
62
#include <float.h>
1067.4.4 by Nathan Williams
The rest of the files in the drizzled directory were purged of the cmin macro and replace with std::min (except for the definition in globals.h and 1 usage in stacktrace.cc).
63
64
using namespace std;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
65
2318.7.14 by Olaf van der Spek
Refactor
66
namespace drizzled {
1067.4.4 by Nathan Williams
The rest of the files in the drizzled directory were purged of the cmin macro and replace with std::min (except for the definition in globals.h and 1 usage in stacktrace.cc).
67
1 by brian
clean slate
68
const String my_null_string("NULL", 4, default_charset_info);
69
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
70
bool Item::is_expensive_processor(unsigned char *)
71
{
681 by Brian Aker
Style cleanup
72
  return false;
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
73
}
74
846 by Brian Aker
Removing on typedeffed class.
75
void Item::fix_after_pullout(Select_Lex *, Item **)
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
76
{}
77
78
Field *Item::tmp_table_field(Table *)
79
{
681 by Brian Aker
Style cleanup
80
  return NULL;
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
81
}
82
83
const char *Item::full_name(void) const
84
{
85
  return name ? name : "???";
86
}
87
88
int64_t Item::val_int_endpoint(bool, bool *)
89
{
90
  assert(0);
91
  return 0;
92
}
93
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
94
/** @todo Make this functions class dependent */
1 by brian
clean slate
95
bool Item::val_bool()
96
{
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
97
  switch(result_type()) 
1 by brian
clean slate
98
  {
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
99
    case INT_RESULT:
100
      return val_int() != 0;
2008 by Brian Aker
Formatting + remove default from switch/case.
101
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
102
    case DECIMAL_RESULT:
103
    {
2030.1.4 by Brian Aker
Change my_decimal to Decimal
104
      type::Decimal decimal_value;
105
      type::Decimal *val= val_decimal(&decimal_value);
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
106
      if (val)
2137.1.5 by Brian Aker
Additional decimal.
107
        return not val->isZero();
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
108
      return false;
109
    }
2008 by Brian Aker
Formatting + remove default from switch/case.
110
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
111
    case REAL_RESULT:
112
    case STRING_RESULT:
113
      return val_real() != 0.0;
2008 by Brian Aker
Formatting + remove default from switch/case.
114
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
115
    case ROW_RESULT:
116
      assert(0);
1996.2.1 by Brian Aker
uuid type code.
117
      abort();
1 by brian
clean slate
118
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
119
120
  assert(0);
121
  abort();
1 by brian
clean slate
122
}
123
124
String *Item::val_string_from_real(String *str)
125
{
126
  double nr= val_real();
127
  if (null_value)
971.6.11 by Eric Day
Removed purecov messages.
128
    return NULL;
681 by Brian Aker
Style cleanup
129
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
130
  str->set_real(nr, decimals, &my_charset_bin);
1 by brian
clean slate
131
  return str;
132
}
133
134
String *Item::val_string_from_int(String *str)
135
{
152 by Brian Aker
longlong replacement
136
  int64_t nr= val_int();
1 by brian
clean slate
137
  if (null_value)
681 by Brian Aker
Style cleanup
138
    return NULL;
139
1 by brian
clean slate
140
  str->set_int(nr, unsigned_flag, &my_charset_bin);
141
  return str;
142
}
143
144
String *Item::val_string_from_decimal(String *str)
145
{
2030.1.4 by Brian Aker
Change my_decimal to Decimal
146
  type::Decimal dec_buf, *dec= val_decimal(&dec_buf);
1 by brian
clean slate
147
  if (null_value)
681 by Brian Aker
Style cleanup
148
    return NULL;
149
2030.1.2 by Brian Aker
First pass in refactoring of the name of my_decimal.
150
  class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
2137.1.8 by Brian Aker
Remove error type from str convert since we always want an error.
151
  class_decimal2string(&dec_buf, 0, str);
1 by brian
clean slate
152
  return str;
153
}
154
2030.1.4 by Brian Aker
Change my_decimal to Decimal
155
type::Decimal *Item::val_decimal_from_real(type::Decimal *decimal_value)
1 by brian
clean slate
156
{
157
  double nr= val_real();
158
  if (null_value)
681 by Brian Aker
Style cleanup
159
    return NULL;
160
2030.1.3 by Brian Aker
Second pass through function names.
161
  double2_class_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
1 by brian
clean slate
162
  return (decimal_value);
163
}
164
2030.1.4 by Brian Aker
Change my_decimal to Decimal
165
type::Decimal *Item::val_decimal_from_int(type::Decimal *decimal_value)
1 by brian
clean slate
166
{
152 by Brian Aker
longlong replacement
167
  int64_t nr= val_int();
1 by brian
clean slate
168
  if (null_value)
681 by Brian Aker
Style cleanup
169
    return NULL;
170
2030.1.3 by Brian Aker
Second pass through function names.
171
  int2_class_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
1 by brian
clean slate
172
  return decimal_value;
173
}
174
2030.1.4 by Brian Aker
Change my_decimal to Decimal
175
type::Decimal *Item::val_decimal_from_string(type::Decimal *decimal_value)
1 by brian
clean slate
176
{
177
  String *res;
178
  if (!(res= val_str(&str_value)))
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
179
    return NULL;
1 by brian
clean slate
180
2034.2.5 by Brian Aker
Improvement for decimal in encapsulation.
181
  if (decimal_value->store(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
182
                     res->ptr(), 
183
                     res->length(), 
2034.2.5 by Brian Aker
Improvement for decimal in encapsulation.
184
                     res->charset()) & E_DEC_BAD_NUM)
1 by brian
clean slate
185
  {
2148.5.3 by Brian Aker
Remove hard coded array for numbers (this also fixes a couple of spots where
186
    push_warning_printf(&getSession(), 
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
187
                        DRIZZLE_ERROR::WARN_LEVEL_WARN,
1 by brian
clean slate
188
                        ER_TRUNCATED_WRONG_VALUE,
189
                        ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
190
                        str_value.c_ptr());
191
  }
192
  return decimal_value;
193
}
194
2030.1.4 by Brian Aker
Change my_decimal to Decimal
195
type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
1 by brian
clean slate
196
{
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
197
  assert(fixed);
2030.1.5 by Brian Aker
Update for moving DRIZZLE_TIME to type::Time
198
  type::Time ltime;
2104.2.8 by Brian Aker
Merge in reference from pointer.
199
  if (get_date(ltime, TIME_FUZZY_DATE))
1 by brian
clean slate
200
  {
2034.2.4 by Brian Aker
Further encapsulation for DECIMAL.
201
    decimal_value->set_zero();
1 by brian
clean slate
202
    null_value= 1;                               // set NULL, stop processing
681 by Brian Aker
Style cleanup
203
    return NULL;
1 by brian
clean slate
204
  }
2030.1.3 by Brian Aker
Second pass through function names.
205
  return date2_class_decimal(&ltime, decimal_value);
1 by brian
clean slate
206
}
207
2030.1.4 by Brian Aker
Change my_decimal to Decimal
208
type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
1 by brian
clean slate
209
{
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
210
  assert(fixed);
2030.1.5 by Brian Aker
Update for moving DRIZZLE_TIME to type::Time
211
  type::Time ltime;
2104.2.8 by Brian Aker
Merge in reference from pointer.
212
  if (get_time(ltime))
1 by brian
clean slate
213
  {
2034.2.4 by Brian Aker
Further encapsulation for DECIMAL.
214
    decimal_value->set_zero();
681 by Brian Aker
Style cleanup
215
    return NULL;
1 by brian
clean slate
216
  }
2030.1.3 by Brian Aker
Second pass through function names.
217
  return date2_class_decimal(&ltime, decimal_value);
1 by brian
clean slate
218
}
219
220
double Item::val_real_from_decimal()
221
{
222
  /* Note that fix_fields may not be called for Item_avg_field items */
223
  double result;
2030.1.4 by Brian Aker
Change my_decimal to Decimal
224
  type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
1 by brian
clean slate
225
  if (null_value)
226
    return 0.0;
2030.1.3 by Brian Aker
Second pass through function names.
227
  class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
1 by brian
clean slate
228
  return result;
229
}
230
152 by Brian Aker
longlong replacement
231
int64_t Item::val_int_from_decimal()
1 by brian
clean slate
232
{
233
  /* Note that fix_fields may not be called for Item_avg_field items */
152 by Brian Aker
longlong replacement
234
  int64_t result;
2030.1.4 by Brian Aker
Change my_decimal to Decimal
235
  type::Decimal value, *dec_val= val_decimal(&value);
2104.2.7 by Brian Aker
Fix store_time to take reference.
236
1 by brian
clean slate
237
  if (null_value)
238
    return 0;
2034.2.1 by Brian Aker
Move class_decimal2int to a method on Decimal.
239
  dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
2104.2.7 by Brian Aker
Fix store_time to take reference.
240
1 by brian
clean slate
241
  return result;
242
}
243
2126.3.1 by Brian Aker
Add bool int patch
244
bool Item::save_time_in_field(Field *field)
1 by brian
clean slate
245
{
2030.1.5 by Brian Aker
Update for moving DRIZZLE_TIME to type::Time
246
  type::Time ltime;
2104.2.7 by Brian Aker
Fix store_time to take reference.
247
2104.2.8 by Brian Aker
Merge in reference from pointer.
248
  if (get_time(ltime))
1 by brian
clean slate
249
    return set_field_to_null(field);
2104.2.7 by Brian Aker
Fix store_time to take reference.
250
1 by brian
clean slate
251
  field->set_notnull();
2104.2.7 by Brian Aker
Fix store_time to take reference.
252
253
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
1 by brian
clean slate
254
}
255
2126.3.1 by Brian Aker
Add bool int patch
256
bool Item::save_date_in_field(Field *field)
1 by brian
clean slate
257
{
2030.1.5 by Brian Aker
Update for moving DRIZZLE_TIME to type::Time
258
  type::Time ltime;
2104.2.7 by Brian Aker
Fix store_time to take reference.
259
2104.2.8 by Brian Aker
Merge in reference from pointer.
260
  if (get_date(ltime, TIME_FUZZY_DATE))
1 by brian
clean slate
261
    return set_field_to_null(field);
2104.2.7 by Brian Aker
Fix store_time to take reference.
262
1 by brian
clean slate
263
  field->set_notnull();
2104.2.7 by Brian Aker
Fix store_time to take reference.
264
265
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
1 by brian
clean slate
266
}
267
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
268
/**
269
 * Check if the Item is null and stores the NULL or the
270
 * result value in the field accordingly.
271
 */
1 by brian
clean slate
272
int Item::save_str_value_in_field(Field *field, String *result)
273
{
274
  if (null_value)
275
    return set_field_to_null(field);
2104.2.7 by Brian Aker
Fix store_time to take reference.
276
1 by brian
clean slate
277
  field->set_notnull();
2104.2.7 by Brian Aker
Fix store_time to take reference.
278
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
279
  return field->store(result->ptr(), result->length(), collation.collation);
1 by brian
clean slate
280
}
281
282
Item::Item():
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
283
  is_expensive_cache(-1),
284
  name(0), 
285
  name_length(0),
286
  max_length(0), 
287
  marker(0),
288
  decimals(0),
289
  fixed(false),
290
  maybe_null(false),
291
  null_value(false),
292
  unsigned_flag(false), 
293
  with_sum_func(false),
294
  is_autogenerated_name(true),
295
  with_subselect(false),
2137.1.10 by Brian Aker
Fix item so that we have session (ie, it always exists within the context so
296
  collation(&my_charset_bin, DERIVATION_COERCIBLE),
297
  _session(*current_session)
1 by brian
clean slate
298
{
299
  cmp_context= (Item_result)-1;
300
301
  /* Put item in free list so that we can free all items at end */
2137.1.10 by Brian Aker
Fix item so that we have session (ie, it always exists within the context so
302
  next= getSession().free_list;
303
  getSession().free_list= this;
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
304
1 by brian
clean slate
305
  /*
306
    Item constructor can be called during execution other then SQL_COM
2227.4.8 by Olaf van der Spek
Session::lex()
307
    command => we should check session->lex().current_select on zero (session->lex
1 by brian
clean slate
308
    can be uninitialised)
309
  */
2227.4.8 by Olaf van der Spek
Session::lex()
310
  if (getSession().lex().current_select)
1 by brian
clean slate
311
  {
2227.4.8 by Olaf van der Spek
Session::lex()
312
    enum_parsing_place place= getSession().lex().current_select->parsing_place;
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
313
    if (place == SELECT_LIST || place == IN_HAVING)
2227.4.8 by Olaf van der Spek
Session::lex()
314
      getSession().lex().current_select->select_n_having_items++;
1 by brian
clean slate
315
  }
316
}
317
520.1.22 by Brian Aker
Second pass of thd cleanup
318
Item::Item(Session *session, Item *item):
1 by brian
clean slate
319
  is_expensive_cache(-1),
320
  str_value(item->str_value),
321
  name(item->name),
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
322
  name_length(item->name_length),
1 by brian
clean slate
323
  max_length(item->max_length),
324
  marker(item->marker),
325
  decimals(item->decimals),
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
326
  fixed(item->fixed),
1 by brian
clean slate
327
  maybe_null(item->maybe_null),
328
  null_value(item->null_value),
329
  unsigned_flag(item->unsigned_flag),
330
  with_sum_func(item->with_sum_func),
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
331
  is_autogenerated_name(item->is_autogenerated_name),
332
  with_subselect(item->with_subselect),
1 by brian
clean slate
333
  collation(item->collation),
2137.1.10 by Brian Aker
Fix item so that we have session (ie, it always exists within the context so
334
  cmp_context(item->cmp_context),
335
  _session(*session)
1 by brian
clean slate
336
{
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
337
  /* Put this item in the session's free list */
2137.1.10 by Brian Aker
Fix item so that we have session (ie, it always exists within the context so
338
  next= getSession().free_list;
339
  getSession().free_list= this;
1 by brian
clean slate
340
}
341
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
342
uint32_t Item::float_length(uint32_t decimals_par) const
343
{
344
  return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;
345
}
346
482 by Brian Aker
Remove uint.
347
uint32_t Item::decimal_precision() const
1 by brian
clean slate
348
{
349
  Item_result restype= result_type();
350
351
  if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
2030.1.2 by Brian Aker
First pass in refactoring of the name of my_decimal.
352
    return min(class_decimal_length_to_precision(max_length, decimals, unsigned_flag),
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
353
               (uint32_t) DECIMAL_MAX_PRECISION);
1067.4.4 by Nathan Williams
The rest of the files in the drizzled directory were purged of the cmin macro and replace with std::min (except for the definition in globals.h and 1 usage in stacktrace.cc).
354
  return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
1 by brian
clean slate
355
}
356
584.1.14 by Monty Taylor
Removed field.h from common_includes.
357
int Item::decimal_int_part() const
358
{
2030.1.2 by Brian Aker
First pass in refactoring of the name of my_decimal.
359
  return class_decimal_int_part(decimal_precision(), decimals);
584.1.14 by Monty Taylor
Removed field.h from common_includes.
360
}
361
2215.2.1 by Stewart Smith
remove enum_query_type which was effectively unused. It was set to one value once, compared to it once (i.e. always true) and passed around everywhere doing nothing.
362
void Item::print(String *str)
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
363
{
364
  str->append(full_name());
365
}
366
2215.2.1 by Stewart Smith
remove enum_query_type which was effectively unused. It was set to one value once, compared to it once (i.e. always true) and passed around everywhere doing nothing.
367
void Item::print_item_w_name(String *str)
1 by brian
clean slate
368
{
2215.2.1 by Stewart Smith
remove enum_query_type which was effectively unused. It was set to one value once, compared to it once (i.e. always true) and passed around everywhere doing nothing.
369
  print(str);
1 by brian
clean slate
370
371
  if (name)
372
  {
373
    str->append(STRING_WITH_LEN(" AS "));
2318.8.1 by Olaf van der Spek
Add data_ref.h
374
    str->append_identifier(str_ref(name));
1 by brian
clean slate
375
  }
376
}
377
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
378
void Item::split_sum_func(Session *, Item **, List<Item> &)
379
{}
380
1 by brian
clean slate
381
void Item::cleanup()
382
{
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
383
  fixed= false;
1 by brian
clean slate
384
  marker= 0;
385
}
386
481 by Brian Aker
Remove all of uchar.
387
Item* Item::transform(Item_transformer transformer, unsigned char *arg)
1 by brian
clean slate
388
{
389
  return (this->*transformer)(arg);
390
}
391
482 by Brian Aker
Remove uint.
392
bool Item::check_cols(uint32_t c)
1 by brian
clean slate
393
{
394
  if (c != 1)
395
  {
396
    my_error(ER_OPERAND_COLUMNS, MYF(0), c);
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
397
    return true;
1 by brian
clean slate
398
  }
681 by Brian Aker
Style cleanup
399
  return false;
1 by brian
clean slate
400
}
401
2440.2.22 by Olaf van der Spek
Refactor
402
void Item::set_name(const char *str, uint32_t length, const charset_info_st* cs)
1 by brian
clean slate
403
{
2440.2.22 by Olaf van der Spek
Refactor
404
  if (not length)
1 by brian
clean slate
405
  {
2440.2.22 by Olaf van der Spek
Refactor
406
    if (0) // str && *str)
407
      std::cerr << "non-empty empty name: " << str << std::endl;
1 by brian
clean slate
408
    /* Empty string, used by AS or internal function like last_insert_id() */
2440.2.22 by Olaf van der Spek
Refactor
409
    name= str; // should be NULL (or "");
1 by brian
clean slate
410
    name_length= 0;
411
    return;
412
  }
413
  if (cs->ctype)
414
  {
482 by Brian Aker
Remove uint.
415
    uint32_t orig_len= length;
2445.1.3 by Olaf van der Spek
Refactor
416
    while (length && not cs->isgraph(*str))
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
417
    {
418
      /* Fix problem with yacc */
1 by brian
clean slate
419
      length--;
420
      str++;
421
    }
2440.2.22 by Olaf van der Spek
Refactor
422
    if (orig_len != length && not is_autogenerated_name)
1 by brian
clean slate
423
    {
424
      if (length == 0)
2318.7.14 by Olaf van der Spek
Refactor
425
        push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NAME_BECOMES_EMPTY, ER(ER_NAME_BECOMES_EMPTY), str + length - orig_len);
1 by brian
clean slate
426
      else
2318.7.14 by Olaf van der Spek
Refactor
427
        push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES), str + length - orig_len);
1 by brian
clean slate
428
    }
429
  }
2318.9.11 by Olaf van der Spek
Use sql_strdup(str_ref)
430
  name= memory::sql_strdup(str_ref(str, length));
1 by brian
clean slate
431
}
432
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
433
bool Item::eq(const Item *item, bool) const
1 by brian
clean slate
434
{
435
  /*
56 by brian
Next pass of true/false update.
436
    Note, that this is never true if item is a Item_param:
1 by brian
clean slate
437
    for all basic constants we have special checks, and Item_param's
438
    type() can be only among basic constant types.
439
  */
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
440
  return type() == item->type() && 
441
         name && 
442
         item->name &&
2456.1.4 by Olaf van der Spek
Refactor
443
         not system_charset_info->strcasecmp(name, item->name);
1 by brian
clean slate
444
}
445
2440.2.22 by Olaf van der Spek
Refactor
446
Item *Item::safe_charset_converter(const charset_info_st* tocs)
1 by brian
clean slate
447
{
448
  Item_func_conv_charset *conv= new Item_func_conv_charset(this, tocs, 1);
2440.2.22 by Olaf van der Spek
Refactor
449
  return conv->safe ? conv : NULL; // memory leak? (conv if not conv->safe)
1 by brian
clean slate
450
}
451
2104.2.8 by Brian Aker
Merge in reference from pointer.
452
bool Item::get_date(type::Time &ltime,uint32_t fuzzydate)
1 by brian
clean slate
453
{
2088.8.7 by Brian Aker
Merge in goto removal.
454
  do
455
  {
2114.5.1 by Brian Aker
Additional abstract around time (this also makes the abort_on_warnings in
456
    if (is_null())
457
    {
458
      break;
459
    }
460
    else if (result_type() == STRING_RESULT)
2088.8.7 by Brian Aker
Merge in goto removal.
461
    {
2148.5.3 by Brian Aker
Remove hard coded array for numbers (this also fixes a couple of spots where
462
      char buff[type::Time::MAX_STRING_LENGTH];
2440.2.22 by Olaf van der Spek
Refactor
463
      String tmp(buff,sizeof(buff), &my_charset_bin);
464
      String *res= val_str(&tmp);
2440.2.23 by Olaf van der Spek
Use str_ref
465
      if (not res || str_to_datetime_with_warn(getSession(), *res, ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
2088.8.7 by Brian Aker
Merge in goto removal.
466
      {
467
        break;
468
      }
469
    }
470
    else
471
    {
472
      int64_t value= val_int();
2098.3.3 by Brian Aker
Isolate out the time code/conversion.
473
      type::datetime_t date_value;
2104.2.6 by Brian Aker
Enum was_cut
474
2104.2.8 by Brian Aker
Merge in reference from pointer.
475
      ltime.convert(date_value, value, fuzzydate);
2104.2.6 by Brian Aker
Enum was_cut
476
2098.3.3 by Brian Aker
Isolate out the time code/conversion.
477
      if (not type::is_valid(date_value))
2088.8.7 by Brian Aker
Merge in goto removal.
478
      {
2440.2.22 by Olaf van der Spek
Refactor
479
        char buff[DECIMAL_LONGLONG_DIGITS];
480
        char* end= internal::int64_t10_to_str(value, buff, -10);
2440.2.23 by Olaf van der Spek
Use str_ref
481
        make_truncated_value_warning(getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, str_ref(buff, (int) (end-buff)), type::DRIZZLE_TIMESTAMP_NONE, NULL);
2088.8.7 by Brian Aker
Merge in goto removal.
482
        break;
483
      }
484
    }
485
486
    return false;
487
  } while (0);
488
2104.2.8 by Brian Aker
Merge in reference from pointer.
489
  ltime.reset();
2088.8.7 by Brian Aker
Merge in goto removal.
490
681 by Brian Aker
Style cleanup
491
  return true;
1 by brian
clean slate
492
}
493
2104.2.8 by Brian Aker
Merge in reference from pointer.
494
bool Item::get_time(type::Time &ltime)
1 by brian
clean slate
495
{
2148.5.3 by Brian Aker
Remove hard coded array for numbers (this also fixes a couple of spots where
496
  char buff[type::Time::MAX_STRING_LENGTH];
2440.2.23 by Olaf van der Spek
Use str_ref
497
  String tmp(buff,sizeof(buff),&my_charset_bin);
498
  String *res= val_str(&tmp);
499
  if (not res || str_to_time_with_warn(getSession(), *res, ltime))
1 by brian
clean slate
500
  {
2104.2.8 by Brian Aker
Merge in reference from pointer.
501
    ltime.reset();
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
502
    return true;
1 by brian
clean slate
503
  }
2088.8.7 by Brian Aker
Merge in goto removal.
504
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
505
  return false;
506
}
507
2104.2.8 by Brian Aker
Merge in reference from pointer.
508
bool Item::get_date_result(type::Time &ltime,uint32_t fuzzydate)
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
509
{
2104.2.8 by Brian Aker
Merge in reference from pointer.
510
  return get_date(ltime, fuzzydate);
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
511
}
512
513
bool Item::is_null()
514
{
515
  return false;
516
}
517
518
void Item::update_null_value ()
519
{
520
  (void) val_int();
521
}
522
523
void Item::top_level_item(void)
524
{}
525
526
void Item::set_result_field(Field *)
527
{}
528
529
bool Item::is_result_field(void)
530
{
681 by Brian Aker
Style cleanup
531
  return false;
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
532
}
533
534
bool Item::is_bool_func(void)
535
{
681 by Brian Aker
Style cleanup
536
  return false;
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
537
}
538
539
void Item::save_in_result_field(bool)
540
{}
541
542
void Item::no_rows_in_result(void)
543
{}
544
545
Item *Item::copy_or_same(Session *)
546
{
547
  return this;
548
}
549
550
Item *Item::copy_andor_structure(Session *)
551
{
552
  return this;
553
}
554
555
Item *Item::real_item(void)
556
{
557
  return this;
558
}
559
779.3.10 by Monty Taylor
Turned on -Wshadow.
560
const Item *Item::real_item(void) const
561
{
562
  return this;
563
}
564
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
565
Item *Item::get_tmp_table_item(Session *session)
566
{
567
  return copy_or_same(session);
568
}
569
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
570
const charset_info_st *Item::default_charset()
1 by brian
clean slate
571
{
748 by Brian Aker
Removal of client side collation.
572
  return current_session->variables.getCollation();
1 by brian
clean slate
573
}
574
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
575
const charset_info_st *Item::compare_collation()
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
576
{
577
  return NULL;
578
}
579
580
bool Item::walk(Item_processor processor, bool, unsigned char *arg)
581
{
582
  return (this->*processor)(arg);
583
}
584
2440.2.22 by Olaf van der Spek
Refactor
585
Item* Item::compile(Item_analyzer analyzer, unsigned char **arg_p, Item_transformer transformer, unsigned char *arg_t)
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
586
{
2440.2.22 by Olaf van der Spek
Refactor
587
  return (this->*analyzer)(arg_p)
588
    ? (this->*transformer)(arg_t)
589
    : NULL;
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
590
}
591
592
void Item::traverse_cond(Cond_traverser traverser, void *arg, traverse_order)
593
{
594
  (*traverser)(this, arg);
595
}
596
597
bool Item::remove_dependence_processor(unsigned char *)
598
{
681 by Brian Aker
Style cleanup
599
  return false;
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
600
}
601
602
bool Item::collect_item_field_processor(unsigned char *)
603
{
681 by Brian Aker
Style cleanup
604
  return false;
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
605
}
606
607
bool Item::find_item_in_field_list_processor(unsigned char *)
608
{
681 by Brian Aker
Style cleanup
609
  return false;
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
610
}
611
612
bool Item::change_context_processor(unsigned char *)
613
{
681 by Brian Aker
Style cleanup
614
  return false;
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
615
}
616
617
bool Item::register_field_in_read_map(unsigned char *)
618
{
681 by Brian Aker
Style cleanup
619
  return false;
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
620
}
621
622
bool Item::subst_argument_checker(unsigned char **arg)
623
{
624
  if (*arg)
625
    *arg= NULL;
626
  return true;
627
}
628
629
Item *Item::equal_fields_propagator(unsigned char *)
630
{
631
  return this;
632
}
633
634
bool Item::set_no_const_sub(unsigned char *)
635
{
636
  return false;
637
}
638
639
Item *Item::replace_equal_field(unsigned char *)
640
{
641
  return this;
642
}
643
644
uint32_t Item::cols()
645
{
646
  return 1;
647
}
648
649
Item* Item::element_index(uint32_t)
650
{
651
  return this;
652
}
653
654
Item** Item::addr(uint32_t)
655
{
681 by Brian Aker
Style cleanup
656
  return NULL;
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
657
}
658
659
bool Item::null_inside()
660
{
681 by Brian Aker
Style cleanup
661
  return false;
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
662
}
663
664
void Item::bring_value()
665
{}
666
667
Item *Item::neg_transformer(Session *)
668
{
669
  return NULL;
670
}
671
672
Item *Item::update_value_transformer(unsigned char *)
673
{
674
  return this;
675
}
676
677
void Item::delete_self()
678
{
679
  cleanup();
680
  delete this;
681
}
682
683
bool Item::result_as_int64_t()
684
{
685
  return false;
686
}
687
688
bool Item::is_expensive()
689
{
690
  if (is_expensive_cache < 0)
2318.7.14 by Olaf van der Spek
Refactor
691
    is_expensive_cache= walk(&Item::is_expensive_processor, 0, NULL);
584.4.8 by Monty Taylor
Moved stuff from item.h to item.cc
692
  return test(is_expensive_cache);
693
}
694
1 by brian
clean slate
695
/*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
696
 need a special class to adjust printing : references to aggregate functions
1 by brian
clean slate
697
 must not be printed as refs because the aggregate functions that are added to
698
 the front of select list are not printed as well.
699
*/
700
class Item_aggregate_ref : public Item_ref
701
{
702
public:
703
  Item_aggregate_ref(Name_resolution_context *context_arg, Item **item,
704
                  const char *table_name_arg, const char *field_name_arg)
705
    :Item_ref(context_arg, item, table_name_arg, field_name_arg) {}
706
2215.2.1 by Stewart Smith
remove enum_query_type which was effectively unused. It was set to one value once, compared to it once (i.e. always true) and passed around everywhere doing nothing.
707
  virtual inline void print (String *str)
1 by brian
clean slate
708
  {
709
    if (ref)
2215.2.1 by Stewart Smith
remove enum_query_type which was effectively unused. It was set to one value once, compared to it once (i.e. always true) and passed around everywhere doing nothing.
710
      (*ref)->print(str);
1 by brian
clean slate
711
    else
2215.2.1 by Stewart Smith
remove enum_query_type which was effectively unused. It was set to one value once, compared to it once (i.e. always true) and passed around everywhere doing nothing.
712
      Item_ident::print(str);
1 by brian
clean slate
713
  }
714
};
715
584.4.9 by Monty Taylor
Renamed split_sum_func2 to split_sum_func. It's C++.
716
void Item::split_sum_func(Session *session, Item **ref_pointer_array,
717
                          List<Item> &fields, Item **ref,
718
                          bool skip_registered)
1 by brian
clean slate
719
{
584.4.9 by Monty Taylor
Renamed split_sum_func2 to split_sum_func. It's C++.
720
  /* An item of type Item_sum  is registered <=> ref_by != 0 */
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
721
  if (type() == SUM_FUNC_ITEM && 
722
      skip_registered &&
1 by brian
clean slate
723
      ((Item_sum *) this)->ref_by)
584.4.9 by Monty Taylor
Renamed split_sum_func2 to split_sum_func. It's C++.
724
    return;
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
725
1 by brian
clean slate
726
  if ((type() != SUM_FUNC_ITEM && with_sum_func) ||
727
      (type() == FUNC_ITEM &&
728
       (((Item_func *) this)->functype() == Item_func::ISNOTNULLTEST_FUNC ||
729
        ((Item_func *) this)->functype() == Item_func::TRIG_COND_FUNC)))
730
  {
731
    /* Will split complicated items and ignore simple ones */
520.1.22 by Brian Aker
Second pass of thd cleanup
732
    split_sum_func(session, ref_pointer_array, fields);
1 by brian
clean slate
733
  }
734
  else if ((type() == SUM_FUNC_ITEM || (used_tables() & ~PARAM_TABLE_BIT)) &&
735
           type() != SUBSELECT_ITEM &&
609 by Brian Aker
Dead class removal (more views)
736
           type() != REF_ITEM)
1 by brian
clean slate
737
  {
738
    /*
739
      Replace item with a reference so that we can easily calculate
740
      it (in case of sum functions) or copy it (in case of fields)
741
742
      The test above is to ensure we don't do a reference for things
743
      that are constants (PARAM_TABLE_BIT is in effect a constant)
744
      or already referenced (for example an item in HAVING)
745
      Exception is Item_direct_view_ref which we need to convert to
746
      Item_ref to allow fields from view being stored in tmp table.
747
    */
748
    Item_aggregate_ref *item_ref;
2183.2.17 by Olaf van der Spek
Use List::size()
749
    uint32_t el= fields.size();
1 by brian
clean slate
750
    Item *real_itm= real_item();
751
752
    ref_pointer_array[el]= real_itm;
2318.6.90 by Olaf van der Spek
Refactor
753
    item_ref= new Item_aggregate_ref(&session->lex().current_select->context, ref_pointer_array + el, 0, name);
1 by brian
clean slate
754
    if (type() == SUM_FUNC_ITEM)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
755
      item_ref->depended_from= ((Item_sum *) this)->depended_from();
1 by brian
clean slate
756
    fields.push_front(real_itm);
2197.3.1 by Olaf van der Spek
Remove Session::change_item_tree
757
    *ref= item_ref;
1 by brian
clean slate
758
  }
759
}
760
761
/*
762
  Functions to convert item to field (for send_fields)
763
*/
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
764
bool Item::fix_fields(Session *, Item **)
1 by brian
clean slate
765
{
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
766
  /* We do not check fields which are fixed during construction */
767
  assert(! fixed || basic_const_item());
768
  fixed= true;
56 by brian
Next pass of true/false update.
769
  return false;
1 by brian
clean slate
770
}
771
846 by Brian Aker
Removing on typedeffed class.
772
void mark_as_dependent(Session *session, Select_Lex *last, Select_Lex *current,
1 by brian
clean slate
773
                              Item_ident *resolved_item,
774
                              Item_ident *mark_item)
775
{
776
  const char *db_name= (resolved_item->db_name ?
777
                        resolved_item->db_name : "");
778
  const char *table_name= (resolved_item->table_name ?
779
                           resolved_item->table_name : "");
846 by Brian Aker
Removing on typedeffed class.
780
  /* store pointer on Select_Lex from which item is dependent */
1 by brian
clean slate
781
  if (mark_item)
782
    mark_item->depended_from= last;
783
  current->mark_as_dependent(last);
2227.4.8 by Olaf van der Spek
Session::lex()
784
  if (session->lex().describe & DESCRIBE_EXTENDED)
1 by brian
clean slate
785
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
786
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
1366.1.17 by Siddharth Prakash Singh
changed another status of sprintf to snprintf
787
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_WARN_FIELD_RESOLVED),
1 by brian
clean slate
788
            db_name, (db_name[0] ? "." : ""),
789
            table_name, (table_name [0] ? "." : ""),
790
            resolved_item->field_name,
791
	    current->select_number, last->select_number);
520.1.22 by Brian Aker
Second pass of thd cleanup
792
    push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1 by brian
clean slate
793
		 ER_WARN_FIELD_RESOLVED, warn_buff);
794
  }
795
}
796
520.1.22 by Brian Aker
Second pass of thd cleanup
797
void mark_select_range_as_dependent(Session *session,
846 by Brian Aker
Removing on typedeffed class.
798
                                    Select_Lex *last_select,
799
                                    Select_Lex *current_sel,
1 by brian
clean slate
800
                                    Field *found_field, Item *found_item,
801
                                    Item_ident *resolved_item)
802
{
803
  /*
804
    Go from current SELECT to SELECT where field was resolved (it
805
    have to be reachable from current SELECT, because it was already
806
    done once when we resolved this field and cached result of
807
    resolving)
808
  */
846 by Brian Aker
Removing on typedeffed class.
809
  Select_Lex *previous_select= current_sel;
1 by brian
clean slate
810
  for (; previous_select->outer_select() != last_select;
811
       previous_select= previous_select->outer_select())
812
  {
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
813
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
1 by brian
clean slate
814
    prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
2060 by Brian Aker
Added native functions into the function table.
815
    prev_subselect_item->const_item_cache= false;
1 by brian
clean slate
816
  }
817
  {
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
818
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
1 by brian
clean slate
819
    Item_ident *dependent= resolved_item;
820
    if (found_field == view_ref_found)
821
    {
822
      Item::Type type= found_item->type();
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
823
      prev_subselect_item->used_tables_cache|= found_item->used_tables();
1 by brian
clean slate
824
      dependent= ((type == Item::REF_ITEM || type == Item::FIELD_ITEM) ?
825
                  (Item_ident*) found_item :
826
                  0);
827
    }
828
    else
1660.1.3 by Brian Aker
Encapsulate Table in field
829
      prev_subselect_item->used_tables_cache|= found_field->getTable()->map;
2060 by Brian Aker
Added native functions into the function table.
830
    prev_subselect_item->const_item_cache= false;
520.1.22 by Brian Aker
Second pass of thd cleanup
831
    mark_as_dependent(session, last_select, current_sel, resolved_item,
1 by brian
clean slate
832
                      dependent);
833
  }
834
}
835
836
/**
837
  Search a GROUP BY clause for a field with a certain name.
838
839
  Search the GROUP BY list for a column named as find_item. When searching
840
  preference is given to columns that are qualified with the same table (and
841
  database) name as the one being searched for.
842
843
  @param find_item     the item being searched for
844
  @param group_list    GROUP BY clause
845
846
  @return
847
    - the found item on success
848
    - NULL if find_item is not in group_list
849
*/
2148.5.3 by Brian Aker
Remove hard coded array for numbers (this also fixes a couple of spots where
850
static Item** find_field_in_group_list(Session *session, Item *find_item, Order *group_list)
1 by brian
clean slate
851
{
852
  const char *db_name;
853
  const char *table_name;
854
  const char *field_name;
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
855
  Order *found_group= NULL;
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
856
  int found_match_degree= 0;
1 by brian
clean slate
857
  Item_ident *cur_field;
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
858
  int cur_match_degree= 0;
859
  char name_buff[NAME_LEN+1];
1 by brian
clean slate
860
861
  if (find_item->type() == Item::FIELD_ITEM ||
862
      find_item->type() == Item::REF_ITEM)
863
  {
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
864
    db_name= ((Item_ident*) find_item)->db_name;
1 by brian
clean slate
865
    table_name= ((Item_ident*) find_item)->table_name;
866
    field_name= ((Item_ident*) find_item)->field_name;
867
  }
868
  else
869
    return NULL;
870
1039.1.5 by Brian Aker
Remove lower case filename bits (aka we just lock into the most compatible
871
  if (db_name)
1 by brian
clean slate
872
  {
873
    /* Convert database to lower case for comparison */
629.5.4 by Toru Maesaka
Fourth pass of replacing MySQL's strmake() with libc calls
874
    strncpy(name_buff, db_name, sizeof(name_buff)-1);
2456.1.4 by Olaf van der Spek
Refactor
875
    files_charset_info->casedn_str(name_buff);
1 by brian
clean slate
876
    db_name= name_buff;
877
  }
878
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
879
  assert(field_name != 0);
1 by brian
clean slate
880
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
881
  for (Order *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
1 by brian
clean slate
882
  {
883
    if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM)
884
    {
885
      cur_field= (Item_ident*) *cur_group->item;
886
      cur_match_degree= 0;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
887
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
888
      assert(cur_field->field_name != 0);
1 by brian
clean slate
889
2456.1.4 by Olaf van der Spek
Refactor
890
      if (system_charset_info->strcasecmp(cur_field->field_name, field_name))
1 by brian
clean slate
891
        continue;
2456.1.4 by Olaf van der Spek
Refactor
892
      ++cur_match_degree;
1 by brian
clean slate
893
894
      if (cur_field->table_name && table_name)
895
      {
896
        /* If field_name is qualified by a table name. */
2456.1.4 by Olaf van der Spek
Refactor
897
        if (table_alias_charset->strcasecmp(cur_field->table_name, table_name))
1 by brian
clean slate
898
          /* Same field names, different tables. */
899
          return NULL;
900
901
        ++cur_match_degree;
902
        if (cur_field->db_name && db_name)
903
        {
904
          /* If field_name is also qualified by a database name. */
2456.1.4 by Olaf van der Spek
Refactor
905
          if (system_charset_info->strcasecmp(cur_field->db_name, db_name))
2085.2.3 by Brian Aker
Fix strcasecmp issues (ie, check UTF-8).
906
          {
1 by brian
clean slate
907
            /* Same field names, different databases. */
908
            return NULL;
2085.2.3 by Brian Aker
Fix strcasecmp issues (ie, check UTF-8).
909
          }
1 by brian
clean slate
910
          ++cur_match_degree;
911
        }
912
      }
913
914
      if (cur_match_degree > found_match_degree)
915
      {
916
        found_match_degree= cur_match_degree;
917
        found_group= cur_group;
918
      }
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
919
      else if (found_group &&
920
               (cur_match_degree == found_match_degree) &&
1 by brian
clean slate
921
               ! (*(found_group->item))->eq(cur_field, 0))
922
      {
923
        /*
924
          If the current resolve candidate matches equally well as the current
925
          best match, they must reference the same column, otherwise the field
926
          is ambiguous.
927
        */
2148.5.3 by Brian Aker
Remove hard coded array for numbers (this also fixes a couple of spots where
928
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), session->where());
1 by brian
clean slate
929
        return NULL;
930
      }
931
    }
932
  }
933
934
  if (found_group)
935
    return found_group->item;
2024.1.3 by Brian Aker
Finish up issues with unsigned/int by fixing cast().
936
937
  return NULL;
1 by brian
clean slate
938
}
939
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
940
Item** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
1 by brian
clean slate
941
{
942
  Item **group_by_ref= NULL;
943
  Item **select_ref= NULL;
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
944
  Order *group_list= (Order*) select->group_list.first;
56 by brian
Next pass of true/false update.
945
  bool ambiguous_fields= false;
482 by Brian Aker
Remove uint.
946
  uint32_t counter;
1 by brian
clean slate
947
  enum_resolution_type resolution;
948
949
  /*
950
    Search for a column or derived column named as 'ref' in the SELECT
951
    clause of the current select.
952
  */
1578.6.3 by Brian Aker
More current_session removal.
953
  if (!(select_ref= find_item_in_list(session,
954
                                      ref, *(select->get_item_list()),
1 by brian
clean slate
955
                                      &counter, REPORT_EXCEPT_NOT_FOUND,
956
                                      &resolution)))
957
    return NULL; /* Some error occurred. */
958
  if (resolution == RESOLVED_AGAINST_ALIAS)
56 by brian
Next pass of true/false update.
959
    ref->alias_name_used= true;
1 by brian
clean slate
960
961
  /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
962
  if (select->having_fix_field && !ref->with_sum_func && group_list)
963
  {
2148.5.3 by Brian Aker
Remove hard coded array for numbers (this also fixes a couple of spots where
964
    group_by_ref= find_field_in_group_list(session, ref, group_list);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
965
1 by brian
clean slate
966
    /* Check if the fields found in SELECT and GROUP BY are the same field. */
967
    if (group_by_ref && (select_ref != not_found_item) &&
968
        !((*group_by_ref)->eq(*select_ref, 0)))
969
    {
56 by brian
Next pass of true/false update.
970
      ambiguous_fields= true;
520.1.22 by Brian Aker
Second pass of thd cleanup
971
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
1 by brian
clean slate
972
                          ER(ER_NON_UNIQ_ERROR), ref->full_name(),
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
973
                          session->where());
1 by brian
clean slate
974
975
    }
976
  }
977
978
  if (select_ref != not_found_item || group_by_ref)
979
  {
980
    if (select_ref != not_found_item && !ambiguous_fields)
981
    {
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
982
      assert(*select_ref != 0);
1 by brian
clean slate
983
      if (!select->ref_pointer_array[counter])
984
      {
985
        my_error(ER_ILLEGAL_REFERENCE, MYF(0),
986
                 ref->name, "forward reference in item list");
987
        return NULL;
988
      }
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
989
      assert((*select_ref)->fixed);
1 by brian
clean slate
990
      return (select->ref_pointer_array + counter);
991
    }
992
    if (group_by_ref)
993
      return group_by_ref;
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
994
    assert(false);
1 by brian
clean slate
995
    return NULL; /* So there is no compiler warning. */
996
  }
997
998
  return (Item**) not_found_item;
999
}
1000
2385.2.10 by Olaf van der Spek
Refactor
1001
void Item::init_make_field(SendField *tmp_field, enum_field_types field_type_arg)
1 by brian
clean slate
1002
{
2385.2.10 by Olaf van der Spek
Refactor
1003
  tmp_field->db_name=	"";
1004
  tmp_field->org_table_name= "";
1005
  tmp_field->org_col_name= "";
1006
  tmp_field->table_name= "";
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
1007
  tmp_field->col_name= name;
1008
  tmp_field->charsetnr= collation.collation->number;
2445.1.4 by Olaf van der Spek
Refactor
1009
  tmp_field->flags= (maybe_null ? 0 : NOT_NULL_FLAG) | (collation.collation->binary_compare() ? BINARY_FLAG : 0);
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
1010
  tmp_field->type= field_type_arg;
1011
  tmp_field->length= max_length;
1012
  tmp_field->decimals= decimals;
1 by brian
clean slate
1013
}
1014
1052.2.4 by Nathan Williams
No actual code changes. Changed Send_field to SendField to be consistent with coding standards.
1015
void Item::make_field(SendField *tmp_field)
1 by brian
clean slate
1016
{
1017
  init_make_field(tmp_field, field_type());
1018
}
1019
1020
enum_field_types Item::string_field_type() const
1021
{
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
1022
  enum_field_types f_type= DRIZZLE_TYPE_VARCHAR;
113 by Brian Aker
First pass on removing blob internals.
1023
  if (max_length >= 65536)
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1024
    f_type= DRIZZLE_TYPE_BLOB;
1 by brian
clean slate
1025
  return f_type;
1026
}
1027
1028
enum_field_types Item::field_type() const
1029
{
1030
  switch (result_type()) {
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
1031
  case STRING_RESULT:  
1032
    return string_field_type();
1033
  case INT_RESULT:     
1034
    return DRIZZLE_TYPE_LONGLONG;
1035
  case DECIMAL_RESULT: 
1211.1.1 by Brian Aker
Updating with my change to to DECIMAL from NEWDECIMAL and Stewart's update
1036
    return DRIZZLE_TYPE_DECIMAL;
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
1037
  case REAL_RESULT:    
1038
    return DRIZZLE_TYPE_DOUBLE;
1 by brian
clean slate
1039
  case ROW_RESULT:
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
1040
    assert(0);
1 by brian
clean slate
1041
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
1042
1043
  abort();
1 by brian
clean slate
1044
}
1045
1046
bool Item::is_datetime()
1047
{
2167.3.1 by Brian Aker
Fix issues where int display length may be too small, and fix collation
1048
  return field::isDateTime(field_type());
1 by brian
clean slate
1049
}
1050
1051
String *Item::check_well_formed_result(String *str, bool send_error)
1052
{
1053
  /* Check whether we got a well-formed string */
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
1054
  const charset_info_st * const cs= str->charset();
1 by brian
clean slate
1055
  int well_formed_error;
2440.2.14 by Olaf van der Spek
Refactor
1056
  uint32_t wlen= cs->cset->well_formed_len(*cs, *str, str->length(), &well_formed_error);
1 by brian
clean slate
1057
  if (wlen < str->length())
1058
  {
1059
    char hexbuf[7];
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
1060
    enum DRIZZLE_ERROR::enum_warning_level level;
482 by Brian Aker
Remove uint.
1061
    uint32_t diff= str->length() - wlen;
937.2.6 by Stewart Smith
make set_if_bigger typesafe for C and C++. Fix up everywhere.
1062
    set_if_smaller(diff, 3U);
971.3.9 by Eric Day
Removed dependency for hex convert, fixed a few Protocol class issues for Solaris.
1063
    (void) drizzled_string_to_hex(hexbuf, str->ptr() + wlen, diff);
1 by brian
clean slate
1064
    if (send_error)
1065
    {
1066
      my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
1067
               cs->csname,  hexbuf);
681 by Brian Aker
Style cleanup
1068
      return NULL;
1 by brian
clean slate
1069
    }
1070
    {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
1071
      level= DRIZZLE_ERROR::WARN_LEVEL_ERROR;
1 by brian
clean slate
1072
      null_value= 1;
1073
      str= 0;
1074
    }
2148.5.3 by Brian Aker
Remove hard coded array for numbers (this also fixes a couple of spots where
1075
    push_warning_printf(&getSession(), level, ER_INVALID_CHARACTER_STRING,
1 by brian
clean slate
1076
                        ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
1077
  }
1078
  return str;
1079
}
1080
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
1081
bool Item::eq_by_collation(Item *item, bool binary_cmp, const charset_info_st * const cs)
1 by brian
clean slate
1082
{
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
1083
  const charset_info_st *save_cs= 0;
1084
  const charset_info_st *save_item_cs= 0;
1 by brian
clean slate
1085
  if (collation.collation != cs)
1086
  {
1087
    save_cs= collation.collation;
1088
    collation.collation= cs;
1089
  }
1090
  if (item->collation.collation != cs)
1091
  {
1092
    save_item_cs= item->collation.collation;
1093
    item->collation.collation= cs;
1094
  }
1095
  bool res= eq(item, binary_cmp);
1096
  if (save_cs)
1097
    collation.collation= save_cs;
1098
  if (save_item_cs)
1099
    item->collation.collation= save_item_cs;
1100
  return res;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1101
}
1 by brian
clean slate
1102
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1103
Field *Item::make_string_field(Table *table)
1 by brian
clean slate
1104
{
1105
  Field *field;
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
1106
  assert(collation.collation);
1 by brian
clean slate
1107
  if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB)
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1108
  {
1 by brian
clean slate
1109
    field= new Field_blob(max_length, maybe_null, name,
1110
                          collation.collation);
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1111
  }
241 by Brian Aker
First pass of CHAR removal.
1112
  else
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1113
  {
1114
    table->setVariableWidth();
1115
    field= new Field_varstring(max_length, maybe_null, name,
1 by brian
clean slate
1116
                               collation.collation);
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1117
  }
241 by Brian Aker
First pass of CHAR removal.
1118
1 by brian
clean slate
1119
  if (field)
1120
    field->init(table);
1121
  return field;
1122
}
1123
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1124
Field *Item::tmp_table_field_from_field_type(Table *table, bool)
1 by brian
clean slate
1125
{
1126
  /*
1127
    The field functions defines a field to be not null if null_ptr is not 0
1128
  */
481 by Brian Aker
Remove all of uchar.
1129
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
1996.2.1 by Brian Aker
uuid type code.
1130
  Field *field= NULL;
1 by brian
clean slate
1131
1132
  switch (field_type()) {
1211.1.1 by Brian Aker
Updating with my change to to DECIMAL from NEWDECIMAL and Stewart's update
1133
  case DRIZZLE_TYPE_DECIMAL:
1134
    field= new Field_decimal((unsigned char*) 0,
1119.9.6 by Jay Pipes
Removes the FIELDFLAG_DECIMAL, the f_is_dec() macro, and unscrews the unsigned flag in various places.
1135
                                 max_length,
1136
                                 null_ptr,
1137
                                 0,
1138
                                 Field::NONE,
1139
                                 name,
2137.1.3 by Brian Aker
Move decimal_zero, and a light fix on the decimal_t type (ditch the
1140
                                 decimals);
1 by brian
clean slate
1141
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1142
  case DRIZZLE_TYPE_LONG:
2007 by Brian Aker
Refactor naming for integers.
1143
    field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
1 by brian
clean slate
1144
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1145
  case DRIZZLE_TYPE_LONGLONG:
2007 by Brian Aker
Refactor naming for integers.
1146
    field= new field::Int64((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
1 by brian
clean slate
1147
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1148
  case DRIZZLE_TYPE_DOUBLE:
481 by Brian Aker
Remove all of uchar.
1149
    field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
2007 by Brian Aker
Refactor naming for integers.
1150
                            name, decimals, 0, unsigned_flag);
1 by brian
clean slate
1151
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1152
  case DRIZZLE_TYPE_NULL:
2167.3.1 by Brian Aker
Fix issues where int display length may be too small, and fix collation
1153
    field= new Field_null((unsigned char*) 0, max_length, name);
1 by brian
clean slate
1154
    break;
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
1155
  case DRIZZLE_TYPE_DATE:
2167.3.1 by Brian Aker
Fix issues where int display length may be too small, and fix collation
1156
    field= new Field_date(maybe_null, name);
1 by brian
clean slate
1157
    break;
2046.2.1 by Brian Aker
First pass on micro timestamp.
1158
1159
  case DRIZZLE_TYPE_MICROTIME:
1160
    field= new field::Microtime(maybe_null, name);
1161
    break;
1162
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1163
  case DRIZZLE_TYPE_TIMESTAMP:
2046.2.1 by Brian Aker
First pass on micro timestamp.
1164
    field= new field::Epoch(maybe_null, name);
1 by brian
clean slate
1165
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1166
  case DRIZZLE_TYPE_DATETIME:
2167.3.1 by Brian Aker
Fix issues where int display length may be too small, and fix collation
1167
    field= new Field_datetime(maybe_null, name);
1 by brian
clean slate
1168
    break;
1999.4.7 by Brian Aker
Merge in first pass of TIME type (closer to EPOCH time).
1169
  case DRIZZLE_TYPE_TIME:
2167.3.1 by Brian Aker
Fix issues where int display length may be too small, and fix collation
1170
    field= new field::Time(maybe_null, name);
1999.4.7 by Brian Aker
Merge in first pass of TIME type (closer to EPOCH time).
1171
    break;
2023.2.2 by Brian Aker
Merge in BOOL change to BOOLEAN.
1172
  case DRIZZLE_TYPE_BOOLEAN:
1996.2.1 by Brian Aker
uuid type code.
1173
  case DRIZZLE_TYPE_UUID:
2398.1.1 by Muhammad Umair
merge lp:~mumair/drizzle/drizzle-IPv6Address
1174
  case DRIZZLE_TYPE_IPV6:
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1175
  case DRIZZLE_TYPE_ENUM:
1176
  case DRIZZLE_TYPE_VARCHAR:
1 by brian
clean slate
1177
    return make_string_field(table);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1178
  case DRIZZLE_TYPE_BLOB:
1 by brian
clean slate
1179
      field= new Field_blob(max_length, maybe_null, name, collation.collation);
1180
    break;					// Blob handled outside of case
1181
  }
1996.2.1 by Brian Aker
uuid type code.
1182
  assert(field);
1183
1 by brian
clean slate
1184
  if (field)
1185
    field->init(table);
1186
  return field;
1187
}
1188
1189
/*
1190
  This implementation can lose str_value content, so if the
1191
  Item uses str_value to store something, it should
1192
  reimplement it's ::save_in_field() as Item_string, for example, does
1193
*/
1194
int Item::save_in_field(Field *field, bool no_conversions)
1195
{
1196
  int error;
1197
  if (result_type() == STRING_RESULT)
1198
  {
1199
    String *result;
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
1200
    const charset_info_st * const cs= collation.collation;
1 by brian
clean slate
1201
    char buff[MAX_FIELD_WIDTH];		// Alloc buffer for small columns
1202
    str_value.set_quick(buff, sizeof(buff), cs);
1203
    result=val_str(&str_value);
1204
    if (null_value)
1205
    {
1206
      str_value.set_quick(0, 0, cs);
1207
      return set_field_to_null_with_conversions(field, no_conversions);
1208
    }
1209
56 by brian
Next pass of true/false update.
1210
    /* NOTE: If null_value == false, "result" must be not NULL.  */
1 by brian
clean slate
1211
1212
    field->set_notnull();
1213
    error=field->store(result->ptr(),result->length(),cs);
1214
    str_value.set_quick(0, 0, cs);
1215
  }
1216
  else if (result_type() == REAL_RESULT &&
1217
           field->result_type() == STRING_RESULT)
1218
  {
1219
    double nr= val_real();
1220
    if (null_value)
1221
      return set_field_to_null_with_conversions(field, no_conversions);
1222
    field->set_notnull();
1223
    error= field->store(nr);
1224
  }
1225
  else if (result_type() == REAL_RESULT)
1226
  {
1227
    double nr= val_real();
1228
    if (null_value)
1229
      return set_field_to_null(field);
1230
    field->set_notnull();
1231
    error=field->store(nr);
1232
  }
1233
  else if (result_type() == DECIMAL_RESULT)
1234
  {
2030.1.4 by Brian Aker
Change my_decimal to Decimal
1235
    type::Decimal decimal_value;
1236
    type::Decimal *value= val_decimal(&decimal_value);
1 by brian
clean slate
1237
    if (null_value)
1238
      return set_field_to_null_with_conversions(field, no_conversions);
1239
    field->set_notnull();
1240
    error=field->store_decimal(value);
1241
  }
1242
  else
1243
  {
152 by Brian Aker
longlong replacement
1244
    int64_t nr=val_int();
1 by brian
clean slate
1245
    if (null_value)
1246
      return set_field_to_null_with_conversions(field, no_conversions);
1247
    field->set_notnull();
1248
    error=field->store(nr, unsigned_flag);
1249
  }
1250
  return error;
1251
}
1252
1240.8.4 by Dennis Schoen
add Item functions
1253
/**
1254
  Check if an item is a constant one and can be cached.
1255
1256
  @param arg [out] TRUE <=> Cache this item.
1257
1258
  @return TRUE  Go deeper in item tree.
1259
  @return FALSE Don't go deeper in item tree.
1260
*/
1261
1262
bool Item::cache_const_expr_analyzer(unsigned char **arg)
1263
{
1264
  bool *cache_flag= (bool*)*arg;
1265
  if (!*cache_flag)
1266
  {
1267
    Item *item= real_item();
1268
    /*
1269
      Cache constant items unless it's a basic constant, constant field or
1270
      a subselect (they use their own cache).
1271
    */
1272
    if (const_item() &&
1273
        !(item->basic_const_item() || item->type() == Item::FIELD_ITEM ||
1274
          item->type() == SUBSELECT_ITEM ||
1275
           /*
1276
             Do not cache GET_USER_VAR() function as its const_item() may
1277
             return TRUE for the current thread but it still may change
1278
             during the execution.
1279
           */
1280
          (item->type() == Item::FUNC_ITEM &&
1281
           ((Item_func*)item)->functype() == Item_func::GUSERVAR_FUNC)))
1282
      *cache_flag= true;
1283
    return true;
1284
  }
1285
  return false;
1286
}
1287
1288
/**
1289
  Cache item if needed.
1290
1291
  @param arg   TRUE <=> Cache this item.
1292
1293
  @return cache if cache needed.
1294
  @return this otherwise.
1295
*/
1296
1297
Item* Item::cache_const_expr_transformer(unsigned char *arg)
1298
{
1299
  if (*(bool*)arg)
1300
  {
1301
    *((bool*)arg)= false;
1302
    Item_cache *cache= Item_cache::get_cache(this);
1303
    if (!cache)
1304
      return NULL;
1305
    cache->setup(this);
1306
    cache->store(this);
1307
    return cache;
1308
  }
1309
  return this;
1310
}
1311
2313.3.7 by Olaf van der Spek
Return void
1312
void Item::send(plugin::Client *client, String *buffer)
1 by brian
clean slate
1313
{
2313.3.7 by Olaf van der Spek
Return void
1314
  switch (field_type())
1315
  {
1996.2.1 by Brian Aker
uuid type code.
1316
  case DRIZZLE_TYPE_DATE:
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1317
  case DRIZZLE_TYPE_NULL:
1318
  case DRIZZLE_TYPE_ENUM:
1319
  case DRIZZLE_TYPE_BLOB:
1320
  case DRIZZLE_TYPE_VARCHAR:
2023.2.2 by Brian Aker
Merge in BOOL change to BOOLEAN.
1321
  case DRIZZLE_TYPE_BOOLEAN:
1996.2.1 by Brian Aker
uuid type code.
1322
  case DRIZZLE_TYPE_UUID:
2398.1.1 by Muhammad Umair
merge lp:~mumair/drizzle/drizzle-IPv6Address
1323
  case DRIZZLE_TYPE_IPV6:
1211.1.1 by Brian Aker
Updating with my change to to DECIMAL from NEWDECIMAL and Stewart's update
1324
  case DRIZZLE_TYPE_DECIMAL:
1996.2.1 by Brian Aker
uuid type code.
1325
    {
2313.3.6 by Olaf van der Spek
Return void
1326
      if (String* res=val_str(buffer))
1327
        client->store(res->ptr(), res->length());
1996.2.1 by Brian Aker
uuid type code.
1328
      break;
1329
    }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1330
  case DRIZZLE_TYPE_LONG:
1996.2.1 by Brian Aker
uuid type code.
1331
    {
2313.3.6 by Olaf van der Spek
Return void
1332
      int64_t nr= val_int();
1996.2.1 by Brian Aker
uuid type code.
1333
      if (!null_value)
2313.3.6 by Olaf van der Spek
Return void
1334
        client->store((int32_t)nr);
1996.2.1 by Brian Aker
uuid type code.
1335
      break;
1336
    }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1337
  case DRIZZLE_TYPE_LONGLONG:
971.3.17 by Eric Day
Cleaned up int/date related store functions.
1338
    {
2313.3.6 by Olaf van der Spek
Return void
1339
      int64_t nr= val_int();
1996.2.1 by Brian Aker
uuid type code.
1340
      if (!null_value)
1341
      {
1342
        if (unsigned_flag)
2313.3.6 by Olaf van der Spek
Return void
1343
          client->store((uint64_t)nr);
1996.2.1 by Brian Aker
uuid type code.
1344
        else
2313.3.6 by Olaf van der Spek
Return void
1345
          client->store((int64_t)nr);
1996.2.1 by Brian Aker
uuid type code.
1346
      }
1347
      break;
971.3.17 by Eric Day
Cleaned up int/date related store functions.
1348
    }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1349
  case DRIZZLE_TYPE_DOUBLE:
1996.2.1 by Brian Aker
uuid type code.
1350
    {
1351
      double nr= val_real();
1352
      if (!null_value)
2313.3.6 by Olaf van der Spek
Return void
1353
        client->store(nr, decimals, buffer);
1996.2.1 by Brian Aker
uuid type code.
1354
      break;
1355
    }
1999.4.7 by Brian Aker
Merge in first pass of TIME type (closer to EPOCH time).
1356
  case DRIZZLE_TYPE_TIME:
1999.4.12 by Brian Aker
Adds additional testing, and fixes CAST again.
1357
    {
2030.1.5 by Brian Aker
Update for moving DRIZZLE_TIME to type::Time
1358
      type::Time tm;
2104.2.8 by Brian Aker
Merge in reference from pointer.
1359
      get_time(tm);
1999.4.12 by Brian Aker
Adds additional testing, and fixes CAST again.
1360
      if (not null_value)
2313.3.6 by Olaf van der Spek
Return void
1361
        client->store(&tm);
1999.4.12 by Brian Aker
Adds additional testing, and fixes CAST again.
1362
      break;
1363
    }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1364
  case DRIZZLE_TYPE_DATETIME:
2046.2.1 by Brian Aker
First pass on micro timestamp.
1365
  case DRIZZLE_TYPE_MICROTIME:
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1366
  case DRIZZLE_TYPE_TIMESTAMP:
1996.2.1 by Brian Aker
uuid type code.
1367
    {
2030.1.5 by Brian Aker
Update for moving DRIZZLE_TIME to type::Time
1368
      type::Time tm;
2104.2.8 by Brian Aker
Merge in reference from pointer.
1369
      get_date(tm, TIME_FUZZY_DATE);
1996.2.1 by Brian Aker
uuid type code.
1370
      if (!null_value)
2313.3.6 by Olaf van der Spek
Return void
1371
        client->store(&tm);
1996.2.1 by Brian Aker
uuid type code.
1372
      break;
1373
    }
1 by brian
clean slate
1374
  }
1375
  if (null_value)
2313.3.6 by Olaf van der Spek
Return void
1376
    client->store();
1 by brian
clean slate
1377
}
1378
2154.2.4 by Brian Aker
This fixes 716459
1379
uint32_t Item::max_char_length() const
1380
{
1381
  return max_length / collation.collation->mbmaxlen;
1382
}
1383
1384
void Item::fix_char_length(uint32_t max_char_length_arg)
1385
{ 
1386
  max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen);
1387
}
1388
1 by brian
clean slate
1389
Item_result item_cmp_type(Item_result a,Item_result b)
1390
{
1391
  if (a == STRING_RESULT && b == STRING_RESULT)
1392
    return STRING_RESULT;
2008 by Brian Aker
Formatting + remove default from switch/case.
1393
1 by brian
clean slate
1394
  if (a == INT_RESULT && b == INT_RESULT)
1395
    return INT_RESULT;
1396
  else if (a == ROW_RESULT || b == ROW_RESULT)
1397
    return ROW_RESULT;
2008 by Brian Aker
Formatting + remove default from switch/case.
1398
1 by brian
clean slate
1399
  if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
1400
      (b == INT_RESULT || b == DECIMAL_RESULT))
1401
    return DECIMAL_RESULT;
2008 by Brian Aker
Formatting + remove default from switch/case.
1402
1 by brian
clean slate
1403
  return REAL_RESULT;
1404
}
1405
520.1.22 by Brian Aker
Second pass of thd cleanup
1406
void resolve_const_item(Session *session, Item **ref, Item *comp_item)
1 by brian
clean slate
1407
{
1408
  Item *item= *ref;
1409
  Item *new_item= NULL;
1410
  if (item->basic_const_item())
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
1411
    return; /* Can't be better */
2318.6.97 by Olaf van der Spek
Remove unused Item::orig_name
1412
  Item_result res_type=item_cmp_type(comp_item->result_type(), item->result_type());
1413
  const char *name=item->name; /* Alloced by memory::sql_alloc */
1 by brian
clean slate
1414
2318.6.97 by Olaf van der Spek
Remove unused Item::orig_name
1415
  switch (res_type) 
1416
  {
1 by brian
clean slate
1417
  case STRING_RESULT:
1418
    {
2008 by Brian Aker
Formatting + remove default from switch/case.
1419
      char buff[MAX_FIELD_WIDTH];
1420
      String tmp(buff,sizeof(buff),&my_charset_bin),*result;
1421
      result=item->val_str(&tmp);
1422
      if (item->null_value)
1423
        new_item= new Item_null(name);
1424
      else
2318.9.11 by Olaf van der Spek
Use sql_strdup(str_ref)
1425
        new_item= new Item_string(name, memory::sql_strdup(*result), result->length(), result->charset());
2008 by Brian Aker
Formatting + remove default from switch/case.
1426
      break;
1 by brian
clean slate
1427
    }
1428
  case INT_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
1429
    {
2318.7.14 by Olaf van der Spek
Refactor
1430
      new_item= item->null_value ? (Item*)new Item_null(name) : (Item*)new Item_int(name, item->val_int(), item->max_length);
2008 by Brian Aker
Formatting + remove default from switch/case.
1431
      break;
1432
    }
1 by brian
clean slate
1433
  case ROW_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
1434
    if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
1435
    {
1436
      /*
1437
        Substitute constants only in Item_rows. Don't affect other Items
1438
        with ROW_RESULT (eg Item_singlerow_subselect).
1 by brian
clean slate
1439
2008 by Brian Aker
Formatting + remove default from switch/case.
1440
        For such Items more optimal is to detect if it is constant and replace
1441
        it with Item_row. This would optimize queries like this:
1442
        SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
1443
      */
1444
      Item_row *item_row= (Item_row*) item;
1445
      Item_row *comp_item_row= (Item_row*) comp_item;
1446
      new_item= 0;
1447
      /*
1448
        If item and comp_item are both Item_rows and have same number of cols
1449
        then process items in Item_row one by one.
1450
        We can't ignore NULL values here as this item may be used with <=>, in
1451
        which case NULL's are significant.
1452
      */
1453
      assert(item->result_type() == comp_item->result_type());
1454
      assert(item_row->cols() == comp_item_row->cols());
2318.7.14 by Olaf van der Spek
Refactor
1455
      for (uint32_t col= item_row->cols(); col--; )
1456
        resolve_const_item(session, item_row->addr(col), comp_item_row->element_index(col));
2008 by Brian Aker
Formatting + remove default from switch/case.
1457
      break;
1458
    }
1459
    /* Fallthrough */
1 by brian
clean slate
1460
  case REAL_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
1461
    {						// It must REAL_RESULT
1462
      double result= item->val_real();
1463
      uint32_t length=item->max_length,decimals=item->decimals;
1464
      bool null_value=item->null_value;
1465
      new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
1466
                 new Item_float(name, result, decimals, length));
1467
      break;
1468
    }
1 by brian
clean slate
1469
  case DECIMAL_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
1470
    {
2030.1.4 by Brian Aker
Change my_decimal to Decimal
1471
      type::Decimal decimal_value;
1472
      type::Decimal *result= item->val_decimal(&decimal_value);
2008 by Brian Aker
Formatting + remove default from switch/case.
1473
      uint32_t length= item->max_length, decimals= item->decimals;
1474
      bool null_value= item->null_value;
1475
      new_item= (null_value ?
1476
                 (Item*) new Item_null(name) :
1477
                 (Item*) new Item_decimal(name, result, length, decimals));
1478
      break;
1479
    }
1480
  }
1481
1 by brian
clean slate
1482
  if (new_item)
2197.3.1 by Olaf van der Spek
Remove Session::change_item_tree
1483
    *ref= new_item;
1 by brian
clean slate
1484
}
1485
1486
bool field_is_equal_to_item(Field *field,Item *item)
1487
{
1488
1489
  Item_result res_type=item_cmp_type(field->result_type(),
1490
				     item->result_type());
1491
  if (res_type == STRING_RESULT)
1492
  {
1493
    char item_buff[MAX_FIELD_WIDTH];
1494
    char field_buff[MAX_FIELD_WIDTH];
1495
    String item_tmp(item_buff,sizeof(item_buff),&my_charset_bin),*item_result;
1496
    String field_tmp(field_buff,sizeof(field_buff),&my_charset_bin);
1497
    item_result=item->val_str(&item_tmp);
1498
    if (item->null_value)
1499
      return 1;					// This must be true
1996.2.1 by Brian Aker
uuid type code.
1500
    field->val_str_internal(&field_tmp);
1501
    return not stringcmp(&field_tmp,item_result);
1 by brian
clean slate
1502
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
1503
1 by brian
clean slate
1504
  if (res_type == INT_RESULT)
1505
    return 1;					// Both where of type int
2008 by Brian Aker
Formatting + remove default from switch/case.
1506
1 by brian
clean slate
1507
  if (res_type == DECIMAL_RESULT)
1508
  {
2030.1.4 by Brian Aker
Change my_decimal to Decimal
1509
    type::Decimal item_buf, *item_val,
1 by brian
clean slate
1510
               field_buf, *field_val;
1511
    item_val= item->val_decimal(&item_buf);
1512
    if (item->null_value)
1513
      return 1;					// This must be true
1514
    field_val= field->val_decimal(&field_buf);
2030.1.2 by Brian Aker
First pass in refactoring of the name of my_decimal.
1515
    return !class_decimal_cmp(item_val, field_val);
1 by brian
clean slate
1516
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
1517
1 by brian
clean slate
1518
  double result= item->val_real();
1519
  if (item->null_value)
1520
    return 1;
2008 by Brian Aker
Formatting + remove default from switch/case.
1521
1 by brian
clean slate
1522
  return result == field->val_real();
1523
}
1524
1525
/**
575.4.7 by Monty Taylor
More header cleanup.
1526
  Create field for temporary table using type of given item.
1527
1528
  @param session                   Thread handler
1529
  @param item                  Item to create a field for
1530
  @param table                 Temporary table
1531
  @param copy_func             If set and item is a function, store copy of
1532
                               item in this array
1533
  @param modify_item           1 if item->result_field should point to new
1534
                               item. This is relevent for how fill_record()
1535
                               is going to work:
1536
                               If modify_item is 1 then fill_record() will
1537
                               update the record in the original table.
1538
                               If modify_item is 0 then fill_record() will
1539
                               update the temporary table
1540
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
1541
                               field instead of blob.
1542
1543
  @retval
1544
    0  on error
1545
  @retval
1546
    new_created field
1547
*/
1548
static Field *create_tmp_field_from_item(Session *,
1549
                                         Item *item, Table *table,
1550
                                         Item ***copy_func, bool modify_item,
1551
                                         uint32_t convert_blob_length)
1552
{
1553
  bool maybe_null= item->maybe_null;
2008 by Brian Aker
Formatting + remove default from switch/case.
1554
  Field *new_field= NULL;
575.4.7 by Monty Taylor
More header cleanup.
1555
1556
  switch (item->result_type()) {
1557
  case REAL_RESULT:
1558
    new_field= new Field_double(item->max_length, maybe_null,
1559
                                item->name, item->decimals, true);
1560
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
1561
575.4.7 by Monty Taylor
More header cleanup.
1562
  case INT_RESULT:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1563
    /*
575.4.7 by Monty Taylor
More header cleanup.
1564
      Select an integer type with the minimal fit precision.
1565
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1566
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
2008 by Brian Aker
Formatting + remove default from switch/case.
1567
      Int32 -> make them field::Int64.
575.4.7 by Monty Taylor
More header cleanup.
1568
    */
2008.2.3 by Brian Aker
Fixing up a, somewhat, hidden unsigned type to solve a few issues around
1569
    if (item->unsigned_flag)
1570
    {
2430.1.6 by Olaf van der Spek
Merge trunk
1571
      new_field= new field::Size(item->max_length, maybe_null, item->name, item->unsigned_flag);
2008.2.3 by Brian Aker
Fixing up a, somewhat, hidden unsigned type to solve a few issues around
1572
    }
2430.1.6 by Olaf van der Spek
Merge trunk
1573
    else if (item->max_length >= MY_INT32_NUM_DECIMAL_DIGITS - 1)
2008.2.3 by Brian Aker
Fixing up a, somewhat, hidden unsigned type to solve a few issues around
1574
    {
2430.1.6 by Olaf van der Spek
Merge trunk
1575
      new_field= new field::Int64(item->max_length, maybe_null, item->name, item->unsigned_flag);
2008.2.3 by Brian Aker
Fixing up a, somewhat, hidden unsigned type to solve a few issues around
1576
    }
575.4.7 by Monty Taylor
More header cleanup.
1577
    else
2008.2.3 by Brian Aker
Fixing up a, somewhat, hidden unsigned type to solve a few issues around
1578
    {
2430.1.6 by Olaf van der Spek
Merge trunk
1579
      new_field= new field::Int32(item->max_length, maybe_null, item->name, item->unsigned_flag);
2008.2.3 by Brian Aker
Fixing up a, somewhat, hidden unsigned type to solve a few issues around
1580
    }
1581
575.4.7 by Monty Taylor
More header cleanup.
1582
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
1583
575.4.7 by Monty Taylor
More header cleanup.
1584
  case STRING_RESULT:
1585
    assert(item->collation.collation);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1586
575.4.7 by Monty Taylor
More header cleanup.
1587
    /*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1588
      DATE/TIME fields have STRING_RESULT result type.
575.4.7 by Monty Taylor
More header cleanup.
1589
      To preserve type they needed to be handled separately.
1590
    */
2167.3.2 by Brian Aker
Make use of isDateTime
1591
    if (field::isDateTime(item->field_type()))
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1592
    {
575.4.7 by Monty Taylor
More header cleanup.
1593
      new_field= item->tmp_table_field_from_field_type(table, 1);
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1594
      /*
1595
        Make sure that the blob fits into a Field_varstring which has
1596
        2-byte lenght.
1597
      */
1598
    }
575.4.7 by Monty Taylor
More header cleanup.
1599
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1600
             convert_blob_length <= Field_varstring::MAX_SIZE &&
575.4.7 by Monty Taylor
More header cleanup.
1601
             convert_blob_length)
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1602
    {
1603
      table->setVariableWidth();
575.4.7 by Monty Taylor
More header cleanup.
1604
      new_field= new Field_varstring(convert_blob_length, maybe_null,
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1605
                                     item->name, item->collation.collation);
1606
    }
575.4.7 by Monty Taylor
More header cleanup.
1607
    else
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1608
    {
575.4.7 by Monty Taylor
More header cleanup.
1609
      new_field= item->make_string_field(table);
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1610
    }
575.4.7 by Monty Taylor
More header cleanup.
1611
    new_field->set_derivation(item->collation.derivation);
1612
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
1613
575.4.7 by Monty Taylor
More header cleanup.
1614
  case DECIMAL_RESULT:
1615
    {
2008 by Brian Aker
Formatting + remove default from switch/case.
1616
      uint8_t dec= item->decimals;
1617
      uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
1618
      uint32_t len= item->max_length;
575.4.7 by Monty Taylor
More header cleanup.
1619
1620
      /*
2008 by Brian Aker
Formatting + remove default from switch/case.
1621
        Trying to put too many digits overall in a DECIMAL(prec,dec)
1622
        will always throw a warning. We must limit dec to
1623
        DECIMAL_MAX_SCALE however to prevent an assert() later.
575.4.7 by Monty Taylor
More header cleanup.
1624
      */
1625
2008 by Brian Aker
Formatting + remove default from switch/case.
1626
      if (dec > 0)
1627
      {
1628
        signed int overflow;
1629
1630
        dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
1631
1632
        /*
1633
          If the value still overflows the field with the corrected dec,
1634
          we'll throw out decimals rather than integers. This is still
1635
          bad and of course throws a truncation warning.
1636
          +1: for decimal point
1637
        */
1638
2030.1.2 by Brian Aker
First pass in refactoring of the name of my_decimal.
1639
        overflow= class_decimal_precision_to_length(intg + dec, dec,
2008 by Brian Aker
Formatting + remove default from switch/case.
1640
                                                 item->unsigned_flag) - len;
1641
1642
        if (overflow > 0)
1643
          dec= max(0, dec - overflow);            // too long, discard fract
1644
        else
1645
          len-= item->decimals - dec;             // corrected value fits
1646
      }
1647
1648
      new_field= new Field_decimal(len,
1649
                                   maybe_null,
1650
                                   item->name,
1651
                                   dec,
1652
                                   item->unsigned_flag);
1653
      break;
575.4.7 by Monty Taylor
More header cleanup.
1654
    }
1655
1656
  case ROW_RESULT:
1657
    // This case should never be choosen
1658
    assert(0);
1996.2.1 by Brian Aker
uuid type code.
1659
    abort();
575.4.7 by Monty Taylor
More header cleanup.
1660
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
1661
575.4.7 by Monty Taylor
More header cleanup.
1662
  if (new_field)
1663
    new_field->init(table);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1664
575.4.7 by Monty Taylor
More header cleanup.
1665
  if (copy_func && item->is_result_field())
1666
    *((*copy_func)++) = item;			// Save for copy_funcs
2008 by Brian Aker
Formatting + remove default from switch/case.
1667
575.4.7 by Monty Taylor
More header cleanup.
1668
  if (modify_item)
1669
    item->set_result_field(new_field);
2008 by Brian Aker
Formatting + remove default from switch/case.
1670
575.4.7 by Monty Taylor
More header cleanup.
1671
  if (item->type() == Item::NULL_ITEM)
1672
    new_field->is_created_from_null_item= true;
2008 by Brian Aker
Formatting + remove default from switch/case.
1673
575.4.7 by Monty Taylor
More header cleanup.
1674
  return new_field;
1675
}
1676
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
1677
Field *create_tmp_field(Session *session,
1678
                        Table *table,
1679
                        Item *item,
1273.2.3 by Stewart Smith
cleanup trailing whitespace
1680
                        Item::Type type,
1681
                        Item ***copy_func,
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
1682
                        Field **from_field,
1273.2.3 by Stewart Smith
cleanup trailing whitespace
1683
                        Field **default_field,
1684
                        bool group,
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
1685
                        bool modify_item,
1686
                        bool make_copy_field,
575.4.7 by Monty Taylor
More header cleanup.
1687
                        uint32_t convert_blob_length)
1688
{
1689
  Field *result;
1690
  Item::Type orig_type= type;
1691
  Item *orig_item= 0;
1692
1693
  if (type != Item::FIELD_ITEM &&
1694
      item->real_item()->type() == Item::FIELD_ITEM)
1695
  {
1696
    orig_item= item;
1697
    item= item->real_item();
1698
    type= Item::FIELD_ITEM;
1699
  }
1700
1701
  switch (type) {
1702
  case Item::SUM_FUNC_ITEM:
1703
  {
1704
    Item_sum *item_sum=(Item_sum*) item;
1705
    result= item_sum->create_tmp_field(group, table, convert_blob_length);
1706
    if (!result)
1707
      my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
1708
    return result;
1709
  }
1710
  case Item::FIELD_ITEM:
1711
  case Item::DEFAULT_VALUE_ITEM:
1712
  {
1713
    Item_field *field= (Item_field*) item;
1714
    bool orig_modify= modify_item;
1715
    if (orig_type == Item::REF_ITEM)
1716
      modify_item= 0;
1717
    /*
1718
      If item have to be able to store NULLs but underlaid field can't do it,
1719
      create_tmp_field_from_field() can't be used for tmp field creation.
1720
    */
1721
    if (field->maybe_null && !field->field->maybe_null())
1722
    {
1723
      result= create_tmp_field_from_item(session, item, table, NULL,
1724
                                         modify_item, convert_blob_length);
1725
      *from_field= field->field;
1726
      if (result && modify_item)
1727
        field->result_field= result;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1728
    }
575.4.7 by Monty Taylor
More header cleanup.
1729
    else
1996.2.1 by Brian Aker
uuid type code.
1730
    {
575.4.7 by Monty Taylor
More header cleanup.
1731
      result= create_tmp_field_from_field(session, (*from_field= field->field),
1732
                                          orig_item ? orig_item->name :
1733
                                          item->name,
1734
                                          table,
1735
                                          modify_item ? field :
1736
                                          NULL,
1737
                                          convert_blob_length);
1996.2.1 by Brian Aker
uuid type code.
1738
    }
575.4.7 by Monty Taylor
More header cleanup.
1739
    if (orig_type == Item::REF_ITEM && orig_modify)
1740
      ((Item_ref*)orig_item)->set_result_field(result);
1741
    if (field->field->eq_def(result))
1742
      *default_field= field->field;
1743
    return result;
1744
  }
1745
  /* Fall through */
1746
  case Item::FUNC_ITEM:
1747
    /* Fall through */
1748
  case Item::COND_ITEM:
1749
  case Item::FIELD_AVG_ITEM:
1750
  case Item::FIELD_STD_ITEM:
1751
  case Item::SUBSELECT_ITEM:
1752
    /* The following can only happen with 'CREATE TABLE ... SELECT' */
1753
  case Item::PROC_ITEM:
1754
  case Item::INT_ITEM:
1755
  case Item::REAL_ITEM:
1756
  case Item::DECIMAL_ITEM:
1757
  case Item::STRING_ITEM:
1758
  case Item::REF_ITEM:
1759
  case Item::NULL_ITEM:
1760
  case Item::VARBIN_ITEM:
1761
    if (make_copy_field)
1762
    {
1763
      assert(((Item_result_field*)item)->result_field);
1764
      *from_field= ((Item_result_field*)item)->result_field;
1765
    }
1766
    return create_tmp_field_from_item(session, item, table,
1767
                                      (make_copy_field ? 0 : copy_func),
1768
                                       modify_item, convert_blob_length);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1769
  case Item::TYPE_HOLDER:
575.4.7 by Monty Taylor
More header cleanup.
1770
    result= ((Item_type_holder *)item)->make_field_by_type(table);
1771
    result->set_derivation(item->collation.derivation);
1772
    return result;
1773
  default:					// Dosen't have to be stored
681 by Brian Aker
Style cleanup
1774
    return NULL;
575.4.7 by Monty Taylor
More header cleanup.
1775
  }
1776
}
1777
1992.5.1 by Brian Aker
Additional cerr output bits for a few classes (Item, Field,...)
1778
std::ostream& operator<<(std::ostream& output, const Item &item)
1779
{
2015.1.3 by Brian Aker
Merge in CAST operators for SIGNED/UNSIGNED
1780
  switch (item.type())
1781
  {
1782
  case drizzled::Item::SUBSELECT_ITEM :
1783
  case drizzled::Item::FIELD_ITEM :
1784
  case drizzled::Item::SUM_FUNC_ITEM :
1785
  case drizzled::Item::STRING_ITEM :
1786
  case drizzled::Item::INT_ITEM :
1787
  case drizzled::Item::REAL_ITEM :
1788
  case drizzled::Item::NULL_ITEM :
1789
  case drizzled::Item::VARBIN_ITEM :
1790
  case drizzled::Item::COPY_STR_ITEM :
1791
  case drizzled::Item::FIELD_AVG_ITEM :
1792
  case drizzled::Item::DEFAULT_VALUE_ITEM :
1793
  case drizzled::Item::PROC_ITEM :
1794
  case drizzled::Item::COND_ITEM :
1795
  case drizzled::Item::REF_ITEM :
1796
  case drizzled::Item::FIELD_STD_ITEM :
1797
  case drizzled::Item::FIELD_VARIANCE_ITEM :
1798
  case drizzled::Item::INSERT_VALUE_ITEM :
1799
  case drizzled::Item::ROW_ITEM:
1800
  case drizzled::Item::CACHE_ITEM :
1801
  case drizzled::Item::TYPE_HOLDER :
1802
  case drizzled::Item::PARAM_ITEM :
1803
  case drizzled::Item::DECIMAL_ITEM :
1804
  case drizzled::Item::FUNC_ITEM :
2023.2.8 by Brian Aker
Manage merge
1805
  case drizzled::Item::BOOLEAN_ITEM :
2015.1.3 by Brian Aker
Merge in CAST operators for SIGNED/UNSIGNED
1806
    {
1807
      output << "Item:(";
1808
      output <<  item.full_name();
1809
      output << ", ";
1810
      output << drizzled::display::type(item.type());
1811
      output << ")";
1812
    }
1813
    break;
1814
  }
1992.5.1 by Brian Aker
Additional cerr output bits for a few classes (Item, Field,...)
1815
1816
  return output;  // for multiple << operators.
1817
}
1818
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
1819
} /* namespace drizzled */