~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
1660.1.3 by Brian Aker
Encapsulate Table in field
833
      prev_subselect_item->used_tables_cache|= found_field->getTable()->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
*/
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
854
static Item** find_field_in_group_list(Item *find_item, Order *group_list)
1 by brian
clean slate
855
{
856
  const char *db_name;
857
  const char *table_name;
858
  const char *field_name;
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
859
  Order *found_group= NULL;
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
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
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
885
  for (Order *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;
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
947
  Order *group_list= (Order*) 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
  */
1578.6.3 by Brian Aker
More current_session removal.
956
  if (!(select_ref= find_item_in_list(session,
957
                                      ref, *(select->get_item_list()),
1 by brian
clean slate
958
                                      &counter, REPORT_EXCEPT_NOT_FOUND,
959
                                      &resolution)))
960
    return NULL; /* Some error occurred. */
961
  if (resolution == RESOLVED_AGAINST_ALIAS)
56 by brian
Next pass of true/false update.
962
    ref->alias_name_used= true;
1 by brian
clean slate
963
964
  /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
965
  if (select->having_fix_field && !ref->with_sum_func && group_list)
966
  {
967
    group_by_ref= find_field_in_group_list(ref, group_list);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
968
1 by brian
clean slate
969
    /* Check if the fields found in SELECT and GROUP BY are the same field. */
970
    if (group_by_ref && (select_ref != not_found_item) &&
971
        !((*group_by_ref)->eq(*select_ref, 0)))
972
    {
56 by brian
Next pass of true/false update.
973
      ambiguous_fields= true;
520.1.22 by Brian Aker
Second pass of thd cleanup
974
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
1 by brian
clean slate
975
                          ER(ER_NON_UNIQ_ERROR), ref->full_name(),
520.1.22 by Brian Aker
Second pass of thd cleanup
976
                          current_session->where);
1 by brian
clean slate
977
978
    }
979
  }
980
981
  if (select_ref != not_found_item || group_by_ref)
982
  {
983
    if (select_ref != not_found_item && !ambiguous_fields)
984
    {
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
985
      assert(*select_ref != 0);
1 by brian
clean slate
986
      if (!select->ref_pointer_array[counter])
987
      {
988
        my_error(ER_ILLEGAL_REFERENCE, MYF(0),
989
                 ref->name, "forward reference in item list");
990
        return NULL;
991
      }
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
992
      assert((*select_ref)->fixed);
1 by brian
clean slate
993
      return (select->ref_pointer_array + counter);
994
    }
995
    if (group_by_ref)
996
      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
997
    assert(false);
1 by brian
clean slate
998
    return NULL; /* So there is no compiler warning. */
999
  }
1000
1001
  return (Item**) not_found_item;
1002
}
1003
1052.2.4 by Nathan Williams
No actual code changes. Changed Send_field to SendField to be consistent with coding standards.
1004
void Item::init_make_field(SendField *tmp_field,
1 by brian
clean slate
1005
			   enum enum_field_types field_type_arg)
