00001 /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 00018 #ifdef USE_PRAGMA_IMPLEMENTATION 00019 #pragma implementation // gcc: Class implementation 00020 #endif 00021 #include "mysql_priv.h" 00022 #include <m_ctype.h> 00023 #include "my_dir.h" 00024 #include "sp_rcontext.h" 00025 #include "sp_head.h" 00026 #include "sql_trigger.h" 00027 #include "sql_select.h" 00028 00029 static void mark_as_dependent(THD *thd, 00030 SELECT_LEX *last, SELECT_LEX *current, 00031 Item_ident *item); 00032 00033 const String my_null_string("NULL", 4, default_charset_info); 00034 00035 /****************************************************************************/ 00036 00037 /* Hybrid_type_traits {_real} */ 00038 00039 void Hybrid_type_traits::fix_length_and_dec(Item *item, Item *arg) const 00040 { 00041 item->decimals= NOT_FIXED_DEC; 00042 item->max_length= item->float_length(arg->decimals); 00043 } 00044 00045 static const Hybrid_type_traits real_traits_instance; 00046 00047 const Hybrid_type_traits *Hybrid_type_traits::instance() 00048 { 00049 return &real_traits_instance; 00050 } 00051 00052 00053 my_decimal * 00054 Hybrid_type_traits::val_decimal(Hybrid_type *val, my_decimal *to) const 00055 { 00056 double2my_decimal(E_DEC_FATAL_ERROR, val->real, val->dec_buf); 00057 return val->dec_buf; 00058 } 00059 00060 00061 String * 00062 Hybrid_type_traits::val_str(Hybrid_type *val, String *to, uint8 decimals) const 00063 { 00064 to->set_real(val->real, decimals, &my_charset_bin); 00065 return to; 00066 } 00067 00068 /* Hybrid_type_traits_decimal */ 00069 static const Hybrid_type_traits_decimal decimal_traits_instance; 00070 00071 const Hybrid_type_traits_decimal *Hybrid_type_traits_decimal::instance() 00072 { 00073 return &decimal_traits_instance; 00074 } 00075 00076 00077 void 00078 Hybrid_type_traits_decimal::fix_length_and_dec(Item *item, Item *arg) const 00079 { 00080 item->decimals= arg->decimals; 00081 item->max_length= min(arg->max_length + DECIMAL_LONGLONG_DIGITS, 00082 DECIMAL_MAX_STR_LENGTH); 00083 } 00084 00085 00086 void Hybrid_type_traits_decimal::set_zero(Hybrid_type *val) const 00087 { 00088 my_decimal_set_zero(&val->dec_buf[0]); 00089 val->used_dec_buf_no= 0; 00090 } 00091 00092 00093 void Hybrid_type_traits_decimal::add(Hybrid_type *val, Field *f) const 00094 { 00095 my_decimal_add(E_DEC_FATAL_ERROR, 00096 &val->dec_buf[val->used_dec_buf_no ^ 1], 00097 &val->dec_buf[val->used_dec_buf_no], 00098 f->val_decimal(&val->dec_buf[2])); 00099 val->used_dec_buf_no^= 1; 00100 } 00101 00102 00103 void Hybrid_type_traits_decimal::div(Hybrid_type *val, ulonglong u) const 00104 { 00105 int2my_decimal(E_DEC_FATAL_ERROR, u, TRUE, &val->dec_buf[2]); 00106 /* XXX: what is '4' for scale? */ 00107 my_decimal_div(E_DEC_FATAL_ERROR, 00108 &val->dec_buf[val->used_dec_buf_no ^ 1], 00109 &val->dec_buf[val->used_dec_buf_no], 00110 &val->dec_buf[2], 4); 00111 val->used_dec_buf_no^= 1; 00112 } 00113 00114 00115 longlong 00116 Hybrid_type_traits_decimal::val_int(Hybrid_type *val, bool unsigned_flag) const 00117 { 00118 longlong result; 00119 my_decimal2int(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no], 00120 unsigned_flag, &result); 00121 return result; 00122 } 00123 00124 00125 double 00126 Hybrid_type_traits_decimal::val_real(Hybrid_type *val) const 00127 { 00128 my_decimal2double(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no], 00129 &val->real); 00130 return val->real; 00131 } 00132 00133 00134 String * 00135 Hybrid_type_traits_decimal::val_str(Hybrid_type *val, String *to, 00136 uint8 decimals) const 00137 { 00138 my_decimal_round(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no], 00139 decimals, FALSE, &val->dec_buf[2]); 00140 my_decimal2string(E_DEC_FATAL_ERROR, &val->dec_buf[2], 0, 0, 0, to); 00141 return to; 00142 } 00143 00144 /* Hybrid_type_traits_integer */ 00145 static const Hybrid_type_traits_integer integer_traits_instance; 00146 00147 const Hybrid_type_traits_integer *Hybrid_type_traits_integer::instance() 00148 { 00149 return &integer_traits_instance; 00150 } 00151 00152 void 00153 Hybrid_type_traits_integer::fix_length_and_dec(Item *item, Item *arg) const 00154 { 00155 item->decimals= 0; 00156 item->max_length= 21; 00157 item->unsigned_flag= 0; 00158 } 00159 00160 /***************************************************************************** 00161 ** Item functions 00162 *****************************************************************************/ 00163 00164 /* Init all special items */ 00165 00166 void item_init(void) 00167 { 00168 item_user_lock_init(); 00169 } 00170 00171 00172 /* 00173 TODO: make this functions class dependent 00174 */ 00175 00176 bool Item::val_bool() 00177 { 00178 switch(result_type()) { 00179 case INT_RESULT: 00180 return val_int() != 0; 00181 case DECIMAL_RESULT: 00182 { 00183 my_decimal decimal_value; 00184 my_decimal *val= val_decimal(&decimal_value); 00185 if (val) 00186 return !my_decimal_is_zero(val); 00187 return 0; 00188 } 00189 case REAL_RESULT: 00190 case STRING_RESULT: 00191 return val_real() != 0.0; 00192 case ROW_RESULT: 00193 default: 00194 DBUG_ASSERT(0); 00195 return 0; // Wrong (but safe) 00196 } 00197 } 00198 00199 00200 String *Item::val_string_from_real(String *str) 00201 { 00202 double nr= val_real(); 00203 if (null_value) 00204 return 0; /* purecov: inspected */ 00205 str->set_real(nr,decimals, &my_charset_bin); 00206 return str; 00207 } 00208 00209 00210 String *Item::val_string_from_int(String *str) 00211 { 00212 longlong nr= val_int(); 00213 if (null_value) 00214 return 0; 00215 str->set_int(nr, unsigned_flag, &my_charset_bin); 00216 return str; 00217 } 00218 00219 00220 String *Item::val_string_from_decimal(String *str) 00221 { 00222 my_decimal dec_buf, *dec= val_decimal(&dec_buf); 00223 if (null_value) 00224 return 0; 00225 my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, FALSE, &dec_buf); 00226 my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, 0, str); 00227 return str; 00228 } 00229 00230 00231 my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value) 00232 { 00233 double nr= val_real(); 00234 if (null_value) 00235 return 0; 00236 double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value); 00237 return (decimal_value); 00238 } 00239 00240 00241 my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value) 00242 { 00243 longlong nr= val_int(); 00244 if (null_value) 00245 return 0; 00246 int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value); 00247 return decimal_value; 00248 } 00249 00250 00251 my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value) 00252 { 00253 String *res; 00254 char *end_ptr; 00255 if (!(res= val_str(&str_value))) 00256 return 0; // NULL or EOM 00257 00258 end_ptr= (char*) res->ptr()+ res->length(); 00259 if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM, 00260 res->ptr(), res->length(), res->charset(), 00261 decimal_value) & E_DEC_BAD_NUM) 00262 { 00263 push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, 00264 ER_TRUNCATED_WRONG_VALUE, 00265 ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL", 00266 str_value.c_ptr()); 00267 } 00268 return decimal_value; 00269 } 00270 00271 00272 double Item::val_real_from_decimal() 00273 { 00274 /* Note that fix_fields may not be called for Item_avg_field items */ 00275 double result; 00276 my_decimal value_buff, *dec_val= val_decimal(&value_buff); 00277 if (null_value) 00278 return 0.0; 00279 my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result); 00280 return result; 00281 } 00282 00283 00284 longlong Item::val_int_from_decimal() 00285 { 00286 /* Note that fix_fields may not be called for Item_avg_field items */ 00287 longlong result; 00288 my_decimal value, *dec_val= val_decimal(&value); 00289 if (null_value) 00290 return 0; 00291 my_decimal2int(E_DEC_FATAL_ERROR, dec_val, unsigned_flag, &result); 00292 return result; 00293 } 00294 00295 00296 Item::Item(): 00297 rsize(0), name(0), orig_name(0), name_length(0), fixed(0), 00298 is_autogenerated_name(TRUE), 00299 collation(&my_charset_bin, DERIVATION_COERCIBLE) 00300 { 00301 marker= 0; 00302 maybe_null=null_value=with_sum_func=unsigned_flag=0; 00303 decimals= 0; max_length= 0; 00304 with_subselect= 0; 00305 00306 /* Put item in free list so that we can free all items at end */ 00307 THD *thd= current_thd; 00308 next= thd->free_list; 00309 thd->free_list= this; 00310 /* 00311 Item constructor can be called during execution other then SQL_COM 00312 command => we should check thd->lex->current_select on zero (thd->lex 00313 can be uninitialised) 00314 */ 00315 if (thd->lex->current_select) 00316 { 00317 enum_parsing_place place= 00318 thd->lex->current_select->parsing_place; 00319 if (place == SELECT_LIST || 00320 place == IN_HAVING) 00321 thd->lex->current_select->select_n_having_items++; 00322 } 00323 } 00324 00325 /* 00326 Constructor used by Item_field, Item_*_ref & aggregate (sum) functions. 00327 Used for duplicating lists in processing queries with temporary 00328 tables 00329 */ 00330 Item::Item(THD *thd, Item *item): 00331 rsize(0), 00332 str_value(item->str_value), 00333 name(item->name), 00334 orig_name(item->orig_name), 00335 max_length(item->max_length), 00336 marker(item->marker), 00337 decimals(item->decimals), 00338 maybe_null(item->maybe_null), 00339 null_value(item->null_value), 00340 unsigned_flag(item->unsigned_flag), 00341 with_sum_func(item->with_sum_func), 00342 fixed(item->fixed), 00343 collation(item->collation) 00344 { 00345 next= thd->free_list; // Put in free list 00346 thd->free_list= this; 00347 } 00348 00349 00350 uint Item::decimal_precision() const 00351 { 00352 Item_result restype= result_type(); 00353 00354 if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT)) 00355 return min(my_decimal_length_to_precision(max_length, decimals, unsigned_flag), 00356 DECIMAL_MAX_PRECISION); 00357 return min(max_length, DECIMAL_MAX_PRECISION); 00358 } 00359 00360 00361 void Item::print_item_w_name(String *str) 00362 { 00363 print(str); 00364 if (name) 00365 { 00366 THD *thd= current_thd; 00367 str->append(STRING_WITH_LEN(" AS ")); 00368 append_identifier(thd, str, name, (uint) strlen(name)); 00369 } 00370 } 00371 00372 00373 void Item::cleanup() 00374 { 00375 DBUG_ENTER("Item::cleanup"); 00376 fixed=0; 00377 marker= 0; 00378 if (orig_name) 00379 name= orig_name; 00380 DBUG_VOID_RETURN; 00381 } 00382 00383 00384 /* 00385 cleanup() item if it is 'fixed' 00386 00387 SYNOPSIS 00388 cleanup_processor() 00389 arg - a dummy parameter, is not used here 00390 */ 00391 00392 bool Item::cleanup_processor(byte *arg) 00393 { 00394 if (fixed) 00395 cleanup(); 00396 return FALSE; 00397 } 00398 00399 00400 /* 00401 rename item (used for views, cleanup() return original name) 00402 00403 SYNOPSIS 00404 Item::rename() 00405 new_name new name of item; 00406 */ 00407 00408 void Item::rename(char *new_name) 00409 { 00410 /* 00411 we can compare pointers to names here, because if name was not changed, 00412 pointer will be same 00413 */ 00414 if (!orig_name && new_name != name) 00415 orig_name= name; 00416 name= new_name; 00417 } 00418 00419 00420 Item_ident::Item_ident(Name_resolution_context *context_arg, 00421 const char *db_name_arg,const char *table_name_arg, 00422 const char *field_name_arg) 00423 :orig_db_name(db_name_arg), orig_table_name(table_name_arg), 00424 orig_field_name(field_name_arg), context(context_arg), 00425 db_name(db_name_arg), table_name(table_name_arg), 00426 field_name(field_name_arg), 00427 alias_name_used(FALSE), cached_field_index(NO_CACHED_FIELD_INDEX), 00428 cached_table(0), depended_from(0) 00429 { 00430 name = (char*) field_name_arg; 00431 } 00432 00433 00434 /* Constructor used by Item_field & Item_*_ref (see Item comment) */ 00435 00436 Item_ident::Item_ident(THD *thd, Item_ident *item) 00437 :Item(thd, item), 00438 orig_db_name(item->orig_db_name), 00439 orig_table_name(item->orig_table_name), 00440 orig_field_name(item->orig_field_name), 00441 context(item->context), 00442 db_name(item->db_name), 00443 table_name(item->table_name), 00444 field_name(item->field_name), 00445 alias_name_used(item->alias_name_used), 00446 cached_field_index(item->cached_field_index), 00447 cached_table(item->cached_table), 00448 depended_from(item->depended_from) 00449 {} 00450 00451 void Item_ident::cleanup() 00452 { 00453 DBUG_ENTER("Item_ident::cleanup"); 00454 #ifdef CANT_BE_USED_AS_MEMORY_IS_FREED 00455 db_name ? db_name : "(null)", 00456 orig_db_name ? orig_db_name : "(null)", 00457 table_name ? table_name : "(null)", 00458 orig_table_name ? orig_table_name : "(null)", 00459 field_name ? field_name : "(null)", 00460 orig_field_name ? orig_field_name : "(null)")); 00461 #endif 00462 Item::cleanup(); 00463 db_name= orig_db_name; 00464 table_name= orig_table_name; 00465 field_name= orig_field_name; 00466 depended_from= 0; 00467 DBUG_VOID_RETURN; 00468 } 00469 00470 bool Item_ident::remove_dependence_processor(byte * arg) 00471 { 00472 DBUG_ENTER("Item_ident::remove_dependence_processor"); 00473 if (depended_from == (st_select_lex *) arg) 00474 depended_from= 0; 00475 DBUG_RETURN(0); 00476 } 00477 00478 00479 /* 00480 Store the pointer to this item field into a list if not already there. 00481 00482 SYNOPSIS 00483 Item_field::collect_item_field_processor() 00484 arg pointer to a List<Item_field> 00485 00486 DESCRIPTION 00487 The method is used by Item::walk to collect all unique Item_field objects 00488 from a tree of Items into a set of items represented as a list. 00489 00490 IMPLEMENTATION 00491 Item_cond::walk() and Item_func::walk() stop the evaluation of the 00492 processor function for its arguments once the processor returns 00493 true.Therefore in order to force this method being called for all item 00494 arguments in a condition the method must return false. 00495 00496 RETURN 00497 FALSE to force the evaluation of collect_item_field_processor 00498 for the subsequent items. 00499 */ 00500 00501 bool Item_field::collect_item_field_processor(byte *arg) 00502 { 00503 DBUG_ENTER("Item_field::collect_item_field_processor"); 00504 DBUG_PRINT("info", ("%s", field->field_name ? field->field_name : "noname")); 00505 List<Item_field> *item_list= (List<Item_field>*) arg; 00506 List_iterator<Item_field> item_list_it(*item_list); 00507 Item_field *curr_item; 00508 while ((curr_item= item_list_it++)) 00509 { 00510 if (curr_item->eq(this, 1)) 00511 DBUG_RETURN(FALSE); /* Already in the set. */ 00512 } 00513 item_list->push_back(this); 00514 DBUG_RETURN(FALSE); 00515 } 00516 00517 00518 /* 00519 Check if an Item_field references some field from a list of fields. 00520 00521 SYNOPSIS 00522 Item_field::find_item_in_field_list_processor 00523 arg Field being compared, arg must be of type Field 00524 00525 DESCRIPTION 00526 Check whether the Item_field represented by 'this' references any 00527 of the fields in the keyparts passed via 'arg'. Used with the 00528 method Item::walk() to test whether any keypart in a sequence of 00529 keyparts is referenced in an expression. 00530 00531 RETURN 00532 TRUE if 'this' references the field 'arg' 00533 FALSE otherwise 00534 */ 00535 00536 bool Item_field::find_item_in_field_list_processor(byte *arg) 00537 { 00538 KEY_PART_INFO *first_non_group_part= *((KEY_PART_INFO **) arg); 00539 KEY_PART_INFO *last_part= *(((KEY_PART_INFO **) arg) + 1); 00540 KEY_PART_INFO *cur_part; 00541 00542 for (cur_part= first_non_group_part; cur_part != last_part; cur_part++) 00543 { 00544 if (field->eq(cur_part->field)) 00545 return TRUE; 00546 } 00547 return FALSE; 00548 } 00549 00550 00551 /* 00552 Mark field in read_map 00553 00554 NOTES 00555 This is used by filesort to register used fields in a a temporary 00556 column read set or to register used fields in a view 00557 */ 00558 00559 bool Item_field::register_field_in_read_map(byte *arg) 00560 { 00561 TABLE *table= (TABLE *) arg; 00562 if (field->table == table || !table) 00563 bitmap_set_bit(field->table->read_set, field->field_index); 00564 return 0; 00565 } 00566 00567 00568 bool Item::check_cols(uint c) 00569 { 00570 if (c != 1) 00571 { 00572 my_error(ER_OPERAND_COLUMNS, MYF(0), c); 00573 return 1; 00574 } 00575 return 0; 00576 } 00577 00578 00579 void Item::set_name(const char *str, uint length, CHARSET_INFO *cs) 00580 { 00581 if (!length) 00582 { 00583 /* Empty string, used by AS or internal function like last_insert_id() */ 00584 name= (char*) str; 00585 name_length= 0; 00586 return; 00587 } 00588 if (cs->ctype) 00589 { 00590 uint orig_len= length; 00591 /* 00592 This will probably need a better implementation in the future: 00593 a function in CHARSET_INFO structure. 00594 */ 00595 while (length && !my_isgraph(cs,*str)) 00596 { // Fix problem with yacc 00597 length--; 00598 str++; 00599 } 00600 if (orig_len != length && !is_autogenerated_name) 00601 push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, 00602 ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES), 00603 str + length - orig_len); 00604 00605 } 00606 if (!my_charset_same(cs, system_charset_info)) 00607 { 00608 uint32 res_length; 00609 name= sql_strmake_with_convert(str, name_length= length, cs, 00610 MAX_ALIAS_NAME, system_charset_info, 00611 &res_length); 00612 } 00613 else 00614 name= sql_strmake(str, (name_length= min(length,MAX_ALIAS_NAME))); 00615 } 00616 00617 00618 /* 00619 This function is called when: 00620 - Comparing items in the WHERE clause (when doing where optimization) 00621 - When trying to find an ORDER BY/GROUP BY item in the SELECT part 00622 */ 00623 00624 bool Item::eq(const Item *item, bool binary_cmp) const 00625 { 00626 /* 00627 Note, that this is never TRUE if item is a Item_param: 00628 for all basic constants we have special checks, and Item_param's 00629 type() can be only among basic constant types. 00630 */ 00631 return type() == item->type() && name && item->name && 00632 !my_strcasecmp(system_charset_info,name,item->name); 00633 } 00634 00635 00636 Item *Item::safe_charset_converter(CHARSET_INFO *tocs) 00637 { 00638 Item_func_conv_charset *conv= new Item_func_conv_charset(this, tocs, 1); 00639 return conv->safe ? conv : NULL; 00640 } 00641 00642 00643 /* 00644 Created mostly for mysql_prepare_table(). Important 00645 when a string ENUM/SET column is described with a numeric default value: 00646 00647 CREATE TABLE t1(a SET('a') DEFAULT 1); 00648 00649 We cannot use generic Item::safe_charset_converter(), because 00650 the latter returns a non-fixed Item, so val_str() crashes afterwards. 00651 Override Item_num method, to return a fixed item. 00652 */ 00653 Item *Item_num::safe_charset_converter(CHARSET_INFO *tocs) 00654 { 00655 Item_string *conv; 00656 char buf[64]; 00657 String *s, tmp(buf, sizeof(buf), &my_charset_bin); 00658 s= val_str(&tmp); 00659 if ((conv= new Item_string(s->ptr(), s->length(), s->charset()))) 00660 { 00661 conv->str_value.copy(); 00662 conv->str_value.mark_as_const(); 00663 } 00664 return conv; 00665 } 00666 00667 00668 Item *Item_static_float_func::safe_charset_converter(CHARSET_INFO *tocs) 00669 { 00670 Item_string *conv; 00671 char buf[64]; 00672 String *s, tmp(buf, sizeof(buf), &my_charset_bin); 00673 s= val_str(&tmp); 00674 if ((conv= new Item_static_string_func(func_name, s->ptr(), s->length(), 00675 s->charset()))) 00676 { 00677 conv->str_value.copy(); 00678 conv->str_value.mark_as_const(); 00679 } 00680 return conv; 00681 } 00682 00683 00684 Item *Item_string::safe_charset_converter(CHARSET_INFO *tocs) 00685 { 00686 Item_string *conv; 00687 uint conv_errors; 00688 char *ptr; 00689 String tmp, cstr, *ostr= val_str(&tmp); 00690 cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors); 00691 if (conv_errors || !(conv= new Item_string(cstr.ptr(), cstr.length(), 00692 cstr.charset(), 00693 collation.derivation))) 00694 { 00695 /* 00696 Safe conversion is not possible (or EOM). 00697 We could not convert a string into the requested character set 00698 without data loss. The target charset does not cover all the 00699 characters from the string. Operation cannot be done correctly. 00700 */ 00701 return NULL; 00702 } 00703 if (!(ptr= current_thd->memdup(cstr.ptr(), cstr.length() + 1 ))) 00704 return NULL; 00705 conv->str_value.set(ptr, cstr.length(), cstr.charset()); 00706 /* Ensure that no one is going to change the result string */ 00707 conv->str_value.mark_as_const(); 00708 return conv; 00709 } 00710 00711 00712 Item *Item_param::safe_charset_converter(CHARSET_INFO *tocs) 00713 { 00714 if (const_item()) 00715 { 00716 uint cnv_errors; 00717 String *ostr= val_str(&cnvstr); 00718 cnvitem->str_value.copy(ostr->ptr(), ostr->length(), 00719 ostr->charset(), tocs, &cnv_errors); 00720 if (cnv_errors) 00721 return NULL; 00722 cnvitem->str_value.mark_as_const(); 00723 cnvitem->max_length= cnvitem->str_value.numchars() * tocs->mbmaxlen; 00724 return cnvitem; 00725 } 00726 return NULL; 00727 } 00728 00729 00730 Item *Item_static_string_func::safe_charset_converter(CHARSET_INFO *tocs) 00731 { 00732 Item_string *conv; 00733 uint conv_errors; 00734 String tmp, cstr, *ostr= val_str(&tmp); 00735 cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors); 00736 if (conv_errors || 00737 !(conv= new Item_static_string_func(func_name, 00738 cstr.ptr(), cstr.length(), 00739 cstr.charset(), 00740 collation.derivation))) 00741 { 00742 /* 00743 Safe conversion is not possible (or EOM). 00744 We could not convert a string into the requested character set 00745 without data loss. The target charset does not cover all the 00746 characters from the string. Operation cannot be done correctly. 00747 */ 00748 return NULL; 00749 } 00750 conv->str_value.copy(); 00751 /* Ensure that no one is going to change the result string */ 00752 conv->str_value.mark_as_const(); 00753 return conv; 00754 } 00755 00756 00757 bool Item_string::eq(const Item *item, bool binary_cmp) const 00758 { 00759 if (type() == item->type() && item->basic_const_item()) 00760 { 00761 if (binary_cmp) 00762 return !stringcmp(&str_value, &item->str_value); 00763 return (collation.collation == item->collation.collation && 00764 !sortcmp(&str_value, &item->str_value, collation.collation)); 00765 } 00766 return 0; 00767 } 00768 00769 00770 /* 00771 Get the value of the function as a TIME structure. 00772 As a extra convenience the time structure is reset on error! 00773 */ 00774 00775 bool Item::get_date(TIME *ltime,uint fuzzydate) 00776 { 00777 char buff[40]; 00778 String tmp(buff,sizeof(buff), &my_charset_bin),*res; 00779 if (!(res=val_str(&tmp)) || 00780 str_to_datetime_with_warn(res->ptr(), res->length(), 00781 ltime, fuzzydate) <= MYSQL_TIMESTAMP_ERROR) 00782 { 00783 bzero((char*) ltime,sizeof(*ltime)); 00784 return 1; 00785 } 00786 return 0; 00787 } 00788 00789 /* 00790 Get time of first argument. 00791 As a extra convenience the time structure is reset on error! 00792 */ 00793 00794 bool Item::get_time(TIME *ltime) 00795 { 00796 char buff[40]; 00797 String tmp(buff,sizeof(buff),&my_charset_bin),*res; 00798 if (!(res=val_str(&tmp)) || 00799 str_to_time_with_warn(res->ptr(), res->length(), ltime)) 00800 { 00801 bzero((char*) ltime,sizeof(*ltime)); 00802 return 1; 00803 } 00804 return 0; 00805 } 00806 00807 CHARSET_INFO *Item::default_charset() 00808 { 00809 return current_thd->variables.collation_connection; 00810 } 00811 00812 00813 /* 00814 Save value in field, but don't give any warnings 00815 00816 NOTES 00817 This is used to temporary store and retrieve a value in a column, 00818 for example in opt_range to adjust the key value to fit the column. 00819 */ 00820 00821 int Item::save_in_field_no_warnings(Field *field, bool no_conversions) 00822 { 00823 int res; 00824 TABLE *table= field->table; 00825 THD *thd= table->in_use; 00826 enum_check_fields tmp= thd->count_cuted_fields; 00827 my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set); 00828 thd->count_cuted_fields= CHECK_FIELD_IGNORE; 00829 res= save_in_field(field, no_conversions); 00830 thd->count_cuted_fields= tmp; 00831 dbug_tmp_restore_column_map(table->write_set, old_map); 00832 return res; 00833 } 00834 00835 00836 /***************************************************************************** 00837 Item_sp_variable methods 00838 *****************************************************************************/ 00839 00840 Item_sp_variable::Item_sp_variable(char *sp_var_name_str, 00841 uint sp_var_name_length) 00842 :m_thd(0) 00843 #ifndef DBUG_OFF 00844 , m_sp(0) 00845 #endif 00846 { 00847 m_name.str= sp_var_name_str; 00848 m_name.length= sp_var_name_length; 00849 } 00850 00851 00852 bool Item_sp_variable::fix_fields(THD *thd, Item **) 00853 { 00854 Item *it; 00855 00856 m_thd= thd; /* NOTE: this must be set before any this_xxx() */ 00857 it= this_item(); 00858 00859 DBUG_ASSERT(it->fixed); 00860 00861 max_length= it->max_length; 00862 decimals= it->decimals; 00863 unsigned_flag= it->unsigned_flag; 00864 fixed= 1; 00865 collation.set(it->collation.collation, it->collation.derivation); 00866 00867 return FALSE; 00868 } 00869 00870 00871 double Item_sp_variable::val_real() 00872 { 00873 DBUG_ASSERT(fixed); 00874 Item *it= this_item(); 00875 double ret= it->val_real(); 00876 null_value= it->null_value; 00877 return ret; 00878 } 00879 00880 00881 longlong Item_sp_variable::val_int() 00882 { 00883 DBUG_ASSERT(fixed); 00884 Item *it= this_item(); 00885 longlong ret= it->val_int(); 00886 null_value= it->null_value; 00887 return ret; 00888 } 00889 00890 00891 String *Item_sp_variable::val_str(String *sp) 00892 { 00893 DBUG_ASSERT(fixed); 00894 Item *it= this_item(); 00895 String *res= it->val_str(sp); 00896 00897 null_value= it->null_value; 00898 00899 if (!res) 00900 return NULL; 00901 00902 /* 00903 This way we mark returned value of val_str as const, 00904 so that various functions (e.g. CONCAT) won't try to 00905 modify the value of the Item. Analogous mechanism is 00906 implemented for Item_param. 00907 Without this trick Item_splocal could be changed as a 00908 side-effect of expression computation. Here is an example 00909 of what happens without it: suppose x is varchar local 00910 variable in a SP with initial value 'ab' Then 00911 select concat(x,'c'); 00912 would change x's value to 'abc', as Item_func_concat::val_str() 00913 would use x's internal buffer to compute the result. 00914 This is intended behaviour of Item_func_concat. Comments to 00915 Item_param class contain some more details on the topic. 00916 */ 00917 00918 if (res != &str_value) 00919 str_value.set(res->ptr(), res->length(), res->charset()); 00920 else 00921 res->mark_as_const(); 00922 00923 return &str_value; 00924 } 00925 00926 00927 my_decimal *Item_sp_variable::val_decimal(my_decimal *decimal_value) 00928 { 00929 DBUG_ASSERT(fixed); 00930 Item *it= this_item(); 00931 my_decimal *val= it->val_decimal(decimal_value); 00932 null_value= it->null_value; 00933 return val; 00934 } 00935 00936 00937 bool Item_sp_variable::is_null() 00938 { 00939 return this_item()->is_null(); 00940 } 00941 00942 00943 /***************************************************************************** 00944 Item_splocal methods 00945 *****************************************************************************/ 00946 00947 Item_splocal::Item_splocal(const LEX_STRING &sp_var_name, 00948 uint sp_var_idx, 00949 enum_field_types sp_var_type, 00950 uint pos_in_q) 00951 :Item_sp_variable(sp_var_name.str, sp_var_name.length), 00952 m_var_idx(sp_var_idx), pos_in_query(pos_in_q) 00953 { 00954 maybe_null= TRUE; 00955 00956 m_type= sp_map_item_type(sp_var_type); 00957 m_result_type= sp_map_result_type(sp_var_type); 00958 } 00959 00960 00961 Item * 00962 Item_splocal::this_item() 00963 { 00964 DBUG_ASSERT(m_sp == m_thd->spcont->sp); 00965 00966 return m_thd->spcont->get_item(m_var_idx); 00967 } 00968 00969 const Item * 00970 Item_splocal::this_item() const 00971 { 00972 DBUG_ASSERT(m_sp == m_thd->spcont->sp); 00973 00974 return m_thd->spcont->get_item(m_var_idx); 00975 } 00976 00977 00978 Item ** 00979 Item_splocal::this_item_addr(THD *thd, Item **) 00980 { 00981 DBUG_ASSERT(m_sp == thd->spcont->sp); 00982 00983 return thd->spcont->get_item_addr(m_var_idx); 00984 } 00985 00986 00987 void Item_splocal::print(String *str) 00988 { 00989 str->reserve(m_name.length+8); 00990 str->append(m_name.str, m_name.length); 00991 str->append('@'); 00992 str->qs_append(m_var_idx); 00993 } 00994 00995 00996 bool Item_splocal::set_value(THD *thd, sp_rcontext *ctx, Item **it) 00997 { 00998 return ctx->set_variable(thd, get_var_idx(), it); 00999 } 01000 01001 01002 /***************************************************************************** 01003 Item_case_expr methods 01004 *****************************************************************************/ 01005 01006 Item_case_expr::Item_case_expr(int case_expr_id) 01007 :Item_sp_variable( C_STRING_WITH_LEN("case_expr")), 01008 m_case_expr_id(case_expr_id) 01009 { 01010 } 01011 01012 01013 Item * 01014 Item_case_expr::this_item() 01015 { 01016 DBUG_ASSERT(m_sp == m_thd->spcont->sp); 01017 01018 return m_thd->spcont->get_case_expr(m_case_expr_id); 01019 } 01020 01021 01022 01023 const Item * 01024 Item_case_expr::this_item() const 01025 { 01026 DBUG_ASSERT(m_sp == m_thd->spcont->sp); 01027 01028 return m_thd->spcont->get_case_expr(m_case_expr_id); 01029 } 01030 01031 01032 Item ** 01033 Item_case_expr::this_item_addr(THD *thd, Item **) 01034 { 01035 DBUG_ASSERT(m_sp == thd->spcont->sp); 01036 01037 return thd->spcont->get_case_expr_addr(m_case_expr_id); 01038 } 01039 01040 01041 void Item_case_expr::print(String *str) 01042 { 01043 VOID(str->append(STRING_WITH_LEN("case_expr@"))); 01044 str->qs_append(m_case_expr_id); 01045 } 01046 01047 01048 /***************************************************************************** 01049 Item_name_const methods 01050 *****************************************************************************/ 01051 01052 double Item_name_const::val_real() 01053 { 01054 DBUG_ASSERT(fixed); 01055 double ret= value_item->val_real(); 01056 null_value= value_item->null_value; 01057 return ret; 01058 } 01059 01060 01061 longlong Item_name_const::val_int() 01062 { 01063 DBUG_ASSERT(fixed); 01064 longlong ret= value_item->val_int(); 01065 null_value= value_item->null_value; 01066 return ret; 01067 } 01068 01069 01070 String *Item_name_const::val_str(String *sp) 01071 { 01072 DBUG_ASSERT(fixed); 01073 String *ret= value_item->val_str(sp); 01074 null_value= value_item->null_value; 01075 return ret; 01076 } 01077 01078 01079 my_decimal *Item_name_const::val_decimal(my_decimal *decimal_value) 01080 { 01081 DBUG_ASSERT(fixed); 01082 my_decimal *val= value_item->val_decimal(decimal_value); 01083 null_value= value_item->null_value; 01084 return val; 01085 } 01086 01087 01088 bool Item_name_const::is_null() 01089 { 01090 return value_item->is_null(); 01091 } 01092 01093 Item::Type Item_name_const::type() const 01094 { 01095 return value_item->type(); 01096 } 01097 01098 01099 bool Item_name_const::fix_fields(THD *thd, Item **ref) 01100 { 01101 char buf[128]; 01102 String *item_name; 01103 String s(buf, sizeof(buf), &my_charset_bin); 01104 s.length(0); 01105 01106 if (value_item->fix_fields(thd, &value_item) || 01107 name_item->fix_fields(thd, &name_item)) 01108 return TRUE; 01109 if (!(value_item->const_item() && name_item->const_item())) 01110 return TRUE; 01111 01112 if (!(item_name= name_item->val_str(&s))) 01113 return TRUE; /* Can't have a NULL name */ 01114 01115 set_name(item_name->ptr(), (uint) item_name->length(), system_charset_info); 01116 max_length= value_item->max_length; 01117 decimals= value_item->decimals; 01118 fixed= 1; 01119 return FALSE; 01120 } 01121 01122 01123 void Item_name_const::print(String *str) 01124 { 01125 str->append(STRING_WITH_LEN("NAME_CONST(")); 01126 name_item->print(str); 01127 str->append(','); 01128 value_item->print(str); 01129 str->append(')'); 01130 } 01131 01132 01133 /* 01134 Move SUM items out from item tree and replace with reference 01135 01136 SYNOPSIS 01137 split_sum_func2() 01138 thd Thread handler 01139 ref_pointer_array Pointer to array of reference fields 01140 fields All fields in select 01141 ref Pointer to item 01142 skip_registered <=> function be must skipped for registered SUM items 01143 01144 NOTES 01145 This is from split_sum_func2() for items that should be split 01146 01147 All found SUM items are added FIRST in the fields list and 01148 we replace the item with a reference. 01149 01150 thd->fatal_error() may be called if we are out of memory 01151 */ 01152 01153 01154 void Item::split_sum_func2(THD *thd, Item **ref_pointer_array, 01155 List<Item> &fields, Item **ref, 01156 bool skip_registered) 01157 { 01158 /* An item of type Item_sum is registered <=> ref_by != 0 */ 01159 if (type() == SUM_FUNC_ITEM && skip_registered && 01160 ((Item_sum *) this)->ref_by) 01161 return; 01162 if (type() != SUM_FUNC_ITEM && with_sum_func) 01163 { 01164 /* Will split complicated items and ignore simple ones */ 01165 split_sum_func(thd, ref_pointer_array, fields); 01166 } 01167 else if ((type() == SUM_FUNC_ITEM || (used_tables() & ~PARAM_TABLE_BIT)) && 01168 (type() != REF_ITEM || 01169 ((Item_ref*)this)->ref_type() == Item_ref::VIEW_REF)) 01170 { 01171 /* 01172 Replace item with a reference so that we can easily calculate 01173 it (in case of sum functions) or copy it (in case of fields) 01174 01175 The test above is to ensure we don't do a reference for things 01176 that are constants (PARAM_TABLE_BIT is in effect a constant) 01177 or already referenced (for example an item in HAVING) 01178 Exception is Item_direct_view_ref which we need to convert to 01179 Item_ref to allow fields from view being stored in tmp table. 01180 */ 01181 uint el= fields.elements; 01182 Item *new_item, *real_itm= real_item(); 01183 01184 ref_pointer_array[el]= real_itm; 01185 if (!(new_item= new Item_ref(&thd->lex->current_select->context, 01186 ref_pointer_array + el, 0, name))) 01187 return; // fatal_error is set 01188 fields.push_front(real_itm); 01189 thd->change_item_tree(ref, new_item); 01190 } 01191 } 01192 01193 01194 /* 01195 Aggregate two collations together taking 01196 into account their coercibility (aka derivation): 01197 01198 0 == DERIVATION_EXPLICIT - an explicitly written COLLATE clause 01199 1 == DERIVATION_NONE - a mix of two different collations 01200 2 == DERIVATION_IMPLICIT - a column 01201 3 == DERIVATION_COERCIBLE - a string constant 01202 01203 The most important rules are: 01204 01205 1. If collations are the same: 01206 chose this collation, and the strongest derivation. 01207 01208 2. If collations are different: 01209 - Character sets may differ, but only if conversion without 01210 data loss is possible. The caller provides flags whether 01211 character set conversion attempts should be done. If no 01212 flags are substituted, then the character sets must be the same. 01213 Currently processed flags are: 01214 MY_COLL_ALLOW_SUPERSET_CONV - allow conversion to a superset 01215 MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value 01216 - two EXPLICIT collations produce an error, e.g. this is wrong: 01217 CONCAT(expr1 collate latin1_swedish_ci, expr2 collate latin1_german_ci) 01218 - the side with smaller derivation value wins, 01219 i.e. a column is stronger than a string constant, 01220 an explicit COLLATE clause is stronger than a column. 01221 - if derivations are the same, we have DERIVATION_NONE, 01222 we'll wait for an explicit COLLATE clause which possibly can 01223 come from another argument later: for example, this is valid, 01224 but we don't know yet when collecting the first two arguments: 01225 CONCAT(latin1_swedish_ci_column, 01226 latin1_german1_ci_column, 01227 expr COLLATE latin1_german2_ci) 01228 */ 01229 bool DTCollation::aggregate(DTCollation &dt, uint flags) 01230 { 01231 if (!my_charset_same(collation, dt.collation)) 01232 { 01233 /* 01234 We do allow to use binary strings (like BLOBS) 01235 together with character strings. 01236 Binaries have more precedence than a character 01237 string of the same derivation. 01238 */ 01239 if (collation == &my_charset_bin) 01240 { 01241 if (derivation <= dt.derivation) 01242 ; // Do nothing 01243 else 01244 { 01245 set(dt); 01246 } 01247 } 01248 else if (dt.collation == &my_charset_bin) 01249 { 01250 if (dt.derivation <= derivation) 01251 { 01252 set(dt); 01253 } 01254 else 01255 ; // Do nothing 01256 } 01257 else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) && 01258 collation->state & MY_CS_UNICODE && 01259 (derivation < dt.derivation || 01260 (derivation == dt.derivation && 01261 !(dt.collation->state & MY_CS_UNICODE)))) 01262 { 01263 // Do nothing 01264 } 01265 else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) && 01266 dt.collation->state & MY_CS_UNICODE && 01267 (dt.derivation < derivation || 01268 (dt.derivation == derivation && 01269 !(collation->state & MY_CS_UNICODE)))) 01270 { 01271 set(dt); 01272 } 01273 else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) && 01274 derivation < dt.derivation && 01275 dt.derivation >= DERIVATION_SYSCONST) 01276 { 01277 // Do nothing; 01278 } 01279 else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) && 01280 dt.derivation < derivation && 01281 derivation >= DERIVATION_SYSCONST) 01282 { 01283 set(dt); 01284 } 01285 else 01286 { 01287 // Cannot apply conversion 01288 set(0, DERIVATION_NONE); 01289 return 1; 01290 } 01291 } 01292 else if (derivation < dt.derivation) 01293 { 01294 // Do nothing 01295 } 01296 else if (dt.derivation < derivation) 01297 { 01298 set(dt); 01299 } 01300 else 01301 { 01302 if (collation == dt.collation) 01303 { 01304 // Do nothing 01305 } 01306 else 01307 { 01308 if (derivation == DERIVATION_EXPLICIT) 01309 { 01310 set(0, DERIVATION_NONE); 01311 return 1; 01312 } 01313 if (collation->state & MY_CS_BINSORT) 01314 return 0; 01315 if (dt.collation->state & MY_CS_BINSORT) 01316 { 01317 set(dt); 01318 return 0; 01319 } 01320 CHARSET_INFO *bin= get_charset_by_csname(collation->csname, 01321 MY_CS_BINSORT,MYF(0)); 01322 set(bin, DERIVATION_NONE); 01323 } 01324 } 01325 return 0; 01326 } 01327 01328 /******************************/ 01329 static 01330 void my_coll_agg_error(DTCollation &c1, DTCollation &c2, const char *fname) 01331 { 01332 my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0), 01333 c1.collation->name,c1.derivation_name(), 01334 c2.collation->name,c2.derivation_name(), 01335 fname); 01336 } 01337 01338 01339 static 01340 void my_coll_agg_error(DTCollation &c1, DTCollation &c2, DTCollation &c3, 01341 const char *fname) 01342 { 01343 my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0), 01344 c1.collation->name,c1.derivation_name(), 01345 c2.collation->name,c2.derivation_name(), 01346 c3.collation->name,c3.derivation_name(), 01347 fname); 01348 } 01349 01350 01351 static 01352 void my_coll_agg_error(Item** args, uint count, const char *fname, 01353 int item_sep) 01354 { 01355 if (count == 2) 01356 my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname); 01357 else if (count == 3) 01358 my_coll_agg_error(args[0]->collation, args[item_sep]->collation, 01359 args[2*item_sep]->collation, fname); 01360 else 01361 my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname); 01362 } 01363 01364 01365 bool agg_item_collations(DTCollation &c, const char *fname, 01366 Item **av, uint count, uint flags, int item_sep) 01367 { 01368 uint i; 01369 Item **arg; 01370 c.set(av[0]->collation); 01371 for (i= 1, arg= &av[item_sep]; i < count; i++, arg++) 01372 { 01373 if (c.aggregate((*arg)->collation, flags)) 01374 { 01375 my_coll_agg_error(av, count, fname, item_sep); 01376 return TRUE; 01377 } 01378 } 01379 if ((flags & MY_COLL_DISALLOW_NONE) && 01380 c.derivation == DERIVATION_NONE) 01381 { 01382 my_coll_agg_error(av, count, fname, item_sep); 01383 return TRUE; 01384 } 01385 return FALSE; 01386 } 01387 01388 01389 bool agg_item_collations_for_comparison(DTCollation &c, const char *fname, 01390 Item **av, uint count, uint flags) 01391 { 01392 return (agg_item_collations(c, fname, av, count, 01393 flags | MY_COLL_DISALLOW_NONE, 1)); 01394 } 01395 01396 01397 /* 01398 Collect arguments' character sets together. 01399 We allow to apply automatic character set conversion in some cases. 01400 The conditions when conversion is possible are: 01401 - arguments A and B have different charsets 01402 - A wins according to coercibility rules 01403 (i.e. a column is stronger than a string constant, 01404 an explicit COLLATE clause is stronger than a column) 01405 - character set of A is either superset for character set of B, 01406 or B is a string constant which can be converted into the 01407 character set of A without data loss. 01408 01409 If all of the above is true, then it's possible to convert 01410 B into the character set of A, and then compare according 01411 to the collation of A. 01412 01413 For functions with more than two arguments: 01414 01415 collect(A,B,C) ::= collect(collect(A,B),C) 01416 01417 Since this function calls THD::change_item_tree() on the passed Item ** 01418 pointers, it is necessary to pass the original Item **'s, not copies. 01419 Otherwise their values will not be properly restored (see BUG#20769). 01420 If the items are not consecutive (eg. args[2] and args[5]), use the 01421 item_sep argument, ie. 01422 01423 agg_item_charsets(coll, fname, &args[2], 2, flags, 3) 01424 01425 */ 01426 01427 bool agg_item_charsets(DTCollation &coll, const char *fname, 01428 Item **args, uint nargs, uint flags, int item_sep) 01429 { 01430 Item **arg, **last, *safe_args[2]; 01431 01432 LINT_INIT(safe_args[0]); 01433 LINT_INIT(safe_args[1]); 01434 01435 if (agg_item_collations(coll, fname, args, nargs, flags, item_sep)) 01436 return TRUE; 01437 01438 /* 01439 For better error reporting: save the first and the second argument. 01440 We need this only if the the number of args is 3 or 2: 01441 - for a longer argument list, "Illegal mix of collations" 01442 doesn't display each argument's characteristics. 01443 - if nargs is 1, then this error cannot happen. 01444 */ 01445 if (nargs >=2 && nargs <= 3) 01446 { 01447 safe_args[0]= args[0]; 01448 safe_args[1]= args[item_sep]; 01449 } 01450 01451 THD *thd= current_thd; 01452 Query_arena *arena, backup; 01453 bool res= FALSE; 01454 uint i; 01455 /* 01456 In case we're in statement prepare, create conversion item 01457 in its memory: it will be reused on each execute. 01458 */ 01459 arena= thd->is_stmt_prepare() ? thd->activate_stmt_arena_if_needed(&backup) 01460 : NULL; 01461 01462 for (i= 0, arg= args; i < nargs; i++, arg+= item_sep) 01463 { 01464 Item* conv; 01465 uint32 dummy_offset; 01466 if (!String::needs_conversion(0, coll.collation, 01467 (*arg)->collation.collation, 01468 &dummy_offset)) 01469 continue; 01470 01471 if (!(conv= (*arg)->safe_charset_converter(coll.collation))) 01472 { 01473 if (nargs >=2 && nargs <= 3) 01474 { 01475 /* restore the original arguments for better error message */ 01476 args[0]= safe_args[0]; 01477 args[item_sep]= safe_args[1]; 01478 } 01479 my_coll_agg_error(args, nargs, fname, item_sep); 01480 res= TRUE; 01481 break; // we cannot return here, we need to restore "arena". 01482 } 01483 if ((*arg)->type() == Item::FIELD_ITEM) 01484 ((Item_field *)(*arg))->no_const_subst= 1; 01485 /* 01486 If in statement prepare, then we create a converter for two 01487 constant items, do it once and then reuse it. 01488 If we're in execution of a prepared statement, arena is NULL, 01489 and the conv was created in runtime memory. This can be 01490 the case only if the argument is a parameter marker ('?'), 01491 because for all true constants the charset converter has already 01492 been created in prepare. In this case register the change for 01493 rollback. 01494 */ 01495 if (arena) 01496 *arg= conv; 01497 else 01498 thd->change_item_tree(arg, conv); 01499 /* 01500 We do not check conv->fixed, because Item_func_conv_charset which can 01501 be return by safe_charset_converter can't be fixed at creation 01502 */ 01503 conv->fix_fields(thd, arg); 01504 } 01505 if (arena) 01506 thd->restore_active_arena(arena, &backup); 01507 return res; 01508 } 01509 01510 01511 void Item_ident_for_show::make_field(Send_field *tmp_field) 01512 { 01513 tmp_field->table_name= tmp_field->org_table_name= table_name; 01514 tmp_field->db_name= db_name; 01515 tmp_field->col_name= tmp_field->org_col_name= field->field_name; 01516 tmp_field->charsetnr= field->charset()->number; 01517 tmp_field->length=field->field_length; 01518 tmp_field->type=field->type(); 01519 tmp_field->flags= field->table->maybe_null ? 01520 (field->flags & ~NOT_NULL_FLAG) : field->flags; 01521 tmp_field->decimals= 0; 01522 } 01523 01524 /**********************************************/ 01525 01526 Item_field::Item_field(Field *f) 01527 :Item_ident(0, NullS, *f->table_name, f->field_name), 01528 item_equal(0), no_const_subst(0), 01529 have_privileges(0), any_privileges(0) 01530 { 01531 set_field(f); 01532 /* 01533 field_name and table_name should not point to garbage 01534 if this item is to be reused 01535 */ 01536 orig_table_name= orig_field_name= ""; 01537 } 01538 01539 01540 Item_field::Item_field(THD *thd, Name_resolution_context *context_arg, 01541 Field *f) 01542 :Item_ident(context_arg, f->table->s->db.str, *f->table_name, f->field_name), 01543 item_equal(0), no_const_subst(0), 01544 have_privileges(0), any_privileges(0) 01545 { 01546 /* 01547 We always need to provide Item_field with a fully qualified field 01548 name to avoid ambiguity when executing prepared statements like 01549 SELECT * from d1.t1, d2.t1; (assuming d1.t1 and d2.t1 have columns 01550 with same names). 01551 This is because prepared statements never deal with wildcards in 01552 select list ('*') and always fix fields using fully specified path 01553 (i.e. db.table.column). 01554 No check for OOM: if db_name is NULL, we'll just get 01555 "Field not found" error. 01556 We need to copy db_name, table_name and field_name because they must 01557 be allocated in the statement memory, not in table memory (the table 01558 structure can go away and pop up again between subsequent executions 01559 of a prepared statement). 01560 */ 01561 if (thd->stmt_arena->is_stmt_prepare_or_first_sp_execute()) 01562 { 01563 if (db_name) 01564 orig_db_name= thd->strdup(db_name); 01565 orig_table_name= thd->strdup(table_name); 01566 orig_field_name= thd->strdup(field_name); 01567 /* 01568 We don't restore 'name' in cleanup because it's not changed 01569 during execution. Still we need it to point to persistent 01570 memory if this item is to be reused. 01571 */ 01572 name= (char*) orig_field_name; 01573 } 01574 set_field(f); 01575 } 01576 01577 01578 Item_field::Item_field(Name_resolution_context *context_arg, 01579 const char *db_arg,const char *table_name_arg, 01580 const char *field_name_arg) 01581 :Item_ident(context_arg, db_arg,table_name_arg,field_name_arg), 01582 field(0), result_field(0), item_equal(0), no_const_subst(0), 01583 have_privileges(0), any_privileges(0) 01584 { 01585 collation.set(DERIVATION_IMPLICIT); 01586 } 01587 01588 // Constructor need to process subselect with temporary tables (see Item) 01589 Item_field::Item_field(THD *thd, Item_field *item) 01590 :Item_ident(thd, item), 01591 field(item->field), 01592 result_field(item->result_field), 01593 item_equal(item->item_equal), 01594 no_const_subst(item->no_const_subst), 01595 have_privileges(item->have_privileges), 01596 any_privileges(item->any_privileges) 01597 { 01598 collation.set(DERIVATION_IMPLICIT); 01599 } 01600 01601 void Item_field::set_field(Field *field_par) 01602 { 01603 field=result_field=field_par; // for easy coding with fields 01604 maybe_null=field->maybe_null(); 01605 decimals= field->decimals(); 01606 max_length= field_par->max_length(); 01607 table_name= *field_par->table_name; 01608 field_name= field_par->field_name; 01609 db_name= field_par->table->s->db.str; 01610 alias_name_used= field_par->table->alias_name_used; 01611 unsigned_flag=test(field_par->flags & UNSIGNED_FLAG); 01612 collation.set(field_par->charset(), DERIVATION_IMPLICIT); 01613 fixed= 1; 01614 } 01615 01616 01617 /* 01618 Reset this item to point to a field from the new temporary table. 01619 This is used when we create a new temporary table for each execution 01620 of prepared statement. 01621 */ 01622 01623 void Item_field::reset_field(Field *f) 01624 { 01625 set_field(f); 01626 /* 'name' is pointing at field->field_name of old field */ 01627 name= (char*) f->field_name; 01628 } 01629 01630 const char *Item_ident::full_name() const 01631 { 01632 char *tmp; 01633 if (!table_name || !field_name) 01634 return field_name ? field_name : name ? name : "tmp_field"; 01635 if (db_name && db_name[0]) 01636 { 01637 tmp=(char*) sql_alloc((uint) strlen(db_name)+(uint) strlen(table_name)+ 01638 (uint) strlen(field_name)+3); 01639 strxmov(tmp,db_name,".",table_name,".",field_name,NullS); 01640 } 01641 else 01642 { 01643 if (table_name[0]) 01644 { 01645 tmp= (char*) sql_alloc((uint) strlen(table_name) + 01646 (uint) strlen(field_name) + 2); 01647 strxmov(tmp, table_name, ".", field_name, NullS); 01648 } 01649 else 01650 tmp= (char*) field_name; 01651 } 01652 return tmp; 01653 } 01654 01655 void Item_ident::print(String *str) 01656 { 01657 THD *thd= current_thd; 01658 char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME]; 01659 const char *d_name= db_name, *t_name= table_name; 01660 if (lower_case_table_names== 1 || 01661 (lower_case_table_names == 2 && !alias_name_used)) 01662 { 01663 if (table_name && table_name[0]) 01664 { 01665 strmov(t_name_buff, table_name); 01666 my_casedn_str(files_charset_info, t_name_buff); 01667 t_name= t_name_buff; 01668 } 01669 if (db_name && db_name[0]) 01670 { 01671 strmov(d_name_buff, db_name); 01672 my_casedn_str(files_charset_info, d_name_buff); 01673 d_name= d_name_buff; 01674 } 01675 } 01676 01677 if (!table_name || !field_name) 01678 { 01679 const char *nm= field_name ? field_name : name ? name : "tmp_field"; 01680 append_identifier(thd, str, nm, (uint) strlen(nm)); 01681 return; 01682 } 01683 if (db_name && db_name[0] && !alias_name_used) 01684 { 01685 if (!(cached_table && cached_table->belong_to_view && 01686 cached_table->belong_to_view->compact_view_format)) 01687 { 01688 append_identifier(thd, str, d_name, (uint)strlen(d_name)); 01689 str->append('.'); 01690 } 01691 append_identifier(thd, str, t_name, (uint)strlen(t_name)); 01692 str->append('.'); 01693 append_identifier(thd, str, field_name, (uint)strlen(field_name)); 01694 } 01695 else 01696 { 01697 if (table_name[0]) 01698 { 01699 append_identifier(thd, str, t_name, (uint) strlen(t_name)); 01700 str->append('.'); 01701 append_identifier(thd, str, field_name, (uint) strlen(field_name)); 01702 } 01703 else 01704 append_identifier(thd, str, field_name, (uint) strlen(field_name)); 01705 } 01706 } 01707 01708 /* ARGSUSED */ 01709 String *Item_field::val_str(String *str) 01710 { 01711 DBUG_ASSERT(fixed == 1); 01712 if ((null_value=field->is_null())) 01713 return 0; 01714 str->set_charset(str_value.charset()); 01715 return field->val_str(str,&str_value); 01716 } 01717 01718 01719 double Item_field::val_real() 01720 { 01721 DBUG_ASSERT(fixed == 1); 01722 if ((null_value=field->is_null())) 01723 return 0.0; 01724 return field->val_real(); 01725 } 01726 01727 01728 longlong Item_field::val_int() 01729 { 01730 DBUG_ASSERT(fixed == 1); 01731 if ((null_value=field->is_null())) 01732 return 0; 01733 return field->val_int(); 01734 } 01735 01736 01737 my_decimal *Item_field::val_decimal(my_decimal *decimal_value) 01738 { 01739 if ((null_value= field->is_null())) 01740 return 0; 01741 return field->val_decimal(decimal_value); 01742 } 01743 01744 01745 String *Item_field::str_result(String *str) 01746 { 01747 if ((null_value=result_field->is_null())) 01748 return 0; 01749 str->set_charset(str_value.charset()); 01750 return result_field->val_str(str,&str_value); 01751 } 01752 01753 bool Item_field::get_date(TIME *ltime,uint fuzzydate) 01754 { 01755 if ((null_value=field->is_null()) || field->get_date(ltime,fuzzydate)) 01756 { 01757 bzero((char*) ltime,sizeof(*ltime)); 01758 return 1; 01759 } 01760 return 0; 01761 } 01762 01763 bool Item_field::get_date_result(TIME *ltime,uint fuzzydate) 01764 { 01765 if ((null_value=result_field->is_null()) || 01766 result_field->get_date(ltime,fuzzydate)) 01767 { 01768 bzero((char*) ltime,sizeof(*ltime)); 01769 return 1; 01770 } 01771 return 0; 01772 } 01773 01774 bool Item_field::get_time(TIME *ltime) 01775 { 01776 if ((null_value=field->is_null()) || field->get_time(ltime)) 01777 { 01778 bzero((char*) ltime,sizeof(*ltime)); 01779 return 1; 01780 } 01781 return 0; 01782 } 01783 01784 double Item_field::val_result() 01785 { 01786 if ((null_value=result_field->is_null())) 01787 return 0.0; 01788 return result_field->val_real(); 01789 } 01790 01791 longlong Item_field::val_int_result() 01792 { 01793 if ((null_value=result_field->is_null())) 01794 return 0; 01795 return result_field->val_int(); 01796 } 01797 01798 01799 my_decimal *Item_field::val_decimal_result(my_decimal *decimal_value) 01800 { 01801 if ((null_value= result_field->is_null())) 01802 return 0; 01803 return result_field->val_decimal(decimal_value); 01804 } 01805 01806 01807 bool Item_field::val_bool_result() 01808 { 01809 if ((null_value= result_field->is_null())) 01810 return FALSE; 01811 switch (result_field->result_type()) { 01812 case INT_RESULT: 01813 return result_field->val_int() != 0; 01814 case DECIMAL_RESULT: 01815 { 01816 my_decimal decimal_value; 01817 my_decimal *val= result_field->val_decimal(&decimal_value); 01818 if (val) 01819 return !my_decimal_is_zero(val); 01820 return 0; 01821 } 01822 case REAL_RESULT: 01823 case STRING_RESULT: 01824 return result_field->val_real() != 0.0; 01825 case ROW_RESULT: 01826 default: 01827 DBUG_ASSERT(0); 01828 return 0; // Shut up compiler 01829 } 01830 } 01831 01832 01833 bool Item_field::eq(const Item *item, bool binary_cmp) const 01834 { 01835 if (item->type() != FIELD_ITEM) 01836 return 0; 01837 01838 Item_field *item_field= (Item_field*) item; 01839 if (item_field->field && field) 01840 return item_field->field == field; 01841 /* 01842 We may come here when we are trying to find a function in a GROUP BY 01843 clause from the select list. 01844 In this case the '100 % correct' way to do this would be to first 01845 run fix_fields() on the GROUP BY item and then retry this function, but 01846 I think it's better to relax the checking a bit as we will in 01847 most cases do the correct thing by just checking the field name. 01848 (In cases where we would choose wrong we would have to generate a 01849 ER_NON_UNIQ_ERROR). 01850 */ 01851 return (!my_strcasecmp(system_charset_info, item_field->name, 01852 field_name) && 01853 (!item_field->table_name || !table_name || 01854 (!my_strcasecmp(table_alias_charset, item_field->table_name, 01855 table_name) && 01856 (!item_field->db_name || !db_name || 01857 (item_field->db_name && !strcmp(item_field->db_name, 01858 db_name)))))); 01859 } 01860 01861 01862 table_map Item_field::used_tables() const 01863 { 01864 if (field->table->const_table) 01865 return 0; // const item 01866 return (depended_from ? OUTER_REF_TABLE_BIT : field->table->map); 01867 } 01868 01869 01870 Item *Item_field::get_tmp_table_item(THD *thd) 01871 { 01872 Item_field *new_item= new Item_field(thd, this); 01873 if (new_item) 01874 new_item->field= new_item->result_field; 01875 return new_item; 01876 } 01877 01878 01879 /* 01880 Create an item from a string we KNOW points to a valid longlong 01881 end \0 terminated number string. 01882 This is always 'signed'. Unsigned values are created with Item_uint() 01883 */ 01884 01885 Item_int::Item_int(const char *str_arg, uint length) 01886 { 01887 char *end_ptr= (char*) str_arg + length; 01888 int error; 01889 value= my_strtoll10(str_arg, &end_ptr, &error); 01890 max_length= (uint) (end_ptr - str_arg); 01891 name= (char*) str_arg; 01892 fixed= 1; 01893 } 01894 01895 01896 my_decimal *Item_int::val_decimal(my_decimal *decimal_value) 01897 { 01898 int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_value); 01899 return decimal_value; 01900 } 01901 01902 String *Item_int::val_str(String *str) 01903 { 01904 // following assert is redundant, because fixed=1 assigned in constructor 01905 DBUG_ASSERT(fixed == 1); 01906 str->set(value, &my_charset_bin); 01907 return str; 01908 } 01909 01910 void Item_int::print(String *str) 01911 { 01912 // my_charset_bin is good enough for numbers 01913 str_value.set(value, &my_charset_bin); 01914 str->append(str_value); 01915 } 01916 01917 01918 Item_uint::Item_uint(const char *str_arg, uint length): 01919 Item_int(str_arg, length) 01920 { 01921 unsigned_flag= 1; 01922 } 01923 01924 01925 Item_uint::Item_uint(const char *str_arg, longlong i, uint length): 01926 Item_int(str_arg, i, length) 01927 { 01928 unsigned_flag= 1; 01929 } 01930 01931 01932 String *Item_uint::val_str(String *str) 01933 { 01934 // following assert is redundant, because fixed=1 assigned in constructor 01935 DBUG_ASSERT(fixed == 1); 01936 str->set((ulonglong) value, &my_charset_bin); 01937 return str; 01938 } 01939 01940 01941 void Item_uint::print(String *str) 01942 { 01943 // latin1 is good enough for numbers 01944 str_value.set((ulonglong) value, default_charset()); 01945 str->append(str_value); 01946 } 01947 01948 01949 Item_decimal::Item_decimal(const char *str_arg, uint length, 01950 CHARSET_INFO *charset) 01951 { 01952 str2my_decimal(E_DEC_FATAL_ERROR, str_arg, length, charset, &decimal_value); 01953 name= (char*) str_arg; 01954 decimals= (uint8) decimal_value.frac; 01955 fixed= 1; 01956 max_length= my_decimal_precision_to_length(decimal_value.intg + decimals, 01957 decimals, unsigned_flag); 01958 } 01959 01960 Item_decimal::Item_decimal(longlong val, bool unsig) 01961 { 01962 int2my_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value); 01963 decimals= (uint8) decimal_value.frac; 01964 fixed= 1; 01965 max_length= my_decimal_precision_to_length(decimal_value.intg + decimals, 01966 decimals, unsigned_flag); 01967 } 01968 01969 01970 Item_decimal::Item_decimal(double val, int precision, int scale) 01971 { 01972 double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value); 01973 decimals= (uint8) decimal_value.frac; 01974 fixed= 1; 01975 max_length= my_decimal_precision_to_length(decimal_value.intg + decimals, 01976 decimals, unsigned_flag); 01977 } 01978 01979 01980 Item_decimal::Item_decimal(const char *str, const my_decimal *val_arg, 01981 uint decimal_par, uint length) 01982 { 01983 my_decimal2decimal(val_arg, &decimal_value); 01984 name= (char*) str; 01985 decimals= (uint8) decimal_par; 01986 max_length= length; 01987 fixed= 1; 01988 } 01989 01990 01991 Item_decimal::Item_decimal(my_decimal *value_par) 01992 { 01993 my_decimal2decimal(value_par, &decimal_value); 01994 decimals= (uint8) decimal_value.frac; 01995 fixed= 1; 01996 max_length= my_decimal_precision_to_length(decimal_value.intg + decimals, 01997 decimals, unsigned_flag); 01998 } 01999 02000 02001 Item_decimal::Item_decimal(const char *bin, int precision, int scale) 02002 { 02003 binary2my_decimal(E_DEC_FATAL_ERROR, bin, 02004 &decimal_value, precision, scale); 02005 decimals= (uint8) decimal_value.frac; 02006 fixed= 1; 02007 max_length= my_decimal_precision_to_length(precision, decimals, 02008 unsigned_flag); 02009 } 02010 02011 02012 longlong Item_decimal::val_int() 02013 { 02014 longlong result; 02015 my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &result); 02016 return result; 02017 } 02018 02019 double Item_decimal::val_real() 02020 { 02021 double result; 02022 my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result); 02023 return result; 02024 } 02025 02026 String *Item_decimal::val_str(String *result) 02027 { 02028 result->set_charset(&my_charset_bin); 02029 my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, result); 02030 return result; 02031 } 02032 02033 void Item_decimal::print(String *str) 02034 { 02035 my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, &str_value); 02036 str->append(str_value); 02037 } 02038 02039 02040 bool Item_decimal::eq(const Item *item, bool binary_cmp) const 02041 { 02042 if (type() == item->type() && item->basic_const_item()) 02043 { 02044 /* 02045 We need to cast off const to call val_decimal(). This should 02046 be OK for a basic constant. Additionally, we can pass 0 as 02047 a true decimal constant will return its internal decimal 02048 storage and ignore the argument. 02049 */ 02050 Item *arg= (Item*) item; 02051 my_decimal *value= arg->val_decimal(0); 02052 return !my_decimal_cmp(&decimal_value, value); 02053 } 02054 return 0; 02055 } 02056 02057 02058 void Item_decimal::set_decimal_value(my_decimal *value_par) 02059 { 02060 my_decimal2decimal(value_par, &decimal_value); 02061 decimals= (uint8) decimal_value.frac; 02062 unsigned_flag= !decimal_value.sign(); 02063 max_length= my_decimal_precision_to_length(decimal_value.intg + decimals, 02064 decimals, unsigned_flag); 02065 } 02066 02067 02068 String *Item_float::val_str(String *str) 02069 { 02070 // following assert is redundant, because fixed=1 assigned in constructor 02071 DBUG_ASSERT(fixed == 1); 02072 str->set_real(value,decimals,&my_charset_bin); 02073 return str; 02074 } 02075 02076 02077 my_decimal *Item_float::val_decimal(my_decimal *decimal_value) 02078 { 02079 // following assert is redundant, because fixed=1 assigned in constructor 02080 DBUG_ASSERT(fixed == 1); 02081 double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_value); 02082 return (decimal_value); 02083 } 02084 02085 02086 void Item_string::print(String *str) 02087 { 02088 str->append('_'); 02089 str->append(collation.collation->csname); 02090 str->append('\''); 02091 str_value.print(str); 02092 str->append('\''); 02093 } 02094 02095 02096 inline bool check_if_only_end_space(CHARSET_INFO *cs, char *str, char *end) 02097 { 02098 return str+ cs->cset->scan(cs, str, end, MY_SEQ_SPACES) == end; 02099 } 02100 02101 02102 double Item_string::val_real() 02103 { 02104 DBUG_ASSERT(fixed == 1); 02105 int error; 02106 char *end, *org_end; 02107 double tmp; 02108 CHARSET_INFO *cs= str_value.charset(); 02109 02110 org_end= (char*) str_value.ptr() + str_value.length(); 02111 tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end, 02112 &error); 02113 if (error || (end != org_end && !check_if_only_end_space(cs, end, org_end))) 02114 { 02115 /* 02116 We can use str_value.ptr() here as Item_string is gurantee to put an 02117 end \0 here. 02118 */ 02119 push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, 02120 ER_TRUNCATED_WRONG_VALUE, 02121 ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE", 02122 str_value.ptr()); 02123 } 02124 return tmp; 02125 } 02126 02127 02128 longlong Item_string::val_int() 02129 { 02130 DBUG_ASSERT(fixed == 1); 02131 int err; 02132 longlong tmp; 02133 char *end= (char*) str_value.ptr()+ str_value.length(); 02134 char *org_end= end; 02135 CHARSET_INFO *cs= str_value.charset(); 02136 02137 tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err); 02138 /* 02139 TODO: Give error if we wanted a signed integer and we got an unsigned 02140 one 02141 */ 02142 if (err > 0 || 02143 (end != org_end && !check_if_only_end_space(cs, end, org_end))) 02144 { 02145 push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, 02146 ER_TRUNCATED_WRONG_VALUE, 02147 ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER", 02148 str_value.ptr()); 02149 } 02150 return tmp; 02151 } 02152 02153 02154 my_decimal *Item_string::val_decimal(my_decimal *decimal_value) 02155 { 02156 return val_decimal_from_string(decimal_value); 02157 } 02158 02159 02160 bool Item_null::eq(const Item *item, bool binary_cmp) const 02161 { return item->type() == type(); } 02162 02163 02164 double Item_null::val_real() 02165 { 02166 // following assert is redundant, because fixed=1 assigned in constructor 02167 DBUG_ASSERT(fixed == 1); 02168 null_value=1; 02169 return 0.0; 02170 } 02171 longlong Item_null::val_int() 02172 { 02173 // following assert is redundant, because fixed=1 assigned in constructor 02174 DBUG_ASSERT(fixed == 1); 02175 null_value=1; 02176 return 0; 02177 } 02178 /* ARGSUSED */ 02179 String *Item_null::val_str(String *str) 02180 { 02181 // following assert is redundant, because fixed=1 assigned in constructor 02182 DBUG_ASSERT(fixed == 1); 02183 null_value=1; 02184 return 0; 02185 } 02186 02187 my_decimal *Item_null::val_decimal(my_decimal *decimal_value) 02188 { 02189 return 0; 02190 } 02191 02192 02193 Item *Item_null::safe_charset_converter(CHARSET_INFO *tocs) 02194 { 02195 collation.set(tocs); 02196 return this; 02197 } 02198 02199 /*********************** Item_param related ******************************/ 02200 02201 /* 02202 Default function of Item_param::set_param_func, so in case 02203 of malformed packet the server won't SIGSEGV 02204 */ 02205 02206 static void 02207 default_set_param_func(Item_param *param, 02208 uchar **pos __attribute__((unused)), 02209 ulong len __attribute__((unused))) 02210 { 02211 param->set_null(); 02212 } 02213 02214 02215 Item_param::Item_param(unsigned pos_in_query_arg) : 02216 state(NO_VALUE), 02217 item_result_type(STRING_RESULT), 02218 /* Don't pretend to be a literal unless value for this item is set. */ 02219 item_type(PARAM_ITEM), 02220 param_type(MYSQL_TYPE_VARCHAR), 02221 pos_in_query(pos_in_query_arg), 02222 set_param_func(default_set_param_func) 02223 { 02224 name= (char*) "?"; 02225 /* 02226 Since we can't say whenever this item can be NULL or cannot be NULL 02227 before mysql_stmt_execute(), so we assuming that it can be NULL until 02228 value is set. 02229 */ 02230 maybe_null= 1; 02231 cnvitem= new Item_string("", 0, &my_charset_bin, DERIVATION_COERCIBLE); 02232 cnvstr.set(cnvbuf, sizeof(cnvbuf), &my_charset_bin); 02233 } 02234 02235 02236 void Item_param::set_null() 02237 { 02238 DBUG_ENTER("Item_param::set_null"); 02239 /* These are cleared after each execution by reset() method */ 02240 null_value= 1; 02241 /* 02242 Because of NULL and string values we need to set max_length for each new 02243 placeholder value: user can submit NULL for any placeholder type, and 02244 string length can be different in each execution. 02245 */ 02246 max_length= 0; 02247 decimals= 0; 02248 state= NULL_VALUE; 02249 item_type= Item::NULL_ITEM; 02250 DBUG_VOID_RETURN; 02251 } 02252 02253 void Item_param::set_int(longlong i, uint32 max_length_arg) 02254 { 02255 DBUG_ENTER("Item_param::set_int"); 02256 value.integer= (longlong) i; 02257 state= INT_VALUE; 02258 max_length= max_length_arg; 02259 decimals= 0; 02260 maybe_null= 0; 02261 DBUG_VOID_RETURN; 02262 } 02263 02264 void Item_param::set_double(double d) 02265 { 02266 DBUG_ENTER("Item_param::set_double"); 02267 value.real= d; 02268 state= REAL_VALUE; 02269 max_length= DBL_DIG + 8; 02270 decimals= NOT_FIXED_DEC; 02271 maybe_null= 0; 02272 DBUG_VOID_RETURN; 02273 } 02274 02275 02276 /* 02277 Set decimal parameter value from string. 02278 02279 SYNOPSIS 02280 set_decimal() 02281 str - character string 02282 length - string length 02283 02284 NOTE 02285 as we use character strings to send decimal values in 02286 binary protocol, we use str2my_decimal to convert it to 02287 internal decimal value. 02288 */ 02289 02290 void Item_param::set_decimal(const char *str, ulong length) 02291 { 02292 char *end; 02293 DBUG_ENTER("Item_param::set_decimal"); 02294 02295 end= (char*) str+length; 02296 str2my_decimal(E_DEC_FATAL_ERROR, str, &decimal_value, &end); 02297 state= DECIMAL_VALUE; 02298 decimals= decimal_value.frac; 02299 max_length= my_decimal_precision_to_length(decimal_value.precision(), 02300 decimals, unsigned_flag); 02301 maybe_null= 0; 02302 DBUG_VOID_RETURN; 02303 } 02304 02305 02306 /* 02307 Set parameter value from TIME value. 02308 02309 SYNOPSIS 02310 set_time() 02311 tm - datetime value to set (time_type is ignored) 02312 type - type of datetime value 02313 max_length_arg - max length of datetime value as string 02314 02315 NOTE 02316 If we value to be stored is not normalized, zero value will be stored 02317 instead and proper warning will be produced. This function relies on 02318 the fact that even wrong value sent over binary protocol fits into 02319 MAX_DATE_STRING_REP_LENGTH buffer. 02320 */ 02321 void Item_param::set_time(TIME *tm, timestamp_type type, uint32 max_length_arg) 02322 { 02323 DBUG_ENTER("Item_param::set_time"); 02324 02325 value.time= *tm; 02326 value.time.time_type= type; 02327 02328 if (value.time.year > 9999 || value.time.month > 12 || 02329 value.time.day > 31 || 02330 type != MYSQL_TIMESTAMP_TIME && value.time.hour > 23 || 02331 value.time.minute > 59 || value.time.second > 59) 02332 { 02333 char buff[MAX_DATE_STRING_REP_LENGTH]; 02334 uint length= my_TIME_to_str(&value.time, buff); 02335 make_truncated_value_warning(current_thd, buff, length, type, 0); 02336 set_zero_time(&value.time, MYSQL_TIMESTAMP_ERROR); 02337 } 02338 02339 state= TIME_VALUE; 02340 maybe_null= 0; 02341 max_length= max_length_arg; 02342 decimals= 0; 02343 DBUG_VOID_RETURN; 02344 } 02345 02346 02347 bool Item_param::set_str(const char *str, ulong length) 02348 { 02349 DBUG_ENTER("Item_param::set_str"); 02350 /* 02351 Assign string with no conversion: data is converted only after it's 02352 been written to the binary log. 02353 */ 02354 uint dummy_errors; 02355 if (str_value.copy(str, length, &my_charset_bin, &my_charset_bin, 02356 &dummy_errors)) 02357 DBUG_RETURN(TRUE); 02358 state= STRING_VALUE; 02359 max_length= length; 02360 maybe_null= 0; 02361 /* max_length and decimals are set after charset conversion */ 02362 /* sic: str may be not null-terminated, don't add DBUG_PRINT here */ 02363 DBUG_RETURN(FALSE); 02364 } 02365 02366 02367 bool Item_param::set_longdata(const char *str, ulong length) 02368 { 02369 DBUG_ENTER("Item_param::set_longdata"); 02370 02371 /* 02372 If client character set is multibyte, end of long data packet 02373 may hit at the middle of a multibyte character. Additionally, 02374 if binary log is open we must write long data value to the 02375 binary log in character set of client. This is why we can't 02376 convert long data to connection character set as it comes 02377 (here), and first have to concatenate all pieces together, 02378 write query to the binary log and only then perform conversion. 02379 */ 02380 if (str_value.append(str, length, &my_charset_bin)) 02381 DBUG_RETURN(TRUE); 02382 state= LONG_DATA_VALUE; 02383 maybe_null= 0; 02384 02385 DBUG_RETURN(FALSE); 02386 } 02387 02388 02389 /* 02390 Set parameter value from user variable value. 02391 02392 SYNOPSIS 02393 set_from_user_var 02394 thd Current thread 02395 entry User variable structure (NULL means use NULL value) 02396 02397 RETURN 02398 0 OK 02399 1 Out of memory 02400 */ 02401 02402 bool Item_param::set_from_user_var(THD *thd, const user_var_entry *entry) 02403 { 02404 DBUG_ENTER("Item_param::set_from_user_var"); 02405 if (entry && entry->value) 02406 { 02407 item_result_type= entry->type; 02408 switch (entry->type) { 02409 case REAL_RESULT: 02410 set_double(*(double*)entry->value); 02411 item_type= Item::REAL_ITEM; 02412 item_result_type= REAL_RESULT; 02413 break; 02414 case INT_RESULT: 02415 set_int(*(longlong*)entry->value, 21); 02416 item_type= Item::INT_ITEM; 02417 item_result_type= INT_RESULT; 02418 break; 02419 case STRING_RESULT: 02420 { 02421 CHARSET_INFO *fromcs= entry->collation.collation; 02422 CHARSET_INFO *tocs= thd->variables.collation_connection; 02423 uint32 dummy_offset; 02424 02425 value.cs_info.character_set_of_placeholder= 02426 value.cs_info.character_set_client= fromcs; 02427 /* 02428 Setup source and destination character sets so that they 02429 are different only if conversion is necessary: this will 02430 make later checks easier. 02431 */ 02432 value.cs_info.final_character_set_of_str_value= 02433 String::needs_conversion(0, fromcs, tocs, &dummy_offset) ? 02434 tocs : fromcs; 02435 /* 02436 Exact value of max_length is not known unless data is converted to 02437 charset of connection, so we have to set it later. 02438 */ 02439 item_type= Item::STRING_ITEM; 02440 item_result_type= STRING_RESULT; 02441 02442 if (set_str((const char *)entry->value, entry->length)) 02443 DBUG_RETURN(1); 02444 break; 02445 } 02446 case DECIMAL_RESULT: 02447 { 02448 const my_decimal *ent_value= (const my_decimal *)entry->value; 02449 my_decimal2decimal(ent_value, &decimal_value); 02450 state= DECIMAL_VALUE; 02451 decimals= ent_value->frac; 02452 max_length= my_decimal_precision_to_length(ent_value->precision(), 02453 decimals, unsigned_flag); 02454 break; 02455 } 02456 default: 02457 DBUG_ASSERT(0); 02458 set_null(); 02459 } 02460 } 02461 else 02462 set_null(); 02463 02464 DBUG_RETURN(0); 02465 } 02466 02467 /* 02468 Resets parameter after execution. 02469 02470 SYNOPSIS 02471 Item_param::reset() 02472 02473 NOTES 02474 We clear null_value here instead of setting it in set_* methods, 02475 because we want more easily handle case for long data. 02476 */ 02477 02478 void Item_param::reset() 02479 { 02480 DBUG_ENTER("Item_param::reset"); 02481 /* Shrink string buffer if it's bigger than max possible CHAR column */ 02482 if (str_value.alloced_length() > MAX_CHAR_WIDTH) 02483 str_value.free(); 02484 else 02485 str_value.length(0); 02486 str_value_ptr.length(0); 02487 /* 02488 We must prevent all charset conversions until data has been written 02489 to the binary log. 02490 */ 02491 str_value.set_charset(&my_charset_bin); 02492 collation.set(&my_charset_bin, DERIVATION_COERCIBLE); 02493 state= NO_VALUE; 02494 maybe_null= 1; 02495 null_value= 0; 02496 /* 02497 Don't reset item_type to PARAM_ITEM: it's only needed to guard 02498 us from item optimizations at prepare stage, when item doesn't yet 02499 contain a literal of some kind. 02500 In all other cases when this object is accessed its value is 02501 set (this assumption is guarded by 'state' and 02502 DBUG_ASSERTS(state != NO_VALUE) in all Item_param::get_* 02503 methods). 02504 */ 02505 DBUG_VOID_RETURN; 02506 } 02507 02508 02509 int Item_param::save_in_field(Field *field, bool no_conversions) 02510 { 02511 field->set_notnull(); 02512 02513 switch (state) { 02514 case INT_VALUE: 02515 return field->store(value.integer, unsigned_flag); 02516 case REAL_VALUE: 02517 return field->store(value.real); 02518 case DECIMAL_VALUE: 02519 return field->store_decimal(&decimal_value); 02520 case TIME_VALUE: 02521 field->store_time(&value.time, value.time.time_type); 02522 return 0; 02523 case STRING_VALUE: 02524 case LONG_DATA_VALUE: 02525 return field->store(str_value.ptr(), str_value.length(), 02526 str_value.charset()); 02527 case NULL_VALUE: 02528 return set_field_to_null_with_conversions(field, no_conversions); 02529 case NO_VALUE: 02530 default: 02531 DBUG_ASSERT(0); 02532 } 02533 return 1; 02534 } 02535 02536 02537 bool Item_param::get_time(TIME *res) 02538 { 02539 if (state == TIME_VALUE) 02540 { 02541 *res= value.time; 02542 return 0; 02543 } 02544 /* 02545 If parameter value isn't supplied assertion will fire in val_str() 02546 which is called from Item::get_time(). 02547 */ 02548 return Item::get_time(res); 02549 } 02550 02551 02552 bool Item_param::get_date(TIME *res, uint fuzzydate) 02553 { 02554 if (state == TIME_VALUE) 02555 { 02556 *res= value.time; 02557 return 0; 02558 } 02559 return Item::get_date(res, fuzzydate); 02560 } 02561 02562 02563 double Item_param::val_real() 02564 { 02565 switch (state) { 02566 case REAL_VALUE: 02567 return value.real; 02568 case INT_VALUE: 02569 return (double) value.integer; 02570 case DECIMAL_VALUE: 02571 { 02572 double result; 02573 my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result); 02574 return result; 02575 } 02576 case STRING_VALUE: 02577 case LONG_DATA_VALUE: 02578 { 02579 int dummy_err; 02580 char *end_not_used; 02581 return my_strntod(str_value.charset(), (char*) str_value.ptr(), 02582 str_value.length(), &end_not_used, &dummy_err); 02583 } 02584 case TIME_VALUE: 02585 /* 02586 This works for example when user says SELECT ?+0.0 and supplies 02587 time value for the placeholder. 02588 */ 02589 return ulonglong2double(TIME_to_ulonglong(&value.time)); 02590 case NULL_VALUE: 02591 return 0.0; 02592 default: 02593 DBUG_ASSERT(0); 02594 } 02595 return 0.0; 02596 } 02597 02598 02599 longlong Item_param::val_int() 02600 { 02601 switch (state) { 02602 case REAL_VALUE: 02603 return (longlong) rint(value.real); 02604 case INT_VALUE: 02605 return value.integer; 02606 case DECIMAL_VALUE: 02607 { 02608 longlong i; 02609 my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &i); 02610 return i; 02611 } 02612 case STRING_VALUE: 02613 case LONG_DATA_VALUE: 02614 { 02615 int dummy_err; 02616 return my_strntoll(str_value.charset(), str_value.ptr(), 02617 str_value.length(), 10, (char**) 0, &dummy_err); 02618 } 02619 case TIME_VALUE: 02620 return (longlong) TIME_to_ulonglong(&value.time); 02621 case NULL_VALUE: 02622 return 0; 02623 default: 02624 DBUG_ASSERT(0); 02625 } 02626 return 0; 02627 } 02628 02629 02630 my_decimal *Item_param::val_decimal(my_decimal *dec) 02631 { 02632 switch (state) { 02633 case DECIMAL_VALUE: 02634 return &decimal_value; 02635 case REAL_VALUE: 02636 double2my_decimal(E_DEC_FATAL_ERROR, value.real, dec); 02637 return dec; 02638 case INT_VALUE: 02639 int2my_decimal(E_DEC_FATAL_ERROR, value.integer, unsigned_flag, dec); 02640 return dec; 02641 case STRING_VALUE: 02642 case LONG_DATA_VALUE: 02643 string2my_decimal(E_DEC_FATAL_ERROR, &str_value, dec); 02644 return dec; 02645 case TIME_VALUE: 02646 { 02647 longlong i= (longlong) TIME_to_ulonglong(&value.time); 02648 int2my_decimal(E_DEC_FATAL_ERROR, i, 0, dec); 02649 return dec; 02650 } 02651 case NULL_VALUE: 02652 return 0; 02653 default: 02654 DBUG_ASSERT(0); 02655 } 02656 return 0; 02657 } 02658 02659 02660 String *Item_param::val_str(String* str) 02661 { 02662 switch (state) { 02663 case STRING_VALUE: 02664 case LONG_DATA_VALUE: 02665 return &str_value_ptr; 02666 case REAL_VALUE: 02667 str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin); 02668 return str; 02669 case INT_VALUE: 02670 str->set(value.integer, &my_charset_bin); 02671 return str; 02672 case DECIMAL_VALUE: 02673 if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 02674 0, 0, 0, str) <= 1) 02675 return str; 02676 return NULL; 02677 case TIME_VALUE: 02678 { 02679 if (str->reserve(MAX_DATE_STRING_REP_LENGTH)) 02680 break; 02681 str->length((uint) my_TIME_to_str(&value.time, (char*) str->ptr())); 02682 str->set_charset(&my_charset_bin); 02683 return str; 02684 } 02685 case NULL_VALUE: 02686 return NULL; 02687 default: 02688 DBUG_ASSERT(0); 02689 } 02690 return str; 02691 } 02692 02693 /* 02694 Return Param item values in string format, for generating the dynamic 02695 query used in update/binary logs 02696 TODO: change interface and implementation to fill log data in place 02697 and avoid one more memcpy/alloc between str and log string. 02698 */ 02699 02700 const String *Item_param::query_val_str(String* str) const 02701 { 02702 switch (state) { 02703 case INT_VALUE: 02704 str->set(value.integer, &my_charset_bin); 02705 break; 02706 case REAL_VALUE: 02707 str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin); 02708 break; 02709 case DECIMAL_VALUE: 02710 if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 02711 0, 0, 0, str) > 1) 02712 return &my_null_string; 02713 break; 02714 case TIME_VALUE: 02715 { 02716 char *buf, *ptr; 02717 str->length(0); 02718 /* 02719 TODO: in case of error we need to notify replication 02720 that binary log contains wrong statement 02721 */ 02722 if (str->reserve(MAX_DATE_STRING_REP_LENGTH+3)) 02723 break; 02724 02725 /* Create date string inplace */ 02726 buf= str->c_ptr_quick(); 02727 ptr= buf; 02728 *ptr++= '\''; 02729 ptr+= (uint) my_TIME_to_str(&value.time, ptr); 02730 *ptr++= '\''; 02731 str->length((uint32) (ptr - buf)); 02732 break; 02733 } 02734 case STRING_VALUE: 02735 case LONG_DATA_VALUE: 02736 { 02737 str->length(0); 02738 append_query_string(value.cs_info.character_set_client, &str_value, str); 02739 break; 02740 } 02741 case NULL_VALUE: 02742 return &my_null_string; 02743 default: 02744 DBUG_ASSERT(0); 02745 } 02746 return str; 02747 } 02748 02749 02750 /* 02751 Convert string from client character set to the character set of 02752 connection. 02753 */ 02754 02755 bool Item_param::convert_str_value(THD *thd) 02756 { 02757 bool rc= FALSE; 02758 if (state == STRING_VALUE || state == LONG_DATA_VALUE) 02759 { 02760 /* 02761 Check is so simple because all charsets were set up properly 02762 in setup_one_conversion_function, where typecode of 02763 placeholder was also taken into account: the variables are different 02764 here only if conversion is really necessary. 02765 */ 02766 if (value.cs_info.final_character_set_of_str_value != 02767 value.cs_info.character_set_of_placeholder) 02768 { 02769 rc= thd->convert_string(&str_value, 02770 value.cs_info.character_set_of_placeholder, 02771 value.cs_info.final_character_set_of_str_value); 02772 } 02773 else 02774 str_value.set_charset(value.cs_info.final_character_set_of_str_value); 02775 /* Here str_value is guaranteed to be in final_character_set_of_str_value */ 02776 02777 max_length= str_value.length(); 02778 decimals= 0; 02779 /* 02780 str_value_ptr is returned from val_str(). It must be not alloced 02781 to prevent it's modification by val_str() invoker. 02782 */ 02783 str_value_ptr.set(str_value.ptr(), str_value.length(), 02784 str_value.charset()); 02785 /* Synchronize item charset with value charset */ 02786 collation.set(str_value.charset(), DERIVATION_COERCIBLE); 02787 } 02788 return rc; 02789 } 02790 02791 02792 bool Item_param::basic_const_item() const 02793 { 02794 if (state == NO_VALUE || state == TIME_VALUE) 02795 return FALSE; 02796 return TRUE; 02797 } 02798 02799 02800 Item * 02801 Item_param::new_item() 02802 { 02803 /* see comments in the header file */ 02804 switch (state) { 02805 case NULL_VALUE: 02806 return new Item_null(name); 02807 case INT_VALUE: 02808 return (unsigned_flag ? 02809 new Item_uint(name, value.integer, max_length) : 02810 new Item_int(name, value.integer, max_length)); 02811 case REAL_VALUE: 02812 return new Item_float(name, value.real, decimals, max_length); 02813 case STRING_VALUE: 02814 case LONG_DATA_VALUE: 02815 return new Item_string(name, str_value.c_ptr_quick(), str_value.length(), 02816 str_value.charset()); 02817 case TIME_VALUE: 02818 break; 02819 case NO_VALUE: 02820 default: 02821 DBUG_ASSERT(0); 02822 }; 02823 return 0; 02824 } 02825 02826 02827 bool 02828 Item_param::eq(const Item *arg, bool binary_cmp) const 02829 { 02830 Item *item; 02831 if (!basic_const_item() || !arg->basic_const_item() || arg->type() != type()) 02832 return FALSE; 02833 /* 02834 We need to cast off const to call val_int(). This should be OK for 02835 a basic constant. 02836 */ 02837 item= (Item*) arg; 02838 02839 switch (state) { 02840 case NULL_VALUE: 02841 return TRUE; 02842 case INT_VALUE: 02843 return value.integer == item->val_int() && 02844 unsigned_flag == item->unsigned_flag; 02845 case REAL_VALUE: 02846 return value.real == item->val_real(); 02847 case STRING_VALUE: 02848 case LONG_DATA_VALUE: 02849 if (binary_cmp) 02850 return !stringcmp(&str_value, &item->str_value); 02851 return !sortcmp(&str_value, &item->str_value, collation.collation); 02852 default: 02853 break; 02854 } 02855 return FALSE; 02856 } 02857 02858 /* End of Item_param related */ 02859 02860 void Item_param::print(String *str) 02861 { 02862 if (state == NO_VALUE) 02863 { 02864 str->append('?'); 02865 } 02866 else 02867 { 02868 char buffer[STRING_BUFFER_USUAL_SIZE]; 02869 String tmp(buffer, sizeof(buffer), &my_charset_bin); 02870 const String *res; 02871 res= query_val_str(&tmp); 02872 str->append(*res); 02873 } 02874 } 02875 02876 02877 /**************************************************************************** 02878 Item_copy_string 02879 ****************************************************************************/ 02880 02881 void Item_copy_string::copy() 02882 { 02883 String *res=item->val_str(&str_value); 02884 if (res && res != &str_value) 02885 str_value.copy(*res); 02886 null_value=item->null_value; 02887 } 02888 02889 /* ARGSUSED */ 02890 String *Item_copy_string::val_str(String *str) 02891 { 02892 // Item_copy_string is used without fix_fields call 02893 if (null_value) 02894 return (String*) 0; 02895 return &str_value; 02896 } 02897 02898 02899 my_decimal *Item_copy_string::val_decimal(my_decimal *decimal_value) 02900 { 02901 // Item_copy_string is used without fix_fields call 02902 if (null_value) 02903 return 0; 02904 string2my_decimal(E_DEC_FATAL_ERROR, &str_value, decimal_value); 02905 return (decimal_value); 02906 } 02907 02908 02909 02910 int Item_copy_string::save_in_field(Field *field, bool no_conversions) 02911 { 02912 if (null_value) 02913 return set_field_to_null(field); 02914 field->set_notnull(); 02915 return field->store(str_value.ptr(),str_value.length(), 02916 collation.collation); 02917 } 02918 02919 /* 02920 Functions to convert item to field (for send_fields) 02921 */ 02922 02923 /* ARGSUSED */ 02924 bool Item::fix_fields(THD *thd, Item **ref) 02925 { 02926 02927 // We do not check fields which are fixed during construction 02928 DBUG_ASSERT(fixed == 0 || basic_const_item()); 02929 fixed= 1; 02930 return FALSE; 02931 } 02932 02933 double Item_ref_null_helper::val_real() 02934 { 02935 DBUG_ASSERT(fixed == 1); 02936 double tmp= (*ref)->val_result(); 02937 owner->was_null|= null_value= (*ref)->null_value; 02938 return tmp; 02939 } 02940 02941 02942 longlong Item_ref_null_helper::val_int() 02943 { 02944 DBUG_ASSERT(fixed == 1); 02945 longlong tmp= (*ref)->val_int_result(); 02946 owner->was_null|= null_value= (*ref)->null_value; 02947 return tmp; 02948 } 02949 02950 02951 my_decimal *Item_ref_null_helper::val_decimal(my_decimal *decimal_value) 02952 { 02953 DBUG_ASSERT(fixed == 1); 02954 my_decimal *val= (*ref)->val_decimal_result(decimal_value); 02955 owner->was_null|= null_value= (*ref)->null_value; 02956 return val; 02957 } 02958 02959 02960 bool Item_ref_null_helper::val_bool() 02961 { 02962 DBUG_ASSERT(fixed == 1); 02963 bool val= (*ref)->val_bool_result(); 02964 owner->was_null|= null_value= (*ref)->null_value; 02965 return val; 02966 } 02967 02968 02969 String* Item_ref_null_helper::val_str(String* s) 02970 { 02971 DBUG_ASSERT(fixed == 1); 02972 String* tmp= (*ref)->str_result(s); 02973 owner->was_null|= null_value= (*ref)->null_value; 02974 return tmp; 02975 } 02976 02977 02978 bool Item_ref_null_helper::get_date(TIME *ltime, uint fuzzydate) 02979 { 02980 return (owner->was_null|= null_value= (*ref)->get_date(ltime, fuzzydate)); 02981 } 02982 02983 02984 /* 02985 Mark item and SELECT_LEXs as dependent if item was resolved in outer SELECT 02986 02987 SYNOPSIS 02988 mark_as_dependent() 02989 thd - thread handler 02990 last - select from which current item depend 02991 current - current select 02992 resolved_item - item which was resolved in outer SELECT(for warning) 02993 mark_item - item which should be marked (can be differ in case of 02994 substitution) 02995 */ 02996 02997 static void mark_as_dependent(THD *thd, SELECT_LEX *last, SELECT_LEX *current, 02998 Item_ident *resolved_item, 02999 Item_ident *mark_item) 03000 { 03001 const char *db_name= (resolved_item->db_name ? 03002 resolved_item->db_name : ""); 03003 const char *table_name= (resolved_item->table_name ? 03004 resolved_item->table_name : ""); 03005 /* store pointer on SELECT_LEX from which item is dependent */ 03006 if (mark_item) 03007 mark_item->depended_from= last; 03008 current->mark_as_dependent(last); 03009 if (thd->lex->describe & DESCRIBE_EXTENDED) 03010 { 03011 char warn_buff[MYSQL_ERRMSG_SIZE]; 03012 sprintf(warn_buff, ER(ER_WARN_FIELD_RESOLVED), 03013 db_name, (db_name[0] ? "." : ""), 03014 table_name, (table_name [0] ? "." : ""), 03015 resolved_item->field_name, 03016 current->select_number, last->select_number); 03017 push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE, 03018 ER_WARN_FIELD_RESOLVED, warn_buff); 03019 } 03020 } 03021 03022 03023 /* 03024 Mark range of selects and resolved identifier (field/reference) item as 03025 dependent 03026 03027 SYNOPSIS 03028 mark_select_range_as_dependent() 03029 thd - thread handler 03030 last_select - select where resolved_item was resolved 03031 current_sel - current select (select where resolved_item was placed) 03032 found_field - field which was found during resolving 03033 found_item - Item which was found during resolving (if resolved 03034 identifier belongs to VIEW) 03035 resolved_item - Identifier which was resolved 03036 03037 NOTE: 03038 We have to mark all items between current_sel (including) and 03039 last_select (excluding) as dependend (select before last_select should 03040 be marked with actual table mask used by resolved item, all other with 03041 OUTER_REF_TABLE_BIT) and also write dependence information to Item of 03042 resolved identifier. 03043 */ 03044 03045 void mark_select_range_as_dependent(THD *thd, 03046 SELECT_LEX *last_select, 03047 SELECT_LEX *current_sel, 03048 Field *found_field, Item *found_item, 03049 Item_ident *resolved_item) 03050 { 03051 /* 03052 Go from current SELECT to SELECT where field was resolved (it 03053 have to be reachable from current SELECT, because it was already 03054 done once when we resolved this field and cached result of 03055 resolving) 03056 */ 03057 SELECT_LEX *previous_select= current_sel; 03058 for (; previous_select->outer_select() != last_select; 03059 previous_select= previous_select->outer_select()) 03060 { 03061 Item_subselect *prev_subselect_item= 03062 previous_select->master_unit()->item; 03063 prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT; 03064 prev_subselect_item->const_item_cache= 0; 03065 } 03066 { 03067 Item_subselect *prev_subselect_item= 03068 previous_select->master_unit()->item; 03069 Item_ident *dependent= resolved_item; 03070 if (found_field == view_ref_found) 03071 { 03072 Item::Type type= found_item->type(); 03073 prev_subselect_item->used_tables_cache|= 03074 found_item->used_tables(); 03075 dependent= ((type == Item::REF_ITEM || type == Item::FIELD_ITEM) ? 03076 (Item_ident*) found_item : 03077 0); 03078 } 03079 else 03080 prev_subselect_item->used_tables_cache|= 03081 found_field->table->map; 03082 prev_subselect_item->const_item_cache= 0; 03083 mark_as_dependent(thd, last_select, current_sel, resolved_item, 03084 dependent); 03085 } 03086 } 03087 03088 03089 /* 03090 Search a GROUP BY clause for a field with a certain name. 03091 03092 SYNOPSIS 03093 find_field_in_group_list() 03094 find_item the item being searched for 03095 group_list GROUP BY clause 03096 03097 DESCRIPTION 03098 Search the GROUP BY list for a column named as find_item. When searching 03099 preference is given to columns that are qualified with the same table (and 03100 database) name as the one being searched for. 03101 03102 RETURN 03103 - the found item on success 03104 - NULL if find_item is not in group_list 03105 */ 03106 03107 static Item** find_field_in_group_list(Item *find_item, ORDER *group_list) 03108 { 03109 const char *db_name; 03110 const char *table_name; 03111 const char *field_name; 03112 ORDER *found_group= NULL; 03113 int found_match_degree= 0; 03114 Item_ident *cur_field; 03115 int cur_match_degree= 0; 03116 char name_buff[NAME_LEN+1]; 03117 03118 if (find_item->type() == Item::FIELD_ITEM || 03119 find_item->type() == Item::REF_ITEM) 03120 { 03121 db_name= ((Item_ident*) find_item)->db_name; 03122 table_name= ((Item_ident*) find_item)->table_name; 03123 field_name= ((Item_ident*) find_item)->field_name; 03124 } 03125 else 03126 return NULL; 03127 03128 if (db_name && lower_case_table_names) 03129 { 03130 /* Convert database to lower case for comparison */ 03131 strmake(name_buff, db_name, sizeof(name_buff)-1); 03132 my_casedn_str(files_charset_info, name_buff); 03133 db_name= name_buff; 03134 } 03135 03136 DBUG_ASSERT(field_name != 0); 03137 03138 for (ORDER *cur_group= group_list ; cur_group ; cur_group= cur_group->next) 03139 { 03140 if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM) 03141 { 03142 cur_field= (Item_ident*) *cur_group->item; 03143 cur_match_degree= 0; 03144 03145 DBUG_ASSERT(cur_field->field_name != 0); 03146 03147 if (!my_strcasecmp(system_charset_info, 03148 cur_field->field_name, field_name)) 03149 ++cur_match_degree; 03150 else 03151 continue; 03152 03153 if (cur_field->table_name && table_name) 03154 { 03155 /* If field_name is qualified by a table name. */ 03156 if (strcmp(cur_field->table_name, table_name)) 03157 /* Same field names, different tables. */ 03158 return NULL; 03159 03160 ++cur_match_degree; 03161 if (cur_field->db_name && db_name) 03162 { 03163 /* If field_name is also qualified by a database name. */ 03164 if (strcmp(cur_field->db_name, db_name)) 03165 /* Same field names, different databases. */ 03166 return NULL; 03167 ++cur_match_degree; 03168 } 03169 } 03170 03171 if (cur_match_degree > found_match_degree) 03172 { 03173 found_match_degree= cur_match_degree; 03174 found_group= cur_group; 03175 } 03176 else if (found_group && (cur_match_degree == found_match_degree) && 03177 ! (*(found_group->item))->eq(cur_field, 0)) 03178 { 03179 /* 03180 If the current resolve candidate matches equally well as the current 03181 best match, they must reference the same column, otherwise the field 03182 is ambiguous. 03183 */ 03184 my_error(ER_NON_UNIQ_ERROR, MYF(0), 03185 find_item->full_name(), current_thd->where); 03186 return NULL; 03187 } 03188 } 03189 } 03190 03191 if (found_group) 03192 return found_group->item; 03193 else 03194 return NULL; 03195 } 03196 03197 03198 /* 03199 Resolve a column reference in a sub-select. 03200 03201 SYNOPSIS 03202 resolve_ref_in_select_and_group() 03203 thd current thread 03204 ref column reference being resolved 03205 select the sub-select that ref is resolved against 03206 03207 DESCRIPTION 03208 Resolve a column reference (usually inside a HAVING clause) against the 03209 SELECT and GROUP BY clauses of the query described by 'select'. The name 03210 resolution algorithm searches both the SELECT and GROUP BY clauses, and in 03211 case of a name conflict prefers GROUP BY column names over SELECT names. If 03212 both clauses contain different fields with the same names, a warning is 03213 issued that name of 'ref' is ambiguous. We extend ANSI SQL in that when no 03214 GROUP BY column is found, then a HAVING name is resolved as a possibly 03215 derived SELECT column. This extension is allowed only if the 03216 MODE_ONLY_FULL_GROUP_BY sql mode isn't enabled. 03217 03218 NOTES 03219 The resolution procedure is: 03220 - Search for a column or derived column named col_ref_i [in table T_j] 03221 in the SELECT clause of Q. 03222 - Search for a column named col_ref_i [in table T_j] 03223 in the GROUP BY clause of Q. 03224 - If found different columns with the same name in GROUP BY and SELECT 03225 - issue a warning and return the GROUP BY column, 03226 - otherwise 03227 - if the MODE_ONLY_FULL_GROUP_BY mode is enabled return error 03228 - else return the found SELECT column. 03229 03230 03231 RETURN 03232 NULL - there was an error, and the error was already reported 03233 not_found_item - the item was not resolved, no error was reported 03234 resolved item - if the item was resolved 03235 */ 03236 03237 static Item** 03238 resolve_ref_in_select_and_group(THD *thd, Item_ident *ref, SELECT_LEX *select) 03239 { 03240 Item **group_by_ref= NULL; 03241 Item **select_ref= NULL; 03242 ORDER *group_list= (ORDER*) select->group_list.first; 03243 bool ambiguous_fields= FALSE; 03244 uint counter; 03245 bool not_used; 03246 03247 /* 03248 Search for a column or derived column named as 'ref' in the SELECT 03249 clause of the current select. 03250 */ 03251 if (!(select_ref= find_item_in_list(ref, *(select->get_item_list()), 03252 &counter, REPORT_EXCEPT_NOT_FOUND, 03253 ¬_used))) 03254 return NULL; /* Some error occurred. */ 03255 03256 /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */ 03257 if (select->having_fix_field && !ref->with_sum_func && group_list) 03258 { 03259 group_by_ref= find_field_in_group_list(ref, group_list); 03260 03261 /* Check if the fields found in SELECT and GROUP BY are the same field. */ 03262 if (group_by_ref && (select_ref != not_found_item) && 03263 !((*group_by_ref)->eq(*select_ref, 0))) 03264 { 03265 ambiguous_fields= TRUE; 03266 push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR, 03267 ER(ER_NON_UNIQ_ERROR), ref->full_name(), 03268 current_thd->where); 03269 03270 } 03271 } 03272 03273 if (thd->variables.sql_mode & MODE_ONLY_FULL_GROUP_BY && 03274 select_ref != not_found_item && !group_by_ref) 03275 { 03276 /* 03277 Report the error if fields was found only in the SELECT item list and 03278 the strict mode is enabled. 03279 */ 03280 my_error(ER_NON_GROUPING_FIELD_USED, MYF(0), 03281 ref->name, "HAVING"); 03282 return NULL; 03283 } 03284 if (select_ref != not_found_item || group_by_ref) 03285 { 03286 if (select_ref != not_found_item && !ambiguous_fields) 03287 { 03288 DBUG_ASSERT(*select_ref != 0); 03289 if (!select->ref_pointer_array[counter]) 03290 { 03291 my_error(ER_ILLEGAL_REFERENCE, MYF(0), 03292 ref->name, "forward reference in item list"); 03293 return NULL; 03294 } 03295 DBUG_ASSERT((*select_ref)->fixed); 03296 return (select->ref_pointer_array + counter); 03297 } 03298 if (group_by_ref) 03299 return group_by_ref; 03300 DBUG_ASSERT(FALSE); 03301 return NULL; /* So there is no compiler warning. */ 03302 } 03303 03304 return (Item**) not_found_item; 03305 } 03306 03307 03308 /* 03309 Resolve the name of an outer select column reference. 03310 03311 SYNOPSIS 03312 Item_field::fix_outer_field() 03313 thd [in] current thread 03314 from_field [in/out] found field reference or (Field*)not_found_field 03315 reference [in/out] view column if this item was resolved to a view column 03316 03317 DESCRIPTION 03318 The method resolves the column reference represented by 'this' as a column 03319 present in outer selects that contain current select. 03320 03321 NOTES 03322 This is the inner loop of Item_field::fix_fields: 03323 03324 for each outer query Q_k beginning from the inner-most one 03325 { 03326 search for a column or derived column named col_ref_i 03327 [in table T_j] in the FROM clause of Q_k; 03328 03329 if such a column is not found 03330 Search for a column or derived column named col_ref_i 03331 [in table T_j] in the SELECT and GROUP clauses of Q_k. 03332 } 03333 03334 IMPLEMENTATION 03335 In prepared statements, because of cache, find_field_in_tables() 03336 can resolve fields even if they don't belong to current context. 03337 In this case this method only finds appropriate context and marks 03338 current select as dependent. The found reference of field should be 03339 provided in 'from_field'. 03340 03341 RETURN 03342 1 - column succefully resolved and fix_fields() should continue. 03343 0 - column fully fixed and fix_fields() should return FALSE 03344 -1 - error occured 03345 */ 03346 int 03347 Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference) 03348 { 03349 enum_parsing_place place= NO_MATTER; 03350 bool field_found= (*from_field != not_found_field); 03351 bool upward_lookup= FALSE; 03352 03353 /* 03354 If there are outer contexts (outer selects, but current select is 03355 not derived table or view) try to resolve this reference in the 03356 outer contexts. 03357 03358 We treat each subselect as a separate namespace, so that different 03359 subselects may contain columns with the same names. The subselects 03360 are searched starting from the innermost. 03361 */ 03362 Name_resolution_context *last_checked_context= context; 03363 Item **ref= (Item **) not_found_item; 03364 Name_resolution_context *outer_context= context->outer_context; 03365 for (; 03366 outer_context; 03367 outer_context= outer_context->outer_context) 03368 { 03369 SELECT_LEX *select= outer_context->select_lex; 03370 Item_subselect *prev_subselect_item= 03371 last_checked_context->select_lex->master_unit()->item; 03372 last_checked_context= outer_context; 03373 upward_lookup= TRUE; 03374 03375 place= prev_subselect_item->parsing_place; 03376 /* 03377 If outer_field is set, field was already found by first call 03378 to find_field_in_tables(). Only need to find appropriate context. 03379 */ 03380 if (field_found && outer_context->select_lex != 03381 cached_table->select_lex) 03382 continue; 03383 /* 03384 In case of a view, find_field_in_tables() writes the pointer to 03385 the found view field into '*reference', in other words, it 03386 substitutes this Item_field with the found expression. 03387 */ 03388 if (field_found || (*from_field= find_field_in_tables(thd, this, 03389 outer_context-> 03390 first_name_resolution_table, 03391 outer_context-> 03392 last_name_resolution_table, 03393 reference, 03394 IGNORE_EXCEPT_NON_UNIQUE, 03395 TRUE, TRUE)) != 03396 not_found_field) 03397 { 03398 if (*from_field) 03399 { 03400 if (*from_field != view_ref_found) 03401 { 03402 prev_subselect_item->used_tables_cache|= (*from_field)->table->map; 03403 prev_subselect_item->const_item_cache= 0; 03404 if (thd->lex->in_sum_func && 03405 thd->lex->in_sum_func->nest_level == 03406 thd->lex->current_select->nest_level) 03407 { 03408 Item::Type type= (*reference)->type(); 03409 set_if_bigger(thd->lex->in_sum_func->max_arg_level, 03410 select->nest_level); 03411 set_field(*from_field); 03412 fixed= 1; 03413 mark_as_dependent(thd, last_checked_context->select_lex, 03414 context->select_lex, this, 03415 ((type == REF_ITEM || type == FIELD_ITEM) ? 03416 (Item_ident*) (*reference) : 0)); 03417 return 0; 03418 } 03419 } 03420 else 03421 { 03422 Item::Type type= (*reference)->type(); 03423 prev_subselect_item->used_tables_cache|= 03424 (*reference)->used_tables(); 03425 prev_subselect_item->const_item_cache&= 03426 (*reference)->const_item(); 03427 mark_as_dependent(thd, last_checked_context->select_lex, 03428 context->select_lex, this, 03429 ((type == REF_ITEM || type == FIELD_ITEM) ? 03430 (Item_ident*) (*reference) : 03431 0)); 03432 /* 03433 A reference to a view field had been found and we 03434 substituted it instead of this Item (find_field_in_tables 03435 does it by assigning the new value to *reference), so now 03436 we can return from this function. 03437 */ 03438 return 0; 03439 } 03440 } 03441 break; 03442 } 03443 03444 /* Search in SELECT and GROUP lists of the outer select. */ 03445 if (outer_context->resolve_in_select_list) 03446 { 03447 if (!(ref= resolve_ref_in_select_and_group(thd, this, select))) 03448 return -1; /* Some error occurred (e.g. ambiguous names). */ 03449 if (ref != not_found_item) 03450 { 03451 DBUG_ASSERT(*ref && (*ref)->fixed); 03452 prev_subselect_item->used_tables_cache|= (*ref)->used_tables(); 03453 prev_subselect_item->const_item_cache&= (*ref)->const_item(); 03454 break; 03455 } 03456 } 03457 03458 /* 03459 Reference is not found in this select => this subquery depend on 03460 outer select (or we just trying to find wrong identifier, in this 03461 case it does not matter which used tables bits we set) 03462 */ 03463 prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT; 03464 prev_subselect_item->const_item_cache= 0; 03465 } 03466 03467 DBUG_ASSERT(ref != 0); 03468 if (!*from_field) 03469 return -1; 03470 if (ref == not_found_item && *from_field == not_found_field) 03471 { 03472 if (upward_lookup) 03473 { 03474 // We can't say exactly what absent table or field 03475 my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), thd->where); 03476 } 03477 else 03478 { 03479 /* Call find_field_in_tables only to report the error */ 03480 find_field_in_tables(thd, this, 03481 context->first_name_resolution_table, 03482 context->last_name_resolution_table, 03483 reference, REPORT_ALL_ERRORS, 03484 !any_privileges && 03485 TRUE, TRUE); 03486 } 03487 return -1; 03488 } 03489 else if (ref != not_found_item) 03490 { 03491 Item *save; 03492 Item_ref *rf; 03493 03494 /* Should have been checked in resolve_ref_in_select_and_group(). */ 03495 DBUG_ASSERT(*ref && (*ref)->fixed); 03496 /* 03497 Here, a subset of actions performed by Item_ref::set_properties 03498 is not enough. So we pass ptr to NULL into Item_[direct]_ref 03499 constructor, so no initialization is performed, and call 03500 fix_fields() below. 03501 */ 03502 save= *ref; 03503 *ref= NULL; // Don't call set_properties() 03504 rf= (place == IN_HAVING ? 03505 new Item_ref(context, ref, (char*) table_name, 03506 (char*) field_name) : 03507 new Item_direct_ref(context, ref, (char*) table_name, 03508 (char*) field_name)); 03509 *ref= save; 03510 if (!rf) 03511 return -1; 03512 thd->change_item_tree(reference, rf); 03513 /* 03514 rf is Item_ref => never substitute other items (in this case) 03515 during fix_fields() => we can use rf after fix_fields() 03516 */ 03517 DBUG_ASSERT(!rf->fixed); // Assured by Item_ref() 03518 if (rf->fix_fields(thd, reference) || rf->check_cols(1)) 03519 return -1; 03520 03521 mark_as_dependent(thd, last_checked_context->select_lex, 03522 context->select_lex, this, 03523 rf); 03524 return 0; 03525 } 03526 else 03527 { 03528 mark_as_dependent(thd, last_checked_context->select_lex, 03529 context->select_lex, 03530 this, this); 03531 if (last_checked_context->select_lex->having_fix_field) 03532 { 03533 Item_ref *rf; 03534 rf= new Item_ref(context, 03535 (cached_table->db[0] ? cached_table->db : 0), 03536 (char*) cached_table->alias, (char*) field_name); 03537 if (!rf) 03538 return -1; 03539 thd->change_item_tree(reference, rf); 03540 /* 03541 rf is Item_ref => never substitute other items (in this case) 03542 during fix_fields() => we can use rf after fix_fields() 03543 */ 03544 DBUG_ASSERT(!rf->fixed); // Assured by Item_ref() 03545 if (rf->fix_fields(thd, reference) || rf->check_cols(1)) 03546 return -1; 03547 return 0; 03548 } 03549 } 03550 return 1; 03551 } 03552 03553 03554 /* 03555 Resolve the name of a column reference. 03556 03557 SYNOPSIS 03558 Item_field::fix_fields() 03559 thd [in] current thread 03560 reference [in/out] view column if this item was resolved to a view column 03561 03562 DESCRIPTION 03563 The method resolves the column reference represented by 'this' as a column 03564 present in one of: FROM clause, SELECT clause, GROUP BY clause of a query 03565 Q, or in outer queries that contain Q. 03566 03567 NOTES 03568 The name resolution algorithm used is (where [T_j] is an optional table 03569 name that qualifies the column name): 03570 03571 resolve_column_reference([T_j].col_ref_i) 03572 { 03573 search for a column or derived column named col_ref_i 03574 [in table T_j] in the FROM clause of Q; 03575 03576 if such a column is NOT found AND // Lookup in outer queries. 03577 there are outer queries 03578 { 03579 for each outer query Q_k beginning from the inner-most one 03580 { 03581 search for a column or derived column named col_ref_i 03582 [in table T_j] in the FROM clause of Q_k; 03583 03584 if such a column is not found 03585 Search for a column or derived column named col_ref_i 03586 [in table T_j] in the SELECT and GROUP clauses of Q_k. 03587 } 03588 } 03589 } 03590 03591 Notice that compared to Item_ref::fix_fields, here we first search the FROM 03592 clause, and then we search the SELECT and GROUP BY clauses. 03593 03594 RETURN 03595 TRUE if error 03596 FALSE on success 03597 */ 03598 03599 bool Item_field::fix_fields(THD *thd, Item **reference) 03600 { 03601 DBUG_ASSERT(fixed == 0); 03602 if (!field) // If field is not checked 03603 { 03604 Field *from_field= (Field *)not_found_field; 03605 bool outer_fixed= false; 03606 /* 03607 In case of view, find_field_in_tables() write pointer to view field 03608 expression to 'reference', i.e. it substitute that expression instead 03609 of this Item_field 03610 */ 03611 if ((from_field= find_field_in_tables(thd, this, 03612 context->first_name_resolution_table, 03613 context->last_name_resolution_table, 03614 reference, 03615 IGNORE_EXCEPT_NON_UNIQUE, 03616 !any_privileges, 03617 TRUE)) == 03618 not_found_field) 03619 { 03620 int ret; 03621 /* Look up in current select's item_list to find aliased fields */ 03622 if (thd->lex->current_select->is_item_list_lookup) 03623 { 03624 uint counter; 03625 bool not_used; 03626 Item** res= find_item_in_list(this, thd->lex->current_select->item_list, 03627 &counter, REPORT_EXCEPT_NOT_FOUND, 03628 ¬_used); 03629 if (res != (Item **)not_found_item && 03630 (*res)->type() == Item::FIELD_ITEM) 03631 { 03632 set_field((*((Item_field**)res))->field); 03633 return 0; 03634 } 03635 } 03636 if ((ret= fix_outer_field(thd, &from_field, reference)) < 0) 03637 goto error; 03638 else if (!ret) 03639 return FALSE; 03640 outer_fixed= TRUE; 03641 } 03642 else if (!from_field) 03643 goto error; 03644 03645 /* 03646 if it is not expression from merged VIEW we will set this field. 03647 03648 We can leave expression substituted from view for next PS/SP rexecution 03649 (i.e. do not register this substitution for reverting on cleanup() 03650 (register_item_tree_changing())), because this subtree will be 03651 fix_field'ed during setup_tables()->setup_underlying() (i.e. before 03652 all other expressions of query, and references on tables which do 03653 not present in query will not make problems. 03654 03655 Also we suppose that view can't be changed during PS/SP life. 03656 */ 03657 if (from_field == view_ref_found) 03658 return FALSE; 03659 03660 if (!outer_fixed && cached_table && cached_table->select_lex && 03661 context->select_lex && 03662 cached_table->select_lex != context->select_lex) 03663 { 03664 int ret; 03665 if ((ret= fix_outer_field(thd, &from_field, reference)) < 0) 03666 goto error; 03667 if (!ret) 03668 return FALSE; 03669 } 03670 03671 set_field(from_field); 03672 if (thd->lex->in_sum_func && 03673 thd->lex->in_sum_func->nest_level == 03674 thd->lex->current_select->nest_level) 03675 set_if_bigger(thd->lex->in_sum_func->max_arg_level, 03676 thd->lex->current_select->nest_level); 03677 } 03678 else if (thd->mark_used_columns != MARK_COLUMNS_NONE) 03679 { 03680 TABLE *table= field->table; 03681 MY_BITMAP *current_bitmap, *other_bitmap; 03682 if (thd->mark_used_columns == MARK_COLUMNS_READ) 03683 { 03684 current_bitmap= table->read_set; 03685 other_bitmap= table->write_set; 03686 } 03687 else 03688 { 03689 current_bitmap= table->write_set; 03690 other_bitmap= table->read_set; 03691 } 03692 if (!bitmap_fast_test_and_set(current_bitmap, field->field_index)) 03693 { 03694 if (!bitmap_is_set(other_bitmap, field->field_index)) 03695 { 03696 /* First usage of column */ 03697 table->used_fields++; // Used to optimize loops 03698 table->used_keys.intersect(field->part_of_key); 03699 } 03700 } 03701 } 03702 #ifndef NO_EMBEDDED_ACCESS_CHECKS 03703 if (any_privileges) 03704 { 03705 char *db, *tab; 03706 if (cached_table->view) 03707 { 03708 db= cached_table->view_db.str; 03709 tab= cached_table->view_name.str; 03710 } 03711 else 03712 { 03713 db= cached_table->db; 03714 tab= cached_table->table_name; 03715 } 03716 if (!(have_privileges= (get_column_grant(thd, &field->table->grant, 03717 db, tab, field_name) & 03718 VIEW_ANY_ACL))) 03719 { 03720 my_error(ER_COLUMNACCESS_DENIED_ERROR, MYF(0), 03721 "ANY", thd->security_ctx->priv_user, 03722 thd->security_ctx->host_or_ip, field_name, tab); 03723 goto error; 03724 } 03725 } 03726 #endif 03727 fixed= 1; 03728 return FALSE; 03729 03730 error: 03731 context->process_error(thd); 03732 return TRUE; 03733 } 03734 03735 03736 Item *Item_field::safe_charset_converter(CHARSET_INFO *tocs) 03737 { 03738 no_const_subst= 1; 03739 return Item::safe_charset_converter(tocs); 03740 } 03741 03742 03743 void Item_field::cleanup() 03744 { 03745 DBUG_ENTER("Item_field::cleanup"); 03746 Item_ident::cleanup(); 03747 /* 03748 Even if this object was created by direct link to field in setup_wild() 03749 it will be linked correctly next time by name of field and table alias. 03750 I.e. we can drop 'field'. 03751 */ 03752 field= result_field= 0; 03753 DBUG_VOID_RETURN; 03754 } 03755 03756 /* 03757 Find a field among specified multiple equalities 03758 03759 SYNOPSIS 03760 find_item_equal() 03761 cond_equal reference to list of multiple equalities where 03762 the field (this object) is to be looked for 03763 03764 DESCRIPTION 03765 The function first searches the field among multiple equalities 03766 of the current level (in the cond_equal->current_level list). 03767 If it fails, it continues searching in upper levels accessed 03768 through a pointer cond_equal->upper_levels. 03769 The search terminates as soon as a multiple equality containing 03770 the field is found. 03771 03772 RETURN VALUES 03773 First Item_equal containing the field, if success 03774 0, otherwise 03775 */ 03776 Item_equal *Item_field::find_item_equal(COND_EQUAL *cond_equal) 03777 { 03778 Item_equal *item= 0; 03779 while (cond_equal) 03780 { 03781 List_iterator_fast<Item_equal> li(cond_equal->current_level); 03782 while ((item= li++)) 03783 { 03784 if (item->contains(field)) 03785 return item; 03786 } 03787 /* 03788 The field is not found in any of the multiple equalities 03789 of the current level. Look for it in upper levels 03790 */ 03791 cond_equal= cond_equal->upper_levels; 03792 } 03793 return 0; 03794 } 03795 03796 03797 /* 03798 Set a pointer to the multiple equality the field reference belongs to 03799 (if any) 03800 03801 SYNOPSIS 03802 equal_fields_propagator() 03803 arg - reference to list of multiple equalities where 03804 the field (this object) is to be looked for 03805 03806 DESCRIPTION 03807 The function looks for a multiple equality containing the field item 03808 among those referenced by arg. 03809 In the case such equality exists the function does the following. 03810 If the found multiple equality contains a constant, then the field 03811 reference is substituted for this constant, otherwise it sets a pointer 03812 to the multiple equality in the field item. 03813 03814 NOTES 03815 This function is supposed to be called as a callback parameter in calls 03816 of the transform method. 03817 03818 RETURN VALUES 03819 pointer to the replacing constant item, if the field item was substituted 03820 pointer to the field item, otherwise. 03821 */ 03822 03823 Item *Item_field::equal_fields_propagator(byte *arg) 03824 { 03825 if (no_const_subst) 03826 return this; 03827 item_equal= find_item_equal((COND_EQUAL *) arg); 03828 Item *item= 0; 03829 if (item_equal) 03830 item= item_equal->get_const(); 03831 if (!item) 03832 item= this; 03833 return item; 03834 } 03835 03836 03837 /* 03838 Mark the item to not be part of substitution if it's not a binary item 03839 See comments in Arg_comparator::set_compare_func() for details 03840 */ 03841 03842 Item *Item_field::set_no_const_sub(byte *arg) 03843 { 03844 if (field->charset() != &my_charset_bin) 03845 no_const_subst=1; 03846 return this; 03847 } 03848 03849 03850 /* 03851 Replace an Item_field for an equal Item_field that evaluated earlier 03852 (if any) 03853 03854 SYNOPSIS 03855 replace_equal_field_() 03856 arg - a dummy parameter, is not used here 03857 03858 DESCRIPTION 03859 The function returns a pointer to an item that is taken from 03860 the very beginning of the item_equal list which the Item_field 03861 object refers to (belongs to). 03862 If the Item_field object does not refer any Item_equal object 03863 'this' is returned 03864 03865 NOTES 03866 This function is supposed to be called as a callback parameter in calls 03867 of the thransformer method. 03868 03869 RETURN VALUES 03870 pointer to a replacement Item_field if there is a better equal item; 03871 this - otherwise. 03872 */ 03873 03874 Item *Item_field::replace_equal_field(byte *arg) 03875 { 03876 if (item_equal) 03877 { 03878 Item_field *subst= item_equal->get_first(); 03879 if (subst && !field->eq(subst->field)) 03880 return subst; 03881 } 03882 return this; 03883 } 03884 03885 03886 void Item::init_make_field(Send_field *tmp_field, 03887 enum enum_field_types field_type) 03888 { 03889 char *empty_name= (char*) ""; 03890 tmp_field->db_name= empty_name; 03891 tmp_field->org_table_name= empty_name; 03892 tmp_field->org_col_name= empty_name; 03893 tmp_field->table_name= empty_name; 03894 tmp_field->col_name= name; 03895 tmp_field->charsetnr= collation.collation->number; 03896 tmp_field->flags= (maybe_null ? 0 : NOT_NULL_FLAG) | 03897 (my_binary_compare(collation.collation) ? 03898 BINARY_FLAG : 0); 03899 tmp_field->type=field_type; 03900 tmp_field->length=max_length; 03901 tmp_field->decimals=decimals; 03902 if (unsigned_flag) 03903 tmp_field->flags |= UNSIGNED_FLAG; 03904 } 03905 03906 void Item::make_field(Send_field *tmp_field) 03907 { 03908 init_make_field(tmp_field, field_type()); 03909 } 03910 03911 03912 void Item_empty_string::make_field(Send_field *tmp_field) 03913 { 03914 enum_field_types type= FIELD_TYPE_VAR_STRING; 03915 if (max_length >= 16777216) 03916 type= FIELD_TYPE_LONG_BLOB; 03917 else if (max_length >= 65536) 03918 type= FIELD_TYPE_MEDIUM_BLOB; 03919 init_make_field(tmp_field, type); 03920 } 03921 03922 03923 enum_field_types Item::field_type() const 03924 { 03925 switch (result_type()) { 03926 case STRING_RESULT: return MYSQL_TYPE_VARCHAR; 03927 case INT_RESULT: return FIELD_TYPE_LONGLONG; 03928 case DECIMAL_RESULT: return FIELD_TYPE_NEWDECIMAL; 03929 case REAL_RESULT: return FIELD_TYPE_DOUBLE; 03930 case ROW_RESULT: 03931 default: 03932 DBUG_ASSERT(0); 03933 return MYSQL_TYPE_VARCHAR; 03934 } 03935 } 03936 03937 03938 /* 03939 Create a field to hold a string value from an item 03940 03941 SYNOPSIS 03942 make_string_field() 03943 table Table for which the field is created 03944 03945 IMPLEMENTATION 03946 If max_length > CONVERT_IF_BIGGER_TO_BLOB create a blob 03947 If max_length > 0 create a varchar 03948 If max_length == 0 create a CHAR(0) 03949 */ 03950 03951 03952 Field *Item::make_string_field(TABLE *table) 03953 { 03954 Field *field; 03955 DBUG_ASSERT(collation.collation); 03956 if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB) 03957 field= new Field_blob(max_length, maybe_null, name, 03958 collation.collation); 03959 /* Item_type_holder holds the exact type, do not change it */ 03960 else if (max_length > 0 && 03961 (type() != Item::TYPE_HOLDER || field_type() != MYSQL_TYPE_STRING)) 03962 field= new Field_varstring(max_length, maybe_null, name, table->s, 03963 collation.collation); 03964 else 03965 field= new Field_string(max_length, maybe_null, name, 03966 collation.collation); 03967 if (field) 03968 field->init(table); 03969 return field; 03970 } 03971 03972 03973 /* 03974 Create a field based on field_type of argument 03975 03976 For now, this is only used to create a field for 03977 IFNULL(x,something) and time functions 03978 03979 RETURN 03980 0 error 03981 # Created field 03982 */ 03983 03984 Field *Item::tmp_table_field_from_field_type(TABLE *table, bool fixed_length) 03985 { 03986 /* 03987 The field functions defines a field to be not null if null_ptr is not 0 03988 */ 03989 uchar *null_ptr= maybe_null ? (uchar*) "" : 0; 03990 Field *field; 03991 03992 switch (field_type()) { 03993 case MYSQL_TYPE_DECIMAL: 03994 case MYSQL_TYPE_NEWDECIMAL: 03995 field= new Field_new_decimal((char*) 0, max_length, null_ptr, 0, 03996 Field::NONE, name, decimals, 0, 03997 unsigned_flag); 03998 break; 03999 case MYSQL_TYPE_TINY: 04000 field= new Field_tiny((char*) 0, max_length, null_ptr, 0, Field::NONE, 04001 name, 0, unsigned_flag); 04002 break; 04003 case MYSQL_TYPE_SHORT: 04004 field= new Field_short((char*) 0, max_length, null_ptr, 0, Field::NONE, 04005 name, 0, unsigned_flag); 04006 break; 04007 case MYSQL_TYPE_LONG: 04008 field= new Field_long((char*) 0, max_length, null_ptr, 0, Field::NONE, 04009 name, 0, unsigned_flag); 04010 break; 04011 #ifdef HAVE_LONG_LONG 04012 case MYSQL_TYPE_LONGLONG: 04013 field= new Field_longlong((char*) 0, max_length, null_ptr, 0, Field::NONE, 04014 name, 0, unsigned_flag); 04015 break; 04016 #endif 04017 case MYSQL_TYPE_FLOAT: 04018 field= new Field_float((char*) 0, max_length, null_ptr, 0, Field::NONE, 04019 name, decimals, 0, unsigned_flag); 04020 break; 04021 case MYSQL_TYPE_DOUBLE: 04022 field= new Field_double((char*) 0, max_length, null_ptr, 0, Field::NONE, 04023 name, decimals, 0, unsigned_flag); 04024 break; 04025 case MYSQL_TYPE_NULL: 04026 field= new Field_null((char*) 0, max_length, Field::NONE, 04027 name, &my_charset_bin); 04028 break; 04029 case MYSQL_TYPE_INT24: 04030 field= new Field_medium((char*) 0, max_length, null_ptr, 0, Field::NONE, 04031 name, 0, unsigned_flag); 04032 break; 04033 case MYSQL_TYPE_NEWDATE: 04034 case MYSQL_TYPE_DATE: 04035 field= new Field_date(maybe_null, name, &my_charset_bin); 04036 break; 04037 case MYSQL_TYPE_TIME: 04038 field= new Field_time(maybe_null, name, &my_charset_bin); 04039 break; 04040 case MYSQL_TYPE_TIMESTAMP: 04041 field= new Field_timestamp(maybe_null, name, &my_charset_bin); 04042 break; 04043 case MYSQL_TYPE_DATETIME: 04044 field= new Field_datetime(maybe_null, name, &my_charset_bin); 04045 break; 04046 case MYSQL_TYPE_YEAR: 04047 field= new Field_year((char*) 0, max_length, null_ptr, 0, Field::NONE, 04048 name); 04049 break; 04050 case MYSQL_TYPE_BIT: 04051 field= new Field_bit_as_char(NULL, max_length, null_ptr, 0, 04052 Field::NONE, name); 04053 break; 04054 default: 04055 /* This case should never be chosen */ 04056 DBUG_ASSERT(0); 04057 /* If something goes awfully wrong, it's better to get a string than die */ 04058 case MYSQL_TYPE_STRING: 04059 if (fixed_length && max_length < CONVERT_IF_BIGGER_TO_BLOB) 04060 { 04061 field= new Field_string(max_length, maybe_null, name, 04062 collation.collation); 04063 break; 04064 } 04065 /* Fall through to make_string_field() */ 04066 case MYSQL_TYPE_ENUM: 04067 case MYSQL_TYPE_SET: 04068 case MYSQL_TYPE_VAR_STRING: 04069 case MYSQL_TYPE_VARCHAR: 04070 return make_string_field(table); 04071 case MYSQL_TYPE_TINY_BLOB: 04072 case MYSQL_TYPE_MEDIUM_BLOB: 04073 case MYSQL_TYPE_LONG_BLOB: 04074 case MYSQL_TYPE_BLOB: 04075 case MYSQL_TYPE_GEOMETRY: 04076 if (this->type() == Item::TYPE_HOLDER) 04077 field= new Field_blob(max_length, maybe_null, name, collation.collation, 04078 1); 04079 else 04080 field= new Field_blob(max_length, maybe_null, name, collation.collation); 04081 break; // Blob handled outside of case 04082 } 04083 if (field) 04084 field->init(table); 04085 return field; 04086 } 04087 04088 04089 /* ARGSUSED */ 04090 void Item_field::make_field(Send_field *tmp_field) 04091 { 04092 field->make_field(tmp_field); 04093 DBUG_ASSERT(tmp_field->table_name != 0); 04094 if (name) 04095 tmp_field->col_name=name; // Use user supplied name 04096 } 04097 04098 04099 /* 04100 Set a field:s value from a item 04101 */ 04102 04103 void Item_field::save_org_in_field(Field *to) 04104 { 04105 if (field->is_null()) 04106 { 04107 null_value=1; 04108 set_field_to_null_with_conversions(to, 1); 04109 } 04110 else 04111 { 04112 to->set_notnull(); 04113 field_conv(to,field); 04114 null_value=0; 04115 } 04116 } 04117 04118 int Item_field::save_in_field(Field *to, bool no_conversions) 04119 { 04120 if (result_field->is_null()) 04121 { 04122 null_value=1; 04123 return set_field_to_null_with_conversions(to, no_conversions); 04124 } 04125 else 04126 { 04127 to->set_notnull(); 04128 field_conv(to,result_field); 04129 null_value=0; 04130 } 04131 return 0; 04132 } 04133 04134 04135 /* 04136 Store null in field 04137 04138 SYNOPSIS 04139 save_in_field() 04140 field Field where we want to store NULL 04141 04142 DESCRIPTION 04143 This is used on INSERT. 04144 Allow NULL to be inserted in timestamp and auto_increment values 04145 04146 RETURN VALUES 04147 0 ok 04148 1 Field doesn't support NULL values and can't handle 'field = NULL' 04149 */ 04150 04151 int Item_null::save_in_field(Field *field, bool no_conversions) 04152 { 04153 return set_field_to_null_with_conversions(field, no_conversions); 04154 } 04155 04156 04157 /* 04158 Store null in field 04159 04160 SYNOPSIS 04161 save_safe_in_field() 04162 field Field where we want to store NULL 04163 04164 RETURN VALUES 04165 0 OK 04166 1 Field doesn't support NULL values 04167 */ 04168 04169 int Item_null::save_safe_in_field(Field *field) 04170 { 04171 return set_field_to_null(field); 04172 } 04173 04174 04175 int Item::save_in_field(Field *field, bool no_conversions) 04176 { 04177 int error; 04178 if (result_type() == STRING_RESULT || 04179 result_type() == REAL_RESULT && 04180 field->result_type() == STRING_RESULT) 04181 { 04182 String *result; 04183 CHARSET_INFO *cs= collation.collation; 04184 char buff[MAX_FIELD_WIDTH]; // Alloc buffer for small columns 04185 str_value.set_quick(buff, sizeof(buff), cs); 04186 result=val_str(&str_value); 04187 if (null_value) 04188 { 04189 str_value.set_quick(0, 0, cs); 04190 return set_field_to_null_with_conversions(field, no_conversions); 04191 } 04192 04193 /* NOTE: If null_value == FALSE, "result" must be not NULL. */ 04194 04195 field->set_notnull(); 04196 error=field->store(result->ptr(),result->length(),cs); 04197 str_value.set_quick(0, 0, cs); 04198 } 04199 else if (result_type() == REAL_RESULT) 04200 { 04201 double nr= val_real(); 04202 if (null_value) 04203 return set_field_to_null(field); 04204 field->set_notnull(); 04205 error=field->store(nr); 04206 } 04207 else if (result_type() == DECIMAL_RESULT) 04208 { 04209 my_decimal decimal_value; 04210 my_decimal *value= val_decimal(&decimal_value); 04211 if (null_value) 04212 return set_field_to_null(field); 04213 field->set_notnull(); 04214 error=field->store_decimal(value); 04215 } 04216 else 04217 { 04218 longlong nr=val_int(); 04219 if (null_value) 04220 return set_field_to_null_with_conversions(field, no_conversions); 04221 field->set_notnull(); 04222 error=field->store(nr, unsigned_flag); 04223 } 04224 return error; 04225 } 04226 04227 04228 int Item_string::save_in_field(Field *field, bool no_conversions) 04229 { 04230 String *result; 04231 result=val_str(&str_value); 04232 if (null_value) 04233 return set_field_to_null(field); 04234 field->set_notnull(); 04235 return field->store(result->ptr(),result->length(),collation.collation); 04236 } 04237 04238 04239 int Item_uint::save_in_field(Field *field, bool no_conversions) 04240 { 04241 /* Item_int::save_in_field handles both signed and unsigned. */ 04242 return Item_int::save_in_field(field, no_conversions); 04243 } 04244 04245 04246 int Item_int::save_in_field(Field *field, bool no_conversions) 04247 { 04248 longlong nr=val_int(); 04249 if (null_value) 04250 return set_field_to_null(field); 04251 field->set_notnull(); 04252 return field->store(nr, unsigned_flag); 04253 } 04254 04255 04256 int Item_decimal::save_in_field(Field *field, bool no_conversions) 04257 { 04258 field->set_notnull(); 04259 return field->store_decimal(&decimal_value); 04260 } 04261 04262 04263 bool Item_int::eq(const Item *arg, bool binary_cmp) const 04264 { 04265 /* No need to check for null value as basic constant can't be NULL */ 04266 if (arg->basic_const_item() && arg->type() == type()) 04267 { 04268 /* 04269 We need to cast off const to call val_int(). This should be OK for 04270 a basic constant. 04271 */ 04272 Item *item= (Item*) arg; 04273 return item->val_int() == value && item->unsigned_flag == unsigned_flag; 04274 } 04275 return FALSE; 04276 } 04277 04278 04279 Item *Item_int_with_ref::new_item() 04280 { 04281 DBUG_ASSERT(ref->const_item()); 04282 /* 04283 We need to evaluate the constant to make sure it works with 04284 parameter markers. 04285 */ 04286 return (ref->unsigned_flag ? 04287 new Item_uint(ref->name, ref->val_int(), ref->max_length) : 04288 new Item_int(ref->name, ref->val_int(), ref->max_length)); 04289 } 04290 04291 04292 Item_num *Item_uint::neg() 04293 { 04294 Item_decimal *item= new Item_decimal(value, 1); 04295 return item->neg(); 04296 } 04297 04298 04299 static uint nr_of_decimals(const char *str, const char *end) 04300 { 04301 const char *decimal_point; 04302 04303 /* Find position for '.' */ 04304 for (;;) 04305 { 04306 if (str == end) 04307 return 0; 04308 if (*str == 'e' || *str == 'E') 04309 return NOT_FIXED_DEC; 04310 if (*str++ == '.') 04311 break; 04312 } 04313 decimal_point= str; 04314 for (; my_isdigit(system_charset_info, *str) ; str++) 04315 ; 04316 if (*str == 'e' || *str == 'E') 04317 return NOT_FIXED_DEC; 04318 return (uint) (str - decimal_point); 04319 } 04320 04321 04322 /* 04323 This function is only called during parsing. We will signal an error if 04324 value is not a true double value (overflow) 04325 */ 04326 04327 Item_float::Item_float(const char *str_arg, uint length) 04328 { 04329 int error; 04330 char *end_not_used; 04331 value= my_strntod(&my_charset_bin, (char*) str_arg, length, &end_not_used, 04332 &error); 04333 if (error) 04334 { 04335 /* 04336 Note that we depend on that str_arg is null terminated, which is true 04337 when we are in the parser 04338 */ 04339 DBUG_ASSERT(str_arg[length] == 0); 04340 my_error(ER_ILLEGAL_VALUE_FOR_TYPE, MYF(0), "double", (char*) str_arg); 04341 } 04342 presentation= name=(char*) str_arg; 04343 decimals=(uint8) nr_of_decimals(str_arg, str_arg+length); 04344 max_length=length; 04345 fixed= 1; 04346 } 04347 04348 04349 int Item_float::save_in_field(Field *field, bool no_conversions) 04350 { 04351 double nr= val_real(); 04352 if (null_value) 04353 return set_field_to_null(field); 04354 field->set_notnull(); 04355 return field->store(nr); 04356 } 04357 04358 04359 void Item_float::print(String *str) 04360 { 04361 if (presentation) 04362 { 04363 str->append(presentation); 04364 return; 04365 } 04366 char buffer[20]; 04367 String num(buffer, sizeof(buffer), &my_charset_bin); 04368 num.set_real(value, decimals, &my_charset_bin); 04369 str->append(num); 04370 } 04371 04372 04373 /* 04374 hex item 04375 In string context this is a binary string. 04376 In number context this is a longlong value. 04377 */ 04378 04379 bool Item_float::eq(const Item *arg, bool binary_cmp) const 04380 { 04381 if (arg->basic_const_item() && arg->type() == type()) 04382 { 04383 /* 04384 We need to cast off const to call val_int(). This should be OK for 04385 a basic constant. 04386 */ 04387 Item *item= (Item*) arg; 04388 return item->val_real() == value; 04389 } 04390 return FALSE; 04391 } 04392 04393 04394 inline uint char_val(char X) 04395 { 04396 return (uint) (X >= '0' && X <= '9' ? X-'0' : 04397 X >= 'A' && X <= 'Z' ? X-'A'+10 : 04398 X-'a'+10); 04399 } 04400 04401 04402 Item_hex_string::Item_hex_string(const char *str, uint str_length) 04403 { 04404 name=(char*) str-2; // Lex makes this start with 0x 04405 max_length=(str_length+1)/2; 04406 char *ptr=(char*) sql_alloc(max_length+1); 04407 if (!ptr) 04408 return; 04409 str_value.set(ptr,max_length,&my_charset_bin); 04410 char *end=ptr+max_length; 04411 if (max_length*2 != str_length) 04412 *ptr++=char_val(*str++); // Not even, assume 0 prefix 04413 while (ptr != end) 04414 { 04415 *ptr++= (char) (char_val(str[0])*16+char_val(str[1])); 04416 str+=2; 04417 } 04418 *ptr=0; // Keep purify happy 04419 collation.set(&my_charset_bin, DERIVATION_COERCIBLE); 04420 fixed= 1; 04421 unsigned_flag= 1; 04422 } 04423 04424 longlong Item_hex_string::val_int() 04425 { 04426 // following assert is redundant, because fixed=1 assigned in constructor 04427 DBUG_ASSERT(fixed == 1); 04428 char *end=(char*) str_value.ptr()+str_value.length(), 04429 *ptr=end-min(str_value.length(),sizeof(longlong)); 04430 04431 ulonglong value=0; 04432 for (; ptr != end ; ptr++) 04433 value=(value << 8)+ (ulonglong) (uchar) *ptr; 04434 return (longlong) value; 04435 } 04436 04437 04438 my_decimal *Item_hex_string::val_decimal(my_decimal *decimal_value) 04439 { 04440 // following assert is redundant, because fixed=1 assigned in constructor 04441 DBUG_ASSERT(fixed == 1); 04442 ulonglong value= (ulonglong)val_int(); 04443 int2my_decimal(E_DEC_FATAL_ERROR, value, TRUE, decimal_value); 04444 return (decimal_value); 04445 } 04446 04447 04448 int Item_hex_string::save_in_field(Field *field, bool no_conversions) 04449 { 04450 int error; 04451 field->set_notnull(); 04452 if (field->result_type() == STRING_RESULT) 04453 { 04454 error=field->store(str_value.ptr(),str_value.length(),collation.collation); 04455 } 04456 else 04457 { 04458 longlong nr=val_int(); 04459 error=field->store(nr, TRUE); // Assume hex numbers are unsigned 04460 } 04461 return error; 04462 } 04463 04464 04465 bool Item_hex_string::eq(const Item *arg, bool binary_cmp) const 04466 { 04467 if (arg->basic_const_item() && arg->type() == type()) 04468 { 04469 if (binary_cmp) 04470 return !stringcmp(&str_value, &arg->str_value); 04471 return !sortcmp(&str_value, &arg->str_value, collation.collation); 04472 } 04473 return FALSE; 04474 } 04475 04476 04477 Item *Item_hex_string::safe_charset_converter(CHARSET_INFO *tocs) 04478 { 04479 Item_string *conv; 04480 String tmp, *str= val_str(&tmp); 04481 04482 if (!(conv= new Item_string(str->ptr(), str->length(), tocs))) 04483 return NULL; 04484 conv->str_value.copy(); 04485 conv->str_value.mark_as_const(); 04486 return conv; 04487 } 04488 04489 04490 /* 04491 bin item. 04492 In string context this is a binary string. 04493 In number context this is a longlong value. 04494 */ 04495 04496 Item_bin_string::Item_bin_string(const char *str, uint str_length) 04497 { 04498 const char *end= str + str_length - 1; 04499 uchar bits= 0; 04500 uint power= 1; 04501 04502 name= (char*) str - 2; 04503 max_length= (str_length + 7) >> 3; 04504 char *ptr= (char*) sql_alloc(max_length + 1); 04505 if (!ptr) 04506 return; 04507 str_value.set(ptr, max_length, &my_charset_bin); 04508 ptr+= max_length - 1; 04509 ptr[1]= 0; // Set end null for string 04510 for (; end >= str; end--) 04511 { 04512 if (power == 256) 04513 { 04514 power= 1; 04515 *ptr--= bits; 04516 bits= 0; 04517 } 04518 if (*end == '1') 04519 bits|= power; 04520 power<<= 1; 04521 } 04522 *ptr= (char) bits; 04523 collation.set(&my_charset_bin, DERIVATION_COERCIBLE); 04524 fixed= 1; 04525 } 04526 04527 04528 /* 04529 Pack data in buffer for sending 04530 */ 04531 04532 bool Item_null::send(Protocol *protocol, String *packet) 04533 { 04534 return protocol->store_null(); 04535 } 04536 04537 /* 04538 This is only called from items that is not of type item_field 04539 */ 04540 04541 bool Item::send(Protocol *protocol, String *buffer) 04542 { 04543 bool result; 04544 enum_field_types type; 04545 LINT_INIT(result); // Will be set if null_value == 0 04546 04547 switch ((type=field_type())) { 04548 default: 04549 case MYSQL_TYPE_NULL: 04550 case MYSQL_TYPE_DECIMAL: 04551 case MYSQL_TYPE_ENUM: 04552 case MYSQL_TYPE_SET: 04553 case MYSQL_TYPE_TINY_BLOB: 04554 case MYSQL_TYPE_MEDIUM_BLOB: 04555 case MYSQL_TYPE_LONG_BLOB: 04556 case MYSQL_TYPE_BLOB: 04557 case MYSQL_TYPE_GEOMETRY: 04558 case MYSQL_TYPE_STRING: 04559 case MYSQL_TYPE_VAR_STRING: 04560 case MYSQL_TYPE_VARCHAR: 04561 case MYSQL_TYPE_BIT: 04562 case MYSQL_TYPE_NEWDECIMAL: 04563 { 04564 String *res; 04565 if ((res=val_str(buffer))) 04566 result= protocol->store(res->ptr(),res->length(),res->charset()); 04567 break; 04568 } 04569 case MYSQL_TYPE_TINY: 04570 { 04571 longlong nr; 04572 nr= val_int(); 04573 if (!null_value) 04574 result= protocol->store_tiny(nr); 04575 break; 04576 } 04577 case MYSQL_TYPE_SHORT: 04578 case MYSQL_TYPE_YEAR: 04579 { 04580 longlong nr; 04581 nr= val_int(); 04582 if (!null_value) 04583 result= protocol->store_short(nr); 04584 break; 04585 } 04586 case MYSQL_TYPE_INT24: 04587 case MYSQL_TYPE_LONG: 04588 { 04589 longlong nr; 04590 nr= val_int(); 04591 if (!null_value) 04592 result= protocol->store_long(nr); 04593 break; 04594 } 04595 case MYSQL_TYPE_LONGLONG: 04596 { 04597 longlong nr; 04598 nr= val_int(); 04599 if (!null_value) 04600 result= protocol->store_longlong(nr, unsigned_flag); 04601 break; 04602 } 04603 case MYSQL_TYPE_FLOAT: 04604 { 04605 float nr; 04606 nr= (float) val_real(); 04607 if (!null_value) 04608 result= protocol->store(nr, decimals, buffer); 04609 break; 04610 } 04611 case MYSQL_TYPE_DOUBLE: 04612 { 04613 double nr= val_real(); 04614 if (!null_value) 04615 result= protocol->store(nr, decimals, buffer); 04616 break; 04617 } 04618 case MYSQL_TYPE_DATETIME: 04619 case MYSQL_TYPE_DATE: 04620 case MYSQL_TYPE_TIMESTAMP: 04621 { 04622 TIME tm; 04623 get_date(&tm, TIME_FUZZY_DATE); 04624 if (!null_value) 04625 { 04626 if (type == MYSQL_TYPE_DATE) 04627 return protocol->store_date(&tm); 04628 else 04629 result= protocol->store(&tm); 04630 } 04631 break; 04632 } 04633 case MYSQL_TYPE_TIME: 04634 { 04635 TIME tm; 04636 get_time(&tm); 04637 if (!null_value) 04638 result= protocol->store_time(&tm); 04639 break; 04640 } 04641 } 04642 if (null_value) 04643 result= protocol->store_null(); 04644 return result; 04645 } 04646 04647 04648 bool Item_field::send(Protocol *protocol, String *buffer) 04649 { 04650 return protocol->store(result_field); 04651 } 04652 04653 04654 Item_ref::Item_ref(Name_resolution_context *context_arg, 04655 Item **item, const char *table_name_arg, 04656 const char *field_name_arg) 04657 :Item_ident(context_arg, NullS, table_name_arg, field_name_arg), 04658 result_field(0), ref(item) 04659 { 04660 /* 04661 This constructor used to create some internals references over fixed items 04662 */ 04663 DBUG_ASSERT(ref != 0); 04664 if (*ref && (*ref)->fixed) 04665 set_properties(); 04666 } 04667 04668 04669 /* 04670 Resolve the name of a reference to a column reference. 04671 04672 SYNOPSIS 04673 Item_ref::fix_fields() 04674 thd [in] current thread 04675 reference [in/out] view column if this item was resolved to a view column 04676 04677 DESCRIPTION 04678 The method resolves the column reference represented by 'this' as a column 04679 present in one of: GROUP BY clause, SELECT clause, outer queries. It is 04680 used typically for columns in the HAVING clause which are not under 04681 aggregate functions. 04682 04683 NOTES 04684 The name resolution algorithm used is (where [T_j] is an optional table 04685 name that qualifies the column name): 04686 04687 resolve_extended([T_j].col_ref_i) 04688 { 04689 Search for a column or derived column named col_ref_i [in table T_j] 04690 in the SELECT and GROUP clauses of Q. 04691 04692 if such a column is NOT found AND // Lookup in outer queries. 04693 there are outer queries 04694 { 04695 for each outer query Q_k beginning from the inner-most one 04696 { 04697 Search for a column or derived column named col_ref_i 04698 [in table T_j] in the SELECT and GROUP clauses of Q_k. 04699 04700 if such a column is not found AND 04701 - Q_k is not a group query AND 04702 - Q_k is not inside an aggregate function 04703 OR 04704 - Q_(k-1) is not in a HAVING or SELECT clause of Q_k 04705 { 04706 search for a column or derived column named col_ref_i 04707 [in table T_j] in the FROM clause of Q_k; 04708 } 04709 } 04710 } 04711 } 04712 04713 This procedure treats GROUP BY and SELECT clauses as one namespace for 04714 column references in HAVING. Notice that compared to 04715 Item_field::fix_fields, here we first search the SELECT and GROUP BY 04716 clauses, and then we search the FROM clause. 04717 04718 POSTCONDITION 04719 Item_ref::ref is 0 or points to a valid item 04720 04721 RETURN 04722 TRUE if error 04723 FALSE on success 04724 */ 04725 04726 bool Item_ref::fix_fields(THD *thd, Item **reference) 04727 { 04728 enum_parsing_place place= NO_MATTER; 04729 DBUG_ASSERT(fixed == 0); 04730 SELECT_LEX *current_sel= thd->lex->current_select; 04731 04732 if (!ref || ref == not_found_item) 04733 { 04734 if (!(ref= resolve_ref_in_select_and_group(thd, this, 04735 context->select_lex))) 04736 goto error; /* Some error occurred (e.g. ambiguous names). */ 04737 04738 if (ref == not_found_item) /* This reference was not resolved. */ 04739 { 04740 Name_resolution_context *last_checked_context= context; 04741 Name_resolution_context *outer_context= context->outer_context; 04742 Field *from_field; 04743 ref= 0; 04744 04745 if (!outer_context) 04746 { 04747 /* The current reference cannot be resolved in this query. */ 04748 my_error(ER_BAD_FIELD_ERROR,MYF(0), 04749 this->full_name(), current_thd->where); 04750 goto error; 04751 } 04752 04753 /* 04754 If there is an outer context (select), and it is not a derived table 04755 (which do not support the use of outer fields for now), try to 04756 resolve this reference in the outer select(s). 04757 04758 We treat each subselect as a separate namespace, so that different 04759 subselects may contain columns with the same names. The subselects are 04760 searched starting from the innermost. 04761 */ 04762 from_field= (Field*) not_found_field; 04763 04764 do 04765 { 04766 SELECT_LEX *select= outer_context->select_lex; 04767 Item_subselect *prev_subselect_item= 04768 last_checked_context->select_lex->master_unit()->item; 04769 last_checked_context= outer_context; 04770 04771 /* Search in the SELECT and GROUP lists of the outer select. */ 04772 if (outer_context->resolve_in_select_list) 04773 { 04774 if (!(ref= resolve_ref_in_select_and_group(thd, this, select))) 04775 goto error; /* Some error occurred (e.g. ambiguous names). */ 04776 if (ref != not_found_item) 04777 { 04778 DBUG_ASSERT(*ref && (*ref)->fixed); 04779 prev_subselect_item->used_tables_cache|= (*ref)->used_tables(); 04780 prev_subselect_item->const_item_cache&= (*ref)->const_item(); 04781 break; 04782 } 04783 /* 04784 Set ref to 0 to ensure that we get an error in case we replaced 04785 this item with another item and still use this item in some 04786 other place of the parse tree. 04787 */ 04788 ref= 0; 04789 } 04790 04791 place= prev_subselect_item->parsing_place; 04792 /* 04793 Check table fields only if the subquery is used somewhere out of 04794 HAVING or the outer SELECT does not use grouping (i.e. tables are 04795 accessible). 04796 TODO: 04797 Here we could first find the field anyway, and then test this 04798 condition, so that we can give a better error message - 04799 ER_WRONG_FIELD_WITH_GROUP, instead of the less informative 04800 ER_BAD_FIELD_ERROR which we produce now. 04801 */ 04802 if ((place != IN_HAVING || 04803 (!select->with_sum_func && 04804 select->group_list.elements == 0))) 04805 { 04806 /* 04807 In case of view, find_field_in_tables() write pointer to view 04808 field expression to 'reference', i.e. it substitute that 04809 expression instead of this Item_ref 04810 */ 04811 from_field= find_field_in_tables(thd, this, 04812 outer_context-> 04813 first_name_resolution_table, 04814 outer_context-> 04815 last_name_resolution_table, 04816 reference, 04817 IGNORE_EXCEPT_NON_UNIQUE, 04818 TRUE, TRUE); 04819 if (! from_field) 04820 goto error; 04821 if (from_field == view_ref_found) 04822 { 04823 Item::Type type= (*reference)->type(); 04824 prev_subselect_item->used_tables_cache|= 04825 (*reference)->used_tables(); 04826 prev_subselect_item->const_item_cache&= 04827 (*reference)->const_item(); 04828 DBUG_ASSERT((*reference)->type() == REF_ITEM); 04829 mark_as_dependent(thd, last_checked_context->select_lex, 04830 context->select_lex, this, 04831 ((type == REF_ITEM || type == FIELD_ITEM) ? 04832 (Item_ident*) (*reference) : 04833 0)); 04834 /* 04835 view reference found, we substituted it instead of this 04836 Item, so can quit 04837 */ 04838 return FALSE; 04839 } 04840 if (from_field != not_found_field) 04841 { 04842 if (cached_table && cached_table->select_lex && 04843 outer_context->select_lex && 04844 cached_table->select_lex != outer_context->select_lex) 04845 { 04846 /* 04847 Due to cache, find_field_in_tables() can return field which 04848 doesn't belong to provided outer_context. In this case we have 04849 to find proper field context in order to fix field correcly. 04850 */ 04851 do 04852 { 04853 outer_context= outer_context->outer_context; 04854 select= outer_context->select_lex; 04855 prev_subselect_item= 04856 last_checked_context->select_lex->master_unit()->item; 04857 last_checked_context= outer_context; 04858 } while (outer_context && outer_context->select_lex && 04859 cached_table->select_lex != outer_context->select_lex); 04860 } 04861 prev_subselect_item->used_tables_cache|= from_field->table->map; 04862 prev_subselect_item->const_item_cache= 0; 04863 break; 04864 } 04865 } 04866 DBUG_ASSERT(from_field == not_found_field); 04867 04868 /* Reference is not found => depend on outer (or just error). */ 04869 prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT; 04870 prev_subselect_item->const_item_cache= 0; 04871 04872 outer_context= outer_context->outer_context; 04873 } while (outer_context); 04874 04875 DBUG_ASSERT(from_field != 0 && from_field != view_ref_found); 04876 if (from_field != not_found_field) 04877 { 04878 Item_field* fld; 04879 if (!(fld= new Item_field(from_field))) 04880 goto error; 04881 thd->change_item_tree(reference, fld); 04882 mark_as_dependent(thd, last_checked_context->select_lex, 04883 thd->lex->current_select, this, fld); 04884 return FALSE; 04885 } 04886 if (ref == 0) 04887 { 04888 /* The item was not a table field and not a reference */ 04889 my_error(ER_BAD_FIELD_ERROR, MYF(0), 04890 this->full_name(), current_thd->where); 04891 goto error; 04892 } 04893 /* Should be checked in resolve_ref_in_select_and_group(). */ 04894 DBUG_ASSERT(*ref && (*ref)->fixed); 04895 mark_as_dependent(thd, last_checked_context->select_lex, 04896 context->select_lex, this, this); 04897 } 04898 } 04899 04900 DBUG_ASSERT(*ref); 04901 /* 04902 Check if this is an incorrect reference in a group function or forward 04903 reference. Do not issue an error if this is an unnamed reference inside an 04904 aggregate function. 04905 */ 04906 if (((*ref)->with_sum_func && name && 04907 !(current_sel->linkage != GLOBAL_OPTIONS_TYPE && 04908 current_sel->having_fix_field)) || 04909 !(*ref)->fixed) 04910 { 04911 my_error(ER_ILLEGAL_REFERENCE, MYF(0), 04912 name, ((*ref)->with_sum_func? 04913 "reference to group function": 04914 "forward reference in item list")); 04915 goto error; 04916 } 04917 04918 set_properties(); 04919 04920 if ((*ref)->check_cols(1)) 04921 goto error; 04922 return FALSE; 04923 04924 error: 04925 context->process_error(thd); 04926 return TRUE; 04927 } 04928 04929 04930 void Item_ref::set_properties() 04931 { 04932 max_length= (*ref)->max_length; 04933 maybe_null= (*ref)->maybe_null; 04934 decimals= (*ref)->decimals; 04935 collation.set((*ref)->collation); 04936 /* 04937 We have to remember if we refer to a sum function, to ensure that 04938 split_sum_func() doesn't try to change the reference. 04939 */ 04940 with_sum_func= (*ref)->with_sum_func; 04941 unsigned_flag= (*ref)->unsigned_flag; 04942 if ((*ref)->type() == FIELD_ITEM) 04943 alias_name_used= ((Item_ident *) (*ref))->alias_name_used; 04944 else 04945 alias_name_used= TRUE; // it is not field, so it is was resolved by alias 04946 fixed= 1; 04947 } 04948 04949 04950 void Item_ref::cleanup() 04951 { 04952 DBUG_ENTER("Item_ref::cleanup"); 04953 Item_ident::cleanup(); 04954 result_field= 0; 04955 DBUG_VOID_RETURN; 04956 } 04957 04958 04959 void Item_ref::print(String *str) 04960 { 04961 if (ref) 04962 { 04963 if ((*ref)->type() != Item::CACHE_ITEM && ref_type() != VIEW_REF && 04964 name && alias_name_used) 04965 { 04966 THD *thd= current_thd; 04967 append_identifier(thd, str, name, (uint) strlen(name)); 04968 } 04969 else 04970 (*ref)->print(str); 04971 } 04972 else 04973 Item_ident::print(str); 04974 } 04975 04976 04977 bool Item_ref::send(Protocol *prot, String *tmp) 04978 { 04979 if (result_field) 04980 return prot->store(result_field); 04981 return (*ref)->send(prot, tmp); 04982 } 04983 04984 04985 double Item_ref::val_result() 04986 { 04987 if (result_field) 04988 { 04989 if ((null_value= result_field->is_null())) 04990 return 0.0; 04991 return result_field->val_real(); 04992 } 04993 return val_real(); 04994 } 04995 04996 04997 longlong Item_ref::val_int_result() 04998 { 04999 if (result_field) 05000 { 05001 if ((null_value= result_field->is_null())) 05002 return 0; 05003 return result_field->val_int(); 05004 } 05005 return val_int(); 05006 } 05007 05008 05009 String *Item_ref::str_result(String* str) 05010 { 05011 if (result_field) 05012 { 05013 if ((null_value= result_field->is_null())) 05014 return 0; 05015 str->set_charset(str_value.charset()); 05016 return result_field->val_str(str, &str_value); 05017 } 05018 return val_str(str); 05019 } 05020 05021 05022 my_decimal *Item_ref::val_decimal_result(my_decimal *decimal_value) 05023 { 05024 if (result_field) 05025 { 05026 if ((null_value= result_field->is_null())) 05027 return 0; 05028 return result_field->val_decimal(decimal_value); 05029 } 05030 return val_decimal(decimal_value); 05031 } 05032 05033 05034 bool Item_ref::val_bool_result() 05035 { 05036 if (result_field) 05037 { 05038 if ((null_value= result_field->is_null())) 05039 return 0; 05040 switch (result_field->result_type()) { 05041 case INT_RESULT: 05042 return result_field->val_int() != 0; 05043 case DECIMAL_RESULT: 05044 { 05045 my_decimal decimal_value; 05046 my_decimal *val= result_field->val_decimal(&decimal_value); 05047 if (val) 05048 return !my_decimal_is_zero(val); 05049 return 0; 05050 } 05051 case REAL_RESULT: 05052 case STRING_RESULT: 05053 return result_field->val_real() != 0.0; 05054 case ROW_RESULT: 05055 default: 05056 DBUG_ASSERT(0); 05057 } 05058 } 05059 return val_bool(); 05060 } 05061 05062 05063 double Item_ref::val_real() 05064 { 05065 DBUG_ASSERT(fixed); 05066 double tmp=(*ref)->val_result(); 05067 null_value=(*ref)->null_value; 05068 return tmp; 05069 } 05070 05071 05072 longlong Item_ref::val_int() 05073 { 05074 DBUG_ASSERT(fixed); 05075 longlong tmp=(*ref)->val_int_result(); 05076 null_value=(*ref)->null_value; 05077 return tmp; 05078 } 05079 05080 05081 bool Item_ref::val_bool() 05082 { 05083 DBUG_ASSERT(fixed); 05084 bool tmp= (*ref)->val_bool_result(); 05085 null_value= (*ref)->null_value; 05086 return tmp; 05087 } 05088 05089 05090 String *Item_ref::val_str(String* tmp) 05091 { 05092 DBUG_ASSERT(fixed); 05093 tmp=(*ref)->str_result(tmp); 05094 null_value=(*ref)->null_value; 05095 return tmp; 05096 } 05097 05098 05099 bool Item_ref::is_null() 05100 { 05101 DBUG_ASSERT(fixed); 05102 return (*ref)->is_null(); 05103 } 05104 05105 05106 bool Item_ref::get_date(TIME *ltime,uint fuzzydate) 05107 { 05108 return (null_value=(*ref)->get_date_result(ltime,fuzzydate)); 05109 } 05110 05111 05112 my_decimal *Item_ref::val_decimal(my_decimal *decimal_value) 05113 { 05114 my_decimal *val= (*ref)->val_decimal_result(decimal_value); 05115 null_value= (*ref)->null_value; 05116 return val; 05117 } 05118 05119 int Item_ref::save_in_field(Field *to, bool no_conversions) 05120 { 05121 int res; 05122 if (result_field) 05123 { 05124 if (result_field->is_null()) 05125 { 05126 null_value= 1; 05127 return set_field_to_null_with_conversions(to, no_conversions); 05128 } 05129 to->set_notnull(); 05130 field_conv(to, result_field); 05131 null_value= 0; 05132 return 0; 05133 } 05134 res= (*ref)->save_in_field(to, no_conversions); 05135 null_value= (*ref)->null_value; 05136 return res; 05137 } 05138 05139 05140 void Item_ref::save_org_in_field(Field *field) 05141 { 05142 (*ref)->save_org_in_field(field); 05143 } 05144 05145 05146 void Item_ref::make_field(Send_field *field) 05147 { 05148 (*ref)->make_field(field); 05149 /* Non-zero in case of a view */ 05150 if (name) 05151 field->col_name= name; 05152 if (table_name) 05153 field->table_name= table_name; 05154 if (db_name) 05155 field->db_name= db_name; 05156 } 05157 05158 05159 void Item_ref_null_helper::print(String *str) 05160 { 05161 str->append(STRING_WITH_LEN("<ref_null_helper>(")); 05162 if (ref) 05163 (*ref)->print(str); 05164 else 05165 str->append('?'); 05166 str->append(')'); 05167 } 05168 05169 05170 double Item_direct_ref::val_real() 05171 { 05172 double tmp=(*ref)->val_real(); 05173 null_value=(*ref)->null_value; 05174 return tmp; 05175 } 05176 05177 05178 longlong Item_direct_ref::val_int() 05179 { 05180 longlong tmp=(*ref)->val_int(); 05181 null_value=(*ref)->null_value; 05182 return tmp; 05183 } 05184 05185 05186 String *Item_direct_ref::val_str(String* tmp) 05187 { 05188 tmp=(*ref)->val_str(tmp); 05189 null_value=(*ref)->null_value; 05190 return tmp; 05191 } 05192 05193 05194 my_decimal *Item_direct_ref::val_decimal(my_decimal *decimal_value) 05195 { 05196 my_decimal *tmp= (*ref)->val_decimal(decimal_value); 05197 null_value=(*ref)->null_value; 05198 return tmp; 05199 } 05200 05201 05202 bool Item_direct_ref::val_bool() 05203 { 05204 bool tmp= (*ref)->val_bool(); 05205 null_value=(*ref)->null_value; 05206 return tmp; 05207 } 05208 05209 05210 bool Item_direct_ref::is_null() 05211 { 05212 return (*ref)->is_null(); 05213 } 05214 05215 05216 bool Item_direct_ref::get_date(TIME *ltime,uint fuzzydate) 05217 { 05218 return (null_value=(*ref)->get_date(ltime,fuzzydate)); 05219 } 05220 05221 05222 /* 05223 Prepare referenced view viewld then call usual Item_direct_ref::fix_fields 05224 05225 SYNOPSIS 05226 Item_direct_view_ref::fix_fields() 05227 thd thread handler 05228 reference reference on reference where this item stored 05229 05230 RETURN 05231 FALSE OK 05232 TRUE Error 05233 */ 05234 05235 bool Item_direct_view_ref::fix_fields(THD *thd, Item **reference) 05236 { 05237 /* view fild reference must be defined */ 05238 DBUG_ASSERT(*ref); 05239 /* (*ref)->check_cols() will be made in Item_direct_ref::fix_fields */ 05240 if (!(*ref)->fixed && 05241 ((*ref)->fix_fields(thd, ref))) 05242 return TRUE; 05243 return Item_direct_ref::fix_fields(thd, reference); 05244 } 05245 05246 /* 05247 Compare two view column references for equality. 05248 05249 SYNOPSIS 05250 Item_direct_view_ref::eq() 05251 item item to compare with 05252 binary_cmp make binary comparison 05253 05254 DESCRIPTION 05255 A view column reference is considered equal to another column 05256 reference if the second one is a view column and if both column 05257 references resolve to the same item. It is assumed that both 05258 items are of the same type. 05259 05260 RETURN 05261 TRUE Referenced item is equal to given item 05262 FALSE otherwise 05263 */ 05264 05265 05266 bool Item_direct_view_ref::eq(const Item *item, bool binary_cmp) const 05267 { 05268 if (item->type() == REF_ITEM) 05269 { 05270 Item_ref *item_ref= (Item_ref*) item; 05271 if (item_ref->ref_type() == VIEW_REF) 05272 { 05273 Item *item_ref_ref= *(item_ref->ref); 05274 DBUG_ASSERT((*ref)->real_item()->type() == 05275 item_ref_ref->real_item()->type()); 05276 return ((*ref)->real_item() == item_ref_ref->real_item()); 05277 } 05278 } 05279 return FALSE; 05280 } 05281 05282 bool Item_default_value::eq(const Item *item, bool binary_cmp) const 05283 { 05284 return item->type() == DEFAULT_VALUE_ITEM && 05285 ((Item_default_value *)item)->arg->eq(arg, binary_cmp); 05286 } 05287 05288 05289 bool Item_default_value::fix_fields(THD *thd, Item **items) 05290 { 05291 Item *real_arg; 05292 Item_field *field_arg; 05293 Field *def_field; 05294 DBUG_ASSERT(fixed == 0); 05295 05296 if (!arg) 05297 { 05298 fixed= 1; 05299 return FALSE; 05300 } 05301 if (!arg->fixed && arg->fix_fields(thd, &arg)) 05302 goto error; 05303 05304 05305 real_arg= arg->real_item(); 05306 if (real_arg->type() != FIELD_ITEM) 05307 { 05308 my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->name); 05309 goto error; 05310 } 05311 05312 field_arg= (Item_field *)real_arg; 05313 if (field_arg->field->flags & NO_DEFAULT_VALUE_FLAG) 05314 { 05315 my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), field_arg->field->field_name); 05316 goto error; 05317 } 05318 if (!(def_field= (Field*) sql_alloc(field_arg->field->size_of()))) 05319 goto error; 05320 memcpy(def_field, field_arg->field, field_arg->field->size_of()); 05321 def_field->move_field_offset((my_ptrdiff_t) 05322 (def_field->table->s->default_values - 05323 def_field->table->record[0])); 05324 set_field(def_field); 05325 return FALSE; 05326 05327 error: 05328 context->process_error(thd); 05329 return TRUE; 05330 } 05331 05332 05333 void Item_default_value::print(String *str) 05334 { 05335 if (!arg) 05336 { 05337 str->append(STRING_WITH_LEN("default")); 05338 return; 05339 } 05340 str->append(STRING_WITH_LEN("default(")); 05341 arg->print(str); 05342 str->append(')'); 05343 } 05344 05345 05346 int Item_default_value::save_in_field(Field *field_arg, bool no_conversions) 05347 { 05348 if (!arg) 05349 { 05350 if (field_arg->flags & NO_DEFAULT_VALUE_FLAG) 05351 { 05352 if (context->error_processor == &view_error_processor) 05353 { 05354 TABLE_LIST *view= cached_table->top_table(); 05355 push_warning_printf(field_arg->table->in_use, 05356 MYSQL_ERROR::WARN_LEVEL_WARN, 05357 ER_NO_DEFAULT_FOR_VIEW_FIELD, 05358 ER(ER_NO_DEFAULT_FOR_VIEW_FIELD), 05359 view->view_db.str, 05360 view->view_name.str); 05361 } 05362 else 05363 { 05364 push_warning_printf(field_arg->table->in_use, 05365 MYSQL_ERROR::WARN_LEVEL_WARN, 05366 ER_NO_DEFAULT_FOR_FIELD, 05367 ER(ER_NO_DEFAULT_FOR_FIELD), 05368 field_arg->field_name); 05369 } 05370 return 1; 05371 } 05372 field_arg->set_default(); 05373 return 0; 05374 } 05375 return Item_field::save_in_field(field_arg, no_conversions); 05376 } 05377 05378 05379 bool Item_insert_value::eq(const Item *item, bool binary_cmp) const 05380 { 05381 return item->type() == INSERT_VALUE_ITEM && 05382 ((Item_default_value *)item)->arg->eq(arg, binary_cmp); 05383 } 05384 05385 05386 bool Item_insert_value::fix_fields(THD *thd, Item **items) 05387 { 05388 DBUG_ASSERT(fixed == 0); 05389 /* We should only check that arg is in first table */ 05390 if (!arg->fixed) 05391 { 05392 bool res; 05393 st_table_list *orig_next_table= context->last_name_resolution_table; 05394 context->last_name_resolution_table= context->first_name_resolution_table; 05395 res= arg->fix_fields(thd, &arg); 05396 context->last_name_resolution_table= orig_next_table; 05397 if (res) 05398 return TRUE; 05399 } 05400 05401 if (arg->type() == REF_ITEM) 05402 { 05403 Item_ref *ref= (Item_ref *)arg; 05404 if (ref->ref[0]->type() != FIELD_ITEM) 05405 { 05406 my_error(ER_BAD_FIELD_ERROR, MYF(0), "", "VALUES() function"); 05407 return TRUE; 05408 } 05409 arg= ref->ref[0]; 05410 } 05411 /* 05412 According to our SQL grammar, VALUES() function can reference 05413 only to a column. 05414 */ 05415 DBUG_ASSERT(arg->type() == FIELD_ITEM); 05416 05417 Item_field *field_arg= (Item_field *)arg; 05418 05419 if (field_arg->field->table->insert_values) 05420 { 05421 Field *def_field= (Field*) sql_alloc(field_arg->field->size_of()); 05422 if (!def_field) 05423 return TRUE; 05424 memcpy(def_field, field_arg->field, field_arg->field->size_of()); 05425 def_field->move_field_offset((my_ptrdiff_t) 05426 (def_field->table->insert_values - 05427 def_field->table->record[0])); 05428 set_field(def_field); 05429 } 05430 else 05431 { 05432 Field *tmp_field= field_arg->field; 05433 /* charset doesn't matter here, it's to avoid sigsegv only */ 05434 tmp_field= new Field_null(0, 0, Field::NONE, field_arg->field->field_name, 05435 &my_charset_bin); 05436 if (tmp_field) 05437 { 05438 tmp_field->init(field_arg->field->table); 05439 set_field(tmp_field); 05440 } 05441 } 05442 return FALSE; 05443 } 05444 05445 void Item_insert_value::print(String *str) 05446 { 05447 str->append(STRING_WITH_LEN("values(")); 05448 arg->print(str); 05449 str->append(')'); 05450 } 05451 05452 05453 /* 05454 Find index of Field object which will be appropriate for item 05455 representing field of row being changed in trigger. 05456 05457 SYNOPSIS 05458 setup_field() 05459 thd - current thread context 05460 table - table of trigger (and where we looking for fields) 05461 table_grant_info - GRANT_INFO of the subject table 05462 05463 NOTE 05464 This function does almost the same as fix_fields() for Item_field 05465 but is invoked right after trigger definition parsing. Since at 05466 this stage we can't say exactly what Field object (corresponding 05467 to TABLE::record[0] or TABLE::record[1]) should be bound to this 05468 Item, we only find out index of the Field and then select concrete 05469 Field object in fix_fields() (by that time Table_trigger_list::old_field/ 05470 new_field should point to proper array of Fields). 05471 It also binds Item_trigger_field to Table_triggers_list object for 05472 table of trigger which uses this item. 05473 */ 05474 05475 void Item_trigger_field::setup_field(THD *thd, TABLE *table, 05476 GRANT_INFO *table_grant_info) 05477 { 05478 /* 05479 It is too early to mark fields used here, because before execution 05480 of statement that will invoke trigger other statements may use same 05481 TABLE object, so all such mark-up will be wiped out. 05482 So instead we do it in Table_triggers_list::mark_fields_used() 05483 method which is called during execution of these statements. 05484 */ 05485 enum_mark_columns save_mark_used_columns= thd->mark_used_columns; 05486 thd->mark_used_columns= MARK_COLUMNS_NONE; 05487 /* 05488 Try to find field by its name and if it will be found 05489 set field_idx properly. 05490 */ 05491 (void)find_field_in_table(thd, table, field_name, (uint) strlen(field_name), 05492 0, &field_idx); 05493 thd->mark_used_columns= save_mark_used_columns; 05494 triggers= table->triggers; 05495 table_grants= table_grant_info; 05496 } 05497 05498 05499 bool Item_trigger_field::eq(const Item *item, bool binary_cmp) const 05500 { 05501 return item->type() == TRIGGER_FIELD_ITEM && 05502 row_version == ((Item_trigger_field *)item)->row_version && 05503 !my_strcasecmp(system_charset_info, field_name, 05504 ((Item_trigger_field *)item)->field_name); 05505 } 05506 05507 05508 void Item_trigger_field::set_required_privilege(bool rw) 05509 { 05510 /* 05511 Require SELECT and UPDATE privilege if this field will be read and 05512 set, and only UPDATE privilege for setting the field. 05513 */ 05514 want_privilege= (rw ? SELECT_ACL | UPDATE_ACL : UPDATE_ACL); 05515 } 05516 05517 05518 bool Item_trigger_field::set_value(THD *thd, sp_rcontext */*ctx*/, Item **it) 05519 { 05520 Item *item= sp_prepare_func_item(thd, it); 05521 05522 return (!item || (!fixed && fix_fields(thd, 0)) || 05523 (item->save_in_field(field, 0) < 0)); 05524 } 05525 05526 05527 bool Item_trigger_field::fix_fields(THD *thd, Item **items) 05528 { 05529 /* 05530 Since trigger is object tightly associated with TABLE object most 05531 of its set up can be performed during trigger loading i.e. trigger 05532 parsing! So we have little to do in fix_fields. :) 05533 */ 05534 05535 DBUG_ASSERT(fixed == 0); 05536 05537 /* Set field. */ 05538 05539 if (field_idx != (uint)-1) 05540 { 05541 #ifndef NO_EMBEDDED_ACCESS_CHECKS 05542 /* 05543 Check access privileges for the subject table. We check privileges only 05544 in runtime. 05545 */ 05546 05547 if (table_grants) 05548 { 05549 table_grants->want_privilege= want_privilege; 05550 05551 if (check_grant_column(thd, table_grants, triggers->table->s->db.str, 05552 triggers->table->s->table_name.str, field_name, 05553 strlen(field_name), thd->security_ctx)) 05554 return TRUE; 05555 } 05556 #endif // NO_EMBEDDED_ACCESS_CHECKS 05557 05558 field= (row_version == OLD_ROW) ? triggers->old_field[field_idx] : 05559 triggers->new_field[field_idx]; 05560 set_field(field); 05561 fixed= 1; 05562 return FALSE; 05563 } 05564 05565 my_error(ER_BAD_FIELD_ERROR, MYF(0), field_name, 05566 (row_version == NEW_ROW) ? "NEW" : "OLD"); 05567 return TRUE; 05568 } 05569 05570 05571 void Item_trigger_field::print(String *str) 05572 { 05573 str->append((row_version == NEW_ROW) ? "NEW" : "OLD", 3); 05574 str->append('.'); 05575 str->append(field_name); 05576 } 05577 05578 05579 void Item_trigger_field::cleanup() 05580 { 05581 want_privilege= original_privilege; 05582 /* 05583 Since special nature of Item_trigger_field we should not do most of 05584 things from Item_field::cleanup() or Item_ident::cleanup() here. 05585 */ 05586 Item::cleanup(); 05587 } 05588 05589 05590 /* 05591 If item is a const function, calculate it and return a const item 05592 The original item is freed if not returned 05593 */ 05594 05595 Item_result item_cmp_type(Item_result a,Item_result b) 05596 { 05597 if (a == STRING_RESULT && b == STRING_RESULT) 05598 return STRING_RESULT; 05599 if (a == INT_RESULT && b == INT_RESULT) 05600 return INT_RESULT; 05601 else if (a == ROW_RESULT || b == ROW_RESULT) 05602 return ROW_RESULT; 05603 if ((a == INT_RESULT || a == DECIMAL_RESULT) && 05604 (b == INT_RESULT || b == DECIMAL_RESULT)) 05605 return DECIMAL_RESULT; 05606 return REAL_RESULT; 05607 } 05608 05609 05610 void resolve_const_item(THD *thd, Item **ref, Item *comp_item) 05611 { 05612 Item *item= *ref; 05613 Item *new_item= NULL; 05614 if (item->basic_const_item()) 05615 return; // Can't be better 05616 Item_result res_type=item_cmp_type(comp_item->result_type(), 05617 item->result_type()); 05618 char *name=item->name; // Alloced by sql_alloc 05619 05620 switch (res_type) { 05621 case STRING_RESULT: 05622 { 05623 char buff[MAX_FIELD_WIDTH]; 05624 String tmp(buff,sizeof(buff),&my_charset_bin),*result; 05625 result=item->val_str(&tmp); 05626 if (item->null_value) 05627 new_item= new Item_null(name); 05628 else 05629 { 05630 uint length= result->length(); 05631 char *tmp_str= sql_strmake(result->ptr(), length); 05632 new_item= new Item_string(name, tmp_str, length, result->charset()); 05633 } 05634 break; 05635 } 05636 case INT_RESULT: 05637 { 05638 longlong result=item->val_int(); 05639 uint length=item->max_length; 05640 bool null_value=item->null_value; 05641 new_item= (null_value ? (Item*) new Item_null(name) : 05642 (Item*) new Item_int(name, result, length)); 05643 break; 05644 } 05645 case ROW_RESULT: 05646 if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM) 05647 { 05648 /* 05649 Substitute constants only in Item_rows. Don't affect other Items 05650 with ROW_RESULT (eg Item_singlerow_subselect). 05651 05652 For such Items more optimal is to detect if it is constant and replace 05653 it with Item_row. This would optimize queries like this: 05654 SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1); 05655 */ 05656 Item_row *item_row= (Item_row*) item; 05657 Item_row *comp_item_row= (Item_row*) comp_item; 05658 uint col; 05659 new_item= 0; 05660 /* 05661 If item and comp_item are both Item_rows and have same number of cols 05662 then process items in Item_row one by one. 05663 We can't ignore NULL values here as this item may be used with <=>, in 05664 which case NULL's are significant. 05665 */ 05666 DBUG_ASSERT(item->result_type() == comp_item->result_type()); 05667 DBUG_ASSERT(item_row->cols() == comp_item_row->cols()); 05668 col= item_row->cols(); 05669 while (col-- > 0) 05670 resolve_const_item(thd, item_row->addr(col), comp_item_row->el(col)); 05671 break; 05672 } 05673 /* Fallthrough */ 05674 case REAL_RESULT: 05675 { // It must REAL_RESULT 05676 double result= item->val_real(); 05677 uint length=item->max_length,decimals=item->decimals; 05678 bool null_value=item->null_value; 05679 new_item= (null_value ? (Item*) new Item_null(name) : (Item*) 05680 new Item_float(name, result, decimals, length)); 05681 break; 05682 } 05683 case DECIMAL_RESULT: 05684 { 05685 my_decimal decimal_value; 05686 my_decimal *result= item->val_decimal(&decimal_value); 05687 uint length= item->max_length, decimals= item->decimals; 05688 bool null_value= item->null_value; 05689 new_item= (null_value ? 05690 (Item*) new Item_null(name) : 05691 (Item*) new Item_decimal(name, result, length, decimals)); 05692 break; 05693 } 05694 default: 05695 DBUG_ASSERT(0); 05696 } 05697 if (new_item) 05698 thd->change_item_tree(ref, new_item); 05699 } 05700 05701 /* 05702 Return true if the value stored in the field is equal to the const item 05703 We need to use this on the range optimizer because in some cases 05704 we can't store the value in the field without some precision/character loss. 05705 */ 05706 05707 bool field_is_equal_to_item(Field *field,Item *item) 05708 { 05709 05710 Item_result res_type=item_cmp_type(field->result_type(), 05711 item->result_type()); 05712 if (res_type == STRING_RESULT) 05713 { 05714 char item_buff[MAX_FIELD_WIDTH]; 05715 char field_buff[MAX_FIELD_WIDTH]; 05716 String item_tmp(item_buff,sizeof(item_buff),&my_charset_bin),*item_result; 05717 String field_tmp(field_buff,sizeof(field_buff),&my_charset_bin); 05718 item_result=item->val_str(&item_tmp); 05719 if (item->null_value) 05720 return 1; // This must be true 05721 field->val_str(&field_tmp); 05722 return !stringcmp(&field_tmp,item_result); 05723 } 05724 if (res_type == INT_RESULT) 05725 return 1; // Both where of type int 05726 if (res_type == DECIMAL_RESULT) 05727 { 05728 my_decimal item_buf, *item_val, 05729 field_buf, *field_val; 05730 item_val= item->val_decimal(&item_buf); 05731 if (item->null_value) 05732 return 1; // This must be true 05733 field_val= field->val_decimal(&field_buf); 05734 return !my_decimal_cmp(item_val, field_val); 05735 } 05736 double result= item->val_real(); 05737 if (item->null_value) 05738 return 1; 05739 return result == field->val_real(); 05740 } 05741 05742 Item_cache* Item_cache::get_cache(Item_result type) 05743 { 05744 switch (type) { 05745 case INT_RESULT: 05746 return new Item_cache_int(); 05747 case REAL_RESULT: 05748 return new Item_cache_real(); 05749 case DECIMAL_RESULT: 05750 return new Item_cache_decimal(); 05751 case STRING_RESULT: 05752 return new Item_cache_str(); 05753 case ROW_RESULT: 05754 return new Item_cache_row(); 05755 default: 05756 // should never be in real life 05757 DBUG_ASSERT(0); 05758 return 0; 05759 } 05760 } 05761 05762 05763 void Item_cache::print(String *str) 05764 { 05765 str->append(STRING_WITH_LEN("<cache>(")); 05766 if (example) 05767 example->print(str); 05768 else 05769 Item::print(str); 05770 str->append(')'); 05771 } 05772 05773 05774 void Item_cache_int::store(Item *item) 05775 { 05776 value= item->val_int_result(); 05777 null_value= item->null_value; 05778 unsigned_flag= item->unsigned_flag; 05779 } 05780 05781 05782 String *Item_cache_int::val_str(String *str) 05783 { 05784 DBUG_ASSERT(fixed == 1); 05785 str->set(value, default_charset()); 05786 return str; 05787 } 05788 05789 05790 my_decimal *Item_cache_int::val_decimal(my_decimal *decimal_val) 05791 { 05792 DBUG_ASSERT(fixed == 1); 05793 int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_val); 05794 return decimal_val; 05795 } 05796 05797 05798 void Item_cache_real::store(Item *item) 05799 { 05800 value= item->val_result(); 05801 null_value= item->null_value; 05802 } 05803 05804 05805 longlong Item_cache_real::val_int() 05806 { 05807 DBUG_ASSERT(fixed == 1); 05808 return (longlong) rint(value); 05809 } 05810 05811 05812 String* Item_cache_real::val_str(String *str) 05813 { 05814 DBUG_ASSERT(fixed == 1); 05815 str->set_real(value, decimals, default_charset()); 05816 return str; 05817 } 05818 05819 05820 my_decimal *Item_cache_real::val_decimal(my_decimal *decimal_val) 05821 { 05822 DBUG_ASSERT(fixed == 1); 05823 double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val); 05824 return decimal_val; 05825 } 05826 05827 05828 void Item_cache_decimal::store(Item *item) 05829 { 05830 my_decimal *val= item->val_decimal_result(&decimal_value); 05831 if (!(null_value= item->null_value) && val != &decimal_value) 05832 my_decimal2decimal(val, &decimal_value); 05833 } 05834 05835 double Item_cache_decimal::val_real() 05836 { 05837 DBUG_ASSERT(fixed); 05838 double res; 05839 my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &res); 05840 return res; 05841 } 05842 05843 longlong Item_cache_decimal::val_int() 05844 { 05845 DBUG_ASSERT(fixed); 05846 longlong res; 05847 my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &res); 05848 return res; 05849 } 05850 05851 String* Item_cache_decimal::val_str(String *str) 05852 { 05853 DBUG_ASSERT(fixed); 05854 my_decimal_round(E_DEC_FATAL_ERROR, &decimal_value, decimals, FALSE, 05855 &decimal_value); 05856 my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, str); 05857 return str; 05858 } 05859 05860 my_decimal *Item_cache_decimal::val_decimal(my_decimal *val) 05861 { 05862 DBUG_ASSERT(fixed); 05863 return &decimal_value; 05864 } 05865 05866 05867 void Item_cache_str::store(Item *item) 05868 { 05869 value_buff.set(buffer, sizeof(buffer), item->collation.collation); 05870 value= item->str_result(&value_buff); 05871 if ((null_value= item->null_value)) 05872 value= 0; 05873 else if (value != &value_buff) 05874 { 05875 /* 05876 We copy string value to avoid changing value if 'item' is table field 05877 in queries like following (where t1.c is varchar): 05878 select a, 05879 (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'), 05880 (select c from t1 where a=t2.a) 05881 from t2; 05882 */ 05883 value_buff.copy(*value); 05884 value= &value_buff; 05885 } 05886 } 05887 05888 double Item_cache_str::val_real() 05889 { 05890 DBUG_ASSERT(fixed == 1); 05891 int err_not_used; 05892 char *end_not_used; 05893 if (value) 05894 return my_strntod(value->charset(), (char*) value->ptr(), 05895 value->length(), &end_not_used, &err_not_used); 05896 return (double) 0; 05897 } 05898 05899 05900 longlong Item_cache_str::val_int() 05901 { 05902 DBUG_ASSERT(fixed == 1); 05903 int err; 05904 if (value) 05905 return my_strntoll(value->charset(), value->ptr(), 05906 value->length(), 10, (char**) 0, &err); 05907 else 05908 return (longlong)0; 05909 } 05910 05911 my_decimal *Item_cache_str::val_decimal(my_decimal *decimal_val) 05912 { 05913 DBUG_ASSERT(fixed == 1); 05914 if (value) 05915 string2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val); 05916 else 05917 decimal_val= 0; 05918 return decimal_val; 05919 } 05920 05921 05922 bool Item_cache_row::allocate(uint num) 05923 { 05924 item_count= num; 05925 THD *thd= current_thd; 05926 return (!(values= 05927 (Item_cache **) thd->calloc(sizeof(Item_cache *)*item_count))); 05928 } 05929 05930 05931 bool Item_cache_row::setup(Item * item) 05932 { 05933 example= item; 05934 if (!values && allocate(item->cols())) 05935 return 1; 05936 for (uint i= 0; i < item_count; i++) 05937 { 05938 Item *el= item->el(i); 05939 Item_cache *tmp; 05940 if (!(tmp= values[i]= Item_cache::get_cache(el->result_type()))) 05941 return 1; 05942 tmp->setup(el); 05943 } 05944 return 0; 05945 } 05946 05947 05948 void Item_cache_row::store(Item * item) 05949 { 05950 null_value= 0; 05951 item->bring_value(); 05952 for (uint i= 0; i < item_count; i++) 05953 { 05954 values[i]->store(item->el(i)); 05955 null_value|= values[i]->null_value; 05956 } 05957 } 05958 05959 05960 void Item_cache_row::illegal_method_call(const char *method) 05961 { 05962 DBUG_ENTER("Item_cache_row::illegal_method_call"); 05963 DBUG_PRINT("error", ("!!! %s method was called for row item", method)); 05964 DBUG_ASSERT(0); 05965 my_error(ER_OPERAND_COLUMNS, MYF(0), 1); 05966 DBUG_VOID_RETURN; 05967 } 05968 05969 05970 bool Item_cache_row::check_cols(uint c) 05971 { 05972 if (c != item_count) 05973 { 05974 my_error(ER_OPERAND_COLUMNS, MYF(0), c); 05975 return 1; 05976 } 05977 return 0; 05978 } 05979 05980 05981 bool Item_cache_row::null_inside() 05982 { 05983 for (uint i= 0; i < item_count; i++) 05984 { 05985 if (values[i]->cols() > 1) 05986 { 05987 if (values[i]->null_inside()) 05988 return 1; 05989 } 05990 else 05991 { 05992 values[i]->val_int(); 05993 if (values[i]->null_value) 05994 return 1; 05995 } 05996 } 05997 return 0; 05998 } 05999 06000 06001 void Item_cache_row::bring_value() 06002 { 06003 for (uint i= 0; i < item_count; i++) 06004 values[i]->bring_value(); 06005 return; 06006 } 06007 06008 06009 Item_type_holder::Item_type_holder(THD *thd, Item *item) 06010 :Item(thd, item), enum_set_typelib(0), fld_type(get_real_type(item)) 06011 { 06012 DBUG_ASSERT(item->fixed); 06013 06014 max_length= display_length(item); 06015 maybe_null= item->maybe_null; 06016 collation.set(item->collation); 06017 get_full_info(item); 06018 /* fix variable decimals which always is NOT_FIXED_DEC */ 06019 if (Field::result_merge_type(fld_type) == INT_RESULT) 06020 decimals= 0; 06021 prev_decimal_int_part= item->decimal_int_part(); 06022 } 06023 06024 06025 /* 06026 Return expression type of Item_type_holder 06027 06028 SYNOPSIS 06029 Item_type_holder::result_type() 06030 06031 RETURN 06032 Item_result (type of internal MySQL expression result) 06033 */ 06034 06035 Item_result Item_type_holder::result_type() const 06036 { 06037 return Field::result_merge_type(fld_type); 06038 } 06039 06040 06041 /* 06042 Find real field type of item 06043 06044 SYNOPSIS 06045 Item_type_holder::get_real_type() 06046 06047 RETURN 06048 type of field which should be created to store item value 06049 */ 06050 06051 enum_field_types Item_type_holder::get_real_type(Item *item) 06052 { 06053 switch(item->type()) 06054 { 06055 case FIELD_ITEM: 06056 { 06057 /* 06058 Item_fields::field_type ask Field_type() but sometimes field return 06059 a different type, like for enum/set, so we need to ask real type. 06060 */ 06061 Field *field= ((Item_field *) item)->field; 06062 enum_field_types type= field->real_type(); 06063 /* work around about varchar type field detection */ 06064 if (type == MYSQL_TYPE_STRING && field->type() == MYSQL_TYPE_VAR_STRING) 06065 return MYSQL_TYPE_VAR_STRING; 06066 return type; 06067 } 06068 case SUM_FUNC_ITEM: 06069 { 06070 /* 06071 Argument of aggregate function sometimes should be asked about field 06072 type 06073 */ 06074 Item_sum *item_sum= (Item_sum *) item; 06075 if (item_sum->keep_field_type()) 06076 return get_real_type(item_sum->args[0]); 06077 break; 06078 } 06079 case FUNC_ITEM: 06080 if (((Item_func *) item)->functype() == Item_func::GUSERVAR_FUNC) 06081 { 06082 /* 06083 There are work around of problem with changing variable type on the 06084 fly and variable always report "string" as field type to get 06085 acceptable information for client in send_field, so we make field 06086 type from expression type. 06087 */ 06088 switch (item->result_type()) { 06089 case STRING_RESULT: 06090 return MYSQL_TYPE_VAR_STRING; 06091 case INT_RESULT: 06092 return MYSQL_TYPE_LONGLONG; 06093 case REAL_RESULT: 06094 return MYSQL_TYPE_DOUBLE; 06095 case DECIMAL_RESULT: 06096 return MYSQL_TYPE_NEWDECIMAL; 06097 case ROW_RESULT: 06098 default: 06099 DBUG_ASSERT(0); 06100 return MYSQL_TYPE_VAR_STRING; 06101 } 06102 } 06103 break; 06104 default: 06105 break; 06106 } 06107 return item->field_type(); 06108 } 06109 06110 /* 06111 Find field type which can carry current Item_type_holder type and 06112 type of given Item. 06113 06114 SYNOPSIS 06115 Item_type_holder::join_types() 06116 thd thread handler 06117 item given item to join its parameters with this item ones 06118 06119 RETURN 06120 TRUE error - types are incompatible 06121 FALSE OK 06122 */ 06123 06124 bool Item_type_holder::join_types(THD *thd, Item *item) 06125 { 06126 uint max_length_orig= max_length; 06127 uint decimals_orig= decimals; 06128 DBUG_ENTER("Item_type_holder::join_types"); 06129 DBUG_PRINT("info:", ("was type %d len %d, dec %d name %s", 06130 fld_type, max_length, decimals, 06131 (name ? name : "<NULL>"))); 06132 DBUG_PRINT("info:", ("in type %d len %d, dec %d", 06133 get_real_type(item), 06134 item->max_length, item->decimals)); 06135 fld_type= Field::field_type_merge(fld_type, get_real_type(item)); 06136 { 06137 int item_decimals= item->decimals; 06138 /* fix variable decimals which always is NOT_FIXED_DEC */ 06139 if (Field::result_merge_type(fld_type) == INT_RESULT) 06140 item_decimals= 0; 06141 decimals= max(decimals, item_decimals); 06142 } 06143 if (Field::result_merge_type(fld_type) == DECIMAL_RESULT) 06144 { 06145 decimals= min(max(decimals, item->decimals), DECIMAL_MAX_SCALE); 06146 int precision= min(max(prev_decimal_int_part, item->decimal_int_part()) 06147 + decimals, DECIMAL_MAX_PRECISION); 06148 unsigned_flag&= item->unsigned_flag; 06149 max_length= my_decimal_precision_to_length(precision, decimals, 06150 unsigned_flag); 06151 } 06152 06153 switch (Field::result_merge_type(fld_type)) 06154 { 06155 case STRING_RESULT: 06156 { 06157 const char *old_cs, *old_derivation; 06158 uint32 old_max_chars= max_length / collation.collation->mbmaxlen; 06159 old_cs= collation.collation->name; 06160 old_derivation= collation.derivation_name(); 06161 if (collation.aggregate(item->collation, MY_COLL_ALLOW_CONV)) 06162 { 06163 my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0), 06164 old_cs, old_derivation, 06165 item->collation.collation->name, 06166 item->collation.derivation_name(), 06167 "UNION"); 06168 DBUG_RETURN(TRUE); 06169 } 06170 /* 06171 To figure out max_length, we have to take into account possible 06172 expansion of the size of the values because of character set 06173 conversions. 06174 */ 06175 max_length= max(old_max_chars * collation.collation->mbmaxlen, 06176 display_length(item) / item->collation.collation->mbmaxlen * 06177 collation.collation->mbmaxlen); 06178 break; 06179 } 06180 case REAL_RESULT: 06181 { 06182 if (decimals != NOT_FIXED_DEC) 06183 { 06184 int delta1= max_length_orig - decimals_orig; 06185 int delta2= item->max_length - item->decimals; 06186 if (fld_type == MYSQL_TYPE_DECIMAL) 06187 max_length= max(delta1, delta2) + decimals; 06188 else 06189 max_length= min(max(delta1, delta2) + decimals, 06190 (fld_type == MYSQL_TYPE_FLOAT) ? FLT_DIG+6 : DBL_DIG+7); 06191 } 06192 else 06193 max_length= (fld_type == MYSQL_TYPE_FLOAT) ? FLT_DIG+6 : DBL_DIG+7; 06194 break; 06195 } 06196 default: 06197 max_length= max(max_length, display_length(item)); 06198 }; 06199 maybe_null|= item->maybe_null; 06200 get_full_info(item); 06201 06202 /* Remember decimal integer part to be used in DECIMAL_RESULT handleng */ 06203 prev_decimal_int_part= decimal_int_part(); 06204 DBUG_PRINT("info", ("become type: %d len: %u dec: %u", 06205 (int) fld_type, max_length, (uint) decimals)); 06206 DBUG_RETURN(FALSE); 06207 } 06208 06209 /* 06210 Calculate lenth for merging result for given Item type 06211 06212 SYNOPSIS 06213 Item_type_holder::real_length() 06214 item Item for lrngth detection 06215 06216 RETURN 06217 length 06218 */ 06219 06220 uint32 Item_type_holder::display_length(Item *item) 06221 { 06222 if (item->type() == Item::FIELD_ITEM) 06223 return ((Item_field *)item)->max_disp_length(); 06224 06225 switch (item->field_type()) 06226 { 06227 case MYSQL_TYPE_DECIMAL: 06228 case MYSQL_TYPE_TIMESTAMP: 06229 case MYSQL_TYPE_DATE: 06230 case MYSQL_TYPE_TIME: 06231 case MYSQL_TYPE_DATETIME: 06232 case MYSQL_TYPE_YEAR: 06233 case MYSQL_TYPE_NEWDATE: 06234 case MYSQL_TYPE_VARCHAR: 06235 case MYSQL_TYPE_BIT: 06236 case MYSQL_TYPE_NEWDECIMAL: 06237 case MYSQL_TYPE_ENUM: 06238 case MYSQL_TYPE_SET: 06239 case MYSQL_TYPE_TINY_BLOB: 06240 case MYSQL_TYPE_MEDIUM_BLOB: 06241 case MYSQL_TYPE_LONG_BLOB: 06242 case MYSQL_TYPE_BLOB: 06243 case MYSQL_TYPE_VAR_STRING: 06244 case MYSQL_TYPE_STRING: 06245 case MYSQL_TYPE_GEOMETRY: 06246 return item->max_length; 06247 case MYSQL_TYPE_TINY: 06248 return 4; 06249 case MYSQL_TYPE_SHORT: 06250 return 6; 06251 case MYSQL_TYPE_LONG: 06252 return 11; 06253 case MYSQL_TYPE_FLOAT: 06254 return 25; 06255 case MYSQL_TYPE_DOUBLE: 06256 return 53; 06257 case MYSQL_TYPE_NULL: 06258 return 0; 06259 case MYSQL_TYPE_LONGLONG: 06260 return 20; 06261 case MYSQL_TYPE_INT24: 06262 return 8; 06263 default: 06264 DBUG_ASSERT(0); // we should never go there 06265 return 0; 06266 } 06267 } 06268 06269 06270 /* 06271 Make temporary table field according collected information about type 06272 of UNION result 06273 06274 SYNOPSIS 06275 Item_type_holder::make_field_by_type() 06276 table temporary table for which we create fields 06277 06278 RETURN 06279 created field 06280 */ 06281 06282 Field *Item_type_holder::make_field_by_type(TABLE *table) 06283 { 06284 /* 06285 The field functions defines a field to be not null if null_ptr is not 0 06286 */ 06287 uchar *null_ptr= maybe_null ? (uchar*) "" : 0; 06288 Field *field; 06289 06290 switch (fld_type) { 06291 case MYSQL_TYPE_ENUM: 06292 DBUG_ASSERT(enum_set_typelib); 06293 field= new Field_enum((char *) 0, max_length, null_ptr, 0, 06294 Field::NONE, name, 06295 get_enum_pack_length(enum_set_typelib->count), 06296 enum_set_typelib, collation.collation); 06297 if (field) 06298 field->init(table); 06299 return field; 06300 case MYSQL_TYPE_SET: 06301 DBUG_ASSERT(enum_set_typelib); 06302 field= new Field_set((char *) 0, max_length, null_ptr, 0, 06303 Field::NONE, name, 06304 get_set_pack_length(enum_set_typelib->count), 06305 enum_set_typelib, collation.collation); 06306 if (field) 06307 field->init(table); 06308 return field; 06309 default: 06310 break; 06311 } 06312 return tmp_table_field_from_field_type(table, 0); 06313 } 06314 06315 06316 /* 06317 Get full information from Item about enum/set fields to be able to create 06318 them later 06319 06320 SYNOPSIS 06321 Item_type_holder::get_full_info 06322 item Item for information collection 06323 */ 06324 void Item_type_holder::get_full_info(Item *item) 06325 { 06326 if (fld_type == MYSQL_TYPE_ENUM || 06327 fld_type == MYSQL_TYPE_SET) 06328 { 06329 if (item->type() == Item::SUM_FUNC_ITEM && 06330 (((Item_sum*)item)->sum_func() == Item_sum::MAX_FUNC || 06331 ((Item_sum*)item)->sum_func() == Item_sum::MIN_FUNC)) 06332 item = ((Item_sum*)item)->args[0]; 06333 /* 06334 We can have enum/set type after merging only if we have one enum|set 06335 field (or MIN|MAX(enum|set field)) and number of NULL fields 06336 */ 06337 DBUG_ASSERT((enum_set_typelib && 06338 get_real_type(item) == MYSQL_TYPE_NULL) || 06339 (!enum_set_typelib && 06340 item->type() == Item::FIELD_ITEM && 06341 (get_real_type(item) == MYSQL_TYPE_ENUM || 06342 get_real_type(item) == MYSQL_TYPE_SET) && 06343 ((Field_enum*)((Item_field *) item)->field)->typelib)); 06344 if (!enum_set_typelib) 06345 { 06346 enum_set_typelib= ((Field_enum*)((Item_field *) item)->field)->typelib; 06347 } 06348 } 06349 } 06350 06351 06352 double Item_type_holder::val_real() 06353 { 06354 DBUG_ASSERT(0); // should never be called 06355 return 0.0; 06356 } 06357 06358 06359 longlong Item_type_holder::val_int() 06360 { 06361 DBUG_ASSERT(0); // should never be called 06362 return 0; 06363 } 06364 06365 my_decimal *Item_type_holder::val_decimal(my_decimal *) 06366 { 06367 DBUG_ASSERT(0); // should never be called 06368 return 0; 06369 } 06370 06371 String *Item_type_holder::val_str(String*) 06372 { 06373 DBUG_ASSERT(0); // should never be called 06374 return 0; 06375 } 06376 06377 void Item_result_field::cleanup() 06378 { 06379 DBUG_ENTER("Item_result_field::cleanup()"); 06380 Item::cleanup(); 06381 result_field= 0; 06382 DBUG_VOID_RETURN; 06383 } 06384 06385 /* 06386 Dummy error processor used by default by Name_resolution_context 06387 06388 SYNOPSIS 06389 dummy_error_processor() 06390 06391 NOTE 06392 do nothing 06393 */ 06394 06395 void dummy_error_processor(THD *thd, void *data) 06396 {} 06397 06398 /* 06399 Wrapper of hide_view_error call for Name_resolution_context error processor 06400 06401 SYNOPSIS 06402 view_error_processor() 06403 06404 NOTE 06405 hide view underlying tables details in error messages 06406 */ 06407 06408 void view_error_processor(THD *thd, void *data) 06409 { 06410 ((TABLE_LIST *)data)->hide_view_error(thd); 06411 } 06412 06413 /***************************************************************************** 06414 ** Instantiate templates 06415 *****************************************************************************/ 06416 06417 #ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION 06418 template class List<Item>; 06419 template class List_iterator<Item>; 06420 template class List_iterator_fast<Item>; 06421 template class List_iterator_fast<Item_field>; 06422 template class List<List_item>; 06423 #endif
1.4.7

