~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Daniel Nichter
  • Date: 2011-10-23 16:01:37 UTC
  • mto: This revision was merged to the branch mainline in revision 2448.
  • Revision ID: daniel@percona.com-20111023160137-7ac3blgz8z4tf8za
Add Administration Getting Started and Logging.  Capitalize SQL clause keywords.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
 
21
 
#include <drizzled/server_includes.h>
22
 
#include CMATH_H
23
 
 
 
20
#include <config.h>
24
21
#include <drizzled/sql_select.h>
25
22
#include <drizzled/error.h>
26
23
#include <drizzled/show.h>
28
25
#include <drizzled/item/cache_row.h>
29
26
#include <drizzled/item/type_holder.h>
30
27
#include <drizzled/item/sum.h>
31
 
#include <drizzled/functions/str/conv_charset.h>
32
 
#include <drizzled/virtual_column_info.h>
 
28
#include <drizzled/item/copy_string.h>
 
29
#include <drizzled/function/str/conv_charset.h>
33
30
#include <drizzled/sql_base.h>
34
 
 
35
 
 
 
31
#include <drizzled/util/convert.h>
 
32
#include <drizzled/plugin/client.h>
 
33
#include <drizzled/time_functions.h>
36
34
#include <drizzled/field/str.h>
37
 
#include <drizzled/field/longstr.h>
38
35
#include <drizzled/field/num.h>
39
36
#include <drizzled/field/blob.h>
 
37
#include <drizzled/field/date.h>
 
38
#include <drizzled/field/datetime.h>
 
39
#include <drizzled/field/decimal.h>
 
40
#include <drizzled/field/double.h>
40
41
#include <drizzled/field/enum.h>
 
42
#include <drizzled/field/epoch.h>
 
43
#include <drizzled/field/int32.h>
 
44
#include <drizzled/field/int64.h>
 
45
#include <drizzled/field/microtime.h>
41
46
#include <drizzled/field/null.h>
42
 
#include <drizzled/field/date.h>
43
 
#include <drizzled/field/fdecimal.h>
44
47
#include <drizzled/field/real.h>
45
 
#include <drizzled/field/double.h>
46
 
#include <drizzled/field/long.h>
47
 
#include <drizzled/field/int64_t.h>
48
 
#include <drizzled/field/num.h>
49
 
#include <drizzled/field/timetype.h>
50
 
#include <drizzled/field/timestamp.h>
51
 
#include <drizzled/field/datetime.h>
52
 
#include <drizzled/field/fstring.h>
 
48
#include <drizzled/field/size.h>
 
49
#include <drizzled/field/time.h>
53
50
#include <drizzled/field/varstring.h>
54
 
 
55
 
 
56
 
#if defined(CMATH_NAMESPACE)
57
 
using namespace CMATH_NAMESPACE;
58
 
#endif
 
51
#include <drizzled/current_session.h>
 
52
#include <drizzled/session.h>
 
53
#include <drizzled/internal/m_string.h>
 
54
#include <drizzled/item/ref.h>
 
55
#include <drizzled/item/subselect.h>
 
56
#include <drizzled/sql_lex.h>
 
57
#include <drizzled/system_variables.h>
 
58
 
 
59
#include <cstdio>
 
60
#include <math.h>
 
61
#include <algorithm>
 
62
#include <float.h>
 
63
 
 
64
using namespace std;
 
65
 
 
66
namespace drizzled {
59
67
 
60
68
const String my_null_string("NULL", 4, default_charset_info);
61
69
 
62
 
 
63
 
const uint32_t NO_CACHED_FIELD_INDEX= UINT32_MAX;
64
 
 
65
 
 
66
 
/*****************************************************************************
67
 
** Item functions
68
 
*****************************************************************************/
69
 
 
70
 
/**
71
 
  Init all special items.
72
 
*/
73
 
 
74
 
void item_init(void)
75
 
{
76
 
}
77
 
 
78
 
 
79
70
bool Item::is_expensive_processor(unsigned char *)
80
71
{
81
 
  return 0;
 
72
  return false;
82
73
}
83
74
 
84
 
void Item::fix_after_pullout(st_select_lex *, Item **)
 
75
void Item::fix_after_pullout(Select_Lex *, Item **)
85
76
{}
86
77
 
87
 
 
88
78
Field *Item::tmp_table_field(Table *)
89
79
{
90
 
  return 0;
 
80
  return NULL;
91
81
}
92
82
 
93
 
 
94
83
const char *Item::full_name(void) const
95
84
{
96
85
  return name ? name : "???";
97
86
}
98
87
 
99
 
 
100
88
int64_t Item::val_int_endpoint(bool, bool *)
101
89
{
102
90
  assert(0);
103
91
  return 0;
104
92
}
105
93
 
106
 
 
107
 
/**
108
 
  @todo
109
 
    Make this functions class dependent
110
 
*/
111
 
 
 
94
/** @todo Make this functions class dependent */
112
95
bool Item::val_bool()
113
96
{
114
 
  switch(result_type()) {
115
 
  case INT_RESULT:
116
 
    return val_int() != 0;
117
 
  case DECIMAL_RESULT:
 
97
  switch(result_type()) 
118
98
  {
119
 
    my_decimal decimal_value;
120
 
    my_decimal *val= val_decimal(&decimal_value);
121
 
    if (val)
122
 
      return !my_decimal_is_zero(val);
123
 
    return 0;
124
 
  }
125
 
  case REAL_RESULT:
126
 
  case STRING_RESULT:
127
 
    return val_real() != 0.0;
128
 
  case ROW_RESULT:
129
 
  default:
130
 
    assert(0);
131
 
    return 0;                                   // Wrong (but safe)
132
 
  }
 
99
    case INT_RESULT:
 
100
      return val_int() != 0;
 
101
 
 
102
    case DECIMAL_RESULT:
 
103
    {
 
104
      type::Decimal decimal_value;
 
105
      type::Decimal *val= val_decimal(&decimal_value);
 
106
      if (val)
 
107
        return not val->isZero();
 
108
      return false;
 
109
    }
 
110
 
 
111
    case REAL_RESULT:
 
112
    case STRING_RESULT:
 
113
      return val_real() != 0.0;
 
114
 
 
115
    case ROW_RESULT:
 
116
      assert(0);
 
117
      abort();
 
118
  }
 
119
 
 
120
  assert(0);
 
121
  abort();
133
122
}
134
123
 
135
 
 
136
124
String *Item::val_string_from_real(String *str)
137
125
{
138
126
  double nr= val_real();
139
127
  if (null_value)
140
 
    return 0;                                   /* purecov: inspected */
141
 
  str->set_real(nr,decimals, &my_charset_bin);
 
128
    return NULL;
 
129
 
 
130
  str->set_real(nr, decimals, &my_charset_bin);
142
131
  return str;
143
132
}
144
133
 
145
 
 
146
134
String *Item::val_string_from_int(String *str)
147
135
{
148
136
  int64_t nr= val_int();
149
137
  if (null_value)
150
 
    return 0;
 
138
    return NULL;
 
139
 
151
140
  str->set_int(nr, unsigned_flag, &my_charset_bin);
152
141
  return str;
153
142
}
154
143
 
155
 
 
156
144
String *Item::val_string_from_decimal(String *str)
157
145
{
158
 
  my_decimal dec_buf, *dec= val_decimal(&dec_buf);
 
146
  type::Decimal dec_buf, *dec= val_decimal(&dec_buf);
159
147
  if (null_value)
160
 
    return 0;
161
 
  my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
162
 
  my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, 0, str);
 
148
    return NULL;
 
149
 
 
150
  class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
 
151
  class_decimal2string(&dec_buf, 0, str);
163
152
  return str;
164
153
}
165
154
 
166
 
 
167
 
my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value)
 
155
type::Decimal *Item::val_decimal_from_real(type::Decimal *decimal_value)
168
156
{
169
157
  double nr= val_real();
170
158
  if (null_value)
171
 
    return 0;
172
 
  double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
 
159
    return NULL;
 
160
 
 
161
  double2_class_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
173
162
  return (decimal_value);
174
163
}
175
164
 
176
 
 
177
 
my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value)
 
165
type::Decimal *Item::val_decimal_from_int(type::Decimal *decimal_value)
178
166
{
179
167
  int64_t nr= val_int();
180
168
  if (null_value)
181
 
    return 0;
182
 
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
 
169
    return NULL;
 
170
 
 
171
  int2_class_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
183
172
  return decimal_value;
184
173
}
185
174
 
186
 
 
187
 
my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value)
 
175
type::Decimal *Item::val_decimal_from_string(type::Decimal *decimal_value)
188
176
{
189
177
  String *res;
190
 
  char *end_ptr;
191
178
  if (!(res= val_str(&str_value)))
192
 
    return 0;                                   // NULL or EOM
 
179
    return NULL;
193
180
 
194
 
  end_ptr= (char*) res->ptr()+ res->length();
195
 
  if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
196
 
                     res->ptr(), res->length(), res->charset(),
197
 
                     decimal_value) & E_DEC_BAD_NUM)
 
181
  if (decimal_value->store(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
 
182
                     res->ptr(), 
 
183
                     res->length(), 
 
184
                     res->charset()) & E_DEC_BAD_NUM)
198
185
  {
199
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
186
    push_warning_printf(&getSession(), 
 
187
                        DRIZZLE_ERROR::WARN_LEVEL_WARN,
200
188
                        ER_TRUNCATED_WRONG_VALUE,
201
189
                        ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
202
190
                        str_value.c_ptr());
204
192
  return decimal_value;
205
193
}
206
194
 
207
 
 
208
 
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
 
195
type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
209
196
{
210
 
  assert(fixed == 1);
211
 
  DRIZZLE_TIME ltime;
212
 
  if (get_date(&ltime, TIME_FUZZY_DATE))
 
197
  assert(fixed);
 
198
  type::Time ltime;
 
199
  if (get_date(ltime, TIME_FUZZY_DATE))
213
200
  {
214
 
    my_decimal_set_zero(decimal_value);
 
201
    decimal_value->set_zero();
215
202
    null_value= 1;                               // set NULL, stop processing
216
 
    return 0;
 
203
    return NULL;
217
204
  }
218
 
  return date2my_decimal(&ltime, decimal_value);
 
205
  return date2_class_decimal(&ltime, decimal_value);
219
206
}
220
207
 
221
 
 
222
 
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
 
208
type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
223
209
{
224
 
  assert(fixed == 1);
225
 
  DRIZZLE_TIME ltime;
226
 
  if (get_time(&ltime))
 
210
  assert(fixed);
 
211
  type::Time ltime;
 
212
  if (get_time(ltime))
227
213
  {
228
 
    my_decimal_set_zero(decimal_value);
229
 
    return 0;
 
214
    decimal_value->set_zero();
 
215
    return NULL;
230
216
  }
231
 
  return date2my_decimal(&ltime, decimal_value);
 
217
  return date2_class_decimal(&ltime, decimal_value);
232
218
}
233
219
 
234
 
 
235
220
double Item::val_real_from_decimal()
236
221
{
237
222
  /* Note that fix_fields may not be called for Item_avg_field items */
238
223
  double result;
239
 
  my_decimal value_buff, *dec_val= val_decimal(&value_buff);
 
224
  type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
240
225
  if (null_value)
241
226
    return 0.0;
242
 
  my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
 
227
  class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
243
228
  return result;
244
229
}
245
230
 
246
 
 
247
231
int64_t Item::val_int_from_decimal()
248
232
{
249
233
  /* Note that fix_fields may not be called for Item_avg_field items */
250
234
  int64_t result;
251
 
  my_decimal value, *dec_val= val_decimal(&value);
 
235
  type::Decimal value, *dec_val= val_decimal(&value);
 
236
 
252
237
  if (null_value)
253
238
    return 0;
254
 
  my_decimal2int(E_DEC_FATAL_ERROR, dec_val, unsigned_flag, &result);
 
239
  dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
 
240
 
255
241
  return result;
256
242
}
257
243
 
258
 
int Item::save_time_in_field(Field *field)
259
 
{
260
 
  DRIZZLE_TIME ltime;
261
 
  if (get_time(&ltime))
262
 
    return set_field_to_null(field);
263
 
  field->set_notnull();
264
 
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_TIME);
265
 
}
266
 
 
267
 
 
268
 
int Item::save_date_in_field(Field *field)
269
 
{
270
 
  DRIZZLE_TIME ltime;
271
 
  if (get_date(&ltime, TIME_FUZZY_DATE))
272
 
    return set_field_to_null(field);
273
 
  field->set_notnull();
274
 
  return field->store_time(&ltime, DRIZZLE_TIMESTAMP_DATETIME);
275
 
}
276
 
 
277
 
 
278
 
/*
279
 
  Store the string value in field directly
280
 
 
281
 
  SYNOPSIS
282
 
    Item::save_str_value_in_field()
283
 
    field   a pointer to field where to store
284
 
    result  the pointer to the string value to be stored
285
 
 
286
 
  DESCRIPTION
287
 
    The method is used by Item_*::save_in_field implementations
288
 
    when we don't need to calculate the value to store
289
 
    See Item_string::save_in_field() implementation for example
290
 
 
291
 
  IMPLEMENTATION
292
 
    Check if the Item is null and stores the NULL or the
293
 
    result value in the field accordingly.
294
 
 
295
 
  RETURN
296
 
    Nonzero value if error
297
 
*/
298
 
 
 
244
bool Item::save_time_in_field(Field *field)
 
245
{
 
246
  type::Time ltime;
 
247
 
 
248
  if (get_time(ltime))
 
249
    return set_field_to_null(field);
 
250
 
 
251
  field->set_notnull();
 
252
 
 
253
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
 
254
}
 
255
 
 
256
bool Item::save_date_in_field(Field *field)
 
257
{
 
258
  type::Time ltime;
 
259
 
 
260
  if (get_date(ltime, TIME_FUZZY_DATE))
 
261
    return set_field_to_null(field);
 
262
 
 
263
  field->set_notnull();
 
264
 
 
265
  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
 
266
}
 
267
 
 
268
/**
 
269
 * Check if the Item is null and stores the NULL or the
 
270
 * result value in the field accordingly.
 
271
 */
299
272
int Item::save_str_value_in_field(Field *field, String *result)
300
273
{
301
274
  if (null_value)
302
275
    return set_field_to_null(field);
 
276
 
303
277
  field->set_notnull();
304
 
  return field->store(result->ptr(), result->length(),
305
 
                      collation.collation);
 
278
 
 
279
  return field->store(result->ptr(), result->length(), collation.collation);
306
280
}
307
281
 
308
 
 
309
282
Item::Item():
310
 
  is_expensive_cache(-1), rsize(0), name(0), orig_name(0), name_length(0),
311
 
  fixed(0), is_autogenerated_name(true),
312
 
  collation(&my_charset_bin, DERIVATION_COERCIBLE)
 
283
  is_expensive_cache(-1),
 
284
  name(0), 
 
285
  name_length(0),
 
286
  max_length(0), 
 
287
  marker(0),
 
288
  decimals(0),
 
289
  fixed(false),
 
290
  maybe_null(false),
 
291
  null_value(false),
 
292
  unsigned_flag(false), 
 
293
  with_sum_func(false),
 
294
  is_autogenerated_name(true),
 
295
  with_subselect(false),
 
296
  collation(&my_charset_bin, DERIVATION_COERCIBLE),
 
297
  _session(*current_session)
313
298
{
314
 
  marker= 0;
315
 
  maybe_null= false;
316
 
  null_value= false;
317
 
  with_sum_func= false;
318
 
  unsigned_flag= false;
319
 
  decimals= 0; 
320
 
  max_length= 0;
321
 
  with_subselect= 0;
322
299
  cmp_context= (Item_result)-1;
323
300
 
324
301
  /* Put item in free list so that we can free all items at end */
325
 
  Session *session= current_session;
326
 
  next= session->free_list;
327
 
  session->free_list= this;
 
302
  next= getSession().free_list;
 
303
  getSession().free_list= this;
 
304
 
328
305
  /*
329
306
    Item constructor can be called during execution other then SQL_COM
330
 
    command => we should check session->lex->current_select on zero (session->lex
 
307
    command => we should check session->lex().current_select on zero (session->lex
331
308
    can be uninitialised)
332
309
  */
333
 
  if (session->lex->current_select)
 
310
  if (getSession().lex().current_select)
334
311
  {
335
 
    enum_parsing_place place= 
336
 
      session->lex->current_select->parsing_place;
337
 
    if (place == SELECT_LIST ||
338
 
        place == IN_HAVING)
339
 
      session->lex->current_select->select_n_having_items++;
 
312
    enum_parsing_place place= getSession().lex().current_select->parsing_place;
 
313
    if (place == SELECT_LIST || place == IN_HAVING)
 
314
      getSession().lex().current_select->select_n_having_items++;
340
315
  }
341
316
}
342
317
 
343
 
/**
344
 
  Constructor used by Item_field, Item_ref & aggregate (sum)
345
 
  functions.
346
 
 
347
 
  Used for duplicating lists in processing queries with temporary
348
 
  tables.
349
 
*/
350
318
Item::Item(Session *session, Item *item):
351
319
  is_expensive_cache(-1),
352
 
  rsize(0),
353
320
  str_value(item->str_value),
354
321
  name(item->name),
355
 
  orig_name(item->orig_name),
 
322
  name_length(item->name_length),
356
323
  max_length(item->max_length),
357
324
  marker(item->marker),
358
325
  decimals(item->decimals),
 
326
  fixed(item->fixed),
359
327
  maybe_null(item->maybe_null),
360
328
  null_value(item->null_value),
361
329
  unsigned_flag(item->unsigned_flag),
362
330
  with_sum_func(item->with_sum_func),
363
 
  fixed(item->fixed),
 
331
  is_autogenerated_name(item->is_autogenerated_name),
 
332
  with_subselect(item->with_subselect),
364
333
  collation(item->collation),
365
 
  cmp_context(item->cmp_context)
 
334
  cmp_context(item->cmp_context),
 
335
  _session(*session)
366
336
{
367
 
  next= session->free_list;                             // Put in free list
368
 
  session->free_list= this;
 
337
  /* Put this item in the session's free list */
 
338
  next= getSession().free_list;
 
339
  getSession().free_list= this;
369
340
}
370
341
 
 
342
uint32_t Item::float_length(uint32_t decimals_par) const
 
343
{
 
344
  return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;
 
345
}
371
346
 
372
347
uint32_t Item::decimal_precision() const
373
348
{
374
349
  Item_result restype= result_type();
375
350
 
376
351
  if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
377
 
    return cmin(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
378
 
               (unsigned int)DECIMAL_MAX_PRECISION);
379
 
  return cmin(max_length, (uint32_t)DECIMAL_MAX_PRECISION);
 
352
    return min(class_decimal_length_to_precision(max_length, decimals, unsigned_flag),
 
353
               (uint32_t) DECIMAL_MAX_PRECISION);
 
354
  return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
380
355
}
381
356
 
382
 
 
383
357
int Item::decimal_int_part() const
384
358
{
385
 
  return my_decimal_int_part(decimal_precision(), decimals);
 
359
  return class_decimal_int_part(decimal_precision(), decimals);
386
360
}
387
361
 
388
 
 
389
 
void Item::print(String *str, enum_query_type)
 
362
void Item::print(String *str)
390
363
{
391
364
  str->append(full_name());
392
365
}
393
366
 
394
 
 
395
 
void Item::print_item_w_name(String *str, enum_query_type query_type)
 
367
void Item::print_item_w_name(String *str)
396
368
{
397
 
  print(str, query_type);
 
369
  print(str);
398
370
 
399
371
  if (name)
400
372
  {
401
 
    Session *session= current_session;
402
373
    str->append(STRING_WITH_LEN(" AS "));
403
 
    append_identifier(session, str, name, (uint) strlen(name));
 
374
    str->append_identifier(str_ref(name));
404
375
  }
405
376
}
406
377
 
407
 
 
408
378
void Item::split_sum_func(Session *, Item **, List<Item> &)
409
379
{}
410
380
 
411
 
 
412
381
void Item::cleanup()
413
382
{
414
 
  fixed=0;
 
383
  fixed= false;
415
384
  marker= 0;
416
 
  if (orig_name)
417
 
    name= orig_name;
418
 
  return;
419
 
}
420
 
 
421
 
 
422
 
/**
423
 
  cleanup() item if it is 'fixed'.
424
 
 
425
 
  @param arg   a dummy parameter, is not used here
426
 
*/
427
 
 
428
 
bool Item::cleanup_processor(unsigned char *)
429
 
{
430
 
  if (fixed)
431
 
    cleanup();
432
 
  return false;
433
 
}
434
 
 
435
 
 
436
 
/**
437
 
  rename item (used for views, cleanup() return original name).
438
 
 
439
 
  @param new_name       new name of item;
440
 
*/
441
 
 
442
 
void Item::rename(char *new_name)
443
 
{
444
 
  /*
445
 
    we can compare pointers to names here, because if name was not changed,
446
 
    pointer will be same
447
 
  */
448
 
  if (!orig_name && new_name != name)
449
 
    orig_name= name;
450
 
  name= new_name;
451
 
}
452
 
 
453
 
 
454
 
/**
455
 
  Traverse item tree possibly transforming it (replacing items).
456
 
 
457
 
  This function is designed to ease transformation of Item trees.
458
 
  Re-execution note: every such transformation is registered for
459
 
  rollback by Session::change_item_tree() and is rolled back at the end
460
 
  of execution by Session::rollback_item_tree_changes().
461
 
 
462
 
  Therefore:
463
 
  - this function can not be used at prepared statement prepare
464
 
  (in particular, in fix_fields!), as only permanent
465
 
  transformation of Item trees are allowed at prepare.
466
 
  - the transformer function shall allocate new Items in execution
467
 
  memory root (session->mem_root) and not anywhere else: allocated
468
 
  items will be gone in the end of execution.
469
 
 
470
 
  If you don't need to transform an item tree, but only traverse
471
 
  it, please use Item::walk() instead.
472
 
 
473
 
 
474
 
  @param transformer    functor that performs transformation of a subtree
475
 
  @param arg            opaque argument passed to the functor
476
 
 
477
 
  @return
478
 
    Returns pointer to the new subtree root.  Session::change_item_tree()
479
 
    should be called for it if transformation took place, i.e. if a
480
 
    pointer to newly allocated item is returned.
481
 
*/
 
385
}
482
386
 
483
387
Item* Item::transform(Item_transformer transformer, unsigned char *arg)
484
388
{
485
389
  return (this->*transformer)(arg);
486
390
}
487
391
 
488
 
 
489
 
Item_ident::Item_ident(Name_resolution_context *context_arg,
490
 
                       const char *db_name_arg,const char *table_name_arg,
491
 
                       const char *field_name_arg)
492
 
  :orig_db_name(db_name_arg), orig_table_name(table_name_arg),
493
 
   orig_field_name(field_name_arg), context(context_arg),
494
 
   db_name(db_name_arg), table_name(table_name_arg),
495
 
   field_name(field_name_arg),
496
 
   alias_name_used(false), cached_field_index(NO_CACHED_FIELD_INDEX),
497
 
   cached_table(0), depended_from(0)
498
 
{
499
 
  name = (char*) field_name_arg;
500
 
}
501
 
 
502
 
 
503
 
/**
504
 
  Constructor used by Item_field & Item_*_ref (see Item comment)
505
 
*/
506
 
 
507
 
Item_ident::Item_ident(Session *session, Item_ident *item)
508
 
  :Item(session, item),
509
 
   orig_db_name(item->orig_db_name),
510
 
   orig_table_name(item->orig_table_name), 
511
 
   orig_field_name(item->orig_field_name),
512
 
   context(item->context),
513
 
   db_name(item->db_name),
514
 
   table_name(item->table_name),
515
 
   field_name(item->field_name),
516
 
   alias_name_used(item->alias_name_used),
517
 
   cached_field_index(item->cached_field_index),
518
 
   cached_table(item->cached_table),
519
 
   depended_from(item->depended_from)
520
 
{}
521
 
 
522
 
void Item_ident::cleanup()
523
 
{
524
 
#ifdef CANT_BE_USED_AS_MEMORY_IS_FREED
525
 
                       db_name ? db_name : "(null)",
526
 
                       orig_db_name ? orig_db_name : "(null)",
527
 
                       table_name ? table_name : "(null)",
528
 
                       orig_table_name ? orig_table_name : "(null)",
529
 
                       field_name ? field_name : "(null)",
530
 
                       orig_field_name ? orig_field_name : "(null)"));
531
 
#endif
532
 
  Item::cleanup();
533
 
  db_name= orig_db_name; 
534
 
  table_name= orig_table_name;
535
 
  field_name= orig_field_name;
536
 
  depended_from= 0;
537
 
  return;
538
 
}
539
 
 
540
 
bool Item_ident::remove_dependence_processor(unsigned char * arg)
541
 
{
542
 
  if (depended_from == (st_select_lex *) arg)
543
 
    depended_from= 0;
544
 
  return(0);
545
 
}
546
 
 
547
 
 
548
 
/**
549
 
  Store the pointer to this item field into a list if not already there.
550
 
 
551
 
  The method is used by Item::walk to collect all unique Item_field objects
552
 
  from a tree of Items into a set of items represented as a list.
553
 
 
554
 
  Item_cond::walk() and Item_func::walk() stop the evaluation of the
555
 
  processor function for its arguments once the processor returns
556
 
  true.Therefore in order to force this method being called for all item
557
 
  arguments in a condition the method must return false.
558
 
 
559
 
  @param arg  pointer to a List<Item_field>
560
 
 
561
 
  @return
562
 
    false to force the evaluation of collect_item_field_processor
563
 
    for the subsequent items.
564
 
*/
565
 
 
566
 
bool Item_field::collect_item_field_processor(unsigned char *arg)
567
 
{
568
 
  List<Item_field> *item_list= (List<Item_field>*) arg;
569
 
  List_iterator<Item_field> item_list_it(*item_list);
570
 
  Item_field *curr_item;
571
 
  while ((curr_item= item_list_it++))
572
 
  {
573
 
    if (curr_item->eq(this, 1))
574
 
      return(false); /* Already in the set. */
575
 
  }
576
 
  item_list->push_back(this);
577
 
  return(false);
578
 
}
579
 
 
580
 
 
581
 
/**
582
 
  Check if an Item_field references some field from a list of fields.
583
 
 
584
 
  Check whether the Item_field represented by 'this' references any
585
 
  of the fields in the keyparts passed via 'arg'. Used with the
586
 
  method Item::walk() to test whether any keypart in a sequence of
587
 
  keyparts is referenced in an expression.
588
 
 
589
 
  @param arg   Field being compared, arg must be of type Field
590
 
 
591
 
  @retval
592
 
    true  if 'this' references the field 'arg'
593
 
  @retval
594
 
    false otherwise
595
 
*/
596
 
 
597
 
bool Item_field::find_item_in_field_list_processor(unsigned char *arg)
598
 
{
599
 
  KEY_PART_INFO *first_non_group_part= *((KEY_PART_INFO **) arg);
600
 
  KEY_PART_INFO *last_part= *(((KEY_PART_INFO **) arg) + 1);
601
 
  KEY_PART_INFO *cur_part;
602
 
 
603
 
  for (cur_part= first_non_group_part; cur_part != last_part; cur_part++)
604
 
  {
605
 
    if (field->eq(cur_part->field))
606
 
      return true;
607
 
  }
608
 
  return false;
609
 
}
610
 
 
611
 
 
612
 
/*
613
 
  Mark field in read_map
614
 
 
615
 
  NOTES
616
 
    This is used by filesort to register used fields in a a temporary
617
 
    column read set or to register used fields in a view
618
 
*/
619
 
 
620
 
bool Item_field::register_field_in_read_map(unsigned char *arg)
621
 
{
622
 
  Table *table= (Table *) arg;
623
 
  if (field->table == table || !table)
624
 
    bitmap_set_bit(field->table->read_set, field->field_index);
625
 
  if (field->vcol_info && field->vcol_info->expr_item)
626
 
    return field->vcol_info->expr_item->walk(&Item::register_field_in_read_map, 
627
 
                                             1, arg);
628
 
  return 0;
629
 
}
630
 
 
631
 
/*
632
 
  Mark field in bitmap supplied as *arg
633
 
 
634
 
*/
635
 
 
636
 
bool Item_field::register_field_in_bitmap(unsigned char *arg)
637
 
{
638
 
  MY_BITMAP *bitmap= (MY_BITMAP *) arg;
639
 
  assert(bitmap);
640
 
  bitmap_set_bit(bitmap, field->field_index);
641
 
  return false;
642
 
}
643
 
 
644
 
 
645
392
bool Item::check_cols(uint32_t c)
646
393
{
647
394
  if (c != 1)
648
395
  {
649
396
    my_error(ER_OPERAND_COLUMNS, MYF(0), c);
650
 
    return 1;
 
397
    return true;
651
398
  }
652
 
  return 0;
 
399
  return false;
653
400
}
654
401
 
655
 
 
656
 
void Item::set_name(const char *str, uint32_t length, const CHARSET_INFO * const cs)
 
402
void Item::set_name(const char *str, uint32_t length, const charset_info_st * const cs)
657
403
{
658
404
  if (!length)
659
405
  {
660
406
    /* Empty string, used by AS or internal function like last_insert_id() */
661
 
    name= (char*) str;
 
407
    name= str;
662
408
    name_length= 0;
663
409
    return;
664
410
  }
665
411
  if (cs->ctype)
666
412
  {
667
413
    uint32_t orig_len= length;
668
 
    /*
669
 
      This will probably need a better implementation in the future:
670
 
      a function in CHARSET_INFO structure.
671
 
    */
672
 
    while (length && !my_isgraph(cs,*str))
673
 
    {                                           // Fix problem with yacc
 
414
    while (length && ! my_isgraph(cs, *str))
 
415
    {
 
416
      /* Fix problem with yacc */
674
417
      length--;
675
418
      str++;
676
419
    }
677
 
    if (orig_len != length && !is_autogenerated_name)
 
420
    if (orig_len != length && ! is_autogenerated_name)
678
421
    {
679
422
      if (length == 0)
680
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
681
 
                            ER_NAME_BECOMES_EMPTY, ER(ER_NAME_BECOMES_EMPTY),
682
 
                            str + length - orig_len);
 
423
        push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NAME_BECOMES_EMPTY, ER(ER_NAME_BECOMES_EMPTY), str + length - orig_len);
683
424
      else
684
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
685
 
                            ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES),
686
 
                            str + length - orig_len);
 
425
        push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES), str + length - orig_len);
687
426
    }
688
427
  }
689
 
  if (!my_charset_same(cs, system_charset_info))
690
 
  {
691
 
    size_t res_length;
692
 
    name= sql_strmake_with_convert(str, name_length= length, cs,
693
 
                                   MAX_ALIAS_NAME, system_charset_info,
694
 
                                   &res_length);
695
 
  }
696
 
  else
697
 
    name= sql_strmake(str, (name_length= cmin(length,(unsigned int)MAX_ALIAS_NAME)));
 
428
  name= memory::sql_strdup(str_ref(str, length));
698
429
}
699
430
 
700
 
 
701
 
/**
702
 
  @details
703
 
  This function is called when:
704
 
  - Comparing items in the WHERE clause (when doing where optimization)
705
 
  - When trying to find an order_st BY/GROUP BY item in the SELECT part
706
 
*/
707
 
 
708
431
bool Item::eq(const Item *item, bool) const
709
432
{
710
433
  /*
712
435
    for all basic constants we have special checks, and Item_param's
713
436
    type() can be only among basic constant types.
714
437
  */
715
 
  return type() == item->type() && name && item->name &&
716
 
    !my_strcasecmp(system_charset_info,name,item->name);
 
438
  return type() == item->type() && 
 
439
         name && 
 
440
         item->name &&
 
441
         ! my_strcasecmp(system_charset_info, name, item->name);
717
442
}
718
443
 
719
 
 
720
 
Item *Item::safe_charset_converter(const CHARSET_INFO * const tocs)
 
444
Item *Item::safe_charset_converter(const charset_info_st * const tocs)
721
445
{
722
446
  Item_func_conv_charset *conv= new Item_func_conv_charset(this, tocs, 1);
723
447
  return conv->safe ? conv : NULL;
724
448
}
725
449
 
726
 
 
727
 
/**
728
 
  @details
729
 
  Created mostly for mysql_prepare_table(). Important
730
 
  when a string ENUM/SET column is described with a numeric default value:
731
 
 
732
 
  CREATE TABLE t1(a SET('a') DEFAULT 1);
733
 
 
734
 
  We cannot use generic Item::safe_charset_converter(), because
735
 
  the latter returns a non-fixed Item, so val_str() crashes afterwards.
736
 
  Override Item_num method, to return a fixed item.
737
 
*/
738
 
Item *Item_num::safe_charset_converter(const CHARSET_INFO * const)
739
 
{
740
 
  Item_string *conv;
741
 
  char buf[64];
742
 
  String *s, tmp(buf, sizeof(buf), &my_charset_bin);
743
 
  s= val_str(&tmp);
744
 
  if ((conv= new Item_string(s->ptr(), s->length(), s->charset())))
745
 
  {
746
 
    conv->str_value.copy();
747
 
    conv->str_value.mark_as_const();
748
 
  }
749
 
  return conv;
750
 
}
751
 
 
752
 
 
753
 
Item *Item_static_float_func::safe_charset_converter(const CHARSET_INFO * const)
754
 
{
755
 
  Item_string *conv;
756
 
  char buf[64];
757
 
  String *s, tmp(buf, sizeof(buf), &my_charset_bin);
758
 
  s= val_str(&tmp);
759
 
  if ((conv= new Item_static_string_func(func_name, s->ptr(), s->length(),
760
 
                                         s->charset())))
761
 
  {
762
 
    conv->str_value.copy();
763
 
    conv->str_value.mark_as_const();
764
 
  }
765
 
  return conv;
766
 
}
767
 
 
768
 
 
769
 
Item *Item_string::safe_charset_converter(const CHARSET_INFO * const tocs)
770
 
{
771
 
  Item_string *conv;
772
 
  uint32_t conv_errors;
773
 
  char *ptr;
774
 
  String tmp, cstr, *ostr= val_str(&tmp);
775
 
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
776
 
  if (conv_errors || !(conv= new Item_string(cstr.ptr(), cstr.length(),
777
 
                                             cstr.charset(),
778
 
                                             collation.derivation)))
779
 
  {
780
 
    /*
781
 
      Safe conversion is not possible (or EOM).
782
 
      We could not convert a string into the requested character set
783
 
      without data loss. The target charset does not cover all the
784
 
      characters from the string. Operation cannot be done correctly.
785
 
    */
786
 
    return NULL;
787
 
  }
788
 
  if (!(ptr= current_session->strmake(cstr.ptr(), cstr.length())))
789
 
    return NULL;
790
 
  conv->str_value.set(ptr, cstr.length(), cstr.charset());
791
 
  /* Ensure that no one is going to change the result string */
792
 
  conv->str_value.mark_as_const();
793
 
  return conv;
794
 
}
795
 
 
796
 
 
797
 
Item *Item_param::safe_charset_converter(const CHARSET_INFO * const tocs)
798
 
{
799
 
  if (const_item())
800
 
  {
801
 
    uint32_t cnv_errors;
802
 
    String *ostr= val_str(&cnvstr);
803
 
    cnvitem->str_value.copy(ostr->ptr(), ostr->length(),
804
 
                            ostr->charset(), tocs, &cnv_errors);
805
 
    if (cnv_errors)
806
 
       return NULL;
807
 
    cnvitem->str_value.mark_as_const();
808
 
    cnvitem->max_length= cnvitem->str_value.numchars() * tocs->mbmaxlen;
809
 
    return cnvitem;
810
 
  }
811
 
  return NULL;
812
 
}
813
 
 
814
 
 
815
 
Item *Item_static_string_func::safe_charset_converter(const CHARSET_INFO * const tocs)
816
 
{
817
 
  Item_string *conv;
818
 
  uint32_t conv_errors;
819
 
  String tmp, cstr, *ostr= val_str(&tmp);
820
 
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
821
 
  if (conv_errors ||
822
 
      !(conv= new Item_static_string_func(func_name,
823
 
                                          cstr.ptr(), cstr.length(),
824
 
                                          cstr.charset(),
825
 
                                          collation.derivation)))
826
 
  {
827
 
    /*
828
 
      Safe conversion is not possible (or EOM).
829
 
      We could not convert a string into the requested character set
830
 
      without data loss. The target charset does not cover all the
831
 
      characters from the string. Operation cannot be done correctly.
832
 
    */
833
 
    return NULL;
834
 
  }
835
 
  conv->str_value.copy();
836
 
  /* Ensure that no one is going to change the result string */
837
 
  conv->str_value.mark_as_const();
838
 
  return conv;
839
 
}
840
 
 
841
 
 
842
 
bool Item_string::eq(const Item *item, bool binary_cmp) const
843
 
{
844
 
  if (type() == item->type() && item->basic_const_item())
845
 
  {
846
 
    if (binary_cmp)
847
 
      return !stringcmp(&str_value, &item->str_value);
848
 
    return (collation.collation == item->collation.collation &&
849
 
            !sortcmp(&str_value, &item->str_value, collation.collation));
850
 
  }
851
 
  return 0;
852
 
}
853
 
 
854
 
 
855
 
/**
856
 
  Get the value of the function as a DRIZZLE_TIME structure.
857
 
  As a extra convenience the time structure is reset on error!
858
 
*/
859
 
 
860
 
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
861
 
{
862
 
  if (result_type() == STRING_RESULT)
863
 
  {
864
 
    char buff[40];
865
 
    String tmp(buff,sizeof(buff), &my_charset_bin),*res;
866
 
    if (!(res=val_str(&tmp)) ||
867
 
        str_to_datetime_with_warn(res->ptr(), res->length(),
868
 
                                  ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
869
 
      goto err;
870
 
  }
871
 
  else
872
 
  {
873
 
    int64_t value= val_int();
874
 
    int was_cut;
875
 
    if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
876
 
    {
877
 
      char buff[22], *end;
878
 
      end= int64_t10_to_str(value, buff, -10);
879
 
      make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
880
 
                                   buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
881
 
                                   NULL);
882
 
      goto err;
883
 
    }
884
 
  }
885
 
  return 0;
886
 
 
887
 
err:
888
 
  memset(ltime, 0, sizeof(*ltime));
889
 
  return 1;
890
 
}
891
 
 
892
 
/**
893
 
  Get time of first argument.\
894
 
 
895
 
  As a extra convenience the time structure is reset on error!
896
 
*/
897
 
 
898
 
bool Item::get_time(DRIZZLE_TIME *ltime)
899
 
{
900
 
  char buff[40];
 
450
bool Item::get_date(type::Time &ltime,uint32_t fuzzydate)
 
451
{
 
452
  do
 
453
  {
 
454
    if (is_null())
 
455
    {
 
456
      break;
 
457
    }
 
458
    else if (result_type() == STRING_RESULT)
 
459
    {
 
460
      char buff[type::Time::MAX_STRING_LENGTH];
 
461
      String tmp(buff,sizeof(buff), &my_charset_bin),*res;
 
462
      if (!(res=val_str(&tmp)) ||
 
463
          str_to_datetime_with_warn(&getSession(), res->ptr(), res->length(),
 
464
                                    &ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
 
465
      {
 
466
        break;
 
467
      }
 
468
    }
 
469
    else
 
470
    {
 
471
      int64_t value= val_int();
 
472
      type::datetime_t date_value;
 
473
 
 
474
      ltime.convert(date_value, value, fuzzydate);
 
475
 
 
476
      if (not type::is_valid(date_value))
 
477
      {
 
478
        char buff[DECIMAL_LONGLONG_DIGITS], *end;
 
479
        end= internal::int64_t10_to_str(value, buff, -10);
 
480
        make_truncated_value_warning(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
481
                                     buff, (int) (end-buff), type::DRIZZLE_TIMESTAMP_NONE, NULL);
 
482
        break;
 
483
      }
 
484
    }
 
485
 
 
486
    return false;
 
487
  } while (0);
 
488
 
 
489
  ltime.reset();
 
490
 
 
491
  return true;
 
492
}
 
493
 
 
494
bool Item::get_time(type::Time &ltime)
 
495
{
 
496
  char buff[type::Time::MAX_STRING_LENGTH];
901
497
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
902
 
  if (!(res=val_str(&tmp)) ||
903
 
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
 
498
  if (!(res=val_str(&tmp)) or
 
499
      str_to_time_with_warn(&getSession(), res->ptr(), res->length(), &ltime))
904
500
  {
905
 
    memset(ltime, 0, sizeof(*ltime));
 
501
    ltime.reset();
 
502
 
906
503
    return true;
907
504
  }
 
505
 
908
506
  return false;
909
507
}
910
508
 
911
 
 
912
 
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
509
bool Item::get_date_result(type::Time &ltime,uint32_t fuzzydate)
913
510
{
914
 
  return get_date(ltime,fuzzydate);
 
511
  return get_date(ltime, fuzzydate);
915
512
}
916
513
 
917
 
 
918
514
bool Item::is_null()
919
515
{
920
516
  return false;
921
517
}
922
518
 
923
 
 
924
519
void Item::update_null_value ()
925
520
{
926
521
  (void) val_int();
927
522
}
928
523
 
929
 
 
930
524
void Item::top_level_item(void)
931
525
{}
932
526
 
933
 
 
934
527
void Item::set_result_field(Field *)
935
528
{}
936
529
 
937
 
 
938
530
bool Item::is_result_field(void)
939
531
{
940
 
  return 0;
 
532
  return false;
941
533
}
942
534
 
943
 
 
944
535
bool Item::is_bool_func(void)
945
536
{
946
 
  return 0;
 
537
  return false;
947
538
}
948
539
 
949
 
 
950
540
void Item::save_in_result_field(bool)
951
541
{}
952
542
 
953
 
 
954
543
void Item::no_rows_in_result(void)
955
544
{}
956
545
 
957
 
 
958
546
Item *Item::copy_or_same(Session *)
959
547
{
960
548
  return this;
961
549
}
962
550
 
963
 
 
964
551
Item *Item::copy_andor_structure(Session *)
965
552
{
966
553
  return this;
967
554
}
968
555
 
969
 
 
970
556
Item *Item::real_item(void)
971
557
{
972
558
  return this;
973
559
}
974
560
 
 
561
const Item *Item::real_item(void) const
 
562
{
 
563
  return this;
 
564
}
975
565
 
976
566
Item *Item::get_tmp_table_item(Session *session)
977
567
{
978
568
  return copy_or_same(session);
979
569
}
980
570
 
981
 
 
982
 
const CHARSET_INFO *Item::default_charset()
 
571
const charset_info_st *Item::default_charset()
983
572
{
984
 
  return current_session->variables.collation_connection;
 
573
  return current_session->variables.getCollation();
985
574
}
986
575
 
987
 
 
988
 
const CHARSET_INFO *Item::compare_collation()
 
576
const charset_info_st *Item::compare_collation()
989
577
{
990
578
  return NULL;
991
579
}
992
580
 
993
 
 
994
581
bool Item::walk(Item_processor processor, bool, unsigned char *arg)
995
582
{
996
583
  return (this->*processor)(arg);
997
584
}
998
585
 
999
 
 
1000
 
Item* Item::compile(Item_analyzer analyzer, unsigned char **arg_p,
1001
 
                    Item_transformer transformer, unsigned char *arg_t)
 
586
Item* Item::compile(Item_analyzer analyzer, 
 
587
                    unsigned char **arg_p,
 
588
                    Item_transformer transformer, 
 
589
                    unsigned char *arg_t)
1002
590
{
1003
591
  if ((this->*analyzer) (arg_p))
1004
592
    return ((this->*transformer) (arg_t));
1005
 
  return 0;
 
593
  return NULL;
1006
594
}
1007
595
 
1008
 
 
1009
596
void Item::traverse_cond(Cond_traverser traverser, void *arg, traverse_order)
1010
597
{
1011
598
  (*traverser)(this, arg);
1012
599
}
1013
600
 
1014
 
 
1015
601
bool Item::remove_dependence_processor(unsigned char *)
1016
602
{
1017
 
  return 0;
1018
 
}
1019
 
 
1020
 
 
1021
 
bool Item::remove_fixed(unsigned char *)
1022
 
{
1023
 
  fixed= 0;
1024
 
  return 0;
1025
 
}
1026
 
 
 
603
  return false;
 
604
}
1027
605
 
1028
606
bool Item::collect_item_field_processor(unsigned char *)
1029
607
{
1030
 
  return 0;
 
608
  return false;
1031
609
}
1032
610
 
1033
 
 
1034
611
bool Item::find_item_in_field_list_processor(unsigned char *)
1035
612
{
1036
 
  return 0;
 
613
  return false;
1037
614
}
1038
615
 
1039
 
 
1040
616
bool Item::change_context_processor(unsigned char *)
1041
617
{
1042
 
  return 0;
1043
 
}
1044
 
 
1045
 
bool Item::reset_query_id_processor(unsigned char *)
1046
 
{
1047
 
  return 0;
1048
 
}
1049
 
 
 
618
  return false;
 
619
}
1050
620
 
1051
621
bool Item::register_field_in_read_map(unsigned char *)
1052
622
{
1053
 
  return 0;
1054
 
}
1055
 
 
1056
 
 
1057
 
bool Item::register_field_in_bitmap(unsigned char *)
1058
 
{
1059
 
  return 0;
1060
 
}
1061
 
 
 
623
  return false;
 
624
}
1062
625
 
1063
626
bool Item::subst_argument_checker(unsigned char **arg)
1064
627
{
1067
630
  return true;
1068
631
}
1069
632
 
1070
 
 
1071
 
bool Item::check_vcol_func_processor(unsigned char *)
1072
 
{
1073
 
  return true;
1074
 
}
1075
 
 
1076
 
 
1077
633
Item *Item::equal_fields_propagator(unsigned char *)
1078
634
{
1079
635
  return this;
1080
636
}
1081
637
 
1082
 
 
1083
638
bool Item::set_no_const_sub(unsigned char *)
1084
639
{
1085
640
  return false;
1086
641
}
1087
642
 
1088
 
 
1089
643
Item *Item::replace_equal_field(unsigned char *)
1090
644
{
1091
645
  return this;
1092
646
}
1093
647
 
1094
 
 
1095
 
Item *Item::this_item(void)
1096
 
{
1097
 
  return this;
1098
 
}
1099
 
 
1100
 
 
1101
 
const Item *Item::this_item(void) const
1102
 
{
1103
 
  return this;
1104
 
}
1105
 
 
1106
 
 
1107
 
Item **Item::this_item_addr(Session *, Item **addr_arg)
1108
 
{
1109
 
  return addr_arg;
1110
 
}
1111
 
 
1112
 
 
1113
648
uint32_t Item::cols()
1114
649
{
1115
650
  return 1;
1116
651
}
1117
652
 
1118
 
 
1119
653
Item* Item::element_index(uint32_t)
1120
654
{
1121
655
  return this;
1122
656
}
1123
657
 
1124
 
 
1125
658
Item** Item::addr(uint32_t)
1126
659
{
1127
 
  return 0;
 
660
  return NULL;
1128
661
}
1129
662
 
1130
 
 
1131
663
bool Item::null_inside()
1132
664
{
1133
 
  return 0;
 
665
  return false;
1134
666
}
1135
667
 
1136
 
 
1137
668
void Item::bring_value()
1138
669
{}
1139
670
 
1140
 
 
1141
 
Item_field *Item::filed_for_view_update()
1142
 
{
1143
 
  return 0;
1144
 
}
1145
 
 
1146
671
Item *Item::neg_transformer(Session *)
1147
672
{
1148
673
  return NULL;
1149
674
}
1150
675
 
1151
 
 
1152
676
Item *Item::update_value_transformer(unsigned char *)
1153
677
{
1154
678
  return this;
1155
679
}
1156
680
 
1157
 
 
1158
681
void Item::delete_self()
1159
682
{
1160
683
  cleanup();
1166
689
  return false;
1167
690
}
1168
691
 
1169
 
 
1170
692
bool Item::is_expensive()
1171
693
{
1172
694
  if (is_expensive_cache < 0)
1173
 
    is_expensive_cache= walk(&Item::is_expensive_processor, 0,
1174
 
                             (unsigned char*)0);
 
695
    is_expensive_cache= walk(&Item::is_expensive_processor, 0, NULL);
1175
696
  return test(is_expensive_cache);
1176
697
}
1177
698
 
1178
 
 
1179
 
int Item::save_in_field_no_warnings(Field *field, bool no_conversions)
1180
 
{
1181
 
  int res;
1182
 
  Table *table= field->table;
1183
 
  Session *session= table->in_use;
1184
 
  enum_check_fields tmp= session->count_cuted_fields;
1185
 
  ulong sql_mode= session->variables.sql_mode;
1186
 
  session->variables.sql_mode&= ~(MODE_NO_ZERO_DATE);
1187
 
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
1188
 
  res= save_in_field(field, no_conversions);
1189
 
  session->count_cuted_fields= tmp;
1190
 
  session->variables.sql_mode= sql_mode;
1191
 
  return res;
1192
 
}
1193
 
 
1194
 
 
1195
699
/*
1196
 
 need a special class to adjust printing : references to aggregate functions 
 
700
 need a special class to adjust printing : references to aggregate functions
1197
701
 must not be printed as refs because the aggregate functions that are added to
1198
702
 the front of select list are not printed as well.
1199
703
*/
1204
708
                  const char *table_name_arg, const char *field_name_arg)
1205
709
    :Item_ref(context_arg, item, table_name_arg, field_name_arg) {}
1206
710
 
1207
 
  virtual inline void print (String *str, enum_query_type query_type)
 
711
  virtual inline void print (String *str)
1208
712
  {
1209
713
    if (ref)
1210
 
      (*ref)->print(str, query_type);
 
714
      (*ref)->print(str);
1211
715
    else
1212
 
      Item_ident::print(str, query_type);
 
716
      Item_ident::print(str);
1213
717
  }
1214
718
};
1215
719
 
1216
 
 
1217
 
/**
1218
 
  Move SUM items out from item tree and replace with reference.
1219
 
 
1220
 
  @param session                        Thread handler
1221
 
  @param ref_pointer_array      Pointer to array of reference fields
1222
 
  @param fields         All fields in select
1223
 
  @param ref                    Pointer to item
1224
 
  @param skip_registered       <=> function be must skipped for registered
1225
 
                               SUM items
1226
 
 
1227
 
  @note
1228
 
    This is from split_sum_func() for items that should be split
1229
 
 
1230
 
    All found SUM items are added FIRST in the fields list and
1231
 
    we replace the item with a reference.
1232
 
 
1233
 
    session->fatal_error() may be called if we are out of memory
1234
 
*/
1235
 
 
1236
720
void Item::split_sum_func(Session *session, Item **ref_pointer_array,
1237
721
                          List<Item> &fields, Item **ref,
1238
722
                          bool skip_registered)
1239
723
{
1240
724
  /* An item of type Item_sum  is registered <=> ref_by != 0 */
1241
 
  if (type() == SUM_FUNC_ITEM && skip_registered &&
 
725
  if (type() == SUM_FUNC_ITEM && 
 
726
      skip_registered &&
1242
727
      ((Item_sum *) this)->ref_by)
1243
728
    return;
 
729
 
1244
730
  if ((type() != SUM_FUNC_ITEM && with_sum_func) ||
1245
731
      (type() == FUNC_ITEM &&
1246
732
       (((Item_func *) this)->functype() == Item_func::ISNOTNULLTEST_FUNC ||
1264
750
      Item_ref to allow fields from view being stored in tmp table.
1265
751
    */
1266
752
    Item_aggregate_ref *item_ref;
1267
 
    uint32_t el= fields.elements;
 
753
    uint32_t el= fields.size();
1268
754
    Item *real_itm= real_item();
1269
755
 
1270
756
    ref_pointer_array[el]= real_itm;
1271
 
    if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
1272
 
                                           ref_pointer_array + el, 0, name)))
1273
 
      return;                                   // fatal_error is set
 
757
    item_ref= new Item_aggregate_ref(&session->lex().current_select->context, ref_pointer_array + el, 0, name);
1274
758
    if (type() == SUM_FUNC_ITEM)
1275
 
      item_ref->depended_from= ((Item_sum *) this)->depended_from(); 
 
759
      item_ref->depended_from= ((Item_sum *) this)->depended_from();
1276
760
    fields.push_front(real_itm);
1277
 
    session->change_item_tree(ref, item_ref);
1278
 
  }
1279
 
}
1280
 
 
1281
 
 
1282
 
void Item_ident_for_show::make_field(Send_field *tmp_field)
1283
 
{
1284
 
  tmp_field->table_name= tmp_field->org_table_name= table_name;
1285
 
  tmp_field->db_name= db_name;
1286
 
  tmp_field->col_name= tmp_field->org_col_name= field->field_name;
1287
 
  tmp_field->charsetnr= field->charset()->number;
1288
 
  tmp_field->length=field->field_length;
1289
 
  tmp_field->type=field->type();
1290
 
  tmp_field->flags= field->table->maybe_null ? 
1291
 
    (field->flags & ~NOT_NULL_FLAG) : field->flags;
1292
 
  tmp_field->decimals= field->decimals();
1293
 
}
1294
 
 
1295
 
 
1296
 
double Item_ident_for_show::val_real()
1297
 
{
1298
 
  return field->val_real();
1299
 
}
1300
 
 
1301
 
 
1302
 
int64_t Item_ident_for_show::val_int()
1303
 
{
1304
 
  return field->val_int();
1305
 
}
1306
 
 
1307
 
 
1308
 
String *Item_ident_for_show::val_str(String *str)
1309
 
{
1310
 
  return field->val_str(str);
1311
 
}
1312
 
 
1313
 
 
1314
 
my_decimal *Item_ident_for_show::val_decimal(my_decimal *dec)
1315
 
{
1316
 
  return field->val_decimal(dec);
1317
 
}
1318
 
 
1319
 
 
1320
 
/**********************************************/
1321
 
 
1322
 
Item_field::Item_field(Field *f)
1323
 
  :Item_ident(0, NULL, *f->table_name, f->field_name),
1324
 
   item_equal(0), no_const_subst(0),
1325
 
   have_privileges(0), any_privileges(0)
1326
 
{
1327
 
  set_field(f);
1328
 
  /*
1329
 
    field_name and table_name should not point to garbage
1330
 
    if this item is to be reused
1331
 
  */
1332
 
  orig_table_name= orig_field_name= "";
1333
 
}
1334
 
 
1335
 
 
1336
 
/**
1337
 
  Constructor used inside setup_wild().
1338
 
 
1339
 
  Ensures that field, table, and database names will live as long as
1340
 
  Item_field (this is important in prepared statements).
1341
 
*/
1342
 
 
1343
 
Item_field::Item_field(Session *, Name_resolution_context *context_arg,
1344
 
                       Field *f)
1345
 
  :Item_ident(context_arg, f->table->s->db.str, *f->table_name, f->field_name),
1346
 
   item_equal(0), no_const_subst(0),
1347
 
   have_privileges(0), any_privileges(0)
1348
 
{
1349
 
  set_field(f);
1350
 
}
1351
 
 
1352
 
 
1353
 
Item_field::Item_field(Name_resolution_context *context_arg,
1354
 
                       const char *db_arg,const char *table_name_arg,
1355
 
                       const char *field_name_arg)
1356
 
  :Item_ident(context_arg, db_arg,table_name_arg,field_name_arg),
1357
 
   field(0), result_field(0), item_equal(0), no_const_subst(0),
1358
 
   have_privileges(0), any_privileges(0)
1359
 
{
1360
 
  SELECT_LEX *select= current_session->lex->current_select;
1361
 
  collation.set(DERIVATION_IMPLICIT);
1362
 
  if (select && select->parsing_place != IN_HAVING)
1363
 
      select->select_n_where_fields++;
1364
 
}
1365
 
 
1366
 
/**
1367
 
  Constructor need to process subselect with temporary tables (see Item)
1368
 
*/
1369
 
 
1370
 
Item_field::Item_field(Session *session, Item_field *item)
1371
 
  :Item_ident(session, item),
1372
 
   field(item->field),
1373
 
   result_field(item->result_field),
1374
 
   item_equal(item->item_equal),
1375
 
   no_const_subst(item->no_const_subst),
1376
 
   have_privileges(item->have_privileges),
1377
 
   any_privileges(item->any_privileges)
1378
 
{
1379
 
  collation.set(DERIVATION_IMPLICIT);
1380
 
}
1381
 
 
1382
 
void Item_field::set_field(Field *field_par)
1383
 
{
1384
 
  field=result_field=field_par;                 // for easy coding with fields
1385
 
  maybe_null=field->maybe_null();
1386
 
  decimals= field->decimals();
1387
 
  max_length= field_par->max_display_length();
1388
 
  table_name= *field_par->table_name;
1389
 
  field_name= field_par->field_name;
1390
 
  db_name= field_par->table->s->db.str;
1391
 
  alias_name_used= field_par->table->alias_name_used;
1392
 
  unsigned_flag=test(field_par->flags & UNSIGNED_FLAG);
1393
 
  collation.set(field_par->charset(), field_par->derivation());
1394
 
  fixed= 1;
1395
 
  if (field->table->s->tmp_table == SYSTEM_TMP_TABLE)
1396
 
    any_privileges= 0;
1397
 
}
1398
 
 
1399
 
 
1400
 
/**
1401
 
  Reset this item to point to a field from the new temporary table.
1402
 
  This is used when we create a new temporary table for each execution
1403
 
  of prepared statement.
1404
 
*/
1405
 
 
1406
 
void Item_field::reset_field(Field *f)
1407
 
{
1408
 
  set_field(f);
1409
 
  /* 'name' is pointing at field->field_name of old field */
1410
 
  name= (char*) f->field_name;
1411
 
}
1412
 
 
1413
 
const char *Item_ident::full_name() const
1414
 
{
1415
 
  char *tmp;
1416
 
  if (!table_name || !field_name)
1417
 
    return field_name ? field_name : name ? name : "tmp_field";
1418
 
  if (db_name && db_name[0])
1419
 
  {
1420
 
    tmp=(char*) sql_alloc((uint) strlen(db_name)+(uint) strlen(table_name)+
1421
 
                          (uint) strlen(field_name)+3);
1422
 
    strxmov(tmp,db_name,".",table_name,".",field_name,NULL);
1423
 
  }
1424
 
  else
1425
 
  {
1426
 
    if (table_name[0])
1427
 
    {
1428
 
      tmp= (char*) sql_alloc((uint) strlen(table_name) +
1429
 
                             (uint) strlen(field_name) + 2);
1430
 
      strxmov(tmp, table_name, ".", field_name, NULL);
1431
 
    }
1432
 
    else
1433
 
      tmp= (char*) field_name;
1434
 
  }
1435
 
  return tmp;
1436
 
}
1437
 
 
1438
 
void Item_ident::print(String *str,
1439
 
                       enum_query_type)
1440
 
{
1441
 
  Session *session= current_session;
1442
 
  char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME];
1443
 
  const char *d_name= db_name, *t_name= table_name;
1444
 
  if (lower_case_table_names== 1 ||
1445
 
      (lower_case_table_names == 2 && !alias_name_used))
1446
 
  {
1447
 
    if (table_name && table_name[0])
1448
 
    {
1449
 
      my_stpcpy(t_name_buff, table_name);
1450
 
      my_casedn_str(files_charset_info, t_name_buff);
1451
 
      t_name= t_name_buff;
1452
 
    }
1453
 
    if (db_name && db_name[0])
1454
 
    {
1455
 
      my_stpcpy(d_name_buff, db_name);
1456
 
      my_casedn_str(files_charset_info, d_name_buff);
1457
 
      d_name= d_name_buff;
1458
 
    }
1459
 
  }
1460
 
 
1461
 
  if (!table_name || !field_name || !field_name[0])
1462
 
  {
1463
 
    const char *nm= (field_name && field_name[0]) ?
1464
 
                      field_name : name ? name : "tmp_field";
1465
 
    append_identifier(session, str, nm, (uint) strlen(nm));
1466
 
    return;
1467
 
  }
1468
 
  if (db_name && db_name[0] && !alias_name_used)
1469
 
  {
1470
 
    {
1471
 
      append_identifier(session, str, d_name, (uint)strlen(d_name));
1472
 
      str->append('.');
1473
 
    }
1474
 
    append_identifier(session, str, t_name, (uint)strlen(t_name));
1475
 
    str->append('.');
1476
 
    append_identifier(session, str, field_name, (uint)strlen(field_name));
1477
 
  }
1478
 
  else
1479
 
  {
1480
 
    if (table_name[0])
1481
 
    {
1482
 
      append_identifier(session, str, t_name, (uint) strlen(t_name));
1483
 
      str->append('.');
1484
 
      append_identifier(session, str, field_name, (uint) strlen(field_name));
1485
 
    }
1486
 
    else
1487
 
      append_identifier(session, str, field_name, (uint) strlen(field_name));
1488
 
  }
1489
 
}
1490
 
 
1491
 
/* ARGSUSED */
1492
 
String *Item_field::val_str(String *str)
1493
 
{
1494
 
  assert(fixed == 1);
1495
 
  if ((null_value=field->is_null()))
1496
 
    return 0;
1497
 
  str->set_charset(str_value.charset());
1498
 
  return field->val_str(str,&str_value);
1499
 
}
1500
 
 
1501
 
 
1502
 
double Item_field::val_real()
1503
 
{
1504
 
  assert(fixed == 1);
1505
 
  if ((null_value=field->is_null()))
1506
 
    return 0.0;
1507
 
  return field->val_real();
1508
 
}
1509
 
 
1510
 
 
1511
 
int64_t Item_field::val_int()
1512
 
{
1513
 
  assert(fixed == 1);
1514
 
  if ((null_value=field->is_null()))
1515
 
    return 0;
1516
 
  return field->val_int();
1517
 
}
1518
 
 
1519
 
 
1520
 
my_decimal *Item_field::val_decimal(my_decimal *decimal_value)
1521
 
{
1522
 
  if ((null_value= field->is_null()))
1523
 
    return 0;
1524
 
  return field->val_decimal(decimal_value);
1525
 
}
1526
 
 
1527
 
 
1528
 
String *Item_field::str_result(String *str)
1529
 
{
1530
 
  if ((null_value=result_field->is_null()))
1531
 
    return 0;
1532
 
  str->set_charset(str_value.charset());
1533
 
  return result_field->val_str(str,&str_value);
1534
 
}
1535
 
 
1536
 
bool Item_field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
1537
 
{
1538
 
  if ((null_value=field->is_null()) || field->get_date(ltime,fuzzydate))
1539
 
  {
1540
 
    memset(ltime, 0, sizeof(*ltime));
1541
 
    return 1;
1542
 
  }
1543
 
  return 0;
1544
 
}
1545
 
 
1546
 
bool Item_field::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
1547
 
{
1548
 
  if ((null_value=result_field->is_null()) ||
1549
 
      result_field->get_date(ltime,fuzzydate))
1550
 
  {
1551
 
    memset(ltime, 0, sizeof(*ltime));
1552
 
    return 1;
1553
 
  }
1554
 
  return 0;
1555
 
}
1556
 
 
1557
 
bool Item_field::get_time(DRIZZLE_TIME *ltime)
1558
 
