22#include <drizzled/session.h>
23#include <drizzled/table.h>
24#include <drizzled/error.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>
36#include <boost/dynamic_bitset.hpp>
61 List<Item_field>::iterator item_list_it(item_list->begin());
62 while (Item_field* curr_item= item_list_it++)
64 if (curr_item->eq(
this, 1))
67 item_list->push_back(
this);
93 for (
KeyPartInfo* cur_part= first_non_group_part; cur_part != last_part; cur_part++)
95 if (field->eq(cur_part->field))
110bool Item_field::register_field_in_read_map(
unsigned char *arg)
113 if (field->getTable() == table || !table)
114 field->getTable()->setReadSet(field->position());
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)
130 orig_table_name= orig_field_name=
"";
144 Item_ident(context_arg,
145 f->getTable()->getShare()->getSchemaName(),
146 f->getTable()->getAlias(),
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),
168 Select_Lex *select= getSession().lex().current_select;
169 collation.set(DERIVATION_IMPLICIT);
171 if (select && select->parsing_place != IN_HAVING)
172 select->select_n_where_fields++;
179Item_field::Item_field(
Session *session, Item_field *item) :
180 Item_ident(session, item),
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)
188 collation.set(DERIVATION_IMPLICIT);
191void Item_field::set_field(
Field *field_par)
193 field=result_field=field_par;
195 decimals= field->decimals();
196 max_length= field_par->max_display_length();
197 table_name= field_par->getTable()->getAlias();
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());
236 return field->val_real();
245 return field->val_int();
253 return field->val_decimal(decimal_value);
262 return result_field->val_str(str,&
str_value);
267 if ((
null_value=field->is_null()) || field->get_date(ltime,fuzzydate))
275bool Item_field::get_date_result(
type::Time <ime,uint32_t fuzzydate)
278 result_field->get_date(ltime,fuzzydate))
288 if ((
null_value=field->is_null()) || field->get_time(ltime))
296double Item_field::val_result()
300 return result_field->val_real();
303int64_t Item_field::val_int_result()
307 return result_field->val_int();
315 return result_field->val_decimal(decimal_value);
319bool Item_field::val_bool_result()
326 switch (result_field->result_type())
329 return result_field->val_int() != 0;
333 type::Decimal decimal_value;
334 type::Decimal *val= result_field->val_decimal(&decimal_value);
336 return not val->isZero();
342 return bool(compare_ne_double(result_field->val_real(), 0.0));
356 const Item *item_ptr= item->real_item();
357 if (item_ptr->type() != FIELD_ITEM)
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;
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))))));
383 if (field->getTable()->const_table)
388 return depended_from ? OUTER_REF_TABLE_BIT : field->getTable()->
map;
391enum Item_result Item_field::result_type ()
const
393 return field->result_type();
397Item_result Item_field::cast_to_int_type()
const
399 return field->cast_to_int_type();
403enum_field_types Item_field::field_type()
const
405 return field->type();
411 if (new_parent == depended_from)
424 return field->is_null();
428Item *Item_field::get_tmp_table_item(
Session *session)
431 new_item->field= new_item->result_field;
484 enum_parsing_place place= NO_MATTER;
485 bool field_found= (*from_field != not_found_field);
486 bool upward_lookup=
false;
498 Item **ref= (
Item **) not_found_item;
499 Select_Lex *current_sel= (Select_Lex *) session->lex().current_select;
501 Select_Lex *select= 0;
503 if (current_sel->master_unit()->first_select()->linkage !=
512 last_checked_context->
select_lex->master_unit()->item;
513 last_checked_context= outer_context;
516 place= prev_subselect_item->parsing_place;
521 if (field_found && outer_context->
select_lex !=
522 cached_table->select_lex)
529 if (field_found || (*from_field= find_field_in_tables(session,
this,
531 first_name_resolution_table,
533 last_name_resolution_table,
535 IGNORE_EXCEPT_NON_UNIQUE,
541 if (*from_field != view_ref_found)
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))
561 select->inner_refs_list.push_back(rf);
562 rf->in_sum_func= session->lex().in_sum_func;
569 if (session->lex().in_sum_func &&
570 session->lex().in_sum_func->nest_level >= select->nest_level)
572 Item::Type ref_type= (*reference)->type();
573 set_if_bigger(session->lex().in_sum_func->max_arg_level,
575 set_field(*from_field);
578 context->select_lex,
this,
579 ((ref_type == REF_ITEM ||
580 ref_type == FIELD_ITEM) ?
581 (Item_ident*) (*reference) : 0));
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();
591 context->select_lex,
this, ((ref_type == REF_ITEM || ref_type == FIELD_ITEM) ? (Item_ident*) (*reference) : 0));
605 if (place != IN_WHERE && place != IN_ON)
609 if (ref != not_found_item)
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();
623 prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
624 prev_subselect_item->const_item_cache=
false;
630 if (ref == not_found_item && *from_field == not_found_field)
635 my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), session->where());
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);
647 else if (ref != not_found_item)
650 assert(*ref && (*ref)->
fixed);
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)));
668 if (place != IN_HAVING && select->group_list.elements)
679 if (rf->
fix_fields(session, reference) || rf->check_cols(1))
688 if (last_checked_context->
select_lex->having_fix_field)
690 Item_ref* rf=
new Item_ref(context, (cached_table->getSchemaName()[0] ? cached_table->getSchemaName() : 0), cached_table->alias, field_name);
697 if (rf->
fix_fields(session, reference) || rf->check_cols(1))
755 bool outer_fixed=
false;
764 if ((from_field= find_field_in_tables(session,
this,
765 context->first_name_resolution_table,
766 context->last_name_resolution_table,
768 session->lex().use_only_table_context ?
770 IGNORE_EXCEPT_NON_UNIQUE,
true)) ==
775 if (session->lex().current_select->is_item_list_lookup)
779 Item** res= find_item_in_list(session,
780 this, session->lex().current_select->item_list,
781 &counter, REPORT_EXCEPT_NOT_FOUND,
785 if (resolution == RESOLVED_AGAINST_ALIAS)
786 alias_name_used=
true;
787 if (res != (
Item **)not_found_item)
789 if ((*res)->type() == Item::FIELD_ITEM)
798 Field *new_field= (*((Item_field**)res))->field;
800 if (new_field == NULL)
803 my_error(ER_BAD_FIELD_ERROR, MYF(0), (*res)->name,
808 set_field(new_field);
825 return rf->
fix_fields(session, reference) || rf->check_cols(1);
833 goto mark_non_agg_field;
835 else if (!from_field)
838 if (!outer_fixed && cached_table && cached_table->select_lex &&
839 context->select_lex &&
840 cached_table->select_lex != context->select_lex)
847 goto mark_non_agg_field;
862 if (from_field == view_ref_found)
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)
870 set_if_bigger(session->lex().in_sum_func->max_arg_level,
871 session->lex().current_select->nest_level);
874 else if (session->mark_used_columns != MARK_COLUMNS_NONE)
876 Table *table= field->getTable();
877 boost::dynamic_bitset<> *current_bitmap, *other_bitmap;
878 if (session->mark_used_columns == MARK_COLUMNS_READ)
880 current_bitmap= table->read_set;
881 other_bitmap= table->write_set;
885 current_bitmap= table->write_set;
886 other_bitmap= table->read_set;
889 if (! current_bitmap->test(field->position()))
891 if (! other_bitmap->test(field->position()))
894 table->used_fields++;
895 table->covering_keys&= field->part_of_key;
907 return Item::safe_charset_converter(tocs);
911void Item_field::cleanup()
913 Item_ident::cleanup();
919 field= result_field= 0;
952 List<Item_equal>::iterator li(cond_equal->current_level.begin());
955 if (item->contains(field))
962 cond_equal= cond_equal->upper_levels;
1000 return (result_type() != STRING_RESULT) || (*arg);
1035 item= item_equal->get_const();
1063 if (field->charset() != &my_charset_bin)
1098 Item *const_item_ptr= item_equal->get_const();
1104 return const_item_ptr;
1106 Item_field *subst= item_equal->get_first();
1107 if (subst && !field->
eq(subst->field))
1114uint32_t Item_field::max_disp_length()
1116 return field->max_display_length();
1121void Item_field::make_field(SendField *tmp_field)
1123 field->make_field(tmp_field);
1124 assert(tmp_field->table_name != 0);
1126 tmp_field->col_name=
name;
1128 tmp_field->table_name= table_name;
1130 tmp_field->db_name= db_name;
1140 if (field->is_null())
1153int Item_field::save_in_field(
Field *to,
bool no_conversions)
1156 if (result_field->is_null())
1173 client->store(result_field);
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;
1213Item *Item_field::update_value_transformer(
unsigned char *select_arg)
1215 Select_Lex *select= (Select_Lex*)select_arg;
1218 if (field->getTable() != select->context.table_list->table)
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);
1234 if (field && field->getTable()->const_table)
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"));
virtual bool can_be_compared_as_int64_t() const
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 <ime, 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)
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)
bool get_time(type::Time <ime)
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)
bool fix_fields(Session *, Item **)
virtual void update_null_value()
TableList * last_name_resolution_table
TableList * first_name_resolution_table
Name_resolution_context * outer_context
table_map map
ID bit of table (1,2,4,8,16...).
TODO: Rename this file - func.h is stupid.
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)
void mark_as_dependent(Session *session, Select_Lex *last, Select_Lex *current, Item_ident *resolved_item, Item_ident *mark_item)