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 /* Structs that defines the TABLE */ 00019 00020 class Item; /* Needed by ORDER */ 00021 class GRANT_TABLE; 00022 class st_select_lex_unit; 00023 class st_select_lex; 00024 class partition_info; 00025 class COND_EQUAL; 00026 class Security_context; 00027 00028 /* Order clause list element */ 00029 00030 typedef struct st_order { 00031 struct st_order *next; 00032 Item **item; /* Point at item in select fields */ 00033 Item *item_ptr; /* Storage for initial item */ 00034 Item **item_copy; /* For SPs; the original item ptr */ 00035 int counter; /* position in SELECT list, correct 00036 only if counter_used is true*/ 00037 bool asc; /* true if ascending */ 00038 bool free_me; /* true if item isn't shared */ 00039 bool in_field_list; /* true if in select field list */ 00040 bool counter_used; /* parameter was counter of columns */ 00041 Field *field; /* If tmp-table group */ 00042 char *buff; /* If tmp-table group */ 00043 table_map used, depend_map; 00044 } ORDER; 00045 00046 typedef struct st_grant_info 00047 { 00048 GRANT_TABLE *grant_table; 00049 uint version; 00050 ulong privilege; 00051 ulong want_privilege; 00052 /* 00053 Stores the requested access acl of top level tables list. Is used to 00054 check access rights to the underlying tables of a view. 00055 */ 00056 ulong orig_want_privilege; 00057 } GRANT_INFO; 00058 00059 enum tmp_table_type 00060 { 00061 NO_TMP_TABLE, TMP_TABLE, TRANSACTIONAL_TMP_TABLE, 00062 INTERNAL_TMP_TABLE, SYSTEM_TMP_TABLE 00063 }; 00064 00065 enum frm_type_enum 00066 { 00067 FRMTYPE_ERROR= 0, 00068 FRMTYPE_TABLE, 00069 FRMTYPE_VIEW 00070 }; 00071 00072 enum release_type { RELEASE_NORMAL, RELEASE_WAIT_FOR_DROP }; 00073 00074 typedef struct st_filesort_info 00075 { 00076 IO_CACHE *io_cache; /* If sorted through filebyte */ 00077 byte *addon_buf; /* Pointer to a buffer if sorted with fields */ 00078 uint addon_length; /* Length of the buffer */ 00079 struct st_sort_addon_field *addon_field; /* Pointer to the fields info */ 00080 void (*unpack)(struct st_sort_addon_field *, byte *); /* To unpack back */ 00081 byte *record_pointers; /* If sorted in memory */ 00082 ha_rows found_records; /* How many records in sort */ 00083 } FILESORT_INFO; 00084 00085 00086 /* 00087 Values in this enum are used to indicate how a tables TIMESTAMP field 00088 should be treated. It can be set to the current timestamp on insert or 00089 update or both. 00090 WARNING: The values are used for bit operations. If you change the 00091 enum, you must keep the bitwise relation of the values. For example: 00092 (int) TIMESTAMP_AUTO_SET_ON_BOTH must be equal to 00093 (int) TIMESTAMP_AUTO_SET_ON_INSERT | (int) TIMESTAMP_AUTO_SET_ON_UPDATE. 00094 We use an enum here so that the debugger can display the value names. 00095 */ 00096 enum timestamp_auto_set_type 00097 { 00098 TIMESTAMP_NO_AUTO_SET= 0, TIMESTAMP_AUTO_SET_ON_INSERT= 1, 00099 TIMESTAMP_AUTO_SET_ON_UPDATE= 2, TIMESTAMP_AUTO_SET_ON_BOTH= 3 00100 }; 00101 #define clear_timestamp_auto_bits(_target_, _bits_) \ 00102 (_target_)= (enum timestamp_auto_set_type)((int)(_target_) & ~(int)(_bits_)) 00103 00104 class Field_timestamp; 00105 class Field_blob; 00106 class Table_triggers_list; 00107 00108 /* 00109 This structure is shared between different table objects. There is one 00110 instance of table share per one table in the database. 00111 */ 00112 00113 typedef struct st_table_share 00114 { 00115 /* hash of field names (contains pointers to elements of field array) */ 00116 HASH name_hash; /* hash of field names */ 00117 MEM_ROOT mem_root; 00118 TYPELIB keynames; /* Pointers to keynames */ 00119 TYPELIB fieldnames; /* Pointer to fieldnames */ 00120 TYPELIB *intervals; /* pointer to interval info */ 00121 pthread_mutex_t mutex; /* For locking the share */ 00122 pthread_cond_t cond; /* To signal that share is ready */ 00123 struct st_table_share *next, /* Link to unused shares */ 00124 **prev; 00125 #ifdef NOT_YET 00126 struct st_table *open_tables; /* link to open tables */ 00127 #endif 00128 00129 /* The following is copied to each TABLE on OPEN */ 00130 Field **field; 00131 Field **found_next_number_field; 00132 Field *timestamp_field; /* Used only during open */ 00133 KEY *key_info; /* data of keys in database */ 00134 uint *blob_field; /* Index to blobs in Field arrray*/ 00135 00136 byte *default_values; /* row with default values */ 00137 LEX_STRING comment; /* Comment about table */ 00138 CHARSET_INFO *table_charset; /* Default charset of string fields */ 00139 00140 MY_BITMAP all_set; 00141 /* 00142 Key which is used for looking-up table in table cache and in the list 00143 of thread's temporary tables. Has the form of: 00144 "database_name\0table_name\0" + optional part for temporary tables. 00145 00146 Note that all three 'table_cache_key', 'db' and 'table_name' members 00147 must be set (and be non-zero) for tables in table cache. They also 00148 should correspond to each other. 00149 To ensure this one can use set_table_cache() methods. 00150 */ 00151 LEX_STRING table_cache_key; 00152 LEX_STRING db; /* Pointer to db */ 00153 LEX_STRING table_name; /* Table name (for open) */ 00154 LEX_STRING path; /* Path to .frm file (from datadir) */ 00155 LEX_STRING normalized_path; /* unpack_filename(path) */ 00156 LEX_STRING connect_string; 00157 key_map keys_in_use; /* Keys in use for table */ 00158 key_map keys_for_keyread; 00159 ha_rows min_rows, max_rows; /* create information */ 00160 ulong avg_row_length; /* create information */ 00161 ulong raid_chunksize; 00162 ulong version, flush_version, mysql_version; 00163 ulong timestamp_offset; /* Set to offset+1 of record */ 00164 ulong reclength; /* Recordlength */ 00165 00166 handlerton *db_type; /* table_type for handler */ 00167 enum row_type row_type; /* How rows are stored */ 00168 enum tmp_table_type tmp_table; 00169 00170 uint ref_count; /* How many TABLE objects uses this */ 00171 uint open_count; /* Number of tables in open list */ 00172 uint blob_ptr_size; /* 4 or 8 */ 00173 uint key_block_size; /* create key_block_size, if used */ 00174 uint null_bytes, last_null_bit_pos; 00175 uint fields; /* Number of fields */ 00176 uint rec_buff_length; /* Size of table->record[] buffer */ 00177 uint keys, key_parts; 00178 uint max_key_length, max_unique_length, total_key_length; 00179 uint uniques; /* Number of UNIQUE index */ 00180 uint null_fields; /* number of null fields */ 00181 uint blob_fields; /* number of blob fields */ 00182 uint timestamp_field_offset; /* Field number for timestamp field */ 00183 uint varchar_fields; /* number of varchar fields */ 00184 uint db_create_options; /* Create options from database */ 00185 uint db_options_in_use; /* Options in use */ 00186 uint db_record_offset; /* if HA_REC_IN_SEQ */ 00187 uint raid_type, raid_chunks; 00188 uint rowid_field_offset; /* Field_nr +1 to rowid field */ 00189 /* Index of auto-updated TIMESTAMP field in field array */ 00190 uint primary_key; 00191 uint next_number_index; 00192 uint next_number_key_offset; 00193 uint error, open_errno, errarg; /* error from open_table_def() */ 00194 uint column_bitmap_size; 00195 uchar frm_version; 00196 bool null_field_first; 00197 bool system; /* Set if system table (one record) */ 00198 bool crypted; /* If .frm file is crypted */ 00199 bool db_low_byte_first; /* Portable row format */ 00200 bool crashed; 00201 bool is_view; 00202 bool name_lock, replace_with_name_lock; 00203 bool waiting_on_cond; /* Protection against free */ 00204 ulong table_map_id; /* for row-based replication */ 00205 ulonglong table_map_version; 00206 00207 /* 00208 Cache for row-based replication table share checks that does not 00209 need to be repeated. Possible values are: -1 when cache value is 00210 not calculated yet, 0 when table *shall not* be replicated, 1 when 00211 table *may* be replicated. 00212 */ 00213 int cached_row_logging_check; 00214 00215 /* 00216 TRUE if this is a system table like 'mysql.proc', which we want to be 00217 able to open and lock even when we already have some tables open and 00218 locked. To avoid deadlocks we have to put certain restrictions on 00219 locking of this table for writing. FALSE - otherwise. 00220 */ 00221 bool system_table; 00222 /* 00223 This flag is set for the log tables. Used during FLUSH instances to skip 00224 log tables, while closing tables (since logs must be always available) 00225 */ 00226 bool log_table; 00227 #ifdef WITH_PARTITION_STORAGE_ENGINE 00228 bool auto_partitioned; 00229 const uchar *partition_info; 00230 uint partition_info_len; 00231 const uchar *part_state; 00232 uint part_state_len; 00233 handlerton *default_part_db_type; 00234 #endif 00235 00236 00237 /* 00238 Set share's table cache key and update its db and table name appropriately. 00239 00240 SYNOPSIS 00241 set_table_cache_key() 00242 key_buff Buffer with already built table cache key to be 00243 referenced from share. 00244 key_length Key length. 00245 00246 NOTES 00247 Since 'key_buff' buffer will be referenced from share it should has same 00248 life-time as share itself. 00249 This method automatically ensures that TABLE_SHARE::table_name/db have 00250 appropriate values by using table cache key as their source. 00251 */ 00252 00253 void set_table_cache_key(char *key_buff, uint key_length) 00254 { 00255 table_cache_key.str= key_buff; 00256 table_cache_key.length= key_length; 00257 /* 00258 Let us use the fact that the key is "db/0/table_name/0" + optional 00259 part for temporary tables. 00260 */ 00261 db.str= table_cache_key.str; 00262 db.length= strlen(db.str); 00263 table_name.str= db.str + db.length + 1; 00264 table_name.length= strlen(table_name.str); 00265 } 00266 00267 00268 /* 00269 Set share's table cache key and update its db and table name appropriately. 00270 00271 SYNOPSIS 00272 set_table_cache_key() 00273 key_buff Buffer to be used as storage for table cache key 00274 (should be at least key_length bytes). 00275 key Value for table cache key. 00276 key_length Key length. 00277 00278 NOTE 00279 Since 'key_buff' buffer will be used as storage for table cache key 00280 it should has same life-time as share itself. 00281 */ 00282 00283 void set_table_cache_key(char *key_buff, const char *key, uint key_length) 00284 { 00285 memcpy(key_buff, key, key_length); 00286 set_table_cache_key(key_buff, key_length); 00287 } 00288 00289 } TABLE_SHARE; 00290 00291 00292 /* Information for one open table */ 00293 00294 struct st_table { 00295 st_table() {} /* Remove gcc warning */ 00296 00297 TABLE_SHARE *s; 00298 handler *file; 00299 #ifdef NOT_YET 00300 struct st_table *used_next, **used_prev; /* Link to used tables */ 00301 #endif 00302 struct st_table *open_next, **open_prev; /* Link to open tables */ 00303 struct st_table *next, *prev; 00304 00305 THD *in_use; /* Which thread uses this */ 00306 Field **field; /* Pointer to fields */ 00307 00308 byte *record[2]; /* Pointer to records */ 00309 byte *write_row_record; /* Used as optimisation in 00310 THD::write_row */ 00311 byte *insert_values; /* used by INSERT ... UPDATE */ 00312 key_map quick_keys, used_keys, keys_in_use_for_query, merge_keys; 00313 KEY *key_info; /* data of keys in database */ 00314 00315 Field *next_number_field; /* Set if next_number is activated */ 00316 Field *found_next_number_field; /* Set on open */ 00317 Field_timestamp *timestamp_field; 00318 00319 /* Table's triggers, 0 if there are no of them */ 00320 Table_triggers_list *triggers; 00321 struct st_table_list *pos_in_table_list;/* Element referring to this table */ 00322 ORDER *group; 00323 const char *alias; /* alias or table name */ 00324 uchar *null_flags; 00325 my_bitmap_map *bitmap_init_value; 00326 MY_BITMAP def_read_set, def_write_set, tmp_set; /* containers */ 00327 MY_BITMAP *read_set, *write_set; /* Active column sets */ 00328 query_id_t query_id; 00329 00330 /* 00331 For each key that has quick_keys.is_set(key) == TRUE: estimate of #records 00332 and max #key parts that range access would use. 00333 */ 00334 ha_rows quick_rows[MAX_KEY]; 00335 00336 /* Bitmaps of key parts that =const for the entire join. */ 00337 key_part_map const_key_parts[MAX_KEY]; 00338 00339 uint quick_key_parts[MAX_KEY]; 00340 uint quick_n_ranges[MAX_KEY]; 00341 00342 /* 00343 Estimate of number of records that satisfy SARGable part of the table 00344 condition, or table->file->records if no SARGable condition could be 00345 constructed. 00346 This value is used by join optimizer as an estimate of number of records 00347 that will pass the table condition (condition that depends on fields of 00348 this table and constants) 00349 */ 00350 ha_rows quick_condition_rows; 00351 00352 /* 00353 If this table has TIMESTAMP field with auto-set property (pointed by 00354 timestamp_field member) then this variable indicates during which 00355 operations (insert only/on update/in both cases) we should set this 00356 field to current timestamp. If there are no such field in this table 00357 or we should not automatically set its value during execution of current 00358 statement then the variable contains TIMESTAMP_NO_AUTO_SET (i.e. 0). 00359 00360 Value of this variable is set for each statement in open_table() and 00361 if needed cleared later in statement processing code (see mysql_update() 00362 as example). 00363 */ 00364 timestamp_auto_set_type timestamp_field_type; 00365 table_map map; /* ID bit of table (1,2,4,8,16...) */ 00366 00367 uint lock_position; /* Position in MYSQL_LOCK.table */ 00368 uint lock_data_start; /* Start pos. in MYSQL_LOCK.locks */ 00369 uint lock_count; /* Number of locks */ 00370 uint tablenr,used_fields; 00371 uint temp_pool_slot; /* Used by intern temp tables */ 00372 uint status; /* What's in record[0] */ 00373 uint db_stat; /* mode of file as in handler.h */ 00374 /* number of select if it is derived table */ 00375 uint derived_select_number; 00376 int current_lock; /* Type of lock on table */ 00377 my_bool copy_blobs; /* copy_blobs when storing */ 00378 00379 /* 00380 0 or JOIN_TYPE_{LEFT|RIGHT}. Currently this is only compared to 0. 00381 If maybe_null !=0, this table is inner w.r.t. some outer join operation, 00382 and null_row may be true. 00383 */ 00384 uint maybe_null; 00385 /* 00386 If true, the current table row is considered to have all columns set to 00387 NULL, including columns declared as "not null" (see maybe_null). 00388 */ 00389 my_bool null_row; 00390 my_bool force_index; 00391 my_bool distinct,const_table,no_rows; 00392 my_bool key_read, no_keyread; 00393 my_bool locked_by_flush; 00394 my_bool locked_by_logger; 00395 my_bool locked_by_name; 00396 my_bool fulltext_searched; 00397 my_bool no_cache; 00398 /* To signal that we should reset query_id for tables and cols */ 00399 my_bool clear_query_id; 00400 my_bool auto_increment_field_not_null; 00401 my_bool insert_or_update; /* Can be used by the handler */ 00402 my_bool alias_name_used; /* true if table_name is alias */ 00403 my_bool get_fields_in_item_tree; /* Signal to fix_field */ 00404 00405 REGINFO reginfo; /* field connections */ 00406 MEM_ROOT mem_root; 00407 GRANT_INFO grant; 00408 FILESORT_INFO sort; 00409 #ifdef WITH_PARTITION_STORAGE_ENGINE 00410 partition_info *part_info; /* Partition related information */ 00411 bool no_partitions_used; /* If true, all partitions have been pruned away */ 00412 #endif 00413 00414 bool fill_item_list(List<Item> *item_list) const; 00415 void reset_item_list(List<Item> *item_list) const; 00416 void clear_column_bitmaps(void); 00417 void prepare_for_position(void); 00418 void mark_columns_used_by_index_no_reset(uint index, MY_BITMAP *map); 00419 void mark_columns_used_by_index(uint index); 00420 void restore_column_maps_after_mark_index(); 00421 void mark_auto_increment_column(void); 00422 void mark_columns_needed_for_update(void); 00423 void mark_columns_needed_for_delete(void); 00424 void mark_columns_needed_for_insert(void); 00425 inline void column_bitmaps_set(MY_BITMAP *read_set_arg, 00426 MY_BITMAP *write_set_arg) 00427 { 00428 read_set= read_set_arg; 00429 write_set= write_set_arg; 00430 if (file) 00431 file->column_bitmaps_signal(); 00432 } 00433 inline void column_bitmaps_set_no_signal(MY_BITMAP *read_set_arg, 00434 MY_BITMAP *write_set_arg) 00435 { 00436 read_set= read_set_arg; 00437 write_set= write_set_arg; 00438 } 00439 inline void use_all_columns() 00440 { 00441 column_bitmaps_set(&s->all_set, &s->all_set); 00442 } 00443 inline void default_column_bitmaps() 00444 { 00445 read_set= &def_read_set; 00446 write_set= &def_write_set; 00447 } 00448 00449 }; 00450 00451 00452 typedef struct st_foreign_key_info 00453 { 00454 LEX_STRING *forein_id; 00455 LEX_STRING *referenced_db; 00456 LEX_STRING *referenced_table; 00457 LEX_STRING *update_method; 00458 LEX_STRING *delete_method; 00459 List<LEX_STRING> foreign_fields; 00460 List<LEX_STRING> referenced_fields; 00461 } FOREIGN_KEY_INFO; 00462 00463 /* 00464 Make sure that the order of schema_tables and enum_schema_tables are the same. 00465 */ 00466 00467 enum enum_schema_tables 00468 { 00469 SCH_CHARSETS= 0, 00470 SCH_COLLATIONS, 00471 SCH_COLLATION_CHARACTER_SET_APPLICABILITY, 00472 SCH_COLUMNS, 00473 SCH_COLUMN_PRIVILEGES, 00474 SCH_ENGINES, 00475 SCH_EVENTS, 00476 SCH_FILES, 00477 SCH_KEY_COLUMN_USAGE, 00478 SCH_OPEN_TABLES, 00479 SCH_PARTITIONS, 00480 SCH_PLUGINS, 00481 SCH_PROCESSLIST, 00482 SCH_REFERENTIAL_CONSTRAINTS, 00483 SCH_PROCEDURES, 00484 SCH_SCHEMATA, 00485 SCH_SCHEMA_PRIVILEGES, 00486 SCH_STATISTICS, 00487 SCH_STATUS, 00488 SCH_TABLES, 00489 SCH_TABLE_CONSTRAINTS, 00490 SCH_TABLE_NAMES, 00491 SCH_TABLE_PRIVILEGES, 00492 SCH_TRIGGERS, 00493 SCH_USER_PRIVILEGES, 00494 SCH_VARIABLES, 00495 SCH_VIEWS 00496 }; 00497 00498 00499 typedef struct st_field_info 00500 { 00501 const char* field_name; 00502 uint field_length; 00503 enum enum_field_types field_type; 00504 int value; 00505 bool maybe_null; 00506 const char* old_name; 00507 } ST_FIELD_INFO; 00508 00509 00510 struct st_table_list; 00511 typedef class Item COND; 00512 00513 typedef struct st_schema_table 00514 { 00515 const char* table_name; 00516 ST_FIELD_INFO *fields_info; 00517 /* Create information_schema table */ 00518 TABLE *(*create_table) (THD *thd, struct st_table_list *table_list); 00519 /* Fill table with data */ 00520 int (*fill_table) (THD *thd, struct st_table_list *tables, COND *cond); 00521 /* Handle fileds for old SHOW */ 00522 int (*old_format) (THD *thd, struct st_schema_table *schema_table); 00523 int (*process_table) (THD *thd, struct st_table_list *tables, 00524 TABLE *table, bool res, const char *base_name, 00525 const char *file_name); 00526 int idx_field1, idx_field2; 00527 bool hidden; 00528 } ST_SCHEMA_TABLE; 00529 00530 00531 #define JOIN_TYPE_LEFT 1 00532 #define JOIN_TYPE_RIGHT 2 00533 00534 #define VIEW_ALGORITHM_UNDEFINED 0 00535 #define VIEW_ALGORITHM_TMPTABLE 1 00536 #define VIEW_ALGORITHM_MERGE 2 00537 00538 #define VIEW_SUID_INVOKER 0 00539 #define VIEW_SUID_DEFINER 1 00540 #define VIEW_SUID_DEFAULT 2 00541 00542 /* view WITH CHECK OPTION parameter options */ 00543 #define VIEW_CHECK_NONE 0 00544 #define VIEW_CHECK_LOCAL 1 00545 #define VIEW_CHECK_CASCADED 2 00546 00547 /* result of view WITH CHECK OPTION parameter check */ 00548 #define VIEW_CHECK_OK 0 00549 #define VIEW_CHECK_ERROR 1 00550 #define VIEW_CHECK_SKIP 2 00551 00552 struct st_lex; 00553 class select_union; 00554 class TMP_TABLE_PARAM; 00555 00556 Item *create_view_field(THD *thd, st_table_list *view, Item **field_ref, 00557 const char *name); 00558 00559 struct Field_translator 00560 { 00561 Item *item; 00562 const char *name; 00563 }; 00564 00565 00566 /* 00567 Column reference of a NATURAL/USING join. Since column references in 00568 joins can be both from views and stored tables, may point to either a 00569 Field (for tables), or a Field_translator (for views). 00570 */ 00571 00572 class Natural_join_column: public Sql_alloc 00573 { 00574 public: 00575 Field_translator *view_field; /* Column reference of merge view. */ 00576 Field *table_field; /* Column reference of table or temp view. */ 00577 st_table_list *table_ref; /* Original base table/view reference. */ 00578 /* 00579 True if a common join column of two NATURAL/USING join operands. Notice 00580 that when we have a hierarchy of nested NATURAL/USING joins, a column can 00581 be common at some level of nesting but it may not be common at higher 00582 levels of nesting. Thus this flag may change depending on at which level 00583 we are looking at some column. 00584 */ 00585 bool is_common; 00586 public: 00587 Natural_join_column(Field_translator *field_param, st_table_list *tab); 00588 Natural_join_column(Field *field_param, st_table_list *tab); 00589 const char *name(); 00590 Item *create_item(THD *thd); 00591 Field *field(); 00592 const char *table_name(); 00593 const char *db_name(); 00594 GRANT_INFO *grant(); 00595 }; 00596 00597 00598 /* 00599 Table reference in the FROM clause. 00600 00601 These table references can be of several types that correspond to 00602 different SQL elements. Below we list all types of TABLE_LISTs with 00603 the necessary conditions to determine when a TABLE_LIST instance 00604 belongs to a certain type. 00605 00606 1) table (TABLE_LIST::view == NULL) 00607 - base table 00608 (TABLE_LIST::derived == NULL) 00609 - subquery - TABLE_LIST::table is a temp table 00610 (TABLE_LIST::derived != NULL) 00611 - information schema table 00612 (TABLE_LIST::schema_table != NULL) 00613 NOTICE: for schema tables TABLE_LIST::field_translation may be != NULL 00614 2) view (TABLE_LIST::view != NULL) 00615 - merge (TABLE_LIST::effective_algorithm == VIEW_ALGORITHM_MERGE) 00616 also (TABLE_LIST::field_translation != NULL) 00617 - tmptable (TABLE_LIST::effective_algorithm == VIEW_ALGORITHM_TMPTABLE) 00618 also (TABLE_LIST::field_translation == NULL) 00619 3) nested table reference (TABLE_LIST::nested_join != NULL) 00620 - table sequence - e.g. (t1, t2, t3) 00621 TODO: how to distinguish from a JOIN? 00622 - general JOIN 00623 TODO: how to distinguish from a table sequence? 00624 - NATURAL JOIN 00625 (TABLE_LIST::natural_join != NULL) 00626 - JOIN ... USING 00627 (TABLE_LIST::join_using_fields != NULL) 00628 */ 00629 00630 typedef struct st_table_list 00631 { 00632 st_table_list() {} /* Remove gcc warning */ 00633 /* 00634 List of tables local to a subquery (used by SQL_LIST). Considers 00635 views as leaves (unlike 'next_leaf' below). Created at parse time 00636 in st_select_lex::add_table_to_list() -> table_list.link_in_list(). 00637 */ 00638 struct st_table_list *next_local; 00639 /* link in a global list of all queries tables */ 00640 struct st_table_list *next_global, **prev_global; 00641 char *db, *alias, *table_name, *schema_table_name; 00642 char *option; /* Used by cache index */ 00643 Item *on_expr; /* Used with outer join */ 00644 /* 00645 The structure of ON expression presented in the member above 00646 can be changed during certain optimizations. This member 00647 contains a snapshot of AND-OR structure of the ON expression 00648 made after permanent transformations of the parse tree, and is 00649 used to restore ON clause before every reexecution of a prepared 00650 statement or stored procedure. 00651 */ 00652 Item *prep_on_expr; 00653 COND_EQUAL *cond_equal; /* Used with outer join */ 00654 /* 00655 During parsing - left operand of NATURAL/USING join where 'this' is 00656 the right operand. After parsing (this->natural_join == this) iff 00657 'this' represents a NATURAL or USING join operation. Thus after 00658 parsing 'this' is a NATURAL/USING join iff (natural_join != NULL). 00659 */ 00660 struct st_table_list *natural_join; 00661 /* 00662 True if 'this' represents a nested join that is a NATURAL JOIN. 00663 For one of the operands of 'this', the member 'natural_join' points 00664 to the other operand of 'this'. 00665 */ 00666 bool is_natural_join; 00667 /* Field names in a USING clause for JOIN ... USING. */ 00668 List<String> *join_using_fields; 00669 /* 00670 Explicitly store the result columns of either a NATURAL/USING join or 00671 an operand of such a join. 00672 */ 00673 List<Natural_join_column> *join_columns; 00674 /* TRUE if join_columns contains all columns of this table reference. */ 00675 bool is_join_columns_complete; 00676 00677 /* 00678 List of nodes in a nested join tree, that should be considered as 00679 leaves with respect to name resolution. The leaves are: views, 00680 top-most nodes representing NATURAL/USING joins, subqueries, and 00681 base tables. All of these TABLE_LIST instances contain a 00682 materialized list of columns. The list is local to a subquery. 00683 */ 00684 struct st_table_list *next_name_resolution_table; 00685 /* Index names in a "... JOIN ... USE/IGNORE INDEX ..." clause. */ 00686 List<String> *use_index, *ignore_index; 00687 TABLE *table; /* opened table */ 00688 uint table_id; /* table id (from binlog) for opened table */ 00689 /* 00690 select_result for derived table to pass it from table creation to table 00691 filling procedure 00692 */ 00693 select_union *derived_result; 00694 /* 00695 Reference from aux_tables to local list entry of main select of 00696 multi-delete statement: 00697 delete t1 from t2,t1 where t1.a<'B' and t2.b=t1.b; 00698 here it will be reference of first occurrence of t1 to second (as you 00699 can see this lists can't be merged) 00700 */ 00701 st_table_list *correspondent_table; 00702 st_select_lex_unit *derived; /* SELECT_LEX_UNIT of derived table */ 00703 ST_SCHEMA_TABLE *schema_table; /* Information_schema table */ 00704 st_select_lex *schema_select_lex; 00705 bool is_schema_table_processed; 00706 /* 00707 True when the view field translation table is used to convert 00708 schema table fields for backwards compatibility with SHOW command. 00709 */ 00710 bool schema_table_reformed; 00711 TMP_TABLE_PARAM *schema_table_param; 00712 /* link to select_lex where this table was used */ 00713 st_select_lex *select_lex; 00714 st_lex *view; /* link on VIEW lex for merging */ 00715 Field_translator *field_translation; /* array of VIEW fields */ 00716 /* pointer to element after last one in translation table above */ 00717 Field_translator *field_translation_end; 00718 /* 00719 List (based on next_local) of underlying tables of this view. I.e. it 00720 does not include the tables of subqueries used in the view. Is set only 00721 for merged views. 00722 */ 00723 st_table_list *merge_underlying_list; 00724 /* 00725 - 0 for base tables 00726 - in case of the view it is the list of all (not only underlying 00727 tables but also used in subquery ones) tables of the view. 00728 */ 00729 List<st_table_list> *view_tables; 00730 /* most upper view this table belongs to */ 00731 st_table_list *belong_to_view; 00732 /* 00733 The view directly referencing this table 00734 (non-zero only for merged underlying tables of a view). 00735 */ 00736 st_table_list *referencing_view; 00737 /* 00738 Security context (non-zero only for tables which belong 00739 to view with SQL SECURITY DEFINER) 00740 */ 00741 Security_context *security_ctx; 00742 /* 00743 This view security context (non-zero only for views with 00744 SQL SECURITY DEFINER) 00745 */ 00746 Security_context *view_sctx; 00747 /* 00748 List of all base tables local to a subquery including all view 00749 tables. Unlike 'next_local', this in this list views are *not* 00750 leaves. Created in setup_tables() -> make_leaves_list(). 00751 */ 00752 bool allowed_show; 00753 st_table_list *next_leaf; 00754 Item *where; /* VIEW WHERE clause condition */ 00755 Item *check_option; /* WITH CHECK OPTION condition */ 00756 LEX_STRING query; /* text of (CRETE/SELECT) statement */ 00757 LEX_STRING md5; /* md5 of query text */ 00758 LEX_STRING source; /* source of CREATE VIEW */ 00759 LEX_STRING view_db; /* saved view database */ 00760 LEX_STRING view_name; /* saved view name */ 00761 LEX_STRING timestamp; /* GMT time stamp of last operation */ 00762 st_lex_user definer; /* definer of view */ 00763 ulonglong file_version; /* version of file's field set */ 00764 ulonglong updatable_view; /* VIEW can be updated */ 00765 ulonglong revision; /* revision control number */ 00766 ulonglong algorithm; /* 0 any, 1 tmp tables , 2 merging */ 00767 ulonglong view_suid; /* view is suid (TRUE dy default) */ 00768 ulonglong with_check; /* WITH CHECK OPTION */ 00769 /* 00770 effective value of WITH CHECK OPTION (differ for temporary table 00771 algorithm) 00772 */ 00773 uint8 effective_with_check; 00774 uint8 effective_algorithm; /* which algorithm was really used */ 00775 GRANT_INFO grant; 00776 /* data need by some engines in query cache*/ 00777 ulonglong engine_data; 00778 /* call back function for asking handler about caching in query cache */ 00779 qc_engine_callback callback_func; 00780 thr_lock_type lock_type; 00781 uint outer_join; /* Which join type */ 00782 uint shared; /* Used in multi-upd */ 00783 uint db_length; 00784 uint32 table_name_length; 00785 bool updatable; /* VIEW/TABLE can be updated now */ 00786 bool straight; /* optimize with prev table */ 00787 bool updating; /* for replicate-do/ignore table */ 00788 bool force_index; /* prefer index over table scan */ 00789 bool ignore_leaves; /* preload only non-leaf nodes */ 00790 table_map dep_tables; /* tables the table depends on */ 00791 table_map on_expr_dep_tables; /* tables on expression depends on */ 00792 struct st_nested_join *nested_join; /* if the element is a nested join */ 00793 st_table_list *embedding; /* nested join containing the table */ 00794 List<struct st_table_list> *join_list;/* join list the table belongs to */ 00795 bool cacheable_table; /* stop PS caching */ 00796 /* used in multi-upd/views privilege check */ 00797 bool table_in_first_from_clause; 00798 bool skip_temporary; /* this table shouldn't be temporary */ 00799 /* TRUE if this merged view contain auto_increment field */ 00800 bool contain_auto_increment; 00801 bool multitable_view; /* TRUE iff this is multitable view */ 00802 bool compact_view_format; /* Use compact format for SHOW CREATE VIEW */ 00803 /* view where processed */ 00804 bool where_processed; 00805 /* FRMTYPE_ERROR if any type is acceptable */ 00806 enum frm_type_enum required_type; 00807 handlerton *db_type; /* table_type for handler */ 00808 char timestamp_buffer[20]; /* buffer for timestamp (19+1) */ 00809 /* 00810 This TABLE_LIST object is just placeholder for prelocking, it will be 00811 used for implicit LOCK TABLES only and won't be used in real statement. 00812 */ 00813 bool prelocking_placeholder; 00814 00815 void calc_md5(char *buffer); 00816 void set_underlying_merge(); 00817 int view_check_option(THD *thd, bool ignore_failure); 00818 bool setup_underlying(THD *thd); 00819 void cleanup_items(); 00820 bool placeholder() {return derived || view; } 00821 void print(THD *thd, String *str); 00822 bool check_single_table(st_table_list **table, table_map map, 00823 st_table_list *view); 00824 bool set_insert_values(MEM_ROOT *mem_root); 00825 void hide_view_error(THD *thd); 00826 st_table_list *find_underlying_table(TABLE *table); 00827 st_table_list *first_leaf_for_name_resolution(); 00828 st_table_list *last_leaf_for_name_resolution(); 00829 bool is_leaf_for_name_resolution(); 00830 inline st_table_list *top_table() 00831 { return belong_to_view ? belong_to_view : this; } 00832 inline bool prepare_check_option(THD *thd) 00833 { 00834 bool res= FALSE; 00835 if (effective_with_check) 00836 res= prep_check_option(thd, effective_with_check); 00837 return res; 00838 } 00839 inline bool prepare_where(THD *thd, Item **conds, 00840 bool no_where_clause) 00841 { 00842 if (effective_algorithm == VIEW_ALGORITHM_MERGE) 00843 return prep_where(thd, conds, no_where_clause); 00844 return FALSE; 00845 } 00846 00847 void register_want_access(ulong want_access); 00848 bool prepare_security(THD *thd); 00849 #ifndef NO_EMBEDDED_ACCESS_CHECKS 00850 Security_context *find_view_security_context(THD *thd); 00851 bool prepare_view_securety_context(THD *thd); 00852 #endif 00853 /* 00854 Cleanup for re-execution in a prepared statement or a stored 00855 procedure. 00856 */ 00857 void reinit_before_use(THD *thd); 00858 00859 private: 00860 bool prep_check_option(THD *thd, uint8 check_opt_type); 00861 bool prep_where(THD *thd, Item **conds, bool no_where_clause); 00862 /* 00863 Cleanup for re-execution in a prepared statement or a stored 00864 procedure. 00865 */ 00866 } TABLE_LIST; 00867 00868 class Item; 00869 00870 /* 00871 Iterator over the fields of a generic table reference. 00872 */ 00873 00874 class Field_iterator: public Sql_alloc 00875 { 00876 public: 00877 Field_iterator() {} /* Remove gcc warning */ 00878 virtual ~Field_iterator() {} 00879 virtual void set(TABLE_LIST *)= 0; 00880 virtual void next()= 0; 00881 virtual bool end_of_fields()= 0; /* Return 1 at end of list */ 00882 virtual const char *name()= 0; 00883 virtual Item *create_item(THD *)= 0; 00884 virtual Field *field()= 0; 00885 }; 00886 00887 00888 /* 00889 Iterator over the fields of a base table, view with temporary 00890 table, or subquery. 00891 */ 00892 00893 class Field_iterator_table: public Field_iterator 00894 { 00895 Field **ptr; 00896 public: 00897 Field_iterator_table() :ptr(0) {} 00898 void set(TABLE_LIST *table) { ptr= table->table->field; } 00899 void set_table(TABLE *table) { ptr= table->field; } 00900 void next() { ptr++; } 00901 bool end_of_fields() { return *ptr == 0; } 00902 const char *name(); 00903 Item *create_item(THD *thd); 00904 Field *field() { return *ptr; } 00905 }; 00906 00907 00908 /* Iterator over the fields of a merge view. */ 00909 00910 class Field_iterator_view: public Field_iterator 00911 { 00912 Field_translator *ptr, *array_end; 00913 TABLE_LIST *view; 00914 public: 00915 Field_iterator_view() :ptr(0), array_end(0) {} 00916 void set(TABLE_LIST *table); 00917 void next() { ptr++; } 00918 bool end_of_fields() { return ptr == array_end; } 00919 const char *name(); 00920 Item *create_item(THD *thd); 00921 Item **item_ptr() {return &ptr->item; } 00922 Field *field() { return 0; } 00923 inline Item *item() { return ptr->item; } 00924 Field_translator *field_translator() { return ptr; } 00925 }; 00926 00927 00928 /* 00929 Field_iterator interface to the list of materialized fields of a 00930 NATURAL/USING join. 00931 */ 00932 00933 class Field_iterator_natural_join: public Field_iterator 00934 { 00935 List_iterator_fast<Natural_join_column> column_ref_it; 00936 Natural_join_column *cur_column_ref; 00937 public: 00938 Field_iterator_natural_join() :cur_column_ref(NULL) {} 00939 ~Field_iterator_natural_join() {} 00940 void set(TABLE_LIST *table); 00941 void next(); 00942 bool end_of_fields() { return !cur_column_ref; } 00943 const char *name() { return cur_column_ref->name(); } 00944 Item *create_item(THD *thd) { return cur_column_ref->create_item(thd); } 00945 Field *field() { return cur_column_ref->field(); } 00946 Natural_join_column *column_ref() { return cur_column_ref; } 00947 }; 00948 00949 00950 /* 00951 Generic iterator over the fields of an arbitrary table reference. 00952 00953 DESCRIPTION 00954 This class unifies the various ways of iterating over the columns 00955 of a table reference depending on the type of SQL entity it 00956 represents. If such an entity represents a nested table reference, 00957 this iterator encapsulates the iteration over the columns of the 00958 members of the table reference. 00959 00960 IMPLEMENTATION 00961 The implementation assumes that all underlying NATURAL/USING table 00962 references already contain their result columns and are linked into 00963 the list TABLE_LIST::next_name_resolution_table. 00964 */ 00965 00966 class Field_iterator_table_ref: public Field_iterator 00967 { 00968 TABLE_LIST *table_ref, *first_leaf, *last_leaf; 00969 Field_iterator_table table_field_it; 00970 Field_iterator_view view_field_it; 00971 Field_iterator_natural_join natural_join_it; 00972 Field_iterator *field_it; 00973 void set_field_iterator(); 00974 public: 00975 Field_iterator_table_ref() :field_it(NULL) {} 00976 void set(TABLE_LIST *table); 00977 void next(); 00978 bool end_of_fields() 00979 { return (table_ref == last_leaf && field_it->end_of_fields()); } 00980 const char *name() { return field_it->name(); } 00981 const char *table_name(); 00982 const char *db_name(); 00983 GRANT_INFO *grant(); 00984 Item *create_item(THD *thd) { return field_it->create_item(thd); } 00985 Field *field() { return field_it->field(); } 00986 Natural_join_column *get_or_create_column_ref(TABLE_LIST *parent_table_ref); 00987 Natural_join_column *get_natural_column_ref(); 00988 }; 00989 00990 00991 typedef struct st_nested_join 00992 { 00993 List<TABLE_LIST> join_list; /* list of elements in the nested join */ 00994 table_map used_tables; /* bitmap of tables in the nested join */ 00995 table_map not_null_tables; /* tables that rejects nulls */ 00996 struct st_join_table *first_nested;/* the first nested table in the plan */ 00997 /* 00998 Used to count tables in the nested join in 2 isolated places: 00999 1. In make_outerjoin_info(). 01000 2. check_interleaving_with_nj/restore_prev_nj_state (these are called 01001 by the join optimizer. 01002 Before each use the counters are zeroed by reset_nj_counters. 01003 */ 01004 uint counter; 01005 nested_join_map nj_map; /* Bit used to identify this nested join*/ 01006 } NESTED_JOIN; 01007 01008 01009 typedef struct st_changed_table_list 01010 { 01011 struct st_changed_table_list *next; 01012 char *key; 01013 uint32 key_length; 01014 } CHANGED_TABLE_LIST; 01015 01016 01017 typedef struct st_open_table_list{ 01018 struct st_open_table_list *next; 01019 char *db,*table; 01020 uint32 in_use,locked; 01021 } OPEN_TABLE_LIST; 01022 01023 typedef struct st_table_field_w_type 01024 { 01025 LEX_STRING name; 01026 LEX_STRING type; 01027 LEX_STRING cset; 01028 } TABLE_FIELD_W_TYPE; 01029 01030 01031 my_bool 01032 table_check_intact(TABLE *table, uint table_f_count, 01033 TABLE_FIELD_W_TYPE *table_def, time_t *last_create_time, 01034 int error_num); 01035 01036 static inline my_bitmap_map *tmp_use_all_columns(TABLE *table, 01037 MY_BITMAP *bitmap) 01038 { 01039 my_bitmap_map *old= bitmap->bitmap; 01040 bitmap->bitmap= table->s->all_set.bitmap; 01041 return old; 01042 } 01043 01044 01045 static inline void tmp_restore_column_map(MY_BITMAP *bitmap, 01046 my_bitmap_map *old) 01047 { 01048 bitmap->bitmap= old; 01049 } 01050 01051 /* The following is only needed for debugging */ 01052 01053 static inline my_bitmap_map *dbug_tmp_use_all_columns(TABLE *table, 01054 MY_BITMAP *bitmap) 01055 { 01056 #ifndef DBUG_OFF 01057 return tmp_use_all_columns(table, bitmap); 01058 #else 01059 return 0; 01060 #endif 01061 } 01062 01063 static inline void dbug_tmp_restore_column_map(MY_BITMAP *bitmap, 01064 my_bitmap_map *old) 01065 { 01066 #ifndef DBUG_OFF 01067 tmp_restore_column_map(bitmap, old); 01068 #endif 01069 }
1.4.7