{
1559
 
  if ((null_value=field->is_null()) || field->get_time(ltime))
1560
 
  {
1561
 
    memset(ltime, 0, sizeof(*ltime));
1562
 
    return 1;
1563
 
  }
1564
 
  return 0;
1565
 
}
1566
 
 
1567
 
double Item_field::val_result()
1568
 
{
1569
 
  if ((null_value=result_field->is_null()))
1570
 
    return 0.0;
1571
 
  return result_field->val_real();
1572
 
}
1573
 
 
1574
 
int64_t Item_field::val_int_result()
1575
 
{
1576
 
  if ((null_value=result_field->is_null()))
1577
 
    return 0;
1578
 
  return result_field->val_int();
1579
 
}
1580
 
 
1581
 
 
1582
 
my_decimal *Item_field::val_decimal_result(my_decimal *decimal_value)
1583
 
{
1584
 
  if ((null_value= result_field->is_null()))
1585
 
    return 0;
1586
 
  return result_field->val_decimal(decimal_value);
1587
 
}
1588
 
 
1589
 
 
1590
 
bool Item_field::val_bool_result()
1591
 
{
1592
 
  if ((null_value= result_field->is_null()))
1593
 
    return false;
1594
 
  switch (result_field->result_type()) {
1595
 
  case INT_RESULT:
1596
 
    return result_field->val_int() != 0;
1597
 
  case DECIMAL_RESULT:
1598
 
  {
1599
 
    my_decimal decimal_value;
1600
 
    my_decimal *val= result_field->val_decimal(&decimal_value);
1601
 
    if (val)
1602
 
      return !my_decimal_is_zero(val);
1603
 
    return 0;
1604
 
  }
1605
 
  case REAL_RESULT:
1606
 
  case STRING_RESULT:
1607
 
    return result_field->val_real() != 0.0;
1608
 
  case ROW_RESULT:
1609
 
  default:
1610
 
    assert(0);
1611
 
    return 0;                                   // Shut up compiler
1612
 
  }
1613
 
}
1614
 
 
1615
 
 
1616
 
bool Item_field::eq(const Item *item, bool) const
1617
 
{
1618
 
  Item *real_item= ((Item *) item)->real_item();
1619
 
  if (real_item->type() != FIELD_ITEM)
1620
 
    return 0;
1621
 
 
1622
 
  Item_field *item_field= (Item_field*) real_item;
1623
 
  if (item_field->field && field)
1624
 
    return item_field->field == field;
1625
 
  /*
1626
 
    We may come here when we are trying to find a function in a GROUP BY
1627
 
    clause from the select list.
1628
 
    In this case the '100 % correct' way to do this would be to first
1629
 
    run fix_fields() on the GROUP BY item and then retry this function, but
1630
 
    I think it's better to relax the checking a bit as we will in
1631
 
    most cases do the correct thing by just checking the field name.
1632
 
    (In cases where we would choose wrong we would have to generate a
1633
 
    ER_NON_UNIQ_ERROR).
1634
 
  */
1635
 
  return (!my_strcasecmp(system_charset_info, item_field->name,
1636
 
                         field_name) &&
1637
 
          (!item_field->table_name || !table_name ||
1638
 
           (!my_strcasecmp(table_alias_charset, item_field->table_name,
1639
 
                           table_name) &&
1640
 
            (!item_field->db_name || !db_name ||
1641
 
             (item_field->db_name && !strcmp(item_field->db_name,
1642
 
                                             db_name))))));
1643
 
}
1644
 
 
1645
 
 
1646
 
table_map Item_field::used_tables() const
1647
 
{
1648
 
  if (field->table->const_table)
1649
 
    return 0;                                   // const item
1650
 
  return (depended_from ? OUTER_REF_TABLE_BIT : field->table->map);
1651
 
}
1652
 
 
1653
 
 
1654
 
enum Item_result Item_field::result_type () const
1655
 
{
1656
 
  return field->result_type();
1657
 
}
1658
 
 
1659
 
 
1660
 
Item_result Item_field::cast_to_int_type() const
1661
 
{
1662
 
  return field->cast_to_int_type();
1663
 
}
1664
 
 
1665
 
 
1666
 
enum_field_types Item_field::field_type() const
1667
 
{
1668
 
  return field->type();
1669
 
}
1670
 
 
1671
 
 
1672
 
void Item_field::fix_after_pullout(st_select_lex *new_parent, Item **)
1673
 
{
1674
 
  if (new_parent == depended_from)
1675
 
    depended_from= NULL;
1676
 
  Name_resolution_context *ctx= new Name_resolution_context();
1677
 
  ctx->outer_context= NULL; // We don't build a complete name resolver
1678
 
  ctx->select_lex= new_parent;
1679
 
  ctx->first_name_resolution_table= context->first_name_resolution_table;
1680
 
  ctx->last_name_resolution_table=  context->last_name_resolution_table;
1681
 
  this->context=ctx;
1682
 
}
1683
 
 
1684
 
 
1685
 
bool Item_field::is_null()
1686
 
{
1687
 
  return field->is_null();
1688
 
}
1689
 
 
1690
 
 
1691
 
Item *Item_field::get_tmp_table_item(Session *session)
1692
 
{
1693
 
  Item_field *new_item= new Item_field(session, this);
1694
 
  if (new_item)
1695
 
    new_item->field= new_item->result_field;
1696
 
  return new_item;
1697
 
}
1698
 
 
1699
 
int64_t Item_field::val_int_endpoint(bool, bool *)
1700
 
{
1701
 
  int64_t res= val_int();
1702
 
  return null_value? INT64_MIN : res;
1703
 
}
1704
 
 
1705
 
/**
1706
 
  Create an item from a string we KNOW points to a valid int64_t
1707
 
  end \\0 terminated number string.
1708
 
  This is always 'signed'. Unsigned values are created with Item_uint()
1709
 
*/
1710
 
 
1711
 
Item_int::Item_int(const char *str_arg, uint32_t length)
1712
 
{
1713
 
  char *end_ptr= (char*) str_arg + length;
1714
 
  int error;
1715
 
  value= my_strtoll10(str_arg, &end_ptr, &error);
1716
 
  max_length= (uint) (end_ptr - str_arg);
1717
 
  name= (char*) str_arg;
1718
 
  fixed= 1;
1719
 
}
1720
 
 
1721
 
 
1722
 
my_decimal *Item_int::val_decimal(my_decimal *decimal_value)
1723
 
{
1724
 
  int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_value);
1725
 
  return decimal_value;
1726
 
}
1727
 
 
1728
 
String *Item_int::val_str(String *str)
1729
 
{
1730
 
  // following assert is redundant, because fixed=1 assigned in constructor
1731
 
  assert(fixed == 1);
1732
 
  str->set(value, &my_charset_bin);
1733
 
  return str;
1734
 
}
1735
 
 
1736
 
void Item_int::print(String *str, enum_query_type)
1737
 
{
1738
 
  // my_charset_bin is good enough for numbers
1739
 
  str_value.set(value, &my_charset_bin);
1740
 
  str->append(str_value);
1741
 
}
1742
 
 
1743
 
 
1744
 
Item_uint::Item_uint(const char *str_arg, uint32_t length):
1745
 
  Item_int(str_arg, length)
1746
 
{
1747
 
  unsigned_flag= 1;
1748
 
}
1749
 
 
1750
 
 
1751
 
Item_uint::Item_uint(const char *str_arg, int64_t i, uint32_t length):
1752
 
  Item_int(str_arg, i, length)
1753
 
{
1754
 
  unsigned_flag= 1;
1755
 
}
1756
 
 
1757
 
 
1758
 
String *Item_uint::val_str(String *str)
1759
 
{
1760
 
  // following assert is redundant, because fixed=1 assigned in constructor
1761
 
  assert(fixed == 1);
1762
 
  str->set((uint64_t) value, &my_charset_bin);
1763
 
  return str;
1764
 
}
1765
 
 
1766
 
 
1767
 
void Item_uint::print(String *str, enum_query_type)
1768
 
{
1769
 
  // latin1 is good enough for numbers
1770
 
  str_value.set((uint64_t) value, default_charset());
1771
 
  str->append(str_value);
1772
 
}
1773
 
 
1774
 
 
1775
 
Item_decimal::Item_decimal(const char *str_arg, uint32_t length,
1776
 
                           const CHARSET_INFO * const charset)
1777
 
{
1778
 
  str2my_decimal(E_DEC_FATAL_ERROR, str_arg, length, charset, &decimal_value);
1779
 
  name= (char*) str_arg;
1780
 
  decimals= (uint8_t) decimal_value.frac;
1781
 
  fixed= 1;
1782
 
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1783
 
                                             decimals, unsigned_flag);
1784
 
}
1785
 
 
1786
 
Item_decimal::Item_decimal(int64_t val, bool unsig)
1787
 
{
1788
 
  int2my_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value);
1789
 
  decimals= (uint8_t) decimal_value.frac;
1790
 
  fixed= 1;
1791
 
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1792
 
                                             decimals, unsigned_flag);
1793
 
}
1794
 
 
1795
 
 
1796
 
Item_decimal::Item_decimal(double val, int, int)
1797
 
{
1798
 
  double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
1799
 
  decimals= (uint8_t) decimal_value.frac;
1800
 
  fixed= 1;
1801
 
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1802
 
                                             decimals, unsigned_flag);
1803
 
}
1804
 
 
1805
 
 
1806
 
Item_decimal::Item_decimal(const char *str, const my_decimal *val_arg,
1807
 
                           uint32_t decimal_par, uint32_t length)
1808
 
{
1809
 
  my_decimal2decimal(val_arg, &decimal_value);
1810
 
  name= (char*) str;
1811
 
  decimals= (uint8_t) decimal_par;
1812
 
  max_length= length;
1813
 
  fixed= 1;
1814
 
}
1815
 
 
1816
 
 
1817
 
Item_decimal::Item_decimal(my_decimal *value_par)
1818
 
{
1819
 
  my_decimal2decimal(value_par, &decimal_value);
1820
 
  decimals= (uint8_t) decimal_value.frac;
1821
 
  fixed= 1;
1822
 
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1823
 
                                             decimals, unsigned_flag);
1824
 
}
1825
 
 
1826
 
 
1827
 
Item_decimal::Item_decimal(const unsigned char *bin, int precision, int scale)
1828
 
{
1829
 
  binary2my_decimal(E_DEC_FATAL_ERROR, bin,
1830
 
                    &decimal_value, precision, scale);
1831
 
  decimals= (uint8_t) decimal_value.frac;
1832
 
  fixed= 1;
1833
 
  max_length= my_decimal_precision_to_length(precision, decimals,
1834
 
                                             unsigned_flag);
1835
 
}
1836
 
 
1837
 
 
1838
 
int64_t Item_decimal::val_int()
1839
 
{
1840
 
  int64_t result;
1841
 
  my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &result);
1842
 
  return result;
1843
 
}
1844
 
 
1845
 
double Item_decimal::val_real()
1846
 
{
1847
 
  double result;
1848
 
  my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
1849
 
  return result;
1850
 
}
1851
 
 
1852
 
String *Item_decimal::val_str(String *result)
1853
 
{
1854
 
  result->set_charset(&my_charset_bin);
1855
 
  my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, result);
1856
 
  return result;
1857
 
}
1858
 
 
1859
 
void Item_decimal::print(String *str, enum_query_type)
1860
 
{
1861
 
  my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, &str_value);
1862
 
  str->append(str_value);
1863
 
}
1864
 
 
1865
 
 
1866
 
bool Item_decimal::eq(const Item *item, bool) const
1867
 
{
1868
 
  if (type() == item->type() && item->basic_const_item())
1869
 
  {
1870
 
    /*
1871
 
      We need to cast off const to call val_decimal(). This should
1872
 
      be OK for a basic constant. Additionally, we can pass 0 as
1873
 
      a true decimal constant will return its internal decimal
1874
 
      storage and ignore the argument.
1875
 
    */
1876
 
    Item *arg= (Item*) item;
1877
 
    my_decimal *value= arg->val_decimal(0);
1878
 
    return !my_decimal_cmp(&decimal_value, value);
1879
 
  }
1880
 
  return 0;
1881
 
}
1882
 
 
1883
 
 
1884
 
void Item_decimal::set_decimal_value(my_decimal *value_par)
1885
 
{
1886
 
  my_decimal2decimal(value_par, &decimal_value);
1887
 
  decimals= (uint8_t) decimal_value.frac;
1888
 
  unsigned_flag= !decimal_value.sign();
1889
 
  max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1890
 
                                             decimals, unsigned_flag);
1891
 
}
1892
 
 
1893
 
 
1894
 
String *Item_float::val_str(String *str)
1895
 
{
1896
 
  // following assert is redundant, because fixed=1 assigned in constructor
1897
 
  assert(fixed == 1);
1898
 
  str->set_real(value,decimals,&my_charset_bin);
1899
 
  return str;
1900
 
}
1901
 
 
1902
 
 
1903
 
int64_t Item_float::val_int()
1904
 
{
1905
 
  assert(fixed == 1);
1906
 
  if (value <= (double) INT64_MIN)
1907
 
  {
1908
 
     return INT64_MIN;
1909
 
  }
1910
 
  else if (value >= (double) (uint64_t) INT64_MAX)
1911
 
  {
1912
 
    return INT64_MAX;
1913
 
  }
1914
 
  return (int64_t) rint(value);
1915
 
}
1916
 
 
1917
 
my_decimal *Item_float::val_decimal(my_decimal *decimal_value)
1918
 
{
1919
 
  // following assert is redundant, because fixed=1 assigned in constructor
1920
 
  assert(fixed == 1);
1921
 
  double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_value);
1922
 
  return (decimal_value);
1923
 
}
1924
 
 
1925
 
 
1926
 
void Item_string::print(String *str, enum_query_type query_type)
1927
 
{
1928
 
  if (query_type == QT_ORDINARY && is_cs_specified())
1929
 
  {
1930
 
    str->append('_');
1931
 
    str->append(collation.collation->csname);
1932
 
  }
1933
 
 
1934
 
  str->append('\'');
1935
 
 
1936
 
  if (query_type == QT_ORDINARY ||
1937
 
      my_charset_same(str_value.charset(), system_charset_info))
1938
 
  {
1939
 
    str_value.print(str);
1940
 
  }
1941
 
  else
1942
 
  {
1943
 
    Session *session= current_session;
1944
 
    LEX_STRING utf8_lex_str;
1945
 
 
1946
 
    session->convert_string(&utf8_lex_str,
1947
 
                        system_charset_info,
1948
 
                        str_value.c_ptr_safe(),
1949
 
                        str_value.length(),
1950
 
                        str_value.charset());
1951
 
 
1952
 
    String utf8_str(utf8_lex_str.str,
1953
 
                    utf8_lex_str.length,
1954
 
                    system_charset_info);
1955
 
 
1956
 
    utf8_str.print(str);
1957
 
  }
1958
 
 
1959
 
  str->append('\'');
1960
 
}
1961
 
 
1962
 
 
1963
 
double Item_string::val_real()
1964
 
{
1965
 
  assert(fixed == 1);
1966
 
  int error;
1967
 
  char *end, *org_end;
1968
 
  double tmp;
1969
 
  const CHARSET_INFO * const cs= str_value.charset();
1970
 
 
1971
 
  org_end= (char*) str_value.ptr() + str_value.length();
1972
 
  tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end,
1973
 
                  &error);
1974
 
  if (error || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
1975
 
  {
1976
 
    /*
1977
 
      We can use str_value.ptr() here as Item_string is gurantee to put an
1978
 
      end \0 here.
1979
 
    */
1980
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1981
 
                        ER_TRUNCATED_WRONG_VALUE,
1982
 
                        ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
1983
 
                        str_value.ptr());
1984
 
  }
1985
 
  return tmp;
1986
 
}
1987
 
 
1988
 
 
1989
 
/**
1990
 
  @todo
1991
 
  Give error if we wanted a signed integer and we got an unsigned one
1992
 
*/
1993
 
int64_t Item_string::val_int()
1994
 
{
1995
 
  assert(fixed == 1);
1996
 
  int err;
1997
 
  int64_t tmp;
1998
 
  char *end= (char*) str_value.ptr()+ str_value.length();
1999
 
  char *org_end= end;
2000
 
  const CHARSET_INFO * const cs= str_value.charset();
2001
 
 
2002
 
  tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err);
2003
 
  /*
2004
 
    TODO: Give error if we wanted a signed integer and we got an unsigned
2005
 
    one
2006
 
  */
2007
 
  if (err > 0 ||
2008
 
      (end != org_end && !check_if_only_end_space(cs, end, org_end)))
2009
 
  {
2010
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2011
 
                        ER_TRUNCATED_WRONG_VALUE,
2012
 
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
2013
 
                        str_value.ptr());
2014
 
  }
2015
 
  return tmp;
2016
 
}
2017
 
 
2018
 
 
2019
 
my_decimal *Item_string::val_decimal(my_decimal *decimal_value)
2020
 
{
2021
 
  return val_decimal_from_string(decimal_value);
2022
 
}
2023
 
 
2024
 
 
2025
 
bool Item_null::eq(const Item *item, bool) const
2026
 
{ return item->type() == type(); }
2027
 
 
2028
 
 
2029
 
double Item_null::val_real()
2030
 
{
2031
 
  // following assert is redundant, because fixed=1 assigned in constructor
2032
 
  assert(fixed == 1);
2033
 
  null_value=1;
2034
 
  return 0.0;
2035
 
}
2036
 
int64_t Item_null::val_int()
2037
 
{
2038
 
  // following assert is redundant, because fixed=1 assigned in constructor
2039
 
  assert(fixed == 1);
2040
 
  null_value=1;
2041
 
  return 0;
2042
 
}
2043
 
/* ARGSUSED */
2044
 
String *Item_null::val_str(String *)
2045
 
{
2046
 
  // following assert is redundant, because fixed=1 assigned in constructor
2047
 
  assert(fixed == 1);
2048
 
  null_value=1;
2049
 
  return 0;
2050
 
}
2051
 
 
2052
 
my_decimal *Item_null::val_decimal(my_decimal *)
2053
 
{
2054
 
  return 0;
2055
 
}
2056
 
 
2057
 
 
2058
 
Item *Item_null::safe_charset_converter(const CHARSET_INFO * const tocs)
2059
 
{
2060
 
  collation.set(tocs);
2061
 
  return this;
2062
 
}
2063
 
 
2064
 
/*********************** Item_param related ******************************/
2065
 
 
2066
 
/** 
2067
 
  Default function of Item_param::set_param_func, so in case
2068
 
  of malformed packet the server won't SIGSEGV.
2069
 
*/
2070
 
 
2071
 
static void
2072
 
default_set_param_func(Item_param *param, unsigned char **, ulong)
2073
 
{
2074
 
  param->set_null();
2075
 
}
2076
 
 
2077
 
 
2078
 
Item_param::Item_param(uint32_t pos_in_query_arg) :
2079
 
  state(NO_VALUE),
2080
 
  item_result_type(STRING_RESULT),
2081
 
  /* Don't pretend to be a literal unless value for this item is set. */
2082
 
  item_type(PARAM_ITEM),
2083
 
  param_type(DRIZZLE_TYPE_VARCHAR),
2084
 
  pos_in_query(pos_in_query_arg),
2085
 
  set_param_func(default_set_param_func),
2086
 
  limit_clause_param(false)
2087
 
{
2088
 
  name= (char*) "?";
2089
 
  /* 
2090
 
    Since we can't say whenever this item can be NULL or cannot be NULL
2091
 
    before mysql_stmt_execute(), so we assuming that it can be NULL until
2092
 
    value is set.
2093
 
  */
2094
 
  maybe_null= 1;
2095
 
  cnvitem= new Item_string("", 0, &my_charset_bin, DERIVATION_COERCIBLE);
2096
 
  cnvstr.set(cnvbuf, sizeof(cnvbuf), &my_charset_bin);
2097
 
}
2098
 
 
2099
 
 
2100
 
void Item_param::set_null()
2101
 
{
2102
 
  /* These are cleared after each execution by reset() method */
2103
 
  null_value= 1;
2104
 
  /* 
2105
 
    Because of NULL and string values we need to set max_length for each new
2106
 
    placeholder value: user can submit NULL for any placeholder type, and 
2107
 
    string length can be different in each execution.
2108
 
  */
2109
 
  max_length= 0;
2110
 
  decimals= 0;
2111
 
  state= NULL_VALUE;
2112
 
  item_type= Item::NULL_ITEM;
2113
 
  return;
2114
 
}
2115
 
 
2116
 
void Item_param::set_int(int64_t i, uint32_t max_length_arg)
2117
 
{
2118
 
  value.integer= (int64_t) i;
2119
 
  state= INT_VALUE;
2120
 
  max_length= max_length_arg;
2121
 
  decimals= 0;
2122
 
  maybe_null= 0;
2123
 
  return;
2124
 
}
2125
 
 
2126
 
void Item_param::set_double(double d)
2127
 
{
2128
 
  value.real= d;
2129
 
  state= REAL_VALUE;
2130
 
  max_length= DBL_DIG + 8;
2131
 
  decimals= NOT_FIXED_DEC;
2132
 
  maybe_null= 0;
2133
 
  return;
2134
 
}
2135
 
 
2136
 
 
2137
 
/**
2138
 
  Set decimal parameter value from string.
2139
 
 
2140
 
  @param str      character string
2141
 
  @param length   string length
2142
 
 
2143
 
  @note
2144
 
    As we use character strings to send decimal values in
2145
 
    binary protocol, we use str2my_decimal to convert it to
2146
 
    internal decimal value.
2147
 
*/
2148
 
 
2149
 
void Item_param::set_decimal(char *str, ulong length)
2150
 
{
2151
 
  char *end;
2152
 
 
2153
 
  end= str+length;
2154
 
  str2my_decimal((uint)E_DEC_FATAL_ERROR, str, &decimal_value, &end);
2155
 
  state= DECIMAL_VALUE;
2156
 
  decimals= decimal_value.frac;
2157
 
  max_length= my_decimal_precision_to_length(decimal_value.precision(),
2158
 
                                             decimals, unsigned_flag);
2159
 
  maybe_null= 0;
2160
 
  return;
2161
 
}
2162
 
 
2163
 
 
2164
 
/**
2165
 
  Set parameter value from DRIZZLE_TIME value.
2166
 
 
2167
 
  @param tm              datetime value to set (time_type is ignored)
2168
 
  @param type            type of datetime value
2169
 
  @param max_length_arg  max length of datetime value as string
2170
 
 
2171
 
  @note
2172
 
    If we value to be stored is not normalized, zero value will be stored
2173
 
    instead and proper warning will be produced. This function relies on
2174
 
    the fact that even wrong value sent over binary protocol fits into
2175
 
    MAX_DATE_STRING_REP_LENGTH buffer.
2176
 
*/
2177
 
void Item_param::set_time(DRIZZLE_TIME *tm,
2178
 
                          enum enum_drizzle_timestamp_type time_type,
2179
 
                          uint32_t max_length_arg)
2180
 
2181
 
  value.time= *tm;
2182
 
  value.time.time_type= time_type;
2183
 
 
2184
 
  if (value.time.year > 9999 || value.time.month > 12 ||
2185
 
      value.time.day > 31 ||
2186
 
      ((time_type != DRIZZLE_TIMESTAMP_TIME) && value.time.hour > 23) ||
2187
 
      value.time.minute > 59 || value.time.second > 59)
2188
 
  {
2189
 
    char buff[MAX_DATE_STRING_REP_LENGTH];
2190
 
    uint32_t length= my_TIME_to_str(&value.time, buff);
2191
 
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2192
 
                                 buff, length, time_type, 0);
2193
 
    set_zero_time(&value.time, DRIZZLE_TIMESTAMP_ERROR);
2194
 
  }
2195
 
 
2196
 
  state= TIME_VALUE;
2197
 
  maybe_null= 0;
2198
 
  max_length= max_length_arg;
2199
 
  decimals= 0;
2200
 
  return;
2201
 
}
2202
 
 
2203
 
 
2204
 
bool Item_param::set_str(const char *str, ulong length)
2205
 
{
2206
 
  /*
2207
 
    Assign string with no conversion: data is converted only after it's
2208
 
    been written to the binary log.
2209
 
  */
2210
 
  uint32_t dummy_errors;
2211
 
  if (str_value.copy(str, length, &my_charset_bin, &my_charset_bin,
2212
 
                     &dummy_errors))
2213
 
    return(true);
2214
 
  state= STRING_VALUE;
2215
 
  max_length= length;
2216
 
  maybe_null= 0;
2217
 
  /* max_length and decimals are set after charset conversion */
2218
 
  /* sic: str may be not null-terminated */
2219
 
  return(false);
2220
 
}
2221
 
 
2222
 
 
2223
 
bool Item_param::set_longdata(const char *str, ulong length)
2224
 
{
2225
 
  /*
2226
 
    If client character set is multibyte, end of long data packet
2227
 
    may hit at the middle of a multibyte character.  Additionally,
2228
 
    if binary log is open we must write long data value to the
2229
 
    binary log in character set of client. This is why we can't
2230
 
    convert long data to connection character set as it comes
2231
 
    (here), and first have to concatenate all pieces together,
2232
 
    write query to the binary log and only then perform conversion.
2233
 
  */
2234
 
  if (str_value.append(str, length, &my_charset_bin))
2235
 
    return(true);
2236
 
  state= LONG_DATA_VALUE;
2237
 
  maybe_null= 0;
2238
 
 
2239
 
  return(false);
2240
 
}
2241
 
 
2242
 
 
2243
 
/**
2244
 
  Set parameter value from user variable value.
2245
 
 
2246
 
  @param session   Current thread
2247
 
  @param entry User variable structure (NULL means use NULL value)
2248
 
 
2249
 
  @retval
2250
 
    0 OK
2251
 
  @retval
2252
 
    1 Out of memory
2253
 
*/
2254
 
 
2255
 
bool Item_param::set_from_user_var(Session *session, const user_var_entry *entry)
2256
 
{
2257
 
  if (entry && entry->value)
2258
 
  {
2259
 
    item_result_type= entry->type;
2260
 
    unsigned_flag= entry->unsigned_flag;
2261
 
    if (limit_clause_param)
2262
 
    {
2263
 
      bool unused;
2264
 
      set_int(entry->val_int(&unused), MY_INT64_NUM_DECIMAL_DIGITS);
2265
 
      item_type= Item::INT_ITEM;
2266
 
      return(!unsigned_flag && value.integer < 0 ? 1 : 0);
2267
 
    }
2268
 
    switch (item_result_type) {
2269
 
    case REAL_RESULT:
2270
 
      set_double(*(double*)entry->value);
2271
 
      item_type= Item::REAL_ITEM;
2272
 
      break;
2273
 
    case INT_RESULT:
2274
 
      set_int(*(int64_t*)entry->value, MY_INT64_NUM_DECIMAL_DIGITS);
2275
 
      item_type= Item::INT_ITEM;
2276
 
      break;
2277
 
    case STRING_RESULT:
2278
 
    {
2279
 
      const CHARSET_INFO * const fromcs= entry->collation.collation;
2280
 
      const CHARSET_INFO * const tocs= session->variables.collation_connection;
2281
 
      uint32_t dummy_offset;
2282
 
 
2283
 
      value.cs_info.character_set_of_placeholder= 
2284
 
        value.cs_info.character_set_client= fromcs;
2285
 
      /*
2286
 
        Setup source and destination character sets so that they
2287
 
        are different only if conversion is necessary: this will
2288
 
        make later checks easier.
2289
 
      */
2290
 
      value.cs_info.final_character_set_of_str_value=
2291
 
        String::needs_conversion(0, fromcs, tocs, &dummy_offset) ?
2292
 
        tocs : fromcs;
2293
 
      /*
2294
 
        Exact value of max_length is not known unless data is converted to
2295
 
        charset of connection, so we have to set it later.
2296
 
      */
2297
 
      item_type= Item::STRING_ITEM;
2298
 
 
2299
 
      if (set_str((const char *)entry->value, entry->length))
2300
 
        return(1);
2301
 
      break;
2302
 
    }
2303
 
    case DECIMAL_RESULT:
2304
 
    {
2305
 
      const my_decimal *ent_value= (const my_decimal *)entry->value;
2306
 
      my_decimal2decimal(ent_value, &decimal_value);
2307
 
      state= DECIMAL_VALUE;
2308
 
      decimals= ent_value->frac;
2309
 
      max_length= my_decimal_precision_to_length(ent_value->precision(),
2310
 
                                                 decimals, unsigned_flag);
2311
 
      item_type= Item::DECIMAL_ITEM;
2312
 
      break;
2313
 
    }
2314
 
    default:
2315
 
      assert(0);
2316
 
      set_null();
2317
 
    }
2318
 
  }
2319
 
  else
2320
 
    set_null();
2321
 
 
2322
 
  return(0);
2323
 
}
2324
 
 
2325
 
/**
2326
 
  Resets parameter after execution.
2327
 
 
2328
 
  @note
2329
 
    We clear null_value here instead of setting it in set_* methods,
2330
 
    because we want more easily handle case for long data.
2331
 
*/
2332
 
 
2333
 
void Item_param::reset()
2334
 
{
2335
 
  /* Shrink string buffer if it's bigger than max possible CHAR column */
2336
 
  if (str_value.alloced_length() > MAX_CHAR_WIDTH)
2337
 
    str_value.free();
2338
 
  else
2339
 
    str_value.length(0);
2340
 
  str_value_ptr.length(0);
2341
 
  /*
2342
 
    We must prevent all charset conversions until data has been written
2343
 
    to the binary log.
2344
 
  */
2345
 
  str_value.set_charset(&my_charset_bin);
2346
 
  collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
2347
 
  state= NO_VALUE;
2348
 
  maybe_null= 1;
2349
 
  null_value= 0;
2350
 
  /*
2351
 
    Don't reset item_type to PARAM_ITEM: it's only needed to guard
2352
 
    us from item optimizations at prepare stage, when item doesn't yet
2353
 
    contain a literal of some kind.
2354
 
    In all other cases when this object is accessed its value is
2355
 
    set (this assumption is guarded by 'state' and
2356
 
    assertS(state != NO_VALUE) in all Item_param::get_*
2357
 
    methods).
2358
 
  */
2359
 
  return;
2360
 
}
2361
 
 
2362
 
 
2363
 
int Item_param::save_in_field(Field *field, bool no_conversions)
2364
 
{
2365
 
  field->set_notnull();
2366
 
 
2367
 
  switch (state) {
2368
 
  case INT_VALUE:
2369
 
    return field->store(value.integer, unsigned_flag);
2370
 
  case REAL_VALUE:
2371
 
    return field->store(value.real);
2372
 
  case DECIMAL_VALUE:
2373
 
    return field->store_decimal(&decimal_value);
2374
 
  case TIME_VALUE:
2375
 
    field->store_time(&value.time, value.time.time_type);
2376
 
    return 0;
2377
 
  case STRING_VALUE:
2378
 
  case LONG_DATA_VALUE:
2379
 
    return field->store(str_value.ptr(), str_value.length(),
2380
 
                        str_value.charset());
2381
 
  case NULL_VALUE:
2382
 
    return set_field_to_null_with_conversions(field, no_conversions);
2383
 
  case NO_VALUE:
2384
 
  default:
2385
 
    assert(0);
2386
 
  }
2387
 
  return 1;
2388
 
}
2389
 
 
2390
 
 
2391
 
bool Item_param::get_time(DRIZZLE_TIME *res)
2392
 
{
2393
 
  if (state == TIME_VALUE)
2394
 
  {
2395
 
    *res= value.time;
2396
 
    return 0;
2397
 
  }
2398
 
  /*
2399
 
    If parameter value isn't supplied assertion will fire in val_str()
2400
 
    which is called from Item::get_time().
2401
 
  */
2402
 
  return Item::get_time(res);
2403
 
}
2404
 
 
2405
 
 
2406
 
