~drizzle-trunk/drizzle/development

642.1.1 by Lee
move functions from item.cc/item.h to item directory
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
642.1.1 by Lee
move functions from item.cc/item.h to item directory
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
20
#include <config.h>
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
21
642.1.1 by Lee
move functions from item.cc/item.h to item directory
22
#include <drizzled/session.h>
23
#include <drizzled/table.h>
24
#include <drizzled/error.h>
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
25
#include <drizzled/join.h>
642.1.1 by Lee
move functions from item.cc/item.h to item directory
26
#include <drizzled/sql_base.h>
27
#include <drizzled/sql_select.h>
28
#include <drizzled/item/cmpfunc.h>
29
#include <drizzled/item/field.h>
675 by Brian Aker
Cleanup around item includes.
30
#include <drizzled/item/outer_ref.h>
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
31
#include <drizzled/plugin/client.h>
2198.1.2 by Olaf van der Spek
Refactor includes
32
#include <drizzled/item/subselect.h>
2234.1.3 by Olaf van der Spek
Refactor includes
33
#include <drizzled/sql_lex.h>
642.1.1 by Lee
move functions from item.cc/item.h to item directory
34
1802.16.8 by Padraig O'Sullivan
Removal of all MyBitmap from the code base. Compiles but test failures exist now.
35
#include <boost/dynamic_bitset.hpp>
36
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
37
namespace drizzled
38
{
642.1.1 by Lee
move functions from item.cc/item.h to item directory
39
40
/**
41
  Store the pointer to this item field into a list if not already there.
42
43
  The method is used by Item::walk to collect all unique Item_field objects
44
  from a tree of Items into a set of items represented as a list.
45
46
  Item_cond::walk() and Item_func::walk() stop the evaluation of the
47
  processor function for its arguments once the processor returns
48
  true.Therefore in order to force this method being called for all item
49
  arguments in a condition the method must return false.
50
51
  @param arg  pointer to a List<Item_field>
52
53
  @return
54
    false to force the evaluation of collect_item_field_processor
55
    for the subsequent items.
56
*/
57
58
bool Item_field::collect_item_field_processor(unsigned char *arg)
59
{
60
  List<Item_field> *item_list= (List<Item_field>*) arg;
2183.2.11 by Olaf van der Spek
Use List::begin()
61
  List<Item_field>::iterator item_list_it(item_list->begin());
642.1.1 by Lee
move functions from item.cc/item.h to item directory
62
  Item_field *curr_item;
63
  while ((curr_item= item_list_it++))
64
  {
65
    if (curr_item->eq(this, 1))
66
      return(false); /* Already in the set. */
67
  }
68
  item_list->push_back(this);
69
  return(false);
70
}
71
72
73
/**
74
  Check if an Item_field references some field from a list of fields.
75
76
  Check whether the Item_field represented by 'this' references any
77
  of the fields in the keyparts passed via 'arg'. Used with the
78
  method Item::walk() to test whether any keypart in a sequence of
79
  keyparts is referenced in an expression.
80
81
  @param arg   Field being compared, arg must be of type Field
82
83
  @retval
84
    true  if 'this' references the field 'arg'
85
  @retval
86
    false otherwise
87
*/
88
89
bool Item_field::find_item_in_field_list_processor(unsigned char *arg)
90
{
1534 by Brian Aker
Remove of KeyPartInfo
91
  KeyPartInfo *first_non_group_part= *((KeyPartInfo **) arg);
92
  KeyPartInfo *last_part= *(((KeyPartInfo **) arg) + 1);
93
  KeyPartInfo *cur_part;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
94
95
  for (cur_part= first_non_group_part; cur_part != last_part; cur_part++)
96
  {
97
    if (field->eq(cur_part->field))
98
      return true;
99
  }
100
  return false;
101
}
102
103
104
/*
105
  Mark field in read_map
106
107
  NOTES
108
    This is used by filesort to register used fields in a a temporary
109
    column read set or to register used fields in a view
110
*/
111
112
bool Item_field::register_field_in_read_map(unsigned char *arg)
113
{
114
  Table *table= (Table *) arg;
1660.1.3 by Brian Aker
Encapsulate Table in field
115
  if (field->getTable() == table || !table)
1999.4.2 by Brian Aker
Encapsulate the field's position.
116
    field->getTable()->setReadSet(field->position());
1005.2.3 by Monty Taylor
Further reversion of P.
117
642.1.1 by Lee
move functions from item.cc/item.h to item directory
118
  return 0;
119
}
120
121
122
Item_field::Item_field(Field *f)
1672.3.1 by Brian Aker
Updates JP tests for clean IS. This also removes table name from Field so
123
  :Item_ident(0, NULL, f->getTable()->getAlias(), f->field_name),
642.1.1 by Lee
move functions from item.cc/item.h to item directory
124
   item_equal(0), no_const_subst(0),
125
   have_privileges(0), any_privileges(0)
126
{
127
  set_field(f);
128
  /*
129
    field_name and table_name should not point to garbage
130
    if this item is to be reused
131
  */
132
  orig_table_name= orig_field_name= "";
133
}
134
135
136
/**
137
  Constructor used inside setup_wild().
138
139
  Ensures that field, table, and database names will live as long as
140
  Item_field (this is important in prepared statements).
141
*/
142
1340.1.3 by Brian Aker
Lets hide stuff.
143
Item_field::Item_field(Session *,
144
                       Name_resolution_context *context_arg,
145
                       Field *f) :
146
  Item_ident(context_arg,
1660.1.3 by Brian Aker
Encapsulate Table in field
147
             f->getTable()->getShare()->getSchemaName(),
1672.3.1 by Brian Aker
Updates JP tests for clean IS. This also removes table name from Field so
148
             f->getTable()->getAlias(),
1340.1.3 by Brian Aker
Lets hide stuff.
149
             f->field_name),
150
   item_equal(0),
151
   no_const_subst(0),
152
   have_privileges(0),
153
   any_privileges(0)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
154
{
155
  set_field(f);
156
}
157
158
159
Item_field::Item_field(Name_resolution_context *context_arg,
160
                       const char *db_arg,const char *table_name_arg,
1340.1.3 by Brian Aker
Lets hide stuff.
161
                       const char *field_name_arg) :
162
  Item_ident(context_arg, db_arg,table_name_arg,field_name_arg),
163
   field(0),
164
   result_field(0),
165
   item_equal(0),
166
   no_const_subst(0),
167
   have_privileges(0),
168
   any_privileges(0)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
169
{
2227.4.8 by Olaf van der Spek
Session::lex()
170
  Select_Lex *select= getSession().lex().current_select;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
171
  collation.set(DERIVATION_IMPLICIT);
1340.1.3 by Brian Aker
Lets hide stuff.
172
642.1.1 by Lee
move functions from item.cc/item.h to item directory
173
  if (select && select->parsing_place != IN_HAVING)
174
      select->select_n_where_fields++;
175
}
176
177
/**
178
  Constructor need to process subselect with temporary tables (see Item)
179
*/
180
1340.1.3 by Brian Aker
Lets hide stuff.
181
Item_field::Item_field(Session *session, Item_field *item) :
182
  Item_ident(session, item),
183
  field(item->field),
184
  result_field(item->result_field),
185
  item_equal(item->item_equal),
186
  no_const_subst(item->no_const_subst),
187
  have_privileges(item->have_privileges),
188
  any_privileges(item->any_privileges)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
189
{
190
  collation.set(DERIVATION_IMPLICIT);
191
}
192
193
void Item_field::set_field(Field *field_par)
194
{
195
  field=result_field=field_par;			// for easy coding with fields
196
  maybe_null=field->maybe_null();
197
  decimals= field->decimals();
198
  max_length= field_par->max_display_length();
1672.3.1 by Brian Aker
Updates JP tests for clean IS. This also removes table name from Field so
199
  table_name= field_par->getTable()->getAlias();
642.1.1 by Lee
move functions from item.cc/item.h to item directory
200
  field_name= field_par->field_name;
1660.1.3 by Brian Aker
Encapsulate Table in field
201
  db_name= field_par->getTable()->getShare()->getSchemaName();
202
  alias_name_used= field_par->getTable()->alias_name_used;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
203
  unsigned_flag=test(field_par->flags & UNSIGNED_FLAG);
204
  collation.set(field_par->charset(), field_par->derivation());
205
  fixed= 1;
206
}
207
208
209
/**
210
  Reset this item to point to a field from the new temporary table.
211
  This is used when we create a new temporary table for each execution
212
  of prepared statement.
213
*/
214
215
void Item_field::reset_field(Field *f)
216
{
217
  set_field(f);
218
  /* 'name' is pointing at field->field_name of old field */
219
  name= (char*) f->field_name;
220
}
221
222
/* ARGSUSED */
223
String *Item_field::val_str(String *str)
224
{
225
  assert(fixed == 1);
226
  if ((null_value=field->is_null()))
227
    return 0;
228
  str->set_charset(str_value.charset());
229
  return field->val_str(str,&str_value);
230
}
231
232
233
double Item_field::val_real()
234
{
235
  assert(fixed == 1);
236
  if ((null_value=field->is_null()))
237
    return 0.0;
238
  return field->val_real();
239
}
240
241
242
int64_t Item_field::val_int()
243
{
244
  assert(fixed == 1);
245
  if ((null_value=field->is_null()))
246
    return 0;
247
  return field->val_int();
248
}
249
250
2030.1.4 by Brian Aker
Change my_decimal to Decimal
251
type::Decimal *Item_field::val_decimal(type::Decimal *decimal_value)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
252
{
253
  if ((null_value= field->is_null()))
254
    return 0;
255
  return field->val_decimal(decimal_value);
256
}
257
258
259
String *Item_field::str_result(String *str)
260
{
261
  if ((null_value=result_field->is_null()))
262
    return 0;
263
  str->set_charset(str_value.charset());
264
  return result_field->val_str(str,&str_value);
265
}
266
2104.2.8 by Brian Aker
Merge in reference from pointer.
267
bool Item_field::get_date(type::Time &ltime,uint32_t fuzzydate)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
268
{
269
  if ((null_value=field->is_null()) || field->get_date(ltime,fuzzydate))
270
  {
2104.2.8 by Brian Aker
Merge in reference from pointer.
271
    ltime.reset();
642.1.1 by Lee
move functions from item.cc/item.h to item directory
272
    return 1;
273
  }
274
  return 0;
275
}
276
2104.2.8 by Brian Aker
Merge in reference from pointer.
277
bool Item_field::get_date_result(type::Time &ltime,uint32_t fuzzydate)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
278
{
279
  if ((null_value=result_field->is_null()) ||
280
      result_field->get_date(ltime,fuzzydate))
281
  {
2104.2.8 by Brian Aker
Merge in reference from pointer.
282
    ltime.reset();
642.1.1 by Lee
move functions from item.cc/item.h to item directory
283
    return 1;
284
  }
285
  return 0;
286
}
287
2104.2.8 by Brian Aker
Merge in reference from pointer.
288
bool Item_field::get_time(type::Time &ltime)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
289
{
290
  if ((null_value=field->is_null()) || field->get_time(ltime))
291
  {
2104.2.8 by Brian Aker
Merge in reference from pointer.
292
    ltime.reset();
642.1.1 by Lee
move functions from item.cc/item.h to item directory
293
    return 1;
294
  }
295
  return 0;
296
}
297
298
double Item_field::val_result()
299
{
300
  if ((null_value=result_field->is_null()))
301
    return 0.0;
302
  return result_field->val_real();
303
}
304
305
int64_t Item_field::val_int_result()
306
{
307
  if ((null_value=result_field->is_null()))
308
    return 0;
309
  return result_field->val_int();
310
}
311
312
2030.1.4 by Brian Aker
Change my_decimal to Decimal
313
type::Decimal *Item_field::val_decimal_result(type::Decimal *decimal_value)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
314
{
315
  if ((null_value= result_field->is_null()))
316
    return 0;
317
  return result_field->val_decimal(decimal_value);
318
}
319
320
321
bool Item_field::val_bool_result()
322
{
323
  if ((null_value= result_field->is_null()))
2085.2.3 by Brian Aker
Fix strcasecmp issues (ie, check UTF-8).
324
  {
642.1.1 by Lee
move functions from item.cc/item.h to item directory
325
    return false;
2085.2.3 by Brian Aker
Fix strcasecmp issues (ie, check UTF-8).
326
  }
327
642.1.1 by Lee
move functions from item.cc/item.h to item directory
328
  switch (result_field->result_type()) {
329
  case INT_RESULT:
330
    return result_field->val_int() != 0;
2008 by Brian Aker
Formatting + remove default from switch/case.
331
642.1.1 by Lee
move functions from item.cc/item.h to item directory
332
  case DECIMAL_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
333
    {
2030.1.4 by Brian Aker
Change my_decimal to Decimal
334
      type::Decimal decimal_value;
335
      type::Decimal *val= result_field->val_decimal(&decimal_value);
2008 by Brian Aker
Formatting + remove default from switch/case.
336
      if (val)
2137.1.5 by Brian Aker
Additional decimal.
337
        return not val->isZero();
2008 by Brian Aker
Formatting + remove default from switch/case.
338
      return 0;
339
    }
340
642.1.1 by Lee
move functions from item.cc/item.h to item directory
341
  case REAL_RESULT:
342
  case STRING_RESULT:
343
    return result_field->val_real() != 0.0;
2008 by Brian Aker
Formatting + remove default from switch/case.
344
642.1.1 by Lee
move functions from item.cc/item.h to item directory
345
  case ROW_RESULT:
346
    assert(0);
347
    return 0;                                   // Shut up compiler
348
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
349
350
  assert(0);
351
  return 0;                                   // Shut up compiler
642.1.1 by Lee
move functions from item.cc/item.h to item directory
352
}
353
354
355
bool Item_field::eq(const Item *item, bool) const
356
{
779.3.10 by Monty Taylor
Turned on -Wshadow.
357
  const Item *item_ptr= item->real_item();
358
  if (item_ptr->type() != FIELD_ITEM)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
359
    return 0;
360
779.3.10 by Monty Taylor
Turned on -Wshadow.
361
  const Item_field *item_field= static_cast<const Item_field *>(item_ptr);
642.1.1 by Lee
move functions from item.cc/item.h to item directory
362
  if (item_field->field && field)
363
    return item_field->field == field;
364
  /*
365
    We may come here when we are trying to find a function in a GROUP BY
366
    clause from the select list.
367
    In this case the '100 % correct' way to do this would be to first
368
    run fix_fields() on the GROUP BY item and then retry this function, but
369
    I think it's better to relax the checking a bit as we will in
370
    most cases do the correct thing by just checking the field name.
371
    (In cases where we would choose wrong we would have to generate a
372
    ER_NON_UNIQ_ERROR).
373
  */
2085.2.3 by Brian Aker
Fix strcasecmp issues (ie, check UTF-8).
374
  return (not my_strcasecmp(system_charset_info, item_field->name, field_name) &&
375
          (not item_field->table_name || not table_name ||
376
           (not my_strcasecmp(table_alias_charset, item_field->table_name, table_name) &&
377
            (not item_field->db_name || not db_name ||
378
             (item_field->db_name && not my_strcasecmp(system_charset_info, item_field->db_name, db_name))))));
642.1.1 by Lee
move functions from item.cc/item.h to item directory
379
}
380
381
382
table_map Item_field::used_tables() const
383
{
1660.1.3 by Brian Aker
Encapsulate Table in field
384
  if (field->getTable()->const_table)
2085.2.3 by Brian Aker
Fix strcasecmp issues (ie, check UTF-8).
385
  {
642.1.1 by Lee
move functions from item.cc/item.h to item directory
386
    return 0;					// const item
2085.2.3 by Brian Aker
Fix strcasecmp issues (ie, check UTF-8).
387
  }
388
1660.1.3 by Brian Aker
Encapsulate Table in field
389
  return (depended_from ? OUTER_REF_TABLE_BIT : field->getTable()->map);
642.1.1 by Lee
move functions from item.cc/item.h to item directory
390
}
391
392
enum Item_result Item_field::result_type () const
393
{
394
  return field->result_type();
395
}
396
397
398
Item_result Item_field::cast_to_int_type() const
399
{
400
  return field->cast_to_int_type();
401
}
402
403
404
enum_field_types Item_field::field_type() const
405
{
406
  return field->type();
407
}
408
409
846 by Brian Aker
Removing on typedeffed class.
410
void Item_field::fix_after_pullout(Select_Lex *new_parent, Item **)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
411
{
412
  if (new_parent == depended_from)
413
    depended_from= NULL;
414
  Name_resolution_context *ctx= new Name_resolution_context();
415
  ctx->outer_context= NULL; // We don't build a complete name resolver
416
  ctx->select_lex= new_parent;
417
  ctx->first_name_resolution_table= context->first_name_resolution_table;
418
  ctx->last_name_resolution_table=  context->last_name_resolution_table;
419
  this->context=ctx;
420
}
421
422
423
bool Item_field::is_null()
424
{
425
  return field->is_null();
426
}
427
428
429
Item *Item_field::get_tmp_table_item(Session *session)
430
{
431
  Item_field *new_item= new Item_field(session, this);
432
  if (new_item)
433
    new_item->field= new_item->result_field;
434
  return new_item;
435
}
436
437
int64_t Item_field::val_int_endpoint(bool, bool *)
438
{
439
  int64_t res= val_int();
440
  return null_value? INT64_MIN : res;
441
}
442
443
444
/**
445
  Resolve the name of an outer select column reference.
446
447
  The method resolves the column reference represented by 'this' as a column
448
  present in outer selects that contain current select.
449
450
  In prepared statements, because of cache, find_field_in_tables()
451
  can resolve fields even if they don't belong to current context.
452
  In this case this method only finds appropriate context and marks
453
  current select as dependent. The found reference of field should be
454
  provided in 'from_field'.
455
456
  @param[in] session             current thread
457
  @param[in,out] from_field  found field reference or (Field*)not_found_field
458
  @param[in,out] reference   view column if this item was resolved to a
459
    view column
460
461
  @note
462
    This is the inner loop of Item_field::fix_fields:
463
  @code
464
        for each outer query Q_k beginning from the inner-most one
465
        {
466
          search for a column or derived column named col_ref_i
467
          [in table T_j] in the FROM clause of Q_k;
468
469
          if such a column is not found
470
            Search for a column or derived column named col_ref_i
471
            [in table T_j] in the SELECT and GROUP clauses of Q_k.
472
        }
473
  @endcode
474
475
  @retval
476
    1   column succefully resolved and fix_fields() should continue.
477
  @retval
478
    0   column fully fixed and fix_fields() should return false
479
  @retval
480
    -1  error occured
481
*/
482
483
int
484
Item_field::fix_outer_field(Session *session, Field **from_field, Item **reference)
485
{
486
  enum_parsing_place place= NO_MATTER;
487
  bool field_found= (*from_field != not_found_field);
488
  bool upward_lookup= false;
489
490
  /*
491
    If there are outer contexts (outer selects, but current select is
492
    not derived table or view) try to resolve this reference in the
493
    outer contexts.
494
495
    We treat each subselect as a separate namespace, so that different
496
    subselects may contain columns with the same names. The subselects
497
    are searched starting from the innermost.
498
  */
499
  Name_resolution_context *last_checked_context= context;
500
  Item **ref= (Item **) not_found_item;
2227.4.8 by Olaf van der Spek
Session::lex()
501
  Select_Lex *current_sel= (Select_Lex *) session->lex().current_select;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
502
  Name_resolution_context *outer_context= 0;
846 by Brian Aker
Removing on typedeffed class.
503
  Select_Lex *select= 0;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
504
  /* Currently derived tables cannot be correlated */
505
  if (current_sel->master_unit()->first_select()->linkage !=
506
      DERIVED_TABLE_TYPE)
507
    outer_context= context->outer_context;
508
  for (;
509
       outer_context;
510
       outer_context= outer_context->outer_context)
511
  {
512
    select= outer_context->select_lex;
513
    Item_subselect *prev_subselect_item=
514
      last_checked_context->select_lex->master_unit()->item;
515
    last_checked_context= outer_context;
516
    upward_lookup= true;
517
518
    place= prev_subselect_item->parsing_place;
519
    /*
520
      If outer_field is set, field was already found by first call
521
      to find_field_in_tables(). Only need to find appropriate context.
522
    */
523
    if (field_found && outer_context->select_lex !=
524
        cached_table->select_lex)
525
      continue;
526
    /*
527
      In case of a view, find_field_in_tables() writes the pointer to
528
      the found view field into '*reference', in other words, it
529
      substitutes this Item_field with the found expression.
530
    */
531
    if (field_found || (*from_field= find_field_in_tables(session, this,
532
                                          outer_context->
533
                                            first_name_resolution_table,
534
                                          outer_context->
535
                                            last_name_resolution_table,
536
                                          reference,
537
                                          IGNORE_EXCEPT_NON_UNIQUE,
1113.1.1 by Brian Aker
Dead code removal around LCOV finds.
538
                                          true)) !=
642.1.1 by Lee
move functions from item.cc/item.h to item directory
539
        not_found_field)
540
    {
541
      if (*from_field)
542
      {
543
        if (*from_field != view_ref_found)
544
        {
1660.1.3 by Brian Aker
Encapsulate Table in field
545
          prev_subselect_item->used_tables_cache|= (*from_field)->getTable()->map;
2060 by Brian Aker
Added native functions into the function table.
546
          prev_subselect_item->const_item_cache= false;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
547
          set_field(*from_field);
548
          if (!last_checked_context->select_lex->having_fix_field &&
549
              select->group_list.elements &&
550
              (place == SELECT_LIST || place == IN_HAVING))
551
          {
552
            Item_outer_ref *rf;
553
            /*
554
              If an outer field is resolved in a grouping select then it
555
              is replaced for an Item_outer_ref object. Otherwise an
556
              Item_field object is used.
557
              The new Item_outer_ref object is saved in the inner_refs_list of
558
              the outer select. Here it is only created. It can be fixed only
559
              after the original field has been fixed and this is done in the
560
              fix_inner_refs() function.
561
            */
562
            ;
563
            if (!(rf= new Item_outer_ref(context, this)))
564
              return -1;
2197.3.1 by Olaf van der Spek
Remove Session::change_item_tree
565
            *reference= rf;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
566
            select->inner_refs_list.push_back(rf);
2227.4.8 by Olaf van der Spek
Session::lex()
567
            rf->in_sum_func= session->lex().in_sum_func;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
568
          }
569
          /*
570
            A reference is resolved to a nest level that's outer or the same as
571
            the nest level of the enclosing set function : adjust the value of
572
            max_arg_level for the function if it's needed.
573
          */
2227.4.8 by Olaf van der Spek
Session::lex()
574
          if (session->lex().in_sum_func &&
575
              session->lex().in_sum_func->nest_level >= select->nest_level)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
576
          {
577
            Item::Type ref_type= (*reference)->type();
2227.4.8 by Olaf van der Spek
Session::lex()
578
            set_if_bigger(session->lex().in_sum_func->max_arg_level,
642.1.1 by Lee
move functions from item.cc/item.h to item directory
579
                          select->nest_level);
580
            set_field(*from_field);
581
            fixed= 1;
582
            mark_as_dependent(session, last_checked_context->select_lex,
583
                              context->select_lex, this,
584
                              ((ref_type == REF_ITEM ||
585
                                ref_type == FIELD_ITEM) ?
586
                               (Item_ident*) (*reference) : 0));
587
            return 0;
588
          }
589
        }
590
        else
591
        {
592
          Item::Type ref_type= (*reference)->type();
593
          prev_subselect_item->used_tables_cache|=
594
            (*reference)->used_tables();
595
          prev_subselect_item->const_item_cache&=
596
            (*reference)->const_item();
597
          mark_as_dependent(session, last_checked_context->select_lex,
598
                            context->select_lex, this,
599
                            ((ref_type == REF_ITEM || ref_type == FIELD_ITEM) ?
600
                             (Item_ident*) (*reference) :
601
                             0));
602
          /*
603
            A reference to a view field had been found and we
604
            substituted it instead of this Item (find_field_in_tables
605
            does it by assigning the new value to *reference), so now
606
            we can return from this function.
607
          */
608
          return 0;
609
        }
610
      }
611
      break;
612
    }
613
614
    /* Search in SELECT and GROUP lists of the outer select. */
615
    if (place != IN_WHERE && place != IN_ON)
616
    {
617
      if (!(ref= resolve_ref_in_select_and_group(session, this, select)))
618
        return -1; /* Some error occurred (e.g. ambiguous names). */
619
      if (ref != not_found_item)
620
      {
621
        assert(*ref && (*ref)->fixed);
622
        prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
623
        prev_subselect_item->const_item_cache&= (*ref)->const_item();
624
        break;
625
      }
626
    }
627
628
    /*
629
      Reference is not found in this select => this subquery depend on
630
      outer select (or we just trying to find wrong identifier, in this
631
      case it does not matter which used tables bits we set)
632
    */
633
    prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
2060 by Brian Aker
Added native functions into the function table.
634
    prev_subselect_item->const_item_cache= false;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
635
  }
636
637
  assert(ref != 0);
638
  if (!*from_field)
639
    return -1;
640
  if (ref == not_found_item && *from_field == not_found_field)
641
  {
642
    if (upward_lookup)
643
    {
644
      // We can't say exactly what absent table or field
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
645
      my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), session->where());
642.1.1 by Lee
move functions from item.cc/item.h to item directory
646
    }
647
    else
648
    {
649
      /* Call find_field_in_tables only to report the error */
650
      find_field_in_tables(session, this,
651
                           context->first_name_resolution_table,
652
                           context->last_name_resolution_table,
1113.1.1 by Brian Aker
Dead code removal around LCOV finds.
653
                           reference, REPORT_ALL_ERRORS, true);
642.1.1 by Lee
move functions from item.cc/item.h to item directory
654
    }
655
    return -1;
656
  }
657
  else if (ref != not_found_item)
658
  {
659
    Item *save;
660
    Item_ref *rf;
661
662
    /* Should have been checked in resolve_ref_in_select_and_group(). */
663
    assert(*ref && (*ref)->fixed);
664
    /*
665
      Here, a subset of actions performed by Item_ref::set_properties
666
      is not enough. So we pass ptr to NULL into Item_[direct]_ref
660.1.6 by Eric Herman
trailing whitespace fixup
667
      constructor, so no initialization is performed, and call
642.1.1 by Lee
move functions from item.cc/item.h to item directory
668
      fix_fields() below.
669
    */
670
    save= *ref;
671
    *ref= NULL;                             // Don't call set_properties()
672
    rf= (place == IN_HAVING ?
673
         new Item_ref(context, ref, (char*) table_name,
674
                      (char*) field_name, alias_name_used) :
675
         (!select->group_list.elements ?
676
         new Item_direct_ref(context, ref, (char*) table_name,
677
                             (char*) field_name, alias_name_used) :
678
         new Item_outer_ref(context, ref, (char*) table_name,
679
                            (char*) field_name, alias_name_used)));
680
    *ref= save;
681
    if (!rf)
682
      return -1;
683
684
    if (place != IN_HAVING && select->group_list.elements)
685
    {
686
      outer_context->select_lex->inner_refs_list.push_back((Item_outer_ref*)rf);
2227.4.8 by Olaf van der Spek
Session::lex()
687
      ((Item_outer_ref*)rf)->in_sum_func= session->lex().in_sum_func;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
688
    }
2197.3.1 by Olaf van der Spek
Remove Session::change_item_tree
689
    *reference= rf;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
690
    /*
691
      rf is Item_ref => never substitute other items (in this case)
692
      during fix_fields() => we can use rf after fix_fields()
693
    */
694
    assert(!rf->fixed);                // Assured by Item_ref()
695
    if (rf->fix_fields(session, reference) || rf->check_cols(1))
696
      return -1;
697
698
    mark_as_dependent(session, last_checked_context->select_lex,
699
                      context->select_lex, this,
700
                      rf);
701
    return 0;
702
  }
703
  else
704
  {
705
    mark_as_dependent(session, last_checked_context->select_lex,
706
                      context->select_lex,
707
                      this, (Item_ident*)*reference);
708
    if (last_checked_context->select_lex->having_fix_field)
709
    {
710
      Item_ref *rf;
711
      rf= new Item_ref(context,
1874.1.1 by Brian Aker
Encapsulate schema_name it table_list.
712
                       (cached_table->getSchemaName()[0] ? cached_table->getSchemaName() : 0),
642.1.1 by Lee
move functions from item.cc/item.h to item directory
713
                       (char*) cached_table->alias, (char*) field_name);
714
      if (!rf)
715
        return -1;
2197.3.1 by Olaf van der Spek
Remove Session::change_item_tree
716
      *reference= rf;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
717
      /*
718
        rf is Item_ref => never substitute other items (in this case)
719
        during fix_fields() => we can use rf after fix_fields()
720
      */
721
      assert(!rf->fixed);                // Assured by Item_ref()
722
      if (rf->fix_fields(session, reference) || rf->check_cols(1))
723
        return -1;
724
      return 0;
725
    }
726
  }
727
  return 1;
728
}
729
730
731
/**
732
  Resolve the name of a column reference.
733
734
  The method resolves the column reference represented by 'this' as a column
735
  present in one of: FROM clause, SELECT clause, GROUP BY clause of a query
736
  Q, or in outer queries that contain Q.
737
738
  The name resolution algorithm used is (where [T_j] is an optional table
739
  name that qualifies the column name):
740
741
  @code
742
    resolve_column_reference([T_j].col_ref_i)
743
    {
744
      search for a column or derived column named col_ref_i
745
      [in table T_j] in the FROM clause of Q;
746
747
      if such a column is NOT found AND    // Lookup in outer queries.
748
         there are outer queries
749
      {
750
        for each outer query Q_k beginning from the inner-most one
751
        {
752
          search for a column or derived column named col_ref_i
753
          [in table T_j] in the FROM clause of Q_k;
754
755
          if such a column is not found
756
            Search for a column or derived column named col_ref_i
757
            [in table T_j] in the SELECT and GROUP clauses of Q_k.
758
        }
759
      }
760
    }
761
  @endcode
762
763
    Notice that compared to Item_ref::fix_fields, here we first search the FROM
764
    clause, and then we search the SELECT and GROUP BY clauses.
765
766
  @param[in]     session        current thread
767
  @param[in,out] reference  view column if this item was resolved to a
768
    view column
769
770
  @retval
771
    true  if error
772
  @retval
773
    false on success
774
*/
775
776
bool Item_field::fix_fields(Session *session, Item **reference)
777
{
778
  assert(fixed == 0);
779
  Field *from_field= (Field *)not_found_field;
780
  bool outer_fixed= false;
781
782
  if (!field)					// If field is not checked
783
  {
784
    /*
785
      In case of view, find_field_in_tables() write pointer to view field
786
      expression to 'reference', i.e. it substitute that expression instead
787
      of this Item_field
788
    */
789
    if ((from_field= find_field_in_tables(session, this,
790
                                          context->first_name_resolution_table,
791
                                          context->last_name_resolution_table,
792
                                          reference,
2227.4.8 by Olaf van der Spek
Session::lex()
793
                                          session->lex().use_only_table_context ?
660.1.6 by Eric Herman
trailing whitespace fixup
794
                                            REPORT_ALL_ERRORS :
1113.1.1 by Brian Aker
Dead code removal around LCOV finds.
795
                                            IGNORE_EXCEPT_NON_UNIQUE, true)) ==
796
        not_found_field)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
797
    {
798
      int ret;
799
      /* Look up in current select's item_list to find aliased fields */
2227.4.8 by Olaf van der Spek
Session::lex()
800
      if (session->lex().current_select->is_item_list_lookup)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
801
      {
802
        uint32_t counter;
803
        enum_resolution_type resolution;
1578.6.3 by Brian Aker
More current_session removal.
804
        Item** res= find_item_in_list(session,
2227.4.8 by Olaf van der Spek
Session::lex()
805
                                      this, session->lex().current_select->item_list,
642.1.1 by Lee
move functions from item.cc/item.h to item directory
806
                                      &counter, REPORT_EXCEPT_NOT_FOUND,
807
                                      &resolution);
808
        if (!res)
809
          return 1;
810
        if (resolution == RESOLVED_AGAINST_ALIAS)
811
          alias_name_used= true;
812
        if (res != (Item **)not_found_item)
813
        {
814
          if ((*res)->type() == Item::FIELD_ITEM)
815
          {
816
            /*
817
              It's an Item_field referencing another Item_field in the select
818
              list.
819
              Use the field from the Item_field in the select list and leave
820
              the Item_field instance in place.
821
            */
822
823
            Field *new_field= (*((Item_field**)res))->field;
824
825
            if (new_field == NULL)
826
            {
827
              /* The column to which we link isn't valid. */
660.1.6 by Eric Herman
trailing whitespace fixup
828
              my_error(ER_BAD_FIELD_ERROR, MYF(0), (*res)->name,
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
829
                       session->where());
642.1.1 by Lee
move functions from item.cc/item.h to item directory
830
              return(1);
831
            }
832
833
            set_field(new_field);
834
            return 0;
835
          }
836
          else
837
          {
838
            /*
839
              It's not an Item_field in the select list so we must make a new
840
              Item_ref to point to the Item in the select list and replace the
841
              Item_field created by the parser with the new Item_ref.
842
            */
843
            Item_ref *rf= new Item_ref(context, db_name,table_name,field_name);
844
            if (!rf)
845
              return 1;
2197.3.1 by Olaf van der Spek
Remove Session::change_item_tree
846
            *reference= rf;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
847
            /*
660.1.6 by Eric Herman
trailing whitespace fixup
848
              Because Item_ref never substitutes itself with other items
849
              in Item_ref::fix_fields(), we can safely use the original
642.1.1 by Lee
move functions from item.cc/item.h to item directory
850
              pointer to it even after fix_fields()
851
             */
852
            return rf->fix_fields(session, reference) ||  rf->check_cols(1);
853
          }
854
        }
855
      }
856
      if ((ret= fix_outer_field(session, &from_field, reference)) < 0)
857
        goto error;
858
      outer_fixed= true;
859
      if (!ret)
860
        goto mark_non_agg_field;
861
    }
862
    else if (!from_field)
863
      goto error;
864
865
    if (!outer_fixed && cached_table && cached_table->select_lex &&
866
        context->select_lex &&
867
        cached_table->select_lex != context->select_lex)
868
    {
869
      int ret;
870
      if ((ret= fix_outer_field(session, &from_field, reference)) < 0)
871
        goto error;
872
      outer_fixed= 1;
873
      if (!ret)
874
        goto mark_non_agg_field;
875
    }
876
877
    /*
878
      if it is not expression from merged VIEW we will set this field.
879
880
      We can leave expression substituted from view for next PS/SP rexecution
881
      (i.e. do not register this substitution for reverting on cleanup()
882
      (register_item_tree_changing())), because this subtree will be
883
      fix_field'ed during setup_tables()->setup_underlying() (i.e. before
884
      all other expressions of query, and references on tables which do
885
      not present in query will not make problems.
886
887
      Also we suppose that view can't be changed during PS/SP life.
888
    */
889
    if (from_field == view_ref_found)
890
      return false;
891
892
    set_field(from_field);
2227.4.8 by Olaf van der Spek
Session::lex()
893
    if (session->lex().in_sum_func &&
894
        session->lex().in_sum_func->nest_level ==
895
        session->lex().current_select->nest_level)
937.2.6 by Stewart Smith
make set_if_bigger typesafe for C and C++. Fix up everywhere.
896
    {
2227.4.8 by Olaf van der Spek
Session::lex()
897
      set_if_bigger(session->lex().in_sum_func->max_arg_level,
898
                    session->lex().current_select->nest_level);
937.2.6 by Stewart Smith
make set_if_bigger typesafe for C and C++. Fix up everywhere.
899
    }
642.1.1 by Lee
move functions from item.cc/item.h to item directory
900
  }
901
  else if (session->mark_used_columns != MARK_COLUMNS_NONE)
902
  {
1660.1.3 by Brian Aker
Encapsulate Table in field
903
    Table *table= field->getTable();
1802.16.8 by Padraig O'Sullivan
Removal of all MyBitmap from the code base. Compiles but test failures exist now.
904
    boost::dynamic_bitset<> *current_bitmap, *other_bitmap;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
905
    if (session->mark_used_columns == MARK_COLUMNS_READ)
906
    {
907
      current_bitmap= table->read_set;
908
      other_bitmap=   table->write_set;
909
    }
910
    else
911
    {
912
      current_bitmap= table->write_set;
913
      other_bitmap=   table->read_set;
914
    }
1999.4.2 by Brian Aker
Encapsulate the field's position.
915
    //if (! current_bitmap->testAndSet(field->position()))
916
    if (! current_bitmap->test(field->position()))
642.1.1 by Lee
move functions from item.cc/item.h to item directory
917
    {
1999.4.2 by Brian Aker
Encapsulate the field's position.
918
      if (! other_bitmap->test(field->position()))
642.1.1 by Lee
move functions from item.cc/item.h to item directory
919
      {
920
        /* First usage of column */
921
        table->used_fields++;                     // Used to optimize loops
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
922
        table->covering_keys&= field->part_of_key;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
923
      }
924
    }
925
  }
926
  fixed= 1;
927
mark_non_agg_field:
928
  return false;
929
930
error:
931
  context->process_error(session);
932
  return true;
933
}
934
935
Item *Item_field::safe_charset_converter(const CHARSET_INFO * const tocs)
936
{
937
  no_const_subst= 1;
938
  return Item::safe_charset_converter(tocs);
939
}
940
941
942
void Item_field::cleanup()
943
{
944
  Item_ident::cleanup();
945
  /*
946
    Even if this object was created by direct link to field in setup_wild()
947
    it will be linked correctly next time by name of field and table alias.
948
    I.e. we can drop 'field'.
949
   */
950
  field= result_field= 0;
951
  null_value= false;
952
  return;
953
}
954
955
956
bool Item_field::result_as_int64_t()
957
{
958
  return field->can_be_compared_as_int64_t();
959
}
960
961
962
/**
963
  Find a field among specified multiple equalities.
964
965
  The function first searches the field among multiple equalities
966
  of the current level (in the cond_equal->current_level list).
967
  If it fails, it continues searching in upper levels accessed
968
  through a pointer cond_equal->upper_levels.
660.1.6 by Eric Herman
trailing whitespace fixup
969
  The search terminates as soon as a multiple equality containing
970
  the field is found.
642.1.1 by Lee
move functions from item.cc/item.h to item directory
971
972
  @param cond_equal   reference to list of multiple equalities where
973
                      the field (this object) is to be looked for
974
975
  @return
976
    - First Item_equal containing the field, if success
977
    - 0, otherwise
978
*/
979
980
Item_equal *Item_field::find_item_equal(COND_EQUAL *cond_equal)
981
{
982
  Item_equal *item= 0;
983
  while (cond_equal)
984
  {
2183.2.11 by Olaf van der Spek
Use List::begin()
985
    List<Item_equal>::iterator li(cond_equal->current_level.begin());
642.1.1 by Lee
move functions from item.cc/item.h to item directory
986
    while ((item= li++))
987
    {
988
      if (item->contains(field))
989
        return item;
990
    }
660.1.6 by Eric Herman
trailing whitespace fixup
991
    /*
642.1.1 by Lee
move functions from item.cc/item.h to item directory
992
      The field is not found in any of the multiple equalities
993
      of the current level. Look for it in upper levels
994
    */
995
    cond_equal= cond_equal->upper_levels;
996
  }
997
  return 0;
998
}
999
1000
1001
/**
1002
  Check whether a field can be substituted by an equal item.
1003
1004
  The function checks whether a substitution of the field
1005
  occurrence for an equal item is valid.
1006
1007
  @param arg   *arg != NULL <-> the field is in the context where
1008
               substitution for an equal item is valid
1009
1010
  @note
1011
    The following statement is not always true:
1012
  @n
1013
    x=y => F(x)=F(x/y).
1014
  @n
1015
    This means substitution of an item for an equal item not always
1016
    yields an equavalent condition. Here's an example:
1017
    @code
1018
    'a'='a '
1019
    (LENGTH('a')=1) != (LENGTH('a ')=2)
1020
  @endcode
1021
    Such a substitution is surely valid if either the substituted
1022
    field is not of a STRING type or if it is an argument of
1023
    a comparison predicate.
1024
1025
  @retval
1026
    true   substitution is valid
1027
  @retval
1028
    false  otherwise
1029
*/
1030
1031
bool Item_field::subst_argument_checker(unsigned char **arg)
1032
{
1033
  return (result_type() != STRING_RESULT) || (*arg);
1034
}
1035
1036
1037
/**
1038
  Set a pointer to the multiple equality the field reference belongs to
1039
  (if any).
1040
1041
  The function looks for a multiple equality containing the field item
1042
  among those referenced by arg.
1043
  In the case such equality exists the function does the following.
1044
  If the found multiple equality contains a constant, then the field
1045
  reference is substituted for this constant, otherwise it sets a pointer
1046
  to the multiple equality in the field item.
1047
1048
1049
  @param arg    reference to list of multiple equalities where
1050
                the field (this object) is to be looked for
1051
1052
  @note
1053
    This function is supposed to be called as a callback parameter in calls
1054
    of the compile method.
1055
1056
  @return
1057
    - pointer to the replacing constant item, if the field item was substituted
1058
    - pointer to the field item, otherwise.
1059
*/
1060
1061
Item *Item_field::equal_fields_propagator(unsigned char *arg)
1062
{
1063
  if (no_const_subst)
1064
    return this;
1065
  item_equal= find_item_equal((COND_EQUAL *) arg);
1066
  Item *item= 0;
1067
  if (item_equal)
1068
    item= item_equal->get_const();
1069
  /*
1070
    Disable const propagation for items used in different comparison contexts.
1071
    This must be done because, for example, Item_hex_string->val_int() is not
1072
    the same as (Item_hex_string->val_str() in BINARY column)->val_int().
1073
    We cannot simply disable the replacement in a particular context (
1074
    e.g. <bin_col> = <int_col> AND <bin_col> = <hex_string>) since
660.1.6 by Eric Herman
trailing whitespace fixup
1075
    Items don't know the context they are in and there are functions like
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1076
    IF (<hex_string>, 'yes', 'no').
1077
    The same problem occurs when comparing a DATE/TIME field with a
1078
    DATE/TIME represented as an int and as a string.
1079
  */
1080
  if (!item ||
1081
      (cmp_context != (Item_result)-1 && item->cmp_context != cmp_context))
1082
    item= this;
1083
1084
  return item;
1085
}
1086
1087
1088
/**
1089
  Mark the item to not be part of substitution if it's not a binary item.
1090
1091
  See comments in Arg_comparator::set_compare_func() for details.
1092
*/
1093
1094
bool Item_field::set_no_const_sub(unsigned char *)
1095
{
1096
  if (field->charset() != &my_charset_bin)
1097
    no_const_subst=1;
1098
  return false;
1099
}
1100
1101
1102
/**
1103
  Replace an Item_field for an equal Item_field that evaluated earlier
1104
  (if any).
1105
1106
  The function returns a pointer to an item that is taken from
1107
  the very beginning of the item_equal list which the Item_field
1108
  object refers to (belongs to) unless item_equal contains  a constant
660.1.6 by Eric Herman
trailing whitespace fixup
1109
  item. In this case the function returns this constant item,
1110
  (if the substitution does not require conversion).
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1111
  If the Item_field object does not refer any Item_equal object
1112
  'this' is returned .
1113
1114
  @param arg   a dummy parameter, is not used here
1115
1116
1117
  @note
1118
    This function is supposed to be called as a callback parameter in calls
1119
    of the thransformer method.
1120
1121
  @return
1122
    - pointer to a replacement Item_field if there is a better equal item or
1123
      a pointer to a constant equal item;
1124
    - this - otherwise.
1125
*/
1126
1127
Item *Item_field::replace_equal_field(unsigned char *)
1128
{
1129
  if (item_equal)
1130
  {
779.3.10 by Monty Taylor
Turned on -Wshadow.
1131
    Item *const_item_ptr= item_equal->get_const();
1132
    if (const_item_ptr)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1133
    {
1134
      if (cmp_context != (Item_result)-1 &&
779.3.10 by Monty Taylor
Turned on -Wshadow.
1135
          const_item_ptr->cmp_context != cmp_context)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1136
        return this;
779.3.10 by Monty Taylor
Turned on -Wshadow.
1137
      return const_item_ptr;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1138
    }
1139
    Item_field *subst= item_equal->get_first();
1140
    if (subst && !field->eq(subst->field))
1141
      return subst;
1142
  }
1143
  return this;
1144
}
1145
1146
1147
uint32_t Item_field::max_disp_length()
1148
{
1149
  return field->max_display_length();
1150
}
1151
1152
1153
/* ARGSUSED */
1052.2.4 by Nathan Williams
No actual code changes. Changed Send_field to SendField to be consistent with coding standards.
1154
void Item_field::make_field(SendField *tmp_field)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1155
{
1156
  field->make_field(tmp_field);
1157
  assert(tmp_field->table_name != 0);
1158
  if (name)
1159
    tmp_field->col_name=name;			// Use user supplied name
1160
  if (table_name)
1161
    tmp_field->table_name= table_name;
1162
  if (db_name)
1163
    tmp_field->db_name= db_name;
1164
}
1165
1166
1167
/**
1168
  Set a field's value from a item.
1169
*/
1170
1171
void Item_field::save_org_in_field(Field *to)
1172
{
1173
  if (field->is_null())
1174
  {
1175
    null_value=1;
1176
    set_field_to_null_with_conversions(to, 1);
1177
  }
1178
  else
1179
  {
1180
    to->set_notnull();
1181
    field_conv(to,field);
1182
    null_value=0;
1183
  }
1184
}
1185
1186
int Item_field::save_in_field(Field *to, bool no_conversions)
1187
{
1188
  int res;
1189
  if (result_field->is_null())
1190
  {
1191
    null_value=1;
1192
    res= set_field_to_null_with_conversions(to, no_conversions);
1193
  }
1194
  else
1195
  {
1196
    to->set_notnull();
1197
    res= field_conv(to,result_field);
1198
    null_value=0;
1199
  }
1200
  return(res);
1201
}
1202
1203
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
1204
bool Item_field::send(plugin::Client *client, String *)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1205
{
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
1206
  return client->store(result_field);
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1207
}
1208
1209
1210
void Item_field::update_null_value()
1211
{
1212
  /*
1213
    need to set no_errors to prevent warnings about type conversion
1214
    popping up.
1215
  */
1660.1.3 by Brian Aker
Encapsulate Table in field
1216
  Session *session= field->getTable()->in_use;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1217
  int no_errors;
1218
1219
  no_errors= session->no_errors;
1220
  session->no_errors= 1;
1221
  Item::update_null_value();
1222
  session->no_errors= no_errors;
1223
}
1224
1225
1226
/*
1227
  Add the field to the select list and substitute it for the reference to
1228
  the field.
1229
1230
  SYNOPSIS
1231
    Item_field::update_value_transformer()
1232
    select_arg      current select
1233
1234
  DESCRIPTION
1235
    If the field doesn't belong to the table being inserted into then it is
1236
    added to the select list, pointer to it is stored in the ref_pointer_array
1237
    of the select and the field itself is substituted for the Item_ref object.
1238
    This is done in order to get correct values from update fields that
1239
    belongs to the SELECT part in the INSERT .. SELECT .. ON DUPLICATE KEY
1240
    UPDATE statement.
1241
1242
  RETURN
1243
    0             if error occured
1244
    ref           if all conditions are met
1245
    this field    otherwise
1246
*/
1247
1248
Item *Item_field::update_value_transformer(unsigned char *select_arg)
1249
{
846 by Brian Aker
Removing on typedeffed class.
1250
  Select_Lex *select= (Select_Lex*)select_arg;
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1251
  assert(fixed);
1252
1660.1.3 by Brian Aker
Encapsulate Table in field
1253
  if (field->getTable() != select->context.table_list->table)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1254
  {
1255
    List<Item> *all_fields= &select->join->all_fields;
1256
    Item **ref_pointer_array= select->ref_pointer_array;
2183.2.17 by Olaf van der Spek
Use List::size()
1257
    int el= all_fields->size();
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1258
    Item_ref *ref;
1259
1260
    ref_pointer_array[el]= (Item*)this;
1261
    all_fields->push_front((Item*)this);
1262
    ref= new Item_ref(&select->context, ref_pointer_array + el,
1263
                      table_name, field_name);
1264
    return ref;
1265
  }
1266
  return this;
1267
}
1268
1269
2215.2.1 by Stewart Smith
remove enum_query_type which was effectively unused. It was set to one value once, compared to it once (i.e. always true) and passed around everywhere doing nothing.
1270
void Item_field::print(String *str)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1271
{
1660.1.3 by Brian Aker
Encapsulate Table in field
1272
  if (field && field->getTable()->const_table)
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1273
  {
1274
    char buff[MAX_FIELD_WIDTH];
1275
    String tmp(buff,sizeof(buff),str->charset());
1996.2.1 by Brian Aker
uuid type code.
1276
    field->val_str_internal(&tmp);
1685.7.1 by Prafulla Tekawade
Fix for Bug 586051
1277
    if (field->is_null())  {
1278
      str->append("NULL");
1279
    }
1280
    else {
1281
      str->append('\'');
1282
      str->append(tmp);
1283
      str->append('\'');
1284
    }
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1285
    return;
1286
  }
2215.2.1 by Stewart Smith
remove enum_query_type which was effectively unused. It was set to one value once, compared to it once (i.e. always true) and passed around everywhere doing nothing.
1287
  Item_ident::print(str);
642.1.1 by Lee
move functions from item.cc/item.h to item directory
1288
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
1289
1290
1291
} /* namespace drizzled */