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