bool Item_param::get_date(DRIZZLE_TIME *res, uint32_t fuzzydate)
2407
 
{
2408
 
  if (state == TIME_VALUE)
2409
 
  {
2410
 
    *res= value.time;
2411
 
    return 0;
2412
 
  }
2413
 
  return Item::get_date(res, fuzzydate);
2414
 
}
2415
 
 
2416
 
 
2417
 
double Item_param::val_real()
2418
 
{
2419
 
  switch (state) {
2420
 
  case REAL_VALUE:
2421
 
    return value.real;
2422
 
  case INT_VALUE:
2423
 
    return (double) value.integer;
2424
 
  case DECIMAL_VALUE:
2425
 
  {
2426
 
    double result;
2427
 
    my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
2428
 
    return result;
2429
 
  }
2430
 
  case STRING_VALUE:
2431
 
  case LONG_DATA_VALUE:
2432
 
  {
2433
 
    int dummy_err;
2434
 
    char *end_not_used;
2435
 
    return my_strntod(str_value.charset(), (char*) str_value.ptr(),
2436
 
                      str_value.length(), &end_not_used, &dummy_err);
2437
 
  }
2438
 
  case TIME_VALUE:
2439
 
    /*
2440
 
      This works for example when user says SELECT ?+0.0 and supplies
2441
 
      time value for the placeholder.
2442
 
    */
2443
 
    return uint64_t2double(TIME_to_uint64_t(&value.time));
2444
 
  case NULL_VALUE:
2445
 
    return 0.0;
2446
 
  default:
2447
 
    assert(0);
2448
 
  }
2449
 
  return 0.0;
2450
 
2451
 
 
2452
 
 
2453
 
int64_t Item_param::val_int() 
2454
 
2455
 
  switch (state) {
2456
 
  case REAL_VALUE:
2457
 
    return (int64_t) rint(value.real);
2458
 
  case INT_VALUE:
2459
 
    return value.integer;
2460
 
  case DECIMAL_VALUE:
2461
 
  {
2462
 
    int64_t i;
2463
 
    my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &i);
2464
 
    return i;
2465
 
  }
2466
 
  case STRING_VALUE:
2467
 
  case LONG_DATA_VALUE:
2468
 
    {
2469
 
      int dummy_err;
2470
 
      return my_strntoll(str_value.charset(), str_value.ptr(),
2471
 
                         str_value.length(), 10, (char**) 0, &dummy_err);
2472
 
    }
2473
 
  case TIME_VALUE:
2474
 
    return (int64_t) TIME_to_uint64_t(&value.time);
2475
 
  case NULL_VALUE:
2476
 
    return 0; 
2477
 
  default:
2478
 
    assert(0);
2479
 
  }
2480
 
  return 0;
2481
 
}
2482
 
 
2483
 
 
2484
 
my_decimal *Item_param::val_decimal(my_decimal *dec)
2485
 
{
2486
 
  switch (state) {
2487
 
  case DECIMAL_VALUE:
2488
 
    return &decimal_value;
2489
 
  case REAL_VALUE:
2490
 
    double2my_decimal(E_DEC_FATAL_ERROR, value.real, dec);
2491
 
    return dec;
2492
 
  case INT_VALUE:
2493
 
    int2my_decimal(E_DEC_FATAL_ERROR, value.integer, unsigned_flag, dec);
2494
 
    return dec;
2495
 
  case STRING_VALUE:
2496
 
  case LONG_DATA_VALUE:
2497
 
    string2my_decimal(E_DEC_FATAL_ERROR, &str_value, dec);
2498
 
    return dec;
2499
 
  case TIME_VALUE:
2500
 
  {
2501
 
    int64_t i= (int64_t) TIME_to_uint64_t(&value.time);
2502
 
    int2my_decimal(E_DEC_FATAL_ERROR, i, 0, dec);
2503
 
    return dec;
2504
 
  }
2505
 
  case NULL_VALUE:
2506
 
    return 0; 
2507
 
  default:
2508
 
    assert(0);
2509
 
  }
2510
 
  return 0;
2511
 
}
2512
 
 
2513
 
 
2514
 
String *Item_param::val_str(String* str) 
2515
 
2516
 
  switch (state) {
2517
 
  case STRING_VALUE:
2518
 
  case LONG_DATA_VALUE:
2519
 
    return &str_value_ptr;
2520
 
  case REAL_VALUE:
2521
 
    str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
2522
 
    return str;
2523
 
  case INT_VALUE:
2524
 
    str->set(value.integer, &my_charset_bin);
2525
 
    return str;
2526
 
  case DECIMAL_VALUE:
2527
 
    if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
2528
 
                          0, 0, 0, str) <= 1)
2529
 
      return str;
2530
 
    return NULL;
2531
 
  case TIME_VALUE:
2532
 
  {
2533
 
    if (str->reserve(MAX_DATE_STRING_REP_LENGTH))
2534
 
      break;
2535
 
    str->length((uint) my_TIME_to_str(&value.time, (char*) str->ptr()));
2536
 
    str->set_charset(&my_charset_bin);
2537
 
    return str;
2538
 
  }
2539
 
  case NULL_VALUE:
2540
 
    return NULL; 
2541
 
  default:
2542
 
    assert(0);
2543
 
  }
2544
 
  return str;
2545
 
}
2546
 
 
2547
 
/**
2548
 
  Return Param item values in string format, for generating the dynamic 
2549
 
  query used in update/binary logs.
2550
 
 
2551
 
  @todo
2552
 
    - Change interface and implementation to fill log data in place
2553
 
    and avoid one more memcpy/alloc between str and log string.
2554
 
    - In case of error we need to notify replication
2555
 
    that binary log contains wrong statement 
2556
 
*/
2557
 
 
2558
 
const String *Item_param::query_val_str(String* str) const
2559
 
{
2560
 
  switch (state) {
2561
 
  case INT_VALUE:
2562
 
    str->set_int(value.integer, unsigned_flag, &my_charset_bin);
2563
 
    break;
2564
 
  case REAL_VALUE:
2565
 
    str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
2566
 
    break;
2567
 
  case DECIMAL_VALUE:
2568
 
    if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
2569
 
                          0, 0, 0, str) > 1)
2570
 
      return &my_null_string;
2571
 
    break;
2572
 
  case TIME_VALUE:
2573
 
    {
2574
 
      char *buf, *ptr;
2575
 
      str->length(0);
2576
 
      /*
2577
 
        TODO: in case of error we need to notify replication
2578
 
        that binary log contains wrong statement 
2579
 
      */
2580
 
      if (str->reserve(MAX_DATE_STRING_REP_LENGTH+3))
2581
 
        break; 
2582
 
 
2583
 
      /* Create date string inplace */
2584
 
      buf= str->c_ptr_quick();
2585
 
      ptr= buf;
2586
 
      *ptr++= '\'';
2587
 
      ptr+= (uint) my_TIME_to_str(&value.time, ptr);
2588
 
      *ptr++= '\'';
2589
 
      str->length((uint32_t) (ptr - buf));
2590
 
      break;
2591
 
    }
2592
 
  case STRING_VALUE:
2593
 
  case LONG_DATA_VALUE:
2594
 
    {
2595
 
      str->length(0);
2596
 
      append_query_string(value.cs_info.character_set_client, &str_value, str);
2597
 
      break;
2598
 
    }
2599
 
  case NULL_VALUE:
2600
 
    return &my_null_string;
2601
 
  default:
2602
 
    assert(0);
2603
 
  }
2604
 
  return str;
2605
 
}
2606
 
 
2607
 
 
2608
 
/**
2609
 
  Convert string from client character set to the character set of
2610
 
  connection.
2611
 
*/
2612
 
 
2613
 
bool Item_param::convert_str_value(Session *session)
2614
 
{
2615
 
  bool rc= false;
2616
 
  if (state == STRING_VALUE || state == LONG_DATA_VALUE)
2617
 
  {
2618
 
    /*
2619
 
      Check is so simple because all charsets were set up properly
2620
 
      in setup_one_conversion_function, where typecode of
2621
 
      placeholder was also taken into account: the variables are different
2622
 
      here only if conversion is really necessary.
2623
 
    */
2624
 
    if (value.cs_info.final_character_set_of_str_value !=
2625
 
        value.cs_info.character_set_of_placeholder)
2626
 
    {
2627
 
      rc= session->convert_string(&str_value,
2628
 
                              value.cs_info.character_set_of_placeholder,
2629
 
                              value.cs_info.final_character_set_of_str_value);
2630
 
    }
2631
 
    else
2632
 
      str_value.set_charset(value.cs_info.final_character_set_of_str_value);
2633
 
    /* Here str_value is guaranteed to be in final_character_set_of_str_value */
2634
 
 
2635
 
    max_length= str_value.length();
2636
 
    decimals= 0;
2637
 
    /*
2638
 
      str_value_ptr is returned from val_str(). It must be not alloced
2639
 
      to prevent it's modification by val_str() invoker.
2640
 
    */
2641
 
    str_value_ptr.set(str_value.ptr(), str_value.length(),
2642
 
                      str_value.charset());
2643
 
    /* Synchronize item charset with value charset */
2644
 
    collation.set(str_value.charset(), DERIVATION_COERCIBLE);
2645
 
  }
2646
 
  return rc;
2647
 
}
2648
 
 
2649
 
 
2650
 
bool Item_param::basic_const_item() const
2651
 
{
2652
 
  if (state == NO_VALUE || state == TIME_VALUE)
2653
 
    return false;
2654
 
  return true;
2655
 
}
2656
 
 
2657
 
 
2658
 
Item *
2659
 
Item_param::clone_item()
2660
 
{
2661
 
  /* see comments in the header file */
2662
 
  switch (state) {
2663
 
  case NULL_VALUE:
2664
 
    return new Item_null(name);
2665
 
  case INT_VALUE:
2666
 
    return (unsigned_flag ?
2667
 
            new Item_uint(name, value.integer, max_length) :
2668
 
            new Item_int(name, value.integer, max_length));
2669
 
  case REAL_VALUE:
2670
 
    return new Item_float(name, value.real, decimals, max_length);
2671
 
  case STRING_VALUE:
2672
 
  case LONG_DATA_VALUE:
2673
 
    return new Item_string(name, str_value.c_ptr_quick(), str_value.length(),
2674
 
                           str_value.charset());
2675
 
  case TIME_VALUE:
2676
 
    break;
2677
 
  case NO_VALUE:
2678
 
  default:
2679
 
    assert(0);
2680
 
  };
2681
 
  return 0;
2682
 
}
2683
 
 
2684
 
 
2685
 
bool
2686
 
Item_param::eq(const Item *arg, bool binary_cmp) const
2687
 
{
2688
 
  Item *item;
2689
 
  if (!basic_const_item() || !arg->basic_const_item() || arg->type() != type())
2690
 
    return false;
2691
 
  /*
2692
 
    We need to cast off const to call val_int(). This should be OK for
2693
 
    a basic constant.
2694
 
  */
2695
 
  item= (Item*) arg;
2696
 
 
2697
 
  switch (state) {
2698
 
  case NULL_VALUE:
2699
 
    return true;
2700
 
  case INT_VALUE:
2701
 
    return value.integer == item->val_int() &&
2702
 
           unsigned_flag == item->unsigned_flag;
2703
 
  case REAL_VALUE:
2704
 
    return value.real == item->val_real();
2705
 
  case STRING_VALUE:
2706
 
  case LONG_DATA_VALUE:
2707
 
    if (binary_cmp)
2708
 
      return !stringcmp(&str_value, &item->str_value);
2709
 
    return !sortcmp(&str_value, &item->str_value, collation.collation);
2710
 
  default:
2711
 
    break;
2712
 
  }
2713
 
  return false;
2714
 
}
2715
 
 
2716
 
/* End of Item_param related */
2717
 
 
2718
 
void Item_param::print(String *str, enum_query_type)
2719
 
{
2720
 
  if (state == NO_VALUE)
2721
 
  {
2722
 
    str->append('?');
2723
 
  }
2724
 
  else
2725
 
  {
2726
 
    char buffer[STRING_BUFFER_USUAL_SIZE];
2727
 
    String tmp(buffer, sizeof(buffer), &my_charset_bin);
2728
 
    const String *res;
2729
 
    res= query_val_str(&tmp);
2730
 
    str->append(*res);
2731
 
  }
2732
 
}
2733
 
 
2734
 
 
2735
 
/****************************************************************************
2736
 
  Item_copy_string
2737
 
****************************************************************************/
2738
 
 
2739
 
void Item_copy_string::copy()
2740
 
{
2741
 
  String *res=item->val_str(&str_value);
2742
 
  if (res && res != &str_value)
2743
 
    str_value.copy(*res);
2744
 
  null_value=item->null_value;
2745
 
}
2746
 
 
2747
 
/* ARGSUSED */
2748
 
String *Item_copy_string::val_str(String *)
2749
 
{
2750
 
  // Item_copy_string is used without fix_fields call
2751
 
  if (null_value)
2752
 
    return (String*) 0;
2753
 
  return &str_value;
2754
 
}
2755
 
 
2756
 
 
2757
 
my_decimal *Item_copy_string::val_decimal(my_decimal *decimal_value)
2758
 
{
2759
 
  // Item_copy_string is used without fix_fields call
2760
 
  if (null_value)
2761
 
    return 0;
2762
 
  string2my_decimal(E_DEC_FATAL_ERROR, &str_value, decimal_value);
2763
 
  return (decimal_value);
2764
 
}
2765
 
 
 
761
    *ref= item_ref;
 
762
  }
 
763
}
2766
764
 
2767
765
/*
2768
766
  Functions to convert item to field (for send_fields)
2769
767
*/
2770
 
 
2771
 
/* ARGSUSED */
2772
768
bool Item::fix_fields(Session *, Item **)
2773
769
{
2774
 
 
2775
 
  // We do not check fields which are fixed during construction
2776
 
  assert(fixed == 0 || basic_const_item());
2777
 
  fixed= 1;
 
770
  /* We do not check fields which are fixed during construction */
 
771
  assert(! fixed || basic_const_item());
 
772
  fixed= true;
2778
773
  return false;
2779
774
}
2780
775
 
2781
 
double Item_ref_null_helper::val_real()
2782
 
{
2783
 
  assert(fixed == 1);
2784
 
  double tmp= (*ref)->val_result();
2785
 
  owner->was_null|= null_value= (*ref)->null_value;
2786
 
  return tmp;
2787
 
}
2788
 
 
2789
 
 
2790
 
int64_t Item_ref_null_helper::val_int()
2791
 
{
2792
 
  assert(fixed == 1);
2793
 
  int64_t tmp= (*ref)->val_int_result();
2794
 
  owner->was_null|= null_value= (*ref)->null_value;
2795
 
  return tmp;
2796
 
}
2797
 
 
2798
 
 
2799
 
my_decimal *Item_ref_null_helper::val_decimal(my_decimal *decimal_value)
2800
 
{
2801
 
  assert(fixed == 1);
2802
 
  my_decimal *val= (*ref)->val_decimal_result(decimal_value);
2803
 
  owner->was_null|= null_value= (*ref)->null_value;
2804
 
  return val;
2805
 
}
2806
 
 
2807
 
 
2808
 
bool Item_ref_null_helper::val_bool()
2809
 
{
2810
 
  assert(fixed == 1);
2811
 
  bool val= (*ref)->val_bool_result();
2812
 
  owner->was_null|= null_value= (*ref)->null_value;
2813
 
  return val;
2814
 
}
2815
 
 
2816
 
 
2817
 
String* Item_ref_null_helper::val_str(String* s)
2818
 
{
2819
 
  assert(fixed == 1);
2820
 
  String* tmp= (*ref)->str_result(s);
2821
 
  owner->was_null|= null_value= (*ref)->null_value;
2822
 
  return tmp;
2823
 
}
2824
 
 
2825
 
 
2826
 
bool Item_ref_null_helper::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzydate)
2827
 
{  
2828
 
  return (owner->was_null|= null_value= (*ref)->get_date(ltime, fuzzydate));
2829
 
}
2830
 
 
2831
 
 
2832
 
/**
2833
 
  Mark item and SELECT_LEXs as dependent if item was resolved in
2834
 
  outer SELECT.
2835
 
 
2836
 
  @param session             thread handler
2837
 
  @param last            select from which current item depend
2838
 
  @param current         current select
2839
 
  @param resolved_item   item which was resolved in outer SELECT(for warning)
2840
 
  @param mark_item       item which should be marked (can be differ in case of
2841
 
                         substitution)
2842
 
*/
2843
 
 
2844
 
static void mark_as_dependent(Session *session, SELECT_LEX *last, SELECT_LEX *current,
 
776
void mark_as_dependent(Session *session, Select_Lex *last, Select_Lex *current,
2845
777
                              Item_ident *resolved_item,
2846
778
                              Item_ident *mark_item)
2847
779
{
2849
781
                        resolved_item->db_name : "");
2850
782
  const char *table_name= (resolved_item->table_name ?
2851
783
                           resolved_item->table_name : "");
2852
 
  /* store pointer on SELECT_LEX from which item is dependent */
 
784
  /* store pointer on Select_Lex from which item is dependent */
2853
785
  if (mark_item)
2854
786
    mark_item->depended_from= last;
2855
787
  current->mark_as_dependent(last);
2856
 
  if (session->lex->describe & DESCRIBE_EXTENDED)
 
788
  if (session->lex().describe & DESCRIBE_EXTENDED)
2857
789
  {
2858
790
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
2859
 
    sprintf(warn_buff, ER(ER_WARN_FIELD_RESOLVED),
 
791
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_WARN_FIELD_RESOLVED),
2860
792
            db_name, (db_name[0] ? "." : ""),
2861
793
            table_name, (table_name [0] ? "." : ""),
2862
794
            resolved_item->field_name,
2866
798
  }
2867
799
}
2868
800
 
2869
 
 
2870
 
/**
2871
 
  Mark range of selects and resolved identifier (field/reference)
2872
 
  item as dependent.
2873
 
 
2874
 
  @param session             thread handler
2875
 
  @param last_select     select where resolved_item was resolved
2876
 
  @param current_sel     current select (select where resolved_item was placed)
2877
 
  @param found_field     field which was found during resolving
2878
 
  @param found_item      Item which was found during resolving (if resolved
2879
 
                         identifier belongs to VIEW)
2880
 
  @param resolved_item   Identifier which was resolved
2881
 
 
2882
 
  @note
2883
 
    We have to mark all items between current_sel (including) and
2884
 
    last_select (excluding) as dependend (select before last_select should
2885
 
    be marked with actual table mask used by resolved item, all other with
2886
 
    OUTER_REF_TABLE_BIT) and also write dependence information to Item of
2887
 
    resolved identifier.
2888
 
*/
2889
 
 
2890
801
void mark_select_range_as_dependent(Session *session,
2891
 
                                    SELECT_LEX *last_select,
2892
 
                                    SELECT_LEX *current_sel,
 
802
                                    Select_Lex *last_select,
 
803
                                    Select_Lex *current_sel,
2893
804
                                    Field *found_field, Item *found_item,
2894
805
                                    Item_ident *resolved_item)
2895
806
{
2899
810
    done once when we resolved this field and cached result of
2900
811
    resolving)
2901
812
  */
2902
 
  SELECT_LEX *previous_select= current_sel;
 
813
  Select_Lex *previous_select= current_sel;
2903
814
  for (; previous_select->outer_select() != last_select;
2904
815
       previous_select= previous_select->outer_select())
2905
816
  {
2906
 
    Item_subselect *prev_subselect_item=
2907
 
      previous_select->master_unit()->item;
 
817
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
2908
818
    prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
2909
 
    prev_subselect_item->const_item_cache= 0;
 
819
    prev_subselect_item->const_item_cache= false;
2910
820
  }
2911
821
  {
2912
 
    Item_subselect *prev_subselect_item=
2913
 
      previous_select->master_unit()->item;
 
822
    Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
2914
823
    Item_ident *dependent= resolved_item;
2915
824
    if (found_field == view_ref_found)
2916
825
    {
2917
826
      Item::Type type= found_item->type();
2918
 
      prev_subselect_item->used_tables_cache|=
2919
 
        found_item->used_tables();
 
827
      prev_subselect_item->used_tables_cache|= found_item->used_tables();
2920
828
      dependent= ((type == Item::REF_ITEM || type == Item::FIELD_ITEM) ?
2921
829
                  (Item_ident*) found_item :
2922
830
                  0);
2923
831
    }
2924
832
    else
2925
 
      prev_subselect_item->used_tables_cache|=
2926
 
        found_field->table->map;
2927
 
    prev_subselect_item->const_item_cache= 0;
 
833
      prev_subselect_item->used_tables_cache|= found_field->getTable()->map;
 
834
    prev_subselect_item->const_item_cache= false;
2928
835
    mark_as_dependent(session, last_select, current_sel, resolved_item,
2929
836
                      dependent);
2930
837
  }
2931
838
}
2932
839
 
2933
 
 
2934
840
/**
2935
841
  Search a GROUP BY clause for a field with a certain name.
2936
842
 
2945
851
    - the found item on success
2946
852
    - NULL if find_item is not in group_list
2947
853
*/
2948
 
 
2949
 
static Item** find_field_in_group_list(Item *find_item, order_st *group_list)
 
854
static Item** find_field_in_group_list(Session *session, Item *find_item, Order *group_list)
2950
855
{
2951
856
  const char *db_name;
2952
857
  const char *table_name;
2953
858
  const char *field_name;
2954
 
  order_st      *found_group= NULL;
2955
 
  int         found_match_degree= 0;
 
859
  Order *found_group= NULL;
 
860
  int found_match_degree= 0;
2956
861
  Item_ident *cur_field;
2957
 
  int         cur_match_degree= 0;
2958
 
  char        name_buff[NAME_LEN+1];
 
862
  int cur_match_degree= 0;
 
863
  char name_buff[NAME_LEN+1];
2959
864
 
2960
865
  if (find_item->type() == Item::FIELD_ITEM ||
2961
866
      find_item->type() == Item::REF_ITEM)
2962
867
  {
2963
 
    db_name=    ((Item_ident*) find_item)->db_name;
 
868
    db_name= ((Item_ident*) find_item)->db_name;
2964
869
    table_name= ((Item_ident*) find_item)->table_name;
2965
870
    field_name= ((Item_ident*) find_item)->field_name;
2966
871
  }
2967
872
  else
2968
873
    return NULL;
2969
874
 
2970
 
  if (db_name && lower_case_table_names)
 
875
  if (db_name)
2971
876
  {
2972
877
    /* Convert database to lower case for comparison */
2973
 
    strmake(name_buff, db_name, sizeof(name_buff)-1);
 
878
    strncpy(name_buff, db_name, sizeof(name_buff)-1);
2974
879
    my_casedn_str(files_charset_info, name_buff);
2975
880
    db_name= name_buff;
2976
881
  }
2977
882
 
2978
883
  assert(field_name != 0);
2979
884
 
2980
 
  for (order_st *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
 
885
  for (Order *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
2981
886
  {
2982
887
    if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM)
2983
888
    {
2984
889
      cur_field= (Item_ident*) *cur_group->item;
2985
890
      cur_match_degree= 0;
2986
 
      
 
891
 
2987
892
      assert(cur_field->field_name != 0);
2988
893
 
2989
 
      if (!my_strcasecmp(system_charset_info,
2990
 
                         cur_field->field_name, field_name))
 
894
      if (! my_strcasecmp(system_charset_info, cur_field->field_name, field_name))
2991
895
        ++cur_match_degree;
2992
896
      else
2993
897
        continue;
3003
907
        if (cur_field->db_name && db_name)
3004
908
        {
3005
909
          /* If field_name is also qualified by a database name. */
3006
 
          if (strcmp(cur_field->db_name, db_name))
 
910
          if (my_strcasecmp(system_charset_info, cur_field->db_name, db_name))
 
911
          {
3007
912
            /* Same field names, different databases. */
3008
913
            return NULL;
 
914
          }
3009
915
          ++cur_match_degree;
3010
916
        }
3011
917
      }
3015
921
        found_match_degree= cur_match_degree;
3016
922
        found_group= cur_group;
3017
923
      }
3018
 
      else if (found_group && (cur_match_degree == found_match_degree) &&
 
924
      else if (found_group &&
 
925
               (cur_match_degree == found_match_degree) &&
3019
926
               ! (*(found_group->item))->eq(cur_field, 0))
3020
927
      {
3021
928
        /*
3023
930
          best match, they must reference the same column, otherwise the field
3024
931
          is ambiguous.
3025
932
        */
3026
 
        my_error(ER_NON_UNIQ_ERROR, MYF(0),
3027
 
                 find_item->full_name(), current_session->where);
 
933
        my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), session->where());
3028
934
        return NULL;
3029
935
      }
3030
936
    }
3032
938
 
3033
939
  if (found_group)
3034
940
    return found_group->item;
3035
 
  else
3036
 
    return NULL;
 
941
 
 
942
  return NULL;
3037
943
}
3038
944
 
3039
 
 
3040
 
/**
3041
 
  Resolve a column reference in a sub-select.
3042
 
 
3043
 
  Resolve a column reference (usually inside a HAVING clause) against the
3044
 
  SELECT and GROUP BY clauses of the query described by 'select'. The name
3045
 
  resolution algorithm searches both the SELECT and GROUP BY clauses, and in
3046
 
  case of a name conflict prefers GROUP BY column names over SELECT names. If
3047
 
  both clauses contain different fields with the same names, a warning is
3048
 
  issued that name of 'ref' is ambiguous. We extend ANSI SQL in that when no
3049
 
  GROUP BY column is found, then a HAVING name is resolved as a possibly
3050
 
  derived SELECT column. This extension is allowed only if the
3051
 
  MODE_ONLY_FULL_GROUP_BY sql mode isn't enabled.
3052
 
 
3053
 
  @param session     current thread
3054
 
  @param ref     column reference being resolved
3055
 
  @param select  the select that ref is resolved against
3056
 
 
3057
 
  @note
3058
 
    The resolution procedure is:
3059
 
    - Search for a column or derived column named col_ref_i [in table T_j]
3060
 
    in the SELECT clause of Q.
3061
 
    - Search for a column named col_ref_i [in table T_j]
3062
 
    in the GROUP BY clause of Q.
3063
 
    - If found different columns with the same name in GROUP BY and SELECT
3064
 
    - issue a warning and return the GROUP BY column,
3065
 
    - otherwise
3066
 
    - if the MODE_ONLY_FULL_GROUP_BY mode is enabled return error
3067
 
    - else return the found SELECT column.
3068
 
 
3069
 
 
3070
 
  @return
3071
 
    - NULL - there was an error, and the error was already reported
3072
 
    - not_found_item - the item was not resolved, no error was reported
3073
 
    - resolved item - if the item was resolved
3074
 
*/
3075
 
 
3076
 
static Item**
3077
 
resolve_ref_in_select_and_group(Session *session, Item_ident *ref, SELECT_LEX *select)
 
945
Item** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
3078
946
{
3079
947
  Item **group_by_ref= NULL;
3080
948
  Item **select_ref= NULL;
3081
 
  order_st *group_list= (order_st*) select->group_list.first;
 
949
  Order *group_list= (Order*) select->group_list.first;
3082
950
  bool ambiguous_fields= false;
3083
951
  uint32_t counter;
3084
952
  enum_resolution_type resolution;
3087
955
    Search for a column or derived column named as 'ref' in the SELECT
3088
956
    clause of the current select.
3089
957
  */
3090
 
  if (!(select_ref= find_item_in_list(ref, *(select->get_item_list()),
 
958
  if (!(select_ref= find_item_in_list(session,
 
959
                                      ref, *(select->get_item_list()),
3091
960
                                      &counter, REPORT_EXCEPT_NOT_FOUND,
3092
961
                                      &resolution)))
3093
962
    return NULL; /* Some error occurred. */
3097
966
  /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
3098
967
  if (select->having_fix_field && !ref->with_sum_func && group_list)
3099
968
  {
3100
 
    group_by_ref= find_field_in_group_list(ref, group_list);
3101
 
    
 
969
    group_by_ref= find_field_in_group_list(session, ref, group_list);
 
970
 
3102
971
    /* Check if the fields found in SELECT and GROUP BY are the same field. */
3103
972
    if (group_by_ref && (select_ref != not_found_item) &&
3104
973
        !((*group_by_ref)->eq(*select_ref, 0)))
3106
975
      ambiguous_fields= true;
3107
976
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
3108
977
                          ER(ER_NON_UNIQ_ERROR), ref->full_name(),
3109
 
                          current_session->where);
 
978
                          session->where());
3110
979
 
3111
980
    }
3112
981
  }
3134
1003
  return (Item**) not_found_item;
3135
1004
}
3136
1005
 
3137
 
 
3138
 
/**
3139
 
  Resolve the name of an outer select column reference.
3140
 
 
3141
 
  The method resolves the column reference represented by 'this' as a column
3142
 
  present in outer selects that contain current select.
3143
 
 
3144
 
  In prepared statements, because of cache, find_field_in_tables()
3145
 
  can resolve fields even if they don't belong to current context.
3146
 
  In this case this method only finds appropriate context and marks
3147
 
  current select as dependent. The found reference of field should be
3148
 
  provided in 'from_field'.
3149
 
 
3150
 
  @param[in] session             current thread
3151
 
  @param[in,out] from_field  found field reference or (Field*)not_found_field
3152
 
  @param[in,out] reference   view column if this item was resolved to a
3153
 
    view column
3154
 
 
3155
 
  @note
3156
 
    This is the inner loop of Item_field::fix_fields:
3157
 
  @code
3158
 
        for each outer query Q_k beginning from the inner-most one
3159
 
        {
3160
 
          search for a column or derived column named col_ref_i
3161
 
          [in table T_j] in the FROM clause of Q_k;
3162
 
 
3163
 
          if such a column is not found
3164
 
            Search for a column or derived column named col_ref_i
3165
 
            [in table T_j] in the SELECT and GROUP clauses of Q_k.
3166
 
        }
3167
 
  @endcode
3168
 
 
3169
 
  @retval
3170
 
    1   column succefully resolved and fix_fields() should continue.
3171
 
  @retval
3172
 
    0   column fully fixed and fix_fields() should return false
3173
 
  @retval
3174
 
    -1  error occured
3175
 
*/
3176
 
 
3177
 
int
3178
 
Item_field::fix_outer_field(Session *session, Field **from_field, Item **reference)
3179
 
