drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
item/field.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
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
20#include <config.h>
21
22#include <drizzled/session.h>
23#include <drizzled/table.h>
24#include <drizzled/error.h>
25#include <drizzled/join.h>
26#include <drizzled/sql_base.h>
27#include <drizzled/sql_select.h>
28#include <drizzled/item/cmpfunc.h>
29#include <drizzled/item/field.h>
30#include <drizzled/item/outer_ref.h>
31#include <drizzled/plugin/client.h>
32#include <drizzled/item/subselect.h>
33#include <drizzled/sql_lex.h>
34#include <drizzled/key_part_info.h>
35
36#include <boost/dynamic_bitset.hpp>
37
38namespace drizzled {
39
57
59{
60 List<Item_field> *item_list= (List<Item_field>*) arg;
61 List<Item_field>::iterator item_list_it(item_list->begin());
62 while (Item_field* curr_item= item_list_it++)
63 {
64 if (curr_item->eq(this, 1))
65 return false; /* Already in the set. */
66 }
67 item_list->push_back(this);
68 return false;
69}
70
71
87
89{
90 KeyPartInfo *first_non_group_part= *((KeyPartInfo **) arg);
91 KeyPartInfo *last_part= *(((KeyPartInfo **) arg) + 1);
92
93 for (KeyPartInfo* cur_part= first_non_group_part; cur_part != last_part; cur_part++)
94 {
95 if (field->eq(cur_part->field))
96 return true;
97 }
98 return false;
99}
100
101
102/*
103 Mark field in read_map
104
105 NOTES
106 This is used by filesort to register used fields in a a temporary
107 column read set or to register used fields in a view
108*/
109
110bool Item_field::register_field_in_read_map(unsigned char *arg)
111{
112 Table *table= (Table *) arg;
113 if (field->getTable() == table || !table)
114 field->getTable()->setReadSet(field->position());
115
116 return 0;
117}
118
119
120Item_field::Item_field(Field *f)
121 :Item_ident(0, NULL, f->getTable()->getAlias(), f->field_name),
122 item_equal(0), no_const_subst(0),
123 have_privileges(0), any_privileges(0)
124{
125 set_field(f);
126 /*
127 field_name and table_name should not point to garbage
128 if this item is to be reused
129 */
130 orig_table_name= orig_field_name= "";
131}
132
133
140
141Item_field::Item_field(Session *,
142 Name_resolution_context *context_arg,
143 Field *f) :
144 Item_ident(context_arg,
145 f->getTable()->getShare()->getSchemaName(),
146 f->getTable()->getAlias(),
147 f->field_name),
148 item_equal(0),
149 no_const_subst(0),
150 have_privileges(0),
151 any_privileges(0)
152{
153 set_field(f);
154}
155
156
157Item_field::Item_field(Name_resolution_context *context_arg,
158 const char *db_arg,const char *table_name_arg,
159 const char *field_name_arg) :
160 Item_ident(context_arg, db_arg,table_name_arg,field_name_arg),
161 field(0),
162 result_field(0),
163 item_equal(0),
164 no_const_subst(0),
165 have_privileges(0),
166 any_privileges(0)
167{
168 Select_Lex *select= getSession().lex().current_select;
169 collation.set(DERIVATION_IMPLICIT);
170
171 if (select && select->parsing_place != IN_HAVING)
172 select->select_n_where_fields++;
173}
174
178
179Item_field::Item_field(Session *session, Item_field *item) :
180 Item_ident(session, item),
181 field(item->field),
182 result_field(item->result_field),
183 item_equal(item->item_equal),
184 no_const_subst(item->no_const_subst),
185 have_privileges(item->have_privileges),
186 any_privileges(item->any_privileges)
187{
188 collation.set(DERIVATION_IMPLICIT);
189}
190
191void Item_field::set_field(Field *field_par)
192{
193 field=result_field=field_par; // for easy coding with fields
194 maybe_null=field->maybe_null();
195 decimals= field->decimals();
196 max_length= field_par->max_display_length();
197 table_name= field_par->getTable()->getAlias();
198 field_name= field_par->field_name;
199 db_name= field_par->getTable()->getShare()->getSchemaName();
200 alias_name_used= field_par->getTable()->alias_name_used;
201 unsigned_flag=test(field_par->flags & UNSIGNED_FLAG);
202 collation.set(field_par->charset(), field_par->derivation());
203 fixed= 1;
204}
205
206
212
214{
215 set_field(f);
216 /* 'name' is pointing at field->field_name of old field */
217 name= f->field_name;
218}
219
220/* ARGSUSED */
222{
223 assert(fixed == 1);
224 if ((null_value=field->is_null()))
225 return 0;
226 str->set_charset(str_value.charset());
227 return field->val_str(str,&str_value);
228}
229
230
232{
233 assert(fixed == 1);
234 if ((null_value=field->is_null()))
235 return 0.0;
236 return field->val_real();
237}
238
239
241{
242 assert(fixed == 1);
243 if ((null_value=field->is_null()))
244 return 0;
245 return field->val_int();
246}
247
248
250{
251 if ((null_value= field->is_null()))
252 return 0;
253 return field->val_decimal(decimal_value);
254}
255
256
257String *Item_field::str_result(String *str)
258{
259 if ((null_value=result_field->is_null()))
260 return 0;
261 str->set_charset(str_value.charset());
262 return result_field->val_str(str,&str_value);
263}
264
265bool Item_field::get_date(type::Time &ltime,uint32_t fuzzydate)
266{
267 if ((null_value=field->is_null()) || field->get_date(ltime,fuzzydate))
268 {
269 ltime.reset();
270 return 1;
271 }
272 return 0;
273}
274
275bool Item_field::get_date_result(type::Time &ltime,uint32_t fuzzydate)
276{
277 if ((null_value=result_field->is_null()) ||
278 result_field->get_date(ltime,fuzzydate))
279 {
280 ltime.reset();
281 return 1;
282 }
283 return 0;
284}
285
287{
288 if ((null_value=field->is_null()) || field->get_time(ltime))
289 {
290 ltime.reset();
291 return 1;
292 }
293 return 0;
294}
295
296double Item_field::val_result()
297{
298 if ((null_value=result_field->is_null()))
299 return 0.0;
300 return result_field->val_real();
301}
302
303int64_t Item_field::val_int_result()
304{
305 if ((null_value=result_field->is_null()))
306 return 0;
307 return result_field->val_int();
308}
309
310
311type::Decimal *Item_field::val_decimal_result(type::Decimal *decimal_value)
312{
313 if ((null_value= result_field->is_null()))
314 return 0;
315 return result_field->val_decimal(decimal_value);
316}
317
318
319bool Item_field::val_bool_result()
320{
321 if ((null_value= result_field->is_null()))
322 {
323 return false;
324 }
325
326 switch (result_field->result_type())
327 {
328 case INT_RESULT:
329 return result_field->val_int() != 0;
330
331 case DECIMAL_RESULT:
332 {
333 type::Decimal decimal_value;
334 type::Decimal *val= result_field->val_decimal(&decimal_value);
335 if (val)
336 return not val->isZero();
337 return 0;
338 }
339
340 case REAL_RESULT:
341 case STRING_RESULT:
342 return bool(compare_ne_double(result_field->val_real(), 0.0));
343
344 case ROW_RESULT:
345 assert(false);
346 return 0;
347 }
348
349 assert(false);
350 return 0;
351}
352
353
354bool Item_field::eq(const Item *item, bool) const
355{
356 const Item *item_ptr= item->real_item();
357 if (item_ptr->type() != FIELD_ITEM)
358 return 0;
359
360 const Item_field *item_field= static_cast<const Item_field *>(item_ptr);
361 if (item_field->field && field)
362 return item_field->field == field;
363 /*
364 We may come here when we are trying to find a function in a GROUP BY
365 clause from the select list.
366 In this case the '100 % correct' way to do this would be to first
367 run fix_fields() on the GROUP BY item and then retry this function, but
368 I think it's better to relax the checking a bit as we will in
369 most cases do the correct thing by just checking the field name.
370 (In cases where we would choose wrong we would have to generate a
371 ER_NON_UNIQ_ERROR).
372 */
373 return (not system_charset_info->strcasecmp(item_field->name, field_name) &&
374 (not item_field->table_name || not table_name ||
375 (not table_alias_charset->strcasecmp(item_field->table_name, table_name) &&
376 (not item_field->db_name || not db_name ||
377 (item_field->db_name && not system_charset_info->strcasecmp(item_field->db_name, db_name))))));
378}
379
380
381table_map Item_field::used_tables() const
382{
383 if (field->getTable()->const_table)
384 {
385 return 0; // const item
386 }
387
388 return depended_from ? OUTER_REF_TABLE_BIT : field->getTable()->map;
389}
390
391enum Item_result Item_field::result_type () const
392{
393 return field->result_type();
394}
395
396
397Item_result Item_field::cast_to_int_type() const
398{
399 return field->cast_to_int_type();
400}
401
402
403enum_field_types Item_field::field_type() const
404{
405 return field->type();
406}
407
408
409void Item_field::fix_after_pullout(Select_Lex *new_parent, Item **)
410{
411 if (new_parent == depended_from)
412 depended_from= NULL;
414 ctx->outer_context= NULL; // We don't build a complete name resolver
415 ctx->select_lex= new_parent;
416 ctx->first_name_resolution_table= context->first_name_resolution_table;
417 ctx->last_name_resolution_table= context->last_name_resolution_table;
418 this->context=ctx;
419}
420
421
423{
424 return field->is_null();
425}
426
427
428Item *Item_field::get_tmp_table_item(Session *session)
429{
430 Item_field *new_item= new Item_field(session, this);
431 new_item->field= new_item->result_field;
432 return new_item;
433}
434
435int64_t Item_field::val_int_endpoint(bool, bool *)
436{
437 int64_t res= val_int();
438 return null_value? INT64_MIN : res;
439}
440
441
480
481int
482Item_field::fix_outer_field(Session *session, Field **from_field, Item **reference)
483{
484 enum_parsing_place place= NO_MATTER;
485 bool field_found= (*from_field != not_found_field);
486 bool upward_lookup= false;
487
488 /*
489 If there are outer contexts (outer selects, but current select is
490 not derived table or view) try to resolve this reference in the
491 outer contexts.
492
493 We treat each subselect as a separate namespace, so that different
494 subselects may contain columns with the same names. The subselects
495 are searched starting from the innermost.
496 */
497 Name_resolution_context *last_checked_context= context;
498 Item **ref= (Item **) not_found_item;
499 Select_Lex *current_sel= (Select_Lex *) session->lex().current_select;
500 Name_resolution_context *outer_context= 0;
501 Select_Lex *select= 0;
502 /* Currently derived tables cannot be correlated */
503 if (current_sel->master_unit()->first_select()->linkage !=
504 DERIVED_TABLE_TYPE)
505 outer_context= context->outer_context;
506 for (;
507 outer_context;
508 outer_context= outer_context->outer_context)
509 {
510 select= outer_context->select_lex;
511 Item_subselect *prev_subselect_item=
512 last_checked_context->select_lex->master_unit()->item;
513 last_checked_context= outer_context;
514 upward_lookup= true;
515
516 place= prev_subselect_item->parsing_place;
517 /*
518 If outer_field is set, field was already found by first call
519 to find_field_in_tables(). Only need to find appropriate context.
520 */
521 if (field_found && outer_context->select_lex !=
522 cached_table->select_lex)
523 continue;
524 /*
525 In case of a view, find_field_in_tables() writes the pointer to
526 the found view field into '*reference', in other words, it
527 substitutes this Item_field with the found expression.
528 */
529 if (field_found || (*from_field= find_field_in_tables(session, this,
530 outer_context->
531 first_name_resolution_table,
532 outer_context->
533 last_name_resolution_table,
534 reference,
535 IGNORE_EXCEPT_NON_UNIQUE,
536 true)) !=
537 not_found_field)
538 {
539 if (*from_field)
540 {
541 if (*from_field != view_ref_found)
542 {
543 prev_subselect_item->used_tables_cache|= (*from_field)->getTable()->map;
544 prev_subselect_item->const_item_cache= false;
545 set_field(*from_field);
546 if (!last_checked_context->select_lex->having_fix_field &&
547 select->group_list.elements &&
548 (place == SELECT_LIST || place == IN_HAVING))
549 {
550 /*
551 If an outer field is resolved in a grouping select then it
552 is replaced for an Item_outer_ref object. Otherwise an
553 Item_field object is used.
554 The new Item_outer_ref object is saved in the inner_refs_list of
555 the outer select. Here it is only created. It can be fixed only
556 after the original field has been fixed and this is done in the
557 fix_inner_refs() function.
558 */
559 Item_outer_ref* rf= new Item_outer_ref(context, this);
560 *reference= rf;
561 select->inner_refs_list.push_back(rf);
562 rf->in_sum_func= session->lex().in_sum_func;
563 }
564 /*
565 A reference is resolved to a nest level that's outer or the same as
566 the nest level of the enclosing set function : adjust the value of
567 max_arg_level for the function if it's needed.
568 */
569 if (session->lex().in_sum_func &&
570 session->lex().in_sum_func->nest_level >= select->nest_level)
571 {
572 Item::Type ref_type= (*reference)->type();
573 set_if_bigger(session->lex().in_sum_func->max_arg_level,
574 select->nest_level);
575 set_field(*from_field);
576 fixed= 1;
577 mark_as_dependent(session, last_checked_context->select_lex,
578 context->select_lex, this,
579 ((ref_type == REF_ITEM ||
580 ref_type == FIELD_ITEM) ?
581 (Item_ident*) (*reference) : 0));
582 return 0;
583 }
584 }
585 else
586 {
587 Item::Type ref_type= (*reference)->type();
588 prev_subselect_item->used_tables_cache|= (*reference)->used_tables();
589 prev_subselect_item->const_item_cache&= (*reference)->const_item();
590 mark_as_dependent(session, last_checked_context->select_lex,
591 context->select_lex, this, ((ref_type == REF_ITEM || ref_type == FIELD_ITEM) ? (Item_ident*) (*reference) : 0));
592 /*
593 A reference to a view field had been found and we
594 substituted it instead of this Item (find_field_in_tables
595 does it by assigning the new value to *reference), so now
596 we can return from this function.
597 */
598 return 0;
599 }
600 }
601 break;
602 }
603
604 /* Search in SELECT and GROUP lists of the outer select. */
605 if (place != IN_WHERE && place != IN_ON)
606 {
607 if (!(ref= resolve_ref_in_select_and_group(session, this, select)))
608 return -1; /* Some error occurred (e.g. ambiguous names). */
609 if (ref != not_found_item)
610 {
611 assert(*ref && (*ref)->fixed);
612 prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
613 prev_subselect_item->const_item_cache&= (*ref)->const_item();
614 break;
615 }
616 }
617
618 /*
619 Reference is not found in this select => this subquery depend on
620 outer select (or we just trying to find wrong identifier, in this
621 case it does not matter which used tables bits we set)
622 */
623 prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
624 prev_subselect_item->const_item_cache= false;
625 }
626
627 assert(ref != 0);
628 if (!*from_field)
629 return -1;
630 if (ref == not_found_item && *from_field == not_found_field)
631 {
632 if (upward_lookup)
633 {
634 // We can't say exactly what absent table or field
635 my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), session->where());
636 }
637 else
638 {
639 /* Call find_field_in_tables only to report the error */
640 find_field_in_tables(session, this,
641 context->first_name_resolution_table,
642 context->last_name_resolution_table,
643 reference, REPORT_ALL_ERRORS, true);
644 }
645 return -1;
646 }
647 else if (ref != not_found_item)
648 {
649 /* Should have been checked in resolve_ref_in_select_and_group(). */
650 assert(*ref && (*ref)->fixed);
651 /*
652 Here, a subset of actions performed by Item_ref::set_properties
653 is not enough. So we pass ptr to NULL into Item_[direct]_ref
654 constructor, so no initialization is performed, and call
655 fix_fields() below.
656 */
657 Item* save= *ref;
658 *ref= NULL; // Don't call set_properties()
659 Item_ref* rf= (place == IN_HAVING ?
660 new Item_ref(context, ref, table_name, field_name, alias_name_used) :
661 (!select->group_list.elements ?
662 new Item_direct_ref(context, ref, table_name, field_name, alias_name_used) :
663 new Item_outer_ref(context, ref, table_name, field_name, alias_name_used)));
664 *ref= save;
665 if (!rf)
666 return -1;
667
668 if (place != IN_HAVING && select->group_list.elements)
669 {
670 outer_context->select_lex->inner_refs_list.push_back((Item_outer_ref*)rf);
671 ((Item_outer_ref*)rf)->in_sum_func= session->lex().in_sum_func;
672 }
673 *reference= rf;
674 /*
675 rf is Item_ref => never substitute other items (in this case)
676 during fix_fields() => we can use rf after fix_fields()
677 */
678 assert(!rf->fixed); // Assured by Item_ref()
679 if (rf->fix_fields(session, reference) || rf->check_cols(1))
680 return -1;
681
682 mark_as_dependent(session, last_checked_context->select_lex, context->select_lex, this, rf);
683 return 0;
684 }
685 else
686 {
687 mark_as_dependent(session, last_checked_context->select_lex, context->select_lex, this, (Item_ident*)*reference);
688 if (last_checked_context->select_lex->having_fix_field)
689 {
690 Item_ref* rf= new Item_ref(context, (cached_table->getSchemaName()[0] ? cached_table->getSchemaName() : 0), cached_table->alias, field_name);
691 *reference= rf;
692 /*
693 rf is Item_ref => never substitute other items (in this case)
694 during fix_fields() => we can use rf after fix_fields()
695 */
696 assert(!rf->fixed); // Assured by Item_ref()
697 if (rf->fix_fields(session, reference) || rf->check_cols(1))
698 return -1;
699 return 0;
700 }
701 }
702 return 1;
703}
704
705
750
751bool Item_field::fix_fields(Session *session, Item **reference)
752{
753 assert(fixed == 0);
754 Field *from_field= (Field *)not_found_field;
755 bool outer_fixed= false;
756
757 if (!field) // If field is not checked
758 {
759 /*
760 In case of view, find_field_in_tables() write pointer to view field
761 expression to 'reference', i.e. it substitute that expression instead
762 of this Item_field
763 */
764 if ((from_field= find_field_in_tables(session, this,
765 context->first_name_resolution_table,
766 context->last_name_resolution_table,
767 reference,
768 session->lex().use_only_table_context ?
769 REPORT_ALL_ERRORS :
770 IGNORE_EXCEPT_NON_UNIQUE, true)) ==
771 not_found_field)
772 {
773 int ret;
774 /* Look up in current select's item_list to find aliased fields */
775 if (session->lex().current_select->is_item_list_lookup)
776 {
777 uint32_t counter;
778 enum_resolution_type resolution;
779 Item** res= find_item_in_list(session,
780 this, session->lex().current_select->item_list,
781 &counter, REPORT_EXCEPT_NOT_FOUND,
782 &resolution);
783 if (!res)
784 return 1;
785 if (resolution == RESOLVED_AGAINST_ALIAS)
786 alias_name_used= true;
787 if (res != (Item **)not_found_item)
788 {
789 if ((*res)->type() == Item::FIELD_ITEM)
790 {
791 /*
792 It's an Item_field referencing another Item_field in the select
793 list.
794 Use the field from the Item_field in the select list and leave
795 the Item_field instance in place.
796 */
797
798 Field *new_field= (*((Item_field**)res))->field;
799
800 if (new_field == NULL)
801 {
802 /* The column to which we link isn't valid. */
803 my_error(ER_BAD_FIELD_ERROR, MYF(0), (*res)->name,
804 session->where());
805 return 1;
806 }
807
808 set_field(new_field);
809 return 0;
810 }
811 else
812 {
813 /*
814 It's not an Item_field in the select list so we must make a new
815 Item_ref to point to the Item in the select list and replace the
816 Item_field created by the parser with the new Item_ref.
817 */
818 Item_ref *rf= new Item_ref(context, db_name,table_name,field_name);
819 *reference= rf;
820 /*
821 Because Item_ref never substitutes itself with other items
822 in Item_ref::fix_fields(), we can safely use the original
823 pointer to it even after fix_fields()
824 */
825 return rf->fix_fields(session, reference) || rf->check_cols(1);
826 }
827 }
828 }
829 if ((ret= fix_outer_field(session, &from_field, reference)) < 0)
830 return true;
831 outer_fixed= true;
832 if (!ret)
833 goto mark_non_agg_field;
834 }
835 else if (!from_field)
836 return true;
837
838 if (!outer_fixed && cached_table && cached_table->select_lex &&
839 context->select_lex &&
840 cached_table->select_lex != context->select_lex)
841 {
842 int ret;
843 if ((ret= fix_outer_field(session, &from_field, reference)) < 0)
844 return true;
845 outer_fixed= 1;
846 if (!ret)
847 goto mark_non_agg_field;
848 }
849
850 /*
851 if it is not expression from merged VIEW we will set this field.
852
853 We can leave expression substituted from view for next PS/SP rexecution
854 (i.e. do not register this substitution for reverting on cleanup()
855 (register_item_tree_changing())), because this subtree will be
856 fix_field'ed during setup_tables()->setup_underlying() (i.e. before
857 all other expressions of query, and references on tables which do
858 not present in query will not make problems.
859
860 Also we suppose that view can't be changed during PS/SP life.
861 */
862 if (from_field == view_ref_found)
863 return false;
864
865 set_field(from_field);
866 if (session->lex().in_sum_func &&
867 session->lex().in_sum_func->nest_level ==
868 session->lex().current_select->nest_level)
869 {
870 set_if_bigger(session->lex().in_sum_func->max_arg_level,
871 session->lex().current_select->nest_level);
872 }
873 }
874 else if (session->mark_used_columns != MARK_COLUMNS_NONE)
875 {
876 Table *table= field->getTable();
877 boost::dynamic_bitset<> *current_bitmap, *other_bitmap;
878 if (session->mark_used_columns == MARK_COLUMNS_READ)
879 {
880 current_bitmap= table->read_set;
881 other_bitmap= table->write_set;
882 }
883 else
884 {
885 current_bitmap= table->write_set;
886 other_bitmap= table->read_set;
887 }
888 //if (! current_bitmap->testAndSet(field->position()))
889 if (! current_bitmap->test(field->position()))
890 {
891 if (! other_bitmap->test(field->position()))
892 {
893 /* First usage of column */
894 table->used_fields++; // Used to optimize loops
895 table->covering_keys&= field->part_of_key;
896 }
897 }
898 }
899 fixed= 1;
900mark_non_agg_field:
901 return false;
902}
903
904Item *Item_field::safe_charset_converter(const charset_info_st * const tocs)
905{
906 no_const_subst= 1;
907 return Item::safe_charset_converter(tocs);
908}
909
910
911void Item_field::cleanup()
912{
913 Item_ident::cleanup();
914 /*
915 Even if this object was created by direct link to field in setup_wild()
916 it will be linked correctly next time by name of field and table alias.
917 I.e. we can drop 'field'.
918 */
919 field= result_field= 0;
920 null_value= false;
921}
922
923
925{
926 return field->can_be_compared_as_int64_t();
927}
928
929
947
949{
950 while (cond_equal)
951 {
952 List<Item_equal>::iterator li(cond_equal->current_level.begin());
953 while (Item_equal* item= li++)
954 {
955 if (item->contains(field))
956 return item;
957 }
958 /*
959 The field is not found in any of the multiple equalities
960 of the current level. Look for it in upper levels
961 */
962 cond_equal= cond_equal->upper_levels;
963 }
964 return 0;
965}
966
967
997
998bool Item_field::subst_argument_checker(unsigned char **arg)
999{
1000 return (result_type() != STRING_RESULT) || (*arg);
1001}
1002
1003
1027
1029{
1030 if (no_const_subst)
1031 return this;
1032 item_equal= find_item_equal((COND_EQUAL *) arg);
1033 Item *item= 0;
1034 if (item_equal)
1035 item= item_equal->get_const();
1036 /*
1037 Disable const propagation for items used in different comparison contexts.
1038 This must be done because, for example, Item_hex_string->val_int() is not
1039 the same as (Item_hex_string->val_str() in BINARY column)->val_int().
1040 We cannot simply disable the replacement in a particular context (
1041 e.g. <bin_col> = <int_col> AND <bin_col> = <hex_string>) since
1042 Items don't know the context they are in and there are functions like
1043 IF (<hex_string>, 'yes', 'no').
1044 The same problem occurs when comparing a DATE/TIME field with a
1045 DATE/TIME represented as an int and as a string.
1046 */
1047 if (!item ||
1048 (cmp_context != (Item_result)-1 && item->cmp_context != cmp_context))
1049 item= this;
1050
1051 return item;
1052}
1053
1054
1060
1061bool Item_field::set_no_const_sub(unsigned char *)
1062{
1063 if (field->charset() != &my_charset_bin)
1064 no_const_subst=1;
1065 return false;
1066}
1067
1068
1093
1095{
1096 if (item_equal)
1097 {
1098 Item *const_item_ptr= item_equal->get_const();
1099 if (const_item_ptr)
1100 {
1101 if (cmp_context != (Item_result)-1 &&
1102 const_item_ptr->cmp_context != cmp_context)
1103 return this;
1104 return const_item_ptr;
1105 }
1106 Item_field *subst= item_equal->get_first();
1107 if (subst && !field->eq(subst->field))
1108 return subst;
1109 }
1110 return this;
1111}
1112
1113
1114uint32_t Item_field::max_disp_length()
1115{
1116 return field->max_display_length();
1117}
1118
1119
1120/* ARGSUSED */
1121void Item_field::make_field(SendField *tmp_field)
1122{
1123 field->make_field(tmp_field);
1124 assert(tmp_field->table_name != 0);
1125 if (name)
1126 tmp_field->col_name=name; // Use user supplied name
1127 if (table_name)
1128 tmp_field->table_name= table_name;
1129 if (db_name)
1130 tmp_field->db_name= db_name;
1131}
1132
1133
1137
1139{
1140 if (field->is_null())
1141 {
1142 null_value=1;
1144 }
1145 else
1146 {
1147 to->set_notnull();
1148 field_conv(to,field);
1149 null_value=0;
1150 }
1151}
1152
1153int Item_field::save_in_field(Field *to, bool no_conversions)
1154{
1155 int res;
1156 if (result_field->is_null())
1157 {
1158 null_value=1;
1159 res= set_field_to_null_with_conversions(to, no_conversions);
1160 }
1161 else
1162 {
1163 to->set_notnull();
1164 res= field_conv(to,result_field);
1165 null_value=0;
1166 }
1167 return res;
1168}
1169
1170
1172{
1173 client->store(result_field);
1174}
1175
1176
1178{
1179 /*
1180 need to set no_errors to prevent warnings about type conversion
1181 popping up.
1182 */
1183 Session *session= field->getTable()->in_use;
1184 int no_errors= session->no_errors;
1185 session->no_errors= 1;
1187 session->no_errors= no_errors;
1188}
1189
1190
1191/*
1192 Add the field to the select list and substitute it for the reference to
1193 the field.
1194
1195 SYNOPSIS
1196 Item_field::update_value_transformer()
1197 select_arg current select
1198
1199 DESCRIPTION
1200 If the field doesn't belong to the table being inserted into then it is
1201 added to the select list, pointer to it is stored in the ref_pointer_array
1202 of the select and the field itself is substituted for the Item_ref object.
1203 This is done in order to get correct values from update fields that
1204 belongs to the SELECT part in the INSERT .. SELECT .. ON DUPLICATE KEY
1205 UPDATE statement.
1206
1207 RETURN
1208 0 if error occured
1209 ref if all conditions are met
1210 this field otherwise
1211*/
1212
1213Item *Item_field::update_value_transformer(unsigned char *select_arg)
1214{
1215 Select_Lex *select= (Select_Lex*)select_arg;
1216 assert(fixed);
1217
1218 if (field->getTable() != select->context.table_list->table)
1219 {
1220 List<Item> *all_fields= &select->join->all_fields;
1221 Item **ref_pointer_array= select->ref_pointer_array;
1222 int el= all_fields->size();
1223 ref_pointer_array[el]= (Item*)this;
1224 all_fields->push_front((Item*)this);
1225 Item_ref* ref= new Item_ref(&select->context, ref_pointer_array + el, table_name, field_name);
1226 return ref;
1227 }
1228 return this;
1229}
1230
1231
1233{
1234 if (field && field->getTable()->const_table)
1235 {
1236 char buff[MAX_FIELD_WIDTH];
1237 String tmp(buff,sizeof(buff),str->charset());
1238 field->val_str_internal(&tmp);
1239 if (field->is_null()) {
1240 str->append(STRING_WITH_LEN("NULL"));
1241 }
1242 else {
1243 str->append('\'');
1244 str->append(tmp);
1245 str->append('\'');
1246 }
1247 return;
1248 }
1249 Item_ident::print(str);
1250}
1251
1252
1253} /* namespace drizzled */
virtual bool can_be_compared_as_int64_t() const
Definition field.h:361
const char * field_name
Definition field.h:104
int64_t val_int_endpoint(bool left_endp, bool *incl_endp)
bool subst_argument_checker(unsigned char **arg)
String * val_str(String *)
bool set_no_const_sub(unsigned char *arg)
virtual void print(String *str)
bool get_date(type::Time &ltime, uint32_t fuzzydate)
void fix_after_pullout(Select_Lex *new_parent, Item **ref)
table_map used_tables() const
Item * equal_fields_propagator(unsigned char *arg)
bool find_item_in_field_list_processor(unsigned char *arg)
Definition item/field.cc:88
void reset_field(Field *f)
bool eq(const Item *item, bool binary_cmp) const
type::Decimal * val_decimal(type::Decimal *)
bool collect_item_field_processor(unsigned char *arg)
Definition item/field.cc:58
bool get_time(type::Time &ltime)
bool fix_fields(Session *, Item **)
Item_equal * find_item_equal(COND_EQUAL *cond_equal)
int fix_outer_field(Session *session, Field **field, Item **reference)
void save_org_in_field(Field *field)
void send(plugin::Client *client, String *str_arg)
Item * replace_equal_field(unsigned char *arg)
virtual void print(String *str)
Definition ident.cc:103
bool fix_fields(Session *, Item **)
Definition ref.cc:113
bool fixed
Definition item.h:122
bool null_value
Definition item.h:124
const char * name
Definition item.h:112
Item_result cmp_context
Definition item.h:146
bool maybe_null
Definition item.h:123
String str_value
Definition item.h:109
virtual void update_null_value()
Definition item.cc:518
table_map map
ID bit of table (1,2,4,8,16...).
TODO: Rename this file - func.h is stupid.
enum_resolution_type
Definition sql_base.h:82
int field_conv(Field *to, Field *from)
int set_field_to_null_with_conversions(Field *field, bool no_conversions)
Item ** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
Definition item.cc:940
void mark_as_dependent(Session *session, Select_Lex *last, Select_Lex *current, Item_ident *resolved_item, Item_ident *mark_item)
Definition item.cc:772