1006
{
1007
  char *empty_name= (char*) "";
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
1008
  tmp_field->db_name=	empty_name;
1009
  tmp_field->org_table_name= empty_name;
1010
  tmp_field->org_col_name= empty_name;
1011
  tmp_field->table_name= empty_name;
1012
  tmp_field->col_name= name;
1013
  tmp_field->charsetnr= collation.collation->number;
1014
  tmp_field->flags= (maybe_null ? 0 : NOT_NULL_FLAG) |
1015
                    (my_binary_compare(collation.collation) ?
1016
                      BINARY_FLAG : 0);
1017
  tmp_field->type= field_type_arg;
1018
  tmp_field->length= max_length;
1019
  tmp_field->decimals= decimals;
1 by brian
clean slate
1020
}
1021
1052.2.4 by Nathan Williams
No actual code changes. Changed Send_field to SendField to be consistent with coding standards.
1022
void Item::make_field(SendField *tmp_field)
1 by brian
clean slate
1023
{
1024
  init_make_field(tmp_field, field_type());
1025
}
1026
1027
enum_field_types Item::string_field_type() const
1028
{
240 by Brian Aker
Removed old/dead VARCHAR type from pre-5.0
1029
  enum_field_types f_type= DRIZZLE_TYPE_VARCHAR;
113 by Brian Aker
First pass on removing blob internals.
1030
  if (max_length >= 65536)
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1031
    f_type= DRIZZLE_TYPE_BLOB;
1 by brian
clean slate
1032
  return f_type;
1033
}
1034
1035
enum_field_types Item::field_type() const
1036
{
1037
  switch (result_type()) {
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
1038
  case STRING_RESULT:  
1039
    return string_field_type();
1040
  case INT_RESULT:     
1041
    return DRIZZLE_TYPE_LONGLONG;
1042
  case DECIMAL_RESULT: 
1211.1.1 by Brian Aker
Updating with my change to to DECIMAL from NEWDECIMAL and Stewart's update
1043
    return DRIZZLE_TYPE_DECIMAL;
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
1044
  case REAL_RESULT:    
1045
    return DRIZZLE_TYPE_DOUBLE;
1 by brian
clean slate
1046
  case ROW_RESULT:
1047
  default:
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
1048
    assert(0);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1049
    return DRIZZLE_TYPE_VARCHAR;
1 by brian
clean slate
1050
  }
1051
}
1052
1053
bool Item::is_datetime()
1054
{
1055
  switch (field_type())
1056
  {
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
1057
    case DRIZZLE_TYPE_DATE:
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1058
    case DRIZZLE_TYPE_DATETIME:
1059
    case DRIZZLE_TYPE_TIMESTAMP:
56 by brian
Next pass of true/false update.
1060
      return true;
1 by brian
clean slate
1061
    default:
1062
      break;
1063
  }
56 by brian
Next pass of true/false update.
1064
  return false;
1 by brian
clean slate
1065
}
1066
1067
String *Item::check_well_formed_result(String *str, bool send_error)
1068
{
1069
  /* 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.
1070
  const CHARSET_INFO * const cs= str->charset();
1 by brian
clean slate
1071
  int well_formed_error;
482 by Brian Aker
Remove uint.
1072
  uint32_t wlen= cs->cset->well_formed_len(cs,
1 by brian
clean slate
1073
                                       str->ptr(), str->ptr() + str->length(),
1074
                                       str->length(), &well_formed_error);
1075
  if (wlen < str->length())
1076
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
1077
    Session *session= current_session;
1 by brian
clean slate
1078
    char hexbuf[7];
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
1079
    enum DRIZZLE_ERROR::enum_warning_level level;
482 by Brian Aker
Remove uint.
1080
    uint32_t diff= str->length() - wlen;
937.2.6 by Stewart Smith
make set_if_bigger typesafe for C and C++. Fix up everywhere.
1081
    set_if_smaller(diff, 3U);
971.3.9 by Eric Day
Removed dependency for hex convert, fixed a few Protocol class issues for Solaris.
1082
    (void) drizzled_string_to_hex(hexbuf, str->ptr() + wlen, diff);
1 by brian
clean slate
1083
    if (send_error)
1084
    {
1085
      my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
1086
               cs->csname,  hexbuf);
681 by Brian Aker
Style cleanup
1087
      return NULL;
1 by brian
clean slate
1088
    }
1089
    {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
1090
      level= DRIZZLE_ERROR::WARN_LEVEL_ERROR;
1 by brian
clean slate
1091
      null_value= 1;
1092
      str= 0;
1093
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
1094
    push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
1 by brian
clean slate
1095
                        ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
1096
  }
1097
  return str;
1098
}
1099
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1100
bool Item::eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO * const cs)
1 by brian
clean slate
1101
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1102
  const CHARSET_INFO *save_cs= 0;
1103
  const CHARSET_INFO *save_item_cs= 0;
1 by brian
clean slate
1104
  if (collation.collation != cs)
1105
  {
1106
    save_cs= collation.collation;
1107
    collation.collation= cs;
1108
  }
1109
  if (item->collation.collation != cs)
1110
  {
1111
    save_item_cs= item->collation.collation;
1112
    item->collation.collation= cs;
1113
  }
1114
  bool res= eq(item, binary_cmp);
1115
  if (save_cs)
1116
    collation.collation= save_cs;
1117
  if (save_item_cs)
1118
    item->collation.collation= save_item_cs;
1119
  return res;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1120
}
1 by brian
clean slate
1121
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1122
Field *Item::make_string_field(Table *table)
1 by brian
clean slate
1123
{
1124
  Field *field;
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
1125
  assert(collation.collation);
1 by brian
clean slate
1126
  if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB)
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1127
  {
1 by brian
clean slate
1128
    field= new Field_blob(max_length, maybe_null, name,
1129
                          collation.collation);
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1130
  }
241 by Brian Aker
First pass of CHAR removal.
1131
  else
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1132
  {
1133
    table->setVariableWidth();
1134
    field= new Field_varstring(max_length, maybe_null, name,
1 by brian
clean slate
1135
                               collation.collation);
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1136
  }
241 by Brian Aker
First pass of CHAR removal.
1137
1 by brian
clean slate
1138
  if (field)
1139
    field->init(table);
1140
  return field;
1141
}
1142
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1143
Field *Item::tmp_table_field_from_field_type(Table *table, bool)
1 by brian
clean slate
1144
{
1145
  /*
1146
    The field functions defines a field to be not null if null_ptr is not 0
1147
  */
481 by Brian Aker
Remove all of uchar.
1148
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
1 by brian
clean slate
1149
  Field *field;
1150
1151
  switch (field_type()) {
1211.1.1 by Brian Aker
Updating with my change to to DECIMAL from NEWDECIMAL and Stewart's update
1152
  case DRIZZLE_TYPE_DECIMAL:
1153
    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.
1154
                                 max_length,
1155
                                 null_ptr,
1156
                                 0,
1157
                                 Field::NONE,
1158
                                 name,
1159
                                 decimals,
1160
                                 0,
1 by brian
clean slate
1161
                                 unsigned_flag);
1162
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1163
  case DRIZZLE_TYPE_LONG:
481 by Brian Aker
Remove all of uchar.
1164
    field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1 by brian
clean slate
1165
			  name, 0, unsigned_flag);