{
3180
 
  enum_parsing_place place= NO_MATTER;
3181
 
  bool field_found= (*from_field != not_found_field);
3182
 
  bool upward_lookup= false;
3183
 
 
3184
 
  /*
3185
 
    If there are outer contexts (outer selects, but current select is
3186
 
    not derived table or view) try to resolve this reference in the
3187
 
    outer contexts.
3188
 
 
3189
 
    We treat each subselect as a separate namespace, so that different
3190
 
    subselects may contain columns with the same names. The subselects
3191
 
    are searched starting from the innermost.
3192
 
  */
3193
 
  Name_resolution_context *last_checked_context= context;
3194
 
  Item **ref= (Item **) not_found_item;
3195
 
  SELECT_LEX *current_sel= (SELECT_LEX *) session->lex->current_select;
3196
 
  Name_resolution_context *outer_context= 0;
3197
 
  SELECT_LEX *select= 0;
3198
 
  /* Currently derived tables cannot be correlated */
3199
 
  if (current_sel->master_unit()->first_select()->linkage !=
3200
 
      DERIVED_TABLE_TYPE)
3201
 
    outer_context= context->outer_context;
3202
 
  for (;
3203
 
       outer_context;
3204
 
       outer_context= outer_context->outer_context)
3205
 
  {
3206
 
    select= outer_context->select_lex;
3207
 
    Item_subselect *prev_subselect_item=
3208
 
      last_checked_context->select_lex->master_unit()->item;
3209
 
    last_checked_context= outer_context;
3210
 
    upward_lookup= true;
3211
 
 
3212
 
    place= prev_subselect_item->parsing_place;
3213
 
    /*
3214
 
      If outer_field is set, field was already found by first call
3215
 
      to find_field_in_tables(). Only need to find appropriate context.
3216
 
    */
3217
 
    if (field_found && outer_context->select_lex !=
3218
 
        cached_table->select_lex)
3219
 
      continue;
3220
 
    /*
3221
 
      In case of a view, find_field_in_tables() writes the pointer to
3222
 
      the found view field into '*reference', in other words, it
3223
 
      substitutes this Item_field with the found expression.
3224
 
    */
3225
 
    if (field_found || (*from_field= find_field_in_tables(session, this,
3226
 
                                          outer_context->
3227
 
                                            first_name_resolution_table,
3228
 
                                          outer_context->
3229
 
                                            last_name_resolution_table,
3230
 
                                          reference,
3231
 
                                          IGNORE_EXCEPT_NON_UNIQUE,
3232
 
                                          true, true)) !=
3233
 
        not_found_field)
3234
 
    {
3235
 
      if (*from_field)
3236
 
      {
3237
 
        if (*from_field != view_ref_found)
3238
 
        {
3239
 
          prev_subselect_item->used_tables_cache|= (*from_field)->table->map;
3240
 
          prev_subselect_item->const_item_cache= 0;
3241
 
          set_field(*from_field);
3242
 
          if (!last_checked_context->select_lex->having_fix_field &&
3243
 
              select->group_list.elements &&
3244
 
              (place == SELECT_LIST || place == IN_HAVING))
3245
 
          {
3246
 
            Item_outer_ref *rf;
3247
 
            /*
3248
 
              If an outer field is resolved in a grouping select then it
3249
 
              is replaced for an Item_outer_ref object. Otherwise an
3250
 
              Item_field object is used.
3251
 
              The new Item_outer_ref object is saved in the inner_refs_list of
3252
 
              the outer select. Here it is only created. It can be fixed only
3253
 
              after the original field has been fixed and this is done in the
3254
 
              fix_inner_refs() function.
3255
 
            */
3256
 
            ;
3257
 
            if (!(rf= new Item_outer_ref(context, this)))
3258
 
              return -1;
3259
 
            session->change_item_tree(reference, rf);
3260
 
            select->inner_refs_list.push_back(rf);
3261
 
            rf->in_sum_func= session->lex->in_sum_func;
3262
 
          }
3263
 
          /*
3264
 
            A reference is resolved to a nest level that's outer or the same as
3265
 
            the nest level of the enclosing set function : adjust the value of
3266
 
            max_arg_level for the function if it's needed.
3267
 
          */
3268
 
          if (session->lex->in_sum_func &&
3269
 
              session->lex->in_sum_func->nest_level >= select->nest_level)
3270
 
          {
3271
 
            Item::Type ref_type= (*reference)->type();
3272
 
            set_if_bigger(session->lex->in_sum_func->max_arg_level,
3273
 
                          select->nest_level);
3274
 
            set_field(*from_field);
3275
 
            fixed= 1;
3276
 
            mark_as_dependent(session, last_checked_context->select_lex,
3277
 
                              context->select_lex, this,
3278
 
                              ((ref_type == REF_ITEM ||
3279
 
                                ref_type == FIELD_ITEM) ?
3280
 
                               (Item_ident*) (*reference) : 0));
3281
 
            return 0;
3282
 
          }
3283
 
        }
3284
 
        else
3285
 
        {
3286
 
          Item::Type ref_type= (*reference)->type();
3287
 
          prev_subselect_item->used_tables_cache|=
3288
 
            (*reference)->used_tables();
3289
 
          prev_subselect_item->const_item_cache&=
3290
 
            (*reference)->const_item();
3291
 
          mark_as_dependent(session, last_checked_context->select_lex,
3292
 
                            context->select_lex, this,
3293
 
                            ((ref_type == REF_ITEM || ref_type == FIELD_ITEM) ?
3294
 
                             (Item_ident*) (*reference) :
3295
 
                             0));
3296
 
          /*
3297
 
            A reference to a view field had been found and we
3298
 
            substituted it instead of this Item (find_field_in_tables
3299
 
            does it by assigning the new value to *reference), so now
3300
 
            we can return from this function.
3301
 
          */
3302
 
          return 0;
3303
 
        }
3304
 
      }
3305
 
      break;
3306
 
    }
3307
 
 
3308
 
    /* Search in SELECT and GROUP lists of the outer select. */
3309
 
    if (place != IN_WHERE && place != IN_ON)
3310
 
    {
3311
 
      if (!(ref= resolve_ref_in_select_and_group(session, this, select)))
3312
 
        return -1; /* Some error occurred (e.g. ambiguous names). */
3313
 
      if (ref != not_found_item)
3314
 
      {
3315
 
        assert(*ref && (*ref)->fixed);
3316
 
        prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
3317
 
        prev_subselect_item->const_item_cache&= (*ref)->const_item();
3318
 
        break;
3319
 
      }
3320
 
    }
3321
 
 
3322
 
    /*
3323
 
      Reference is not found in this select => this subquery depend on
3324
 
      outer select (or we just trying to find wrong identifier, in this
3325
 
      case it does not matter which used tables bits we set)
3326
 
    */
3327
 
    prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
3328
 
    prev_subselect_item->const_item_cache= 0;
3329
 
  }
3330
 
 
3331
 
  assert(ref != 0);
3332
 
  if (!*from_field)
3333
 
    return -1;
3334
 
  if (ref == not_found_item && *from_field == not_found_field)
3335
 
  {
3336
 
    if (upward_lookup)
3337
 
    {
3338
 
      // We can't say exactly what absent table or field
3339
 
      my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), session->where);
3340
 
    }
3341
 
    else
3342
 
    {
3343
 
      /* Call find_field_in_tables only to report the error */
3344
 
      find_field_in_tables(session, this,
3345
 
                           context->first_name_resolution_table,
3346
 
                           context->last_name_resolution_table,
3347
 
                           reference, REPORT_ALL_ERRORS,
3348
 
                           !any_privileges &&
3349
 
                           true, true);
3350
 
    }
3351
 
    return -1;
3352
 
  }
3353
 
  else if (ref != not_found_item)
3354
 
  {
3355
 
    Item *save;
3356
 
    Item_ref *rf;
3357
 
 
3358
 
    /* Should have been checked in resolve_ref_in_select_and_group(). */
3359
 
    assert(*ref && (*ref)->fixed);
3360
 
    /*
3361
 
      Here, a subset of actions performed by Item_ref::set_properties
3362
 
      is not enough. So we pass ptr to NULL into Item_[direct]_ref
3363
 
      constructor, so no initialization is performed, and call 
3364
 
      fix_fields() below.
3365
 
    */
3366
 
    save= *ref;
3367
 
    *ref= NULL;                             // Don't call set_properties()
3368
 
    rf= (place == IN_HAVING ?
3369
 
         new Item_ref(context, ref, (char*) table_name,
3370
 
                      (char*) field_name, alias_name_used) :
3371
 
         (!select->group_list.elements ?
3372
 
         new Item_direct_ref(context, ref, (char*) table_name,
3373
 
                             (char*) field_name, alias_name_used) :
3374
 
         new Item_outer_ref(context, ref, (char*) table_name,
3375
 
                            (char*) field_name, alias_name_used)));
3376
 
    *ref= save;
3377
 
    if (!rf)
3378
 
      return -1;
3379
 
 
3380
 
    if (place != IN_HAVING && select->group_list.elements)
3381
 
    {
3382
 
      outer_context->select_lex->inner_refs_list.push_back((Item_outer_ref*)rf);
3383
 
      ((Item_outer_ref*)rf)->in_sum_func= session->lex->in_sum_func;
3384
 
    }
3385
 
    session->change_item_tree(reference, rf);
3386
 
    /*
3387
 
      rf is Item_ref => never substitute other items (in this case)
3388
 
      during fix_fields() => we can use rf after fix_fields()
3389
 
    */
3390
 
    assert(!rf->fixed);                // Assured by Item_ref()
3391
 
    if (rf->fix_fields(session, reference) || rf->check_cols(1))
3392
 
      return -1;
3393
 
 
3394
 
    mark_as_dependent(session, last_checked_context->select_lex,
3395
 
                      context->select_lex, this,
3396
 
                      rf);
3397
 
    return 0;
3398
 
  }
3399
 
  else
3400
 
  {
3401
 
    mark_as_dependent(session, last_checked_context->select_lex,
3402
 
                      context->select_lex,
3403
 
                      this, (Item_ident*)*reference);
3404
 
    if (last_checked_context->select_lex->having_fix_field)
3405
 
    {
3406
 
      Item_ref *rf;
3407
 
      rf= new Item_ref(context,
3408
 
                       (cached_table->db[0] ? cached_table->db : 0),
3409
 
                       (char*) cached_table->alias, (char*) field_name);
3410
 
      if (!rf)
3411
 
        return -1;
3412
 
      session->change_item_tree(reference, rf);
3413
 
      /*
3414
 
        rf is Item_ref => never substitute other items (in this case)
3415
 
        during fix_fields() => we can use rf after fix_fields()
3416
 
      */
3417
 
      assert(!rf->fixed);                // Assured by Item_ref()
3418
 
      if (rf->fix_fields(session, reference) || rf->check_cols(1))
3419
 
        return -1;
3420
 
      return 0;
3421
 
    }
3422
 
  }
3423
 
  return 1;
3424
 
}
3425
 
 
3426
 
 
3427
 
/**
3428
 
  Resolve the name of a column reference.
3429
 
 
3430
 
  The method resolves the column reference represented by 'this' as a column
3431
 
  present in one of: FROM clause, SELECT clause, GROUP BY clause of a query
3432
 
  Q, or in outer queries that contain Q.
3433
 
 
3434
 
  The name resolution algorithm used is (where [T_j] is an optional table
3435
 
  name that qualifies the column name):
3436
 
 
3437
 
  @code
3438
 
    resolve_column_reference([T_j].col_ref_i)
3439
 
    {
3440
 
      search for a column or derived column named col_ref_i
3441
 
      [in table T_j] in the FROM clause of Q;
3442
 
 
3443
 
      if such a column is NOT found AND    // Lookup in outer queries.
3444
 
         there are outer queries
3445
 
      {
3446
 
        for each outer query Q_k beginning from the inner-most one
3447
 
        {
3448
 
          search for a column or derived column named col_ref_i
3449
 
          [in table T_j] in the FROM clause of Q_k;
3450
 
 
3451
 
          if such a column is not found
3452
 
            Search for a column or derived column named col_ref_i
3453
 
            [in table T_j] in the SELECT and GROUP clauses of Q_k.
3454
 
        }
3455
 
      }
3456
 
    }
3457
 
  @endcode
3458
 
 
3459
 
    Notice that compared to Item_ref::fix_fields, here we first search the FROM
3460
 
    clause, and then we search the SELECT and GROUP BY clauses.
3461
 
 
3462
 
  @param[in]     session        current thread
3463
 
  @param[in,out] reference  view column if this item was resolved to a
3464
 
    view column
3465
 
 
3466
 
  @retval
3467
 
    true  if error
3468
 
  @retval
3469
 
    false on success
3470
 
*/
3471
 
 
3472
 
bool Item_field::fix_fields(Session *session, Item **reference)
3473
 
{
3474
 
  assert(fixed == 0);
3475
 
  Field *from_field= (Field *)not_found_field;
3476
 
  bool outer_fixed= false;
3477
 
 
3478
 
  if (!field)                                   // If field is not checked
3479
 
  {
3480
 
    /*
3481
 
      In case of view, find_field_in_tables() write pointer to view field
3482
 
      expression to 'reference', i.e. it substitute that expression instead
3483
 
      of this Item_field
3484
 
    */
3485
 
    if ((from_field= find_field_in_tables(session, this,
3486
 
                                          context->first_name_resolution_table,
3487
 
                                          context->last_name_resolution_table,
3488
 
                                          reference,
3489
 
                                          session->lex->use_only_table_context ?
3490
 
                                            REPORT_ALL_ERRORS : 
3491
 
                                            IGNORE_EXCEPT_NON_UNIQUE,
3492
 
                                          !any_privileges,
3493
 
                                          true)) ==
3494
 
        not_found_field)
3495
 
    {
3496
 
      int ret;
3497
 
      /* Look up in current select's item_list to find aliased fields */
3498
 
      if (session->lex->current_select->is_item_list_lookup)
3499
 
      {
3500
 
        uint32_t counter;
3501
 
        enum_resolution_type resolution;
3502
 
        Item** res= find_item_in_list(this, session->lex->current_select->item_list,
3503
 
                                      &counter, REPORT_EXCEPT_NOT_FOUND,
3504
 
                                      &resolution);
3505
 
        if (!res)
3506
 
          return 1;
3507
 
        if (resolution == RESOLVED_AGAINST_ALIAS)
3508
 
          alias_name_used= true;
3509
 
        if (res != (Item **)not_found_item)
3510
 
        {
3511
 
          if ((*res)->type() == Item::FIELD_ITEM)
3512
 
          {
3513
 
            /*
3514
 
              It's an Item_field referencing another Item_field in the select
3515
 
              list.
3516
 
              Use the field from the Item_field in the select list and leave
3517
 
              the Item_field instance in place.
3518
 
            */
3519
 
 
3520
 
            Field *new_field= (*((Item_field**)res))->field;
3521
 
 
3522
 
            if (new_field == NULL)
3523
 
            {
3524
 
              /* The column to which we link isn't valid. */
3525
 
              my_error(ER_BAD_FIELD_ERROR, MYF(0), (*res)->name, 
3526
 
                       current_session->where);
3527
 
              return(1);
3528
 
            }
3529
 
 
3530
 
            set_field(new_field);
3531
 
            return 0;
3532
 
          }
3533
 
          else
3534
 
          {
3535
 
            /*
3536
 
              It's not an Item_field in the select list so we must make a new
3537
 
              Item_ref to point to the Item in the select list and replace the
3538
 
              Item_field created by the parser with the new Item_ref.
3539
 
            */
3540
 
            Item_ref *rf= new Item_ref(context, db_name,table_name,field_name);
3541
 
            if (!rf)
3542
 
              return 1;
3543
 
            session->change_item_tree(reference, rf);
3544
 
            /*
3545
 
              Because Item_ref never substitutes itself with other items 
3546
 
              in Item_ref::fix_fields(), we can safely use the original 
3547
 
              pointer to it even after fix_fields()
3548
 
             */
3549
 
            return rf->fix_fields(session, reference) ||  rf->check_cols(1);
3550
 
          }
3551
 
        }
3552
 
      }
3553
 
      if ((ret= fix_outer_field(session, &from_field, reference)) < 0)
3554
 
        goto error;
3555
 
      outer_fixed= true;
3556
 
      if (!ret)
3557
 
        goto mark_non_agg_field;
3558
 
    }
3559
 
    else if (!from_field)
3560
 
      goto error;
3561
 
 
3562
 
    if (!outer_fixed && cached_table && cached_table->select_lex &&
3563
 
        context->select_lex &&
3564
 
        cached_table->select_lex != context->select_lex)
3565
 
    {
3566
 
      int ret;
3567
 
      if ((ret= fix_outer_field(session, &from_field, reference)) < 0)
3568
 
        goto error;
3569
 
      outer_fixed= 1;
3570
 
      if (!ret)
3571
 
        goto mark_non_agg_field;
3572
 
    }
3573
 
 
3574
 
    /*
3575
 
      if it is not expression from merged VIEW we will set this field.
3576
 
 
3577
 
      We can leave expression substituted from view for next PS/SP rexecution
3578
 
      (i.e. do not register this substitution for reverting on cleanup()
3579
 
      (register_item_tree_changing())), because this subtree will be
3580
 
      fix_field'ed during setup_tables()->setup_underlying() (i.e. before
3581
 
      all other expressions of query, and references on tables which do
3582
 
      not present in query will not make problems.
3583
 
 
3584
 
      Also we suppose that view can't be changed during PS/SP life.
3585
 
    */
3586
 
    if (from_field == view_ref_found)
3587
 
      return false;
3588
 
 
3589
 
    set_field(from_field);
3590
 
    if (session->lex->in_sum_func &&
3591
 
        session->lex->in_sum_func->nest_level == 
3592
 
        session->lex->current_select->nest_level)
3593
 
      set_if_bigger(session->lex->in_sum_func->max_arg_level,
3594
 
                    session->lex->current_select->nest_level);
3595
 
  }
3596
 
  else if (session->mark_used_columns != MARK_COLUMNS_NONE)
3597
 
  {
3598
 
    Table *table= field->table;
3599
 
    MY_BITMAP *current_bitmap, *other_bitmap;
3600
 
    if (session->mark_used_columns == MARK_COLUMNS_READ)
3601
 
    {
3602
 
      current_bitmap= table->read_set;
3603
 
      other_bitmap=   table->write_set;
3604
 
    }
3605
 
    else
3606
 
    {
3607
 
      current_bitmap= table->write_set;
3608
 
      other_bitmap=   table->read_set;
3609
 
    }
3610
 
    if (!bitmap_fast_test_and_set(current_bitmap, field->field_index))
3611
 
    {
3612
 
      if (!bitmap_is_set(other_bitmap, field->field_index))
3613
 
      {
3614
 
        /* First usage of column */
3615
 
        table->used_fields++;                     // Used to optimize loops
3616
 
        /* purecov: begin inspected */
3617
 
        table->covering_keys.intersect(field->part_of_key);
3618
 
        /* purecov: end */
3619
 
      }
3620
 
    }
3621
 
  }
3622
 
  fixed= 1;
3623
 
mark_non_agg_field:
3624
 
  return false;
3625
 
 
3626
 
error:
3627
 
  context->process_error(session);
3628
 
  return true;
3629
 
}
3630
 
 
3631
 
Item *Item_field::safe_charset_converter(const CHARSET_INFO * const tocs)
3632
 
{
3633
 
  no_const_subst= 1;
3634
 
  return Item::safe_charset_converter(tocs);
3635
 
}
3636
 
 
3637
 
 
3638
 
void Item_field::cleanup()
3639
 
{
3640
 
  Item_ident::cleanup();
3641
 
  /*
3642
 
    Even if this object was created by direct link to field in setup_wild()
3643
 
    it will be linked correctly next time by name of field and table alias.
3644
 
    I.e. we can drop 'field'.
3645
 
   */
3646
 
  field= result_field= 0;
3647
 
  null_value= false;
3648
 
  return;
3649
 
}
3650
 
 
3651
 
 
3652
 
bool Item_field::result_as_int64_t()
3653
 
{
3654
 
  return field->can_be_compared_as_int64_t();
3655
 
}
3656
 
 
3657
 
 
3658
 
/**
3659
 
  Find a field among specified multiple equalities.
3660
 
 
3661
 
  The function first searches the field among multiple equalities
3662
 
  of the current level (in the cond_equal->current_level list).
3663
 
  If it fails, it continues searching in upper levels accessed
3664
 
  through a pointer cond_equal->upper_levels.
3665
 
  The search terminates as soon as a multiple equality containing 
3666
 
  the field is found. 
3667
 
 
3668
 
  @param cond_equal   reference to list of multiple equalities where
3669
 
                      the field (this object) is to be looked for
3670
 
 
3671
 
  @return
3672
 
    - First Item_equal containing the field, if success
3673
 
    - 0, otherwise
3674
 
*/
3675
 
 
3676
 
Item_equal *Item_field::find_item_equal(COND_EQUAL *cond_equal)
3677
 
{
3678
 
  Item_equal *item= 0;
3679
 
  while (cond_equal)
3680
 
  {
3681
 
    List_iterator_fast<Item_equal> li(cond_equal->current_level);
3682
 
    while ((item= li++))
3683
 
    {
3684
 
      if (item->contains(field))
3685
 
        return item;
3686
 
    }
3687
 
    /* 
3688
 
      The field is not found in any of the multiple equalities
3689
 
      of the current level. Look for it in upper levels
3690
 
    */
3691
 
    cond_equal= cond_equal->upper_levels;
3692
 
  }
3693
 
  return 0;
3694
 
}
3695
 
 
3696
 
 
3697
 
/**
3698
 
  Check whether a field can be substituted by an equal item.
3699
 
 
3700
 
  The function checks whether a substitution of the field
3701
 
  occurrence for an equal item is valid.
3702
 
 
3703
 
  @param arg   *arg != NULL <-> the field is in the context where
3704
 
               substitution for an equal item is valid
3705
 
 
3706
 
  @note
3707
 
    The following statement is not always true:
3708
 
  @n
3709
 
    x=y => F(x)=F(x/y).
3710
 
  @n
3711
 
    This means substitution of an item for an equal item not always
3712
 
    yields an equavalent condition. Here's an example:
3713
 
    @code
3714
 
    'a'='a '
3715
 
    (LENGTH('a')=1) != (LENGTH('a ')=2)
3716
 
  @endcode
3717
 
    Such a substitution is surely valid if either the substituted
3718
 
    field is not of a STRING type or if it is an argument of
3719
 
    a comparison predicate.
3720
 
 
3721
 
  @retval
3722
 
    true   substitution is valid
3723
 
  @retval
3724
 
    false  otherwise
3725
 
*/
3726
 
 
3727
 
bool Item_field::subst_argument_checker(unsigned char **arg)
3728
 
{
3729
 
  return (result_type() != STRING_RESULT) || (*arg);
3730
 
}
3731
 
 
3732
 
 
3733
 
/**
3734
 
  Set a pointer to the multiple equality the field reference belongs to
3735
 
  (if any).
3736
 
 
3737
 
  The function looks for a multiple equality containing the field item
3738
 
  among those referenced by arg.
3739
 
  In the case such equality exists the function does the following.
3740
 
  If the found multiple equality contains a constant, then the field
3741
 
  reference is substituted for this constant, otherwise it sets a pointer
3742
 
  to the multiple equality in the field item.
3743
 
 
3744
 
 
3745
 
  @param arg    reference to list of multiple equalities where
3746
 
                the field (this object) is to be looked for
3747
 
 
3748
 
  @note
3749
 
    This function is supposed to be called as a callback parameter in calls
3750
 
    of the compile method.
3751
 
 
3752
 
  @return
3753
 
    - pointer to the replacing constant item, if the field item was substituted
3754
 
    - pointer to the field item, otherwise.
3755
 
*/
3756
 
 
3757
 
Item *Item_field::equal_fields_propagator(unsigned char *arg)
3758
 
{
3759
 
  if (no_const_subst)
3760
 
    return this;
3761
 
  item_equal= find_item_equal((COND_EQUAL *) arg);
3762
 
  Item *item= 0;
3763
 
  if (item_equal)
3764
 
    item= item_equal->get_const();
3765
 
  /*
3766
 
    Disable const propagation for items used in different comparison contexts.
3767
 
    This must be done because, for example, Item_hex_string->val_int() is not
3768
 
    the same as (Item_hex_string->val_str() in BINARY column)->val_int().
3769
 
    We cannot simply disable the replacement in a particular context (
3770
 
    e.g. <bin_col> = <int_col> AND <bin_col> = <hex_string>) since
3771
 
    Items don't know the context they are in and there are functions like 
3772
 
    IF (<hex_string>, 'yes', 'no').
3773
 
    The same problem occurs when comparing a DATE/TIME field with a
3774
 
    DATE/TIME represented as an int and as a string.
3775
 
  */
3776
 
  if (!item ||
3777
 
      (cmp_context != (Item_result)-1 && item->cmp_context != cmp_context))
3778
 
    item= this;
3779
 
 
3780
 
  return item;
3781
 
}
3782
 
 
3783
 
 
3784
 
/**
3785
 
  Mark the item to not be part of substitution if it's not a binary item.
3786
 
 
3787
 
  See comments in Arg_comparator::set_compare_func() for details.
3788
 
*/
3789
 
 
3790
 
bool Item_field::set_no_const_sub(unsigned char *)
3791
 
{
3792
 
  if (field->charset() != &my_charset_bin)
3793
 
    no_const_subst=1;
3794
 
  return false;
3795
 
}
3796
 
 
3797
 
 
3798
 
/**
3799
 
  Replace an Item_field for an equal Item_field that evaluated earlier
3800
 
  (if any).
3801
 
 
3802
 
  The function returns a pointer to an item that is taken from
3803
 
  the very beginning of the item_equal list which the Item_field
3804
 
  object refers to (belongs to) unless item_equal contains  a constant
3805
 
  item. In this case the function returns this constant item, 
3806
 
  (if the substitution does not require conversion).   
3807
 
  If the Item_field object does not refer any Item_equal object
3808
 
  'this' is returned .
3809
 
 
3810
 
  @param arg   a dummy parameter, is not used here
3811
 
 
3812
 
 
3813
 
  @note
3814
 
    This function is supposed to be called as a callback parameter in calls
3815
 
    of the thransformer method.
3816
 
 
3817
 
  @return
3818
 
    - pointer to a replacement Item_field if there is a better equal item or
3819
 
      a pointer to a constant equal item;
3820
 
    - this - otherwise.
3821
 
*/
3822
 
 
3823
 
Item *Item_field::replace_equal_field(unsigned char *)
3824
 
{
3825
 
  if (item_equal)
3826
 
  {
3827
 
    Item *const_item= item_equal->get_const();
3828
 
    if (const_item)
3829
 
    {
3830
 
      if (cmp_context != (Item_result)-1 &&
3831
 
          const_item->cmp_context != cmp_context)
3832
 
        return this;
3833
 
      return const_item;
3834
 
    }
3835
 
    Item_field *subst= item_equal->get_first();
3836
 
    if (subst && !field->eq(subst->field))
3837
 
      return subst;
3838
 
  }
3839
 
  return this;
3840
 
}
3841
 
 
3842
 
 
3843
 
uint32_t Item_field::max_disp_length()
3844
 
{
3845
 
  return field->max_display_length();
3846
 
}
3847
 
 
3848
 
 
3849
 
void Item::init_make_field(Send_field *tmp_field,
3850
 
                           enum enum_field_types field_type_arg)
3851
 
{
3852
 
  char *empty_name= (char*) "";
3853
 
  tmp_field->db_name=           empty_name;
3854
 
  tmp_field->org_table_name=    empty_name;
3855
 
  tmp_field->org_col_name=      empty_name;
3856
 
  tmp_field->table_name=        empty_name;
3857
 
  tmp_field->col_name=          name;
3858
 
  tmp_field->charsetnr=         collation.collation->number;
3859
 
  tmp_field->flags=             (maybe_null ? 0 : NOT_NULL_FLAG) | 
3860
 
                                (my_binary_compare(collation.collation) ?
3861
 
                                 BINARY_FLAG : 0);
3862
 
  tmp_field->type=              field_type_arg;
3863
 
  tmp_field->length=max_length;
3864
 
  tmp_field->decimals=decimals;
3865
 
}
3866
 
 
3867
 
void Item::make_field(Send_field *tmp_field)
 
1006
void Item::init_make_field(SendField *tmp_field, enum_field_types field_type_arg)
 
1007
{
 
1008
  tmp_field->db_name=   "";
 
1009
  tmp_field->org_table_name= "";
 
1010
  tmp_field->org_col_name= "";
 
1011
  tmp_field->table_name= "";
 
1012
  tmp_field->col_name= name;
 
1013
  tmp_field->charsetnr= collation.collation->number;
 
1014
  tmp_field->flags= (maybe_null ? 0 : NOT_NULL_FLAG) | (my_binary_compare(collation.collation) ? BINARY_FLAG : 0);
 
1015
  tmp_field->type= field_type_arg;
 
1016
  tmp_field->length= max_length;
 
1017
  tmp_field->decimals= decimals;
 
1018
}
 
1019
 
 
1020
void Item::make_field(SendField *tmp_field)
3868
1021
{
3869
1022
  init_make_field(tmp_field, field_type());
3870
1023
}
3871
1024
 
3872
 
 
3873
1025
enum_field_types Item::string_field_type() const
3874
1026
{
3875
1027
  enum_field_types f_type= DRIZZLE_TYPE_VARCHAR;
3878
1030
  return f_type;
3879
1031
}
3880
1032
 
3881
 
 
3882
 
void Item_empty_string::make_field(Send_field *tmp_field)
3883
 
{
3884
 
  init_make_field(tmp_field, string_field_type());
3885
 
}
3886
 
 
3887
 
 
3888
1033
enum_field_types Item::field_type() const
3889
1034
{
3890
1035
  switch (result_type()) {
3891
 
  case STRING_RESULT:  return string_field_type();
3892
 
  case INT_RESULT:     return DRIZZLE_TYPE_LONGLONG;
3893
 
  case DECIMAL_RESULT: return DRIZZLE_TYPE_NEWDECIMAL;
3894
 
  case REAL_RESULT:    return DRIZZLE_TYPE_DOUBLE;
 
1036
  case STRING_RESULT:  
 
1037
    return string_field_type();
 
1038
  case INT_RESULT:     
 
1039
    return DRIZZLE_TYPE_LONGLONG;
 
1040
  case DECIMAL_RESULT: 
 
1041
    return DRIZZLE_TYPE_DECIMAL;
 
1042
  case REAL_RESULT:    
 
1043
    return DRIZZLE_TYPE_DOUBLE;
3895
1044
  case ROW_RESULT:
3896
 
  default:
3897
1045
    assert(0);
3898
 
    return DRIZZLE_TYPE_VARCHAR;
3899
1046
  }
 
1047
 
 
1048
  abort();
3900
1049
}
3901
1050
 
3902
 
 
3903
1051
bool Item::is_datetime()
3904
1052
{
3905
 
  switch (field_type())
3906
 
  {
3907
 
    case DRIZZLE_TYPE_DATE:
3908
 
    case DRIZZLE_TYPE_DATETIME:
3909
 
    case DRIZZLE_TYPE_TIMESTAMP:
3910
 
      return true;
3911
 
    default:
3912
 
      break;
3913
 
  }
3914
 
  return false;
 
1053
  return field::isDateTime(field_type());
3915
1054
}
3916
1055
 
3917
 
 
3918
1056
String *Item::check_well_formed_result(String *str, bool send_error)
3919
1057
{
3920
1058
  /* Check whether we got a well-formed string */
3921
 
  const CHARSET_INFO * const cs= str->charset();
 
1059
  const charset_info_st * const cs= str->charset();
3922
1060
  int well_formed_error;
3923
1061
  uint32_t wlen= cs->cset->well_formed_len(cs,
3924
1062
                                       str->ptr(), str->ptr() + str->length(),
3925
1063
                                       str->length(), &well_formed_error);
3926
1064
  if (wlen < str->length())
3927
1065
  {
3928
 
    Session *session= current_session;
3929
1066
    char hexbuf[7];
3930
1067
    enum DRIZZLE_ERROR::enum_warning_level level;
3931
1068
    uint32_t diff= str->length() - wlen;
3932
 
    set_if_smaller(diff, 3);
3933
 
    octet2hex(hexbuf, str->ptr() + wlen, diff);
 
1069
    set_if_smaller(diff, 3U);
 
1070
    (void) drizzled_string_to_hex(hexbuf, str->ptr() + wlen, diff);
3934
1071
    if (send_error)
3935
1072
    {
3936
1073
      my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
3937
1074
               cs->csname,  hexbuf);
3938
 
      return 0;
 
1075
      return NULL;
3939
1076
    }
3940
1077
    {
3941
1078
      level= DRIZZLE_ERROR::WARN_LEVEL_ERROR;
3942
1079
      null_value= 1;
3943
1080
      str= 0;
3944
1081
    }
3945
 
    push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
 
1082
    push_warning_printf(&getSession(), level, ER_INVALID_CHARACTER_STRING,
3946
1083
                        ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
3947
1084
  }
3948
1085
  return str;
3949
1086
}
3950
1087
 
3951
 
