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