1166
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1167
  case DRIZZLE_TYPE_LONGLONG:
481 by Brian Aker
Remove all of uchar.
1168
    field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1 by brian
clean slate
1169
			      name, 0, unsigned_flag);
1170
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1171
  case DRIZZLE_TYPE_DOUBLE:
481 by Brian Aker
Remove all of uchar.
1172
    field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1 by brian
clean slate
1173
			    name, decimals, 0, unsigned_flag);
1174
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1175
  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.
1176
    field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
1 by brian
clean slate
1177
    break;
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
1178
  case DRIZZLE_TYPE_DATE:
1179
    field= new Field_date(maybe_null, name, &my_charset_bin);
1 by brian
clean slate
1180
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1181
  case DRIZZLE_TYPE_TIMESTAMP:
1 by brian
clean slate
1182
    field= new Field_timestamp(maybe_null, name, &my_charset_bin);
1183
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1184
  case DRIZZLE_TYPE_DATETIME:
1 by brian
clean slate
1185
    field= new Field_datetime(maybe_null, name, &my_charset_bin);
1186
    break;
1187
  default:
1188
    /* 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
1189
    assert(0);
1 by brian
clean slate
1190
    /* Fall through to make_string_field() */
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1191
  case DRIZZLE_TYPE_ENUM:
1192
  case DRIZZLE_TYPE_VARCHAR:
1 by brian
clean slate
1193
    return make_string_field(table);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1194
  case DRIZZLE_TYPE_BLOB:
1 by brian
clean slate
1195
    if (this->type() == Item::TYPE_HOLDER)
1196
      field= new Field_blob(max_length, maybe_null, name, collation.collation,
1197
                            1);
1198
    else
1199
      field= new Field_blob(max_length, maybe_null, name, collation.collation);
1200
    break;					// Blob handled outside of case
1201
  }
1202
  if (field)
1203
    field->init(table);
1204
  return field;