/*
3952
 
  Compare two items using a given collation
3953
 
  
3954
 
  SYNOPSIS
3955
 
    eq_by_collation()
3956
 
    item               item to compare with
3957
 
    binary_cmp         true <-> compare as binaries
3958
 
    cs                 collation to use when comparing strings
3959
 
 
3960
 
  DESCRIPTION
3961
 
    This method works exactly as Item::eq if the collation cs coincides with
3962
 
    the collation of the compared objects. Otherwise, first the collations that
3963
 
    differ from cs are replaced for cs and then the items are compared by
3964
 
    Item::eq. After the comparison the original collations of items are
3965
 
    restored.
3966
 
 
3967
 
  RETURN
3968
 
    1    compared items has been detected as equal   
3969
 
    0    otherwise
3970
 
*/
3971
 
 
3972
 
bool Item::eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO * const cs)
 
1088
bool Item::eq_by_collation(Item *item, bool binary_cmp, const charset_info_st * const cs)
3973
1089
{
3974
 
  const CHARSET_INFO *save_cs= 0;
3975
 
  const CHARSET_INFO *save_item_cs= 0;
 
1090
  const charset_info_st *save_cs= 0;
 
1091
  const charset_info_st *save_item_cs= 0;
3976
1092
  if (collation.collation != cs)
3977
1093
  {
3978
1094
    save_cs= collation.collation;
3989
1105
  if (save_item_cs)
3990
1106
    item->collation.collation= save_item_cs;
3991
1107
  return res;
3992
 
}  
3993
 
 
3994
 
 
3995
 
/**
3996
 
  Create a field to hold a string value from an item.
3997
 
 
3998
 
  If max_length > CONVERT_IF_BIGGER_TO_BLOB create a blob @n
3999
 
  If max_length > 0 create a varchar @n
4000
 
  If max_length == 0 create a CHAR(0) 
4001
 
 
4002
 
  @param table          Table for which the field is created
4003
 
*/
 
1108
}
4004
1109
 
4005
1110
Field *Item::make_string_field(Table *table)
4006
1111
{
4007
1112
  Field *field;
4008
1113
  assert(collation.collation);
4009
1114
  if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB)
 
1115
  {
4010
1116
    field= new Field_blob(max_length, maybe_null, name,
4011
1117
                          collation.collation);
 
1118
  }
4012
1119
  else
4013
 
    field= new Field_varstring(max_length, maybe_null, name, table->s,
 
1120
  {
 
1121
    table->setVariableWidth();
 
1122
    field= new Field_varstring(max_length, maybe_null, name,
4014
1123
                               collation.collation);
 
1124
  }
4015
1125
 
4016
1126
  if (field)
4017
1127
    field->init(table);
4018
1128
  return field;
4019
1129
}
4020
1130
 
4021
 
 
4022
 
/**
4023
 
  Create a field based on field_type of argument.
4024
 
 
4025
 
  For now, this is only used to create a field for
4026
 
  IFNULL(x,something) and time functions
4027
 
 
4028
 
  @retval
4029
 
    NULL  error
4030
 
  @retval
4031
 
    \#    Created field
4032
 
*/
4033
 
 
4034
1131
Field *Item::tmp_table_field_from_field_type(Table *table, bool)
4035
1132
{
4036
1133
  /*
4037
1134
    The field functions defines a field to be not null if null_ptr is not 0
4038
1135
  */
4039
1136
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
4040
 
  Field *field;
 
1137
  Field *field= NULL;
4041
1138
 
4042
1139
  switch (field_type()) {
4043
 
  case DRIZZLE_TYPE_NEWDECIMAL:
4044
 
    field= new Field_new_decimal((unsigned char*) 0, max_length, null_ptr, 0,
4045
 
                                 Field::NONE, name, decimals, 0,
4046
 
                                 unsigned_flag);
 
1140
  case DRIZZLE_TYPE_DECIMAL:
 
1141
    field= new Field_decimal((unsigned char*) 0,
 
1142
                                 max_length,
 
1143
                                 null_ptr,
 
1144
                                 0,
 
1145
                                 Field::NONE,
 
1146
                                 name,
 
1147
                                 decimals);
4047
1148
    break;
4048
1149
  case DRIZZLE_TYPE_LONG:
4049
 
    field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
4050
 
                          name, 0, unsigned_flag);
 
1150
    field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
4051
1151
    break;
4052
1152
  case DRIZZLE_TYPE_LONGLONG:
4053
 
    field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
4054
 
                              name, 0, unsigned_flag);
 
1153
    field= new field::Int64((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
4055
1154
    break;
4056
1155
  case DRIZZLE_TYPE_DOUBLE:
4057
1156
    field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
4058
 
                            name, decimals, 0, unsigned_flag);
 
1157
                            name, decimals, 0, unsigned_flag);
4059
1158
    break;
4060
1159
  case DRIZZLE_TYPE_NULL:
4061
 
    field= new Field_null((unsigned char*) 0, max_length, Field::NONE,
4062
 
                          name, &my_charset_bin);
 
1160
    field= new Field_null((unsigned char*) 0, max_length, name);
4063
1161
    break;
4064
1162
  case DRIZZLE_TYPE_DATE:
4065
 
    field= new Field_date(maybe_null, name, &my_charset_bin);
4066
 
    break;
4067
 
  case DRIZZLE_TYPE_TIME:
4068
 
    field= new Field_time(maybe_null, name, &my_charset_bin);
4069
 
    break;
 
1163
    field= new Field_date(maybe_null, name);
 
1164
    break;
 
1165
 
 
1166
  case DRIZZLE_TYPE_MICROTIME:
 
1167
    field= new field::Microtime(maybe_null, name);
 
1168
    break;
 
1169
 
4070
1170
  case DRIZZLE_TYPE_TIMESTAMP:
4071
 
    field= new Field_timestamp(maybe_null, name, &my_charset_bin);
 
1171
    field= new field::Epoch(maybe_null, name);
4072
1172
    break;
4073
1173
  case DRIZZLE_TYPE_DATETIME:
4074
 
    field= new Field_datetime(maybe_null, name, &my_charset_bin);
4075
 
    break;
4076
 
  default:
4077
 
    /* This case should never be chosen */
4078
 
    assert(0);
4079
 
    /* Fall through to make_string_field() */
 
1174
    field= new Field_datetime(maybe_null, name);
 
1175
    break;
 
1176
  case DRIZZLE_TYPE_TIME:
 
1177
    field= new field::Time(maybe_null, name);
 
1178
    break;
 
1179
  case DRIZZLE_TYPE_BOOLEAN:
 
1180
  case DRIZZLE_TYPE_UUID:
 
1181
  case DRIZZLE_TYPE_IPV6:
4080
1182
  case DRIZZLE_TYPE_ENUM:
4081
1183
  case DRIZZLE_TYPE_VARCHAR:
4082
1184
    return make_string_field(table);
4083
1185
  case DRIZZLE_TYPE_BLOB:
4084
 
    if (this->type() == Item::TYPE_HOLDER)
4085
 
      field= new Field_blob(max_length, maybe_null, name, collation.collation,
4086
 
                            1);
4087
 
    else
4088
1186
      field= new Field_blob(max_length, maybe_null, name, collation.collation);
4089
1187
    break;                                      // Blob handled outside of case
4090
1188
  }
 
1189
  assert(field);
 
1190
 
4091
1191
  if (field)
4092
1192
    field->init(table);
4093
1193
  return field;
4094
1194
}
4095
1195
 
4096
 
 
4097
 
/* ARGSUSED */
4098
 
void Item_field::make_field(Send_field *tmp_field)
4099
 
{
4100
 
  field->make_field(tmp_field);
4101
 
  assert(tmp_field->table_name != 0);
4102
 
  if (name)
4103
 
    tmp_field->col_name=name;                   // Use user supplied name
4104
 
  if (table_name)
4105
 
    tmp_field->table_name= table_name;
4106
 
  if (db_name)
4107
 
    tmp_field->db_name= db_name;
4108
 
}
4109
 
 
4110
 
 
4111
 
/**
4112
 
  Set a field's value from a item.
4113
 
*/
4114
 
 
4115
 
void Item_field::save_org_in_field(Field *to)
4116
 
{
4117
 
  if (field->is_null())
4118
 
  {
4119
 
    null_value=1;
4120
 
    set_field_to_null_with_conversions(to, 1);
4121
 
  }
4122
 
  else
4123
 
  {
4124
 
    to->set_notnull();
4125
 
    field_conv(to,field);
4126
 
    null_value=0;
4127
 
  }
4128
 
}
4129
 
 
4130
 
int Item_field::save_in_field(Field *to, bool no_conversions)
4131
 
{
4132
 
  int res;
4133
 
  if (result_field->is_null())
4134
 
  {
4135
 
    null_value=1;
4136
 
    res= set_field_to_null_with_conversions(to, no_conversions);
4137
 
  }
4138
 
  else
4139
 
  {
4140
 
    to->set_notnull();
4141
 
    res= field_conv(to,result_field);
4142
 
    null_value=0;
4143
 
  }
4144
 
  return(res);
4145
 
}
4146
 
 
4147
 
 
4148
 
/**
4149
 
  Store null in field.
4150
 
 
4151
 
  This is used on INSERT.
4152
 
  Allow NULL to be inserted in timestamp and auto_increment values.
4153
 
 
4154
 
  @param field          Field where we want to store NULL
4155
 
 
4156
 
  @retval
4157
 
    0   ok
4158
 
  @retval
4159
 
    1   Field doesn't support NULL values and can't handle 'field = NULL'
4160
 
*/
4161
 
 
4162
 
int Item_null::save_in_field(Field *field, bool no_conversions)
4163
 
{
4164
 
  return set_field_to_null_with_conversions(field, no_conversions);
4165
 
}
4166
 
 
4167
 
 
4168
 
/**
4169
 
  Store null in field.
4170
 
 
4171
 
  @param field          Field where we want to store NULL
4172
 
 
4173
 
  @retval
4174
 
    0    OK
4175
 
  @retval
4176
 
    1    Field doesn't support NULL values
4177
 
*/
4178
 
 
4179
 
int Item_null::save_safe_in_field(Field *field)
4180
 
{
4181
 
  return set_field_to_null(field);
4182
 
}
4183
 
 
4184
 
 
4185
1196
/*
4186
1197
  This implementation can lose str_value content, so if the
4187
1198
  Item uses str_value to store something, it should
4188
1199
  reimplement it's ::save_in_field() as Item_string, for example, does
4189
1200
*/
4190
 
 
4191
1201
int Item::save_in_field(Field *field, bool no_conversions)
4192
1202
{
4193
1203
  int error;
4194
1204
  if (result_type() == STRING_RESULT)
4195
1205
  {
4196
1206
    String *result;
4197
 
    const CHARSET_INFO * const cs= collation.collation;
 
1207
    const charset_info_st * const cs= collation.collation;
4198
1208
    char buff[MAX_FIELD_WIDTH];         // Alloc buffer for small columns
4199
1209
    str_value.set_quick(buff, sizeof(buff), cs);
4200
1210
    result=val_str(&str_value);
4229
1239
  }
4230
1240
  else if (result_type() == DECIMAL_RESULT)
4231
1241
  {
4232
 
    my_decimal decimal_value;
4233
 
    my_decimal *value= val_decimal(&decimal_value);
 
1242
    type::Decimal decimal_value;
 
1243
    type::Decimal *value= val_decimal(&decimal_value);
4234
1244
    if (null_value)
4235
1245
      return set_field_to_null_with_conversions(field, no_conversions);
4236
1246
    field->set_notnull();
4247
1257
  return error;
4248
1258
}
4249
1259
 
4250
 
 
4251
 
int Item_string::save_in_field(Field *field, bool)
4252
 
{
4253
 
  String *result;
4254
 
  result=val_str(&str_value);
4255
 
  return save_str_value_in_field(field, result);
4256
 
}
4257
 
 
4258
 
 
4259
 
int Item_uint::save_in_field(Field *field, bool no_conversions)
4260
 
{
4261
 
  /* Item_int::save_in_field handles both signed and unsigned. */
4262
 
  return Item_int::save_in_field(field, no_conversions);
4263
 
}
4264
 
 
4265
 
 
4266
 
int Item_int::save_in_field(Field *field, bool)
4267
 
{
4268
 
  int64_t nr=val_int();
4269
 
  if (null_value)
4270
 
    return set_field_to_null(field);
4271
 
  field->set_notnull();
4272
 
  return field->store(nr, unsigned_flag);
4273
 
}
4274
 
 
4275
 
 
4276
 
int Item_decimal::save_in_field(Field *field, bool)
4277
 
{
4278
 
  field->set_notnull();
4279
 
  return field->store_decimal(&decimal_value);
4280
 
}
4281
 
 
4282
 
 
4283
 
bool Item_int::eq(const Item *arg, bool) const
4284
 
{
4285
 
  /* No need to check for null value as basic constant can't be NULL */
4286
 
  if (arg->basic_const_item() && arg->type() == type())
4287
 
  {
4288
 
    /*
4289
 
      We need to cast off const to call val_int(). This should be OK for
4290
 
      a basic constant.
4291
 
    */
4292
 
    Item *item= (Item*) arg;
4293
 
    return item->val_int() == value && item->unsigned_flag == unsigned_flag;
4294
 
  }
4295
 
  return false;
4296
 
}
4297
 
 
4298
 
 
4299
 
Item *Item_int_with_ref::clone_item()
4300
 
{
4301
 
  assert(ref->const_item());
4302
 
  /*
4303
 
    We need to evaluate the constant to make sure it works with
4304
 
    parameter markers.
4305
 
  */
4306
 
  return (ref->unsigned_flag ?
4307
 
          new Item_uint(ref->name, ref->val_int(), ref->max_length) :
4308
 
          new Item_int(ref->name, ref->val_int(), ref->max_length));
4309
 
}
4310
 
 
4311
 
 
4312
 
Item_num *Item_uint::neg()
4313
 
{
4314
 
  Item_decimal *item= new Item_decimal(value, 1);
4315
 
  return item->neg();
4316
 
}
4317
 
 
4318
 
 
4319
 
static uint32_t nr_of_decimals(const char *str, const char *end)
4320
 
{
4321
 
  const char *decimal_point;
4322
 
 
4323
 
  /* Find position for '.' */
4324
 
  for (;;)
4325
 
  {
4326
 
    if (str == end)
4327
 
      return 0;
4328
 
    if (*str == 'e' || *str == 'E')
4329
 
      return NOT_FIXED_DEC;    
4330
 
    if (*str++ == '.')
4331
 
      break;
4332
 
  }
4333
 
  decimal_point= str;
4334
 
  for (; my_isdigit(system_charset_info, *str) ; str++)
4335
 
    ;
4336
 
  if (*str == 'e' || *str == 'E')
4337
 
    return NOT_FIXED_DEC;
4338
 
  return (uint) (str - decimal_point);
4339
 
}
4340
 
 
4341
 
 
4342
 
/**
4343
 
  This function is only called during parsing. We will signal an error if
4344
 
  value is not a true double value (overflow)
4345
 
*/
4346
 
 
4347
 
Item_float::Item_float(const char *str_arg, uint32_t length)
4348
 
{
4349
 
  int error;
4350
 
  char *end_not_used;
4351
 
  value= my_strntod(&my_charset_bin, (char*) str_arg, length, &end_not_used,
4352
 
                    &error);
4353
 
  if (error)
4354
 
  {
4355
 
    /*
4356
 
      Note that we depend on that str_arg is null terminated, which is true
4357
 
      when we are in the parser
4358
 
    */
4359
 
    assert(str_arg[length] == 0);
4360
 
    my_error(ER_ILLEGAL_VALUE_FOR_TYPE, MYF(0), "double", (char*) str_arg);
4361
 
  }
4362
 
  presentation= name=(char*) str_arg;
4363
 
  decimals=(uint8_t) nr_of_decimals(str_arg, str_arg+length);
4364
 
  max_length=length;
4365
 
  fixed= 1;
4366
 
}
4367
 
 
4368
 
 
4369
 
int Item_float::save_in_field(Field *field, bool)
4370
 
{
4371
 
  double nr= val_real();
4372
 
  if (null_value)
4373
 
    return set_field_to_null(field);
4374
 
  field->set_notnull();
4375
 
  return field->store(nr);
4376
 
}
4377
 
 
4378
 
 
4379
 
void Item_float::print(String *str, enum_query_type)
4380
 
{
4381
 
  if (presentation)
4382
 
  {
4383
 
    str->append(presentation);
4384
 
    return;
4385
 
  }
4386
 
  char buffer[20];
4387
 
  String num(buffer, sizeof(buffer), &my_charset_bin);
4388
 
  num.set_real(value, decimals, &my_charset_bin);
4389
 
  str->append(num);
4390
 
}
4391
 
 
4392
 
 
4393
 
/*
4394
 
  hex item
4395
 
  In string context this is a binary string.
4396
 
  In number context this is a int64_t value.
4397
 
*/
4398
 
 
4399
 
bool Item_float::eq(const Item *arg, bool) const
4400
 
{
4401
 
  if (arg->basic_const_item() && arg->type() == type())
4402
 
  {
4403
 
    /*
4404
 
      We need to cast off const to call val_int(). This should be OK for
4405
 
      a basic constant.
4406
 
    */
4407
 
    Item *item= (Item*) arg;
4408
 
    return item->val_real() == value;
4409
 
  }
4410
 
  return false;
4411
 
}
4412
 
 
4413
 
 
4414
 
inline uint32_t char_val(char X)
4415
 
{
4416
 
  return (uint) (X >= '0' && X <= '9' ? X-'0' :
4417
 
                 X >= 'A' && X <= 'Z' ? X-'A'+10 :
4418
 
                 X-'a'+10);
4419
 
}
4420
 
 
4421
 
 
4422
 
Item_hex_string::Item_hex_string(const char *str, uint32_t str_length)
4423
 
{
4424
 
  max_length=(str_length+1)/2;
4425
 
  char *ptr=(char*) sql_alloc(max_length+1);
4426
 
  if (!ptr)
4427
 
    return;
4428
 
  str_value.set(ptr,max_length,&my_charset_bin);
4429
 
  char *end=ptr+max_length;
4430
 
  if (max_length*2 != str_length)
4431
 
    *ptr++=char_val(*str++);                    // Not even, assume 0 prefix
4432
 
  while (ptr != end)
4433
 
  {
4434
 
    *ptr++= (char) (char_val(str[0])*16+char_val(str[1]));
4435
 
    str+=2;
4436
 
  }
4437
 
  *ptr=0;                                       // Keep purify happy
4438
 
  collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
4439
 
  fixed= 1;
4440
 
  unsigned_flag= 1;
4441
 
}
4442
 
 
4443
 
int64_t Item_hex_string::val_int()
4444
 
{
4445
 
  // following assert is redundant, because fixed=1 assigned in constructor
4446
 
  assert(fixed == 1);
4447
 
  char *end=(char*) str_value.ptr()+str_value.length(),
4448
 
       *ptr=end-cmin(str_value.length(),(uint32_t)sizeof(int64_t));
4449
 
 
4450
 
  uint64_t value=0;
4451
 
  for (; ptr != end ; ptr++)
4452
 
    value=(value << 8)+ (uint64_t) (unsigned char) *ptr;
4453
 
  return (int64_t) value;
4454
 
}
4455
 
 
4456
 
 
4457
 
my_decimal *Item_hex_string::val_decimal(my_decimal *decimal_value)
4458
 
{
4459
 
  // following assert is redundant, because fixed=1 assigned in constructor
4460
 
  assert(fixed == 1);
4461
 
  uint64_t value= (uint64_t)val_int();
4462
 
  int2my_decimal(E_DEC_FATAL_ERROR, value, true, decimal_value);
4463
 
  return (decimal_value);
4464
 
}
4465
 
 
4466
 
 
4467
 
int Item_hex_string::save_in_field(Field *field, bool)
4468
 
{
4469
 
  field->set_notnull();
4470
 
  if (field->result_type() == STRING_RESULT)
4471
 
    return field->store(str_value.ptr(), str_value.length(),
4472
 
                        collation.collation);
4473
 
 
4474
 
  uint64_t nr;
4475
 
  uint32_t length= str_value.length();
4476
 
  if (length > 8)
4477
 
  {
4478
 
    nr= field->flags & UNSIGNED_FLAG ? UINT64_MAX : INT64_MAX;
4479
 
    goto warn;
4480
 
  }
4481
 
  nr= (uint64_t) val_int();
4482
 
  if ((length == 8) && !(field->flags & UNSIGNED_FLAG) && (nr > INT64_MAX))
4483
 
  {
4484
 
    nr= INT64_MAX;
4485
 
    goto warn;
4486
 
  }
4487
 
  return field->store((int64_t) nr, true);  // Assume hex numbers are unsigned
4488
 
 
4489
 
warn:
4490
 
  if (!field->store((int64_t) nr, true))
4491
 
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE,
4492
 
                       1);
4493
 
  return 1;
4494
 
}
4495
 
 
4496
 
 
4497
 
void Item_hex_string::print(String *str, enum_query_type)
4498
 
{
4499
 
  char *end= (char*) str_value.ptr() + str_value.length(),
4500
 
       *ptr= end - cmin(str_value.length(), (uint32_t)sizeof(int64_t));
4501
 
  str->append("0x");
4502
 
  for (; ptr != end ; ptr++)
4503
 
  {
4504
 
    str->append(_dig_vec_lower[((unsigned char) *ptr) >> 4]);
4505
 
    str->append(_dig_vec_lower[((unsigned char) *ptr) & 0x0F]);
4506
 
  }
4507
 
}
4508
 
 
4509
 
 
4510
 
bool Item_hex_string::eq(const Item *arg, bool binary_cmp) const
4511
 
{
4512
 
  if (arg->basic_const_item() && arg->type() == type())
4513
 
  {
4514
 
    if (binary_cmp)
4515
 
      return !stringcmp(&str_value, &arg->str_value);
4516
 
    return !sortcmp(&str_value, &arg->str_value, collation.collation);
4517
 
  }
4518
 
  return false;
4519
 
}
4520
 
 
4521
 
 
4522
 
Item *Item_hex_string::safe_charset_converter(const CHARSET_INFO * const tocs)
4523
 
{
4524
 
  Item_string *conv;
4525
 
  String tmp, *str= val_str(&tmp);
4526
 
 
4527
 
  if (!(conv= new Item_string(str->ptr(), str->length(), tocs)))
4528
 
    return NULL;
4529
 
  conv->str_value.copy();
4530
 
  conv->str_value.mark_as_const();
4531
 
  return conv;
4532
 
}
4533
 
 
4534
 
 
4535
 
/*
4536
 
  bin item.
4537
 
  In string context this is a binary string.
4538
 
  In number context this is a int64_t value.
4539
 
*/
4540
 
  
4541
 
Item_bin_string::Item_bin_string(const char *str, uint32_t str_length)
4542
 
{
4543
 
  const char *end= str + str_length - 1;
4544
 
  unsigned char bits= 0;
4545
 
  uint32_t power= 1;
4546
 
 
4547
 
  max_length= (str_length + 7) >> 3;
4548
 
  char *ptr= (char*) sql_alloc(max_length + 1);
4549
 
  if (!ptr)
4550
 
    return;
4551
 
  str_value.set(ptr, max_length, &my_charset_bin);
4552
 
  ptr+= max_length - 1;
4553
 
  ptr[1]= 0;                     // Set end null for string
4554
 
  for (; end >= str; end--)
4555
 
  {
4556
 
    if (power == 256)
4557
 
    {
4558
 
      power= 1;
4559
 
      *ptr--= bits;
4560
 
      bits= 0;     
4561
 
    }
4562
 
    if (*end == '1')
4563
 
      bits|= power; 
4564
 
    power<<= 1;
4565
 
  }
4566
 
  *ptr= (char) bits;
4567
 
  collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
4568
 
  fixed= 1;
4569
 
}
4570
 
 
4571
 
 
4572
 
/**
4573
 
  Pack data in buffer for sending.
4574
 
*/
4575
 
 
4576
 
bool Item_null::send(Protocol *protocol,
4577
 
                     String *)
4578
 
{
4579
 
  return protocol->store_null();
4580
 
}
4581
 
 
4582
 
/**
4583
 
  This is only called from items that is not of type item_field.
4584
 
*/
4585
 
 
4586
 
bool Item::send(Protocol *protocol, String *buffer)
4587
 
