22 #include <drizzled/session.h>
23 #include <drizzled/error.h>
25 #include <drizzled/item/ref.h>
26 #include <drizzled/plugin/client.h>
27 #include <drizzled/item/sum.h>
28 #include <drizzled/item/subselect.h>
29 #include <drizzled/sql_lex.h>
33 Item_ref::Item_ref(Name_resolution_context *context_arg,
34 Item **item,
const char *table_name_arg,
35 const char *field_name_arg,
36 bool alias_name_used_arg)
37 :Item_ident(context_arg, NULL, table_name_arg, field_name_arg),
38 result_field(0), ref(item)
40 alias_name_used= alias_name_used_arg;
44 if (ref && *ref && (*ref)->fixed)
115 enum_parsing_place place= NO_MATTER;
117 Select_Lex *current_sel= session->lex().current_select;
119 if (!ref || ref == not_found_item)
124 if (ref == not_found_item)
134 my_error(ER_BAD_FIELD_ERROR,MYF(0), full_name(), session->where());
147 from_field= (
Field*) not_found_field;
151 Select_Lex *select= outer_context->
select_lex;
153 last_checked_context->
select_lex->master_unit()->item;
154 last_checked_context= outer_context;
161 if (ref != not_found_item)
163 assert(*ref && (*ref)->
fixed);
164 prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
165 prev_subselect_item->const_item_cache&= (*ref)->const_item();
176 place= prev_subselect_item->parsing_place;
189 if ((place != IN_HAVING ||
190 (!select->with_sum_func &&
191 select->group_list.elements == 0)))
198 from_field= find_field_in_tables(session,
this,
200 first_name_resolution_table,
202 last_name_resolution_table,
204 IGNORE_EXCEPT_NON_UNIQUE,
true);
207 if (from_field == view_ref_found)
209 Item::Type refer_type= (*reference)->type();
210 prev_subselect_item->used_tables_cache|=
211 (*reference)->used_tables();
212 prev_subselect_item->const_item_cache&=
213 (*reference)->const_item();
214 assert((*reference)->type() == REF_ITEM);
217 ((refer_type == REF_ITEM ||
218 refer_type == FIELD_ITEM) ?
227 if (from_field != not_found_field)
229 if (cached_table && cached_table->
select_lex &&
243 last_checked_context->
select_lex->master_unit()->item;
244 last_checked_context= outer_context;
245 }
while (outer_context && outer_context->
select_lex &&
248 prev_subselect_item->used_tables_cache|= from_field->getTable()->
map;
249 prev_subselect_item->const_item_cache=
false;
253 assert(from_field == not_found_field);
256 prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
257 prev_subselect_item->const_item_cache=
false;
260 }
while (outer_context);
262 assert(from_field != 0 && from_field != view_ref_found);
263 if (from_field != not_found_field)
269 session->lex().current_select,
this, fld);
275 if (session->lex().in_sum_func &&
276 session->lex().in_sum_func->nest_level >=
278 set_if_bigger(session->lex().in_sum_func->max_arg_level,
279 last_checked_context->
select_lex->nest_level);
285 my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), session->where());
289 assert(*ref && (*ref)->
fixed);
297 if (session->lex().in_sum_func &&
298 session->lex().in_sum_func->nest_level >=
300 set_if_bigger(session->lex().in_sum_func->max_arg_level,
301 last_checked_context->
select_lex->nest_level);
312 if (!((*ref)->type() == REF_ITEM &&
313 ((
Item_ref *)(*ref))->ref_type() == OUTER_REF) &&
314 (((*ref)->with_sum_func &&
name &&
315 !(current_sel->linkage != GLOBAL_OPTIONS_TYPE &&
316 current_sel->having_fix_field)) ||
319 my_error(ER_ILLEGAL_REFERENCE, MYF(0),
name, ((*ref)->with_sum_func ?
"reference to group function" :
"forward reference in item list"));
325 if ((*ref)->check_cols(1))
331 void Item_ref::set_properties()
333 max_length= (*ref)->max_length;
335 decimals= (*ref)->decimals;
336 collation.set((*ref)->collation);
341 with_sum_func= (*ref)->with_sum_func;
342 unsigned_flag= (*ref)->unsigned_flag;
346 if ((*ref)->type() == FIELD_ITEM)
347 alias_name_used= ((
Item_ident *) (*ref))->alias_name_used;
349 alias_name_used=
true;
353 void Item_ref::cleanup()
355 Item_ident::cleanup();
365 if ((*ref)->type() != Item::CACHE_ITEM &&
366 !table_name &&
name && alias_name_used)
382 client->store(result_field);
385 return (*ref)->send(client, tmp);
389 double Item_ref::val_result()
395 return result_field->val_real();
401 int64_t Item_ref::val_int_result()
407 return result_field->val_int();
413 String *Item_ref::str_result(String* str)
420 return result_field->val_str(str, &
str_value);
426 type::Decimal *Item_ref::val_decimal_result(type::Decimal *decimal_value)
432 return result_field->val_decimal(decimal_value);
438 bool Item_ref::val_bool_result()
444 switch (result_field->result_type()) {
446 return result_field->val_int() != 0;
450 type::Decimal decimal_value;
451 type::Decimal *val= result_field->val_decimal(&decimal_value);
453 return not val->isZero();
459 return result_field->val_real() != 0.0;
473 double tmp=(*ref)->val_result();
482 int64_t tmp=(*ref)->val_int_result();
491 bool tmp= (*ref)->val_bool_result();
500 tmp=(*ref)->str_result(tmp);
509 return (*ref)->is_null();
515 return (
null_value=(*ref)->get_date_result(ltime,fuzzydate));
521 type::Decimal *val= (*ref)->val_decimal_result(decimal_value);
526 int Item_ref::save_in_field(
Field *to,
bool no_conversions)
529 assert(!result_field);
530 res= (*ref)->save_in_field(to, no_conversions);
536 void Item_ref::save_org_in_field(Field *field)
538 (*ref)->save_org_in_field(field);
542 void Item_ref::make_field(SendField *field)
544 (*ref)->make_field(field);
547 field->col_name=
name;
549 field->table_name= table_name;
551 field->db_name= db_name;
555 Item *Item_ref::get_tmp_table_item(Session *session)
558 return (*ref)->get_tmp_table_item(session);
560 Item_field *item=
new Item_field(result_field);
563 item->table_name= table_name;
564 item->db_name= db_name;
571 if (depended_from == new_parent)
573 (*ref)->fix_after_pullout(new_parent, ref);