1205
}
1206
1207
/*
1208
  This implementation can lose str_value content, so if the
1209
  Item uses str_value to store something, it should
1210
  reimplement it's ::save_in_field() as Item_string, for example, does
1211
*/
1212
int Item::save_in_field(Field *field, bool no_conversions)
1213
{
1214
  int error;
1215
  if (result_type() == STRING_RESULT)
1216
  {
1217
    String *result;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1218
    const CHARSET_INFO * const cs= collation.collation;
1 by brian
clean slate
1219
    char buff[MAX_FIELD_WIDTH];		// Alloc buffer for small columns
1220
    str_value.set_quick(buff, sizeof(buff), cs);
1221
    result=val_str(&str_value);
1222
    if (null_value)
1223
    {
1224
      str_value.set_quick(0, 0, cs);
1225
      return set_field_to_null_with_conversions(field, no_conversions);
1226
    }
1227
56 by brian
Next pass of true/false update.
1228
    /* NOTE: If null_value == false, "result" must be not NULL.  */
1 by brian
clean slate
1229
1230
    field->set_notnull();
1231
    error=field->store(result->ptr(),result->length(),cs);
1232
    str_value.set_quick(0, 0, cs);
1233
  }
1234
  else if (result_type() == REAL_RESULT &&
1235
           field->result_type() == STRING_RESULT)
1236
  {
1237
    double nr= val_real();
1238
    if (null_value)
1239
      return set_field_to_null_with_conversions(field, no_conversions);
1240
    field->set_notnull();
1241
    error= field->store(nr);
1242
  }
1243
  else if (result_type() == REAL_RESULT)
1244
  {
1245
    double nr= val_real();
1246
    if (null_value)
1247
      return set_field_to_null(field);
1248
    field->set_notnull();
1249
    error=field->store(nr);
1250
  }
1251
  else if (result_type() == DECIMAL_RESULT)
1252
  {
1253
    my_decimal decimal_value;
1254
    my_decimal *value= val_decimal(&decimal_value);
1255
    if (null_value)
1256
      return set_field_to_null_with_conversions(field, no_conversions);
1257
    field->set_notnull();
1258
    error=field->store_decimal(value);
1259
  }
1260
  else
1261
  {
152 by Brian Aker
longlong replacement
1262
    int64_t nr=val_int();
1 by brian
clean slate
1263
    if (null_value)
1264
      return set_field_to_null_with_conversions(field, no_conversions);
1265
    field->set_notnull();
1266
    error=field->store(nr, unsigned_flag);
1267
  }
1268
  return error;
1269
}
1270
1240.8.4 by Dennis Schoen
add Item functions
1271
/**
1272
  Check if an item is a constant one and can be cached.
1273
1274
  @param arg [out] TRUE <=> Cache this item.
1275
1276
  @return TRUE  Go deeper in item tree.
1277
  @return FALSE Don't go deeper in item tree.
1278
*/
1279
1280
bool Item::cache_const_expr_analyzer(unsigned char **arg)
1281
{
1282
  bool *cache_flag= (bool*)*arg;
1283
  if (!*cache_flag)
1284
  {
1285
    Item *item= real_item();
1286
    /*
1287
      Cache constant items unless it's a basic constant, constant field or
1288
      a subselect (they use their own cache).
1289
    */
1290
    if (const_item() &&
1291
        !(item->basic_const_item() || item->type() == Item::FIELD_ITEM ||
1292
          item->type() == SUBSELECT_ITEM ||
1293
           /*
1294
             Do not cache GET_USER_VAR() function as its const_item() may
1295
             return TRUE for the current thread but it still may change
1296
             during the execution.
1297
           */
1298
          (item->type() == Item::FUNC_ITEM &&
1299
           ((Item_func*)item)->functype() == Item_func::GUSERVAR_FUNC)))
1300
      *cache_flag= true;
1301
    return true;
1302
  }
1303
  return false;
1304
}
1305
1306
/**
1307
  Cache item if needed.
1308
1309
  @param arg   TRUE <=> Cache this item.
1310
1311
  @return cache if cache needed.
1312
  @return this otherwise.
1313
*/
1314
1315
Item* Item::cache_const_expr_transformer(unsigned char *arg)
1316
{
1317
  if (*(bool*)arg)
1318
  {
1319
    *((bool*)arg)= false;
1320
    Item_cache *cache= Item_cache::get_cache(this);
1321
    if (!cache)
1322
      return NULL;
1323
    cache->setup(this);
1324
    cache->store(this);
1325
    return cache;
1326
  }
1327
  return this;
1328
}
1329
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
1330
bool Item::send(plugin::Client *client, String *buffer)
1 by brian
clean slate
1331
{
1332
  bool result= false;
1333
  enum_field_types f_type;
1334
1335
  switch ((f_type=field_type())) {
1336
  default:
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1337
  case DRIZZLE_TYPE_NULL:
1338
  case DRIZZLE_TYPE_ENUM:
1339
  case DRIZZLE_TYPE_BLOB:
1340
  case DRIZZLE_TYPE_VARCHAR:
1211.1.1 by Brian Aker
Updating with my change to to DECIMAL from NEWDECIMAL and Stewart's update
1341
  case DRIZZLE_TYPE_DECIMAL:
1 by brian
clean slate
1342
  {
1343
    String *res;
1344
    if ((res=val_str(buffer)))
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
1345
      result= client->store(res->ptr(),res->length());
1 by brian
clean slate
1346
    break;
1347
  }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1348
  case DRIZZLE_TYPE_LONG:
1 by brian
clean slate
1349
  {
152 by Brian Aker
longlong replacement
1350
    int64_t nr;
1 by brian
clean slate
1351
    nr= val_int();
1352
    if (!null_value)
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
1353
      result= client->store((int32_t)nr);
1 by brian
clean slate
1354
    break;
1355
  }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1356
  case DRIZZLE_TYPE_LONGLONG:
1 by brian
clean slate
1357
  {
152 by Brian Aker
longlong replacement
1358
    int64_t nr;
1 by brian
clean slate
1359
    nr= val_int();
1360
    if (!null_value)
971.3.17 by Eric Day
Cleaned up int/date related store functions.
1361
    {
1362
      if (unsigned_flag)
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
1363
        result= client->store((uint64_t)nr);
971.3.17 by Eric Day
Cleaned up int/date related store functions.
1364
      else
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
1365
        result= client->store((int64_t)nr);
971.3.17 by Eric Day
Cleaned up int/date related store functions.
1366
    }
1 by brian
clean slate
1367
    break;
1368
  }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1369
  case DRIZZLE_TYPE_DOUBLE:
1 by brian
clean slate
1370
  {
1371
    double nr= val_real();
1372
    if (!null_value)
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
1373
      result= client->store(nr, decimals, buffer);
1 by brian
clean slate
1374
    break;
1375
  }
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1376
  case DRIZZLE_TYPE_DATETIME:
1377
  case DRIZZLE_TYPE_TIMESTAMP:
1 by brian
clean slate
1378
  {
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
1379
    DRIZZLE_TIME tm;
1 by brian
clean slate
1380
    get_date(&tm, TIME_FUZZY_DATE);
1381
    if (!null_value)
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
1382
      result= client->store(&tm);
1 by brian
clean slate
1383
    break;
1384
  }
1385
  }
1386
  if (null_value)
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
1387
    result= client->store();
1 by brian
clean slate
1388
  return result;
1389
}
1390
1391
Item_result item_cmp_type(Item_result a,Item_result b)
1392
{
1393
  if (a == STRING_RESULT && b == STRING_RESULT)
1394
    return STRING_RESULT;
1395
  if (a == INT_RESULT && b == INT_RESULT)
1396
    return INT_RESULT;
1397
  else if (a == ROW_RESULT || b == ROW_RESULT)
1398
    return ROW_RESULT;
1399
  if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
1400
      (b == INT_RESULT || b == DECIMAL_RESULT))