{
4588
 
  bool result= false;
4589
 
  enum_field_types f_type;
4590
 
 
4591
 
  switch ((f_type=field_type())) {
4592
 
  default:
 
1260
/**
 
1261
  Check if an item is a constant one and can be cached.
 
1262
 
 
1263
  @param arg [out] TRUE <=> Cache this item.
 
1264
 
 
1265
  @return TRUE  Go deeper in item tree.
 
1266
  @return FALSE Don't go deeper in item tree.
 
1267
*/
 
1268
 
 
1269
bool Item::cache_const_expr_analyzer(unsigned char **arg)
 
1270
{
 
1271
  bool *cache_flag= (bool*)*arg;
 
1272
  if (!*cache_flag)
 
1273
  {
 
1274
    Item *item= real_item();
 
1275
    /*
 
1276
      Cache constant items unless it's a basic constant, constant field or
 
1277
      a subselect (they use their own cache).
 
1278
    */
 
1279
    if (const_item() &&
 
1280
        !(item->basic_const_item() || item->type() == Item::FIELD_ITEM ||
 
1281
          item->type() == SUBSELECT_ITEM ||
 
1282
           /*
 
1283
             Do not cache GET_USER_VAR() function as its const_item() may
 
1284
             return TRUE for the current thread but it still may change
 
1285
             during the execution.
 
1286
           */
 
1287
          (item->type() == Item::FUNC_ITEM &&
 
1288
           ((Item_func*)item)->functype() == Item_func::GUSERVAR_FUNC)))
 
1289
      *cache_flag= true;
 
1290
    return true;
 
1291
  }
 
1292
  return false;
 
1293
}
 
1294
 
 
1295
/**
 
1296
  Cache item if needed.
 
1297
 
 
1298
  @param arg   TRUE <=> Cache this item.
 
1299
 
 
1300
  @return cache if cache needed.
 
1301
  @return this otherwise.
 
1302
*/
 
1303
 
 
1304
Item* Item::cache_const_expr_transformer(unsigned char *arg)
 
1305
{
 
1306
  if (*(bool*)arg)
 
1307
  {
 
1308
    *((bool*)arg)= false;
 
1309
    Item_cache *cache= Item_cache::get_cache(this);
 
1310
    if (!cache)
 
1311
      return NULL;
 
1312
    cache->setup(this);
 
1313
    cache->store(this);
 
1314
    return cache;
 
1315
  }
 
1316
  return this;
 
1317
}
 
1318
 
 
1319
void Item::send(plugin::Client *client, String *buffer)
 
1320
{
 
1321
  switch (field_type())
 
1322
  {
 
1323
  case DRIZZLE_TYPE_DATE:
4593
1324
  case DRIZZLE_TYPE_NULL:
4594
1325
  case DRIZZLE_TYPE_ENUM:
4595
1326
  case DRIZZLE_TYPE_BLOB:
4596
1327
  case DRIZZLE_TYPE_VARCHAR:
4597
 
  case DRIZZLE_TYPE_NEWDECIMAL:
4598
 
  {
4599
 
    String *res;
4600
 
    if ((res=val_str(buffer)))
4601
 
      result= protocol->store(res->ptr(),res->length(),res->charset());
4602
 
    break;
4603
 
  }
 
1328
  case DRIZZLE_TYPE_BOOLEAN:
 
1329
  case DRIZZLE_TYPE_UUID:
 
1330
  case DRIZZLE_TYPE_IPV6:
 
1331
  case DRIZZLE_TYPE_DECIMAL:
 
1332
    {
 
1333
      if (String* res=val_str(buffer))
 
1334
        client->store(res->ptr(), res->length());
 
1335
      break;
 
1336
    }
4604
1337
  case DRIZZLE_TYPE_LONG:
4605
 
  {
4606
 
    int64_t nr;
4607
 
    nr= val_int();
4608
 
    if (!null_value)
4609
 
      result= protocol->store_long(nr);
4610
 
    break;
4611
 
  }
 
1338
    {
 
1339
      int64_t nr= val_int();
 
1340
      if (!null_value)
 
1341
        client->store((int32_t)nr);
 
1342
      break;
 
1343
    }
4612
1344
  case DRIZZLE_TYPE_LONGLONG:
4613
 
  {
4614
 
    int64_t nr;
4615
 
    nr= val_int();
4616
 
    if (!null_value)
4617
 
      result= protocol->store_int64_t(nr, unsigned_flag);
4618
 
    break;
4619
 
  }
 
1345
    {
 
1346
      int64_t nr= val_int();
 
1347
      if (!null_value)
 
1348
      {
 
1349
        if (unsigned_flag)
 
1350
          client->store((uint64_t)nr);
 
1351
        else
 
1352
          client->store((int64_t)nr);
 
1353
      }
 
1354
      break;
 
1355
    }
4620
1356
  case DRIZZLE_TYPE_DOUBLE:
4621
 
  {
4622
 
    double nr= val_real();
4623
 
    if (!null_value)
4624
 
      result= protocol->store(nr, decimals, buffer);
4625
 
    break;
4626
 
  }
 
1357
    {
 
1358
      double nr= val_real();
 
1359
      if (!null_value)
 
1360
        client->store(nr, decimals, buffer);
 
1361
      break;
 
1362
    }
 
1363
  case DRIZZLE_TYPE_TIME:
 
1364
    {
 
1365
      type::Time tm;
 
1366
      get_time(tm);
 
1367
      if (not null_value)
 
1368
        client->store(&tm);
 
1369
      break;
 
1370
    }
4627
1371
  case DRIZZLE_TYPE_DATETIME:
 
1372
  case DRIZZLE_TYPE_MICROTIME:
4628
1373
  case DRIZZLE_TYPE_TIMESTAMP:
4629
 
  {
4630
 
    DRIZZLE_TIME tm;
4631
 
    get_date(&tm, TIME_FUZZY_DATE);
4632
 
    if (!null_value)
4633
1374
    {
4634
 
      if (f_type == DRIZZLE_TYPE_DATE)
4635
 
        return protocol->store_date(&tm);
4636
 
      else
4637
 
        result= protocol->store(&tm);
 
1375
      type::Time tm;
 
1376
      get_date(tm, TIME_FUZZY_DATE);
 
1377
      if (!null_value)
 
1378
        client->store(&tm);
 
1379
      break;
4638
1380
    }
4639
 
    break;
4640
 
  }
4641
 
  case DRIZZLE_TYPE_TIME:
4642
 
  {
4643
 
    DRIZZLE_TIME tm;
4644
 
    get_time(&tm);
4645
 
    if (!null_value)
4646
 
      result= protocol->store_time(&tm);
4647
 
    break;
4648
 
  }
4649
1381
  }
4650
1382
  if (null_value)
4651
 
    result= protocol->store_null();
4652
 
  return result;
4653
 
}
4654
 
 
4655
 
 
4656
 
bool Item_field::send(Protocol *protocol, String *)
4657
 
{
4658
 
  return protocol->store(result_field);
4659
 
}
4660
 
 
4661
 
 
4662
 
void Item_field::update_null_value()
4663
 
{
4664
 
  /*
4665
 
    need to set no_errors to prevent warnings about type conversion
4666
 
    popping up.
4667
 
  */
4668
 
  Session *session= field->table->in_use;
4669
 
  int no_errors;
4670
 
 
4671
 
  no_errors= session->no_errors;
4672
 
  session->no_errors= 1;
4673
 
  Item::update_null_value();
4674
 
  session->no_errors= no_errors;
4675
 
}
4676
 
 
4677
 
 
4678
 
/*
4679
 
  Add the field to the select list and substitute it for the reference to
4680
 
  the field.
4681
 
 
4682
 
  SYNOPSIS
4683
 
    Item_field::update_value_transformer()
4684
 
    select_arg      current select
4685
 
 
4686
 
  DESCRIPTION
4687
 
    If the field doesn't belong to the table being inserted into then it is
4688
 
    added to the select list, pointer to it is stored in the ref_pointer_array
4689
 
    of the select and the field itself is substituted for the Item_ref object.
4690
 
    This is done in order to get correct values from update fields that
4691
 
    belongs to the SELECT part in the INSERT .. SELECT .. ON DUPLICATE KEY
4692
 
    UPDATE statement.
4693
 
 
4694
 
  RETURN
4695
 
    0             if error occured
4696
 
    ref           if all conditions are met
4697
 
    this field    otherwise
4698
 
*/
4699
 
 
4700
 
Item *Item_field::update_value_transformer(unsigned char *select_arg)
4701
 
{
4702
 
  SELECT_LEX *select= (SELECT_LEX*)select_arg;
4703
 
  assert(fixed);
4704
 
 
4705
 
  if (field->table != select->context.table_list->table)
4706
 
  {
4707
 
    List<Item> *all_fields= &select->join->all_fields;
4708
 
    Item **ref_pointer_array= select->ref_pointer_array;
4709
 
    int el= all_fields->elements;
4710
 
    Item_ref *ref;
4711
 
 
4712
 
    ref_pointer_array[el]= (Item*)this;
4713
 
    all_fields->push_front((Item*)this);
4714
 
    ref= new Item_ref(&select->context, ref_pointer_array + el,
4715
 
                      table_name, field_name);
4716
 
    return ref;
4717
 
  }
4718
 
  return this;
4719
 
}
4720
 
 
4721
 
 
4722
 
void Item_field::print(String *str, enum_query_type query_type)
4723
 
{
4724
 
  if (field && field->table->const_table)
4725
 
  {
4726
 
    char buff[MAX_FIELD_WIDTH];
4727
 
    String tmp(buff,sizeof(buff),str->charset());
4728
 
    field->val_str(&tmp);
4729
 
    str->append('\'');
4730
 
    str->append(tmp);
4731
 
    str->append('\'');
4732
 
    return;
4733
 
  }
4734
 
  Item_ident::print(str, query_type);
4735
 
}
4736
 
 
4737
 
 
4738
 
Item_ref::Item_ref(Name_resolution_context *context_arg,
4739
 
                   Item **item, const char *table_name_arg,
4740
 
                   const char *field_name_arg,
4741
 
                   bool alias_name_used_arg)
4742
 
  :Item_ident(context_arg, NULL, table_name_arg, field_name_arg),
4743
 
   result_field(0), ref(item)
4744
 
{
4745
 
  alias_name_used= alias_name_used_arg;
4746
 
  /*
4747
 
    This constructor used to create some internals references over fixed items
4748
 
  */
4749
 
  if (ref && *ref && (*ref)->fixed)
4750
 
    set_properties();
4751
 
}
4752
 
 
4753
 
 
4754
 
/**
4755
 
  Resolve the name of a reference to a column reference.
4756
 
 
4757
 
  The method resolves the column reference represented by 'this' as a column
4758
 
  present in one of: GROUP BY clause, SELECT clause, outer queries. It is
4759
 
  used typically for columns in the HAVING clause which are not under
4760
 
  aggregate functions.
4761
 
 
4762
 
  POSTCONDITION @n
4763
 
  Item_ref::ref is 0 or points to a valid item.
4764
 
 
4765
 
  @note
4766
 
    The name resolution algorithm used is (where [T_j] is an optional table
4767
 
    name that qualifies the column name):
4768
 
 
4769
 
  @code
4770
 
        resolve_extended([T_j].col_ref_i)
4771
 
        {
4772
 
          Search for a column or derived column named col_ref_i [in table T_j]
4773
 
          in the SELECT and GROUP clauses of Q.
4774
 
 
4775
 
          if such a column is NOT found AND    // Lookup in outer queries.
4776
 
             there are outer queries
4777
 
          {
4778
 
            for each outer query Q_k beginning from the inner-most one
4779
 
           {
4780
 
              Search for a column or derived column named col_ref_i
4781
 
              [in table T_j] in the SELECT and GROUP clauses of Q_k.
4782
 
 
4783
 
              if such a column is not found AND
4784
 
                 - Q_k is not a group query AND
4785
 
                 - Q_k is not inside an aggregate function
4786
 
                 OR
4787
 
                 - Q_(k-1) is not in a HAVING or SELECT clause of Q_k
4788
 
              {
4789
 
                search for a column or derived column named col_ref_i
4790
 
                [in table T_j] in the FROM clause of Q_k;
4791
 
              }
4792
 
            }
4793
 
          }
4794
 
        }
4795
 
  @endcode
4796
 
  @n
4797
 
    This procedure treats GROUP BY and SELECT clauses as one namespace for
4798
 
    column references in HAVING. Notice that compared to
4799
 
    Item_field::fix_fields, here we first search the SELECT and GROUP BY
4800
 
    clauses, and then we search the FROM clause.
4801
 
 
4802
 
  @param[in]     session        current thread
4803
 
  @param[in,out] reference  view column if this item was resolved to a
4804
 
    view column
4805
 
 
4806
 
  @todo
4807
 
    Here we could first find the field anyway, and then test this
4808
 
    condition, so that we can give a better error message -
4809
 
    ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
4810
 
    ER_BAD_FIELD_ERROR which we produce now.
4811
 
 
4812
 
  @retval
4813
 
    true  if error
4814
 
  @retval
4815
 
    false on success
4816
 
*/
4817
 
 
4818
 
bool Item_ref::fix_fields(Session *session, Item **reference)
4819
 
{
4820
 
  enum_parsing_place place= NO_MATTER;
4821
 
  assert(fixed == 0);
4822
 
  SELECT_LEX *current_sel= session->lex->current_select;
4823
 
 
4824
 
  if (!ref || ref == not_found_item)
4825
 
  {
4826
 
    if (!(ref= resolve_ref_in_select_and_group(session, this,
4827
 
                                               context->select_lex)))
4828
 
      goto error;             /* Some error occurred (e.g. ambiguous names). */
4829
 
 
4830
 
    if (ref == not_found_item) /* This reference was not resolved. */
4831
 
    {
4832
 
      Name_resolution_context *last_checked_context= context;
4833
 
      Name_resolution_context *outer_context= context->outer_context;
4834
 
      Field *from_field;
4835
 
      ref= 0;
4836
 
 
4837
 
      if (!outer_context)
4838
 
      {
4839
 
        /* The current reference cannot be resolved in this query. */
4840
 
        my_error(ER_BAD_FIELD_ERROR,MYF(0),
4841
 
                 this->full_name(), current_session->where);
4842
 
        goto error;
4843
 
      }
4844
 
 
4845
 
      /*
4846
 
        If there is an outer context (select), and it is not a derived table
4847
 
        (which do not support the use of outer fields for now), try to
4848
 
        resolve this reference in the outer select(s).
4849
 
 
4850
 
        We treat each subselect as a separate namespace, so that different
4851
 
        subselects may contain columns with the same names. The subselects are
4852
 
        searched starting from the innermost.
4853
 
      */
4854
 
      from_field= (Field*) not_found_field;
4855
 
 
4856
 
      do
4857
 
      {
4858
 
        SELECT_LEX *select= outer_context->select_lex;
4859
 
        Item_subselect *prev_subselect_item=
4860
 
          last_checked_context->select_lex->master_unit()->item;
4861
 
        last_checked_context= outer_context;
4862
 
 
4863
 
        /* Search in the SELECT and GROUP lists of the outer select. */
4864
 
        if (outer_context->resolve_in_select_list)
4865
 
        {
4866
 
          if (!(ref= resolve_ref_in_select_and_group(session, this, select)))
4867
 
            goto error; /* Some error occurred (e.g. ambiguous names). */
4868
 
          if (ref != not_found_item)
4869
 
          {
4870
 
            assert(*ref && (*ref)->fixed);
4871
 
            prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
4872
 
            prev_subselect_item->const_item_cache&= (*ref)->const_item();
4873
 
            break;
4874
 
          }
4875
 
          /*
4876
 
            Set ref to 0 to ensure that we get an error in case we replaced
4877
 
            this item with another item and still use this item in some
4878
 
            other place of the parse tree.
4879
 
          */
4880
 
          ref= 0;
4881
 
        }
4882
 
 
4883
 
        place= prev_subselect_item->parsing_place;
4884
 
        /*
4885
 
          Check table fields only if the subquery is used somewhere out of
4886
 
          HAVING or the outer SELECT does not use grouping (i.e. tables are
4887
 
          accessible).
4888
 
          TODO:
4889
 
          Here we could first find the field anyway, and then test this
4890
 
          condition, so that we can give a better error message -
4891
 
          ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
4892
 
          ER_BAD_FIELD_ERROR which we produce now.
4893
 
        */
4894
 
        if ((place != IN_HAVING ||
4895
 
             (!select->with_sum_func &&
4896
 
              select->group_list.elements == 0)))
4897
 
        {
4898
 
          /*
4899
 
            In case of view, find_field_in_tables() write pointer to view
4900
 
            field expression to 'reference', i.e. it substitute that
4901
 
            expression instead of this Item_ref
4902
 
          */
4903
 
          from_field= find_field_in_tables(session, this,
4904
 
                                           outer_context->
4905
 
                                             first_name_resolution_table,
4906
 
                                           outer_context->
4907
 
                                             last_name_resolution_table,
4908
 
                                           reference,
4909
 
                                           IGNORE_EXCEPT_NON_UNIQUE,
4910
 
                                           true, true);
4911
 
          if (! from_field)
4912
 
            goto error;
4913
 
          if (from_field == view_ref_found)
4914
 
          {
4915
 
            Item::Type refer_type= (*reference)->type();
4916
 
            prev_subselect_item->used_tables_cache|=
4917
 
              (*reference)->used_tables();
4918
 
            prev_subselect_item->const_item_cache&=
4919
 
              (*reference)->const_item();
4920
 
            assert((*reference)->type() == REF_ITEM);
4921
 
            mark_as_dependent(session, last_checked_context->select_lex,
4922
 
                              context->select_lex, this,
4923
 
                              ((refer_type == REF_ITEM ||
4924
 
                                refer_type == FIELD_ITEM) ?
4925
 
                               (Item_ident*) (*reference) :
4926
 
                               0));
4927
 
            /*
4928
 
              view reference found, we substituted it instead of this
4929
 
              Item, so can quit
4930
 
            */
4931
 
            return false;
4932
 
          }
4933
 
          if (from_field != not_found_field)
4934
 
          {
4935
 
            if (cached_table && cached_table->select_lex &&
4936
 
                outer_context->select_lex &&
4937
 
                cached_table->select_lex != outer_context->select_lex)
4938
 
            {
4939
 
              /*
4940
 
                Due to cache, find_field_in_tables() can return field which
4941
 
                doesn't belong to provided outer_context. In this case we have
4942
 
                to find proper field context in order to fix field correcly.
4943
 
              */
4944
 
              do
4945
 
              {
4946
 
                outer_context= outer_context->outer_context;
4947
 
                select= outer_context->select_lex;
4948
 
                prev_subselect_item=
4949
 
                  last_checked_context->select_lex->master_unit()->item;
4950
 
                last_checked_context= outer_context;
4951
 
              } while (outer_context && outer_context->select_lex &&
4952
 
                       cached_table->select_lex != outer_context->select_lex);
4953
 
            }
4954
 
            prev_subselect_item->used_tables_cache|= from_field->table->map;
4955
 
            prev_subselect_item->const_item_cache= 0;
4956
 
            break;
4957
 
          }
4958
 
        }
4959
 
        assert(from_field == not_found_field);
4960
 
 
4961
 
        /* Reference is not found => depend on outer (or just error). */
4962
 
        prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
4963
 
        prev_subselect_item->const_item_cache= 0;
4964
 
 
4965
 
        outer_context= outer_context->outer_context;
4966
 
      } while (outer_context);
4967
 
 
4968
 
      assert(from_field != 0 && from_field != view_ref_found);
4969
 
      if (from_field != not_found_field)
4970
 
      {
4971
 
        Item_field* fld;
4972
 
        if (!(fld= new Item_field(from_field)))
4973
 
          goto error;
4974
 
        session->change_item_tree(reference, fld);
4975
 
        mark_as_dependent(session, last_checked_context->select_lex,
4976
 
                          session->lex->current_select, this, fld);
4977
 
        /*
4978
 
          A reference is resolved to a nest level that's outer or the same as
4979
 
          the nest level of the enclosing set function : adjust the value of
4980
 
          max_arg_level for the function if it's needed.
4981
 
        */
4982
 
        if (session->lex->in_sum_func &&
4983
 
            session->lex->in_sum_func->nest_level >= 
4984
 
            last_checked_context->select_lex->nest_level)
4985
 
          set_if_bigger(session->lex->in_sum_func->max_arg_level,
4986
 
                        last_checked_context->select_lex->nest_level);
4987
 
        return false;
4988
 
      }
4989
 
      if (ref == 0)
4990
 
      {
4991
 
        /* The item was not a table field and not a reference */
4992
 
        my_error(ER_BAD_FIELD_ERROR, MYF(0),
4993
 
                 this->full_name(), current_session->where);
4994
 
        goto error;
4995
 
      }
4996
 
      /* Should be checked in resolve_ref_in_select_and_group(). */
4997
 
      assert(*ref && (*ref)->fixed);
4998
 
      mark_as_dependent(session, last_checked_context->select_lex,
4999
 
                        context->select_lex, this, this);
5000
 
      /*
5001
 
        A reference is resolved to a nest level that's outer or the same as
5002
 
        the nest level of the enclosing set function : adjust the value of
5003
 
        max_arg_level for the function if it's needed.
5004
 
      */
5005
 
      if (session->lex->in_sum_func &&
5006
 
          session->lex->in_sum_func->nest_level >= 
5007
 
          last_checked_context->select_lex->nest_level)
5008
 
        set_if_bigger(session->lex->in_sum_func->max_arg_level,
5009
 
                      last_checked_context->select_lex->nest_level);
5010
 
    }
5011
 
  }
5012
 
 
5013
 
  assert(*ref);
5014
 
  /*
5015
 
    Check if this is an incorrect reference in a group function or forward
5016
 
    reference. Do not issue an error if this is:
5017
 
      1. outer reference (will be fixed later by the fix_inner_refs function);
5018
 
      2. an unnamed reference inside an aggregate function.
5019
 
  */
5020
 
  if (!((*ref)->type() == REF_ITEM &&
5021
 
       ((Item_ref *)(*ref))->ref_type() == OUTER_REF) &&
5022
 
      (((*ref)->with_sum_func && name &&
5023
 
        !(current_sel->linkage != GLOBAL_OPTIONS_TYPE &&
5024
 
          current_sel->having_fix_field)) ||
5025
 
       !(*ref)->fixed))
5026
 
  {
5027
 
    my_error(ER_ILLEGAL_REFERENCE, MYF(0),
5028
 
             name, ((*ref)->with_sum_func?
5029
 
                    "reference to group function":
5030
 
                    "forward reference in item list"));
5031
 
    goto error;
5032
 
  }
5033
 
 
5034
 
  set_properties();
5035
 
 
5036
 
  if ((*ref)->check_cols(1))
5037
 
    goto error;
5038
 
  return false;
5039
 
 
5040
 
error:
5041
 
  context->process_error(session);
5042
 
  return true;
5043
 
}
5044
 
 
5045
 
 
5046
 
void Item_ref::set_properties()
5047
 
{
5048
 
  max_length= (*ref)->max_length;
5049
 
  maybe_null= (*ref)->maybe_null;
5050
 
  decimals=   (*ref)->decimals;
5051
 
  collation.set((*ref)->collation);
5052
 
  /*
5053
 
    We have to remember if we refer to a sum function, to ensure that
5054
 
    split_sum_func() doesn't try to change the reference.
5055
 
  */
5056
 
  with_sum_func= (*ref)->with_sum_func;
5057
 
  unsigned_flag= (*ref)->unsigned_flag;
5058
 
  fixed= 1;
5059
 
  if (alias_name_used)
5060
 
    return;
5061
 
  if ((*ref)->type() == FIELD_ITEM)
5062
 
    alias_name_used= ((Item_ident *) (*ref))->alias_name_used;
5063
 
  else
5064
 
    alias_name_used= true; // it is not field, so it is was resolved by alias
5065
 
}
5066
 
 
5067
 
 
5068
 
void Item_ref::cleanup()
5069
 
{
5070
 
  Item_ident::cleanup();
5071
 
  result_field= 0;
5072
 
  return;
5073
 
}
5074
 
 
5075
 
 
5076
 
void Item_ref::print(String *str, enum_query_type query_type)
5077
 
{
5078
 
  if (ref)
5079
 
  {
5080
 
    if ((*ref)->type() != Item::CACHE_ITEM &&
5081
 
        !table_name && name && alias_name_used)
5082
 
    {
5083
 
      Session *session= current_session;
5084
 
      append_identifier(session, str, name, (uint) strlen(name));
5085
 
    }
5086
 
    else
5087
 
      (*ref)->print(str, query_type);
5088
 
  }
5089
 
  else
5090
 
    Item_ident::print(str, query_type);
5091
 
}
5092
 
 
5093
 
 
5094
 
bool Item_ref::send(Protocol *prot, String *tmp)
5095
 
{
5096
 
  if (result_field)
5097
 
    return prot->store(result_field);
5098
 
  return (*ref)->send(prot, tmp);
5099
 
}
5100
 
 
5101
 
 
5102
 
double Item_ref::val_result()
5103
 
{
5104
 
  if (result_field)
5105
 
  {
5106
 
    if ((null_value= result_field->is_null()))
5107
 
      return 0.0;
5108
 
    return result_field->val_real();
5109
 
  }
5110
 
  return val_real();
5111
 
}
5112
 
 
5113
 
 
5114
 
int64_t Item_ref::val_int_result()
5115
 
{
5116
 
  if (result_field)
5117
 
  {
5118
 
    if ((null_value= result_field->is_null()))
5119
 
      return 0;
5120
 
    return result_field->val_int();
5121
 
  }
5122
 
  return val_int();
5123
 
}
5124
 
 
5125
 
 
5126
 
String *Item_ref::str_result(String* str)
5127
 
{
5128
 
  if (result_field)
5129
 
  {
5130
 
    if ((null_value= result_field->is_null()))
5131
 
      return 0;
5132
 
    str->set_charset(str_value.charset());
5133
 
    return result_field->val_str(str, &str_value);
5134
 
  }
5135
 
  return val_str(str);
5136
 
}
5137
 
 
5138
 
 
5139
 
my_decimal *Item_ref::val_decimal_result(my_decimal *decimal_value)
5140
 
{
5141
 
  if (result_field)
5142
 
  {
5143
 
    if ((null_value= result_field->is_null()))
5144
 
      return 0;
5145
 
    return result_field->val_decimal(decimal_value);
5146
 
  }
5147
 
  return val_decimal(decimal_value);
5148
 
}
5149
 
 
5150
 
 
5151
 
bool Item_ref::val_bool_result()
5152
 
{
5153
 
  if (result_field)
5154
 
  {
5155
 
    if ((null_value= result_field->is_null()))
5156
 
      return 0;
5157
 
    switch (result_field->result_type()) {
5158
 
    case INT_RESULT:
5159
 
      return result_field->val_int() != 0;
5160
 
    case DECIMAL_RESULT:
5161
 
    {
5162
 
      my_decimal decimal_value;
5163
 
      my_decimal *val= result_field->val_decimal(&decimal_value);
5164
 
      if (val)
5165
 
        return !my_decimal_is_zero(val);
5166
 
      return 0;
5167
 
    }
5168
 
    case REAL_RESULT:
5169
 
    case STRING_RESULT:
5170
 
      return result_field->val_real() != 0.0;
5171
 
    case ROW_RESULT:
5172
 
    default:
5173
 
      assert(0);
5174
 
    }
5175
 
  }
5176
 
  return val_bool();
5177
 
}
5178
 
 
5179
 
 
5180
 
double Item_ref::val_real()
5181
 
{
5182
 
  assert(fixed);
5183
 
  double tmp=(*ref)->val_result();
5184
 
  null_value=(*ref)->null_value;
5185
 
  return tmp;
5186
 
}
5187
 
 
5188
 
 
5189
 
int64_t Item_ref::val_int()
5190
 
{
5191
 
  assert(fixed);
5192
 
  int64_t tmp=(*ref)->val_int_result();
5193
 
  null_value=(*ref)->null_value;
5194
 
  return tmp;
5195
 
}
5196
 
 
5197
 
 
5198
 
bool Item_ref::val_bool()
5199
 
{
5200
 
  assert(fixed);
5201
 
  bool tmp= (*ref)->val_bool_result();
5202
 
  null_value= (*ref)->null_value;
5203
 
  return tmp;
5204
 
}
5205
 
 
5206
 
 
5207
 
String *Item_ref::val_str(String* tmp)
5208
 
{
5209
 
  assert(fixed);
5210
 
  tmp=(*ref)->str_result(tmp);
5211
 
  null_value=(*ref)->null_value;
5212
 
  return tmp;
5213
 
}
5214
 
 
5215
 
 
5216
 
bool Item_ref::is_null()
5217
 
{
5218
 
  assert(fixed);
5219
 
  return (*ref)->is_null();
5220
 
}
5221
 
 
5222
 
 
5223
 
bool Item_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
5224
 
{
5225
 
  return (null_value=(*ref)->get_date_result(ltime,fuzzydate));
5226
 
}
5227
 
 
5228
 
 
5229
 
my_decimal *Item_ref::val_decimal(my_decimal *decimal_value)
5230
 
{
5231
 
  my_decimal *val= (*ref)->val_decimal_result(decimal_value);
5232
 
  null_value= (*ref)->null_value;
5233
 
  return val;
5234
 
}
5235
 
 
5236
 
int Item_ref::save_in_field(Field *to, bool no_conversions)
5237
 
{
5238
 
  int res;
5239
 
  assert(!result_field);
5240
 
  res= (*ref)->save_in_field(to, no_conversions);
5241
 
  null_value= (*ref)->null_value;
5242
 
  return res;
5243
 
}
5244
 
 
5245
 
 
5246
 
void Item_ref::save_org_in_field(Field *field)
5247
 
{
5248
 
  (*ref)->save_org_in_field(field);
5249
 
}
5250
 
 
5251
 
 
5252
 
void Item_ref::make_field(Send_field *field)
5253
 
{
5254
 
  (*ref)->make_field(field);
5255
 
  /* Non-zero in case of a view */
5256
 
  if (name)
5257
 
    field->col_name= name;
5258
 
  if (table_name)
5259
 
    field->table_name= table_name;
5260
 
  if (db_name)
5261
 
    field->db_name= db_name;
5262
 
}
5263
 
 
5264
 
 
5265
 
Item *Item_ref::get_tmp_table_item(Session *session)
5266
 
{
5267
 
  if (!result_field)
5268
 
    return (*ref)->get_tmp_table_item(session);
5269
 
 
5270
 
  Item_field *item= new Item_field(result_field);
5271
 
  if (item)
5272
 
  {
5273
 
    item->table_name= table_name;
5274
 
    item->db_name= db_name;
5275
 
  }
5276
 
  return item;
5277
 
}
5278
 
 
5279
 
 
5280
 
void Item_ref_null_helper::print(String *str, enum_query_type query_type)
5281
 
{
5282
 
  str->append(STRING_WITH_LEN("<ref_null_helper>("));
5283
 
  if (ref)
5284
 
    (*ref)->print(str, query_type);
5285
 
  else
5286
 
    str->append('?');
5287
 
  str->append(')');
5288
 
}
5289
 
 
5290
 
 
5291
 
double Item_direct_ref::val_real()
5292
 
{
5293
 
  double tmp=(*ref)->val_real();
5294
 
  null_value=(*ref)->null_value;
5295
 
  return tmp;
5296
 
}
5297
 
 
5298
 
 
5299
 
int64_t Item_direct_ref::val_int()
5300
 
{
5301
 
  int64_t tmp=(*ref)->val_int();
5302
 
  null_value=(*ref)->null_value;
5303
 
  return tmp;
5304
 
}
5305
 
 
5306
 
 
5307
 
String *Item_direct_ref::val_str(String* tmp)
5308
 
{
5309
 
  tmp=(*ref)->val_str(tmp);
5310
 
  null_value=(*ref)->null_value;
5311
 
  return tmp;
5312
 
}
5313
 
 
5314
 
 
5315
 
my_decimal *Item_direct_ref::val_decimal(my_decimal *decimal_value)
5316
 
{
5317
 
  my_decimal *tmp= (*ref)->val_decimal(decimal_value);
5318
 
  null_value=(*ref)->null_value;
5319
 
  return tmp;
5320
 
}
5321
 
 
5322
 
 
5323
 
bool Item_direct_ref::val_bool()
5324
 
{
5325
 
  bool tmp= (*ref)->val_bool();
5326
 
  null_value=(*ref)->null_value;
5327
 
  return tmp;
5328
 
}
5329
 
 
5330
 
 
5331
 
bool Item_direct_ref::is_null()
5332
 
{
5333
 
  return (*ref)->is_null();
5334
 
}
5335
 
 
5336
 
 
5337
 
bool Item_direct_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
5338
 
{
5339
 
  return (null_value=(*ref)->get_date(ltime,fuzzydate));
5340
 
}
5341
 
 
5342
 
/*
5343
 
  Prepare referenced outer field then call usual Item_direct_ref::fix_fields
5344
 
 
5345
 
  SYNOPSIS
5346
 
    Item_outer_ref::fix_fields()
5347
 
    session         thread handler
5348
 
    reference   reference on reference where this item stored
5349
 
 
5350
 
  RETURN
5351
 
    false   OK
5352
 
    true    Error
5353
 
*/
5354
 
 
5355
 
bool Item_outer_ref::fix_fields(Session *session, Item **reference)
5356
 
{
5357
 
  bool err;
5358
 
  /* outer_ref->check_cols() will be made in Item_direct_ref::fix_fields */
5359
 
  if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(session, reference)))
5360
 
    return true;
5361
 
  err= Item_direct_ref::fix_fields(session, reference);
5362
 
  if (!outer_ref)
5363
 
    outer_ref= *ref;
5364
 
  if ((*ref)->type() == Item::FIELD_ITEM)
5365
 
    table_name= ((Item_field*)outer_ref)->table_name;
5366
 
  return err;
5367
 
}
5368
 
 
5369
 
void Item_outer_ref::fix_after_pullout(st_select_lex *new_parent, Item **ref)
5370
 
{
5371
 
  if (depended_from == new_parent)
5372
 
  {
5373
 
    *ref= outer_ref;
5374
 
    outer_ref->fix_after_pullout(new_parent, ref);
5375
 
  }
5376
 
}
5377
 
 
5378
 
void Item_ref::fix_after_pullout(st_select_lex *new_parent, Item **)
5379
 
{
5380
 
  if (depended_from == new_parent)
5381
 
  {
5382
 
    (*ref)->fix_after_pullout(new_parent, ref);
5383
 
    depended_from= NULL;
5384
 
  }
5385
 
}
5386
 
 
5387
 
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
5388
 
{
5389
 
  return item->type() == DEFAULT_VALUE_ITEM &&
5390
 
    ((Item_default_value *)item)->arg->eq(arg, binary_cmp);
5391
 
}
5392
 
 
5393
 
 
5394
 
bool Item_default_value::fix_fields(Session *session, Item **)
5395
 
{
5396
 
  Item *real_arg;
5397
 
  Item_field *field_arg;
5398
 
  Field *def_field;
5399
 
  assert(fixed == 0);
5400
 
 
5401
 
  if (!arg)
5402
 
  {
5403
 
    fixed= 1;
5404
 
    return false;
5405
 
  }
5406
 
  if (!arg->fixed && arg->fix_fields(session, &arg))
5407
 
    goto error;
5408
 
 
5409
 
 
5410
 
  real_arg= arg->real_item();
5411
 
  if (real_arg->type() != FIELD_ITEM)
5412
 
  {
5413
 
    my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->name);
5414
 
    goto error;
5415
 
  }
5416
 
 
5417
 
  field_arg= (Item_field *)real_arg;
5418
 
  if (field_arg->field->flags & NO_DEFAULT_VALUE_FLAG)
5419
 
  {
5420
 
    my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), field_arg->field->field_name);
5421
 
    goto error;
5422
 
  }
5423
 
  if (!(def_field= (Field*) sql_alloc(field_arg->field->size_of())))
5424
 
    goto error;
5425
 
  memcpy(def_field, field_arg->field, field_arg->field->size_of());
5426
 
  def_field->move_field_offset((my_ptrdiff_t)
5427
 
                               (def_field->table->s->default_values -
5428
 
                                def_field->table->record[0]));
5429
 
  set_field(def_field);
5430
 
  return false;
5431
 
 
5432
 
error:
5433
 
  context->process_error(session);
5434
 
  return true;
5435
 
}
5436
 
 
5437
 
 
5438
 
void Item_default_value::print(String *str, enum_query_type query_type)
5439
 
{
5440
 
  if (!arg)
5441
 
  {
5442
 
    str->append(STRING_WITH_LEN("default"));
5443
 
    return;
5444
 
  }
5445
 
  str->append(STRING_WITH_LEN("default("));
5446
 
  arg->print(str, query_type);
5447
 
  str->append(')');
5448
 
}
5449
 
 
5450
 
 
5451
 
int Item_default_value::save_in_field(Field *field_arg, bool no_conversions)
5452
 
{
5453
 
  if (!arg)
5454
 
  {
5455
 
    if (field_arg->flags & NO_DEFAULT_VALUE_FLAG)
5456
 
    {
5457
 
      if (field_arg->reset())
5458
 
      {
5459
 
        my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
5460
 
                   ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
5461
 
        return -1;
5462
 
      }
5463
 
 
5464
 
      {
5465
 
        push_warning_printf(field_arg->table->in_use,
5466
 
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
5467
 
                            ER_NO_DEFAULT_FOR_FIELD,
5468
 
                            ER(ER_NO_DEFAULT_FOR_FIELD),
5469
 
                            field_arg->field_name);
5470
 
      }
5471
 
      return 1;
5472
 
    }
5473
 
    field_arg->set_default();
5474
 
    return 0;
5475
 
  }
5476
 
  return Item_field::save_in_field(field_arg, no_conversions);
5477
 
}
5478
 
 
5479
 
 
5480
 
/**
5481
 
  This method like the walk method traverses the item tree, but at the
5482
 
  same time it can replace some nodes in the tree.
5483
 
*/ 
5484
 
 
5485
 
Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
5486
 
{
5487
 
  Item *new_item= arg->transform(transformer, args);
5488
 
  if (!new_item)
5489
 
    return 0;
5490
 
 
5491
 
  /*
5492
 
    Session::change_item_tree() should be called only if the tree was
5493
 
    really transformed, i.e. when a new item has been created.
5494
 
    Otherwise we'll be allocating a lot of unnecessary memory for
5495
 
    change records at each execution.
5496
 
  */
5497
 
  if (arg != new_item)
5498
 
    current_session->change_item_tree(&arg, new_item);
5499
 
  return (this->*transformer)(args);
5500
 
}
5501
 
 
5502
 
 
5503
 
bool Item_insert_value::eq(const Item *item, bool binary_cmp) const
5504
 
{
5505
 
  return item->type() == INSERT_VALUE_ITEM &&
5506
 
    ((Item_default_value *)item)->arg->eq(arg, binary_cmp);
5507
 
}
5508
 
 
5509
 
 
5510
 
bool Item_insert_value::fix_fields(Session *session, Item **)
5511
 
