00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021 #include <drizzled/sql_select.h>
00022 #include <drizzled/error.h>
00023 #include <drizzled/show.h>
00024 #include <drizzled/item/cmpfunc.h>
00025 #include <drizzled/item/cache_row.h>
00026 #include <drizzled/item/type_holder.h>
00027 #include <drizzled/item/sum.h>
00028 #include <drizzled/item/copy_string.h>
00029 #include <drizzled/function/str/conv_charset.h>
00030 #include <drizzled/sql_base.h>
00031 #include <drizzled/util/convert.h>
00032 #include <drizzled/plugin/client.h>
00033 #include <drizzled/time_functions.h>
00034 #include <drizzled/field/str.h>
00035 #include <drizzled/field/num.h>
00036 #include <drizzled/field/blob.h>
00037 #include <drizzled/field/date.h>
00038 #include <drizzled/field/datetime.h>
00039 #include <drizzled/field/decimal.h>
00040 #include <drizzled/field/double.h>
00041 #include <drizzled/field/enum.h>
00042 #include <drizzled/field/epoch.h>
00043 #include <drizzled/field/int32.h>
00044 #include <drizzled/field/int64.h>
00045 #include <drizzled/field/microtime.h>
00046 #include <drizzled/field/null.h>
00047 #include <drizzled/field/real.h>
00048 #include <drizzled/field/size.h>
00049 #include <drizzled/field/time.h>
00050 #include <drizzled/field/varstring.h>
00051 #include <drizzled/current_session.h>
00052 #include <drizzled/session.h>
00053 #include <drizzled/internal/m_string.h>
00054 #include <drizzled/item/ref.h>
00055 #include <drizzled/item/subselect.h>
00056 #include <drizzled/sql_lex.h>
00057
00058 #include <cstdio>
00059 #include <math.h>
00060 #include <algorithm>
00061 #include <float.h>
00062
00063 using namespace std;
00064
00065 namespace drizzled
00066 {
00067
00068 const String my_null_string("NULL", 4, default_charset_info);
00069
00070 bool Item::is_expensive_processor(unsigned char *)
00071 {
00072 return false;
00073 }
00074
00075 void Item::fix_after_pullout(Select_Lex *, Item **)
00076 {}
00077
00078 Field *Item::tmp_table_field(Table *)
00079 {
00080 return NULL;
00081 }
00082
00083 const char *Item::full_name(void) const
00084 {
00085 return name ? name : "???";
00086 }
00087
00088 int64_t Item::val_int_endpoint(bool, bool *)
00089 {
00090 assert(0);
00091 return 0;
00092 }
00093
00095 bool Item::val_bool()
00096 {
00097 switch(result_type())
00098 {
00099 case INT_RESULT:
00100 return val_int() != 0;
00101
00102 case DECIMAL_RESULT:
00103 {
00104 type::Decimal decimal_value;
00105 type::Decimal *val= val_decimal(&decimal_value);
00106 if (val)
00107 return not val->isZero();
00108 return false;
00109 }
00110
00111 case REAL_RESULT:
00112 case STRING_RESULT:
00113 return val_real() != 0.0;
00114
00115 case ROW_RESULT:
00116 assert(0);
00117 abort();
00118 }
00119
00120 assert(0);
00121 abort();
00122 }
00123
00124 String *Item::val_string_from_real(String *str)
00125 {
00126 double nr= val_real();
00127 if (null_value)
00128 return NULL;
00129
00130 str->set_real(nr, decimals, &my_charset_bin);
00131 return str;
00132 }
00133
00134 String *Item::val_string_from_int(String *str)
00135 {
00136 int64_t nr= val_int();
00137 if (null_value)
00138 return NULL;
00139
00140 str->set_int(nr, unsigned_flag, &my_charset_bin);
00141 return str;
00142 }
00143
00144 String *Item::val_string_from_decimal(String *str)
00145 {
00146 type::Decimal dec_buf, *dec= val_decimal(&dec_buf);
00147 if (null_value)
00148 return NULL;
00149
00150 class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
00151 class_decimal2string(&dec_buf, 0, str);
00152 return str;
00153 }
00154
00155 type::Decimal *Item::val_decimal_from_real(type::Decimal *decimal_value)
00156 {
00157 double nr= val_real();
00158 if (null_value)
00159 return NULL;
00160
00161 double2_class_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
00162 return (decimal_value);
00163 }
00164
00165 type::Decimal *Item::val_decimal_from_int(type::Decimal *decimal_value)
00166 {
00167 int64_t nr= val_int();
00168 if (null_value)
00169 return NULL;
00170
00171 int2_class_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
00172 return decimal_value;
00173 }
00174
00175 type::Decimal *Item::val_decimal_from_string(type::Decimal *decimal_value)
00176 {
00177 String *res;
00178 char *end_ptr;
00179 if (!(res= val_str(&str_value)))
00180 return NULL;
00181
00182 end_ptr= (char*) res->ptr()+ res->length();
00183 if (decimal_value->store(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
00184 res->ptr(),
00185 res->length(),
00186 res->charset()) & E_DEC_BAD_NUM)
00187 {
00188 push_warning_printf(&getSession(),
00189 DRIZZLE_ERROR::WARN_LEVEL_WARN,
00190 ER_TRUNCATED_WRONG_VALUE,
00191 ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
00192 str_value.c_ptr());
00193 }
00194 return decimal_value;
00195 }
00196
00197 type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
00198 {
00199 assert(fixed);
00200 type::Time ltime;
00201 if (get_date(ltime, TIME_FUZZY_DATE))
00202 {
00203 decimal_value->set_zero();
00204 null_value= 1;
00205 return NULL;
00206 }
00207 return date2_class_decimal(<ime, decimal_value);
00208 }
00209
00210 type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
00211 {
00212 assert(fixed);
00213 type::Time ltime;
00214 if (get_time(ltime))
00215 {
00216 decimal_value->set_zero();
00217 return NULL;
00218 }
00219 return date2_class_decimal(<ime, decimal_value);
00220 }
00221
00222 double Item::val_real_from_decimal()
00223 {
00224
00225 double result;
00226 type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
00227 if (null_value)
00228 return 0.0;
00229 class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
00230 return result;
00231 }
00232
00233 int64_t Item::val_int_from_decimal()
00234 {
00235
00236 int64_t result;
00237 type::Decimal value, *dec_val= val_decimal(&value);
00238
00239 if (null_value)
00240 return 0;
00241 dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
00242
00243 return result;
00244 }
00245
00246 bool Item::save_time_in_field(Field *field)
00247 {
00248 type::Time ltime;
00249
00250 if (get_time(ltime))
00251 return set_field_to_null(field);
00252
00253 field->set_notnull();
00254
00255 return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
00256 }
00257
00258 bool Item::save_date_in_field(Field *field)
00259 {
00260 type::Time ltime;
00261
00262 if (get_date(ltime, TIME_FUZZY_DATE))
00263 return set_field_to_null(field);
00264
00265 field->set_notnull();
00266
00267 return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
00268 }
00269
00274 int Item::save_str_value_in_field(Field *field, String *result)
00275 {
00276 if (null_value)
00277 return set_field_to_null(field);
00278
00279 field->set_notnull();
00280
00281 return field->store(result->ptr(), result->length(), collation.collation);
00282 }
00283
00284 Item::Item():
00285 is_expensive_cache(-1),
00286 name(0),
00287 name_length(0),
00288 orig_name(0),
00289 max_length(0),
00290 marker(0),
00291 decimals(0),
00292 fixed(false),
00293 maybe_null(false),
00294 null_value(false),
00295 unsigned_flag(false),
00296 with_sum_func(false),
00297 is_autogenerated_name(true),
00298 with_subselect(false),
00299 collation(&my_charset_bin, DERIVATION_COERCIBLE),
00300 _session(*current_session)
00301 {
00302 cmp_context= (Item_result)-1;
00303
00304
00305 next= getSession().free_list;
00306 getSession().free_list= this;
00307
00308
00309
00310
00311
00312
00313 if (getSession().lex().current_select)
00314 {
00315 enum_parsing_place place= getSession().lex().current_select->parsing_place;
00316 if (place == SELECT_LIST || place == IN_HAVING)
00317 getSession().lex().current_select->select_n_having_items++;
00318 }
00319 }
00320
00321 Item::Item(Session *session, Item *item):
00322 is_expensive_cache(-1),
00323 str_value(item->str_value),
00324 name(item->name),
00325 name_length(item->name_length),
00326 orig_name(item->orig_name),
00327 max_length(item->max_length),
00328 marker(item->marker),
00329 decimals(item->decimals),
00330 fixed(item->fixed),
00331 maybe_null(item->maybe_null),
00332 null_value(item->null_value),
00333 unsigned_flag(item->unsigned_flag),
00334 with_sum_func(item->with_sum_func),
00335 is_autogenerated_name(item->is_autogenerated_name),
00336 with_subselect(item->with_subselect),
00337 collation(item->collation),
00338 cmp_context(item->cmp_context),
00339 _session(*session)
00340 {
00341
00342 next= getSession().free_list;
00343 getSession().free_list= this;
00344 }
00345
00346 uint32_t Item::float_length(uint32_t decimals_par) const
00347 {
00348 return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;
00349 }
00350
00351 uint32_t Item::decimal_precision() const
00352 {
00353 Item_result restype= result_type();
00354
00355 if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
00356 return min(class_decimal_length_to_precision(max_length, decimals, unsigned_flag),
00357 (uint32_t) DECIMAL_MAX_PRECISION);
00358 return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
00359 }
00360
00361 int Item::decimal_int_part() const
00362 {
00363 return class_decimal_int_part(decimal_precision(), decimals);
00364 }
00365
00366 void Item::print(String *str)
00367 {
00368 str->append(full_name());
00369 }
00370
00371 void Item::print_item_w_name(String *str)
00372 {
00373 print(str);
00374
00375 if (name)
00376 {
00377 str->append(STRING_WITH_LEN(" AS "));
00378 str->append_identifier(name, (uint32_t) strlen(name));
00379 }
00380 }
00381
00382 void Item::split_sum_func(Session *, Item **, List<Item> &)
00383 {}
00384
00385 void Item::cleanup()
00386 {
00387 fixed= false;
00388 marker= 0;
00389 if (orig_name)
00390 name= orig_name;
00391 return;
00392 }
00393
00394 void Item::rename(char *new_name)
00395 {
00396
00397
00398
00399
00400 if (! orig_name && new_name != name)
00401 orig_name= name;
00402 name= new_name;
00403 }
00404
00405 Item* Item::transform(Item_transformer transformer, unsigned char *arg)
00406 {
00407 return (this->*transformer)(arg);
00408 }
00409
00410 bool Item::check_cols(uint32_t c)
00411 {
00412 if (c != 1)
00413 {
00414 my_error(ER_OPERAND_COLUMNS, MYF(0), c);
00415 return true;
00416 }
00417 return false;
00418 }
00419
00420 void Item::set_name(const char *str, uint32_t length, const CHARSET_INFO * const cs)
00421 {
00422 if (!length)
00423 {
00424
00425 name= (char*) str;
00426 name_length= 0;
00427 return;
00428 }
00429 if (cs->ctype)
00430 {
00431 uint32_t orig_len= length;
00432 while (length && ! my_isgraph(cs, *str))
00433 {
00434
00435 length--;
00436 str++;
00437 }
00438 if (orig_len != length && ! is_autogenerated_name)
00439 {
00440 if (length == 0)
00441 push_warning_printf(&getSession(),
00442 DRIZZLE_ERROR::WARN_LEVEL_WARN,
00443 ER_NAME_BECOMES_EMPTY,
00444 ER(ER_NAME_BECOMES_EMPTY),
00445 str + length - orig_len);
00446 else
00447 push_warning_printf(&getSession(),
00448 DRIZZLE_ERROR::WARN_LEVEL_WARN,
00449 ER_REMOVED_SPACES,
00450 ER(ER_REMOVED_SPACES),
00451 str + length - orig_len);
00452 }
00453 }
00454 name= memory::sql_strmake(str, length);
00455 }
00456
00457 bool Item::eq(const Item *item, bool) const
00458 {
00459
00460
00461
00462
00463
00464 return type() == item->type() &&
00465 name &&
00466 item->name &&
00467 ! my_strcasecmp(system_charset_info, name, item->name);
00468 }
00469
00470 Item *Item::safe_charset_converter(const CHARSET_INFO * const tocs)
00471 {
00472 Item_func_conv_charset *conv= new Item_func_conv_charset(this, tocs, 1);
00473 return conv->safe ? conv : NULL;
00474 }
00475
00476 bool Item::get_date(type::Time <ime,uint32_t fuzzydate)
00477 {
00478 do
00479 {
00480 if (is_null())
00481 {
00482 break;
00483 }
00484 else if (result_type() == STRING_RESULT)
00485 {
00486 char buff[type::Time::MAX_STRING_LENGTH];
00487 String tmp(buff,sizeof(buff), &my_charset_bin),*res;
00488 if (!(res=val_str(&tmp)) ||
00489 str_to_datetime_with_warn(&getSession(), res->ptr(), res->length(),
00490 <ime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
00491 {
00492 break;
00493 }
00494 }
00495 else
00496 {
00497 int64_t value= val_int();
00498 type::datetime_t date_value;
00499
00500 ltime.convert(date_value, value, fuzzydate);
00501
00502 if (not type::is_valid(date_value))
00503 {
00504 char buff[DECIMAL_LONGLONG_DIGITS], *end;
00505 end= internal::int64_t10_to_str(value, buff, -10);
00506 make_truncated_value_warning(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN,
00507 buff, (int) (end-buff), type::DRIZZLE_TIMESTAMP_NONE, NULL);
00508 break;
00509 }
00510 }
00511
00512 return false;
00513 } while (0);
00514
00515 ltime.reset();
00516
00517 return true;
00518 }
00519
00520 bool Item::get_time(type::Time <ime)
00521 {
00522 char buff[type::Time::MAX_STRING_LENGTH];
00523 String tmp(buff,sizeof(buff),&my_charset_bin),*res;
00524 if (!(res=val_str(&tmp)) or
00525 str_to_time_with_warn(&getSession(), res->ptr(), res->length(), <ime))
00526 {
00527 ltime.reset();
00528
00529 return true;
00530 }
00531
00532 return false;
00533 }
00534
00535 bool Item::get_date_result(type::Time <ime,uint32_t fuzzydate)
00536 {
00537 return get_date(ltime, fuzzydate);
00538 }
00539
00540 bool Item::is_null()
00541 {
00542 return false;
00543 }
00544
00545 void Item::update_null_value ()
00546 {
00547 (void) val_int();
00548 }
00549
00550 void Item::top_level_item(void)
00551 {}
00552
00553 void Item::set_result_field(Field *)
00554 {}
00555
00556 bool Item::is_result_field(void)
00557 {
00558 return false;
00559 }
00560
00561 bool Item::is_bool_func(void)
00562 {
00563 return false;
00564 }
00565
00566 void Item::save_in_result_field(bool)
00567 {}
00568
00569 void Item::no_rows_in_result(void)
00570 {}
00571
00572 Item *Item::copy_or_same(Session *)
00573 {
00574 return this;
00575 }
00576
00577 Item *Item::copy_andor_structure(Session *)
00578 {
00579 return this;
00580 }
00581
00582 Item *Item::real_item(void)
00583 {
00584 return this;
00585 }
00586
00587 const Item *Item::real_item(void) const
00588 {
00589 return this;
00590 }
00591
00592 Item *Item::get_tmp_table_item(Session *session)
00593 {
00594 return copy_or_same(session);
00595 }
00596
00597 const CHARSET_INFO *Item::default_charset()
00598 {
00599 return current_session->variables.getCollation();
00600 }
00601
00602 const CHARSET_INFO *Item::compare_collation()
00603 {
00604 return NULL;
00605 }
00606
00607 bool Item::walk(Item_processor processor, bool, unsigned char *arg)
00608 {
00609 return (this->*processor)(arg);
00610 }
00611
00612 Item* Item::compile(Item_analyzer analyzer,
00613 unsigned char **arg_p,
00614 Item_transformer transformer,
00615 unsigned char *arg_t)
00616 {
00617 if ((this->*analyzer) (arg_p))
00618 return ((this->*transformer) (arg_t));
00619 return NULL;
00620 }
00621
00622 void Item::traverse_cond(Cond_traverser traverser, void *arg, traverse_order)
00623 {
00624 (*traverser)(this, arg);
00625 }
00626
00627 bool Item::remove_dependence_processor(unsigned char *)
00628 {
00629 return false;
00630 }
00631
00632 bool Item::remove_fixed(unsigned char *)
00633 {
00634 fixed= false;
00635 return false;
00636 }
00637
00638 bool Item::collect_item_field_processor(unsigned char *)
00639 {
00640 return false;
00641 }
00642
00643 bool Item::find_item_in_field_list_processor(unsigned char *)
00644 {
00645 return false;
00646 }
00647
00648 bool Item::change_context_processor(unsigned char *)
00649 {
00650 return false;
00651 }
00652
00653 bool Item::register_field_in_read_map(unsigned char *)
00654 {
00655 return false;
00656 }
00657
00658 bool Item::subst_argument_checker(unsigned char **arg)
00659 {
00660 if (*arg)
00661 *arg= NULL;
00662 return true;
00663 }
00664
00665 Item *Item::equal_fields_propagator(unsigned char *)
00666 {
00667 return this;
00668 }
00669
00670 bool Item::set_no_const_sub(unsigned char *)
00671 {
00672 return false;
00673 }
00674
00675 Item *Item::replace_equal_field(unsigned char *)
00676 {
00677 return this;
00678 }
00679
00680 uint32_t Item::cols()
00681 {
00682 return 1;
00683 }
00684
00685 Item* Item::element_index(uint32_t)
00686 {
00687 return this;
00688 }
00689
00690 Item** Item::addr(uint32_t)
00691 {
00692 return NULL;
00693 }
00694
00695 bool Item::null_inside()
00696 {
00697 return false;
00698 }
00699
00700 void Item::bring_value()
00701 {}
00702
00703 Item *Item::neg_transformer(Session *)
00704 {
00705 return NULL;
00706 }
00707
00708 Item *Item::update_value_transformer(unsigned char *)
00709 {
00710 return this;
00711 }
00712
00713 void Item::delete_self()
00714 {
00715 cleanup();
00716 delete this;
00717 }
00718
00719 bool Item::result_as_int64_t()
00720 {
00721 return false;
00722 }
00723
00724 bool Item::is_expensive()
00725 {
00726 if (is_expensive_cache < 0)
00727 is_expensive_cache= walk(&Item::is_expensive_processor, 0,
00728 (unsigned char*)0);
00729 return test(is_expensive_cache);
00730 }
00731
00732
00733
00734
00735
00736
00737 class Item_aggregate_ref : public Item_ref
00738 {
00739 public:
00740 Item_aggregate_ref(Name_resolution_context *context_arg, Item **item,
00741 const char *table_name_arg, const char *field_name_arg)
00742 :Item_ref(context_arg, item, table_name_arg, field_name_arg) {}
00743
00744 virtual inline void print (String *str)
00745 {
00746 if (ref)
00747 (*ref)->print(str);
00748 else
00749 Item_ident::print(str);
00750 }
00751 };
00752
00753 void Item::split_sum_func(Session *session, Item **ref_pointer_array,
00754 List<Item> &fields, Item **ref,
00755 bool skip_registered)
00756 {
00757
00758 if (type() == SUM_FUNC_ITEM &&
00759 skip_registered &&
00760 ((Item_sum *) this)->ref_by)
00761 return;
00762
00763 if ((type() != SUM_FUNC_ITEM && with_sum_func) ||
00764 (type() == FUNC_ITEM &&
00765 (((Item_func *) this)->functype() == Item_func::ISNOTNULLTEST_FUNC ||
00766 ((Item_func *) this)->functype() == Item_func::TRIG_COND_FUNC)))
00767 {
00768
00769 split_sum_func(session, ref_pointer_array, fields);
00770 }
00771 else if ((type() == SUM_FUNC_ITEM || (used_tables() & ~PARAM_TABLE_BIT)) &&
00772 type() != SUBSELECT_ITEM &&
00773 type() != REF_ITEM)
00774 {
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785 Item_aggregate_ref *item_ref;
00786 uint32_t el= fields.size();
00787 Item *real_itm= real_item();
00788
00789 ref_pointer_array[el]= real_itm;
00790 if (!(item_ref= new Item_aggregate_ref(&session->lex().current_select->context,
00791 ref_pointer_array + el, 0, name)))
00792 return;
00793 if (type() == SUM_FUNC_ITEM)
00794 item_ref->depended_from= ((Item_sum *) this)->depended_from();
00795 fields.push_front(real_itm);
00796 *ref= item_ref;
00797 }
00798 }
00799
00800
00801
00802
00803 bool Item::fix_fields(Session *, Item **)
00804 {
00805
00806 assert(! fixed || basic_const_item());
00807 fixed= true;
00808 return false;
00809 }
00810
00811 void mark_as_dependent(Session *session, Select_Lex *last, Select_Lex *current,
00812 Item_ident *resolved_item,
00813 Item_ident *mark_item)
00814 {
00815 const char *db_name= (resolved_item->db_name ?
00816 resolved_item->db_name : "");
00817 const char *table_name= (resolved_item->table_name ?
00818 resolved_item->table_name : "");
00819
00820 if (mark_item)
00821 mark_item->depended_from= last;
00822 current->mark_as_dependent(last);
00823 if (session->lex().describe & DESCRIBE_EXTENDED)
00824 {
00825 char warn_buff[DRIZZLE_ERRMSG_SIZE];
00826 snprintf(warn_buff, sizeof(warn_buff), ER(ER_WARN_FIELD_RESOLVED),
00827 db_name, (db_name[0] ? "." : ""),
00828 table_name, (table_name [0] ? "." : ""),
00829 resolved_item->field_name,
00830 current->select_number, last->select_number);
00831 push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
00832 ER_WARN_FIELD_RESOLVED, warn_buff);
00833 }
00834 }
00835
00836 void mark_select_range_as_dependent(Session *session,
00837 Select_Lex *last_select,
00838 Select_Lex *current_sel,
00839 Field *found_field, Item *found_item,
00840 Item_ident *resolved_item)
00841 {
00842
00843
00844
00845
00846
00847
00848 Select_Lex *previous_select= current_sel;
00849 for (; previous_select->outer_select() != last_select;
00850 previous_select= previous_select->outer_select())
00851 {
00852 Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
00853 prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
00854 prev_subselect_item->const_item_cache= false;
00855 }
00856 {
00857 Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
00858 Item_ident *dependent= resolved_item;
00859 if (found_field == view_ref_found)
00860 {
00861 Item::Type type= found_item->type();
00862 prev_subselect_item->used_tables_cache|= found_item->used_tables();
00863 dependent= ((type == Item::REF_ITEM || type == Item::FIELD_ITEM) ?
00864 (Item_ident*) found_item :
00865 0);
00866 }
00867 else
00868 prev_subselect_item->used_tables_cache|= found_field->getTable()->map;
00869 prev_subselect_item->const_item_cache= false;
00870 mark_as_dependent(session, last_select, current_sel, resolved_item,
00871 dependent);
00872 }
00873 }
00874
00889 static Item** find_field_in_group_list(Session *session, Item *find_item, Order *group_list)
00890 {
00891 const char *db_name;
00892 const char *table_name;
00893 const char *field_name;
00894 Order *found_group= NULL;
00895 int found_match_degree= 0;
00896 Item_ident *cur_field;
00897 int cur_match_degree= 0;
00898 char name_buff[NAME_LEN+1];
00899
00900 if (find_item->type() == Item::FIELD_ITEM ||
00901 find_item->type() == Item::REF_ITEM)
00902 {
00903 db_name= ((Item_ident*) find_item)->db_name;
00904 table_name= ((Item_ident*) find_item)->table_name;
00905 field_name= ((Item_ident*) find_item)->field_name;
00906 }
00907 else
00908 return NULL;
00909
00910 if (db_name)
00911 {
00912
00913 strncpy(name_buff, db_name, sizeof(name_buff)-1);
00914 my_casedn_str(files_charset_info, name_buff);
00915 db_name= name_buff;
00916 }
00917
00918 assert(field_name != 0);
00919
00920 for (Order *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
00921 {
00922 if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM)
00923 {
00924 cur_field= (Item_ident*) *cur_group->item;
00925 cur_match_degree= 0;
00926
00927 assert(cur_field->field_name != 0);
00928
00929 if (! my_strcasecmp(system_charset_info, cur_field->field_name, field_name))
00930 ++cur_match_degree;
00931 else
00932 continue;
00933
00934 if (cur_field->table_name && table_name)
00935 {
00936
00937 if (my_strcasecmp(table_alias_charset, cur_field->table_name, table_name))
00938
00939 return NULL;
00940
00941 ++cur_match_degree;
00942 if (cur_field->db_name && db_name)
00943 {
00944
00945 if (my_strcasecmp(system_charset_info, cur_field->db_name, db_name))
00946 {
00947
00948 return NULL;
00949 }
00950 ++cur_match_degree;
00951 }
00952 }
00953
00954 if (cur_match_degree > found_match_degree)
00955 {
00956 found_match_degree= cur_match_degree;
00957 found_group= cur_group;
00958 }
00959 else if (found_group &&
00960 (cur_match_degree == found_match_degree) &&
00961 ! (*(found_group->item))->eq(cur_field, 0))
00962 {
00963
00964
00965
00966
00967
00968 my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), session->where());
00969 return NULL;
00970 }
00971 }
00972 }
00973
00974 if (found_group)
00975 return found_group->item;
00976
00977 return NULL;
00978 }
00979
00980 Item** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
00981 {
00982 Item **group_by_ref= NULL;
00983 Item **select_ref= NULL;
00984 Order *group_list= (Order*) select->group_list.first;
00985 bool ambiguous_fields= false;
00986 uint32_t counter;
00987 enum_resolution_type resolution;
00988
00989
00990
00991
00992
00993 if (!(select_ref= find_item_in_list(session,
00994 ref, *(select->get_item_list()),
00995 &counter, REPORT_EXCEPT_NOT_FOUND,
00996 &resolution)))
00997 return NULL;
00998 if (resolution == RESOLVED_AGAINST_ALIAS)
00999 ref->alias_name_used= true;
01000
01001
01002 if (select->having_fix_field && !ref->with_sum_func && group_list)
01003 {
01004 group_by_ref= find_field_in_group_list(session, ref, group_list);
01005
01006
01007 if (group_by_ref && (select_ref != not_found_item) &&
01008 !((*group_by_ref)->eq(*select_ref, 0)))
01009 {
01010 ambiguous_fields= true;
01011 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
01012 ER(ER_NON_UNIQ_ERROR), ref->full_name(),
01013 session->where());
01014
01015 }
01016 }
01017
01018 if (select_ref != not_found_item || group_by_ref)
01019 {
01020 if (select_ref != not_found_item && !ambiguous_fields)
01021 {
01022 assert(*select_ref != 0);
01023 if (!select->ref_pointer_array[counter])
01024 {
01025 my_error(ER_ILLEGAL_REFERENCE, MYF(0),
01026 ref->name, "forward reference in item list");
01027 return NULL;
01028 }
01029 assert((*select_ref)->fixed);
01030 return (select->ref_pointer_array + counter);
01031 }
01032 if (group_by_ref)
01033 return group_by_ref;
01034 assert(false);
01035 return NULL;
01036 }
01037
01038 return (Item**) not_found_item;
01039 }
01040
01041 void Item::init_make_field(SendField *tmp_field,
01042 enum enum_field_types field_type_arg)
01043 {
01044 char *empty_name= (char*) "";
01045 tmp_field->db_name= empty_name;
01046 tmp_field->org_table_name= empty_name;
01047 tmp_field->org_col_name= empty_name;
01048 tmp_field->table_name= empty_name;
01049 tmp_field->col_name= name;
01050 tmp_field->charsetnr= collation.collation->number;
01051 tmp_field->flags= (maybe_null ? 0 : NOT_NULL_FLAG) |
01052 (my_binary_compare(collation.collation) ?
01053 BINARY_FLAG : 0);
01054 tmp_field->type= field_type_arg;
01055 tmp_field->length= max_length;
01056 tmp_field->decimals= decimals;
01057 }
01058
01059 void Item::make_field(SendField *tmp_field)
01060 {
01061 init_make_field(tmp_field, field_type());
01062 }
01063
01064 enum_field_types Item::string_field_type() const
01065 {
01066 enum_field_types f_type= DRIZZLE_TYPE_VARCHAR;
01067 if (max_length >= 65536)
01068 f_type= DRIZZLE_TYPE_BLOB;
01069 return f_type;
01070 }
01071
01072 enum_field_types Item::field_type() const
01073 {
01074 switch (result_type()) {
01075 case STRING_RESULT:
01076 return string_field_type();
01077 case INT_RESULT:
01078 return DRIZZLE_TYPE_LONGLONG;
01079 case DECIMAL_RESULT:
01080 return DRIZZLE_TYPE_DECIMAL;
01081 case REAL_RESULT:
01082 return DRIZZLE_TYPE_DOUBLE;
01083 case ROW_RESULT:
01084 assert(0);
01085 }
01086
01087 abort();
01088 }
01089
01090 bool Item::is_datetime()
01091 {
01092 return field::isDateTime(field_type());
01093 }
01094
01095 String *Item::check_well_formed_result(String *str, bool send_error)
01096 {
01097
01098 const CHARSET_INFO * const cs= str->charset();
01099 int well_formed_error;
01100 uint32_t wlen= cs->cset->well_formed_len(cs,
01101 str->ptr(), str->ptr() + str->length(),
01102 str->length(), &well_formed_error);
01103 if (wlen < str->length())
01104 {
01105 char hexbuf[7];
01106 enum DRIZZLE_ERROR::enum_warning_level level;
01107 uint32_t diff= str->length() - wlen;
01108 set_if_smaller(diff, 3U);
01109 (void) drizzled_string_to_hex(hexbuf, str->ptr() + wlen, diff);
01110 if (send_error)
01111 {
01112 my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
01113 cs->csname, hexbuf);
01114 return NULL;
01115 }
01116 {
01117 level= DRIZZLE_ERROR::WARN_LEVEL_ERROR;
01118 null_value= 1;
01119 str= 0;
01120 }
01121 push_warning_printf(&getSession(), level, ER_INVALID_CHARACTER_STRING,
01122 ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
01123 }
01124 return str;
01125 }
01126
01127 bool Item::eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO * const cs)
01128 {
01129 const CHARSET_INFO *save_cs= 0;
01130 const CHARSET_INFO *save_item_cs= 0;
01131 if (collation.collation != cs)
01132 {
01133 save_cs= collation.collation;
01134 collation.collation= cs;
01135 }
01136 if (item->collation.collation != cs)
01137 {
01138 save_item_cs= item->collation.collation;
01139 item->collation.collation= cs;
01140 }
01141 bool res= eq(item, binary_cmp);
01142 if (save_cs)
01143 collation.collation= save_cs;
01144 if (save_item_cs)
01145 item->collation.collation= save_item_cs;
01146 return res;
01147 }
01148
01149 Field *Item::make_string_field(Table *table)
01150 {
01151 Field *field;
01152 assert(collation.collation);
01153 if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB)
01154 {
01155 field= new Field_blob(max_length, maybe_null, name,
01156 collation.collation);
01157 }
01158 else
01159 {
01160 table->setVariableWidth();
01161 field= new Field_varstring(max_length, maybe_null, name,
01162 collation.collation);
01163 }
01164
01165 if (field)
01166 field->init(table);
01167 return field;
01168 }
01169
01170 Field *Item::tmp_table_field_from_field_type(Table *table, bool)
01171 {
01172
01173
01174
01175 unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
01176 Field *field= NULL;
01177
01178 switch (field_type()) {
01179 case DRIZZLE_TYPE_DECIMAL:
01180 field= new Field_decimal((unsigned char*) 0,
01181 max_length,
01182 null_ptr,
01183 0,
01184 Field::NONE,
01185 name,
01186 decimals);
01187 break;
01188 case DRIZZLE_TYPE_LONG:
01189 field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
01190 break;
01191 case DRIZZLE_TYPE_LONGLONG:
01192 field= new field::Int64((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
01193 break;
01194 case DRIZZLE_TYPE_DOUBLE:
01195 field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
01196 name, decimals, 0, unsigned_flag);
01197 break;
01198 case DRIZZLE_TYPE_NULL:
01199 field= new Field_null((unsigned char*) 0, max_length, name);
01200 break;
01201 case DRIZZLE_TYPE_DATE:
01202 field= new Field_date(maybe_null, name);
01203 break;
01204
01205 case DRIZZLE_TYPE_MICROTIME:
01206 field= new field::Microtime(maybe_null, name);
01207 break;
01208
01209 case DRIZZLE_TYPE_TIMESTAMP:
01210 field= new field::Epoch(maybe_null, name);
01211 break;
01212 case DRIZZLE_TYPE_DATETIME:
01213 field= new Field_datetime(maybe_null, name);
01214 break;
01215 case DRIZZLE_TYPE_TIME:
01216 field= new field::Time(maybe_null, name);
01217 break;
01218 case DRIZZLE_TYPE_BOOLEAN:
01219 case DRIZZLE_TYPE_UUID:
01220 case DRIZZLE_TYPE_ENUM:
01221 case DRIZZLE_TYPE_VARCHAR:
01222 return make_string_field(table);
01223 case DRIZZLE_TYPE_BLOB:
01224 field= new Field_blob(max_length, maybe_null, name, collation.collation);
01225 break;
01226 }
01227 assert(field);
01228
01229 if (field)
01230 field->init(table);
01231 return field;
01232 }
01233
01234
01235
01236
01237
01238
01239 int Item::save_in_field(Field *field, bool no_conversions)
01240 {
01241 int error;
01242 if (result_type() == STRING_RESULT)
01243 {
01244 String *result;
01245 const CHARSET_INFO * const cs= collation.collation;
01246 char buff[MAX_FIELD_WIDTH];
01247 str_value.set_quick(buff, sizeof(buff), cs);
01248 result=val_str(&str_value);
01249 if (null_value)
01250 {
01251 str_value.set_quick(0, 0, cs);
01252 return set_field_to_null_with_conversions(field, no_conversions);
01253 }
01254
01255
01256
01257 field->set_notnull();
01258 error=field->store(result->ptr(),result->length(),cs);
01259 str_value.set_quick(0, 0, cs);
01260 }
01261 else if (result_type() == REAL_RESULT &&
01262 field->result_type() == STRING_RESULT)
01263 {
01264 double nr= val_real();
01265 if (null_value)
01266 return set_field_to_null_with_conversions(field, no_conversions);
01267 field->set_notnull();
01268 error= field->store(nr);
01269 }
01270 else if (result_type() == REAL_RESULT)
01271 {
01272 double nr= val_real();
01273 if (null_value)
01274 return set_field_to_null(field);
01275 field->set_notnull();
01276 error=field->store(nr);
01277 }
01278 else if (result_type() == DECIMAL_RESULT)
01279 {
01280 type::Decimal decimal_value;
01281 type::Decimal *value= val_decimal(&decimal_value);
01282 if (null_value)
01283 return set_field_to_null_with_conversions(field, no_conversions);
01284 field->set_notnull();
01285 error=field->store_decimal(value);
01286 }
01287 else
01288 {
01289 int64_t nr=val_int();
01290 if (null_value)
01291 return set_field_to_null_with_conversions(field, no_conversions);
01292 field->set_notnull();
01293 error=field->store(nr, unsigned_flag);
01294 }
01295 return error;
01296 }
01297
01307 bool Item::cache_const_expr_analyzer(unsigned char **arg)
01308 {
01309 bool *cache_flag= (bool*)*arg;
01310 if (!*cache_flag)
01311 {
01312 Item *item= real_item();
01313
01314
01315
01316
01317 if (const_item() &&
01318 !(item->basic_const_item() || item->type() == Item::FIELD_ITEM ||
01319 item->type() == SUBSELECT_ITEM ||
01320
01321
01322
01323
01324
01325 (item->type() == Item::FUNC_ITEM &&
01326 ((Item_func*)item)->functype() == Item_func::GUSERVAR_FUNC)))
01327 *cache_flag= true;
01328 return true;
01329 }
01330 return false;
01331 }
01332
01342 Item* Item::cache_const_expr_transformer(unsigned char *arg)
01343 {
01344 if (*(bool*)arg)
01345 {
01346 *((bool*)arg)= false;
01347 Item_cache *cache= Item_cache::get_cache(this);
01348 if (!cache)
01349 return NULL;
01350 cache->setup(this);
01351 cache->store(this);
01352 return cache;
01353 }
01354 return this;
01355 }
01356
01357 bool Item::send(plugin::Client *client, String *buffer)
01358 {
01359 bool result= false;
01360 enum_field_types f_type;
01361
01362 switch ((f_type=field_type())) {
01363 case DRIZZLE_TYPE_DATE:
01364 case DRIZZLE_TYPE_NULL:
01365 case DRIZZLE_TYPE_ENUM:
01366 case DRIZZLE_TYPE_BLOB:
01367 case DRIZZLE_TYPE_VARCHAR:
01368 case DRIZZLE_TYPE_BOOLEAN:
01369 case DRIZZLE_TYPE_UUID:
01370 case DRIZZLE_TYPE_DECIMAL:
01371 {
01372 String *res;
01373 if ((res=val_str(buffer)))
01374 result= client->store(res->ptr(),res->length());
01375 break;
01376 }
01377 case DRIZZLE_TYPE_LONG:
01378 {
01379 int64_t nr;
01380 nr= val_int();
01381 if (!null_value)
01382 result= client->store((int32_t)nr);
01383 break;
01384 }
01385 case DRIZZLE_TYPE_LONGLONG:
01386 {
01387 int64_t nr;
01388 nr= val_int();
01389 if (!null_value)
01390 {
01391 if (unsigned_flag)
01392 result= client->store((uint64_t)nr);
01393 else
01394 result= client->store((int64_t)nr);
01395 }
01396 break;
01397 }
01398 case DRIZZLE_TYPE_DOUBLE:
01399 {
01400 double nr= val_real();
01401 if (!null_value)
01402 result= client->store(nr, decimals, buffer);
01403 break;
01404 }
01405 case DRIZZLE_TYPE_TIME:
01406 {
01407 type::Time tm;
01408 get_time(tm);
01409 if (not null_value)
01410 result= client->store(&tm);
01411 break;
01412 }
01413 case DRIZZLE_TYPE_DATETIME:
01414 case DRIZZLE_TYPE_MICROTIME:
01415 case DRIZZLE_TYPE_TIMESTAMP:
01416 {
01417 type::Time tm;
01418 get_date(tm, TIME_FUZZY_DATE);
01419 if (!null_value)
01420 result= client->store(&tm);
01421 break;
01422 }
01423 }
01424 if (null_value)
01425 result= client->store();
01426
01427 return result;
01428 }
01429
01430 uint32_t Item::max_char_length() const
01431 {
01432 return max_length / collation.collation->mbmaxlen;
01433 }
01434
01435 void Item::fix_length_and_charset(uint32_t max_char_length_arg, CHARSET_INFO *cs)
01436 {
01437 max_length= char_to_byte_length_safe(max_char_length_arg, cs->mbmaxlen);
01438 collation.collation= cs;
01439 }
01440
01441 void Item::fix_char_length(uint32_t max_char_length_arg)
01442 {
01443 max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen);
01444 }
01445
01446 void Item::fix_char_length_uint64_t(uint64_t max_char_length_arg)
01447 {
01448 uint64_t max_result_length= max_char_length_arg *
01449 collation.collation->mbmaxlen;
01450
01451 if (max_result_length >= MAX_BLOB_WIDTH)
01452 {
01453 max_length= MAX_BLOB_WIDTH;
01454 maybe_null= false;
01455 }
01456 else
01457 {
01458 max_length= max_result_length;
01459 }
01460 }
01461
01462 void Item::fix_length_and_charset_datetime(uint32_t max_char_length_arg)
01463 {
01464 collation.set(&my_charset_bin);
01465 fix_char_length(max_char_length_arg);
01466 }
01467
01468 Item_result item_cmp_type(Item_result a,Item_result b)
01469 {
01470 if (a == STRING_RESULT && b == STRING_RESULT)
01471 return STRING_RESULT;
01472
01473 if (a == INT_RESULT && b == INT_RESULT)
01474 return INT_RESULT;
01475 else if (a == ROW_RESULT || b == ROW_RESULT)
01476 return ROW_RESULT;
01477
01478 if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
01479 (b == INT_RESULT || b == DECIMAL_RESULT))
01480 return DECIMAL_RESULT;
01481
01482 return REAL_RESULT;
01483 }
01484
01485 void resolve_const_item(Session *session, Item **ref, Item *comp_item)
01486 {
01487 Item *item= *ref;
01488 Item *new_item= NULL;
01489 if (item->basic_const_item())
01490 return;
01491 Item_result res_type=item_cmp_type(comp_item->result_type(),
01492 item->result_type());
01493 char *name=item->name;
01494
01495 switch (res_type) {
01496 case STRING_RESULT:
01497 {
01498 char buff[MAX_FIELD_WIDTH];
01499 String tmp(buff,sizeof(buff),&my_charset_bin),*result;
01500 result=item->val_str(&tmp);
01501 if (item->null_value)
01502 new_item= new Item_null(name);
01503 else
01504 {
01505 uint32_t length= result->length();
01506 char *tmp_str= memory::sql_strmake(result->ptr(), length);
01507 new_item= new Item_string(name, tmp_str, length, result->charset());
01508 }
01509 break;
01510 }
01511 case INT_RESULT:
01512 {
01513 int64_t result=item->val_int();
01514 uint32_t length=item->max_length;
01515 bool null_value=item->null_value;
01516 new_item= (null_value ? (Item*) new Item_null(name) :
01517 (Item*) new Item_int(name, result, length));
01518 break;
01519 }
01520 case ROW_RESULT:
01521 if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
01522 {
01523
01524
01525
01526
01527
01528
01529
01530
01531 Item_row *item_row= (Item_row*) item;
01532 Item_row *comp_item_row= (Item_row*) comp_item;
01533 uint32_t col;
01534 new_item= 0;
01535
01536
01537
01538
01539
01540
01541 assert(item->result_type() == comp_item->result_type());
01542 assert(item_row->cols() == comp_item_row->cols());
01543 col= item_row->cols();
01544 while (col-- > 0)
01545 resolve_const_item(session, item_row->addr(col),
01546 comp_item_row->element_index(col));
01547 break;
01548 }
01549
01550 case REAL_RESULT:
01551 {
01552 double result= item->val_real();
01553 uint32_t length=item->max_length,decimals=item->decimals;
01554 bool null_value=item->null_value;
01555 new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
01556 new Item_float(name, result, decimals, length));
01557 break;
01558 }
01559 case DECIMAL_RESULT:
01560 {
01561 type::Decimal decimal_value;
01562 type::Decimal *result= item->val_decimal(&decimal_value);
01563 uint32_t length= item->max_length, decimals= item->decimals;
01564 bool null_value= item->null_value;
01565 new_item= (null_value ?
01566 (Item*) new Item_null(name) :
01567 (Item*) new Item_decimal(name, result, length, decimals));
01568 break;
01569 }
01570 }
01571
01572 if (new_item)
01573 *ref= new_item;
01574 }
01575
01576 bool field_is_equal_to_item(Field *field,Item *item)
01577 {
01578
01579 Item_result res_type=item_cmp_type(field->result_type(),
01580 item->result_type());
01581 if (res_type == STRING_RESULT)
01582 {
01583 char item_buff[MAX_FIELD_WIDTH];
01584 char field_buff[MAX_FIELD_WIDTH];
01585 String item_tmp(item_buff,sizeof(item_buff),&my_charset_bin),*item_result;
01586 String field_tmp(field_buff,sizeof(field_buff),&my_charset_bin);
01587 item_result=item->val_str(&item_tmp);
01588 if (item->null_value)
01589 return 1;
01590 field->val_str_internal(&field_tmp);
01591 return not stringcmp(&field_tmp,item_result);
01592 }
01593
01594 if (res_type == INT_RESULT)
01595 return 1;
01596
01597 if (res_type == DECIMAL_RESULT)
01598 {
01599 type::Decimal item_buf, *item_val,
01600 field_buf, *field_val;
01601 item_val= item->val_decimal(&item_buf);
01602 if (item->null_value)
01603 return 1;
01604 field_val= field->val_decimal(&field_buf);
01605 return !class_decimal_cmp(item_val, field_val);
01606 }
01607
01608 double result= item->val_real();
01609 if (item->null_value)
01610 return 1;
01611
01612 return result == field->val_real();
01613 }
01614
01615 void dummy_error_processor(Session *, void *)
01616 {}
01617
01641 static Field *create_tmp_field_from_item(Session *,
01642 Item *item, Table *table,
01643 Item ***copy_func, bool modify_item,
01644 uint32_t convert_blob_length)
01645 {
01646 bool maybe_null= item->maybe_null;
01647 Field *new_field= NULL;
01648
01649 switch (item->result_type()) {
01650 case REAL_RESULT:
01651 new_field= new Field_double(item->max_length, maybe_null,
01652 item->name, item->decimals, true);
01653 break;
01654
01655 case INT_RESULT:
01656
01657
01658
01659
01660
01661
01662 if (item->unsigned_flag)
01663 {
01664 new_field= new field::Size(item->max_length, maybe_null,
01665 item->name, item->unsigned_flag);
01666 }
01667 else if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
01668 {
01669 new_field= new field::Int64(item->max_length, maybe_null,
01670 item->name, item->unsigned_flag);
01671 }
01672 else
01673 {
01674 new_field= new field::Int32(item->max_length, maybe_null,
01675 item->name, item->unsigned_flag);
01676 }
01677
01678 break;
01679
01680 case STRING_RESULT:
01681 assert(item->collation.collation);
01682
01683
01684
01685
01686
01687 if (field::isDateTime(item->field_type()))
01688 {
01689 new_field= item->tmp_table_field_from_field_type(table, 1);
01690
01691
01692
01693
01694 }
01695 else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
01696 convert_blob_length <= Field_varstring::MAX_SIZE &&
01697 convert_blob_length)
01698 {
01699 table->setVariableWidth();
01700 new_field= new Field_varstring(convert_blob_length, maybe_null,
01701 item->name, item->collation.collation);
01702 }
01703 else
01704 {
01705 new_field= item->make_string_field(table);
01706 }
01707 new_field->set_derivation(item->collation.derivation);
01708 break;
01709
01710 case DECIMAL_RESULT:
01711 {
01712 uint8_t dec= item->decimals;
01713 uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
01714 uint32_t len= item->max_length;
01715
01716
01717
01718
01719
01720
01721
01722 if (dec > 0)
01723 {
01724 signed int overflow;
01725
01726 dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
01727
01728
01729
01730
01731
01732
01733
01734
01735 overflow= class_decimal_precision_to_length(intg + dec, dec,
01736 item->unsigned_flag) - len;
01737
01738 if (overflow > 0)
01739 dec= max(0, dec - overflow);
01740 else
01741 len-= item->decimals - dec;
01742 }
01743
01744 new_field= new Field_decimal(len,
01745 maybe_null,
01746 item->name,
01747 dec,
01748 item->unsigned_flag);
01749 break;
01750 }
01751
01752 case ROW_RESULT:
01753
01754 assert(0);
01755 abort();
01756 }
01757
01758 if (new_field)
01759 new_field->init(table);
01760
01761 if (copy_func && item->is_result_field())
01762 *((*copy_func)++) = item;
01763
01764 if (modify_item)
01765 item->set_result_field(new_field);
01766
01767 if (item->type() == Item::NULL_ITEM)
01768 new_field->is_created_from_null_item= true;
01769
01770 return new_field;
01771 }
01772
01773 Field *create_tmp_field(Session *session,
01774 Table *table,
01775 Item *item,
01776 Item::Type type,
01777 Item ***copy_func,
01778 Field **from_field,
01779 Field **default_field,
01780 bool group,
01781 bool modify_item,
01782 bool make_copy_field,
01783 uint32_t convert_blob_length)
01784 {
01785 Field *result;
01786 Item::Type orig_type= type;
01787 Item *orig_item= 0;
01788
01789 if (type != Item::FIELD_ITEM &&
01790 item->real_item()->type() == Item::FIELD_ITEM)
01791 {
01792 orig_item= item;
01793 item= item->real_item();
01794 type= Item::FIELD_ITEM;
01795 }
01796
01797 switch (type) {
01798 case Item::SUM_FUNC_ITEM:
01799 {
01800 Item_sum *item_sum=(Item_sum*) item;
01801 result= item_sum->create_tmp_field(group, table, convert_blob_length);
01802 if (!result)
01803 my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
01804 return result;
01805 }
01806 case Item::FIELD_ITEM:
01807 case Item::DEFAULT_VALUE_ITEM:
01808 {
01809 Item_field *field= (Item_field*) item;
01810 bool orig_modify= modify_item;
01811 if (orig_type == Item::REF_ITEM)
01812 modify_item= 0;
01813
01814
01815
01816
01817 if (field->maybe_null && !field->field->maybe_null())
01818 {
01819 result= create_tmp_field_from_item(session, item, table, NULL,
01820 modify_item, convert_blob_length);
01821 *from_field= field->field;
01822 if (result && modify_item)
01823 field->result_field= result;
01824 }
01825 else
01826 {
01827 result= create_tmp_field_from_field(session, (*from_field= field->field),
01828 orig_item ? orig_item->name :
01829 item->name,
01830 table,
01831 modify_item ? field :
01832 NULL,
01833 convert_blob_length);
01834 }
01835 if (orig_type == Item::REF_ITEM && orig_modify)
01836 ((Item_ref*)orig_item)->set_result_field(result);
01837 if (field->field->eq_def(result))
01838 *default_field= field->field;
01839 return result;
01840 }
01841
01842 case Item::FUNC_ITEM:
01843
01844 case Item::COND_ITEM:
01845 case Item::FIELD_AVG_ITEM:
01846 case Item::FIELD_STD_ITEM:
01847 case Item::SUBSELECT_ITEM:
01848
01849 case Item::PROC_ITEM:
01850 case Item::INT_ITEM:
01851 case Item::REAL_ITEM:
01852 case Item::DECIMAL_ITEM:
01853 case Item::STRING_ITEM:
01854 case Item::REF_ITEM:
01855 case Item::NULL_ITEM:
01856 case Item::VARBIN_ITEM:
01857 if (make_copy_field)
01858 {
01859 assert(((Item_result_field*)item)->result_field);
01860 *from_field= ((Item_result_field*)item)->result_field;
01861 }
01862 return create_tmp_field_from_item(session, item, table,
01863 (make_copy_field ? 0 : copy_func),
01864 modify_item, convert_blob_length);
01865 case Item::TYPE_HOLDER:
01866 result= ((Item_type_holder *)item)->make_field_by_type(table);
01867 result->set_derivation(item->collation.derivation);
01868 return result;
01869 default:
01870 return NULL;
01871 }
01872 }
01873
01874 std::ostream& operator<<(std::ostream& output, const Item &item)
01875 {
01876 switch (item.type())
01877 {
01878 case drizzled::Item::SUBSELECT_ITEM :
01879 case drizzled::Item::FIELD_ITEM :
01880 case drizzled::Item::SUM_FUNC_ITEM :
01881 case drizzled::Item::STRING_ITEM :
01882 case drizzled::Item::INT_ITEM :
01883 case drizzled::Item::REAL_ITEM :
01884 case drizzled::Item::NULL_ITEM :
01885 case drizzled::Item::VARBIN_ITEM :
01886 case drizzled::Item::COPY_STR_ITEM :
01887 case drizzled::Item::FIELD_AVG_ITEM :
01888 case drizzled::Item::DEFAULT_VALUE_ITEM :
01889 case drizzled::Item::PROC_ITEM :
01890 case drizzled::Item::COND_ITEM :
01891 case drizzled::Item::REF_ITEM :
01892 case drizzled::Item::FIELD_STD_ITEM :
01893 case drizzled::Item::FIELD_VARIANCE_ITEM :
01894 case drizzled::Item::INSERT_VALUE_ITEM :
01895 case drizzled::Item::ROW_ITEM:
01896 case drizzled::Item::CACHE_ITEM :
01897 case drizzled::Item::TYPE_HOLDER :
01898 case drizzled::Item::PARAM_ITEM :
01899 case drizzled::Item::DECIMAL_ITEM :
01900 case drizzled::Item::FUNC_ITEM :
01901 case drizzled::Item::BOOLEAN_ITEM :
01902 {
01903 output << "Item:(";
01904 output << item.full_name();
01905 output << ", ";
01906 output << drizzled::display::type(item.type());
01907 output << ")";
01908 }
01909 break;
01910 }
01911
01912 return output;
01913 }
01914
01915 }