1401
    return DECIMAL_RESULT;
1402
  return REAL_RESULT;
1403
}
1404
520.1.22 by Brian Aker
Second pass of thd cleanup
1405
void resolve_const_item(Session *session, Item **ref, Item *comp_item)
1 by brian
clean slate
1406
{
1407
  Item *item= *ref;
1408
  Item *new_item= NULL;
1409
  if (item->basic_const_item())
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
1410
    return; /* Can't be better */
1 by brian
clean slate
1411
  Item_result res_type=item_cmp_type(comp_item->result_type(),
1412
				     item->result_type());
1253.1.6 by Monty Taylor
Moved mem_root functions into drizzled::memory:: namespace.
1413
  char *name=item->name; /* Alloced by memory::sql_alloc */
1 by brian
clean slate
1414
1415
  switch (res_type) {
1416
  case STRING_RESULT:
1417
  {
1418
    char buff[MAX_FIELD_WIDTH];
1419
    String tmp(buff,sizeof(buff),&my_charset_bin),*result;
1420
    result=item->val_str(&tmp);
1421
    if (item->null_value)
1422
      new_item= new Item_null(name);
1423
    else
1424
    {
482 by Brian Aker
Remove uint.
1425
      uint32_t length= result->length();
1253.1.6 by Monty Taylor
Moved mem_root functions into drizzled::memory:: namespace.
1426
      char *tmp_str= memory::sql_strmake(result->ptr(), length);
1 by brian
clean slate
1427
      new_item= new Item_string(name, tmp_str, length, result->charset());
1428
    }
1429
    break;
1430
  }
1431
  case INT_RESULT:
1432
  {
152 by Brian Aker
longlong replacement
1433
    int64_t result=item->val_int();
482 by Brian Aker
Remove uint.
1434
    uint32_t length=item->max_length;
1 by brian
clean slate
1435
    bool null_value=item->null_value;
1436
    new_item= (null_value ? (Item*) new Item_null(name) :
1437
               (Item*) new Item_int(name, result, length));
1438
    break;
1439
  }
1440
  case ROW_RESULT:
1441
  if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
1442
  {
1443
    /*
1444
      Substitute constants only in Item_rows. Don't affect other Items
1445
      with ROW_RESULT (eg Item_singlerow_subselect).
1446
1447
      For such Items more optimal is to detect if it is constant and replace
1448
      it with Item_row. This would optimize queries like this:
1449
      SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
1450
    */
1451
    Item_row *item_row= (Item_row*) item;
1452
    Item_row *comp_item_row= (Item_row*) comp_item;
482 by Brian Aker
Remove uint.
1453
    uint32_t col;
1 by brian
clean slate
1454
    new_item= 0;
1455
    /*
1456
      If item and comp_item are both Item_rows and have same number of cols
1457
      then process items in Item_row one by one.
1458
      We can't ignore NULL values here as this item may be used with <=>, in
1459
      which case NULL's are significant.
1460
    */
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
1461
    assert(item->result_type() == comp_item->result_type());
1462
    assert(item_row->cols() == comp_item_row->cols());
1 by brian
clean slate
1463
    col= item_row->cols();
1464
    while (col-- > 0)
520.1.22 by Brian Aker
Second pass of thd cleanup
1465
      resolve_const_item(session, item_row->addr(col),
1 by brian
clean slate
1466
                         comp_item_row->element_index(col));
1467
    break;
1468
  }
1469
  /* Fallthrough */
1470
  case REAL_RESULT:
1471
  {						// It must REAL_RESULT
1472
    double result= item->val_real();
482 by Brian Aker
Remove uint.
1473
    uint32_t length=item->max_length,decimals=item->decimals;
1 by brian
clean slate
1474
    bool null_value=item->null_value;
1475
    new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
1476
               new Item_float(name, result, decimals, length));
1477
    break;
1478
  }
1479
  case DECIMAL_RESULT:
1480
  {
1481
    my_decimal decimal_value;
1482
    my_decimal *result= item->val_decimal(&decimal_value);
482 by Brian Aker
Remove uint.
1483
    uint32_t length= item->max_length, decimals= item->decimals;
1 by brian
clean slate
1484
    bool null_value= item->null_value;
1485
    new_item= (null_value ?
1486
               (Item*) new Item_null(name) :
1487
               (Item*) new Item_decimal(name, result, length, decimals));
1488
    break;
1489
  }
1490
  default:
51.1.55 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE - 1st clean pass on item.h/cc
1491
    assert(0);
1 by brian
clean slate
1492
  }
