1 /* Copyright (c) 2014, 2020, 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 DD__TRANSACTION_IMPL_INCLUDED
24 #define DD__TRANSACTION_IMPL_INCLUDED
25 
26 #include <sys/types.h>
27 #include <map>
28 
29 #include "my_inttypes.h"
30 #include "mysql/udf_registration_types.h"
31 #include "sql/dd/dd_kill_immunizer.h"  // dd::DD_kill_immunizer
32 #include "sql/dd/impl/types/abstract_table_impl.h"
33 #include "sql/dd/impl/types/charset_impl.h"
34 #include "sql/dd/impl/types/check_constraint_impl.h"
35 #include "sql/dd/impl/types/collation_impl.h"
36 #include "sql/dd/impl/types/column_impl.h"
37 #include "sql/dd/impl/types/column_statistics_impl.h"
38 #include "sql/dd/impl/types/event_impl.h"
39 #include "sql/dd/impl/types/foreign_key_impl.h"
40 #include "sql/dd/impl/types/function_impl.h"
41 #include "sql/dd/impl/types/index_stat_impl.h"
42 #include "sql/dd/impl/types/parameter_impl.h"
43 #include "sql/dd/impl/types/partition_impl.h"
44 #include "sql/dd/impl/types/procedure_impl.h"
45 #include "sql/dd/impl/types/resource_group_impl.h"
46 #include "sql/dd/impl/types/routine_impl.h"
47 #include "sql/dd/impl/types/schema_impl.h"
48 #include "sql/dd/impl/types/spatial_reference_system_impl.h"
49 #include "sql/dd/impl/types/table_impl.h"
50 #include "sql/dd/impl/types/table_stat_impl.h"
51 #include "sql/dd/impl/types/tablespace_impl.h"
52 #include "sql/dd/impl/types/trigger_impl.h"
53 #include "sql/dd/impl/types/view_impl.h"
54 #include "sql/dd/impl/types/view_routine_impl.h"
55 #include "sql/dd/impl/types/view_table_impl.h"
56 #include "sql/dd/string_type.h"  // dd::String_type
57 #include "sql/discrete_interval.h"
58 #include "sql/field.h"
59 #include "sql/handler.h"
60 #include "sql/set_var.h"
61 #include "sql/sql_class.h"  // THD::killed_state
62 #include "thr_lock.h"
63 
64 struct LEX;
65 
66 namespace dd {
67 
68 class Raw_table;
69 
70 ///////////////////////////////////////////////////////////////////////////
71 
72 /**
73   Auxiliary class for opening dictionary tables.
74 */
75 class Open_dictionary_tables_ctx {
76  public:
Open_dictionary_tables_ctx(THD * thd,thr_lock_type lock_type)77   Open_dictionary_tables_ctx(THD *thd, thr_lock_type lock_type)
78       : m_thd(thd), m_lock_type(lock_type), m_ignore_global_read_lock(false) {}
79 
80   ~Open_dictionary_tables_ctx();
81 
82   Raw_table *get_table(const String_type &name) const;
83 
84   template <typename T>
get_table()85   Raw_table *get_table() const {
86     return get_table(T::DD_table::instance().name());
87   }
88 
89   template <typename X>
register_tables()90   void register_tables() {
91     X::Impl::register_tables(this);
92   }
93 
94   template <typename X>
add_table()95   void add_table() {
96     this->add_table(X::instance().name());
97   }
98 
99   /**
100     Open all the DD tables in list Open_dictionary_tables_ctx::m_tables.
101 
102     @return true - On failure and error is reported.
103     @return false - On success.
104   */
105   bool open_tables();
106 
107   void add_table(const String_type &name);
108 
get_thd()109   THD *get_thd() const { return m_thd; }
110 
111   /**
112     Ignore global read lock when opening the tables.
113   */
mark_ignore_global_read_lock()114   void mark_ignore_global_read_lock() {
115     if (m_lock_type == TL_WRITE) m_ignore_global_read_lock = true;
116   }
117 
118  private:
119   THD *m_thd;
120   thr_lock_type m_lock_type;
121   bool m_ignore_global_read_lock;
122   typedef std::map<String_type, Raw_table *> Object_table_map;
123   Object_table_map m_tables;
124 };
125 
126 /**
127   Implementation of read-only data-dictionary transaction.
128   Relies on attachable transactions infrastructure.
129 */
130 class Transaction_ro {
131  public:
Transaction_ro(THD * thd,enum_tx_isolation isolation)132   Transaction_ro(THD *thd, enum_tx_isolation isolation)
133       : otx(thd, TL_READ), m_thd(thd), m_kill_immunizer(thd) {
134     thd->begin_attachable_ro_transaction();
135     thd->tx_isolation = isolation;
136   }
137 
~Transaction_ro()138   ~Transaction_ro() { m_thd->end_attachable_transaction(); }
139 
140   Open_dictionary_tables_ctx otx;
141 
142  private:
143   THD *m_thd;
144 
145   DD_kill_immunizer m_kill_immunizer;
146 };
147 
148 ///////////////////////////////////////////////////////////////////////////
149 
150 /**
151   Class for storing/restoring state during dictionary update operations.
152 */
153 class Update_dictionary_tables_ctx {
154  public:
155   Update_dictionary_tables_ctx(THD *thd);
156 
157   ~Update_dictionary_tables_ctx();
158 
159   Open_dictionary_tables_ctx otx;
160 
161  private:
162   THD *m_thd;
163 
164   DD_kill_immunizer m_kill_immunizer;
165 
166   LEX *m_lex_saved;
167 
168   // Stores state before DD operations
169   Open_tables_backup m_open_tables_state_backup;
170   bool m_saved_binlog_row_based;
171   ulonglong m_saved_options;
172   sql_mode_t m_saved_mode;
173   long long m_latest_auto_incr_id;
174   enum_check_fields m_saved_check_for_truncated_fields;
175   uint m_saved_in_sub_stmt;
176   bool m_saved_time_zone_used;
177 
178   /*
179     Save auto_increment_increment state.
180 
181     Value of the auto_increment_increment is set to 1 in the constructor.
182     auto_increment_offset is used only when auto_increment_increment is greater
183     than 1. So saving only auto_increment_increment value.
184   */
185   ulong m_saved_auto_increment_increment;
186 
187   // Save auto_inc_intervals_in_cur_stmt_for_binlog
188   Discrete_intervals_list m_auto_inc_intervals_in_cur_stmt_for_binlog_saved;
189 
190   Discrete_intervals_list m_auto_inc_intervals_forced_saved;
191 };
192 
193 ///////////////////////////////////////////////////////////////////////////
194 
195 }  // namespace dd
196 
197 #endif  // DD__TRANSACTION_IMPL_INCLUDED
198