00001 /* Copyright (C) 2000-2003 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_INTERFACE 00019 #pragma interface /* gcc class implementation */ 00020 #endif 00021 00022 class Protocol; 00023 struct st_table_list; 00024 void item_init(void); /* Init item functions */ 00025 class Item_field; 00026 00027 /* 00028 "Declared Type Collation" 00029 A combination of collation and its derivation. 00030 */ 00031 00032 enum Derivation 00033 { 00034 DERIVATION_IGNORABLE= 5, 00035 DERIVATION_COERCIBLE= 4, 00036 DERIVATION_SYSCONST= 3, 00037 DERIVATION_IMPLICIT= 2, 00038 DERIVATION_NONE= 1, 00039 DERIVATION_EXPLICIT= 0 00040 }; 00041 00042 /* 00043 Flags for collation aggregation modes: 00044 MY_COLL_ALLOW_SUPERSET_CONV - allow conversion to a superset 00045 MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value 00046 (i.e. constant). 00047 MY_COLL_ALLOW_CONV - allow any kind of conversion 00048 (combination of the above two) 00049 MY_COLL_DISALLOW_NONE - don't allow return DERIVATION_NONE 00050 (e.g. when aggregating for comparison) 00051 MY_COLL_CMP_CONV - combination of MY_COLL_ALLOW_CONV 00052 and MY_COLL_DISALLOW_NONE 00053 */ 00054 00055 #define MY_COLL_ALLOW_SUPERSET_CONV 1 00056 #define MY_COLL_ALLOW_COERCIBLE_CONV 2 00057 #define MY_COLL_ALLOW_CONV 3 00058 #define MY_COLL_DISALLOW_NONE 4 00059 #define MY_COLL_CMP_CONV 7 00060 00061 class DTCollation { 00062 public: 00063 CHARSET_INFO *collation; 00064 enum Derivation derivation; 00065 00066 DTCollation() 00067 { 00068 collation= &my_charset_bin; 00069 derivation= DERIVATION_NONE; 00070 } 00071 DTCollation(CHARSET_INFO *collation_arg, Derivation derivation_arg) 00072 { 00073 collation= collation_arg; 00074 derivation= derivation_arg; 00075 } 00076 void set(DTCollation &dt) 00077 { 00078 collation= dt.collation; 00079 derivation= dt.derivation; 00080 } 00081 void set(CHARSET_INFO *collation_arg, Derivation derivation_arg) 00082 { 00083 collation= collation_arg; 00084 derivation= derivation_arg; 00085 } 00086 void set(CHARSET_INFO *collation_arg) 00087 { collation= collation_arg; } 00088 void set(Derivation derivation_arg) 00089 { derivation= derivation_arg; } 00090 bool aggregate(DTCollation &dt, uint flags= 0); 00091 bool set(DTCollation &dt1, DTCollation &dt2, uint flags= 0) 00092 { set(dt1); return aggregate(dt2, flags); } 00093 const char *derivation_name() const 00094 { 00095 switch(derivation) 00096 { 00097 case DERIVATION_IGNORABLE: return "IGNORABLE"; 00098 case DERIVATION_COERCIBLE: return "COERCIBLE"; 00099 case DERIVATION_IMPLICIT: return "IMPLICIT"; 00100 case DERIVATION_SYSCONST: return "SYSCONST"; 00101 case DERIVATION_EXPLICIT: return "EXPLICIT"; 00102 case DERIVATION_NONE: return "NONE"; 00103 default: return "UNKNOWN"; 00104 } 00105 } 00106 }; 00107 00108 00109 /*************************************************************************/ 00110 /* 00111 A framework to easily handle different return types for hybrid items 00112 (hybrid item is an item whose operand can be of any type, e.g. integer, 00113 real, decimal). 00114 */ 00115 00116 struct Hybrid_type_traits; 00117 00118 struct Hybrid_type 00119 { 00120 longlong integer; 00121 00122 double real; 00123 /* 00124 Use two decimal buffers interchangeably to speed up += operation 00125 which has no native support in decimal library. 00126 Hybrid_type+= arg is implemented as dec_buf[1]= dec_buf[0] + arg. 00127 The third decimal is used as a handy temporary storage. 00128 */ 00129 my_decimal dec_buf[3]; 00130 int used_dec_buf_no; 00131 00132 /* 00133 Traits moved to a separate class to 00134 a) be able to easily change object traits in runtime 00135 b) they work as a differentiator for the union above 00136 */ 00137 const Hybrid_type_traits *traits; 00138 00139 Hybrid_type() {} 00140 /* XXX: add traits->copy() when needed */ 00141 Hybrid_type(const Hybrid_type &rhs) :traits(rhs.traits) {} 00142 }; 00143 00144 00145 /* Hybryd_type_traits interface + default implementation for REAL_RESULT */ 00146 00147 struct Hybrid_type_traits 00148 { 00149 virtual Item_result type() const { return REAL_RESULT; } 00150 00151 virtual void 00152 fix_length_and_dec(Item *item, Item *arg) const; 00153 00154 /* Hybrid_type operations. */ 00155 virtual void set_zero(Hybrid_type *val) const { val->real= 0.0; } 00156 virtual void add(Hybrid_type *val, Field *f) const 00157 { val->real+= f->val_real(); } 00158 virtual void div(Hybrid_type *val, ulonglong u) const 00159 { val->real/= ulonglong2double(u); } 00160 00161 virtual longlong val_int(Hybrid_type *val, bool unsigned_flag) const 00162 { return (longlong) rint(val->real); } 00163 virtual double val_real(Hybrid_type *val) const { return val->real; } 00164 virtual my_decimal *val_decimal(Hybrid_type *val, my_decimal *buf) const; 00165 virtual String *val_str(Hybrid_type *val, String *buf, uint8 decimals) const; 00166 static const Hybrid_type_traits *instance(); 00167 Hybrid_type_traits() {} 00168 virtual ~Hybrid_type_traits() {} 00169 }; 00170 00171 00172 struct Hybrid_type_traits_decimal: public Hybrid_type_traits 00173 { 00174 virtual Item_result type() const { return DECIMAL_RESULT; } 00175 00176 virtual void 00177 fix_length_and_dec(Item *arg, Item *item) const; 00178 00179 /* Hybrid_type operations. */ 00180 virtual void set_zero(Hybrid_type *val) const; 00181 virtual void add(Hybrid_type *val, Field *f) const; 00182 virtual void div(Hybrid_type *val, ulonglong u) const; 00183 00184 virtual longlong val_int(Hybrid_type *val, bool unsigned_flag) const; 00185 virtual double val_real(Hybrid_type *val) const; 00186 virtual my_decimal *val_decimal(Hybrid_type *val, my_decimal *buf) const 00187 { return &val->dec_buf[val->used_dec_buf_no]; } 00188 virtual String *val_str(Hybrid_type *val, String *buf, uint8 decimals) const; 00189 static const Hybrid_type_traits_decimal *instance(); 00190 Hybrid_type_traits_decimal() {}; 00191 }; 00192 00193 00194 struct Hybrid_type_traits_integer: public Hybrid_type_traits 00195 { 00196 virtual Item_result type() const { return INT_RESULT; } 00197 00198 virtual void 00199 fix_length_and_dec(Item *arg, Item *item) const; 00200 00201 /* Hybrid_type operations. */ 00202 virtual void set_zero(Hybrid_type *val) const 00203 { val->integer= 0; } 00204 virtual void add(Hybrid_type *val, Field *f) const 00205 { val->integer+= f->val_int(); } 00206 virtual void div(Hybrid_type *val, ulonglong u) const 00207 { val->integer/= (longlong) u; } 00208 00209 virtual longlong val_int(Hybrid_type *val, bool unsigned_flag) const 00210 { return val->integer; } 00211 virtual double val_real(Hybrid_type *val) const 00212 { return (double) val->integer; } 00213 virtual my_decimal *val_decimal(Hybrid_type *val, my_decimal *buf) const 00214 { 00215 int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, &val->dec_buf[2]); 00216 return &val->dec_buf[2]; 00217 } 00218 virtual String *val_str(Hybrid_type *val, String *buf, uint8 decimals) const 00219 { buf->set(val->integer, &my_charset_bin); return buf;} 00220 static const Hybrid_type_traits_integer *instance(); 00221 Hybrid_type_traits_integer() {}; 00222 }; 00223 00224 00225 void dummy_error_processor(THD *thd, void *data); 00226 00227 void view_error_processor(THD *thd, void *data); 00228 00229 /* 00230 Instances of Name_resolution_context store the information necesary for 00231 name resolution of Items and other context analysis of a query made in 00232 fix_fields(). 00233 00234 This structure is a part of SELECT_LEX, a pointer to this structure is 00235 assigned when an item is created (which happens mostly during parsing 00236 (sql_yacc.yy)), but the structure itself will be initialized after parsing 00237 is complete 00238 00239 TODO: move subquery of INSERT ... SELECT and CREATE ... SELECT to 00240 separate SELECT_LEX which allow to remove tricks of changing this 00241 structure before and after INSERT/CREATE and its SELECT to make correct 00242 field name resolution. 00243 */ 00244 struct Name_resolution_context: Sql_alloc 00245 { 00246 /* 00247 The name resolution context to search in when an Item cannot be 00248 resolved in this context (the context of an outer select) 00249 */ 00250 Name_resolution_context *outer_context; 00251 00252 /* 00253 List of tables used to resolve the items of this context. Usually these 00254 are tables from the FROM clause of SELECT statement. The exceptions are 00255 INSERT ... SELECT and CREATE ... SELECT statements, where SELECT 00256 subquery is not moved to a separate SELECT_LEX. For these types of 00257 statements we have to change this member dynamically to ensure correct 00258 name resolution of different parts of the statement. 00259 */ 00260 TABLE_LIST *table_list; 00261 /* 00262 In most cases the two table references below replace 'table_list' above 00263 for the purpose of name resolution. The first and last name resolution 00264 table references allow us to search only in a sub-tree of the nested 00265 join tree in a FROM clause. This is needed for NATURAL JOIN, JOIN ... USING 00266 and JOIN ... ON. 00267 */ 00268 TABLE_LIST *first_name_resolution_table; 00269 /* 00270 Last table to search in the list of leaf table references that begins 00271 with first_name_resolution_table. 00272 */ 00273 TABLE_LIST *last_name_resolution_table; 00274 00275 /* 00276 SELECT_LEX item belong to, in case of merged VIEW it can differ from 00277 SELECT_LEX where item was created, so we can't use table_list/field_list 00278 from there 00279 */ 00280 st_select_lex *select_lex; 00281 00282 /* 00283 Processor of errors caused during Item name resolving, now used only to 00284 hide underlying tables in errors about views (i.e. it substitute some 00285 errors for views) 00286 */ 00287 void (*error_processor)(THD *, void *); 00288 void *error_processor_data; 00289 00290 /* 00291 When TRUE items are resolved in this context both against the 00292 SELECT list and this->table_list. If FALSE, items are resolved 00293 only against this->table_list. 00294 */ 00295 bool resolve_in_select_list; 00296 00297 /* 00298 Security context of this name resolution context. It's used for views 00299 and is non-zero only if the view is defined with SQL SECURITY DEFINER. 00300 */ 00301 Security_context *security_ctx; 00302 00303 Name_resolution_context() 00304 :outer_context(0), table_list(0), select_lex(0), 00305 error_processor_data(0), 00306 security_ctx(0) 00307 {} 00308 00309 void init() 00310 { 00311 resolve_in_select_list= FALSE; 00312 error_processor= &dummy_error_processor; 00313 first_name_resolution_table= NULL; 00314 last_name_resolution_table= NULL; 00315 } 00316 00317 void resolve_in_table_list_only(TABLE_LIST *tables) 00318 { 00319 table_list= first_name_resolution_table= tables; 00320 resolve_in_select_list= FALSE; 00321 } 00322 00323 void process_error(THD *thd) 00324 { 00325 (*error_processor)(thd, error_processor_data); 00326 } 00327 }; 00328 00329 00330 /* 00331 Store and restore the current state of a name resolution context. 00332 */ 00333 00334 class Name_resolution_context_state 00335 { 00336 private: 00337 TABLE_LIST *save_table_list; 00338 TABLE_LIST *save_first_name_resolution_table; 00339 TABLE_LIST *save_next_name_resolution_table; 00340 bool save_resolve_in_select_list; 00341 00342 public: 00343 Name_resolution_context_state() {} /* Remove gcc warning */ 00344 TABLE_LIST *save_next_local; 00345 00346 public: 00347 /* Save the state of a name resolution context. */ 00348 void save_state(Name_resolution_context *context, TABLE_LIST *table_list) 00349 { 00350 save_table_list= context->table_list; 00351 save_first_name_resolution_table= context->first_name_resolution_table; 00352 save_next_name_resolution_table= (context->first_name_resolution_table) ? 00353 context->first_name_resolution_table-> 00354 next_name_resolution_table : 00355 NULL; 00356 save_resolve_in_select_list= context->resolve_in_select_list; 00357 save_next_local= table_list->next_local; 00358 } 00359 00360 /* Restore a name resolution context from saved state. */ 00361 void restore_state(Name_resolution_context *context, TABLE_LIST *table_list) 00362 { 00363 table_list->next_local= save_next_local; 00364 context->table_list= save_table_list; 00365 context->first_name_resolution_table= save_first_name_resolution_table; 00366 if (context->first_name_resolution_table) 00367 context->first_name_resolution_table-> 00368 next_name_resolution_table= save_next_name_resolution_table; 00369 context->resolve_in_select_list= save_resolve_in_select_list; 00370 } 00371 }; 00372 00373 00374 /* 00375 This enum is used to report information about monotonicity of function 00376 represented by Item* tree. 00377 Monotonicity is defined only for Item* trees that represent table 00378 partitioning expressions (i.e. have no subselects/user vars/PS parameters 00379 etc etc). An Item* tree is assumed to have the same monotonicity properties 00380 as its correspoinding function F: 00381 00382 [signed] longlong F(field1, field2, ...) { 00383 put values of field_i into table record buffer; 00384 return item->val_int(); 00385 } 00386 00387 NOTE 00388 At the moment function monotonicity is not well defined (and so may be 00389 incorrect) for Item trees with parameters/return types that are different 00390 from INT_RESULT, may be NULL, or are unsigned. 00391 It will be possible to address this issue once the related partitioning bugs 00392 (BUG#16002, BUG#15447, BUG#13436) are fixed. 00393 */ 00394 00395 typedef enum monotonicity_info 00396 { 00397 NON_MONOTONIC, /* none of the below holds */ 00398 MONOTONIC_INCREASING, /* F() is unary and (x < y) => (F(x) <= F(y)) */ 00399 MONOTONIC_STRICT_INCREASING /* F() is unary and (x < y) => (F(x) < F(y)) */ 00400 } enum_monotonicity_info; 00401 00402 /*************************************************************************/ 00403 00404 class sp_rcontext; 00405 00406 00407 class Settable_routine_parameter 00408 { 00409 public: 00410 /* 00411 Set required privileges for accessing the parameter. 00412 00413 SYNOPSIS 00414 set_required_privilege() 00415 rw if 'rw' is true then we are going to read and set the 00416 parameter, so SELECT and UPDATE privileges might be 00417 required, otherwise we only reading it and SELECT 00418 privilege might be required. 00419 */ 00420 Settable_routine_parameter() {} 00421 virtual ~Settable_routine_parameter() {} 00422 virtual void set_required_privilege(bool rw) {}; 00423 00424 /* 00425 Set parameter value. 00426 00427 SYNOPSIS 00428 set_value() 00429 thd thread handle 00430 ctx context to which parameter belongs (if it is local 00431 variable). 00432 it item which represents new value 00433 00434 RETURN 00435 FALSE if parameter value has been set, 00436 TRUE if error has occured. 00437 */ 00438 virtual bool set_value(THD *thd, sp_rcontext *ctx, Item **it)= 0; 00439 }; 00440 00441 00442 typedef bool (Item::*Item_processor)(byte *arg); 00443 typedef Item* (Item::*Item_transformer) (byte *arg); 00444 typedef void (*Cond_traverser) (const Item *item, void *arg); 00445 00446 00447 class Item { 00448 Item(const Item &); /* Prevent use of these */ 00449 void operator=(Item &); 00450 public: 00451 static void *operator new(size_t size) 00452 { return (void*) sql_alloc((uint) size); } 00453 static void *operator new(size_t size, MEM_ROOT *mem_root) 00454 { return (void*) alloc_root(mem_root, (uint) size); } 00455 static void operator delete(void *ptr,size_t size) { TRASH(ptr, size); } 00456 static void operator delete(void *ptr, MEM_ROOT *mem_root) {} 00457 00458 enum Type {FIELD_ITEM= 0, FUNC_ITEM, SUM_FUNC_ITEM, STRING_ITEM, 00459 INT_ITEM, REAL_ITEM, NULL_ITEM, VARBIN_ITEM, 00460 COPY_STR_ITEM, FIELD_AVG_ITEM, DEFAULT_VALUE_ITEM, 00461 PROC_ITEM,COND_ITEM, REF_ITEM, FIELD_STD_ITEM, 00462 FIELD_VARIANCE_ITEM, INSERT_VALUE_ITEM, 00463 SUBSELECT_ITEM, ROW_ITEM, CACHE_ITEM, TYPE_HOLDER, 00464 PARAM_ITEM, TRIGGER_FIELD_ITEM, DECIMAL_ITEM, 00465 XPATH_NODESET, XPATH_NODESET_CMP, 00466 VIEW_FIXER_ITEM}; 00467 00468 enum cond_result { COND_UNDEF,COND_OK,COND_TRUE,COND_FALSE }; 00469 00470 enum traverse_order { POSTFIX, PREFIX }; 00471 00472 /* Reuse size, only used by SP local variable assignment, otherwize 0 */ 00473 uint rsize; 00474 00475 /* 00476 str_values's main purpose is to be used to cache the value in 00477 save_in_field 00478 */ 00479 String str_value; 00480 my_string name; /* Name from select */ 00481 /* Original item name (if it was renamed)*/ 00482 my_string orig_name; 00483 Item *next; 00484 uint32 max_length; 00485 uint name_length; /* Length of name */ 00486 uint8 marker, decimals; 00487 my_bool maybe_null; /* If item may be null */ 00488 my_bool null_value; /* if item is null */ 00489 my_bool unsigned_flag; 00490 my_bool with_sum_func; 00491 my_bool fixed; /* If item fixed with fix_fields */ 00492 my_bool is_autogenerated_name; /* indicate was name of this Item 00493 autogenerated or set by user */ 00494 DTCollation collation; 00495 my_bool with_subselect; /* If this item is a subselect or some 00496 of its arguments is or contains a 00497 subselect */ 00498 00499 // alloc & destruct is done as start of select using sql_alloc 00500 Item(); 00501 /* 00502 Constructor used by Item_field, Item_ref & aggregate (sum) functions. 00503 Used for duplicating lists in processing queries with temporary 00504 tables 00505 Also it used for Item_cond_and/Item_cond_or for creating 00506 top AND/OR structure of WHERE clause to protect it of 00507 optimisation changes in prepared statements 00508 */ 00509 Item(THD *thd, Item *item); 00510 virtual ~Item() 00511 { 00512 #ifdef EXTRA_DEBUG 00513 name=0; 00514 #endif 00515 } /*lint -e1509 */ 00516 void set_name(const char *str, uint length, CHARSET_INFO *cs); 00517 void rename(char *new_name); 00518 void init_make_field(Send_field *tmp_field,enum enum_field_types type); 00519 virtual void cleanup(); 00520 virtual void make_field(Send_field *field); 00521 Field *make_string_field(TABLE *table); 00522 virtual bool fix_fields(THD *, Item **); 00523 /* 00524 should be used in case where we are sure that we do not need 00525 complete fix_fields() procedure. 00526 */ 00527 inline void quick_fix_field() { fixed= 1; } 00528 /* Function returns 1 on overflow and -1 on fatal errors */ 00529 int save_in_field_no_warnings(Field *field, bool no_conversions); 00530 virtual int save_in_field(Field *field, bool no_conversions); 00531 virtual void save_org_in_field(Field *field) 00532 { (void) save_in_field(field, 1); } 00533 virtual int save_safe_in_field(Field *field) 00534 { return save_in_field(field, 1); } 00535 virtual bool send(Protocol *protocol, String *str); 00536 virtual bool eq(const Item *, bool binary_cmp) const; 00537 virtual Item_result result_type() const { return REAL_RESULT; } 00538 virtual Item_result cast_to_int_type() const { return result_type(); } 00539 virtual enum_field_types field_type() const; 00540 virtual enum Type type() const =0; 00541 00542 /* 00543 Return information about function monotonicity. See comment for 00544 enum_monotonicity_info for details. This function can only be called 00545 after fix_fields() call. 00546 */ 00547 virtual enum_monotonicity_info get_monotonicity_info() const 00548 { return NON_MONOTONIC; } 00549 00550 /* valXXX methods must return NULL or 0 or 0.0 if null_value is set. */ 00551 /* 00552 Return double precision floating point representation of item. 00553 00554 SYNOPSIS 00555 val_real() 00556 00557 RETURN 00558 In case of NULL value return 0.0 and set null_value flag to TRUE. 00559 If value is not null null_value flag will be reset to FALSE. 00560 */ 00561 virtual double val_real()=0; 00562 /* 00563 Return integer representation of item. 00564 00565 SYNOPSIS 00566 val_int() 00567 00568 RETURN 00569 In case of NULL value return 0 and set null_value flag to TRUE. 00570 If value is not null null_value flag will be reset to FALSE. 00571 */ 00572 virtual longlong val_int()=0; 00573 /* 00574 This is just a shortcut to avoid the cast. You should still use 00575 unsigned_flag to check the sign of the item. 00576 */ 00577 inline ulonglong val_uint() { return (ulonglong) val_int(); } 00578 /* 00579 Return string representation of this item object. 00580 00581 SYNOPSIS 00582 val_str() 00583 str an allocated buffer this or any nested Item object can use to 00584 store return value of this method. 00585 00586 NOTE 00587 Buffer passed via argument should only be used if the item itself 00588 doesn't have an own String buffer. In case when the item maintains 00589 it's own string buffer, it's preferable to return it instead to 00590 minimize number of mallocs/memcpys. 00591 The caller of this method can modify returned string, but only in case 00592 when it was allocated on heap, (is_alloced() is true). This allows 00593 the caller to efficiently use a buffer allocated by a child without 00594 having to allocate a buffer of it's own. The buffer, given to 00595 val_str() as argument, belongs to the caller and is later used by the 00596 caller at it's own choosing. 00597 A few implications from the above: 00598 - unless you return a string object which only points to your buffer 00599 but doesn't manages it you should be ready that it will be 00600 modified. 00601 - even for not allocated strings (is_alloced() == false) the caller 00602 can change charset (see Item_func_{typecast/binary}. XXX: is this 00603 a bug? 00604 - still you should try to minimize data copying and return internal 00605 object whenever possible. 00606 00607 RETURN 00608 In case of NULL value return 0 (NULL pointer) and set null_value flag 00609 to TRUE. 00610 If value is not null null_value flag will be reset to FALSE. 00611 */ 00612 virtual String *val_str(String *str)=0; 00613 /* 00614 Return decimal representation of item with fixed point. 00615 00616 SYNOPSIS 00617 val_decimal() 00618 decimal_buffer buffer which can be used by Item for returning value 00619 (but can be not) 00620 00621 NOTE 00622 Returned value should not be changed if it is not the same which was 00623 passed via argument. 00624 00625 RETURN 00626 Return pointer on my_decimal (it can be other then passed via argument) 00627 if value is not NULL (null_value flag will be reset to FALSE). 00628 In case of NULL value it return 0 pointer and set null_value flag 00629 to TRUE. 00630 */ 00631 virtual my_decimal *val_decimal(my_decimal *decimal_buffer)= 0; 00632 /* 00633 Return boolean value of item. 00634 00635 RETURN 00636 FALSE value is false or NULL 00637 TRUE value is true (not equal to 0) 00638 */ 00639 virtual bool val_bool(); 00640 virtual String *val_nodeset(String*) { return 0; } 00641 /* Helper functions, see item_sum.cc */ 00642 String *val_string_from_real(String *str); 00643 String *val_string_from_int(String *str); 00644 String *val_string_from_decimal(String *str); 00645 my_decimal *val_decimal_from_real(my_decimal *decimal_value); 00646 my_decimal *val_decimal_from_int(my_decimal *decimal_value); 00647 my_decimal *val_decimal_from_string(my_decimal *decimal_value); 00648 longlong val_int_from_decimal(); 00649 double val_real_from_decimal(); 00650 00651 virtual Field *get_tmp_table_field() { return 0; } 00652 /* This is also used to create fields in CREATE ... SELECT: */ 00653 virtual Field *tmp_table_field(TABLE *t_arg) { return 0; } 00654 virtual const char *full_name() const { return name ? name : "???"; } 00655 00656 /* 00657 *result* family of methods is analog of *val* family (see above) but 00658 return value of result_field of item if it is present. If Item have not 00659 result field, it return val(). This methods set null_value flag in same 00660 way as *val* methods do it. 00661 */ 00662 virtual double val_result() { return val_real(); } 00663 virtual longlong val_int_result() { return val_int(); } 00664 virtual String *str_result(String* tmp) { return val_str(tmp); } 00665 virtual my_decimal *val_decimal_result(my_decimal *val) 00666 { return val_decimal(val); } 00667 virtual bool val_bool_result() { return val_bool(); } 00668 00669 /* bit map of tables used by item */ 00670 virtual table_map used_tables() const { return (table_map) 0L; } 00671 /* 00672 Return table map of tables that can't be NULL tables (tables that are 00673 used in a context where if they would contain a NULL row generated 00674 by a LEFT or RIGHT join, the item would not be true). 00675 This expression is used on WHERE item to determinate if a LEFT JOIN can be 00676 converted to a normal join. 00677 Generally this function should return used_tables() if the function 00678 would return null if any of the arguments are null 00679 As this is only used in the beginning of optimization, the value don't 00680 have to be updated in update_used_tables() 00681 */ 00682 virtual table_map not_null_tables() const { return used_tables(); } 00683 /* 00684 Returns true if this is a simple constant item like an integer, not 00685 a constant expression. Used in the optimizer to propagate basic constants. 00686 */ 00687 virtual bool basic_const_item() const { return 0; } 00688 /* cloning of constant items (0 if it is not const) */ 00689 virtual Item *new_item() { return 0; } 00690 virtual cond_result eq_cmp_result() const { return COND_OK; } 00691 inline uint float_length(uint decimals_par) const 00692 { return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;} 00693 virtual uint decimal_precision() const; 00694 inline int decimal_int_part() const 00695 { return my_decimal_int_part(decimal_precision(), decimals); } 00696 /* 00697 Returns true if this is constant (during query execution, i.e. its value 00698 will not change until next fix_fields) and its value is known. 00699 */ 00700 virtual bool const_item() const { return used_tables() == 0; } 00701 /* 00702 Returns true if this is constant but its value may be not known yet. 00703 (Can be used for parameters of prep. stmts or of stored procedures.) 00704 */ 00705 virtual bool const_during_execution() const 00706 { return (used_tables() & ~PARAM_TABLE_BIT) == 0; } 00707 /* 00708 This is an essential method for correct functioning of VIEWS. 00709 To save a view in an .frm file we need its unequivocal 00710 definition in SQL that takes into account sql_mode and 00711 environmental settings. Currently such definition is restored 00712 by traversing through the parsed tree of a view and 00713 print()'ing SQL syntax of every node to a String buffer. This 00714 method is used to print the SQL definition of an item. The 00715 second use of this method is for EXPLAIN EXTENDED, to print 00716 the SQL of a query after all optimizations of the parsed tree 00717 have been done. 00718 */ 00719 virtual void print(String *str_arg) { str_arg->append(full_name()); } 00720 void print_item_w_name(String *); 00721 virtual void update_used_tables() {} 00722 virtual void split_sum_func(THD *thd, Item **ref_pointer_array, 00723 List<Item> &fields) {} 00724 /* Called for items that really have to be split */ 00725 void split_sum_func2(THD *thd, Item **ref_pointer_array, List<Item> &fields, 00726 Item **ref, bool skip_registered); 00727 virtual bool get_date(TIME *ltime,uint fuzzydate); 00728 virtual bool get_time(TIME *ltime); 00729 virtual bool get_date_result(TIME *ltime,uint fuzzydate) 00730 { return get_date(ltime,fuzzydate); } 00731 /* 00732 This function is used only in Item_func_isnull/Item_func_isnotnull 00733 (implementations of IS NULL/IS NOT NULL clauses). Item_func_is{not}null 00734 calls this method instead of one of val/result*() methods, which 00735 normally will set null_value. This allows to determine nullness of 00736 a complex expression without fully evaluating it. 00737 Any new item which can be NULL must implement this call. 00738 */ 00739 virtual bool is_null() { return 0; } 00740 00741 /* 00742 Inform the item that there will be no distinction between its result 00743 being FALSE or NULL. 00744 00745 NOTE 00746 This function will be called for eg. Items that are top-level AND-parts 00747 of the WHERE clause. Items implementing this function (currently 00748 Item_cond_and and subquery-related item) enable special optimizations 00749 when they are "top level". 00750 */ 00751 virtual void top_level_item() {} 00752 /* 00753 set field of temporary table for Item which can be switched on temporary 00754 table during query processing (grouping and so on) 00755 */ 00756 virtual void set_result_field(Field *field) {} 00757 virtual bool is_result_field() { return 0; } 00758 virtual bool is_bool_func() { return 0; } 00759 virtual void save_in_result_field(bool no_conversions) {} 00760 /* 00761 set value of aggregate function in case of no rows for grouping were found 00762 */ 00763 virtual void no_rows_in_result() {} 00764 virtual Item *copy_or_same(THD *thd) { return this; } 00765 virtual Item *copy_andor_structure(THD *thd) { return this; } 00766 virtual Item *real_item() { return this; } 00767 virtual Item *get_tmp_table_item(THD *thd) { return copy_or_same(thd); } 00768 00769 static CHARSET_INFO *default_charset(); 00770 virtual CHARSET_INFO *compare_collation() { return NULL; } 00771 00772 virtual bool walk(Item_processor processor, bool walk_subquery, byte *arg) 00773 { 00774 return (this->*processor)(arg); 00775 } 00776 00777 virtual Item* transform(Item_transformer transformer, byte *arg) 00778 { 00779 return (this->*transformer)(arg); 00780 } 00781 00782 virtual void traverse_cond(Cond_traverser traverser, 00783 void *arg, traverse_order order) 00784 { 00785 (*traverser)(this, arg); 00786 } 00787 00788 virtual bool remove_dependence_processor(byte * arg) { return 0; } 00789 virtual bool remove_fixed(byte * arg) { fixed= 0; return 0; } 00790 virtual bool cleanup_processor(byte *arg); 00791 virtual bool collect_item_field_processor(byte * arg) { return 0; } 00792 virtual bool find_item_in_field_list_processor(byte *arg) { return 0; } 00793 virtual bool change_context_processor(byte *context) { return 0; } 00794 virtual bool is_expensive_processor(byte *arg) { return 0; } 00795 virtual bool register_field_in_read_map(byte *arg) { return 0; } 00796 /* 00797 Check if a partition function is allowed 00798 SYNOPSIS 00799 check_partition_func_processor() 00800 bool_arg Return argument 00801 RETURN VALUE 00802 0 00803 DESCRIPTION 00804 check_partition_func_processor is used to check if a partition function 00805 uses an allowed function. The default is that an item is not allowed 00806 in a partition function. However all mathematical functions, string 00807 manipulation functions, date functions are allowed. Allowed functions 00808 can never depend on server version, they cannot depend on anything 00809 related to the environment. They can also only depend on a set of 00810 fields in the table itself. They cannot depend on other tables and 00811 cannot contain any queries and cannot contain udf's or similar. 00812 If a new Item class is defined and it inherits from a class that is 00813 allowed in a partition function then it is very important to consider 00814 whether this should be inherited to the new class. If not the function 00815 below should be defined in the new Item class. 00816 */ 00817 virtual bool check_partition_func_processor(byte *bool_arg) 00818 { *(bool *)bool_arg= FALSE; return 0; } 00819 00820 virtual Item *equal_fields_propagator(byte * arg) { return this; } 00821 virtual Item *set_no_const_sub(byte *arg) { return this; } 00822 virtual Item *replace_equal_field(byte * arg) { return this; } 00823 00824 /* 00825 For SP local variable returns pointer to Item representing its 00826 current value and pointer to current Item otherwise. 00827 */ 00828 virtual Item *this_item() { return this; } 00829 virtual const Item *this_item() const { return this; } 00830 00831 /* 00832 For SP local variable returns address of pointer to Item representing its 00833 current value and pointer passed via parameter otherwise. 00834 */ 00835 virtual Item **this_item_addr(THD *thd, Item **addr) { return addr; } 00836 00837 // Row emulation 00838 virtual uint cols() { return 1; } 00839 virtual Item* el(uint i) { return this; } 00840 virtual Item** addr(uint i) { return 0; } 00841 virtual bool check_cols(uint c); 00842 // It is not row => null inside is impossible 00843 virtual bool null_inside() { return 0; } 00844 // used in row subselects to get value of elements 00845 virtual void bring_value() {} 00846 00847 Field *tmp_table_field_from_field_type(TABLE *table, bool fixed_length); 00848 virtual Item_field *filed_for_view_update() { return 0; } 00849 00850 virtual Item *neg_transformer(THD *thd) { return NULL; } 00851 virtual Item *safe_charset_converter(CHARSET_INFO *tocs); 00852 void delete_self() 00853 { 00854 cleanup(); 00855 delete this; 00856 } 00857 00858 virtual bool is_splocal() { return 0; } /* Needed for error checking */ 00859 00860 /* 00861 Return Settable_routine_parameter interface of the Item. Return 0 00862 if this Item is not Settable_routine_parameter. 00863 */ 00864 virtual Settable_routine_parameter *get_settable_routine_parameter() 00865 { 00866 return 0; 00867 } 00868 /* 00869 result_as_longlong() must return TRUE for Items representing DATE/TIME 00870 functions and DATE/TIME table fields. 00871 Those Items have result_type()==STRING_RESULT (and not INT_RESULT), but 00872 their values should be compared as integers (because the integer 00873 representation is more precise than the string one). 00874 */ 00875 virtual bool result_as_longlong() { return FALSE; } 00876 }; 00877 00878 00879 class sp_head; 00880 00881 00882 /***************************************************************************** 00883 The class is a base class for representation of stored routine variables in 00884 the Item-hierarchy. There are the following kinds of SP-vars: 00885 - local variables (Item_splocal); 00886 - CASE expression (Item_case_expr); 00887 *****************************************************************************/ 00888 00889 class Item_sp_variable :public Item 00890 { 00891 protected: 00892 /* 00893 THD, which is stored in fix_fields() and is used in this_item() to avoid 00894 current_thd use. 00895 */ 00896 THD *m_thd; 00897 00898 public: 00899 LEX_STRING m_name; 00900 00901 public: 00902 #ifndef DBUG_OFF 00903 /* 00904 Routine to which this Item_splocal belongs. Used for checking if correct 00905 runtime context is used for variable handling. 00906 */ 00907 sp_head *m_sp; 00908 #endif 00909 00910 public: 00911 Item_sp_variable(char *sp_var_name_str, uint sp_var_name_length); 00912 00913 public: 00914 bool fix_fields(THD *thd, Item **); 00915 00916 double val_real(); 00917 longlong val_int(); 00918 String *val_str(String *sp); 00919 my_decimal *val_decimal(my_decimal *decimal_value); 00920 bool is_null(); 00921 00922 public: 00923 inline void make_field(Send_field *field); 00924 00925 inline bool const_item() const; 00926 00927 inline int save_in_field(Field *field, bool no_conversions); 00928 inline bool send(Protocol *protocol, String *str); 00929 }; 00930 00931 /***************************************************************************** 00932 Item_sp_variable inline implementation. 00933 *****************************************************************************/ 00934 00935 inline void Item_sp_variable::make_field(Send_field *field) 00936 { 00937 Item *it= this_item(); 00938 00939 if (name) 00940 it->set_name(name, (uint) strlen(name), system_charset_info); 00941 else 00942 it->set_name(m_name.str, m_name.length, system_charset_info); 00943 it->make_field(field); 00944 } 00945 00946 inline bool Item_sp_variable::const_item() const 00947 { 00948 return TRUE; 00949 } 00950 00951 inline int Item_sp_variable::save_in_field(Field *field, bool no_conversions) 00952 { 00953 return this_item()->save_in_field(field, no_conversions); 00954 } 00955 00956 inline bool Item_sp_variable::send(Protocol *protocol, String *str) 00957 { 00958 return this_item()->send(protocol, str); 00959 } 00960 00961 00962 /***************************************************************************** 00963 A reference to local SP variable (incl. reference to SP parameter), used in 00964 runtime. 00965 *****************************************************************************/ 00966 00967 class Item_splocal :public Item_sp_variable, 00968 private Settable_routine_parameter 00969 { 00970 uint m_var_idx; 00971 00972 Type m_type; 00973 Item_result m_result_type; 00974 00975 public: 00976 /* 00977 Position of this reference to SP variable in the statement (the 00978 statement itself is in sp_instr_stmt::m_query). 00979 This is valid only for references to SP variables in statements, 00980 excluding DECLARE CURSOR statement. It is used to replace references to SP 00981 variables with NAME_CONST calls when putting statements into the binary 00982 log. 00983 Value of 0 means that this object doesn't corresponding to reference to 00984 SP variable in query text. 00985 */ 00986 uint pos_in_query; 00987 00988 Item_splocal(const LEX_STRING &sp_var_name, uint sp_var_idx, 00989 enum_field_types sp_var_type, uint pos_in_q= 0); 00990 00991 bool is_splocal() { return 1; } /* Needed for error checking */ 00992 00993 Item *this_item(); 00994 const Item *this_item() const; 00995 Item **this_item_addr(THD *thd, Item **); 00996 00997 void print(String *str); 00998 00999 public: 01000 inline const LEX_STRING *my_name() const; 01001 01002 inline uint get_var_idx() const; 01003 01004 inline enum Type type() const; 01005 inline Item_result result_type() const; 01006 01007 private: 01008 bool set_value(THD *thd, sp_rcontext *ctx, Item **it); 01009 01010 public: 01011 Settable_routine_parameter *get_settable_routine_parameter() 01012 { 01013 return this; 01014 } 01015 }; 01016 01017 /***************************************************************************** 01018 Item_splocal inline implementation. 01019 *****************************************************************************/ 01020 01021 inline const LEX_STRING *Item_splocal::my_name() const 01022 { 01023 return &m_name; 01024 } 01025 01026 inline uint Item_splocal::get_var_idx() const 01027 { 01028 return m_var_idx; 01029 } 01030 01031 inline enum Item::Type Item_splocal::type() const 01032 { 01033 return m_type; 01034 } 01035 01036 inline Item_result Item_splocal::result_type() const 01037 { 01038 return m_result_type; 01039 } 01040 01041 01042 /***************************************************************************** 01043 A reference to case expression in SP, used in runtime. 01044 *****************************************************************************/ 01045 01046 class Item_case_expr :public Item_sp_variable 01047 { 01048 public: 01049 Item_case_expr(int case_expr_id); 01050 01051 public: 01052 Item *this_item(); 01053 const Item *this_item() const; 01054 Item **this_item_addr(THD *thd, Item **); 01055 01056 inline enum Type type() const; 01057 inline Item_result result_type() const; 01058 01059 public: 01060 /* 01061 NOTE: print() is intended to be used from views and for debug. 01062 Item_case_expr can not occur in views, so here it is only for debug 01063 purposes. 01064 */ 01065 void print(String *str); 01066 01067 private: 01068 int m_case_expr_id; 01069 }; 01070 01071 /***************************************************************************** 01072 Item_case_expr inline implementation. 01073 *****************************************************************************/ 01074 01075 inline enum Item::Type Item_case_expr::type() const 01076 { 01077 return this_item()->type(); 01078 } 01079 01080 inline Item_result Item_case_expr::result_type() const 01081 { 01082 return this_item()->result_type(); 01083 } 01084 01085 01086 /* 01087 NAME_CONST(given_name, const_value). 01088 This 'function' has all properties of the supplied const_value (which is 01089 assumed to be a literal constant), and the name given_name. 01090 01091 This is used to replace references to SP variables when we write PROCEDURE 01092 statements into the binary log. 01093 01094 TODO 01095 Together with Item_splocal and Item::this_item() we can actually extract 01096 common a base of this class and Item_splocal. Maybe it is possible to 01097 extract a common base with class Item_ref, too. 01098 */ 01099 01100 class Item_name_const : public Item 01101 { 01102 Item *value_item; 01103 Item *name_item; 01104 public: 01105 Item_name_const(Item *name, Item *val): value_item(val), name_item(name) 01106 { 01107 Item::maybe_null= TRUE; 01108 } 01109 01110 bool check_partition_func_processor(byte *bool_arg) { return 0; } 01111 bool fix_fields(THD *, Item **); 01112 01113 enum Type type() const; 01114 double val_real(); 01115 longlong val_int(); 01116 String *val_str(String *sp); 01117 my_decimal *val_decimal(my_decimal *); 01118 bool is_null(); 01119 void print(String *str); 01120 01121 Item_result result_type() const 01122 { 01123 return value_item->result_type(); 01124 } 01125 01126 bool const_item() const 01127 { 01128 return TRUE; 01129 } 01130 01131 int save_in_field(Field *field, bool no_conversions) 01132 { 01133 return value_item->save_in_field(field, no_conversions); 01134 } 01135 01136 bool send(Protocol *protocol, String *str) 01137 { 01138 return value_item->send(protocol, str); 01139 } 01140 }; 01141 01142 bool agg_item_collations(DTCollation &c, const char *name, 01143 Item **items, uint nitems, uint flags, int item_sep); 01144 bool agg_item_collations_for_comparison(DTCollation &c, const char *name, 01145 Item **items, uint nitems, uint flags); 01146 bool agg_item_charsets(DTCollation &c, const char *name, 01147 Item **items, uint nitems, uint flags, int item_sep); 01148 01149 01150 class Item_num: public Item 01151 { 01152 public: 01153 Item_num() {} /* Remove gcc warning */ 01154 virtual Item_num *neg()= 0; 01155 Item *safe_charset_converter(CHARSET_INFO *tocs); 01156 bool check_partition_func_processor(byte *bool_arg) { return 0;} 01157 }; 01158 01159 #define NO_CACHED_FIELD_INDEX ((uint)(-1)) 01160 01161 class st_select_lex; 01162 class Item_ident :public Item 01163 { 01164 protected: 01165 /* 01166 We have to store initial values of db_name, table_name and field_name 01167 to be able to restore them during cleanup() because they can be 01168 updated during fix_fields() to values from Field object and life-time 01169 of those is shorter than life-time of Item_field. 01170 */ 01171 const char *orig_db_name; 01172 const char *orig_table_name; 01173 const char *orig_field_name; 01174 01175 public: 01176 Name_resolution_context *context; 01177 const char *db_name; 01178 const char *table_name; 01179 const char *field_name; 01180 bool alias_name_used; /* true if item was resolved against alias */ 01181 /* 01182 Cached value of index for this field in table->field array, used by prep. 01183 stmts for speeding up their re-execution. Holds NO_CACHED_FIELD_INDEX 01184 if index value is not known. 01185 */ 01186 uint cached_field_index; 01187 /* 01188 Cached pointer to table which contains this field, used for the same reason 01189 by prep. stmt. too in case then we have not-fully qualified field. 01190 0 - means no cached value. 01191 */ 01192 TABLE_LIST *cached_table; 01193 st_select_lex *depended_from; 01194 Item_ident(Name_resolution_context *context_arg, 01195 const char *db_name_arg, const char *table_name_arg, 01196 const char *field_name_arg); 01197 Item_ident(THD *thd, Item_ident *item); 01198 const char *full_name() const; 01199 void cleanup(); 01200 bool remove_dependence_processor(byte * arg); 01201 void print(String *str); 01202 virtual bool change_context_processor(byte *cntx) 01203 { context= (Name_resolution_context *)cntx; return FALSE; } 01204 friend bool insert_fields(THD *thd, Name_resolution_context *context, 01205 const char *db_name, 01206 const char *table_name, List_iterator<Item> *it, 01207 bool any_privileges); 01208 }; 01209 01210 01211 class Item_ident_for_show :public Item 01212 { 01213 public: 01214 Field *field; 01215 const char *db_name; 01216 const char *table_name; 01217 01218 Item_ident_for_show(Field *par_field, const char *db_arg, 01219 const char *table_name_arg) 01220 :field(par_field), db_name(db_arg), table_name(table_name_arg) 01221 {} 01222 01223 enum Type type() const { return FIELD_ITEM; } 01224 double val_real() { return field->val_real(); } 01225 longlong val_int() { return field->val_int(); } 01226 String *val_str(String *str) { return field->val_str(str); } 01227 my_decimal *val_decimal(my_decimal *dec) { return field->val_decimal(dec); } 01228 void make_field(Send_field *tmp_field); 01229 }; 01230 01231 01232 class Item_equal; 01233 class COND_EQUAL; 01234 01235 class Item_field :public Item_ident 01236 { 01237 protected: 01238 void set_field(Field *field); 01239 public: 01240 Field *field,*result_field; 01241 Item_equal *item_equal; 01242 bool no_const_subst; 01243 /* 01244 if any_privileges set to TRUE then here real effective privileges will 01245 be stored 01246 */ 01247 uint have_privileges; 01248 /* field need any privileges (for VIEW creation) */ 01249 bool any_privileges; 01250 01251 Item_field(Name_resolution_context *context_arg, 01252 const char *db_arg,const char *table_name_arg, 01253 const char *field_name_arg); 01254 /* 01255 Constructor needed to process subselect with temporary tables (see Item) 01256 */ 01257 Item_field(THD *thd, Item_field *item); 01258 /* 01259 Constructor used inside setup_wild(), ensures that field, table, 01260 and database names will live as long as Item_field (this is important 01261 in prepared statements). 01262 */ 01263 Item_field(THD *thd, Name_resolution_context *context_arg, Field *field); 01264 /* 01265 If this constructor is used, fix_fields() won't work, because 01266 db_name, table_name and column_name are unknown. It's necessary to call 01267 reset_field() before fix_fields() for all fields created this way. 01268 */ 01269 Item_field(Field *field); 01270 enum Type type() const { return FIELD_ITEM; } 01271 bool eq(const Item *item, bool binary_cmp) const; 01272 double val_real(); 01273 longlong val_int(); 01274 my_decimal *val_decimal(my_decimal *); 01275 String *val_str(String*); 01276 double val_result(); 01277 longlong val_int_result(); 01278 String *str_result(String* tmp); 01279 my_decimal *val_decimal_result(my_decimal *); 01280 bool val_bool_result(); 01281 bool send(Protocol *protocol, String *str_arg); 01282 void reset_field(Field *f); 01283 bool fix_fields(THD *, Item **); 01284 void make_field(Send_field *tmp_field); 01285 int save_in_field(Field *field,bool no_conversions); 01286 void save_org_in_field(Field *field); 01287 table_map used_tables() const; 01288 enum Item_result result_type () const 01289 { 01290 return field->result_type(); 01291 } 01292 Item_result cast_to_int_type() const 01293 { 01294 return field->cast_to_int_type(); 01295 } 01296 enum_field_types field_type() const 01297 { 01298 return field->type(); 01299 } 01300 enum_monotonicity_info get_monotonicity_info() const 01301 { 01302 return MONOTONIC_STRICT_INCREASING; 01303 } 01304 Field *get_tmp_table_field() { return result_field; } 01305 Field *tmp_table_field(TABLE *t_arg) { return result_field; } 01306 bool get_date(TIME *ltime,uint fuzzydate); 01307 bool get_date_result(TIME *ltime,uint fuzzydate); 01308 bool get_time(TIME *ltime); 01309 bool is_null() { return field->is_null(); } 01310 Item *get_tmp_table_item(THD *thd); 01311 bool collect_item_field_processor(byte * arg); 01312 bool find_item_in_field_list_processor(byte *arg); 01313 bool register_field_in_read_map(byte *arg); 01314 bool check_partition_func_processor(byte *bool_arg) { return 0; } 01315 void cleanup(); 01316 bool result_as_longlong() 01317 { 01318 return field->can_be_compared_as_longlong(); 01319 } 01320 Item_equal *find_item_equal(COND_EQUAL *cond_equal); 01321 Item *equal_fields_propagator(byte *arg); 01322 Item *set_no_const_sub(byte *arg); 01323 Item *replace_equal_field(byte *arg); 01324 inline uint32 max_disp_length() { return field->max_length(); } 01325 Item_field *filed_for_view_update() { return this; } 01326 Item *safe_charset_converter(CHARSET_INFO *tocs); 01327 int fix_outer_field(THD *thd, Field **field, Item **reference); 01328 friend class Item_default_value; 01329 friend class Item_insert_value; 01330 friend class st_select_lex_unit; 01331 }; 01332 01333 class Item_null :public Item 01334 { 01335 public: 01336 Item_null(char *name_par=0) 01337 { 01338 maybe_null= null_value= TRUE; 01339 max_length= 0; 01340 name= name_par ? name_par : (char*) "NULL"; 01341 fixed= 1; 01342 collation.set(&my_charset_bin, DERIVATION_IGNORABLE); 01343 } 01344 enum Type type() const { return NULL_ITEM; } 01345 bool eq(const Item *item, bool binary_cmp) const; 01346 double val_real(); 01347 longlong val_int(); 01348 String *val_str(String *str); 01349 my_decimal *val_decimal(my_decimal *); 01350 int save_in_field(Field *field, bool no_conversions); 01351 int save_safe_in_field(Field *field); 01352 bool send(Protocol *protocol, String *str); 01353 enum Item_result result_type () const { return STRING_RESULT; } 01354 enum_field_types field_type() const { return MYSQL_TYPE_NULL; } 01355 /* to prevent drop fixed flag (no need parent cleanup call) */ 01356 void cleanup() {} 01357 bool basic_const_item() const { return 1; } 01358 Item *new_item() { return new Item_null(name); } 01359 bool is_null() { return 1; } 01360 void print(String *str) { str->append(STRING_WITH_LEN("NULL")); } 01361 Item *safe_charset_converter(CHARSET_INFO *tocs); 01362 bool check_partition_func_processor(byte *bool_arg) { return 0;} 01363 }; 01364 01365 class Item_null_result :public Item_null 01366 { 01367 public: 01368 Field *result_field; 01369 Item_null_result() : Item_null(), result_field(0) {} 01370 bool is_result_field() { return result_field != 0; } 01371 void save_in_result_field(bool no_conversions) 01372 { 01373 save_in_field(result_field, no_conversions); 01374 } 01375 bool check_partition_func_processor(byte *bool_arg) 01376 { *(bool *)bool_arg= FALSE; return 0; } 01377 }; 01378 01379 /* Item represents one placeholder ('?') of prepared statement */ 01380 01381 class Item_param :public Item 01382 { 01383 char cnvbuf[MAX_FIELD_WIDTH]; 01384 String cnvstr; 01385 Item *cnvitem; 01386 public: 01387 01388 enum enum_item_param_state 01389 { 01390 NO_VALUE, NULL_VALUE, INT_VALUE, REAL_VALUE, 01391 STRING_VALUE, TIME_VALUE, LONG_DATA_VALUE, 01392 DECIMAL_VALUE 01393 } state; 01394 01395 /* 01396 A buffer for string and long data values. Historically all allocated 01397 values returned from val_str() were treated as eligible to 01398 modification. I. e. in some cases Item_func_concat can append it's 01399 second argument to return value of the first one. Because of that we 01400 can't return the original buffer holding string data from val_str(), 01401 and have to have one buffer for data and another just pointing to 01402 the data. This is the latter one and it's returned from val_str(). 01403 Can not be declared inside the union as it's not a POD type. 01404 */ 01405 String str_value_ptr; 01406 my_decimal decimal_value; 01407 union 01408 { 01409 longlong integer; 01410 double real; 01411 /* 01412 Character sets conversion info for string values. 01413 Character sets of client and connection defined at bind time are used 01414 for all conversions, even if one of them is later changed (i.e. 01415 between subsequent calls to mysql_stmt_execute). 01416 */ 01417 struct CONVERSION_INFO 01418 { 01419 CHARSET_INFO *character_set_client; 01420 CHARSET_INFO *character_set_of_placeholder; 01421 /* 01422 This points at character set of connection if conversion 01423 to it is required (i. e. if placeholder typecode is not BLOB). 01424 Otherwise it's equal to character_set_client (to simplify 01425 check in convert_str_value()). 01426 */ 01427 CHARSET_INFO *final_character_set_of_str_value; 01428 } cs_info; 01429 TIME time; 01430 } value; 01431 01432 /* Cached values for virtual methods to save us one switch. */ 01433 enum Item_result item_result_type; 01434 enum Type item_type; 01435 01436 /* 01437 Used when this item is used in a temporary table. 01438 This is NOT placeholder metadata sent to client, as this value 01439 is assigned after sending metadata (in setup_one_conversion_function). 01440 For example in case of 'SELECT ?' you'll get MYSQL_TYPE_STRING both 01441 in result set and placeholders metadata, no matter what type you will 01442 supply for this placeholder in mysql_stmt_execute. 01443 */ 01444 enum enum_field_types param_type; 01445 /* 01446 Offset of placeholder inside statement text. Used to create 01447 no-placeholders version of this statement for the binary log. 01448 */ 01449 uint pos_in_query; 01450 01451 Item_param(uint pos_in_query_arg); 01452 01453 enum Item_result result_type () const { return item_result_type; } 01454 enum Type type() const { return item_type; } 01455 enum_field_types field_type() const { return param_type; } 01456 01457 double val_real(); 01458 longlong val_int(); 01459 my_decimal *val_decimal(my_decimal*); 01460 String *val_str(String*); 01461 bool get_time(TIME *tm); 01462 bool get_date(TIME *tm, uint fuzzydate); 01463 int save_in_field(Field *field, bool no_conversions); 01464 01465 void set_null(); 01466 void set_int(longlong i, uint32 max_length_arg); 01467 void set_double(double i); 01468 void set_decimal(const char *str, ulong length); 01469 bool set_str(const char *str, ulong length); 01470 bool set_longdata(const char *str, ulong length); 01471 void set_time(TIME *tm, timestamp_type type, uint32 max_length_arg); 01472 bool set_from_user_var(THD *thd, const user_var_entry *entry); 01473 void reset(); 01474 /* 01475 Assign placeholder value from bind data. 01476 Note, that 'len' has different semantics in embedded library (as we 01477 don't need to check that packet is not broken there). See 01478 sql_prepare.cc for details. 01479 */ 01480 void (*set_param_func)(Item_param *param, uchar **pos, ulong len); 01481 01482 const String *query_val_str(String *str) const; 01483 01484 bool convert_str_value(THD *thd); 01485 01486 /* 01487 If value for parameter was not set we treat it as non-const 01488 so noone will use parameters value in fix_fields still 01489 parameter is constant during execution. 01490 */ 01491 virtual table_map used_tables() const 01492 { return state != NO_VALUE ? (table_map)0 : PARAM_TABLE_BIT; } 01493 void print(String *str); 01494 bool is_null() 01495 { DBUG_ASSERT(state != NO_VALUE); return state == NULL_VALUE; } 01496 bool basic_const_item() const; 01497 /* 01498 This method is used to make a copy of a basic constant item when 01499 propagating constants in the optimizer. The reason to create a new 01500 item and not use the existing one is not precisely known (2005/04/16). 01501 Probably we are trying to preserve tree structure of items, in other 01502 words, avoid pointing at one item from two different nodes of the tree. 01503 Return a new basic constant item if parameter value is a basic 01504 constant, assert otherwise. This method is called only if 01505 basic_const_item returned TRUE. 01506 */ 01507 Item *safe_charset_converter(CHARSET_INFO *tocs); 01508 Item *new_item(); 01509 /* 01510 Implement by-value equality evaluation if parameter value 01511 is set and is a basic constant (integer, real or string). 01512 Otherwise return FALSE. 01513 */ 01514 bool eq(const Item *item, bool binary_cmp) const; 01515 }; 01516 01517 01518 class Item_int :public Item_num 01519 { 01520 public: 01521 longlong value; 01522 Item_int(int32 i,uint length=11) :value((longlong) i) 01523 { max_length=length; fixed= 1; } 01524 Item_int(longlong i,uint length=21) :value(i) 01525 { max_length=length; fixed= 1; } 01526 Item_int(ulonglong i, uint length= 21) :value((longlong)i) 01527 { max_length=length; fixed= 1; unsigned_flag= 1; } 01528 Item_int(const char *str_arg,longlong i,uint length) :value(i) 01529 { max_length=length; name=(char*) str_arg; fixed= 1; } 01530 Item_int(const char *str_arg, uint length=64); 01531 enum Type type() const { return INT_ITEM; } 01532 enum Item_result result_type () const { return INT_RESULT; } 01533 enum_field_types field_type() const { return MYSQL_TYPE_LONGLONG; } 01534 longlong val_int() { DBUG_ASSERT(fixed == 1); return value; } 01535 double val_real() { DBUG_ASSERT(fixed == 1); return (double) value; } 01536 my_decimal *val_decimal(my_decimal *); 01537 String *val_str(String*); 01538 int save_in_field(Field *field, bool no_conversions); 01539 bool basic_const_item() const { return 1; } 01540 Item *new_item() { return new Item_int(name,value,max_length); } 01541 // to prevent drop fixed flag (no need parent cleanup call) 01542 void cleanup() {} 01543 void print(String *str); 01544 Item_num *neg() { value= -value; return this; } 01545 uint decimal_precision() const 01546 { return (uint)(max_length - test(value < 0)); } 01547 bool eq(const Item *, bool binary_cmp) const; 01548 }; 01549 01550 01551 class Item_uint :public Item_int 01552 { 01553 public: 01554 Item_uint(const char *str_arg, uint length); 01555 Item_uint(ulonglong i) :Item_int((ulonglong) i, 10) {} 01556 Item_uint(const char *str_arg, longlong i, uint length); 01557 double val_real() 01558 { DBUG_ASSERT(fixed == 1); return ulonglong2double((ulonglong)value); } 01559 String *val_str(String*); 01560 Item *new_item() { return new Item_uint(name,max_length); } 01561 int save_in_field(Field *field, bool no_conversions); 01562 void print(String *str); 01563 Item_num *neg (); 01564 uint decimal_precision() const { return max_length; } 01565 }; 01566 01567 01568 /* decimal (fixed point) constant */ 01569 class Item_decimal :public Item_num 01570 { 01571 protected: 01572 my_decimal decimal_value; 01573 public: 01574 Item_decimal(const char *str_arg, uint length, CHARSET_INFO *charset); 01575 Item_decimal(const char *str, const my_decimal *val_arg, 01576 uint decimal_par, uint length); 01577 Item_decimal(my_decimal *value_par); 01578 Item_decimal(longlong val, bool unsig); 01579 Item_decimal(double val, int precision, int scale); 01580 Item_decimal(const char *bin, int precision, int scale); 01581 01582 enum Type type() const { return DECIMAL_ITEM; } 01583 enum Item_result result_type () const { return DECIMAL_RESULT; } 01584 enum_field_types field_type() const { return MYSQL_TYPE_NEWDECIMAL; } 01585 longlong val_int(); 01586 double val_real(); 01587 String *val_str(String*); 01588 my_decimal *val_decimal(my_decimal *val) { return &decimal_value; } 01589 int save_in_field(Field *field, bool no_conversions); 01590 bool basic_const_item() const { return 1; } 01591 Item *new_item() 01592 { 01593 return new Item_decimal(name, &decimal_value, decimals, max_length); 01594 } 01595 // to prevent drop fixed flag (no need parent cleanup call) 01596 void cleanup() {} 01597 void print(String *str); 01598 Item_num *neg() 01599 { 01600 my_decimal_neg(&decimal_value); 01601 unsigned_flag= !decimal_value.sign(); 01602 return this; 01603 } 01604 uint decimal_precision() const { return decimal_value.precision(); } 01605 bool eq(const Item *, bool binary_cmp) const; 01606 void set_decimal_value(my_decimal *value_par); 01607 }; 01608 01609 01610 class Item_float :public Item_num 01611 { 01612 char *presentation; 01613 public: 01614 double value; 01615 // Item_real() :value(0) {} 01616 Item_float(const char *str_arg, uint length); 01617 Item_float(const char *str,double val_arg,uint decimal_par,uint length) 01618 :value(val_arg) 01619 { 01620 presentation= name=(char*) str; 01621 decimals=(uint8) decimal_par; 01622 max_length=length; 01623 fixed= 1; 01624 } 01625 Item_float(double value_par) :presentation(0), value(value_par) { fixed= 1; } 01626 01627 int save_in_field(Field *field, bool no_conversions); 01628 enum Type type() const { return REAL_ITEM; } 01629 enum_field_types field_type() const { return MYSQL_TYPE_DOUBLE; } 01630 double val_real() { DBUG_ASSERT(fixed == 1); return value; } 01631 longlong val_int() 01632 { 01633 DBUG_ASSERT(fixed == 1); 01634 if (value <= (double) LONGLONG_MIN) 01635 { 01636 return LONGLONG_MIN; 01637 } 01638 else if (value >= (double) (ulonglong) LONGLONG_MAX) 01639 { 01640 return LONGLONG_MAX; 01641 } 01642 return (longlong) rint(value); 01643 } 01644 String *val_str(String*); 01645 my_decimal *val_decimal(my_decimal *); 01646 bool basic_const_item() const { return 1; } 01647 // to prevent drop fixed flag (no need parent cleanup call) 01648 void cleanup() {} 01649 Item *new_item() 01650 { return new Item_float(name, value, decimals, max_length); } 01651 Item_num *neg() { value= -value; return this; } 01652 void print(String *str); 01653 bool eq(const Item *, bool binary_cmp) const; 01654 }; 01655 01656 01657 class Item_static_float_func :public Item_float 01658 { 01659 const char *func_name; 01660 public: 01661 Item_static_float_func(const char *str, double val_arg, uint decimal_par, 01662 uint length) 01663 :Item_float(NullS, val_arg, decimal_par, length), func_name(str) 01664 {} 01665 void print(String *str) { str->append(func_name); } 01666 Item *safe_charset_converter(CHARSET_INFO *tocs); 01667 bool check_partition_func_processor(byte *bool_arg) 01668 { *(bool *)bool_arg= FALSE; return 0; } 01669 }; 01670 01671 01672 class Item_string :public Item 01673 { 01674 public: 01675 Item_string(const char *str,uint length, 01676 CHARSET_INFO *cs, Derivation dv= DERIVATION_COERCIBLE) 01677 { 01678 collation.set(cs, dv); 01679 str_value.set_or_copy_aligned(str,length,cs); 01680 /* 01681 We have to have a different max_length than 'length' here to 01682 ensure that we get the right length if we do use the item 01683 to create a new table. In this case max_length must be the maximum 01684 number of chars for a string of this type because we in create_field:: 01685 divide the max_length with mbmaxlen). 01686 */ 01687 max_length= str_value.numchars()*cs->mbmaxlen; 01688 set_name(str, length, cs); 01689 decimals=NOT_FIXED_DEC; 01690 // it is constant => can be used without fix_fields (and frequently used) 01691 fixed= 1; 01692 } 01693 /* Just create an item and do not fill string representation */ 01694 Item_string(CHARSET_INFO *cs, Derivation dv= DERIVATION_COERCIBLE) 01695 { 01696 collation.set(cs, dv); 01697 max_length= 0; 01698 set_name(NULL, 0, cs); 01699 decimals= NOT_FIXED_DEC; 01700 fixed= 1; 01701 } 01702 Item_string(const char *name_par, const char *str, uint length, 01703 CHARSET_INFO *cs, Derivation dv= DERIVATION_COERCIBLE) 01704 { 01705 collation.set(cs, dv); 01706 str_value.set_or_copy_aligned(str,length,cs); 01707 max_length= str_value.numchars()*cs->mbmaxlen; 01708 set_name(name_par, 0, cs); 01709 decimals=NOT_FIXED_DEC; 01710 // it is constant => can be used without fix_fields (and frequently used) 01711 fixed= 1; 01712 } 01713 /* 01714 This is used in stored procedures to avoid memory leaks and 01715 does a deep copy of its argument. 01716 */ 01717 void set_str_with_copy(const char *str_arg, uint length_arg) 01718 { 01719 str_value.copy(str_arg, length_arg, collation.collation); 01720 max_length= str_value.numchars() * collation.collation->mbmaxlen; 01721 } 01722 enum Type type() const { return STRING_ITEM; } 01723 double val_real(); 01724 longlong val_int(); 01725 String *val_str(String*) 01726 { 01727 DBUG_ASSERT(fixed == 1); 01728 return (String*) &str_value; 01729 } 01730 my_decimal *val_decimal(my_decimal *); 01731 int save_in_field(Field *field, bool no_conversions); 01732 enum Item_result result_type () const { return STRING_RESULT; } 01733 enum_field_types field_type() const { return MYSQL_TYPE_VARCHAR; } 01734 bool basic_const_item() const { return 1; } 01735 bool eq(const Item *item, bool binary_cmp) const; 01736 Item *new_item() 01737 { 01738 return new Item_string(name, str_value.ptr(), 01739 str_value.length(), collation.collation); 01740 } 01741 Item *safe_charset_converter(CHARSET_INFO *tocs); 01742 inline void append(char *str, uint length) { str_value.append(str, length); } 01743 void print(String *str); 01744 // to prevent drop fixed flag (no need parent cleanup call) 01745 void cleanup() {} 01746 bool check_partition_func_processor(byte *bool_arg) { return 0;} 01747 }; 01748 01749 01750 class Item_static_string_func :public Item_string 01751 { 01752 const char *func_name; 01753 public: 01754 Item_static_string_func(const char *name_par, const char *str, uint length, 01755 CHARSET_INFO *cs, 01756 Derivation dv= DERIVATION_COERCIBLE) 01757 :Item_string(NullS, str, length, cs, dv), func_name(name_par) 01758 {} 01759 Item *safe_charset_converter(CHARSET_INFO *tocs); 01760 void print(String *str) { str->append(func_name); } 01761 bool check_partition_func_processor(byte *bool_arg) 01762 { *(bool *)bool_arg= FALSE; return 0; } 01763 }; 01764 01765 01766 /* for show tables */ 01767 01768 class Item_datetime :public Item_string 01769 { 01770 public: 01771 Item_datetime(const char *item_name): Item_string(item_name,"",0, 01772 &my_charset_bin) 01773 { max_length=19;} 01774 enum_field_types field_type() const { return MYSQL_TYPE_DATETIME; } 01775 bool check_partition_func_processor(byte *bool_arg) 01776 { *(bool *)bool_arg= FALSE; return 0; } 01777 }; 01778 01779 class Item_empty_string :public Item_string 01780 { 01781 public: 01782 Item_empty_string(const char *header,uint length, CHARSET_INFO *cs= NULL) : 01783 Item_string("",0, cs ? cs : &my_charset_bin) 01784 { name=(char*) header; max_length= cs ? length * cs->mbmaxlen : length; } 01785 void make_field(Send_field *field); 01786 }; 01787 01788 class Item_return_int :public Item_int 01789 { 01790 enum_field_types int_field_type; 01791 public: 01792 Item_return_int(const char *name, uint length, 01793 enum_field_types field_type_arg) 01794 :Item_int(name, 0, length), int_field_type(field_type_arg) 01795 { 01796 unsigned_flag=1; 01797 } 01798 enum_field_types field_type() const { return int_field_type; } 01799 bool check_partition_func_processor(byte *bool_arg) 01800 { *(bool *)bool_arg= FALSE; return 0; } 01801 }; 01802 01803 01804 class Item_hex_string: public Item 01805 { 01806 public: 01807 Item_hex_string(): Item() {} 01808 Item_hex_string(const char *str,uint str_length); 01809 enum Type type() const { return VARBIN_ITEM; } 01810 double val_real() 01811 { DBUG_ASSERT(fixed == 1); return (double) Item_hex_string::val_int(); } 01812 longlong val_int(); 01813 bool basic_const_item() const { return 1; } 01814 String *val_str(String*) { DBUG_ASSERT(fixed == 1); return &str_value; } 01815 my_decimal *val_decimal(my_decimal *); 01816 int save_in_field(Field *field, bool no_conversions); 01817 enum Item_result result_type () const { return STRING_RESULT; } 01818 enum Item_result cast_to_int_type() const { return INT_RESULT; } 01819 enum_field_types field_type() const { return MYSQL_TYPE_VARCHAR; } 01820 // to prevent drop fixed flag (no need parent cleanup call) 01821 void cleanup() {} 01822 bool eq(const Item *item, bool binary_cmp) const; 01823 virtual Item *safe_charset_converter(CHARSET_INFO *tocs); 01824 bool check_partition_func_processor(byte *bool_arg) { return 0;} 01825 }; 01826 01827 01828 class Item_bin_string: public Item_hex_string 01829 { 01830 public: 01831 Item_bin_string(const char *str,uint str_length); 01832 }; 01833 01834 class Item_result_field :public Item /* Item with result field */ 01835 { 01836 public: 01837 Field *result_field; /* Save result here */ 01838 Item_result_field() :result_field(0) {} 01839 // Constructor used for Item_sum/Item_cond_and/or (see Item comment) 01840 Item_result_field(THD *thd, Item_result_field *item): 01841 Item(thd, item), result_field(item->result_field) 01842 {} 01843 ~Item_result_field() {} /* Required with gcc 2.95 */ 01844 Field *get_tmp_table_field() { return result_field; } 01845 Field *tmp_table_field(TABLE *t_arg) { return result_field; } 01846 table_map used_tables() const { return 1; } 01847 virtual void fix_length_and_dec()=0; 01848 void set_result_field(Field *field) { result_field= field; } 01849 bool is_result_field() { return 1; } 01850 void save_in_result_field(bool no_conversions) 01851 { 01852 save_in_field(result_field, no_conversions); 01853 } 01854 void cleanup(); 01855 }; 01856 01857 01858 class Item_ref :public Item_ident 01859 { 01860 protected: 01861 void set_properties(); 01862 public: 01863 enum Ref_Type { REF, DIRECT_REF, VIEW_REF }; 01864 Field *result_field; /* Save result here */ 01865 Item **ref; 01866 Item_ref(Name_resolution_context *context_arg, 01867 const char *db_arg, const char *table_name_arg, 01868 const char *field_name_arg) 01869 :Item_ident(context_arg, db_arg, table_name_arg, field_name_arg), 01870 result_field(0), ref(0) {} 01871 /* 01872 This constructor is used in two scenarios: 01873 A) *item = NULL 01874 No initialization is performed, fix_fields() call will be necessary. 01875 01876 B) *item points to an Item this Item_ref will refer to. This is 01877 used for GROUP BY. fix_fields() will not be called in this case, 01878 so we call set_properties to make this item "fixed". set_properties 01879 performs a subset of action Item_ref::fix_fields does, and this subset 01880 is enough for Item_ref's used in GROUP BY. 01881 01882 TODO we probably fix a superset of problems like in BUG#6658. Check this 01883 with Bar, and if we have a more broader set of problems like this. 01884 */ 01885 Item_ref(Name_resolution_context *context_arg, Item **item, 01886 const char *table_name_arg, const char *field_name_arg); 01887 01888 /* Constructor need to process subselect with temporary tables (see Item) */ 01889 Item_ref(THD *thd, Item_ref *item) 01890 :Item_ident(thd, item), result_field(item->result_field), ref(item->ref) {} 01891 enum Type type() const { return REF_ITEM; } 01892 bool eq(const Item *item, bool binary_cmp) const 01893 { 01894 Item *it= ((Item *) item)->real_item(); 01895 return ref && (*ref)->eq(it, binary_cmp); 01896 } 01897 double val_real(); 01898 longlong val_int(); 01899 my_decimal *val_decimal(my_decimal *); 01900 bool val_bool(); 01901 String *val_str(String* tmp); 01902 bool is_null(); 01903 bool get_date(TIME *ltime,uint fuzzydate); 01904 double val_result(); 01905 longlong val_int_result(); 01906 String *str_result(String* tmp); 01907 my_decimal *val_decimal_result(my_decimal *); 01908 bool val_bool_result(); 01909 bool send(Protocol *prot, String *tmp); 01910 void make_field(Send_field *field); 01911 bool fix_fields(THD *, Item **); 01912 int save_in_field(Field *field, bool no_conversions); 01913 void save_org_in_field(Field *field); 01914 enum Item_result result_type () const { return (*ref)->result_type(); } 01915 enum_field_types field_type() const { return (*ref)->field_type(); } 01916 Field *get_tmp_table_field() 01917 { return result_field ? result_field : (*ref)->get_tmp_table_field(); } 01918 Item *get_tmp_table_item(THD *thd) 01919 { 01920 return (result_field ? new Item_field(result_field) : 01921 (*ref)->get_tmp_table_item(thd)); 01922 } 01923 table_map used_tables() const 01924 { 01925 return depended_from ? OUTER_REF_TABLE_BIT : (*ref)->used_tables(); 01926 } 01927 table_map not_null_tables() const { return (*ref)->not_null_tables(); } 01928 void set_result_field(Field *field) { result_field= field; } 01929 bool is_result_field() { return 1; } 01930 void save_in_result_field(bool no_conversions) 01931 { 01932 (*ref)->save_in_field(result_field, no_conversions); 01933 } 01934 Item *real_item() 01935 { 01936 return ref ? (*ref)->real_item() : this; 01937 } 01938 bool walk(Item_processor processor, bool walk_subquery, byte *arg) 01939 { return (*ref)->walk(processor, walk_subquery, arg); } 01940 void print(String *str); 01941 bool result_as_longlong() 01942 { 01943 return (*ref)->result_as_longlong(); 01944 } 01945 void cleanup(); 01946 Item_field *filed_for_view_update() 01947 { return (*ref)->filed_for_view_update(); } 01948 virtual Ref_Type ref_type() { return REF; } 01949 }; 01950 01951 01952 /* 01953 The same as Item_ref, but get value from val_* family of method to get 01954 value of item on which it referred instead of result* family. 01955 */ 01956 class Item_direct_ref :public Item_ref 01957 { 01958 public: 01959 Item_direct_ref(Name_resolution_context *context_arg, Item **item, 01960 const char *table_name_arg, 01961 const char *field_name_arg) 01962 :Item_ref(context_arg, item, table_name_arg, field_name_arg) {} 01963 /* Constructor need to process subselect with temporary tables (see Item) */ 01964 Item_direct_ref(THD *thd, Item_direct_ref *item) : Item_ref(thd, item) {} 01965 01966 double val_real(); 01967 longlong val_int(); 01968 String *val_str(String* tmp); 01969 my_decimal *val_decimal(my_decimal *); 01970 bool val_bool(); 01971 bool is_null(); 01972 bool get_date(TIME *ltime,uint fuzzydate); 01973 virtual Ref_Type ref_type() { return DIRECT_REF; } 01974 }; 01975 01976 /* 01977 Class for view fields, the same as Item_direct_ref, but call fix_fields 01978 of reference if it is not called yet 01979 */ 01980 class Item_direct_view_ref :public Item_direct_ref 01981 { 01982 public: 01983 Item_direct_view_ref(Name_resolution_context *context_arg, Item **item, 01984 const char *table_name_arg, 01985 const char *field_name_arg) 01986 :Item_direct_ref(context_arg, item, table_name_arg, field_name_arg) {} 01987 /* Constructor need to process subselect with temporary tables (see Item) */ 01988 Item_direct_view_ref(THD *thd, Item_direct_ref *item) 01989 :Item_direct_ref(thd, item) {} 01990 01991 bool fix_fields(THD *, Item **); 01992 bool eq(const Item *item, bool binary_cmp) const; 01993 virtual Ref_Type ref_type() { return VIEW_REF; } 01994 }; 01995 01996 01997 class Item_in_subselect; 01998 01999 class Item_ref_null_helper: public Item_ref 02000 { 02001 protected: 02002 Item_in_subselect* owner; 02003 public: 02004 Item_ref_null_helper(Name_resolution_context *context_arg, 02005 Item_in_subselect* master, Item **item, 02006 const char *table_name_arg, const char *field_name_arg) 02007 :Item_ref(context_arg, item, table_name_arg, field_name_arg), 02008 owner(master) {} 02009 double val_real(); 02010 longlong val_int(); 02011 String* val_str(String* s); 02012 my_decimal *val_decimal(my_decimal *); 02013 bool val_bool(); 02014 bool get_date(TIME *ltime, uint fuzzydate); 02015 void print(String *str); 02016 /* 02017 we add RAND_TABLE_BIT to prevent moving this item from HAVING to WHERE 02018 */ 02019 table_map used_tables() const 02020 { 02021 return (depended_from ? 02022 OUTER_REF_TABLE_BIT : 02023 (*ref)->used_tables() | RAND_TABLE_BIT); 02024 } 02025 }; 02026 02027 /* 02028 The following class is used to optimize comparing of date and bigint columns 02029 We need to save the original item ('ref') to be able to call 02030 ref->save_in_field(). This is used to create index search keys. 02031 02032 An instance of Item_int_with_ref may have signed or unsigned integer value. 02033 02034 */ 02035 02036 class Item_int_with_ref :public Item_int 02037 { 02038 Item *ref; 02039 public: 02040 Item_int_with_ref(longlong i, Item *ref_arg, my_bool unsigned_arg) : 02041 Item_int(i), ref(ref_arg) 02042 { 02043 unsigned_flag= unsigned_arg; 02044 } 02045 int save_in_field(Field *field, bool no_conversions) 02046 { 02047 return ref->save_in_field(field, no_conversions); 02048 } 02049 Item *new_item(); 02050 virtual Item *real_item() { return ref; } 02051 bool check_partition_func_processor(byte *bool_arg) 02052 { *(bool *)bool_arg= FALSE; return 0; } 02053 }; 02054 02055 #ifdef MYSQL_SERVER 02056 #include "gstream.h" 02057 #include "spatial.h" 02058 #include "item_sum.h" 02059 #include "item_func.h" 02060 #include "item_row.h" 02061 #include "item_cmpfunc.h" 02062 #include "item_strfunc.h" 02063 #include "item_geofunc.h" 02064 #include "item_timefunc.h" 02065 #include "item_uniq.h" 02066 #include "item_subselect.h" 02067 #include "item_xmlfunc.h" 02068 #endif 02069 02070 class Item_copy_string :public Item 02071 { 02072 enum enum_field_types cached_field_type; 02073 public: 02074 Item *item; 02075 Item_copy_string(Item *i) :item(i) 02076 { 02077 null_value=maybe_null=item->maybe_null; 02078 decimals=item->decimals; 02079 max_length=item->max_length; 02080 name=item->name; 02081 cached_field_type= item->field_type(); 02082 } 02083 enum Type type() const { return COPY_STR_ITEM; } 02084 enum Item_result result_type () const { return STRING_RESULT; } 02085 enum_field_types field_type() const { return cached_field_type; } 02086 double val_real() 02087 { 02088 int err_not_used; 02089 char *end_not_used; 02090 return (null_value ? 0.0 : 02091 my_strntod(str_value.charset(), (char*) str_value.ptr(), 02092 str_value.length(), &end_not_used, &err_not_used)); 02093 } 02094 longlong val_int() 02095 { 02096 int err; 02097 return null_value ? LL(0) : my_strntoll(str_value.charset(),str_value.ptr(), 02098 str_value.length(),10, (char**) 0, 02099 &err); 02100 } 02101 String *val_str(String*); 02102 my_decimal *val_decimal(my_decimal *); 02103 void make_field(Send_field *field) { item->make_field(field); } 02104 void copy(); 02105 int save_in_field(Field *field, bool no_conversions); 02106 table_map used_tables() const { return (table_map) 1L; } 02107 bool const_item() const { return 0; } 02108 bool is_null() { return null_value; } 02109 }; 02110 02111 02112 class Cached_item :public Sql_alloc 02113 { 02114 public: 02115 my_bool null_value; 02116 Cached_item() :null_value(0) {} 02117 virtual bool cmp(void)=0; 02118 virtual ~Cached_item(); /*line -e1509 */ 02119 }; 02120 02121 class Cached_item_str :public Cached_item 02122 { 02123 Item *item; 02124 String value,tmp_value; 02125 public: 02126 Cached_item_str(THD *thd, Item *arg); 02127 bool cmp(void); 02128 ~Cached_item_str(); // Deallocate String:s 02129 }; 02130 02131 02132 class Cached_item_real :public Cached_item 02133 { 02134 Item *item; 02135 double value; 02136 public: 02137 Cached_item_real(Item *item_par) :item(item_par),value(0.0) {} 02138 bool cmp(void); 02139 }; 02140 02141 class Cached_item_int :public Cached_item 02142 { 02143 Item *item; 02144 longlong value; 02145 public: 02146 Cached_item_int(Item *item_par) :item(item_par),value(0) {} 02147 bool cmp(void); 02148 }; 02149 02150 02151 class Cached_item_decimal :public Cached_item 02152 { 02153 Item *item; 02154 my_decimal value; 02155 public: 02156 Cached_item_decimal(Item *item_par); 02157 bool cmp(void); 02158 }; 02159 02160 class Cached_item_field :public Cached_item 02161 { 02162 char *buff; 02163 Field *field; 02164 uint length; 02165 02166 public: 02167 Cached_item_field(Item_field *item) 02168 { 02169 field= item->field; 02170 buff= (char*) sql_calloc(length=field->pack_length()); 02171 } 02172 bool cmp(void); 02173 }; 02174 02175 class Item_default_value : public Item_field 02176 { 02177 public: 02178 Item *arg; 02179 Item_default_value(Name_resolution_context *context_arg) 02180 :Item_field(context_arg, (const char *)NULL, (const char *)NULL, 02181 (const char *)NULL), 02182 arg(NULL) {} 02183 Item_default_value(Name_resolution_context *context_arg, Item *a) 02184 :Item_field(context_arg, (const char *)NULL, (const char *)NULL, 02185 (const char *)NULL), 02186 arg(a) {} 02187 enum Type type() const { return DEFAULT_VALUE_ITEM; } 02188 bool eq(const Item *item, bool binary_cmp) const; 02189 bool fix_fields(THD *, Item **); 02190 void print(String *str); 02191 int save_in_field(Field *field_arg, bool no_conversions); 02192 table_map used_tables() const { return (table_map)0L; } 02193 02194 bool walk(Item_processor processor, bool walk_subquery, byte *args) 02195 { 02196 return arg->walk(processor, walk_subquery, args) || 02197 (this->*processor)(args); 02198 } 02199 02200 /* 02201 This method like the walk method traverses the item tree, but 02202 at the same time it can replace some nodes in the tree 02203 */ 02204 Item *transform(Item_transformer transformer, byte *args) 02205 { 02206 Item *new_item= arg->transform(transformer, args); 02207 if (!new_item) 02208 return 0; 02209 arg= new_item; 02210 return (this->*transformer)(args); 02211 } 02212 }; 02213 02214 /* 02215 Item_insert_value -- an implementation of VALUES() function. 02216 You can use the VALUES(col_name) function in the UPDATE clause 02217 to refer to column values from the INSERT portion of the INSERT 02218 ... UPDATE statement. In other words, VALUES(col_name) in the 02219 UPDATE clause refers to the value of col_name that would be 02220 inserted, had no duplicate-key conflict occurred. 02221 In all other places this function returns NULL. 02222 */ 02223 02224 class Item_insert_value : public Item_field 02225 { 02226 public: 02227 Item *arg; 02228 Item_insert_value(Name_resolution_context *context_arg, Item *a) 02229 :Item_field(context_arg, (const char *)NULL, (const char *)NULL, 02230 (const char *)NULL), 02231 arg(a) {} 02232 bool eq(const Item *item, bool binary_cmp) const; 02233 bool fix_fields(THD *, Item **); 02234 void print(String *str); 02235 int save_in_field(Field *field_arg, bool no_conversions) 02236 { 02237 return Item_field::save_in_field(field_arg, no_conversions); 02238 } 02239 table_map used_tables() const { return (table_map)0L; } 02240 02241 bool walk(Item_processor processor, bool walk_subquery, byte *args) 02242 { 02243 return arg->walk(processor, walk_subquery, args) || 02244 (this->*processor)(args); 02245 } 02246 }; 02247 02248 02249 /* 02250 We need this two enums here instead of sql_lex.h because 02251 at least one of them is used by Item_trigger_field interface. 02252 02253 Time when trigger is invoked (i.e. before or after row actually 02254 inserted/updated/deleted). 02255 */ 02256 enum trg_action_time_type 02257 { 02258 TRG_ACTION_BEFORE= 0, TRG_ACTION_AFTER= 1, TRG_ACTION_MAX 02259 }; 02260 02261 /* 02262 Event on which trigger is invoked. 02263 */ 02264 enum trg_event_type 02265 { 02266 TRG_EVENT_INSERT= 0 , TRG_EVENT_UPDATE= 1, TRG_EVENT_DELETE= 2, TRG_EVENT_MAX 02267 }; 02268 02269 class Table_triggers_list; 02270 02271 /* 02272 Represents NEW/OLD version of field of row which is 02273 changed/read in trigger. 02274 02275 Note: For this item main part of actual binding to Field object happens 02276 not during fix_fields() call (like for Item_field) but right after 02277 parsing of trigger definition, when table is opened, with special 02278 setup_field() call. On fix_fields() stage we simply choose one of 02279 two Field instances representing either OLD or NEW version of this 02280 field. 02281 */ 02282 class Item_trigger_field : public Item_field, 02283 private Settable_routine_parameter 02284 { 02285 public: 02286 /* Is this item represents row from NEW or OLD row ? */ 02287 enum row_version_type {OLD_ROW, NEW_ROW}; 02288 row_version_type row_version; 02289 /* Next in list of all Item_trigger_field's in trigger */ 02290 Item_trigger_field *next_trg_field; 02291 /* Index of the field in the TABLE::field array */ 02292 uint field_idx; 02293 /* Pointer to Table_trigger_list object for table of this trigger */ 02294 Table_triggers_list *triggers; 02295 02296 Item_trigger_field(Name_resolution_context *context_arg, 02297 row_version_type row_ver_arg, 02298 const char *field_name_arg, 02299 ulong priv, const bool ro) 02300 :Item_field(context_arg, 02301 (const char *)NULL, (const char *)NULL, field_name_arg), 02302 row_version(row_ver_arg), field_idx((uint)-1), original_privilege(priv), 02303 want_privilege(priv), table_grants(NULL), read_only (ro) 02304 {} 02305 void setup_field(THD *thd, TABLE *table, GRANT_INFO *table_grant_info); 02306 enum Type type() const { return TRIGGER_FIELD_ITEM; } 02307 bool eq(const Item *item, bool binary_cmp) const; 02308 bool fix_fields(THD *, Item **); 02309 void print(String *str); 02310 table_map used_tables() const { return (table_map)0L; } 02311 void cleanup(); 02312 02313 private: 02314 void set_required_privilege(bool rw); 02315 bool set_value(THD *thd, sp_rcontext *ctx, Item **it); 02316 02317 public: 02318 Settable_routine_parameter *get_settable_routine_parameter() 02319 { 02320 return (read_only ? 0 : this); 02321 } 02322 02323 bool set_value(THD *thd, Item **it) 02324 { 02325 return set_value(thd, NULL, it); 02326 } 02327 02328 private: 02329 /* 02330 'want_privilege' holds privileges required to perform operation on 02331 this trigger field (SELECT_ACL if we are going to read it and 02332 UPDATE_ACL if we are going to update it). It is initialized at 02333 parse time but can be updated later if this trigger field is used 02334 as OUT or INOUT parameter of stored routine (in this case 02335 set_required_privilege() is called to appropriately update 02336 want_privilege and cleanup() is responsible for restoring of 02337 original want_privilege once parameter's value is updated). 02338 */ 02339 ulong original_privilege; 02340 ulong want_privilege; 02341 GRANT_INFO *table_grants; 02342 /* 02343 Trigger field is read-only unless it belongs to the NEW row in a 02344 BEFORE INSERT of BEFORE UPDATE trigger. 02345 */ 02346 bool read_only; 02347 }; 02348 02349 02350 class Item_cache: public Item 02351 { 02352 protected: 02353 Item *example; 02354 table_map used_table_map; 02355 public: 02356 Item_cache(): example(0), used_table_map(0) {fixed= 1; null_value= 1;} 02357 02358 void set_used_tables(table_map map) { used_table_map= map; } 02359 02360 virtual bool allocate(uint i) { return 0; } 02361 virtual bool setup(Item *item) 02362 { 02363 example= item; 02364 max_length= item->max_length; 02365 decimals= item->decimals; 02366 collation.set(item->collation); 02367 unsigned_flag= item->unsigned_flag; 02368 return 0; 02369 }; 02370 virtual void store(Item *)= 0; 02371 enum Type type() const { return CACHE_ITEM; } 02372 static Item_cache* get_cache(Item_result type); 02373 table_map used_tables() const { return used_table_map; } 02374 virtual void keep_array() {} 02375 // to prevent drop fixed flag (no need parent cleanup call) 02376 void cleanup() {} 02377 void print(String *str); 02378 }; 02379 02380 02381 class Item_cache_int: public Item_cache 02382 { 02383 protected: 02384 longlong value; 02385 public: 02386 Item_cache_int(): Item_cache(), value(0) {} 02387 02388 void store(Item *item); 02389 double val_real() { DBUG_ASSERT(fixed == 1); return (double) value; } 02390 longlong val_int() { DBUG_ASSERT(fixed == 1); return value; } 02391 String* val_str(String *str); 02392 my_decimal *val_decimal(my_decimal *); 02393 enum Item_result result_type() const { return INT_RESULT; } 02394 }; 02395 02396 02397 class Item_cache_real: public Item_cache 02398 { 02399 double value; 02400 public: 02401 Item_cache_real(): Item_cache(), value(0) {} 02402 02403 void store(Item *item); 02404 double val_real() { DBUG_ASSERT(fixed == 1); return value; } 02405 longlong val_int(); 02406 String* val_str(String *str); 02407 my_decimal *val_decimal(my_decimal *); 02408 enum Item_result result_type() const { return REAL_RESULT; } 02409 }; 02410 02411 02412 class Item_cache_decimal: public Item_cache 02413 { 02414 protected: 02415 my_decimal decimal_value; 02416 public: 02417 Item_cache_decimal(): Item_cache() {} 02418 02419 void store(Item *item); 02420 double val_real(); 02421 longlong val_int(); 02422 String* val_str(String *str); 02423 my_decimal *val_decimal(my_decimal *); 02424 enum Item_result result_type() const { return DECIMAL_RESULT; } 02425 }; 02426 02427 02428 class Item_cache_str: public Item_cache 02429 { 02430 char buffer[STRING_BUFFER_USUAL_SIZE]; 02431 String *value, value_buff; 02432 public: 02433 Item_cache_str(): Item_cache(), value(0) { } 02434 02435 void store(Item *item); 02436 double val_real(); 02437 longlong val_int(); 02438 String* val_str(String *) { DBUG_ASSERT(fixed == 1); return value; } 02439 my_decimal *val_decimal(my_decimal *); 02440 enum Item_result result_type() const { return STRING_RESULT; } 02441 CHARSET_INFO *charset() const { return value->charset(); }; 02442 }; 02443 02444 class Item_cache_row: public Item_cache 02445 { 02446 Item_cache **values; 02447 uint item_count; 02448 bool save_array; 02449 public: 02450 Item_cache_row() 02451 :Item_cache(), values(0), item_count(2), save_array(0) {} 02452 02453 /* 02454 'allocate' used only in row transformer, to preallocate space for row 02455 cache. 02456 */ 02457 bool allocate(uint num); 02458 /* 02459 'setup' is needed only by row => it not called by simple row subselect 02460 (only by IN subselect (in subselect optimizer)) 02461 */ 02462 bool setup(Item *item); 02463 void store(Item *item); 02464 void illegal_method_call(const char *); 02465 void make_field(Send_field *) 02466 { 02467 illegal_method_call((const char*)"make_field"); 02468 }; 02469 double val_real() 02470 { 02471 illegal_method_call((const char*)"val"); 02472 return 0; 02473 }; 02474 longlong val_int() 02475 { 02476 illegal_method_call((const char*)"val_int"); 02477 return 0; 02478 }; 02479 String *val_str(String *) 02480 { 02481 illegal_method_call((const char*)"val_str"); 02482 return 0; 02483 }; 02484 my_decimal *val_decimal(my_decimal *val) 02485 { 02486 illegal_method_call((const char*)"val_decimal"); 02487 return 0; 02488 }; 02489 02490 enum Item_result result_type() const { return ROW_RESULT; } 02491 02492 uint cols() { return item_count; } 02493 Item* el(uint i) { return values[i]; } 02494 Item** addr(uint i) { return (Item **) (values + i); } 02495 bool check_cols(uint c); 02496 bool null_inside(); 02497 void bring_value(); 02498 void keep_array() { save_array= 1; } 02499 void cleanup() 02500 { 02501 DBUG_ENTER("Item_cache_row::cleanup"); 02502 Item_cache::cleanup(); 02503 if (save_array) 02504 bzero(values, item_count*sizeof(Item**)); 02505 else 02506 values= 0; 02507 DBUG_VOID_RETURN; 02508 } 02509 }; 02510 02511 02512 /* 02513 Item_type_holder used to store type. name, length of Item for UNIONS & 02514 derived tables. 02515 02516 Item_type_holder do not need cleanup() because its time of live limited by 02517 single SP/PS execution. 02518 */ 02519 class Item_type_holder: public Item 02520 { 02521 protected: 02522 TYPELIB *enum_set_typelib; 02523 enum_field_types fld_type; 02524 02525 void get_full_info(Item *item); 02526 02527 /* It is used to count decimal precision in join_types */ 02528 int prev_decimal_int_part; 02529 public: 02530 Item_type_holder(THD*, Item*); 02531 02532 Item_result result_type() const; 02533 enum_field_types field_type() const { return fld_type; }; 02534 enum Type type() const { return TYPE_HOLDER; } 02535 double val_real(); 02536 longlong val_int(); 02537 my_decimal *val_decimal(my_decimal *); 02538 String *val_str(String*); 02539 bool join_types(THD *thd, Item *); 02540 Field *make_field_by_type(TABLE *table); 02541 static uint32 display_length(Item *item); 02542 static enum_field_types get_real_type(Item *); 02543 }; 02544 02545 02546 class st_select_lex; 02547 void mark_select_range_as_dependent(THD *thd, 02548 st_select_lex *last_select, 02549 st_select_lex *current_sel, 02550 Field *found_field, Item *found_item, 02551 Item_ident *resolved_item); 02552 02553 extern Cached_item *new_Cached_item(THD *thd, Item *item); 02554 extern Item_result item_cmp_type(Item_result a,Item_result b); 02555 extern void resolve_const_item(THD *thd, Item **ref, Item *cmp_item); 02556 extern bool field_is_equal_to_item(Field *field,Item *item);
1.4.7