1493
  if (new_item)
520.1.22 by Brian Aker
Second pass of thd cleanup
1494
    session->change_item_tree(ref, new_item);
1 by brian
clean slate
1495
}
1496
1497
bool field_is_equal_to_item(Field *field,Item *item)
1498
{
1499
1500
  Item_result res_type=item_cmp_type(field->result_type(),
1501
				     item->result_type());
1502
  if (res_type == STRING_RESULT)
1503
  {
1504
    char item_buff[MAX_FIELD_WIDTH];
1505
    char field_buff[MAX_FIELD_WIDTH];
1506
    String item_tmp(item_buff,sizeof(item_buff),&my_charset_bin),*item_result;
1507
    String field_tmp(field_buff,sizeof(field_buff),&my_charset_bin);
1508
    item_result=item->val_str(&item_tmp);
1509
    if (item->null_value)
1510
      return 1;					// This must be true
1511
    field->val_str(&field_tmp);
1512
    return !stringcmp(&field_tmp,item_result);
1513
  }
1514
  if (res_type == INT_RESULT)
1515
    return 1;					// Both where of type int
1516
  if (res_type == DECIMAL_RESULT)
1517
  {
1518
    my_decimal item_buf, *item_val,
1519
               field_buf, *field_val;
1520
    item_val= item->val_decimal(&item_buf);
1521
    if (item->null_value)
1522
      return 1;					// This must be true
1523
    field_val= field->val_decimal(&field_buf);
1524
    return !my_decimal_cmp(item_val, field_val);
1525
  }
1526
  double result= item->val_real();
1527
  if (item->null_value)
1528
    return 1;
1529
  return result == field->val_real();
1530
}
1531
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1532
void dummy_error_processor(Session *, void *)
1 by brian
clean slate
1533
{}
1534
1535
/**
575.4.7 by Monty Taylor
More header cleanup.
1536
  Create field for temporary table using type of given item.
1537
1538
  @param session                   Thread handler
1539
  @param item                  Item to create a field for
1540
  @param table                 Temporary table
1541
  @param copy_func             If set and item is a function, store copy of
1542
                               item in this array
1543
  @param modify_item           1 if item->result_field should point to new
1544
                               item. This is relevent for how fill_record()
1545
                               is going to work:
1546
                               If modify_item is 1 then fill_record() will
1547
                               update the record in the original table.
1548
                               If modify_item is 0 then fill_record() will
1549
                               update the temporary table
1550
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
1551
                               field instead of blob.
1552
1553
  @retval
1554
    0  on error
1555
  @retval
1556
    new_created field
1557
*/
1558
static Field *create_tmp_field_from_item(Session *,
1559
                                         Item *item, Table *table,
1560
                                         Item ***copy_func, bool modify_item,
1561
                                         uint32_t convert_blob_length)
