My Project
sql_base.h
00001 /* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
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; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 #ifndef SQL_BASE_INCLUDED
00017 #define SQL_BASE_INCLUDED
00018 
00019 #include "unireg.h"                    // REQUIRED: for other includes
00020 #include "sql_trigger.h"                        /* trg_event_type */
00021 #include "sql_class.h"                          /* enum_mark_columns */
00022 #include "mysqld.h"                             /* key_map */
00023 
00024 class Item_ident;
00025 struct Name_resolution_context;
00026 class Open_table_context;
00027 class Open_tables_state;
00028 class Prelocking_strategy;
00029 struct TABLE_LIST;
00030 class THD;
00031 struct handlerton;
00032 struct TABLE;
00033 
00034 typedef class st_select_lex SELECT_LEX;
00035 
00036 typedef struct st_lock_param_type ALTER_PARTITION_PARAM_TYPE;
00037 
00038 /*
00039   This enumeration type is used only by the function find_item_in_list
00040   to return the info on how an item has been resolved against a list
00041   of possibly aliased items.
00042   The item can be resolved:
00043    - against an alias name of the list's element (RESOLVED_AGAINST_ALIAS)
00044    - against non-aliased field name of the list  (RESOLVED_WITH_NO_ALIAS)
00045    - against an aliased field name of the list   (RESOLVED_BEHIND_ALIAS)
00046    - ignoring the alias name in cases when SQL requires to ignore aliases
00047      (e.g. when the resolved field reference contains a table name or
00048      when the resolved item is an expression)   (RESOLVED_IGNORING_ALIAS)
00049 */
00050 enum enum_resolution_type {
00051   NOT_RESOLVED=0,
00052   RESOLVED_IGNORING_ALIAS,
00053   RESOLVED_BEHIND_ALIAS,
00054   RESOLVED_WITH_NO_ALIAS,
00055   RESOLVED_AGAINST_ALIAS
00056 };
00057 
00058 enum find_item_error_report_type {REPORT_ALL_ERRORS, REPORT_EXCEPT_NOT_FOUND,
00059                                   IGNORE_ERRORS, REPORT_EXCEPT_NON_UNIQUE,
00060                                   IGNORE_EXCEPT_NON_UNIQUE};
00061 
00062 enum enum_tdc_remove_table_type {TDC_RT_REMOVE_ALL, TDC_RT_REMOVE_NOT_OWN,
00063                                  TDC_RT_REMOVE_UNUSED,
00064                                  TDC_RT_REMOVE_NOT_OWN_KEEP_SHARE};
00065 
00066 /* bits for last argument to remove_table_from_cache() */
00067 #define RTFC_NO_FLAG                0x0000
00068 #define RTFC_OWNED_BY_THD_FLAG      0x0001
00069 #define RTFC_WAIT_OTHER_THREAD_FLAG 0x0002
00070 #define RTFC_CHECK_KILLED_FLAG      0x0004
00071 
00072 bool check_dup(const char *db, const char *name, TABLE_LIST *tables);
00073 extern mysql_mutex_t LOCK_open;
00074 bool table_cache_init(void);
00075 void table_cache_free(void);
00076 bool table_def_init(void);
00077 void table_def_free(void);
00078 void table_def_start_shutdown(void);
00079 void assign_new_table_id(TABLE_SHARE *share);
00080 uint cached_table_definitions(void);
00081 uint get_table_def_key(const TABLE_LIST *table_list, const char **key);
00082 TABLE_SHARE *get_table_share(THD *thd, TABLE_LIST *table_list,
00083                              const char *key, uint key_length,
00084                              uint db_flags, int *error,
00085                              my_hash_value_type hash_value);
00086 void release_table_share(TABLE_SHARE *share);
00087 TABLE_SHARE *get_cached_table_share(const char *db, const char *table_name);
00088 
00089 TABLE *open_ltable(THD *thd, TABLE_LIST *table_list, thr_lock_type update,
00090                    uint lock_flags);
00091 
00092 /* mysql_lock_tables() and open_table() flags bits */
00093 #define MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK      0x0001
00094 #define MYSQL_OPEN_IGNORE_FLUSH                 0x0002
00095 /* MYSQL_OPEN_TEMPORARY_ONLY (0x0004) is not used anymore. */
00096 #define MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY      0x0008
00097 #define MYSQL_LOCK_LOG_TABLE                    0x0010
00098 
00102 #define MYSQL_OPEN_HAS_MDL_LOCK                 0x0020
00103 
00107 #define MYSQL_OPEN_GET_NEW_TABLE                0x0040
00108 /* 0x0080 used to be MYSQL_OPEN_SKIP_TEMPORARY */
00110 #define MYSQL_OPEN_FAIL_ON_MDL_CONFLICT         0x0100
00111 
00112 #define MYSQL_OPEN_FORCE_SHARED_MDL             0x0200
00113 
00117 #define MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL   0x0400
00118 
00122 #define MYSQL_LOCK_IGNORE_TIMEOUT               0x0800
00123 
00127 #define MYSQL_OPEN_SKIP_SCOPED_MDL_LOCK         0x1000
00128 
00132 #define MYSQL_LOCK_RPL_INFO_TABLE               0x2000
00133 
00137 #define MYSQL_OPEN_IGNORE_KILLED                0x4000
00138 
00140 #define MYSQL_OPEN_REOPEN  (MYSQL_OPEN_IGNORE_FLUSH |\
00141                             MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK |\
00142                             MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY |\
00143                             MYSQL_LOCK_IGNORE_TIMEOUT |\
00144                             MYSQL_OPEN_IGNORE_KILLED |\
00145                             MYSQL_OPEN_GET_NEW_TABLE |\
00146                             MYSQL_OPEN_HAS_MDL_LOCK)
00147 
00148 bool open_table(THD *thd, TABLE_LIST *table_list, Open_table_context *ot_ctx);
00149 
00150 bool get_key_map_from_key_list(key_map *map, TABLE *table,
00151                                List<String> *index_list);
00152 TABLE *open_table_uncached(THD *thd, const char *path, const char *db,
00153                            const char *table_name,
00154                            bool add_to_temporary_tables_list,
00155                            bool open_in_engine);
00156 TABLE *find_locked_table(TABLE *list, const char *db, const char *table_name);
00157 thr_lock_type read_lock_type_for_table(THD *thd,
00158                                        Query_tables_list *prelocking_ctx,
00159                                        TABLE_LIST *table_list,
00160                                        bool routine_modifies_data);
00161 
00162 my_bool mysql_rm_tmp_tables(void);
00163 bool rm_temporary_table(handlerton *base, const char *path);
00164 void close_tables_for_reopen(THD *thd, TABLE_LIST **tables,
00165                              const MDL_savepoint &start_of_statement_svp);
00166 TABLE_LIST *find_table_in_list(TABLE_LIST *table,
00167                                TABLE_LIST *TABLE_LIST::*link,
00168                                const char *db_name,
00169                                const char *table_name);
00170 TABLE *find_temporary_table(THD *thd, const char *db, const char *table_name);
00171 TABLE *find_temporary_table(THD *thd, const TABLE_LIST *tl);
00172 TABLE *find_temporary_table(THD *thd, const char *table_key,
00173                             uint table_key_length);
00174 void close_thread_tables(THD *thd);
00175 bool fill_record_n_invoke_before_triggers(THD *thd, List<Item> &fields,
00176                                           List<Item> &values,
00177                                           bool ignore_errors,
00178                                           Table_triggers_list *triggers,
00179                                           enum trg_event_type event);
00180 bool fill_record_n_invoke_before_triggers(THD *thd, Field **field,
00181                                           List<Item> &values,
00182                                           bool ignore_errors,
00183                                           Table_triggers_list *triggers,
00184                                           enum trg_event_type event);
00185 bool insert_fields(THD *thd, Name_resolution_context *context,
00186                    const char *db_name, const char *table_name,
00187                    List_iterator<Item> *it, bool any_privileges);
00188 int setup_wild(THD *thd, TABLE_LIST *tables, List<Item> &fields,
00189                List<Item> *sum_func_list, uint wild_num);
00190 bool setup_fields(THD *thd, Ref_ptr_array ref_pointer_array,
00191                   List<Item> &item, enum_mark_columns mark_used_columns,
00192                   List<Item> *sum_func_list, bool allow_sum_func);
00193 bool fill_record(THD * thd, List<Item> &fields, List<Item> &values,
00194                  bool ignore_errors, MY_BITMAP *bitmap);
00195 bool fill_record(THD *thd, Field **field, List<Item> &values,
00196                  bool ignore_errors, MY_BITMAP *bitmap);
00197 
00198 Field *
00199 find_field_in_tables(THD *thd, Item_ident *item,
00200                      TABLE_LIST *first_table, TABLE_LIST *last_table,
00201                      Item **ref, find_item_error_report_type report_error,
00202                      bool check_privileges, bool register_tree_change);
00203 Field *
00204 find_field_in_table_ref(THD *thd, TABLE_LIST *table_list,
00205                         const char *name, uint length,
00206                         const char *item_name, const char *db_name,
00207                         const char *table_name, Item **ref,
00208                         bool check_privileges, bool allow_rowid,
00209                         uint *cached_field_index_ptr,
00210                         bool register_tree_change, TABLE_LIST **actual_table);
00211 Field *
00212 find_field_in_table(THD *thd, TABLE *table, const char *name, uint length,
00213                     bool allow_rowid, uint *cached_field_index_ptr);
00214 Field *
00215 find_field_in_table_sef(TABLE *table, const char *name);
00216 Item ** find_item_in_list(Item *item, List<Item> &items, uint *counter,
00217                           find_item_error_report_type report_error,
00218                           enum_resolution_type *resolution);
00219 bool setup_tables(THD *thd, Name_resolution_context *context,
00220                   List<TABLE_LIST> *from_clause, TABLE_LIST *tables,
00221                   TABLE_LIST **leaves, bool select_insert);
00222 bool setup_tables_and_check_access(THD *thd,
00223                                    Name_resolution_context *context,
00224                                    List<TABLE_LIST> *from_clause,
00225                                    TABLE_LIST *tables,
00226                                    TABLE_LIST **leaves,
00227                                    bool select_insert,
00228                                    ulong want_access_first,
00229                                    ulong want_access);
00230 bool wait_while_table_is_used(THD *thd, TABLE *table,
00231                               enum ha_extra_function function);
00232 
00233 void drop_open_table(THD *thd, TABLE *table, const char *db_name,
00234                      const char *table_name);
00235 void update_non_unique_table_error(TABLE_LIST *update,
00236                                    const char *operation,
00237                                    TABLE_LIST *duplicate);
00238 int setup_conds(THD *thd, TABLE_LIST *tables, TABLE_LIST *leaves,
00239                 Item **conds);
00240 int setup_ftfuncs(SELECT_LEX* select);
00241 int init_ftfuncs(THD *thd, SELECT_LEX* select, bool no_order);
00242 bool lock_table_names(THD *thd, TABLE_LIST *table_list,
00243                       TABLE_LIST *table_list_end, ulong lock_wait_timeout,
00244                       uint flags);
00245 bool open_tables(THD *thd, TABLE_LIST **tables, uint *counter, uint flags,
00246                  Prelocking_strategy *prelocking_strategy);
00247 /* open_and_lock_tables with optional derived handling */
00248 bool open_and_lock_tables(THD *thd, TABLE_LIST *tables,
00249                           bool derived, uint flags,
00250                           Prelocking_strategy *prelocking_strategy);
00251 /* simple open_and_lock_tables without derived handling for single table */
00252 TABLE *open_n_lock_single_table(THD *thd, TABLE_LIST *table_l,
00253                                 thr_lock_type lock_type, uint flags,
00254                                 Prelocking_strategy *prelocking_strategy);
00255 bool open_normal_and_derived_tables(THD *thd, TABLE_LIST *tables, uint flags);
00256 bool lock_tables(THD *thd, TABLE_LIST *tables, uint counter, uint flags);
00257 void free_io_cache(TABLE *entry);
00258 void intern_close_table(TABLE *entry);
00259 void close_thread_table(THD *thd, TABLE **table_ptr);
00260 bool close_temporary_tables(THD *thd);
00261 TABLE_LIST *unique_table(THD *thd, TABLE_LIST *table, TABLE_LIST *table_list,
00262                          bool check_alias);
00263 int drop_temporary_table(THD *thd, TABLE_LIST *table_list, bool *is_trans);
00264 void close_temporary_table(THD *thd, TABLE *table, bool free_share,
00265                            bool delete_table);
00266 void close_temporary(TABLE *table, bool free_share, bool delete_table);
00267 bool rename_temporary_table(THD* thd, TABLE *table, const char *new_db,
00268                             const char *table_name);
00269 bool open_temporary_tables(THD *thd, TABLE_LIST *tl_list);
00270 bool open_temporary_table(THD *thd, TABLE_LIST *tl);
00271 bool is_equal(const LEX_STRING *a, const LEX_STRING *b);
00272 
00273 /* Functions to work with system tables. */
00274 bool open_system_tables_for_read(THD *thd, TABLE_LIST *table_list,
00275                                  Open_tables_backup *backup);
00276 void close_system_tables(THD *thd, Open_tables_backup *backup);
00277 void close_mysql_tables(THD *thd);
00278 TABLE *open_system_table_for_update(THD *thd, TABLE_LIST *one_table);
00279 TABLE *open_log_table(THD *thd, TABLE_LIST *one_table, Open_tables_backup *backup);
00280 void close_log_table(THD *thd, Open_tables_backup *backup);
00281 
00282 TABLE *open_performance_schema_table(THD *thd, TABLE_LIST *one_table,
00283                                      Open_tables_state *backup);
00284 void close_performance_schema_table(THD *thd, Open_tables_state *backup);
00285 
00286 bool close_cached_tables(THD *thd, TABLE_LIST *tables,
00287                          bool wait_for_refresh, ulong timeout);
00288 bool close_cached_connection_tables(THD *thd, LEX_STRING *connect_string);
00289 void close_all_tables_for_name(THD *thd, TABLE_SHARE *share,
00290                                bool remove_from_locked_tables,
00291                                TABLE *skip_table);
00292 OPEN_TABLE_LIST *list_open_tables(THD *thd, const char *db, const char *wild);
00293 void tdc_remove_table(THD *thd, enum_tdc_remove_table_type remove_type,
00294                       const char *db, const char *table_name,
00295                       bool has_lock);
00296 bool tdc_open_view(THD *thd, TABLE_LIST *table_list, const char *alias,
00297                    const char *cache_key, uint cache_key_length, uint flags);
00298 void tdc_flush_unused_tables();
00299 TABLE *find_table_for_mdl_upgrade(THD *thd, const char *db,
00300                                   const char *table_name,
00301                                   bool no_error);
00302 void mark_tmp_table_for_reuse(TABLE *table);
00303 bool check_if_table_exists(THD *thd, TABLE_LIST *table, bool *exists);
00304 
00305 extern Item **not_found_item;
00306 extern Field *not_found_field;
00307 extern Field *view_ref_found;
00308 extern HASH table_def_cache;
00309 
00319 inline void setup_table_map(TABLE *table, TABLE_LIST *table_list, uint tablenr)
00320 {
00321   table->used_fields= 0;
00322   table->const_table= 0;
00323   table->null_row= 0;
00324   table->status= STATUS_GARBAGE | STATUS_NOT_FOUND;
00325   table->maybe_null= table_list->outer_join;
00326   TABLE_LIST *embedding= table_list->embedding;
00327   while (!table->maybe_null && embedding)
00328   {
00329     table->maybe_null= embedding->outer_join;
00330     embedding= embedding->embedding;
00331   }
00332   table->tablenr= tablenr;
00333   table->map= (table_map) 1 << tablenr;
00334   table->force_index= table_list->force_index;
00335   table->force_index_order= table->force_index_group= 0;
00336   table->covering_keys= table->s->keys_for_keyread;
00337   table->merge_keys.clear_all();
00338 }
00339 
00340 inline TABLE_LIST *find_table_in_global_list(TABLE_LIST *table,
00341                                              const char *db_name,
00342                                              const char *table_name)
00343 {
00344   return find_table_in_list(table, &TABLE_LIST::next_global,
00345                             db_name, table_name);
00346 }
00347 
00348 inline TABLE_LIST *find_table_in_local_list(TABLE_LIST *table,
00349                                             const char *db_name,
00350                                             const char *table_name)
00351 {
00352   return find_table_in_list(table, &TABLE_LIST::next_local,
00353                             db_name, table_name);
00354 }
00355 
00356 
00357 inline bool setup_fields_with_no_wrap(THD *thd, Ref_ptr_array ref_pointer_array,
00358                                       List<Item> &item,
00359                                       enum_mark_columns mark_used_columns,
00360                                       List<Item> *sum_func_list,
00361                                       bool allow_sum_func)
00362 {
00363   bool res;
00364   thd->lex->select_lex.no_wrap_view_item= TRUE;
00365   res= setup_fields(thd, ref_pointer_array, item, mark_used_columns,
00366                     sum_func_list, allow_sum_func);
00367   thd->lex->select_lex.no_wrap_view_item= FALSE;
00368   return res;
00369 }
00370 
00377 class Prelocking_strategy
00378 {
00379 public:
00380   virtual ~Prelocking_strategy() { }
00381 
00382   virtual bool handle_routine(THD *thd, Query_tables_list *prelocking_ctx,
00383                               Sroutine_hash_entry *rt, sp_head *sp,
00384                               bool *need_prelocking) = 0;
00385   virtual bool handle_table(THD *thd, Query_tables_list *prelocking_ctx,
00386                             TABLE_LIST *table_list, bool *need_prelocking) = 0;
00387   virtual bool handle_view(THD *thd, Query_tables_list *prelocking_ctx,
00388                            TABLE_LIST *table_list, bool *need_prelocking)= 0;
00389 };
00390 
00391 
00400 class DML_prelocking_strategy : public Prelocking_strategy
00401 {
00402 public:
00403   virtual bool handle_routine(THD *thd, Query_tables_list *prelocking_ctx,
00404                               Sroutine_hash_entry *rt, sp_head *sp,
00405                               bool *need_prelocking);
00406   virtual bool handle_table(THD *thd, Query_tables_list *prelocking_ctx,
00407                             TABLE_LIST *table_list, bool *need_prelocking);
00408   virtual bool handle_view(THD *thd, Query_tables_list *prelocking_ctx,
00409                            TABLE_LIST *table_list, bool *need_prelocking);
00410 };
00411 
00412 
00418 class Lock_tables_prelocking_strategy : public DML_prelocking_strategy
00419 {
00420   virtual bool handle_table(THD *thd, Query_tables_list *prelocking_ctx,
00421                             TABLE_LIST *table_list, bool *need_prelocking);
00422 };
00423 
00424 
00433 class Alter_table_prelocking_strategy : public Prelocking_strategy
00434 {
00435 public:
00436   virtual bool handle_routine(THD *thd, Query_tables_list *prelocking_ctx,
00437                               Sroutine_hash_entry *rt, sp_head *sp,
00438                               bool *need_prelocking);
00439   virtual bool handle_table(THD *thd, Query_tables_list *prelocking_ctx,
00440                             TABLE_LIST *table_list, bool *need_prelocking);
00441   virtual bool handle_view(THD *thd, Query_tables_list *prelocking_ctx,
00442                            TABLE_LIST *table_list, bool *need_prelocking);
00443 };
00444 
00445 
00446 inline bool
00447 open_tables(THD *thd, TABLE_LIST **tables, uint *counter, uint flags)
00448 {
00449   DML_prelocking_strategy prelocking_strategy;
00450 
00451   return open_tables(thd, tables, counter, flags, &prelocking_strategy);
00452 }
00453 
00454 
00455 inline TABLE *open_n_lock_single_table(THD *thd, TABLE_LIST *table_l,
00456                                        thr_lock_type lock_type, uint flags)
00457 {
00458   DML_prelocking_strategy prelocking_strategy;
00459 
00460   return open_n_lock_single_table(thd, table_l, lock_type, flags,
00461                                   &prelocking_strategy);
00462 }
00463 
00464 
00465 /* open_and_lock_tables with derived handling */
00466 inline bool open_and_lock_tables(THD *thd, TABLE_LIST *tables,
00467                                  bool derived, uint flags)
00468 {
00469   DML_prelocking_strategy prelocking_strategy;
00470 
00471   return open_and_lock_tables(thd, tables, derived, flags,
00472                               &prelocking_strategy);
00473 }
00474 
00475 
00481 class Open_table_context
00482 {
00483 public:
00484   enum enum_open_table_action
00485   {
00486     OT_NO_ACTION= 0,
00487     OT_BACKOFF_AND_RETRY,
00488     OT_REOPEN_TABLES,
00489     OT_DISCOVER,
00490     OT_REPAIR
00491   };
00492   Open_table_context(THD *thd, uint flags);
00493 
00494   bool recover_from_failed_open();
00495   bool request_backoff_action(enum_open_table_action action_arg,
00496                               TABLE_LIST *table);
00497 
00498   bool can_recover_from_failed_open() const
00499   { return m_action != OT_NO_ACTION; }
00500 
00507   const MDL_savepoint &start_of_statement_svp() const
00508   {
00509     return m_start_of_statement_svp;
00510   }
00511 
00512   inline ulong get_timeout() const
00513   {
00514     return m_timeout;
00515   }
00516 
00517   uint get_flags() const { return m_flags; }
00518 
00523   void set_has_protection_against_grl()
00524   {
00525     m_has_protection_against_grl= TRUE;
00526   }
00527 
00528   bool has_protection_against_grl() const
00529   {
00530     return m_has_protection_against_grl;
00531   }
00532 
00533 private:
00534   /* THD for which tables are opened. */
00535   THD *m_thd;
00541   TABLE_LIST *m_failed_table;
00542   MDL_savepoint m_start_of_statement_svp;
00547   ulong m_timeout;
00548   /* open_table() flags. */
00549   uint m_flags;
00551   enum enum_open_table_action m_action;
00557   bool m_has_locks;
00562   bool m_has_protection_against_grl;
00563 };
00564 
00565 
00570 inline bool is_temporary_table(TABLE_LIST *tl)
00571 {
00572   if (tl->view || tl->schema_table)
00573     return FALSE;
00574 
00575   if (!tl->table)
00576     return FALSE;
00577 
00578   /*
00579     NOTE: 'table->s' might be NULL for specially constructed TABLE
00580     instances. See SHOW TRIGGERS for example.
00581   */
00582 
00583   if (!tl->table->s)
00584     return FALSE;
00585 
00586   return tl->table->s->tmp_table != NO_TMP_TABLE;
00587 }
00588 
00589 
00594 class No_such_table_error_handler : public Internal_error_handler
00595 {
00596 public:
00597   No_such_table_error_handler()
00598     : m_handled_errors(0), m_unhandled_errors(0)
00599   {}
00600 
00601   bool handle_condition(THD *thd,
00602                         uint sql_errno,
00603                         const char* sqlstate,
00604                         Sql_condition::enum_warning_level level,
00605                         const char* msg,
00606                         Sql_condition ** cond_hdl);
00607 
00612   bool safely_trapped_errors();
00613 
00614 private:
00615   int m_handled_errors;
00616   int m_unhandled_errors;
00617 };
00618 
00619 #endif /* SQL_BASE_INCLUDED */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines