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