1562
{
1563
  bool maybe_null= item->maybe_null;
1564
  Field *new_field;
1565
1566
  switch (item->result_type()) {
1567
  case REAL_RESULT:
1568
    new_field= new Field_double(item->max_length, maybe_null,
1569
                                item->name, item->decimals, true);
1570
    break;
1571
  case INT_RESULT:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1572
    /*
575.4.7 by Monty Taylor
More header cleanup.
1573
      Select an integer type with the minimal fit precision.
1574
      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:
1575
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
1576
      Field_long : make them Field_int64_t.
575.4.7 by Monty Taylor
More header cleanup.
1577
    */
1578
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
1579
      new_field=new Field_int64_t(item->max_length, maybe_null,
1580
                                   item->name, item->unsigned_flag);
1581
    else
1582
      new_field=new Field_long(item->max_length, maybe_null,
1583
                               item->name, item->unsigned_flag);
1584
    break;
1585
  case STRING_RESULT:
1586
    assert(item->collation.collation);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1587
575.4.7 by Monty Taylor
More header cleanup.
1588
    enum enum_field_types type;
1589
    /*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1590
      DATE/TIME fields have STRING_RESULT result type.
575.4.7 by Monty Taylor
More header cleanup.
1591
      To preserve type they needed to be handled separately.
1592
    */
1593
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
896.5.1 by Jay Pipes
Removes the TIME column type and related time functions.
1594
        type == DRIZZLE_TYPE_DATE ||
575.4.7 by Monty Taylor
More header cleanup.
1595
        type == DRIZZLE_TYPE_TIMESTAMP)
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1596
    {
575.4.7 by Monty Taylor
More header cleanup.
1597
      new_field= item->tmp_table_field_from_field_type(table, 1);
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1598
      /*
1599
        Make sure that the blob fits into a Field_varstring which has
1600
        2-byte lenght.
1601
      */
1602
    }
575.4.7 by Monty Taylor
More header cleanup.
1603
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1604
             convert_blob_length <= Field_varstring::MAX_SIZE &&
575.4.7 by Monty Taylor
More header cleanup.
1605
             convert_blob_length)
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1606
    {
1607
      table->setVariableWidth();
575.4.7 by Monty Taylor
More header cleanup.
1608
      new_field= new Field_varstring(convert_blob_length, maybe_null,
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1609
                                     item->name, item->collation.collation);
1610
    }
575.4.7 by Monty Taylor
More header cleanup.
1611
    else
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1612
    {
575.4.7 by Monty Taylor
More header cleanup.
1613
      new_field= item->make_string_field(table);
1835.1.3 by Brian Aker
Fix variable such that we no longer pass share to varstring on creation.
1614
    }
575.4.7 by Monty Taylor
More header cleanup.
1615
    new_field->set_derivation(item->collation.derivation);
1616
    break;
1617
  case DECIMAL_RESULT:
1618
  {
1619
    uint8_t dec= item->decimals;
1620
    uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
1621
    uint32_t len= item->max_length;
1622
1623
    /*
1624
      Trying to put too many digits overall in a DECIMAL(prec,dec)
1625
      will always throw a warning. We must limit dec to
1626
      DECIMAL_MAX_SCALE however to prevent an assert() later.
1627
    */
1628
1629
    if (dec > 0)
1630
    {
1631
      signed int overflow;
1632
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).
1633
      dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
575.4.7 by Monty Taylor
More header cleanup.
1634
1635
      /*
1636
        If the value still overflows the field with the corrected dec,
1637
        we'll throw out decimals rather than integers. This is still
1638
        bad and of course throws a truncation warning.
1639
        +1: for decimal point
1640
      */
1641
1642
      overflow= my_decimal_precision_to_length(intg + dec, dec,
1643
                                               item->unsigned_flag) - len;
1644
1645
      if (overflow > 0)
1067.4.7 by Nathan Williams
The remaining files using cmax have been converted to std::max.
1646
        dec= max(0, dec - overflow);            // too long, discard fract
575.4.7 by Monty Taylor
More header cleanup.
1647
      else
1067.4.7 by Nathan Williams
The remaining files using cmax have been converted to std::max.
1648
        len-= item->decimals - dec;             // corrected value fits
575.4.7 by Monty Taylor
More header cleanup.
1649
    }
1650
1211.1.1 by Brian Aker
Updating with my change to to DECIMAL from NEWDECIMAL and Stewart's update
1651
    new_field= new Field_decimal(len,
1652
                                 maybe_null,
1653
                                 item->name,
1654
                                 dec,
1655
                                 item->unsigned_flag);
575.4.7 by Monty Taylor
More header cleanup.
1656
    break;
1657
  }
1658
  case ROW_RESULT:
1659
  default:
1660
    // This case should never be choosen
1661
    assert(0);
1662
    new_field= 0;
1663
    break;
1664
  }
1665
  if (new_field)
1666
    new_field->init(table);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1667
575.4.7 by Monty Taylor
More header cleanup.
1668
  if (copy_func && item->is_result_field())
1669
    *((*copy_func)++) = item;			// Save for copy_funcs
1670
  if (modify_item)
