MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_base.h
1 /* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #ifndef SQL_BASE_INCLUDED
17 #define SQL_BASE_INCLUDED
18 
19 #include "unireg.h" // REQUIRED: for other includes
20 #include "sql_trigger.h" /* trg_event_type */
21 #include "sql_class.h" /* enum_mark_columns */
22 #include "mysqld.h" /* key_map */
23 
24 class Item_ident;
26 class Open_table_context;
27 class Open_tables_state;
29 struct TABLE_LIST;
30 class THD;
31 struct handlerton;
32 struct TABLE;
33 
34 typedef class st_select_lex SELECT_LEX;
35 
37 
38 /*
39  This enumeration type is used only by the function find_item_in_list
40  to return the info on how an item has been resolved against a list
41  of possibly aliased items.
42  The item can be resolved:
43  - against an alias name of the list's element (RESOLVED_AGAINST_ALIAS)
44  - against non-aliased field name of the list (RESOLVED_WITH_NO_ALIAS)
45  - against an aliased field name of the list (RESOLVED_BEHIND_ALIAS)
46  - ignoring the alias name in cases when SQL requires to ignore aliases
47  (e.g. when the resolved field reference contains a table name or
48  when the resolved item is an expression) (RESOLVED_IGNORING_ALIAS)
49 */
50 enum enum_resolution_type {
51  NOT_RESOLVED=0,
52  RESOLVED_IGNORING_ALIAS,
53  RESOLVED_BEHIND_ALIAS,
54  RESOLVED_WITH_NO_ALIAS,
55  RESOLVED_AGAINST_ALIAS
56 };
57 
58 enum find_item_error_report_type {REPORT_ALL_ERRORS, REPORT_EXCEPT_NOT_FOUND,
59  IGNORE_ERRORS, REPORT_EXCEPT_NON_UNIQUE,
60  IGNORE_EXCEPT_NON_UNIQUE};
61 
62 enum enum_tdc_remove_table_type {TDC_RT_REMOVE_ALL, TDC_RT_REMOVE_NOT_OWN,
63  TDC_RT_REMOVE_UNUSED,
64  TDC_RT_REMOVE_NOT_OWN_KEEP_SHARE};
65 
66 /* bits for last argument to remove_table_from_cache() */
67 #define RTFC_NO_FLAG 0x0000
68 #define RTFC_OWNED_BY_THD_FLAG 0x0001
69 #define RTFC_WAIT_OTHER_THREAD_FLAG 0x0002
70 #define RTFC_CHECK_KILLED_FLAG 0x0004
71 
72 bool check_dup(const char *db, const char *name, TABLE_LIST *tables);
74 bool table_cache_init(void);
75 void table_cache_free(void);
76 bool table_def_init(void);
77 void table_def_free(void);
78 void table_def_start_shutdown(void);
79 void assign_new_table_id(TABLE_SHARE *share);
80 uint cached_table_definitions(void);
81 uint get_table_def_key(const TABLE_LIST *table_list, const char **key);
82 TABLE_SHARE *get_table_share(THD *thd, TABLE_LIST *table_list,
83  const char *key, uint key_length,
84  uint db_flags, int *error,
85  my_hash_value_type hash_value);
86 void release_table_share(TABLE_SHARE *share);
87 TABLE_SHARE *get_cached_table_share(const char *db, const char *table_name);
88 
89 TABLE *open_ltable(THD *thd, TABLE_LIST *table_list, thr_lock_type update,
90  uint lock_flags);
91 
92 /* mysql_lock_tables() and open_table() flags bits */
93 #define MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK 0x0001
94 #define MYSQL_OPEN_IGNORE_FLUSH 0x0002
95 /* MYSQL_OPEN_TEMPORARY_ONLY (0x0004) is not used anymore. */
96 #define MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY 0x0008
97 #define MYSQL_LOCK_LOG_TABLE 0x0010
98 
102 #define MYSQL_OPEN_HAS_MDL_LOCK 0x0020
103 
107 #define MYSQL_OPEN_GET_NEW_TABLE 0x0040
108 /* 0x0080 used to be MYSQL_OPEN_SKIP_TEMPORARY */
110 #define MYSQL_OPEN_FAIL_ON_MDL_CONFLICT 0x0100
111 
112 #define MYSQL_OPEN_FORCE_SHARED_MDL 0x0200
113 
117 #define MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL 0x0400
118 
122 #define MYSQL_LOCK_IGNORE_TIMEOUT 0x0800
123 
127 #define MYSQL_OPEN_SKIP_SCOPED_MDL_LOCK 0x1000
128 
132 #define MYSQL_LOCK_RPL_INFO_TABLE 0x2000
133 
137 #define MYSQL_OPEN_IGNORE_KILLED 0x4000
138 
140 #define MYSQL_OPEN_REOPEN (MYSQL_OPEN_IGNORE_FLUSH |\
141  MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK |\
142  MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY |\
143  MYSQL_LOCK_IGNORE_TIMEOUT |\
144  MYSQL_OPEN_IGNORE_KILLED |\
145  MYSQL_OPEN_GET_NEW_TABLE |\
146  MYSQL_OPEN_HAS_MDL_LOCK)
147 
148 bool open_table(THD *thd, TABLE_LIST *table_list, Open_table_context *ot_ctx);
149 
150 bool get_key_map_from_key_list(key_map *map, TABLE *table,
151  List<String> *index_list);
152 TABLE *open_table_uncached(THD *thd, const char *path, const char *db,
153  const char *table_name,
154  bool add_to_temporary_tables_list,
155  bool open_in_engine);
156 TABLE *find_locked_table(TABLE *list, const char *db, const char *table_name);
157 thr_lock_type read_lock_type_for_table(THD *thd,
158  Query_tables_list *prelocking_ctx,
159  TABLE_LIST *table_list,
160  bool routine_modifies_data);
161 
162 my_bool mysql_rm_tmp_tables(void);
163 bool rm_temporary_table(handlerton *base, const char *path);
164 void close_tables_for_reopen(THD *thd, TABLE_LIST **tables,
165  const MDL_savepoint &start_of_statement_svp);
166 TABLE_LIST *find_table_in_list(TABLE_LIST *table,
168  const char *db_name,
169  const char *table_name);
170 TABLE *find_temporary_table(THD *thd, const char *db, const char *table_name);
171 TABLE *find_temporary_table(THD *thd, const TABLE_LIST *tl);
172 TABLE *find_temporary_table(THD *thd, const char *table_key,
173  uint table_key_length);
174 void close_thread_tables(THD *thd);
175 bool fill_record_n_invoke_before_triggers(THD *thd, List<Item> &fields,
176  List<Item> &values,
177  bool ignore_errors,
178  Table_triggers_list *triggers,
179  enum trg_event_type event);
180 bool fill_record_n_invoke_before_triggers(THD *thd, Field **field,
181  List<Item> &values,
182  bool ignore_errors,
183  Table_triggers_list *triggers,
184  enum trg_event_type event);
185 bool insert_fields(THD *thd, Name_resolution_context *context,
186  const char *db_name, const char *table_name,
187  List_iterator<Item> *it, bool any_privileges);
188 int setup_wild(THD *thd, TABLE_LIST *tables, List<Item> &fields,
189  List<Item> *sum_func_list, uint wild_num);
190 bool setup_fields(THD *thd, Ref_ptr_array ref_pointer_array,
191  List<Item> &item, enum_mark_columns mark_used_columns,
192  List<Item> *sum_func_list, bool allow_sum_func);
193 bool fill_record(THD * thd, List<Item> &fields, List<Item> &values,
194  bool ignore_errors, MY_BITMAP *bitmap);
195 bool fill_record(THD *thd, Field **field, List<Item> &values,
196  bool ignore_errors, MY_BITMAP *bitmap);
197 
198 Field *
199 find_field_in_tables(THD *thd, Item_ident *item,
200  TABLE_LIST *first_table, TABLE_LIST *last_table,
201  Item **ref, find_item_error_report_type report_error,
202  bool check_privileges, bool register_tree_change);
203 Field *
204 find_field_in_table_ref(THD *thd, TABLE_LIST *table_list,
205  const char *name, uint length,
206  const char *item_name, const char *db_name,
207  const char *table_name, Item **ref,
208  bool check_privileges, bool allow_rowid,
209  uint *cached_field_index_ptr,
210  bool register_tree_change, TABLE_LIST **actual_table);
211 Field *
212 find_field_in_table(THD *thd, TABLE *table, const char *name, uint length,
213  bool allow_rowid, uint *cached_field_index_ptr);
214 Field *
215 find_field_in_table_sef(TABLE *table, const char *name);
216 Item ** find_item_in_list(Item *item, List<Item> &items, uint *counter,
217  find_item_error_report_type report_error,
218  enum_resolution_type *resolution);
219 bool setup_tables(THD *thd, Name_resolution_context *context,
220  List<TABLE_LIST> *from_clause, TABLE_LIST *tables,
221  TABLE_LIST **leaves, bool select_insert);
222 bool setup_tables_and_check_access(THD *thd,
223  Name_resolution_context *context,
224  List<TABLE_LIST> *from_clause,
225  TABLE_LIST *tables,
226  TABLE_LIST **leaves,
227  bool select_insert,
228  ulong want_access_first,
229  ulong want_access);
230 bool wait_while_table_is_used(THD *thd, TABLE *table,
231  enum ha_extra_function function);
232 
233 void drop_open_table(THD *thd, TABLE *table, const char *db_name,
234  const char *table_name);
235 void update_non_unique_table_error(TABLE_LIST *update,
236  const char *operation,
237  TABLE_LIST *duplicate);
238 int setup_conds(THD *thd, TABLE_LIST *tables, TABLE_LIST *leaves,
239  Item **conds);
240 int setup_ftfuncs(SELECT_LEX* select);
241 int init_ftfuncs(THD *thd, SELECT_LEX* select, bool no_order);
242 bool lock_table_names(THD *thd, TABLE_LIST *table_list,
243  TABLE_LIST *table_list_end, ulong lock_wait_timeout,
244  uint flags);
245 bool open_tables(THD *thd, TABLE_LIST **tables, uint *counter, uint flags,
246  Prelocking_strategy *prelocking_strategy);
247 /* open_and_lock_tables with optional derived handling */
248 bool open_and_lock_tables(THD *thd, TABLE_LIST *tables,
249  bool derived, uint flags,
250  Prelocking_strategy *prelocking_strategy);
251 /* simple open_and_lock_tables without derived handling for single table */
252 TABLE *open_n_lock_single_table(THD *thd, TABLE_LIST *table_l,
253  thr_lock_type lock_type, uint flags,
254  Prelocking_strategy *prelocking_strategy);
255 bool open_normal_and_derived_tables(THD *thd, TABLE_LIST *tables, uint flags);
256 bool lock_tables(THD *thd, TABLE_LIST *tables, uint counter, uint flags);
257 void free_io_cache(TABLE *entry);
258 void intern_close_table(TABLE *entry);
259 void close_thread_table(THD *thd, TABLE **table_ptr);
260 bool close_temporary_tables(THD *thd);
261 TABLE_LIST *unique_table(THD *thd, TABLE_LIST *table, TABLE_LIST *table_list,
262  bool check_alias);
263 int drop_temporary_table(THD *thd, TABLE_LIST *table_list, bool *is_trans);
264 void close_temporary_table(THD *thd, TABLE *table, bool free_share,
265  bool delete_table);
266 void close_temporary(TABLE *table, bool free_share, bool delete_table);
267 bool rename_temporary_table(THD* thd, TABLE *table, const char *new_db,
268  const char *table_name);
269 bool open_temporary_tables(THD *thd, TABLE_LIST *tl_list);
270 bool open_temporary_table(THD *thd, TABLE_LIST *tl);
271 bool is_equal(const LEX_STRING *a, const LEX_STRING *b);
272 
273 /* Functions to work with system tables. */
274 bool open_system_tables_for_read(THD *thd, TABLE_LIST *table_list,
275  Open_tables_backup *backup);
276 void close_system_tables(THD *thd, Open_tables_backup *backup);
277 void close_mysql_tables(THD *thd);
278 TABLE *open_system_table_for_update(THD *thd, TABLE_LIST *one_table);
279 TABLE *open_log_table(THD *thd, TABLE_LIST *one_table, Open_tables_backup *backup);
280 void close_log_table(THD *thd, Open_tables_backup *backup);
281 
282 TABLE *open_performance_schema_table(THD *thd, TABLE_LIST *one_table,
283  Open_tables_state *backup);
284 void close_performance_schema_table(THD *thd, Open_tables_state *backup);
285 
286 bool close_cached_tables(THD *thd, TABLE_LIST *tables,
287  bool wait_for_refresh, ulong timeout);
288 bool close_cached_connection_tables(THD *thd, LEX_STRING *connect_string);
289 void close_all_tables_for_name(THD *thd, TABLE_SHARE *share,
290  bool remove_from_locked_tables,
291  TABLE *skip_table);
292 OPEN_TABLE_LIST *list_open_tables(THD *thd, const char *db, const char *wild);
293 void tdc_remove_table(THD *thd, enum_tdc_remove_table_type remove_type,
294  const char *db, const char *table_name,
295  bool has_lock);
296 bool tdc_open_view(THD *thd, TABLE_LIST *table_list, const char *alias,
297  const char *cache_key, uint cache_key_length, uint flags);
298 void tdc_flush_unused_tables();
299 TABLE *find_table_for_mdl_upgrade(THD *thd, const char *db,
300  const char *table_name,
301  bool no_error);
303 bool check_if_table_exists(THD *thd, TABLE_LIST *table, bool *exists);
304 
305 extern Item **not_found_item;
306 extern Field *not_found_field;
307 extern Field *view_ref_found;
308 extern HASH table_def_cache;
309 
319 inline void setup_table_map(TABLE *table, TABLE_LIST *table_list, uint tablenr)
320 {
321  table->used_fields= 0;
322  table->const_table= 0;
323  table->null_row= 0;
324  table->status= STATUS_GARBAGE | STATUS_NOT_FOUND;
325  table->maybe_null= table_list->outer_join;
326  TABLE_LIST *embedding= table_list->embedding;
327  while (!table->maybe_null && embedding)
328  {
329  table->maybe_null= embedding->outer_join;
330  embedding= embedding->embedding;
331  }
332  table->tablenr= tablenr;
333  table->map= (table_map) 1 << tablenr;
334  table->force_index= table_list->force_index;
335  table->force_index_order= table->force_index_group= 0;
336  table->covering_keys= table->s->keys_for_keyread;
337  table->merge_keys.clear_all();
338 }
339 
340 inline TABLE_LIST *find_table_in_global_list(TABLE_LIST *table,
341  const char *db_name,
342  const char *table_name)
343 {
344  return find_table_in_list(table, &TABLE_LIST::next_global,
345  db_name, table_name);
346 }
347 
348 inline TABLE_LIST *find_table_in_local_list(TABLE_LIST *table,
349  const char *db_name,
350  const char *table_name)
351 {
352  return find_table_in_list(table, &TABLE_LIST::next_local,
353  db_name, table_name);
354 }
355 
356 
357 inline bool setup_fields_with_no_wrap(THD *thd, Ref_ptr_array ref_pointer_array,
358  List<Item> &item,
359  enum_mark_columns mark_used_columns,
360  List<Item> *sum_func_list,
361  bool allow_sum_func)
362 {
363  bool res;
364  thd->lex->select_lex.no_wrap_view_item= TRUE;
365  res= setup_fields(thd, ref_pointer_array, item, mark_used_columns,
366  sum_func_list, allow_sum_func);
367  thd->lex->select_lex.no_wrap_view_item= FALSE;
368  return res;
369 }
370 
378 {
379 public:
380  virtual ~Prelocking_strategy() { }
381 
382  virtual bool handle_routine(THD *thd, Query_tables_list *prelocking_ctx,
383  Sroutine_hash_entry *rt, sp_head *sp,
384  bool *need_prelocking) = 0;
385  virtual bool handle_table(THD *thd, Query_tables_list *prelocking_ctx,
386  TABLE_LIST *table_list, bool *need_prelocking) = 0;
387  virtual bool handle_view(THD *thd, Query_tables_list *prelocking_ctx,
388  TABLE_LIST *table_list, bool *need_prelocking)= 0;
389 };
390 
391 
401 {
402 public:
403  virtual bool handle_routine(THD *thd, Query_tables_list *prelocking_ctx,
404  Sroutine_hash_entry *rt, sp_head *sp,
405  bool *need_prelocking);
406  virtual bool handle_table(THD *thd, Query_tables_list *prelocking_ctx,
407  TABLE_LIST *table_list, bool *need_prelocking);
408  virtual bool handle_view(THD *thd, Query_tables_list *prelocking_ctx,
409  TABLE_LIST *table_list, bool *need_prelocking);
410 };
411 
412 
419 {
420  virtual bool handle_table(THD *thd, Query_tables_list *prelocking_ctx,
421  TABLE_LIST *table_list, bool *need_prelocking);
422 };
423 
424 
434 {
435 public:
436  virtual bool handle_routine(THD *thd, Query_tables_list *prelocking_ctx,
437  Sroutine_hash_entry *rt, sp_head *sp,
438  bool *need_prelocking);
439  virtual bool handle_table(THD *thd, Query_tables_list *prelocking_ctx,
440  TABLE_LIST *table_list, bool *need_prelocking);
441  virtual bool handle_view(THD *thd, Query_tables_list *prelocking_ctx,
442  TABLE_LIST *table_list, bool *need_prelocking);
443 };
444 
445 
446 inline bool
447 open_tables(THD *thd, TABLE_LIST **tables, uint *counter, uint flags)
448 {
449  DML_prelocking_strategy prelocking_strategy;
450 
451  return open_tables(thd, tables, counter, flags, &prelocking_strategy);
452 }
453 
454 
455 inline TABLE *open_n_lock_single_table(THD *thd, TABLE_LIST *table_l,
456  thr_lock_type lock_type, uint flags)
457 {
458  DML_prelocking_strategy prelocking_strategy;
459 
460  return open_n_lock_single_table(thd, table_l, lock_type, flags,
461  &prelocking_strategy);
462 }
463 
464 
465 /* open_and_lock_tables with derived handling */
466 inline bool open_and_lock_tables(THD *thd, TABLE_LIST *tables,
467  bool derived, uint flags)
468 {
469  DML_prelocking_strategy prelocking_strategy;
470 
471  return open_and_lock_tables(thd, tables, derived, flags,
472  &prelocking_strategy);
473 }
474 
475 
482 {
483 public:
484  enum enum_open_table_action
485  {
486  OT_NO_ACTION= 0,
487  OT_BACKOFF_AND_RETRY,
488  OT_REOPEN_TABLES,
489  OT_DISCOVER,
490  OT_REPAIR
491  };
492  Open_table_context(THD *thd, uint flags);
493 
495  bool request_backoff_action(enum_open_table_action action_arg,
496  TABLE_LIST *table);
497 
498  bool can_recover_from_failed_open() const
499  { return m_action != OT_NO_ACTION; }
500 
508  {
509  return m_start_of_statement_svp;
510  }
511 
512  inline ulong get_timeout() const
513  {
514  return m_timeout;
515  }
516 
517  uint get_flags() const { return m_flags; }
518 
524  {
525  m_has_protection_against_grl= TRUE;
526  }
527 
528  bool has_protection_against_grl() const
529  {
530  return m_has_protection_against_grl;
531  }
532 
533 private:
534  /* THD for which tables are opened. */
535  THD *m_thd;
541  TABLE_LIST *m_failed_table;
542  MDL_savepoint m_start_of_statement_svp;
547  ulong m_timeout;
548  /* open_table() flags. */
549  uint m_flags;
551  enum enum_open_table_action m_action;
557  bool m_has_locks;
562  bool m_has_protection_against_grl;
563 };
564 
565 
570 inline bool is_temporary_table(TABLE_LIST *tl)
571 {
572  if (tl->view || tl->schema_table)
573  return FALSE;
574 
575  if (!tl->table)
576  return FALSE;
577 
578  /*
579  NOTE: 'table->s' might be NULL for specially constructed TABLE
580  instances. See SHOW TRIGGERS for example.
581  */
582 
583  if (!tl->table->s)
584  return FALSE;
585 
586  return tl->table->s->tmp_table != NO_TMP_TABLE;
587 }
588 
589 
594 class No_such_table_error_handler : public Internal_error_handler
595 {
596 public:
598  : m_handled_errors(0), m_unhandled_errors(0)
599  {}
600 
601  bool handle_condition(THD *thd,
602  uint sql_errno,
603  const char* sqlstate,
604  Sql_condition::enum_warning_level level,
605  const char* msg,
606  Sql_condition ** cond_hdl);
607 
612  bool safely_trapped_errors();
613 
614 private:
615  int m_handled_errors;
616  int m_unhandled_errors;
617 };
618 
619 #endif /* SQL_BASE_INCLUDED */