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>
35 #include <boost/dynamic_bitset.hpp>
63 if (curr_item->eq(
this, 1))
66 item_list->push_back(
this);
92 for (
KeyPartInfo* cur_part= first_non_group_part; cur_part != last_part; cur_part++)
94 if (field->eq(cur_part->field))
109 bool Item_field::register_field_in_read_map(
unsigned char *arg)
112 if (field->getTable() == table || !table)
113 field->getTable()->setReadSet(field->position());
119 Item_field::Item_field(Field *f)
120 :Item_ident(0, NULL, f->getTable()->getAlias(), f->field_name),
121 item_equal(0), no_const_subst(0),
122 have_privileges(0), any_privileges(0)
129 orig_table_name= orig_field_name=
"";
144 f->getTable()->getShare()->getSchemaName(),
145 f->getTable()->getAlias(),
157 const char *db_arg,
const char *table_name_arg,
158 const char *field_name_arg) :
159 Item_ident(context_arg, db_arg,table_name_arg,field_name_arg),
167 Select_Lex *select= getSession().lex().current_select;
168 collation.set(DERIVATION_IMPLICIT);
170 if (select && select->parsing_place != IN_HAVING)
171 select->select_n_where_fields++;
181 result_field(item->result_field),
182 item_equal(item->item_equal),
183 no_const_subst(item->no_const_subst),
184 have_privileges(item->have_privileges),
185 any_privileges(item->any_privileges)
187 collation.set(DERIVATION_IMPLICIT);
190 void Item_field::set_field(
Field *field_par)
192 field=result_field=field_par;
194 decimals= field->decimals();
195 max_length= field_par->max_display_length();
196 table_name= field_par->getTable()->getAlias();
198 db_name= field_par->getTable()->getShare()->getSchemaName();
199 alias_name_used= field_par->getTable()->alias_name_used;
200 unsigned_flag=test(field_par->flags & UNSIGNED_FLAG);
201 collation.set(field_par->charset(), field_par->derivation());
235 return field->val_real();
244 return field->val_int();
252 return field->val_decimal(decimal_value);
261 return result_field->val_str(str,&
str_value);
266 if ((
null_value=field->is_null()) || field->get_date(ltime,fuzzydate))
274 bool Item_field::get_date_result(
type::Time <ime,uint32_t fuzzydate)
277 result_field->get_date(ltime,fuzzydate))
287 if ((
null_value=field->is_null()) || field->get_time(ltime))
295 double Item_field::val_result()
299 return result_field->val_real();
302 int64_t Item_field::val_int_result()
306 return result_field->val_int();
310 type::Decimal *Item_field::val_decimal_result(type::Decimal *decimal_value)
314 return result_field->val_decimal(decimal_value);
318 bool Item_field::val_bool_result()
325 switch (result_field->result_type())
328 return result_field->val_int() != 0;
332 type::Decimal decimal_value;
333 type::Decimal *val= result_field->val_decimal(&decimal_value);
335 return not val->isZero();
341 return result_field->val_real() != 0.0;
355 const Item *item_ptr= item->real_item();
356 if (item_ptr->type() != FIELD_ITEM)
360 if (item_field->field && field)
361 return item_field->field == field;
372 return (not system_charset_info->strcasecmp(item_field->
name, field_name) &&
373 (not item_field->table_name || not table_name ||
374 (not table_alias_charset->strcasecmp(item_field->table_name, table_name) &&
375 (not item_field->db_name || not db_name ||
376 (item_field->db_name && not system_charset_info->strcasecmp(item_field->db_name, db_name))))));
382 if (field->getTable()->const_table)
387 return depended_from ? OUTER_REF_TABLE_BIT : field->getTable()->
map;
390 enum Item_result Item_field::result_type ()
const
392 return field->result_type();
396 Item_result Item_field::cast_to_int_type()
const
398 return field->cast_to_int_type();
402 enum_field_types Item_field::field_type()
const
404 return field->type();
410 if (new_parent == depended_from)
423 return field->is_null();
427 Item *Item_field::get_tmp_table_item(
Session *session)
430 new_item->field= new_item->result_field;
483 enum_parsing_place place= NO_MATTER;
484 bool field_found= (*from_field != not_found_field);
485 bool upward_lookup=
false;
497 Item **ref= (
Item **) not_found_item;
498 Select_Lex *current_sel= (Select_Lex *) session->lex().current_select;
500 Select_Lex *select= 0;
502 if (current_sel->master_unit()->first_select()->linkage !=
511 last_checked_context->
select_lex->master_unit()->item;
512 last_checked_context= outer_context;
515 place= prev_subselect_item->parsing_place;
520 if (field_found && outer_context->
select_lex !=
528 if (field_found || (*from_field= find_field_in_tables(session,
this,
530 first_name_resolution_table,
532 last_name_resolution_table,
534 IGNORE_EXCEPT_NON_UNIQUE,
540 if (*from_field != view_ref_found)
542 prev_subselect_item->used_tables_cache|= (*from_field)->getTable()->map;
543 prev_subselect_item->const_item_cache=
false;
544 set_field(*from_field);
545 if (!last_checked_context->
select_lex->having_fix_field &&
546 select->group_list.elements &&
547 (place == SELECT_LIST || place == IN_HAVING))
560 select->inner_refs_list.push_back(rf);
561 rf->in_sum_func= session->lex().in_sum_func;
568 if (session->lex().in_sum_func &&
569 session->lex().in_sum_func->nest_level >= select->nest_level)
571 Item::Type ref_type= (*reference)->type();
572 set_if_bigger(session->lex().in_sum_func->max_arg_level,
574 set_field(*from_field);
578 ((ref_type == REF_ITEM ||
579 ref_type == FIELD_ITEM) ?
586 Item::Type ref_type= (*reference)->type();
587 prev_subselect_item->used_tables_cache|= (*reference)->used_tables();
588 prev_subselect_item->const_item_cache&= (*reference)->const_item();
590 context->
select_lex,
this, ((ref_type == REF_ITEM || ref_type == FIELD_ITEM) ? (
Item_ident*) (*reference) : 0));
604 if (place != IN_WHERE && place != IN_ON)
608 if (ref != not_found_item)
610 assert(*ref && (*ref)->
fixed);
611 prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
612 prev_subselect_item->const_item_cache&= (*ref)->const_item();
622 prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
623 prev_subselect_item->const_item_cache=
false;
629 if (ref == not_found_item && *from_field == not_found_field)
634 my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), session->where());
639 find_field_in_tables(session,
this,
642 reference, REPORT_ALL_ERRORS,
true);
646 else if (ref != not_found_item)
649 assert(*ref && (*ref)->
fixed);
659 new Item_ref(context, ref, table_name, field_name, alias_name_used) :
660 (!select->group_list.elements ?
661 new Item_direct_ref(context, ref, table_name, field_name, alias_name_used) :
662 new Item_outer_ref(context, ref, table_name, field_name, alias_name_used)));
667 if (place != IN_HAVING && select->group_list.elements)
678 if (rf->
fix_fields(session, reference) || rf->check_cols(1))
687 if (last_checked_context->
select_lex->having_fix_field)
689 Item_ref* rf=
new Item_ref(context, (cached_table->getSchemaName()[0] ? cached_table->getSchemaName() : 0), cached_table->alias, field_name);
696 if (rf->
fix_fields(session, reference) || rf->check_cols(1))
754 bool outer_fixed=
false;
763 if ((from_field= find_field_in_tables(session,
this,
767 session->lex().use_only_table_context ?
769 IGNORE_EXCEPT_NON_UNIQUE,
true)) ==
774 if (session->lex().current_select->is_item_list_lookup)
778 Item** res= find_item_in_list(session,
779 this, session->lex().current_select->item_list,
780 &counter, REPORT_EXCEPT_NOT_FOUND,
784 if (resolution == RESOLVED_AGAINST_ALIAS)
785 alias_name_used=
true;
786 if (res != (
Item **)not_found_item)
788 if ((*res)->type() == Item::FIELD_ITEM)
799 if (new_field == NULL)
802 my_error(ER_BAD_FIELD_ERROR, MYF(0), (*res)->name,
807 set_field(new_field);
824 return rf->
fix_fields(session, reference) || rf->check_cols(1);
832 goto mark_non_agg_field;
834 else if (!from_field)
837 if (!outer_fixed && cached_table && cached_table->
select_lex &&
846 goto mark_non_agg_field;
861 if (from_field == view_ref_found)
864 set_field(from_field);
865 if (session->lex().in_sum_func &&
866 session->lex().in_sum_func->nest_level ==
867 session->lex().current_select->nest_level)
869 set_if_bigger(session->lex().in_sum_func->max_arg_level,
870 session->lex().current_select->nest_level);
873 else if (session->mark_used_columns != MARK_COLUMNS_NONE)
875 Table *table= field->getTable();
876 boost::dynamic_bitset<> *current_bitmap, *other_bitmap;
877 if (session->mark_used_columns == MARK_COLUMNS_READ)
879 current_bitmap= table->read_set;
880 other_bitmap= table->write_set;
884 current_bitmap= table->write_set;
885 other_bitmap= table->read_set;
888 if (! current_bitmap->test(field->position()))
890 if (! other_bitmap->test(field->position()))
893 table->used_fields++;
894 table->covering_keys&= field->part_of_key;
906 return Item::safe_charset_converter(tocs);
910 void Item_field::cleanup()
912 Item_ident::cleanup();
918 field= result_field= 0;
954 if (item->contains(field))
961 cond_equal= cond_equal->upper_levels;
999 return (result_type() != STRING_RESULT) || (*arg);
1034 item= item_equal->get_const();
1062 if (field->charset() != &my_charset_bin)
1097 Item *const_item_ptr= item_equal->get_const();
1103 return const_item_ptr;
1106 if (subst && !field->eq(subst->field))
1113 uint32_t Item_field::max_disp_length()
1115 return field->max_display_length();
1120 void Item_field::make_field(SendField *tmp_field)
1122 field->make_field(tmp_field);
1123 assert(tmp_field->table_name != 0);
1125 tmp_field->col_name=
name;
1127 tmp_field->table_name= table_name;
1129 tmp_field->db_name= db_name;
1139 if (field->is_null())
1152 int Item_field::save_in_field(
Field *to,
bool no_conversions)
1155 if (result_field->is_null())
1172 client->store(result_field);
1183 int no_errors= session->no_errors;
1184 session->no_errors= 1;
1186 session->no_errors= no_errors;
1212 Item *Item_field::update_value_transformer(
unsigned char *select_arg)
1214 Select_Lex *select= (Select_Lex*)select_arg;
1217 if (field->getTable() != select->context.table_list->table)
1219 List<Item> *all_fields= &select->join->all_fields;
1220 Item **ref_pointer_array= select->ref_pointer_array;
1221 int el= all_fields->size();
1222 ref_pointer_array[el]= (
Item*)
this;
1223 all_fields->push_front((
Item*)
this);
1224 Item_ref* ref=
new Item_ref(&select->context, ref_pointer_array + el, table_name, field_name);
1233 if (field && field->getTable()->const_table)
1235 char buff[MAX_FIELD_WIDTH];
1236 String tmp(buff,
sizeof(buff),str->charset());
1237 field->val_str_internal(&tmp);
1238 if (field->is_null()) {
1239 str->append(STRING_WITH_LEN(
"NULL"));
table_map map
ID bit of table (1,2,4,8,16...)
type::Decimal * val_decimal(type::Decimal *)
virtual void print(String *str)
Item_equal * find_item_equal(COND_EQUAL *cond_equal)
Item ** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
virtual void print(String *str)
void save_org_in_field(Field *field)
bool find_item_in_field_list_processor(unsigned char *arg)
table_map used_tables() const
bool get_time(type::Time <ime)
bool get_date(type::Time <ime, uint32_t fuzzydate)
bool fix_fields(Session *, Item **)
bool set_no_const_sub(unsigned char *arg)
virtual void update_null_value()
bool subst_argument_checker(unsigned char **arg)
String * val_str(String *)
void fix_after_pullout(Select_Lex *new_parent, Item **ref)
bool fix_fields(Session *, Item **)
bool collect_item_field_processor(unsigned char *arg)
virtual bool can_be_compared_as_int64_t() const
void reset_field(Field *f)
int field_conv(Field *to, Field *from)
int set_field_to_null_with_conversions(Field *field, bool no_conversions)
void mark_as_dependent(Session *session, Select_Lex *last, Select_Lex *current, Item_ident *resolved_item, Item_ident *mark_item)
void send(plugin::Client *client, String *str_arg)
Item * equal_fields_propagator(unsigned char *arg)
TableList * first_name_resolution_table
TableList * last_name_resolution_table
bool eq(const Item *item, bool binary_cmp) const
Item * replace_equal_field(unsigned char *arg)
int fix_outer_field(Session *session, Field **field, Item **reference)
int64_t val_int_endpoint(bool left_endp, bool *incl_endp)
Name_resolution_context * outer_context