My Project
table.h
00001 #ifndef TABLE_INCLUDED
00002 #define TABLE_INCLUDED
00003 
00004 /* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; version 2 of the License.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
00018 
00019 #include "my_global.h"                          /* NO_EMBEDDED_ACCESS_CHECKS */
00020 #include "sql_plist.h"
00021 #include "sql_alloc.h"
00022 #include "mdl.h"
00023 #include "datadict.h"
00024 
00025 #ifndef MYSQL_CLIENT
00026 
00027 #include "hash.h"                               /* HASH */
00028 #include "handler.h"                /* row_type, ha_choice, handler */
00029 #include "mysql_com.h"              /* enum_field_types */
00030 #include "thr_lock.h"                  /* thr_lock_type */
00031 #include "filesort_utils.h"
00032 #include "parse_file.h"
00033 #include "table_id.h"
00034 
00035 /* Structs that defines the TABLE */
00036 
00037 class Item;                             /* Needed by ORDER */
00038 class Item_subselect;
00039 class Item_field;
00040 class GRANT_TABLE;
00041 class st_select_lex_unit;
00042 class st_select_lex;
00043 class partition_info;
00044 class COND_EQUAL;
00045 class Security_context;
00046 struct TABLE_LIST;
00047 class ACL_internal_schema_access;
00048 class ACL_internal_table_access;
00049 class Field;
00050 class Field_temporal_with_date_and_time;
00051 class Table_cache_element;
00052 
00053 /*
00054   Used to identify NESTED_JOIN structures within a join (applicable to
00055   structures representing outer joins that have not been simplified away).
00056 */
00057 typedef ulonglong nested_join_map;
00058 
00059 
00060 #define tmp_file_prefix "#sql"                  
00061 #define tmp_file_prefix_length 4
00062 #define TMP_TABLE_KEY_EXTRA 8
00063 
00079 enum enum_table_ref_type
00080 {
00082   TABLE_REF_NULL= 0,
00083   TABLE_REF_VIEW,
00084   TABLE_REF_BASE_TABLE,
00085   TABLE_REF_I_S_TABLE,
00086   TABLE_REF_TMP_TABLE
00087 };
00088 
00093 enum enum_ident_name_check
00094 {
00095   IDENT_NAME_OK,
00096   IDENT_NAME_WRONG,
00097   IDENT_NAME_TOO_LONG
00098 };
00099 
00100 /*************************************************************************/
00101 
00109 class Object_creation_ctx
00110 {
00111 public:
00112   Object_creation_ctx *set_n_backup(THD *thd);
00113 
00114   void restore_env(THD *thd, Object_creation_ctx *backup_ctx);
00115 
00116 protected:
00117   Object_creation_ctx() {}
00118   virtual Object_creation_ctx *create_backup_ctx(THD *thd) const = 0;
00119 
00120   virtual void change_env(THD *thd) const = 0;
00121 
00122 public:
00123   virtual ~Object_creation_ctx()
00124   { }
00125 };
00126 
00127 /*************************************************************************/
00128 
00134 class Default_object_creation_ctx : public Object_creation_ctx
00135 {
00136 public:
00137   const CHARSET_INFO *get_client_cs()
00138   {
00139     return m_client_cs;
00140   }
00141 
00142   const CHARSET_INFO *get_connection_cl()
00143   {
00144     return m_connection_cl;
00145   }
00146 
00147 protected:
00148   Default_object_creation_ctx(THD *thd);
00149 
00150   Default_object_creation_ctx(const CHARSET_INFO *client_cs,
00151                               const CHARSET_INFO *connection_cl);
00152 
00153 protected:
00154   virtual Object_creation_ctx *create_backup_ctx(THD *thd) const;
00155 
00156   virtual void change_env(THD *thd) const;
00157 
00158 protected:
00168   const CHARSET_INFO *m_client_cs;
00169 
00178   const CHARSET_INFO *m_connection_cl;
00179 };
00180 
00181 
00186 class View_creation_ctx : public Default_object_creation_ctx,
00187                           public Sql_alloc
00188 {
00189 public:
00190   static View_creation_ctx *create(THD *thd);
00191 
00192   static View_creation_ctx *create(THD *thd,
00193                                    TABLE_LIST *view);
00194 
00195 private:
00196   View_creation_ctx(THD *thd)
00197     : Default_object_creation_ctx(thd)
00198   { }
00199 };
00200 
00201 /*************************************************************************/
00202 
00205 typedef struct st_order {
00206   struct st_order *next;
00207   Item   **item;                        /* Point at item in select fields */
00208   Item   *item_ptr;                     /* Storage for initial item */
00209   int    counter;                       /* position in SELECT list, correct
00210                                            only if counter_used is true */
00211   enum enum_order {
00212     ORDER_NOT_RELEVANT,
00213     ORDER_ASC,
00214     ORDER_DESC
00215   };
00216 
00217   enum_order direction;                 /* Requested direction of ordering */
00218   bool   in_field_list;                 /* true if in select field list */
00219   bool   counter_used;                  /* parameter was counter of columns */
00226   bool   used_alias;
00227   Field  *field;                        /* If tmp-table group */
00228   char   *buff;                         /* If tmp-table group */
00229   table_map used, depend_map;
00230 } ORDER;
00231 
00238 struct st_grant_internal_info
00239 {
00241   bool m_schema_lookup_done;
00243   const ACL_internal_schema_access *m_schema_access;
00245   bool m_table_lookup_done;
00247   const ACL_internal_table_access *m_table_access;
00248 };
00249 typedef struct st_grant_internal_info GRANT_INTERNAL_INFO;
00250 
00265 typedef struct st_grant_info
00266 {
00273   GRANT_TABLE *grant_table;
00289   uint version;
00301   ulong privilege;
00306   ulong want_privilege;
00311   ulong orig_want_privilege;
00313   GRANT_INTERNAL_INFO m_internal;
00314 } GRANT_INFO;
00315 
00316 enum tmp_table_type
00317 {
00318   NO_TMP_TABLE, NON_TRANSACTIONAL_TMP_TABLE, TRANSACTIONAL_TMP_TABLE,
00319   INTERNAL_TMP_TABLE, SYSTEM_TMP_TABLE
00320 };
00321 enum release_type { RELEASE_NORMAL, RELEASE_WAIT_FOR_DROP };
00322 
00323 
00324 class Filesort_info
00325 {
00327   Filesort_buffer filesort_buffer;
00328 
00329 public:
00330   IO_CACHE *io_cache;           /* If sorted through filesort */
00331   uchar     *buffpek;           /* Buffer for buffpek structures */
00332   uint      buffpek_len;        /* Max number of buffpeks in the buffer */
00333   uchar     *addon_buf;         /* Pointer to a buffer if sorted with fields */
00334   size_t    addon_length;       /* Length of the buffer */
00335   struct st_sort_addon_field *addon_field;     /* Pointer to the fields info */
00336   void    (*unpack)(struct st_sort_addon_field *, uchar *); /* To unpack back */
00337   uchar     *record_pointers;    /* If sorted in memory */
00338   ha_rows   found_records;      /* How many records in sort */
00339 
00340   Filesort_info(): record_pointers(0) {};
00342   void sort_buffer(Sort_param *param, uint count)
00343   { filesort_buffer.sort_buffer(param, count); }
00344 
00348   uchar *get_record_buffer(uint idx)
00349   { return filesort_buffer.get_record_buffer(idx); }
00350 
00351   uchar **get_sort_keys()
00352   { return filesort_buffer.get_sort_keys(); }
00353 
00354   uchar **alloc_sort_buffer(uint num_records, uint record_length)
00355   { return filesort_buffer.alloc_sort_buffer(num_records, record_length); }
00356 
00357   void free_sort_buffer()
00358   { filesort_buffer.free_sort_buffer(); }
00359 
00360   void init_record_pointers()
00361   { filesort_buffer.init_record_pointers(); }
00362 
00363   size_t sort_buffer_size() const
00364   { return filesort_buffer.sort_buffer_size(); }
00365 };
00366 
00367 class Field_blob;
00368 class Table_triggers_list;
00369 
00373 enum enum_table_category
00374 {
00378   TABLE_UNKNOWN_CATEGORY=0,
00379 
00391   TABLE_CATEGORY_TEMPORARY=1,
00392 
00401   TABLE_CATEGORY_USER=2,
00402 
00413   TABLE_CATEGORY_SYSTEM=3,
00414 
00430   /*
00431     TODO: Fixing the performance issues of I_S will lead
00432     to I_S tables in the table cache, which should use
00433     this table type.
00434   */
00435   TABLE_CATEGORY_INFORMATION=4,
00436 
00457   TABLE_CATEGORY_LOG=5,
00458 
00479   TABLE_CATEGORY_PERFORMANCE=6,
00480 
00498   TABLE_CATEGORY_RPL_INFO=7
00499 };
00500 typedef enum enum_table_category TABLE_CATEGORY;
00501 
00502 TABLE_CATEGORY get_table_category(const LEX_STRING *db,
00503                                   const LEX_STRING *name);
00504 
00505 
00506 extern ulong refresh_version;
00507 
00508 typedef struct st_table_field_type
00509 {
00510   LEX_STRING name;
00511   LEX_STRING type;
00512   LEX_STRING cset;
00513 } TABLE_FIELD_TYPE;
00514 
00515 
00516 typedef struct st_table_field_def
00517 {
00518   uint count;
00519   const TABLE_FIELD_TYPE *field;
00520 } TABLE_FIELD_DEF;
00521 
00522 
00523 class Table_check_intact
00524 {
00525 protected:
00526   virtual void report_error(uint code, const char *fmt, ...)= 0;
00527 
00528 public:
00529   Table_check_intact() {}
00530   virtual ~Table_check_intact() {}
00531 
00533   bool check(TABLE *table, const TABLE_FIELD_DEF *table_def);
00534 };
00535 
00536 
00543 class Wait_for_flush : public MDL_wait_for_subgraph
00544 {
00545   MDL_context *m_ctx;
00546   TABLE_SHARE *m_share;
00547   uint m_deadlock_weight;
00548 public:
00549   Wait_for_flush(MDL_context *ctx_arg, TABLE_SHARE *share_arg,
00550                uint deadlock_weight_arg)
00551     : m_ctx(ctx_arg), m_share(share_arg),
00552       m_deadlock_weight(deadlock_weight_arg)
00553   {}
00554 
00555   MDL_context *get_ctx() const { return m_ctx; }
00556 
00557   virtual bool accept_visitor(MDL_wait_for_graph_visitor *dvisitor);
00558 
00559   virtual uint get_deadlock_weight() const;
00560 
00564   Wait_for_flush *next_in_share;
00565   Wait_for_flush **prev_in_share;
00566 };
00567 
00568 
00569 typedef I_P_List <Wait_for_flush,
00570                   I_P_List_adapter<Wait_for_flush,
00571                                    &Wait_for_flush::next_in_share,
00572                                    &Wait_for_flush::prev_in_share> >
00573                  Wait_for_flush_list;
00574 
00575 
00581 struct TABLE_SHARE
00582 {
00583   TABLE_SHARE() {}                    /* Remove gcc warning */
00584 
00586   TABLE_CATEGORY table_category;
00587 
00588   /* hash of field names (contains pointers to elements of field array) */
00589   HASH  name_hash;                      /* hash of field names */
00590   MEM_ROOT mem_root;
00591   TYPELIB keynames;                     /* Pointers to keynames */
00592   TYPELIB fieldnames;                   /* Pointer to fieldnames */
00593   TYPELIB *intervals;                   /* pointer to interval info */
00594   mysql_mutex_t LOCK_ha_data;           /* To protect access to ha_data */
00595   TABLE_SHARE *next, **prev;            /* Link to unused shares */
00604   Table_cache_element **cache_element;
00605 
00606   /* The following is copied to each TABLE on OPEN */
00607   Field **field;
00608   Field **found_next_number_field;
00609   KEY  *key_info;                       /* data of keys defined for the table */
00610   uint  *blob_field;                    /* Index to blobs in Field arrray*/
00611 
00612   uchar *default_values;                /* row with default values */
00613   LEX_STRING comment;                   /* Comment about table */
00614   const CHARSET_INFO *table_charset;    /* Default charset of string fields */
00615 
00616   MY_BITMAP all_set;
00617   /*
00618     Key which is used for looking-up table in table cache and in the list
00619     of thread's temporary tables. Has the form of:
00620       "database_name\0table_name\0" + optional part for temporary tables.
00621 
00622     Note that all three 'table_cache_key', 'db' and 'table_name' members
00623     must be set (and be non-zero) for tables in table cache. They also
00624     should correspond to each other.
00625     To ensure this one can use set_table_cache() methods.
00626   */
00627   LEX_STRING table_cache_key;
00628   LEX_STRING db;                        /* Pointer to db */
00629   LEX_STRING table_name;                /* Table name (for open) */
00630   LEX_STRING path;                      /* Path to .frm file (from datadir) */
00631   LEX_STRING normalized_path;           /* unpack_filename(path) */
00632   LEX_STRING connect_string;
00633 
00634   /* 
00635      Set of keys in use, implemented as a Bitmap.
00636      Excludes keys disabled by ALTER TABLE ... DISABLE KEYS.
00637   */
00638   key_map keys_in_use;
00639   key_map keys_for_keyread;
00640   ha_rows min_rows, max_rows;           /* create information */
00641   ulong   avg_row_length;               /* create information */
00647   ulong   version;
00648   ulong   mysql_version;                /* 0 if .frm is created before 5.0 */
00649   ulong   reclength;                    /* Recordlength */
00650 
00651   plugin_ref db_plugin;                 /* storage engine plugin */
00652   inline handlerton *db_type() const    /* table_type for handler */
00653   { 
00654     // DBUG_ASSERT(db_plugin);
00655     return db_plugin ? plugin_data(db_plugin, handlerton*) : NULL;
00656   }
00657   enum row_type row_type;               /* How rows are stored */
00658   enum tmp_table_type tmp_table;
00659 
00660   uint ref_count;                       /* How many TABLE objects uses this */
00661   uint key_block_size;                  /* create key_block_size, if used */
00662   uint stats_sample_pages;              /* number of pages to sample during
00663                                         stats estimation, if used, otherwise 0. */
00664   enum_stats_auto_recalc stats_auto_recalc; /* Automatic recalc of stats. */
00665   uint null_bytes, last_null_bit_pos;
00666   uint fields;                          /* Number of fields */
00667   uint rec_buff_length;                 /* Size of table->record[] buffer */
00668   uint keys;                            /* Number of keys defined for the table*/
00669   uint key_parts;                       /* Number of key parts of all keys
00670                                            defined for the table
00671                                         */
00672   uint max_key_length;                  /* Length of the longest key */
00673   uint max_unique_length;               /* Length of the longest unique key */
00674   uint total_key_length;
00675   uint uniques;                         /* Number of UNIQUE index */
00676   uint null_fields;                     /* number of null fields */
00677   uint blob_fields;                     /* number of blob fields */
00678   uint varchar_fields;                  /* number of varchar fields */
00679   uint db_create_options;               /* Create options from database */
00680   uint db_options_in_use;               /* Options in use */
00681   uint db_record_offset;                /* if HA_REC_IN_SEQ */
00682   uint rowid_field_offset;              /* Field_nr +1 to rowid field */
00683   /* Primary key index number, used in TABLE::key_info[] */
00684   uint primary_key;                     
00685   uint next_number_index;               /* autoincrement key number */
00686   uint next_number_key_offset;          /* autoinc keypart offset in a key */
00687   uint next_number_keypart;             /* autoinc keypart number in a key */
00688   uint error, open_errno, errarg;       /* error from open_table_def() */
00689   uint column_bitmap_size;
00690   uchar frm_version;
00691   bool null_field_first;
00692   bool system;                          /* Set if system table (one record) */
00693   bool crypted;                         /* If .frm file is crypted */
00694   bool db_low_byte_first;               /* Portable row format */
00695   bool crashed;
00696   bool is_view;
00697   Table_id table_map_id;                   /* for row-based replication */
00698 
00699   /*
00700     Cache for row-based replication table share checks that does not
00701     need to be repeated. Possible values are: -1 when cache value is
00702     not calculated yet, 0 when table *shall not* be replicated, 1 when
00703     table *may* be replicated.
00704   */
00705   int cached_row_logging_check;
00706 
00707   /*
00708     Storage media to use for this table (unless another storage
00709     media has been specified on an individual column - in versions
00710     where that is supported)
00711   */
00712   enum ha_storage_media default_storage_media;
00713 
00714   /* Name of the tablespace used for this table */
00715   char *tablespace;
00716 
00717 #ifdef WITH_PARTITION_STORAGE_ENGINE
00718   /* filled in when reading from frm */
00719   bool auto_partitioned;
00720   char *partition_info_str;
00721   uint  partition_info_str_len;
00722   uint  partition_info_buffer_size;
00723   handlerton *default_part_db_type;
00724 #endif
00725 
00736   const TABLE_FIELD_DEF *table_field_def_cache;
00737 
00739   Handler_share *ha_share;
00740 
00742   PSI_table_share *m_psi;
00743 
00747   Wait_for_flush_list m_flush_tickets;
00748 
00753   const File_parser *view_def;
00754 
00755 
00756   /*
00757     Set share's table cache key and update its db and table name appropriately.
00758 
00759     SYNOPSIS
00760       set_table_cache_key()
00761         key_buff    Buffer with already built table cache key to be
00762                     referenced from share.
00763         key_length  Key length.
00764 
00765     NOTES
00766       Since 'key_buff' buffer will be referenced from share it should has same
00767       life-time as share itself.
00768       This method automatically ensures that TABLE_SHARE::table_name/db have
00769       appropriate values by using table cache key as their source.
00770   */
00771 
00772   void set_table_cache_key(char *key_buff, uint key_length)
00773   {
00774     table_cache_key.str= key_buff;
00775     table_cache_key.length= key_length;
00776     /*
00777       Let us use the fact that the key is "db/0/table_name/0" + optional
00778       part for temporary tables.
00779     */
00780     db.str=            table_cache_key.str;
00781     db.length=         strlen(db.str);
00782     table_name.str=    db.str + db.length + 1;
00783     table_name.length= strlen(table_name.str);
00784   }
00785 
00786 
00787   /*
00788     Set share's table cache key and update its db and table name appropriately.
00789 
00790     SYNOPSIS
00791       set_table_cache_key()
00792         key_buff    Buffer to be used as storage for table cache key
00793                     (should be at least key_length bytes).
00794         key         Value for table cache key.
00795         key_length  Key length.
00796 
00797     NOTE
00798       Since 'key_buff' buffer will be used as storage for table cache key
00799       it should has same life-time as share itself.
00800   */
00801 
00802   void set_table_cache_key(char *key_buff, const char *key, uint key_length)
00803   {
00804     memcpy(key_buff, key, key_length);
00805     set_table_cache_key(key_buff, key_length);
00806   }
00807 
00808   inline bool honor_global_locks()
00809   {
00810     return ((table_category == TABLE_CATEGORY_USER)
00811             || (table_category == TABLE_CATEGORY_SYSTEM));
00812   }
00813 
00814   inline ulonglong get_table_def_version()
00815   {
00816     return table_map_id;
00817   }
00818 
00819 
00821   inline bool has_old_version() const
00822   {
00823     return version != refresh_version;
00824   }
00831   enum enum_table_ref_type get_table_ref_type() const
00832   {
00833     if (is_view)
00834       return TABLE_REF_VIEW;
00835     switch (tmp_table) {
00836     case NO_TMP_TABLE:
00837       return TABLE_REF_BASE_TABLE;
00838     case SYSTEM_TMP_TABLE:
00839       return TABLE_REF_I_S_TABLE;
00840     default:
00841       return TABLE_REF_TMP_TABLE;
00842     }
00843   }
00899   ulonglong get_table_ref_version() const
00900   {
00901     return (tmp_table == SYSTEM_TMP_TABLE) ? 0 : table_map_id.id();
00902   }
00903 
00904   bool visit_subgraph(Wait_for_flush *waiting_ticket,
00905                       MDL_wait_for_graph_visitor *gvisitor);
00906 
00907   bool wait_for_old_version(THD *thd, struct timespec *abstime,
00908                             uint deadlock_weight);
00910   void destroy();
00911 };
00912 
00913 
00920 class Blob_mem_storage: public Sql_alloc
00921 {
00922 private:
00923   MEM_ROOT storage;
00928   bool truncated_value;
00929 public:
00930   Blob_mem_storage() :truncated_value(false)
00931   {
00932     init_alloc_root(&storage, MAX_FIELD_VARCHARLENGTH, 0);
00933   }
00934   ~ Blob_mem_storage()
00935   {
00936     free_root(&storage, MYF(0));
00937   }
00938   void reset()
00939   {
00940     free_root(&storage, MYF(MY_MARK_BLOCKS_FREE));
00941     truncated_value= false;
00942   }
00953   char *store(const char *from, uint length)
00954   {
00955     return (char*) memdup_root(&storage, from, length);
00956   }
00957   void set_truncated_value(bool is_truncated_value)
00958   {
00959     truncated_value= is_truncated_value;
00960   }
00961   bool is_truncated_value() { return truncated_value; }
00962 };
00963 
00964 
00965 /* Information for one open table */
00966 enum index_hint_type
00967 {
00968   INDEX_HINT_IGNORE,
00969   INDEX_HINT_USE,
00970   INDEX_HINT_FORCE
00971 };
00972 
00973 /* Bitmap of table's fields */
00974 typedef Bitmap<MAX_FIELDS> Field_map;
00975 
00976 struct TABLE
00977 {
00978   TABLE() {}                               /* Remove gcc warning */
00979   /*
00980     Since TABLE instances are often cleared using memset(), do not
00981     add virtual members and do not inherit from TABLE.
00982     Otherwise memset() will start overwriting the vtable pointer.
00983   */
00984 
00985   TABLE_SHARE   *s;
00986   handler       *file;
00987   TABLE *next, *prev;
00988 
00989 private:
00997   TABLE *cache_next, **cache_prev;
00998 
00999   /*
01000     Give Table_cache_element access to the above two members to allow
01001     using them for linking TABLE objects in a list.
01002   */
01003   friend class Table_cache_element;
01004 
01005 public:
01006 
01007   THD   *in_use;                        /* Which thread uses this */
01008   Field **field;                        /* Pointer to fields */
01009 
01010   uchar *record[2];                     /* Pointer to records */
01011   uchar *write_row_record;              /* Used as optimisation in
01012                                            THD::write_row */
01013   uchar *insert_values;                  /* used by INSERT ... UPDATE */
01014   /* 
01015     Map of keys that can be used to retrieve all data from this table 
01016     needed by the query without reading the row.
01017   */
01018   key_map covering_keys;
01019   key_map quick_keys, merge_keys;
01020   key_map used_keys;  /* Indexes that cover all fields used by the query */
01021   
01022   /*
01023     possible_quick_keys is a superset of quick_keys to use with EXPLAIN of
01024     JOIN-less commands (single-table UPDATE and DELETE).
01025     
01026     When explaining regular JOINs, we use JOIN_TAB::keys to output the 
01027     "possible_keys" column value. However, it is not available for
01028     single-table UPDATE and DELETE commands, since they don't use JOIN
01029     optimizer at the top level. OTOH they directly use the range optimizer,
01030     that collects all keys usable for range access here.
01031   */
01032   key_map possible_quick_keys;
01033 
01034   /*
01035     A set of keys that can be used in the query that references this
01036     table.
01037 
01038     All indexes disabled on the table's TABLE_SHARE (see TABLE::s) will be 
01039     subtracted from this set upon instantiation. Thus for any TABLE t it holds
01040     that t.keys_in_use_for_query is a subset of t.s.keys_in_use. Generally we 
01041     must not introduce any new keys here (see setup_tables).
01042 
01043     The set is implemented as a bitmap.
01044   */
01045   key_map keys_in_use_for_query;
01046   /* Map of keys that can be used to calculate GROUP BY without sorting */
01047   key_map keys_in_use_for_group_by;
01048   /* Map of keys that can be used to calculate ORDER BY without sorting */
01049   key_map keys_in_use_for_order_by;
01050   KEY  *key_info;                       /* data of keys defined for the table */
01051 
01052   Field *next_number_field;             /* Set if next_number is activated */
01053   Field *found_next_number_field;       /* Set on open */
01054   Field *fts_doc_id_field;              /* Set if FTS_DOC_ID field is present */
01055 
01056   /* Table's triggers, 0 if there are no of them */
01057   Table_triggers_list *triggers;
01058   TABLE_LIST *pos_in_table_list;/* Element referring to this table */
01059   /* Position in thd->locked_table_list under LOCK TABLES */
01060   TABLE_LIST *pos_in_locked_tables;
01061   ORDER         *group;
01062   const char    *alias;                   /* alias or table name */
01063   uchar         *null_flags;
01064   my_bitmap_map *bitmap_init_value;
01065   MY_BITMAP     def_read_set, def_write_set, tmp_set; /* containers */
01066   MY_BITMAP     *read_set, *write_set;          /* Active column sets */
01067   /*
01068    The ID of the query that opened and is using this table. Has different
01069    meanings depending on the table type.
01070 
01071    Temporary tables:
01072 
01073    table->query_id is set to thd->query_id for the duration of a statement
01074    and is reset to 0 once it is closed by the same statement. A non-zero
01075    table->query_id means that a statement is using the table even if it's
01076    not the current statement (table is in use by some outer statement).
01077 
01078    Non-temporary tables:
01079 
01080    Under pre-locked or LOCK TABLES mode: query_id is set to thd->query_id
01081    for the duration of a statement and is reset to 0 once it is closed by
01082    the same statement. A non-zero query_id is used to control which tables
01083    in the list of pre-opened and locked tables are actually being used.
01084   */
01085   query_id_t    query_id;
01086 
01087   /* 
01088     For each key that has quick_keys.is_set(key) == TRUE: estimate of #records
01089     and max #key parts that range access would use.
01090   */
01091   ha_rows       quick_rows[MAX_KEY];
01092 
01093   /* Bitmaps of key parts that =const for the entire join. */
01094   key_part_map  const_key_parts[MAX_KEY];
01095 
01096   uint          quick_key_parts[MAX_KEY];
01097   uint          quick_n_ranges[MAX_KEY];
01098 
01099   /* 
01100     Estimate of number of records that satisfy SARGable part of the table
01101     condition, or table->file->records if no SARGable condition could be
01102     constructed.
01103     This value is used by join optimizer as an estimate of number of records
01104     that will pass the table condition (condition that depends on fields of 
01105     this table and constants)
01106   */
01107   ha_rows       quick_condition_rows;
01108   table_map     map;                    /* ID bit of table (1,2,4,8,16...) */
01109 
01110   uint          lock_position;          /* Position in MYSQL_LOCK.table */
01111   uint          lock_data_start;        /* Start pos. in MYSQL_LOCK.locks */
01112   uint          lock_count;             /* Number of locks */
01113   uint          tablenr,used_fields;
01114   uint          temp_pool_slot;         /* Used by intern temp tables */
01115   uint          db_stat;                /* mode of file as in handler.h */
01116   int           current_lock;           /* Type of lock on table */
01117 
01118   /*
01119     0 or JOIN_TYPE_{LEFT|RIGHT}. Currently this is only compared to 0.
01120     If maybe_null !=0, this table is inner w.r.t. some outer join operation,
01121     and null_row may be true.
01122   */
01123   uint maybe_null;
01124   /*
01125     If true, the current table row is considered to have all columns set to 
01126     NULL, including columns declared as "not null" (see maybe_null).
01127   */
01128   my_bool null_row;
01129 
01130   uint8   status;                       /* What's in record[0] */
01131   my_bool copy_blobs;                   /* copy_blobs when storing */
01132 
01133   /*
01134     TODO: Each of the following flags take up 8 bits. They can just as easily
01135     be put into one single unsigned long and instead of taking up 18
01136     bytes, it would take up 4.
01137   */
01138   my_bool force_index;
01139 
01144   my_bool force_index_order;
01145 
01150   my_bool force_index_group;
01151   my_bool distinct,const_table,no_rows;
01152 
01157   my_bool key_read;
01158   my_bool no_keyread;
01159   my_bool locked_by_logger;
01163   my_bool no_replicate;
01164   my_bool locked_by_name;
01165   my_bool fulltext_searched;
01166   my_bool no_cache;
01167   /* To signal that the table is associated with a HANDLER statement */
01168   my_bool open_by_handler;
01169   /*
01170     To indicate that a non-null value of the auto_increment field
01171     was provided by the user or retrieved from the current record.
01172     Used only in the MODE_NO_AUTO_VALUE_ON_ZERO mode.
01173   */
01174   my_bool auto_increment_field_not_null;
01175   my_bool insert_or_update;             /* Can be used by the handler */
01176   my_bool alias_name_used;              /* true if table_name is alias */
01177   my_bool get_fields_in_item_tree;      /* Signal to fix_field */
01183   my_bool m_needs_reopen;
01184 private:
01185   bool created; /* For tmp tables. TRUE <=> tmp table has been instantiated.*/
01186 public:
01187   uint max_keys; /* Size of allocated key_info array. */
01188 
01189   REGINFO reginfo;                      /* field connections */
01195   MEM_ROOT mem_root;
01201   Blob_mem_storage *blob_storage;
01202   GRANT_INFO grant;
01203   Filesort_info sort;
01204 #ifdef WITH_PARTITION_STORAGE_ENGINE
01205   partition_info *part_info;            /* Partition related information */
01206   /* If true, all partitions have been pruned away */
01207   bool all_partitions_pruned_away;
01208 #endif
01209   MDL_ticket *mdl_ticket;
01210 
01211   void init(THD *thd, TABLE_LIST *tl);
01212   bool fill_item_list(List<Item> *item_list) const;
01213   void reset_item_list(List<Item> *item_list) const;
01214   void clear_column_bitmaps(void);
01215   void prepare_for_position(void);
01216   void mark_columns_used_by_index_no_reset(uint index, MY_BITMAP *map);
01217   void mark_columns_used_by_index(uint index);
01218   void mark_auto_increment_column(void);
01219   void mark_columns_needed_for_update(void);
01220   void mark_columns_needed_for_delete(void);
01221   void mark_columns_needed_for_insert(void);
01222   void mark_columns_per_binlog_row_image(void);
01223   inline void column_bitmaps_set(MY_BITMAP *read_set_arg,
01224                                  MY_BITMAP *write_set_arg)
01225   {
01226     read_set= read_set_arg;
01227     write_set= write_set_arg;
01228     if (file && created)
01229       file->column_bitmaps_signal();
01230   }
01231   inline void column_bitmaps_set_no_signal(MY_BITMAP *read_set_arg,
01232                                            MY_BITMAP *write_set_arg)
01233   {
01234     read_set= read_set_arg;
01235     write_set= write_set_arg;
01236   }
01237   inline void use_all_columns()
01238   {
01239     column_bitmaps_set(&s->all_set, &s->all_set);
01240   }
01241   inline void default_column_bitmaps()
01242   {
01243     read_set= &def_read_set;
01244     write_set= &def_write_set;
01245   }
01247   inline bool needs_reopen()
01248   { return !db_stat || m_needs_reopen; }
01249   bool alloc_keys(uint key_count);
01250   bool add_tmp_key(Field_map *key_parts, char *key_name);
01251   void use_index(int key_to_save);
01252 
01253   void set_keyread(bool flag)
01254   {
01255     DBUG_ASSERT(file);
01256     if (flag && !key_read)
01257     {
01258       key_read= 1;
01259       if (is_created())
01260         file->extra(HA_EXTRA_KEYREAD);
01261     }
01262     else if (!flag && key_read)
01263     {
01264       key_read= 0;
01265       if (is_created())
01266         file->extra(HA_EXTRA_NO_KEYREAD);
01267     }
01268   }
01269 
01270   bool update_const_key_parts(Item *conds);
01271 
01272   bool check_read_removal(uint index);
01273 
01275   bool is_created() const { return created; }
01276 
01281   void set_created()
01282   {
01283     if (created)
01284       return;
01285     if (key_read)
01286       file->extra(HA_EXTRA_KEYREAD);
01287     created= true;
01288   }
01293   void set_deleted()
01294   {
01295     created= false;
01296   }
01297 };
01298 
01299 
01300 enum enum_schema_table_state
01301 { 
01302   NOT_PROCESSED= 0,
01303   PROCESSED_BY_CREATE_SORT_INDEX,
01304   PROCESSED_BY_JOIN_EXEC
01305 };
01306 
01307 typedef struct st_foreign_key_info
01308 {
01309   LEX_STRING *foreign_id;
01310   LEX_STRING *foreign_db;
01311   LEX_STRING *foreign_table;
01312   LEX_STRING *referenced_db;
01313   LEX_STRING *referenced_table;
01314   LEX_STRING *update_method;
01315   LEX_STRING *delete_method;
01316   LEX_STRING *referenced_key_name;
01317   List<LEX_STRING> foreign_fields;
01318   List<LEX_STRING> referenced_fields;
01319 } FOREIGN_KEY_INFO;
01320 
01321 #define MY_I_S_MAYBE_NULL 1
01322 #define MY_I_S_UNSIGNED   2
01323 
01324 
01325 #define SKIP_OPEN_TABLE 0                // do not open table
01326 #define OPEN_FRM_ONLY   1                // open FRM file only
01327 #define OPEN_FULL_TABLE 2                // open FRM,MYD, MYI files
01328 
01329 typedef struct st_field_info
01330 {
01334   const char* field_name;
01339   uint field_length;
01345   enum enum_field_types field_type;
01346   int value;
01355   uint field_flags;        // Field atributes(maybe_null, signed, unsigned etc.)
01356   const char* old_name;
01361   uint open_method;
01362 } ST_FIELD_INFO;
01363 
01364 
01365 struct TABLE_LIST;
01366 
01367 typedef struct st_schema_table
01368 {
01369   const char* table_name;
01370   ST_FIELD_INFO *fields_info;
01371   /* Create information_schema table */
01372   TABLE *(*create_table)  (THD *thd, TABLE_LIST *table_list);
01373   /* Fill table with data */
01374   int (*fill_table) (THD *thd, TABLE_LIST *tables, Item *cond);
01375   /* Handle fileds for old SHOW */
01376   int (*old_format) (THD *thd, struct st_schema_table *schema_table);
01377   int (*process_table) (THD *thd, TABLE_LIST *tables, TABLE *table,
01378                         bool res, LEX_STRING *db_name, LEX_STRING *table_name);
01379   int idx_field1, idx_field2; 
01380   bool hidden;
01381   uint i_s_requested_object;  /* the object we need to open(TABLE | VIEW) */
01382 } ST_SCHEMA_TABLE;
01383 
01384 
01385 #define JOIN_TYPE_LEFT  1
01386 #define JOIN_TYPE_RIGHT 2
01387 
01388 enum enum_derived_type {
01389   VIEW_ALGORITHM_UNDEFINED = 0,
01390   VIEW_ALGORITHM_TMPTABLE,
01391   VIEW_ALGORITHM_MERGE,
01392   DERIVED_ALGORITHM_TMPTABLE
01393 };
01394 
01395 #define VIEW_SUID_INVOKER               0
01396 #define VIEW_SUID_DEFINER               1
01397 #define VIEW_SUID_DEFAULT               2
01398 
01399 /* view WITH CHECK OPTION parameter options */
01400 #define VIEW_CHECK_NONE       0
01401 #define VIEW_CHECK_LOCAL      1
01402 #define VIEW_CHECK_CASCADED   2
01403 
01404 /* result of view WITH CHECK OPTION parameter check */
01405 #define VIEW_CHECK_OK         0
01406 #define VIEW_CHECK_ERROR      1
01407 #define VIEW_CHECK_SKIP       2
01408 
01410 #define MAX_TDC_BLOB_SIZE 65536
01411 
01412 class select_union;
01413 class TMP_TABLE_PARAM;
01414 
01415 struct Field_translator
01416 {
01417   Item *item;
01418   const char *name;
01419 };
01420 
01421 
01422 /*
01423   Column reference of a NATURAL/USING join. Since column references in
01424   joins can be both from views and stored tables, may point to either a
01425   Field (for tables), or a Field_translator (for views).
01426 */
01427 
01428 class Natural_join_column: public Sql_alloc
01429 {
01430 public:
01431   Field_translator *view_field;  /* Column reference of merge view. */
01432   Item_field       *table_field; /* Column reference of table or temp view. */
01433   TABLE_LIST *table_ref; /* Original base table/view reference. */
01434   /*
01435     True if a common join column of two NATURAL/USING join operands. Notice
01436     that when we have a hierarchy of nested NATURAL/USING joins, a column can
01437     be common at some level of nesting but it may not be common at higher
01438     levels of nesting. Thus this flag may change depending on at which level
01439     we are looking at some column.
01440   */
01441   bool is_common;
01442 public:
01443   Natural_join_column(Field_translator *field_param, TABLE_LIST *tab);
01444   Natural_join_column(Item_field *field_param, TABLE_LIST *tab);
01445   const char *name();
01446   Item *create_item(THD *thd);
01447   Field *field();
01448   const char *table_name();
01449   const char *db_name();
01450   GRANT_INFO *grant();
01451 };
01452 
01453 
01458 enum enum_open_type
01459 {
01460   OT_TEMPORARY_OR_BASE= 0, OT_TEMPORARY_ONLY, OT_BASE_ONLY
01461 };
01462 
01472 class Derived_key: public Sql_alloc {
01473 public:
01474   table_map referenced_by;
01475   Field_map used_fields;
01476 };
01477 
01478 class Index_hint;
01479 class Item_exists_subselect;
01480 
01481 
01482 /*
01483   Table reference in the FROM clause.
01484 
01485   These table references can be of several types that correspond to
01486   different SQL elements. Below we list all types of TABLE_LISTs with
01487   the necessary conditions to determine when a TABLE_LIST instance
01488   belongs to a certain type.
01489 
01490   1) table (TABLE_LIST::view == NULL)
01491      - base table
01492        (TABLE_LIST::derived == NULL)
01493      - subquery - TABLE_LIST::table is a temp table
01494        (TABLE_LIST::derived != NULL)
01495      - information schema table
01496        (TABLE_LIST::schema_table != NULL)
01497        NOTICE: for schema tables TABLE_LIST::field_translation may be != NULL
01498   2) view (TABLE_LIST::view != NULL)
01499      - merge    (TABLE_LIST::effective_algorithm == VIEW_ALGORITHM_MERGE)
01500            also (TABLE_LIST::field_translation != NULL)
01501      - tmptable (TABLE_LIST::effective_algorithm == VIEW_ALGORITHM_TMPTABLE)
01502            also (TABLE_LIST::field_translation == NULL)
01503   3) nested table reference (TABLE_LIST::nested_join != NULL)
01504      - table sequence - e.g. (t1, t2, t3)
01505        TODO: how to distinguish from a JOIN?
01506      - general JOIN
01507        TODO: how to distinguish from a table sequence?
01508      - NATURAL JOIN
01509        (TABLE_LIST::natural_join != NULL)
01510        - JOIN ... USING
01511          (TABLE_LIST::join_using_fields != NULL)
01512      - semi-join
01513        ;
01514 */
01515 
01516 struct Name_resolution_context;
01517 struct LEX;
01518 struct TABLE_LIST
01519 {
01520   TABLE_LIST() {}                          /* Remove gcc warning */
01521 
01526   inline void init_one_table(const char *db_name_arg,
01527                              size_t db_length_arg,
01528                              const char *table_name_arg,
01529                              size_t table_name_length_arg,
01530                              const char *alias_arg,
01531                              enum thr_lock_type lock_type_arg)
01532   {
01533     memset(this, 0, sizeof(*this));
01534     db= (char*) db_name_arg;
01535     db_length= db_length_arg;
01536     table_name= (char*) table_name_arg;
01537     table_name_length= table_name_length_arg;
01538     alias= (char*) alias_arg;
01539     lock_type= lock_type_arg;
01540     mdl_request.init(MDL_key::TABLE, db, table_name,
01541                      (lock_type >= TL_WRITE_ALLOW_WRITE) ?
01542                      MDL_SHARED_WRITE : MDL_SHARED_READ,
01543                      MDL_TRANSACTION);
01544     callback_func= 0;
01545   }
01546 
01548   static TABLE_LIST *new_nested_join(MEM_ROOT *allocator,
01549                                      const char *alias,
01550                                      TABLE_LIST *embedding,
01551                                      List<TABLE_LIST> *belongs_to,
01552                                      class st_select_lex *select);
01553 
01554   /*
01555     List of tables local to a subquery or the top-level SELECT (used by
01556     SQL_I_List). Considers views as leaves (unlike 'next_leaf' below).
01557     Created at parse time in st_select_lex::add_table_to_list() ->
01558     table_list.link_in_list().
01559   */
01560   TABLE_LIST *next_local;
01561   /* link in a global list of all queries tables */
01562   TABLE_LIST *next_global, **prev_global;
01563   char          *db, *alias, *table_name, *schema_table_name;
01564   char          *option;                /* Used by cache index  */
01571   Name_resolution_context *context_of_embedding;
01572 
01573 private:
01574   Item          *m_join_cond;           /* Used with outer join */
01575 public:
01576   Item         **join_cond_ref() { return &m_join_cond; }
01577   Item          *join_cond() const { return m_join_cond; }
01578   Item          *set_join_cond(Item *val)
01579                  { return m_join_cond= val; }
01580   /*
01581     The structure of the join condition presented in the member above
01582     can be changed during certain optimizations. This member
01583     contains a snapshot of AND-OR structure of the join condition
01584     made after permanent transformations of the parse tree, and is
01585     used to restore the join condition before every reexecution of a prepared
01586     statement or stored procedure.
01587   */
01588   Item          *prep_join_cond;
01589 
01590   Item          *sj_on_expr;            /* Synthesized semijoin condition */
01591   /*
01592     (Valid only for semi-join nests) Bitmap of tables that are within the
01593     semi-join (this is different from bitmap of all nest's children because
01594     tables that were pulled out of the semi-join nest remain listed as
01595     nest's children).
01596   */
01597   table_map     sj_inner_tables;
01598 
01599   COND_EQUAL    *cond_equal;            /* Used with outer join */
01600   /*
01601     During parsing - left operand of NATURAL/USING join where 'this' is
01602     the right operand. After parsing (this->natural_join == this) iff
01603     'this' represents a NATURAL or USING join operation. Thus after
01604     parsing 'this' is a NATURAL/USING join iff (natural_join != NULL).
01605   */
01606   TABLE_LIST *natural_join;
01607   /*
01608     True if 'this' represents a nested join that is a NATURAL JOIN.
01609     For one of the operands of 'this', the member 'natural_join' points
01610     to the other operand of 'this'.
01611   */
01612   bool is_natural_join;
01613   /* Field names in a USING clause for JOIN ... USING. */
01614   List<String> *join_using_fields;
01615   /*
01616     Explicitly store the result columns of either a NATURAL/USING join or
01617     an operand of such a join.
01618   */
01619   List<Natural_join_column> *join_columns;
01620   /* TRUE if join_columns contains all columns of this table reference. */
01621   bool is_join_columns_complete;
01622 
01623   /*
01624     List of nodes in a nested join tree, that should be considered as
01625     leaves with respect to name resolution. The leaves are: views,
01626     top-most nodes representing NATURAL/USING joins, subqueries, and
01627     base tables. All of these TABLE_LIST instances contain a
01628     materialized list of columns. The list is local to a subquery.
01629   */
01630   TABLE_LIST *next_name_resolution_table;
01631   /* Index names in a "... JOIN ... USE/IGNORE INDEX ..." clause. */
01632   List<Index_hint> *index_hints;
01633   TABLE        *table;                          /* opened table */
01634   Table_id table_id; /* table id (from binlog) for opened table */
01635   /*
01636     select_result for derived table to pass it from table creation to table
01637     filling procedure
01638   */
01639   select_union  *derived_result;
01640   /*
01641     Reference from aux_tables to local list entry of main select of
01642     multi-delete statement:
01643     delete t1 from t2,t1 where t1.a<'B' and t2.b=t1.b;
01644     here it will be reference of first occurrence of t1 to second (as you
01645     can see this lists can't be merged)
01646   */
01647   TABLE_LIST    *correspondent_table;
01669   st_select_lex_unit *derived;          /* SELECT_LEX_UNIT of derived table */
01670   /*
01671     TRUE <=> all possible keys for a derived table were collected and
01672     could be re-used while statement re-execution.
01673   */
01674   bool derived_keys_ready;
01675   ST_SCHEMA_TABLE *schema_table;        /* Information_schema table */
01676   st_select_lex *schema_select_lex;
01677   /*
01678     True when the view field translation table is used to convert
01679     schema table fields for backwards compatibility with SHOW command.
01680   */
01681   bool schema_table_reformed;
01682   TMP_TABLE_PARAM *schema_table_param;
01683   /* link to select_lex where this table was used */
01684   st_select_lex *select_lex;
01685   LEX *view;                    /* link on VIEW lex for merging */
01686   Field_translator *field_translation;  /* array of VIEW fields */
01687   /* pointer to element after last one in translation table above */
01688   Field_translator *field_translation_end;
01689   /*
01690     List (based on next_local) of underlying tables of this view. I.e. it
01691     does not include the tables of subqueries used in the view. Is set only
01692     for merged views.
01693   */
01694   TABLE_LIST    *merge_underlying_list;
01695   /*
01696     - 0 for base tables
01697     - in case of the view it is the list of all (not only underlying
01698     tables but also used in subquery ones) tables of the view.
01699   */
01700   List<TABLE_LIST> *view_tables;
01701   /* most upper view this table belongs to */
01702   TABLE_LIST    *belong_to_view;
01703   /*
01704     The view directly referencing this table
01705     (non-zero only for merged underlying tables of a view).
01706   */
01707   TABLE_LIST    *referencing_view;
01708   /* Ptr to parent MERGE table list item. See top comment in ha_myisammrg.cc */
01709   TABLE_LIST    *parent_l;
01710   /*
01711     Security  context (non-zero only for tables which belong
01712     to view with SQL SECURITY DEFINER)
01713   */
01714   Security_context *security_ctx;
01715   /*
01716     This view security context (non-zero only for views with
01717     SQL SECURITY DEFINER)
01718   */
01719   Security_context *view_sctx;
01720   /*
01721     List of all base tables local to a subquery including all view
01722     tables. Unlike 'next_local', this in this list views are *not*
01723     leaves. Created in setup_tables() -> make_leaves_list().
01724   */
01725   bool allowed_show;
01726   TABLE_LIST    *next_leaf;
01727   Item          *where;                 /* VIEW WHERE clause condition */
01728   Item          *check_option;          /* WITH CHECK OPTION condition */
01729   LEX_STRING    select_stmt;            /* text of (CREATE/SELECT) statement */
01730   LEX_STRING    md5;                    /* md5 of query text */
01731   LEX_STRING    source;                 /* source of CREATE VIEW */
01732   LEX_STRING    view_db;                /* saved view database */
01733   LEX_STRING    view_name;              /* saved view name */
01734   LEX_STRING    timestamp;              /* GMT time stamp of last operation */
01735   st_lex_user   definer;                /* definer of view */
01736   ulonglong     file_version;           /* version of file's field set */
01737   ulonglong     updatable_view;         /* VIEW can be updated */
01746   ulonglong     algorithm;
01747   ulonglong     view_suid;              /* view is suid (TRUE dy default) */
01748   ulonglong     with_check;             /* WITH CHECK OPTION */
01749   /*
01750     effective value of WITH CHECK OPTION (differ for temporary table
01751     algorithm)
01752   */
01753   uint8         effective_with_check;
01762   enum_derived_type effective_algorithm;
01763   GRANT_INFO    grant;
01764   /* data need by some engines in query cache*/
01765   ulonglong     engine_data;
01766   /* call back function for asking handler about caching in query cache */
01767   qc_engine_callback callback_func;
01768   thr_lock_type lock_type;
01769   uint          outer_join;             /* Which join type */
01770   uint          shared;                 /* Used in multi-upd */
01771   size_t        db_length;
01772   size_t        table_name_length;
01773   bool          updatable;              /* VIEW/TABLE can be updated now */
01774   bool          straight;               /* optimize with prev table */
01775   bool          updating;               /* for replicate-do/ignore table */
01776   bool          force_index;            /* prefer index over table scan */
01777   bool          ignore_leaves;          /* preload only non-leaf nodes */
01778   table_map     dep_tables;             /* tables the table depends on      */
01779   table_map     on_expr_dep_tables;     /* tables on expression depends on  */
01780   struct st_nested_join *nested_join;   /* if the element is a nested join  */
01781   TABLE_LIST *embedding;             /* nested join containing the table */
01782   List<TABLE_LIST> *join_list;/* join list the table belongs to   */
01783   bool          cacheable_table;        /* stop PS caching */
01784   /* used in multi-upd/views privilege check */
01785   bool          table_in_first_from_clause;
01790   enum enum_open_type open_type;
01791   /* TRUE if this merged view contain auto_increment field */
01792   bool          contain_auto_increment;
01793   bool          multitable_view;        /* TRUE iff this is multitable view */
01794   bool          compact_view_format;    /* Use compact format for SHOW CREATE VIEW */
01795   /* view where processed */
01796   bool          where_processed;
01797   /* TRUE <=> VIEW CHECK OPTION expression has been processed */
01798   bool          check_option_processed;
01799   /* FRMTYPE_ERROR if any type is acceptable */
01800   enum frm_type_enum required_type;
01801   char          timestamp_buffer[20];   /* buffer for timestamp (19+1) */
01802   /*
01803     This TABLE_LIST object is just placeholder for prelocking, it will be
01804     used for implicit LOCK TABLES only and won't be used in real statement.
01805   */
01806   bool          prelocking_placeholder;
01811   enum
01812   {
01813     /* Normal open. */
01814     OPEN_NORMAL= 0,
01815     /* Associate a table share only if the the table exists. */
01816     OPEN_IF_EXISTS,
01817     /*
01818       Associate a table share only if the the table exists.
01819       Also upgrade metadata lock to exclusive if table doesn't exist.
01820     */
01821     OPEN_FOR_CREATE,
01822     /* Don't associate a table share. */
01823     OPEN_STUB
01824   } open_strategy;
01825   bool          internal_tmp_table;
01827   bool          is_alias;
01831   bool          is_fqtn;
01832 
01833 
01834   /* View creation context. */
01835 
01836   View_creation_ctx *view_creation_ctx;
01837 
01838   /*
01839     Attributes to save/load view creation context in/from frm-file.
01840 
01841     Ther are required only to be able to use existing parser to load
01842     view-definition file. As soon as the parser parsed the file, view
01843     creation context is initialized and the attributes become redundant.
01844 
01845     These attributes MUST NOT be used for any purposes but the parsing.
01846   */
01847 
01848   LEX_STRING view_client_cs_name;
01849   LEX_STRING view_connection_cl_name;
01850 
01851   /*
01852     View definition (SELECT-statement) in the UTF-form.
01853   */
01854 
01855   LEX_STRING view_body_utf8;
01856 
01857    /* End of view definition context. */
01858   /* List of possible keys. Valid only for materialized derived tables/views. */
01859   List<Derived_key> derived_key_list;
01860 
01866   uint8 trg_event_map;
01867   /* TRUE <=> this table is a const one and was optimized away. */
01868   bool optimized_away;
01869   uint i_s_requested_object;
01870   bool has_db_lookup_value;
01871   bool has_table_lookup_value;
01872   uint table_open_method;
01873   enum enum_schema_table_state schema_table_state;
01874 
01875   MDL_request mdl_request;
01876 
01877 #ifdef WITH_PARTITION_STORAGE_ENGINE
01878   /* List to carry partition names from PARTITION (...) clause in statement */
01879   List<String> *partition_names;
01880 #endif /* WITH_PARTITION_STORAGE_ENGINE */
01881 
01882   void calc_md5(char *buffer);
01883   void set_underlying_merge();
01884   int view_check_option(THD *thd, bool ignore_failure) const;
01885   bool setup_underlying(THD *thd);
01886   void cleanup_items();
01887   bool placeholder()
01888   {
01889     return derived || view || schema_table || !table;
01890   }
01891   void print(THD *thd, String *str, enum_query_type query_type);
01892   bool check_single_table(TABLE_LIST **table, table_map map,
01893                           TABLE_LIST *view);
01894   bool set_insert_values(MEM_ROOT *mem_root);
01895   void hide_view_error(THD *thd);
01896   TABLE_LIST *find_underlying_table(TABLE *table);
01897   TABLE_LIST *first_leaf_for_name_resolution();
01898   TABLE_LIST *last_leaf_for_name_resolution();
01899   bool is_leaf_for_name_resolution();
01900   inline const TABLE_LIST *top_table() const
01901     { return belong_to_view ? belong_to_view : this; }
01902 
01903   inline TABLE_LIST *top_table()
01904   {
01905     return
01906       const_cast<TABLE_LIST*>(const_cast<const TABLE_LIST*>(this)->top_table());
01907   }
01908 
01909   inline bool prepare_check_option(THD *thd)
01910   {
01911     bool res= FALSE;
01912     if (effective_with_check)
01913       res= prep_check_option(thd, effective_with_check);
01914     return res;
01915   }
01916   inline bool prepare_where(THD *thd, Item **conds,
01917                             bool no_where_clause)
01918   {
01919     if (effective_algorithm == VIEW_ALGORITHM_MERGE)
01920       return prep_where(thd, conds, no_where_clause);
01921     return FALSE;
01922   }
01928   inline bool uses_materialization() const
01929   {
01930     return (effective_algorithm == VIEW_ALGORITHM_TMPTABLE ||
01931             effective_algorithm == DERIVED_ALGORITHM_TMPTABLE);
01932   }
01933   inline bool is_view_or_derived() const
01934   {
01935     return (effective_algorithm != VIEW_ALGORITHM_UNDEFINED);
01936   }
01943   bool materializable_is_const() const;
01944 
01945   void register_want_access(ulong want_access);
01946   bool prepare_security(THD *thd);
01947 #ifndef NO_EMBEDDED_ACCESS_CHECKS
01948   Security_context *find_view_security_context(THD *thd);
01949   bool prepare_view_securety_context(THD *thd);
01950 #endif
01951   /*
01952     Cleanup for re-execution in a prepared statement or a stored
01953     procedure.
01954   */
01955   void reinit_before_use(THD *thd);
01956   Item_subselect *containing_subselect();
01957 
01958   /* 
01959     Compiles the tagged hints list and fills up TABLE::keys_in_use_for_query,
01960     TABLE::keys_in_use_for_group_by, TABLE::keys_in_use_for_order_by,
01961     TABLE::force_index and TABLE::covering_keys.
01962   */
01963   bool process_index_hints(TABLE *table);
01964 
01972   inline
01973   bool is_table_ref_id_equal(TABLE_SHARE *s) const
01974   {
01975     return (m_table_ref_type == s->get_table_ref_type() &&
01976             m_table_ref_version == s->get_table_ref_version());
01977   }
01978 
01985   inline
01986   void set_table_ref_id(TABLE_SHARE *s)
01987   { set_table_ref_id(s->get_table_ref_type(), s->get_table_ref_version()); }
01988 
01989   inline
01990   void set_table_ref_id(enum_table_ref_type table_ref_type_arg,
01991                         ulonglong table_ref_version_arg)
01992   {
01993     m_table_ref_type= table_ref_type_arg;
01994     m_table_ref_version= table_ref_version_arg;
01995   }
01996 
02001   bool is_anonymous_derived_table() const { return derived && !view; }
02002 
02004   uint query_block_id() const;
02005 
02010   char *get_db_name() const { return view != NULL ? view_db.str : db; }
02011 
02018   char *get_table_name() const { return view != NULL ? view_name.str : table_name; }
02019   int fetch_number_of_rows();
02020   bool update_derived_keys(Field*, Item**, uint);
02021   bool generate_keys();
02022   bool handle_derived(LEX *lex, bool (*processor)(THD*, LEX*, TABLE_LIST*));
02023   st_select_lex_unit *get_unit() const;
02024 
02042   TABLE_LIST *outer_join_nest() const
02043   {
02044     if (!embedding)
02045       return NULL;
02046     if (embedding->sj_on_expr)
02047       return embedding->embedding;
02048     return embedding;
02049   }
02050 
02051 private:
02052   bool prep_check_option(THD *thd, uint8 check_opt_type);
02053   bool prep_where(THD *thd, Item **conds, bool no_where_clause);
02055   enum enum_table_ref_type m_table_ref_type;
02057   ulonglong m_table_ref_version;
02058 };
02059 
02060 
02061 struct st_position;
02062   
02063 class Item;
02064 
02065 /*
02066   Iterator over the fields of a generic table reference.
02067 */
02068 
02069 class Field_iterator: public Sql_alloc
02070 {
02071 public:
02072   Field_iterator() {}                         /* Remove gcc warning */
02073   virtual ~Field_iterator() {}
02074   virtual void set(TABLE_LIST *)= 0;
02075   virtual void next()= 0;
02076   virtual bool end_of_fields()= 0;              /* Return 1 at end of list */
02077   virtual const char *name()= 0;
02078   virtual Item *create_item(THD *)= 0;
02079   virtual Field *field()= 0;
02080 };
02081 
02082 
02083 /* 
02084   Iterator over the fields of a base table, view with temporary
02085   table, or subquery.
02086 */
02087 
02088 class Field_iterator_table: public Field_iterator
02089 {
02090   Field **ptr;
02091 public:
02092   Field_iterator_table() :ptr(0) {}
02093   void set(TABLE_LIST *table) { ptr= table->table->field; }
02094   void set_table(TABLE *table) { ptr= table->field; }
02095   void next() { ptr++; }
02096   bool end_of_fields() { return *ptr == 0; }
02097   const char *name();
02098   Item *create_item(THD *thd);
02099   Field *field() { return *ptr; }
02100 };
02101 
02102 
02103 /* Iterator over the fields of a merge view. */
02104 
02105 class Field_iterator_view: public Field_iterator
02106 {
02107   Field_translator *ptr, *array_end;
02108   TABLE_LIST *view;
02109 public:
02110   Field_iterator_view() :ptr(0), array_end(0) {}
02111   void set(TABLE_LIST *table);
02112   void next() { ptr++; }
02113   bool end_of_fields() { return ptr == array_end; }
02114   const char *name();
02115   Item *create_item(THD *thd);
02116   Item **item_ptr() {return &ptr->item; }
02117   Field *field() { return 0; }
02118   inline Item *item() { return ptr->item; }
02119   Field_translator *field_translator() { return ptr; }
02120 };
02121 
02122 
02123 /*
02124   Field_iterator interface to the list of materialized fields of a
02125   NATURAL/USING join.
02126 */
02127 
02128 class Field_iterator_natural_join: public Field_iterator
02129 {
02130   List_iterator_fast<Natural_join_column> column_ref_it;
02131   Natural_join_column *cur_column_ref;
02132 public:
02133   Field_iterator_natural_join() :cur_column_ref(NULL) {}
02134   ~Field_iterator_natural_join() {}
02135   void set(TABLE_LIST *table);
02136   void next();
02137   bool end_of_fields() { return !cur_column_ref; }
02138   const char *name() { return cur_column_ref->name(); }
02139   Item *create_item(THD *thd) { return cur_column_ref->create_item(thd); }
02140   Field *field() { return cur_column_ref->field(); }
02141   Natural_join_column *column_ref() { return cur_column_ref; }
02142 };
02143 
02144 
02145 /*
02146   Generic iterator over the fields of an arbitrary table reference.
02147 
02148   DESCRIPTION
02149     This class unifies the various ways of iterating over the columns
02150     of a table reference depending on the type of SQL entity it
02151     represents. If such an entity represents a nested table reference,
02152     this iterator encapsulates the iteration over the columns of the
02153     members of the table reference.
02154 
02155   IMPLEMENTATION
02156     The implementation assumes that all underlying NATURAL/USING table
02157     references already contain their result columns and are linked into
02158     the list TABLE_LIST::next_name_resolution_table.
02159 */
02160 
02161 class Field_iterator_table_ref: public Field_iterator
02162 {
02163   TABLE_LIST *table_ref, *first_leaf, *last_leaf;
02164   Field_iterator_table        table_field_it;
02165   Field_iterator_view         view_field_it;
02166   Field_iterator_natural_join natural_join_it;
02167   Field_iterator *field_it;
02168   void set_field_iterator();
02169 public:
02170   Field_iterator_table_ref() :field_it(NULL) {}
02171   void set(TABLE_LIST *table);
02172   void next();
02173   bool end_of_fields()
02174   { return (table_ref == last_leaf && field_it->end_of_fields()); }
02175   const char *name() { return field_it->name(); }
02176   const char *get_table_name();
02177   const char *get_db_name();
02178   GRANT_INFO *grant();
02179   Item *create_item(THD *thd) { return field_it->create_item(thd); }
02180   Field *field() { return field_it->field(); }
02181   Natural_join_column *get_or_create_column_ref(THD *thd, TABLE_LIST *parent_table_ref);
02182   Natural_join_column *get_natural_column_ref();
02183 };
02184 
02191 struct Semijoin_mat_optimize
02192 {
02194   struct st_position *positions;
02196   bool lookup_allowed;
02198   bool scan_allowed;
02200   double expected_rowcount;
02202   Cost_estimate materialization_cost;
02204   Cost_estimate lookup_cost;
02206   Cost_estimate scan_cost;
02208   Item_field **mat_fields;
02209 };
02210 
02219 typedef struct st_nested_join
02220 {
02221   List<TABLE_LIST>  join_list;       /* list of elements in the nested join */
02222   table_map         used_tables;     /* bitmap of tables in the nested join */
02223   table_map         not_null_tables; /* tables that rejects nulls           */
02228   struct st_join_table *first_nested;
02235   uint              nj_total;
02243   uint              nj_counter;
02248   nested_join_map   nj_map;
02253   table_map         sj_depends_on;
02257   table_map         sj_corr_tables;
02261   uint query_block_id;
02262   /*
02263     Lists of trivially-correlated expressions from the outer and inner tables
02264     of the semi-join, respectively.
02265   */
02266   List<Item>        sj_outer_exprs, sj_inner_exprs;
02267   Semijoin_mat_optimize sjm;
02268 } NESTED_JOIN;
02269 
02270 
02271 typedef struct st_changed_table_list
02272 {
02273   struct        st_changed_table_list *next;
02274   char          *key;
02275   uint32        key_length;
02276 } CHANGED_TABLE_LIST;
02277 
02278 
02279 typedef struct st_open_table_list{
02280   struct st_open_table_list *next;
02281   char  *db,*table;
02282   uint32 in_use,locked;
02283 } OPEN_TABLE_LIST;
02284 
02285 
02286 static inline my_bitmap_map *tmp_use_all_columns(TABLE *table,
02287                                                  MY_BITMAP *bitmap)
02288 {
02289   my_bitmap_map *old= bitmap->bitmap;
02290   bitmap->bitmap= table->s->all_set.bitmap;// does not repoint last_word_ptr
02291   return old;
02292 }
02293 
02294 
02295 static inline void tmp_restore_column_map(MY_BITMAP *bitmap,
02296                                           my_bitmap_map *old)
02297 {
02298   bitmap->bitmap= old;
02299 }
02300 
02301 /* The following is only needed for debugging */
02302 
02303 static inline my_bitmap_map *dbug_tmp_use_all_columns(TABLE *table,
02304                                                       MY_BITMAP *bitmap)
02305 {
02306 #ifndef DBUG_OFF
02307   return tmp_use_all_columns(table, bitmap);
02308 #else
02309   return 0;
02310 #endif
02311 }
02312 
02313 static inline void dbug_tmp_restore_column_map(MY_BITMAP *bitmap,
02314                                                my_bitmap_map *old)
02315 {
02316 #ifndef DBUG_OFF
02317   tmp_restore_column_map(bitmap, old);
02318 #endif
02319 }
02320 
02321 
02322 /* 
02323   Variant of the above : handle both read and write sets.
02324   Provide for the possiblity of the read set being the same as the write set
02325 */
02326 static inline void dbug_tmp_use_all_columns(TABLE *table,
02327                                             my_bitmap_map **save,
02328                                             MY_BITMAP *read_set,
02329                                             MY_BITMAP *write_set)
02330 {
02331 #ifndef DBUG_OFF
02332   save[0]= read_set->bitmap;
02333   save[1]= write_set->bitmap;
02334   (void) tmp_use_all_columns(table, read_set);
02335   (void) tmp_use_all_columns(table, write_set);
02336 #endif
02337 }
02338 
02339 
02340 static inline void dbug_tmp_restore_column_maps(MY_BITMAP *read_set,
02341                                                 MY_BITMAP *write_set,
02342                                                 my_bitmap_map **old)
02343 {
02344 #ifndef DBUG_OFF
02345   tmp_restore_column_map(read_set, old[0]);
02346   tmp_restore_column_map(write_set, old[1]);
02347 #endif
02348 }
02349 
02350 
02351 size_t max_row_length(TABLE *table, const uchar *data);
02352 
02353 
02354 void init_mdl_requests(TABLE_LIST *table_list);
02355 
02356 int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias,
02357                           uint db_stat, uint prgflag, uint ha_open_flags,
02358                           TABLE *outparam, bool is_create_table);
02359 TABLE_SHARE *alloc_table_share(TABLE_LIST *table_list, const char *key,
02360                                uint key_length);
02361 void init_tmp_table_share(THD *thd, TABLE_SHARE *share, const char *key,
02362                           uint key_length,
02363                           const char *table_name, const char *path);
02364 void free_table_share(TABLE_SHARE *share);
02365 int open_table_def(THD *thd, TABLE_SHARE *share, uint db_flags);
02366 void open_table_error(TABLE_SHARE *share, int error, int db_errno, int errarg);
02367 void update_create_info_from_table(HA_CREATE_INFO *info, TABLE *form);
02368 enum_ident_name_check check_and_convert_db_name(LEX_STRING *db,
02369                                                 bool preserve_lettercase);
02370 bool check_column_name(const char *name);
02371 enum_ident_name_check check_table_name(const char *name, size_t length,
02372                                        bool check_for_path_chars);
02373 int rename_file_ext(const char * from,const char * to,const char * ext);
02374 char *get_field(MEM_ROOT *mem, Field *field);
02375 bool get_field(MEM_ROOT *mem, Field *field, class String *res);
02376 
02377 int closefrm(TABLE *table, bool free_share);
02378 int read_string(File file, uchar* *to, size_t length);
02379 void free_blobs(TABLE *table);
02380 void free_field_buffers_larger_than(TABLE *table, uint32 size);
02381 int set_zone(int nr,int min_zone,int max_zone);
02382 ulong get_form_pos(File file, uchar *head, TYPELIB *save_names);
02383 ulong make_new_entry(File file,uchar *fileinfo,TYPELIB *formnames,
02384                      const char *newname);
02385 ulong next_io_size(ulong pos);
02386 void append_unescaped(String *res, const char *pos, uint length);
02387 File create_frm(THD *thd, const char *name, const char *db,
02388                 const char *table, uint reclength, uchar *fileinfo,
02389                 HA_CREATE_INFO *create_info, uint keys, KEY *key_info);
02390 char *fn_rext(char *name);
02391 
02392 /* performance schema */
02393 extern LEX_STRING PERFORMANCE_SCHEMA_DB_NAME;
02394 
02395 extern LEX_STRING GENERAL_LOG_NAME;
02396 extern LEX_STRING SLOW_LOG_NAME;
02397 
02398 /* information schema */
02399 extern LEX_STRING INFORMATION_SCHEMA_NAME;
02400 extern LEX_STRING MYSQL_SCHEMA_NAME;
02401 
02402 /* replication's tables */
02403 extern LEX_STRING RLI_INFO_NAME;
02404 extern LEX_STRING MI_INFO_NAME;
02405 extern LEX_STRING WORKER_INFO_NAME;
02406 
02407 inline bool is_infoschema_db(const char *name, size_t len)
02408 {
02409   return (INFORMATION_SCHEMA_NAME.length == len &&
02410           !my_strcasecmp(system_charset_info,
02411                          INFORMATION_SCHEMA_NAME.str, name));
02412 }
02413 
02414 inline bool is_infoschema_db(const char *name)
02415 {
02416   return !my_strcasecmp(system_charset_info,
02417                         INFORMATION_SCHEMA_NAME.str, name);
02418 }
02419 
02420 TYPELIB *typelib(MEM_ROOT *mem_root, List<String> &strings);
02421 
02425 inline bool is_user_table(TABLE * table)
02426 {
02427   const char *name= table->s->table_name.str;
02428   return strncmp(name, tmp_file_prefix, tmp_file_prefix_length);
02429 }
02430 
02431 inline void mark_as_null_row(TABLE *table)
02432 {
02433   table->null_row=1;
02434   table->status|=STATUS_NULL_ROW;
02435   memset(table->null_flags, 255, table->s->null_bytes);
02436 }
02437 
02438 bool is_simple_order(ORDER *order);
02439 
02440 #endif /* MYSQL_CLIENT */
02441 
02442 #endif /* TABLE_INCLUDED */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines