1 /* Copyright (c) 2008, 2019, 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, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef LOCKED_TABLES_LIST_INCLUDED
24 #define LOCKED_TABLES_LIST_INCLUDED
25 
26 #include <sys/types.h>
27 #include <vector>
28 #include "my_alloc.h"
29 
30 struct MYSQL_LOCK;
31 class MDL_context;
32 class MDL_ticket;
33 struct TABLE;
34 struct TABLE_LIST;
35 class THD;
36 
37 /**
38   Type of locked tables mode.
39   See comment for THD::locked_tables_mode for complete description.
40   While adding new enum values add them to the getter method for this enum
41   declared below and defined in binlog.cc as well.
42 */
43 
44 enum enum_locked_tables_mode {
45   LTM_NONE = 0,
46   LTM_LOCK_TABLES,
47   LTM_PRELOCKED,
48   LTM_PRELOCKED_UNDER_LOCK_TABLES
49 };
50 
51 #ifndef DBUG_OFF
52 /**
53   Getter for the enum enum_locked_tables_mode
54   @param locked_tables_mode enum for types of locked tables mode
55 
56   @return The string represantation of that enum value
57 */
58 const char *get_locked_tables_mode_name(
59     enum_locked_tables_mode locked_tables_mode);
60 #endif
61 
62 /**
63   Tables that were locked with LOCK TABLES statement.
64 
65   Encapsulates a list of TABLE_LIST instances for tables
66   locked by LOCK TABLES statement, memory root for metadata locks,
67   and, generally, the context of LOCK TABLES statement.
68 
69   In LOCK TABLES mode, the locked tables are kept open between
70   statements.
71   Therefore, we can't allocate metadata locks on execution memory
72   root -- as well as tables, the locks need to stay around till
73   UNLOCK TABLES is called.
74   The locks are allocated in the memory root encapsulated in this
75   class.
76 
77   Some SQL commands, like FLUSH TABLE or ALTER TABLE, demand that
78   the tables they operate on are closed, at least temporarily.
79   This class encapsulates a list of TABLE_LIST instances, one
80   for each base table from LOCK TABLES list,
81   which helps conveniently close the TABLEs when it's necessary
82   and later reopen them.
83 
84 */
85 
86 class Locked_tables_list {
87  private:
88   MEM_ROOT m_locked_tables_root;
89   TABLE_LIST *m_locked_tables;
90   TABLE_LIST **m_locked_tables_last;
91   /** An auxiliary array used only in reopen_tables(). */
92   TABLE **m_reopen_array;
93   /**
94     Count the number of tables in m_locked_tables list. We can't
95     rely on thd->lock->table_count because it excludes
96     non-transactional temporary tables. We need to know
97     an exact number of TABLE objects.
98   */
99   size_t m_locked_tables_count;
100 
101   struct MDL_ticket_pair {
102     MDL_ticket *m_src;
103     MDL_ticket *m_dst;
104   };
105   using MDL_ticket_pairs = std::vector<MDL_ticket_pair>;
106 
107   MDL_ticket_pairs m_rename_tablespace_mdls;
108 
109  public:
110   Locked_tables_list();
111 
112   void unlock_locked_tables(THD *thd);
~Locked_tables_list()113   ~Locked_tables_list() {
114     unlock_locked_tables(nullptr);
115     DBUG_ASSERT(m_rename_tablespace_mdls.empty());
116   }
117   bool init_locked_tables(THD *thd);
locked_tables()118   TABLE_LIST *locked_tables() const { return m_locked_tables; }
119   void unlink_from_list(const THD *thd, TABLE_LIST *table_list,
120                         bool remove_from_locked_tables);
121   void unlink_all_closed_tables(THD *thd, MYSQL_LOCK *lock,
122                                 size_t reopen_count);
123   bool reopen_tables(THD *thd);
124   void rename_locked_table(TABLE_LIST *old_table_list, const char *new_db,
125                            const char *new_table_name,
126                            MDL_ticket *target_mdl_ticket);
127 
128   void add_rename_tablespace_mdls(MDL_ticket *src, MDL_ticket *dst);
129   void adjust_renamed_tablespace_mdls(MDL_context *mctx);
discard_renamed_tablespace_mdls()130   void discard_renamed_tablespace_mdls() { m_rename_tablespace_mdls.clear(); }
131 };
132 
133 #endif /*not defined LOCK_TABLES_LIST_INCLUDED */
134