{
5512
 
  assert(fixed == 0);
5513
 
  /* We should only check that arg is in first table */
5514
 
  if (!arg->fixed)
5515
 
  {
5516
 
    bool res;
5517
 
    TableList *orig_next_table= context->last_name_resolution_table;
5518
 
    context->last_name_resolution_table= context->first_name_resolution_table;
5519
 
    res= arg->fix_fields(session, &arg);
5520
 
    context->last_name_resolution_table= orig_next_table;
5521
 
    if (res)
5522
 
      return true;
5523
 
  }
5524
 
 
5525
 
  if (arg->type() == REF_ITEM)
5526
 
  {
5527
 
    Item_ref *ref= (Item_ref *)arg;
5528
 
    if (ref->ref[0]->type() != FIELD_ITEM)
5529
 
    {
5530
 
      my_error(ER_BAD_FIELD_ERROR, MYF(0), "", "VALUES() function");
5531
 
      return true;
5532
 
    }
5533
 
    arg= ref->ref[0];
5534
 
  }
5535
 
  /*
5536
 
    According to our SQL grammar, VALUES() function can reference
5537
 
    only to a column.
5538
 
  */
5539
 
  assert(arg->type() == FIELD_ITEM);
5540
 
 
5541
 
  Item_field *field_arg= (Item_field *)arg;
5542
 
 
5543
 
  if (field_arg->field->table->insert_values)
5544
 
  {
5545
 
    Field *def_field= (Field*) sql_alloc(field_arg->field->size_of());
5546
 
    if (!def_field)
5547
 
      return true;
5548
 
    memcpy(def_field, field_arg->field, field_arg->field->size_of());
5549
 
    def_field->move_field_offset((my_ptrdiff_t)
5550
 
                                 (def_field->table->insert_values -
5551
 
                                  def_field->table->record[0]));
5552
 
    set_field(def_field);
5553
 
  }
5554
 
  else
5555
 
  {
5556
 
    Field *tmp_field= field_arg->field;
5557
 
    /* charset doesn't matter here, it's to avoid sigsegv only */
5558
 
    tmp_field= new Field_null(0, 0, Field::NONE, field_arg->field->field_name,
5559
 
                          &my_charset_bin);
5560
 
    if (tmp_field)
5561
 
    {
5562
 
      tmp_field->init(field_arg->field->table);
5563
 
      set_field(tmp_field);
5564
 
    }
5565
 
  }
5566
 
  return false;
5567
 
}
5568
 
 
5569
 
void Item_insert_value::print(String *str, enum_query_type query_type)
5570
 
{
5571
 
  str->append(STRING_WITH_LEN("values("));
5572
 
  arg->print(str, query_type);
5573
 
  str->append(')');
5574
 
}
5575
 
 
 
1383
    client->store();
 
1384
}
 
1385
 
 
1386
uint32_t Item::max_char_length() const
 
1387
{
 
1388
  return max_length / collation.collation->mbmaxlen;
 
1389
}
 
1390
 
 
1391
void Item::fix_char_length(uint32_t max_char_length_arg)
 
1392
 
1393
  max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen);
 
1394
}
5576
1395
 
5577
1396
Item_result item_cmp_type(Item_result a,Item_result b)
5578
1397
{
5579
1398
  if (a == STRING_RESULT && b == STRING_RESULT)
5580
1399
    return STRING_RESULT;
 
1400
 
5581
1401
  if (a == INT_RESULT && b == INT_RESULT)
5582
1402
    return INT_RESULT;
5583
1403
  else if (a == ROW_RESULT || b == ROW_RESULT)
5584
1404
    return ROW_RESULT;
 
1405
 
5585
1406
  if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
5586
1407
      (b == INT_RESULT || b == DECIMAL_RESULT))
5587
1408
    return DECIMAL_RESULT;
 
1409
 
5588
1410
  return REAL_RESULT;
5589
1411
}
5590
1412
 
5591
 
 
5592
1413
void resolve_const_item(Session *session, Item **ref, Item *comp_item)
5593
1414
{
5594
1415
  Item *item= *ref;
5595
1416
  Item *new_item= NULL;
5596
1417
  if (item->basic_const_item())
5597
 
    return;                                     // Can't be better
5598
 
  Item_result res_type=item_cmp_type(comp_item->result_type(),
5599
 
                                     item->result_type());
5600
 
  char *name=item->name;                        // Alloced by sql_alloc
 
1418
    return; /* Can't be better */
 
1419
  Item_result res_type=item_cmp_type(comp_item->result_type(), item->result_type());
 
1420
  const char *name=item->name; /* Alloced by memory::sql_alloc */
5601
1421
 
5602
 
  switch (res_type) {
 
1422
  switch (res_type) 
 
1423
  {
5603
1424
  case STRING_RESULT:
5604
 
  {
5605
 
    char buff[MAX_FIELD_WIDTH];
5606
 
    String tmp(buff,sizeof(buff),&my_charset_bin),*result;
5607
 
    result=item->val_str(&tmp);
5608
 
    if (item->null_value)
5609
 
      new_item= new Item_null(name);
5610
 
    else
5611
1425
    {
5612
 
      uint32_t length= result->length();
5613
 
      char *tmp_str= sql_strmake(result->ptr(), length);
5614
 
      new_item= new Item_string(name, tmp_str, length, result->charset());
 
1426
      char buff[MAX_FIELD_WIDTH];
 
1427
      String tmp(buff,sizeof(buff),&my_charset_bin),*result;
 
1428
      result=item->val_str(&tmp);
 
1429
      if (item->null_value)
 
1430
        new_item= new Item_null(name);
 
1431
      else
 
1432
        new_item= new Item_string(name, memory::sql_strdup(*result), result->length(), result->charset());
 
1433
      break;
5615
1434
    }
5616
 
    break;
5617
 
  }
5618
1435
  case INT_RESULT:
5619
 
  {
5620
 
    int64_t result=item->val_int();
5621
 
    uint32_t length=item->max_length;
5622
 
    bool null_value=item->null_value;
5623
 
    new_item= (null_value ? (Item*) new Item_null(name) :
5624
 
               (Item*) new Item_int(name, result, length));
5625
 
    break;
5626
 
  }
 
1436
    {
 
1437
      new_item= item->null_value ? (Item*)new Item_null(name) : (Item*)new Item_int(name, item->val_int(), item->max_length);
 
1438
      break;
 
1439
    }
5627
1440
  case ROW_RESULT:
5628
 
  if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
5629
 
  {
5630
 
    /*
5631
 
      Substitute constants only in Item_rows. Don't affect other Items
5632
 
      with ROW_RESULT (eg Item_singlerow_subselect).
 
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).
5633
1446
 
5634
 
      For such Items more optimal is to detect if it is constant and replace
5635
 
      it with Item_row. This would optimize queries like this:
5636
 
      SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
5637
 
    */
5638
 
    Item_row *item_row= (Item_row*) item;
5639
 
    Item_row *comp_item_row= (Item_row*) comp_item;
5640
 
    uint32_t col;
5641
 
    new_item= 0;
5642
 
    /*
5643
 
      If item and comp_item are both Item_rows and have same number of cols
5644
 
      then process items in Item_row one by one.
5645
 
      We can't ignore NULL values here as this item may be used with <=>, in
5646
 
      which case NULL's are significant.
5647
 
    */
5648
 
    assert(item->result_type() == comp_item->result_type());
5649
 
    assert(item_row->cols() == comp_item_row->cols());
5650
 
    col= item_row->cols();
5651
 
    while (col-- > 0)
5652
 
      resolve_const_item(session, item_row->addr(col),
5653
 
                         comp_item_row->element_index(col));
5654
 
    break;
5655
 
  }
5656
 
  /* Fallthrough */
 
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;
 
1453
      new_item= 0;
 
1454
      /*
 
1455
        If item and comp_item are both Item_rows and have same number of cols
 
1456
        then process items in Item_row one by one.
 
1457
        We can't ignore NULL values here as this item may be used with <=>, in
 
1458
        which case NULL's are significant.
 
1459
      */
 
1460
      assert(item->result_type() == comp_item->result_type());
 
1461
      assert(item_row->cols() == comp_item_row->cols());
 
1462
      for (uint32_t col= item_row->cols(); col--; )
 
1463
        resolve_const_item(session, item_row->addr(col), comp_item_row->element_index(col));
 
1464
      break;
 
1465
    }
 
1466
    /* Fallthrough */
5657
1467
  case REAL_RESULT:
5658
 
  {                                             // It must REAL_RESULT
5659
 
    double result= item->val_real();
5660
 
    uint32_t length=item->max_length,decimals=item->decimals;
5661
 
    bool null_value=item->null_value;
5662
 
    new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
5663
 
               new Item_float(name, result, decimals, length));
5664
 
    break;
5665
 
  }
 
1468
    {                                           // It must REAL_RESULT
 
1469
      double result= item->val_real();
 
1470
      uint32_t length=item->max_length,decimals=item->decimals;
 
1471
      bool null_value=item->null_value;
 
1472
      new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
 
1473
                 new Item_float(name, result, decimals, length));
 
1474
      break;
 
1475
    }
5666
1476
  case DECIMAL_RESULT:
5667
 
  {
5668
 
    my_decimal decimal_value;
5669
 
    my_decimal *result= item->val_decimal(&decimal_value);
5670
 
    uint32_t length= item->max_length, decimals= item->decimals;
5671
 
    bool null_value= item->null_value;
5672
 
    new_item= (null_value ?
5673
 
               (Item*) new Item_null(name) :
5674
 
               (Item*) new Item_decimal(name, result, length, decimals));
5675
 
    break;
5676
 
  }
5677
 
  default:
5678
 
    assert(0);
5679
 
  }
 
1477
    {
 
1478
      type::Decimal decimal_value;
 
1479
      type::Decimal *result= item->val_decimal(&decimal_value);
 
1480
      uint32_t length= item->max_length, decimals= item->decimals;
 
1481
      bool null_value= item->null_value;
 
1482
      new_item= (null_value ?
 
1483
                 (Item*) new Item_null(name) :
 
1484
                 (Item*) new Item_decimal(name, result, length, decimals));
 
1485
      break;
 
1486
    }
 
1487
  }
 
1488
 
5680
1489
  if (new_item)
5681
 
    session->change_item_tree(ref, new_item);
 
1490
    *ref= new_item;
5682
1491
}
5683
1492
 
5684
 
/**
5685
 
  Return true if the value stored in the field is equal to the const
5686
 
  item.
5687
 
 
5688
 
  We need to use this on the range optimizer because in some cases
5689
 
  we can't store the value in the field without some precision/character loss.
5690
 
*/
5691
 
 
5692
1493
bool field_is_equal_to_item(Field *field,Item *item)
5693
1494
{
5694
1495
 
5703
1504
    item_result=item->val_str(&item_tmp);
5704
1505
    if (item->null_value)
5705
1506
      return 1;                                 // This must be true
5706
 
    field->val_str(&field_tmp);
5707
 
    return !stringcmp(&field_tmp,item_result);
 
1507
    field->val_str_internal(&field_tmp);
 
1508
    return not stringcmp(&field_tmp,item_result);
5708
1509
  }
 
1510
 
5709
1511
  if (res_type == INT_RESULT)
5710
1512
    return 1;                                   // Both where of type int
 
1513
 
5711
1514
  if (res_type == DECIMAL_RESULT)
5712
1515
  {
5713
 
    my_decimal item_buf, *item_val,
 
1516
    type::Decimal item_buf, *item_val,
5714
1517
               field_buf, *field_val;
5715
1518
    item_val= item->val_decimal(&item_buf);
5716
1519
    if (item->null_value)
5717
1520
      return 1;                                 // This must be true
5718
1521
    field_val= field->val_decimal(&field_buf);
5719
 
    return !my_decimal_cmp(item_val, field_val);
 
1522
    return !class_decimal_cmp(item_val, field_val);
5720
1523
  }
 
1524
 
5721
1525
  double result= item->val_real();
5722
1526
  if (item->null_value)
5723
1527
    return 1;
 
1528
 
5724
1529
  return result == field->val_real();
5725
1530
}
5726
1531
 
5727
 
Item_cache* Item_cache::get_cache(const Item *item)
5728
 
{
5729
 
  switch (item->result_type()) {
5730
 
  case INT_RESULT:
5731
 
    return new Item_cache_int();
5732
 
  case REAL_RESULT:
5733
 
    return new Item_cache_real();
5734
 
  case DECIMAL_RESULT:
5735
 
    return new Item_cache_decimal();
5736
 
  case STRING_RESULT:
5737
 
    return new Item_cache_str(item);
5738
 
  case ROW_RESULT:
5739
 
    return new Item_cache_row();
5740
 
  default:
5741
 
    // should never be in real life
5742
 
    assert(0);
5743
 
    return 0;
5744
 
  }
5745
 
}
5746
 
 
5747
 
 
5748
 
void Item_cache::print(String *str, enum_query_type query_type)
5749
 
{
5750
 
  str->append(STRING_WITH_LEN("<cache>("));
5751
 
  if (example)
5752
 
    example->print(str, query_type);
5753
 
  else
5754
 
    Item::print(str, query_type);
5755
 
  str->append(')');
5756
 
}
5757
 
 
5758
 
 
5759
 
bool Item_cache::eq_def(Field *field)
5760
 
{
5761
 
  return cached_field ? cached_field->eq_def (field) : false;
5762
 
}
5763
 
 
5764
 
 
5765
 
void Item_cache_int::store(Item *item)
5766
 
{
5767
 
  value= item->val_int_result();
5768
 
  null_value= item->null_value;
5769
 
  unsigned_flag= item->unsigned_flag;
5770
 
}
5771
 
 
5772
 
 
5773
 
void Item_cache_int::store(Item *item, int64_t val_arg)
5774
 
{
5775
 
  value= val_arg;
5776
 
  null_value= item->null_value;
5777
 
  unsigned_flag= item->unsigned_flag;
5778
 
}
5779
 
 
5780
 
 
5781
 
String *Item_cache_int::val_str(String *str)
5782
 
{
5783
 
  assert(fixed == 1);
5784
 
  str->set(value, default_charset());
5785
 
  return str;
5786
 
}
5787
 
 
5788
 
 
5789
 
my_decimal *Item_cache_int::val_decimal(my_decimal *decimal_val)
5790
 
{
5791
 
  assert(fixed == 1);
5792
 
  int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_val);
5793
 
  return decimal_val;
5794
 
}
5795
 
 
5796
 
 
5797
 
void Item_cache_real::store(Item *item)
5798
 
{
5799
 
  value= item->val_result();
5800
 
  null_value= item->null_value;
5801
 
}
5802
 
 
5803
 
 
5804
 
int64_t Item_cache_real::val_int()
5805
 
{
5806
 
  assert(fixed == 1);
5807
 
  return (int64_t) rint(value);
5808
 
}
5809
 
 
5810
 
 
5811
 
String* Item_cache_real::val_str(String *str)
5812
 
{
5813
 
  assert(fixed == 1);
5814
 
  str->set_real(value, decimals, default_charset());
5815
 
  return str;
5816
 
}
5817
 
 
5818
 
 
5819
 
my_decimal *Item_cache_real::val_decimal(my_decimal *decimal_val)
5820
 
{
5821
 
  assert(fixed == 1);
5822
 
  double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
5823
 
  return decimal_val;
5824
 
}
5825
 
 
5826
 
 
5827
 
void Item_cache_decimal::store(Item *item)
5828
 
{
5829
 
  my_decimal *val= item->val_decimal_result(&decimal_value);
5830
 
  if (!(null_value= item->null_value) && val != &decimal_value)
5831
 
    my_decimal2decimal(val, &decimal_value);
5832
 
}
5833
 
 
5834
 
double Item_cache_decimal::val_real()
5835
 
{
5836
 
  assert(fixed);
5837
 
  double res;
5838
 
  my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &res);
5839
 
  return res;
5840
 
}
5841
 
 
5842
 
int64_t Item_cache_decimal::val_int()
5843
 
{
5844
 
  assert(fixed);
5845
 
  int64_t res;
5846
 
  my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &res);
5847
 
  return res;
5848
 
}
5849
 
 
5850
 
String* Item_cache_decimal::val_str(String *str)
5851
 
{
5852
 
  assert(fixed);
5853
 
  my_decimal_round(E_DEC_FATAL_ERROR, &decimal_value, decimals, false,
5854
 
                   &decimal_value);
5855
 
  my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, str);
5856
 
  return str;
5857
 
}
5858
 
 
5859
 
my_decimal *Item_cache_decimal::val_decimal(my_decimal *)
5860
 
{
5861
 
  assert(fixed);
5862
 
  return &decimal_value;
5863
 
}
5864
 
 
5865
 
 
5866
 
Item_cache_str::Item_cache_str(const Item *item) :
5867
 
  Item_cache(), value(0),
5868
 
  is_varbinary(item->type() == FIELD_ITEM &&
5869
 
               ((const Item_field *) item)->field->type() ==
5870
 
               DRIZZLE_TYPE_VARCHAR &&
5871
 
               !((const Item_field *) item)->field->has_charset())
5872
 
{}
5873
 
 
5874
 
void Item_cache_str::store(Item *item)
5875
 
{
5876
 
  value_buff.set(buffer, sizeof(buffer), item->collation.collation);
5877
 
  value= item->str_result(&value_buff);
5878
 
  if ((null_value= item->null_value))
5879
 
    value= 0;
5880
 
  else if (value != &value_buff)
5881
 
  {
5882
 
    /*
5883
 
      We copy string value to avoid changing value if 'item' is table field
5884
 
      in queries like following (where t1.c is varchar):
5885
 
      select a, 
5886
 
             (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'),
5887
 
             (select c from t1 where a=t2.a)
5888
 
        from t2;
5889
 
    */
5890
 
    value_buff.copy(*value);
5891
 
    value= &value_buff;
5892
 
  }
5893
 
}
5894
 
 
5895
 
double Item_cache_str::val_real()
5896
 
{
5897
 
  assert(fixed == 1);
5898
 
  int err_not_used;
5899
 
  char *end_not_used;
5900
 
  if (value)
5901
 
    return my_strntod(value->charset(), (char*) value->ptr(),
5902
 
                      value->length(), &end_not_used, &err_not_used);
5903
 
  return (double) 0;
5904
 
}
5905
 
 
5906
 
 
5907
 
int64_t Item_cache_str::val_int()
5908
 
{
5909
 
  assert(fixed == 1);
5910
 
  int err;
5911
 
  if (value)
5912
 
    return my_strntoll(value->charset(), value->ptr(),
5913
 
                       value->length(), 10, (char**) 0, &err);
5914
 
  else
5915
 
    return (int64_t)0;
5916
 
}
5917
 
 
5918
 
my_decimal *Item_cache_str::val_decimal(my_decimal *decimal_val)
5919
 
{
5920
 
  assert(fixed == 1);
5921
 
  if (value)
5922
 
    string2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
5923
 
  else
5924
 
    decimal_val= 0;
5925
 
  return decimal_val;
5926
 
}
5927
 
 
5928
 
 
5929
 
int Item_cache_str::save_in_field(Field *field, bool no_conversions)
5930
 
{
5931
 
  int res= Item_cache::save_in_field(field, no_conversions);
5932
 
 
5933
 
  return res;
5934
 
}
5935
 
 
5936
 
 
5937
 
/**
5938
 
  Dummy error processor used by default by Name_resolution_context.
5939
 
 
5940
 
  @note
5941
 
    do nothing
5942
 
*/
5943
 
 
5944
 
void dummy_error_processor(Session *, void *)
5945
 
{}
5946
 
 
5947
1532
/**
5948
1533
  Create field for temporary table using type of given item.
5949
1534
 
5967
1552
  @retval
5968
1553
    new_created field
5969
1554
*/
5970
 
 
5971
1555
static Field *create_tmp_field_from_item(Session *,
5972
1556
                                         Item *item, Table *table,
5973
1557
                                         Item ***copy_func, bool modify_item,
5974
1558
                                         uint32_t convert_blob_length)
5975
1559
{
5976
1560
  bool maybe_null= item->maybe_null;
5977
 
  Field *new_field;
 
1561
  Field *new_field= NULL;
5978
1562
 
5979
1563
  switch (item->result_type()) {
5980
1564
  case REAL_RESULT:
5981
1565
    new_field= new Field_double(item->max_length, maybe_null,
5982
1566
                                item->name, item->decimals, true);
5983
1567
    break;
 
1568
 
5984
1569
  case INT_RESULT:
5985
 
    /* 
 
1570
    /*
5986
1571
      Select an integer type with the minimal fit precision.
5987
1572
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
5988
 
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into 
5989
 
      Field_long : make them Field_int64_t.  
 
1573
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
 
1574
      Int32 -> make them field::Int64.
5990
1575
    */
5991
 
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
5992
 
      new_field=new Field_int64_t(item->max_length, maybe_null,
5993
 
                                   item->name, item->unsigned_flag);
 
1576
    if (item->unsigned_flag)
 
1577
    {
 
1578
      new_field= new field::Size(item->max_length, maybe_null,
 
1579
                                  item->name, item->unsigned_flag);
 
1580
    }
 
1581
    else if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
 
1582
    {
 
1583
      new_field= new field::Int64(item->max_length, maybe_null,
 
1584
                                  item->name, item->unsigned_flag);
 
1585
    }
5994
1586
    else
5995
 
      new_field=new Field_long(item->max_length, maybe_null,
5996
 
                               item->name, item->unsigned_flag);
 
1587
    {
 
1588
      new_field= new field::Int32(item->max_length, maybe_null,
 
1589
                                  item->name, item->unsigned_flag);
 
1590
    }
 
1591
 
5997
1592
    break;
 
1593
 
5998
1594
  case STRING_RESULT:
5999
1595
    assert(item->collation.collation);
6000
 
  
6001
 
    enum enum_field_types type;
 
1596
 
6002
1597
    /*
6003
 
      DATE/TIME fields have STRING_RESULT result type. 
 
1598
      DATE/TIME fields have STRING_RESULT result type.
6004
1599
      To preserve type they needed to be handled separately.
6005
1600
    */
6006
 
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
6007
 
        type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_DATE ||
6008
 
        type == DRIZZLE_TYPE_TIMESTAMP)
 
1601
    if (field::isDateTime(item->field_type()))
 
1602
    {
6009
1603
      new_field= item->tmp_table_field_from_field_type(table, 1);
6010
 
    /* 
6011
 
      Make sure that the blob fits into a Field_varstring which has 
6012
 
      2-byte lenght. 
6013
 
    */
 
1604
      /*
 
1605
        Make sure that the blob fits into a Field_varstring which has
 
1606
        2-byte lenght.
 
1607
      */
 
1608
    }
6014
1609
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
6015
 
             convert_blob_length <= Field_varstring::MAX_SIZE && 
 
1610
             convert_blob_length <= Field_varstring::MAX_SIZE &&
6016
1611
             convert_blob_length)
 
1612
    {
 
1613
      table->setVariableWidth();
6017
1614
      new_field= new Field_varstring(convert_blob_length, maybe_null,
6018
 
                                     item->name, table->s,
6019
 
                                     item->collation.collation);
 
1615
                                     item->name, item->collation.collation);
 
1616
    }
6020
1617
    else
 
1618
    {
6021
1619
      new_field= item->make_string_field(table);
 
1620
    }
6022
1621
    new_field->set_derivation(item->collation.derivation);
6023
1622
    break;
 
1623
 
6024
1624
  case DECIMAL_RESULT:
6025
 
  {
6026
 
    uint8_t dec= item->decimals;
6027
 
    uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
6028
 
    uint32_t len= item->max_length;
6029
 
 
6030
 
    /*
6031
 
      Trying to put too many digits overall in a DECIMAL(prec,dec)
6032
 
      will always throw a warning. We must limit dec to
6033
 
      DECIMAL_MAX_SCALE however to prevent an assert() later.
6034
 
    */
6035
 
 
6036
 
    if (dec > 0)
6037
1625
    {
6038
 
      signed int overflow;
6039
 
 
6040
 
      dec= cmin(dec, (uint8_t)DECIMAL_MAX_SCALE);
 
1626
      uint8_t dec= item->decimals;
 
1627
      uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
 
1628
      uint32_t len= item->max_length;
6041
1629
 
6042
1630
      /*
6043
 
        If the value still overflows the field with the corrected dec,
6044
 
        we'll throw out decimals rather than integers. This is still
6045
 
        bad and of course throws a truncation warning.
6046
 
        +1: for decimal point
 
1631
        Trying to put too many digits overall in a DECIMAL(prec,dec)
 
1632
        will always throw a warning. We must limit dec to
 
1633
        DECIMAL_MAX_SCALE however to prevent an assert() later.
6047
1634
      */
6048
1635
 
6049
 
      overflow= my_decimal_precision_to_length(intg + dec, dec,
6050
 
                                               item->unsigned_flag) - len;
6051
 
 
6052
 
      if (overflow > 0)
6053
 
        dec= cmax(0, dec - overflow);            // too long, discard fract
6054
 
      else
6055
 
        len -= item->decimals - dec;            // corrected value fits
 
1636
      if (dec > 0)
 
1637
      {
 
1638
        signed int overflow;
 
1639
 
 
1640
        dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
 
1641
 
 
1642
        /*
 
1643
          If the value still overflows the field with the corrected dec,
 
1644
          we'll throw out decimals rather than integers. This is still
 
1645
          bad and of course throws a truncation warning.
 
1646
          +1: for decimal point
 
1647
        */
 
1648
 
 
1649
        overflow= class_decimal_precision_to_length(intg + dec, dec,
 
1650
                                                 item->unsigned_flag) - len;
 
1651
 
 
1652
        if (overflow > 0)
 
1653
          dec= max(0, dec - overflow);            // too long, discard fract
 
1654
        else
 
1655
          len-= item->decimals - dec;             // corrected value fits
 
1656
      }
 
1657
 
 
1658
      new_field= new Field_decimal(len,
 
1659
                                   maybe_null,
 
1660
                                   item->name,
 
1661
                                   dec,
 
1662
                                   item->unsigned_flag);
 
1663
      break;
6056
1664
    }
6057
1665
 
6058
 
    new_field= new Field_new_decimal(len, maybe_null, item->name,
6059
 
                                     dec, item->unsigned_flag);
6060
 
    break;
6061
 
  }
6062
1666
  case ROW_RESULT:
6063
 
  default:
6064
1667
    // This case should never be choosen
6065
1668
    assert(0);
6066
 
    new_field= 0;
6067
 
    break;
 
1669
    abort();
6068
1670
  }
 
1671
 
6069
1672
  if (new_field)
6070
1673
    new_field->init(table);
6071
 
    
 
1674
 
6072
1675
  if (copy_func && item->is_result_field())
6073
1676
    *((*copy_func)++) = item;                   // Save for copy_funcs
 
1677
 
6074
1678
  if (modify_item)
6075
1679
    item->set_result_field(new_field);
 
1680
 
6076
1681
  if (item->type() == Item::NULL_ITEM)
6077
1682
    new_field->is_created_from_null_item= true;
 
1683
 
6078
1684
  return new_field;
6079
1685
}
6080
1686
 
6081
 
Field *create_tmp_field(Session *session, Table *table,Item *item,
6082
 
                        Item::Type type, Item ***copy_func, Field **from_field,
6083
 
                        Field **default_field, bool group, bool modify_item,
6084
 
                        bool, bool make_copy_field,
 
1687
Field *create_tmp_field(Session *session,
 
1688
                        Table *table,
 
1689
                        Item *item,
 
1690
                        Item::Type type,
 
1691
                        Item ***copy_func,
 
1692
                        Field **from_field,
 
1693
                        Field **default_field,
 
1694
                        bool group,
 
1695
                        bool modify_item,
 
1696
                        bool make_copy_field,
6085
1697
                        uint32_t convert_blob_length)
6086
1698
{
6087
1699
  Field *result;
6123
1735
      *from_field= field->field;
6124
1736
      if (result && modify_item)
6125
1737
        field->result_field= result;
6126
 
    } 
 
1738
    }
6127
1739
    else
 
1740
    {
6128
1741
      result= create_tmp_field_from_field(session, (*from_field= field->field),
6129
1742
                                          orig_item ? orig_item->name :
6130
1743
                                          item->name,
6132
1745
                                          modify_item ? field :
6133
1746
                                          NULL,
6134
1747
                                          convert_blob_length);
 
1748
    }
6135
1749
    if (orig_type == Item::REF_ITEM && orig_modify)
6136
1750
      ((Item_ref*)orig_item)->set_result_field(result);
6137
1751
    if (field->field->eq_def(result))
6162
1776
    return create_tmp_field_from_item(session, item, table,
6163
1777
                                      (make_copy_field ? 0 : copy_func),
6164
1778
                                       modify_item, convert_blob_length);
6165
 
  case Item::TYPE_HOLDER:  
 
1779
  case Item::TYPE_HOLDER:
6166
1780
    result= ((Item_type_holder *)item)->make_field_by_type(table);
6167
1781
    result->set_derivation(item->collation.derivation);
6168
1782
    return result;
6169
1783
  default:                                      // Dosen't have to be stored
6170
 
    return 0;
6171
 
  }
6172
 
}
6173
 
 
6174
 
 
6175
 
/**
6176
 
  Wrapper of hide_view_error call for Name_resolution_context error
6177
 
  processor.
6178
 
 
6179
 
  @note
6180
 
    hide view underlying tables details in error messages
6181
 
*/
6182
 
 
6183
 
/*****************************************************************************
6184
 
** Instantiate templates
6185
 
*****************************************************************************/
6186
 
 
6187
 
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
6188
 
template class List<Item>;
6189
 
template class List_iterator<Item>;
6190
 
template class List_iterator_fast<Item>;
6191
 
template class List_iterator_fast<Item_field>;
6192
 
template class List<List_item>;
6193
 
#endif
 
1784
    return NULL;
 
1785
  }
 
1786
}
 
1787
 
 
1788
std::ostream& operator<<(std::ostream& output, const Item &item)
 
1789
{
 
1790
  switch (item.type())
 
1791
  {
 
1792
  case drizzled::Item::SUBSELECT_ITEM :
 
1793
  case drizzled::Item::FIELD_ITEM :
 
1794
  case drizzled::Item::SUM_FUNC_ITEM :
 
1795
  case drizzled::Item::STRING_ITEM :
 
1796
  case drizzled::Item::INT_ITEM :
 
1797
  case drizzled::Item::REAL_ITEM :
 
1798
  case drizzled::Item::NULL_ITEM :
 
1799
  case drizzled::Item::VARBIN_ITEM :
 
1800
  case drizzled::Item::COPY_STR_ITEM :
 
1801
  case drizzled::Item::FIELD_AVG_ITEM :
 
1802
  case drizzled::Item::DEFAULT_VALUE_ITEM :
 
1803
  case drizzled::Item::PROC_ITEM :
 
1804
  case drizzled::Item::COND_ITEM :
 
1805
  case drizzled::Item::REF_ITEM :
 
1806
  case drizzled::Item::FIELD_STD_ITEM :
 
1807
  case drizzled::Item::FIELD_VARIANCE_ITEM :
 
1808
  case drizzled::Item::INSERT_VALUE_ITEM :
 
1809
  case drizzled::Item::ROW_ITEM:
 
1810
  case drizzled::Item::CACHE_ITEM :
 
1811
  case drizzled::Item::TYPE_HOLDER :
 
1812
  case drizzled::Item::PARAM_ITEM :
 
1813
  case drizzled::Item::DECIMAL_ITEM :
 
1814
  case drizzled::Item::FUNC_ITEM :
 
1815
  case drizzled::Item::BOOLEAN_ITEM :
 
1816
    {
 
1817
      output << "Item:(";
 
1818
      output <<  item.full_name();
 
1819
      output << ", ";
 
1820
      output << drizzled::display::type(item.type());
 
1821
      output << ")";
 
1822
    }
 
1823
    break;
 
1824
  }
 
1825
 
 
1826
  return output;  // for multiple << operators.
 
1827
}
 
1828
 
 
1829
} /* namespace drizzled */