1671
    item->set_result_field(new_field);
1672
  if (item->type() == Item::NULL_ITEM)
1673
    new_field->is_created_from_null_item= true;
1674
  return new_field;
1675
}
1676
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
1677
Field *create_tmp_field(Session *session,
1678
                        Table *table,
1679
                        Item *item,
1273.2.3 by Stewart Smith
cleanup trailing whitespace
1680
                        Item::Type type,
1681
                        Item ***copy_func,
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
1682
                        Field **from_field,
1273.2.3 by Stewart Smith
cleanup trailing whitespace
1683
                        Field **default_field,
1684
                        bool group,
1055.2.12 by Jay Pipes
Documentation and style cleanup on Item class
1685
                        bool modify_item,
1686
                        bool make_copy_field,
575.4.7 by Monty Taylor
More header cleanup.
1687
                        uint32_t convert_blob_length)
1688
{
1689
  Field *result;
1690
  Item::Type orig_type= type;
1691
  Item *orig_item= 0;
1692
1693
  if (type != Item::FIELD_ITEM &&
1694
      item->real_item()->type() == Item::FIELD_ITEM)
1695
  {
1696
    orig_item= item;
1697
    item= item->real_item();
1698
    type= Item::FIELD_ITEM;
1699
  }
1700
1701
  switch (type) {
1702
  case Item::SUM_FUNC_ITEM:
1703
  {
1704
    Item_sum *item_sum=(Item_sum*) item;
1705
    result= item_sum->create_tmp_field(group, table, convert_blob_length);
1706
    if (!result)
1707
      my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
1708
    return result;
1709
  }
1710
  case Item::FIELD_ITEM:
1711
  case Item::DEFAULT_VALUE_ITEM:
1712
  {
1713
    Item_field *field= (Item_field*) item;
1714
    bool orig_modify= modify_item;
1715
    if (orig_type == Item::REF_ITEM)
1716
      modify_item= 0;
1717
    /*
1718
      If item have to be able to store NULLs but underlaid field can't do it,
1719
      create_tmp_field_from_field() can't be used for tmp field creation.
1720
    */
1721
    if (field->maybe_null && !field->field->maybe_null())
1722
    {
1723
      result= create_tmp_field_from_item(session, item, table, NULL,
1724
                                         modify_item, convert_blob_length);
1725
      *from_field= field->field;
1726
      if (result && modify_item)
1727
        field->result_field= result;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1728
    }
575.4.7 by Monty Taylor
More header cleanup.
1729
    else
1730
      result= create_tmp_field_from_field(session, (*from_field= field->field),
1731
                                          orig_item ? orig_item->name :
1732
                                          item->name,
1733
                                          table,
1734
                                          modify_item ? field :
1735
                                          NULL,
1736
                                          convert_blob_length);
1737
    if (orig_type == Item::REF_ITEM && orig_modify)
1738
      ((Item_ref*)orig_item)->set_result_field(result);
1739
    if (field->field->eq_def(result))
1740
      *default_field= field->field;
1741
    return result;
1742
  }
1743
  /* Fall through */
1744
  case Item::FUNC_ITEM:
1745
    /* Fall through */
1746
  case Item::COND_ITEM:
1747
  case Item::FIELD_AVG_ITEM:
1748
  case Item::FIELD_STD_ITEM:
1749
  case Item::SUBSELECT_ITEM:
1750
    /* The following can only happen with 'CREATE TABLE ... SELECT' */
1751
  case Item::PROC_ITEM:
1752
  case Item::INT_ITEM:
1753
  case Item::REAL_ITEM:
1754
  case Item::DECIMAL_ITEM:
1755
  case Item::STRING_ITEM:
1756
  case Item::REF_ITEM:
1757
  case Item::NULL_ITEM:
1758
  case Item::VARBIN_ITEM:
1759
    if (make_copy_field)
1760
    {
1761
      assert(((Item_result_field*)item)->result_field);
1762
      *from_field= ((Item_result_field*)item)->result_field;
1763
    }
1764
    return create_tmp_field_from_item(session, item, table,
1765
                                      (make_copy_field ? 0 : copy_func),
1766
                                       modify_item, convert_blob_length);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1767
  case Item::TYPE_HOLDER:
575.4.7 by Monty Taylor
More header cleanup.
1768
    result= ((Item_type_holder *)item)->make_field_by_type(table);
1769
    result->set_derivation(item->collation.derivation);
1770
    return result;
1771
  default:					// Dosen't have to be stored
681 by Brian Aker
Style cleanup
1772
    return NULL;
575.4.7 by Monty Taylor
More header cleanup.
1773
  }
1774
}
1775
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
1776
} /* namespace drizzled */