MySQL  8.0.18
Source Code Documentation
thd_raii.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, 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 THD_RAII_INCLUDED
24 #define THD_RAII_INCLUDED 1
25 
26 /**
27  * @file thd_raii.h
28  * Some RAII classes that set THD state.
29  */
30 
31 #include <sys/types.h>
32 
33 #include "my_dbug.h"
34 #include "my_inttypes.h"
35 #include "sql/query_options.h"
36 #include "sql/sql_class.h"
37 #include "sql/system_variables.h"
38 
39 /*************************************************************************/
40 
41 /** RAII class for temporarily turning off @@autocommit in the connection. */
42 
44  public:
45  /**
46  @param thd non-NULL - pointer to the context of connection in which
47  @@autocommit mode needs to be disabled.
48  NULL - if @@autocommit mode needs to be left as is.
49  */
51  : m_thd(thd), m_save_option_bits(thd ? thd->variables.option_bits : 0) {
52  if (m_thd) {
53  /*
54  We can't disable auto-commit if there is ongoing transaction as this
55  might easily break statement/session transaction invariants.
56  */
59 
62  }
63  }
64 
66  if (m_thd) {
67  /*
68  Both session and statement transactions need to be finished by the
69  time when we enable auto-commit mode back.
70  */
74  }
75  }
76 
77  private:
80 };
81 
82 /**
83  RAII class which allows to temporary disable updating Gtid_state.
84 */
85 
87  public:
89  : m_thd(thd),
91  thd->is_operating_substatement_implicitly),
92  m_save_skip_gtid_rollback(thd->skip_gtid_rollback) {
94  m_thd->skip_gtid_rollback = true;
95  }
96 
101  }
102 
103  private:
107 };
108 
109 /**
110  RAII class to temporarily disable binlogging.
111 */
112 
114  public:
116  : m_thd(thd),
117  m_binlog_disabled(thd->variables.option_bits & OPTION_BIN_LOG) {
119  }
120 
123  }
124 
125  private:
126  THD *const m_thd;
127  const bool m_binlog_disabled;
128 };
129 
131  public:
133  : m_thd(thd), m_saved_sql_log_bin(thd->variables.sql_log_bin) {
134  thd->variables.sql_log_bin = false;
135  }
136 
139  }
140 
141  private:
142  THD *const m_thd;
144 };
145 
146 /**
147  RAII class which allows to save, clear and store binlog format state
148  There are two variables in THD class that will decide the binlog
149  format of a statement
150  i) THD::current_stmt_binlog_format
151  ii) THD::variables.binlog_format
152  Saving or Clearing or Storing of binlog format state should be done
153  for these two variables together all the time.
154 */
156  public:
158  : m_thd(thd),
159  m_global_binlog_format(thd->variables.binlog_format),
163 
166  }
167 
173  }
174 
175  private:
179 };
180 
181 /**
182  RAII class to temporarily turn off SQL modes that affect parsing
183  of expressions. Can also be used when printing expressions even
184  if it turns off more SQL modes than strictly necessary for it
185  (these extra modes are harmless as they do not affect expression
186  printing).
187 */
189  public:
191  : m_thd(thd), m_old_sql_mode(thd->variables.sql_mode) {
192  /*
193  Switch off modes which can prevent normal parsing of expressions:
194 
195  - MODE_REAL_AS_FLOAT affect only CREATE TABLE parsing
196  + MODE_PIPES_AS_CONCAT affect expression parsing
197  + MODE_ANSI_QUOTES affect expression parsing
198  + MODE_IGNORE_SPACE affect expression parsing
199  - MODE_NOT_USED not used :)
200  * MODE_ONLY_FULL_GROUP_BY affect execution
201  * MODE_NO_UNSIGNED_SUBTRACTION affect execution
202  - MODE_NO_DIR_IN_CREATE affect table creation only
203  - MODE_POSTGRESQL compounded from other modes
204  - MODE_ORACLE compounded from other modes
205  - MODE_MSSQL compounded from other modes
206  - MODE_DB2 compounded from other modes
207  - MODE_MAXDB affect only CREATE TABLE parsing
208  - MODE_NO_KEY_OPTIONS affect only SHOW
209  - MODE_NO_TABLE_OPTIONS affect only SHOW
210  - MODE_NO_FIELD_OPTIONS affect only SHOW
211  - MODE_MYSQL323 affect only SHOW
212  - MODE_MYSQL40 affect only SHOW
213  - MODE_ANSI compounded from other modes
214  (+ transaction mode)
215  ? MODE_NO_AUTO_VALUE_ON_ZERO affect UPDATEs
216  + MODE_NO_BACKSLASH_ESCAPES affect expression parsing
217  */
220  }
221 
223 
224  private:
227 };
228 
229 /**
230  RAII class to temporarily swap thd->mem_root to a different mem_root.
231 */
233  public:
235  : m_thd(thd), m_old_mem_root(thd->mem_root) {
236  thd->mem_root = mem_root;
237  }
238 
240 
241  private:
244 };
245 
246 #endif // THD_RAII_INCLUDED
RAII class to temporarily swap thd->mem_root to a different mem_root.
Definition: thd_raii.h:232
THD *const m_thd
Definition: thd_raii.h:142
unsigned long long int ulonglong
Definition: my_inttypes.h:55
bool sql_log_bin
Definition: system_variables.h:250
~Sql_mode_parse_guard()
Definition: thd_raii.h:222
enum_binlog_format
Definition: system_variables.h:44
THD * m_thd
Definition: thd_raii.h:225
int is_current_stmt_binlog_format_row() const
Determine the binlog format of the current statement.
Definition: sql_class.h:1436
const bool m_saved_sql_log_bin
Definition: thd_raii.h:143
~Disable_binlog_guard()
Definition: thd_raii.h:121
row-based
Definition: system_variables.h:47
Some integer typedefs for easier portability.
THD * m_thd
Definition: thd_raii.h:176
THD * m_thd
Definition: thd_raii.h:104
RAII class for temporarily turning off @autocommit in the connection.
Definition: thd_raii.h:43
#define MODE_NO_BACKSLASH_ESCAPES
Definition: system_variables.h:110
struct System_variables variables
Definition: sql_class.h:957
bool is_empty(enum_trx_scope scope) const
Definition: transaction_info.h:337
Transaction_ctx * get_transaction()
Definition: sql_class.h:1714
RAII class which allows to save, clear and store binlog format state There are two variables in THD c...
Definition: thd_raii.h:155
ulonglong option_bits
OPTION_xxx constants, e.g. OPTION_PROFILING.
Definition: system_variables.h:203
~Disable_gtid_state_update_guard()
Definition: thd_raii.h:97
#define MODE_ANSI_QUOTES
Definition: system_variables.h:102
Definition: transaction_info.h:53
Save_and_Restore_binlog_format_state(THD *thd)
Definition: thd_raii.h:157
ulong binlog_format
binlog format for this thd (see enum_binlog_format)
Definition: system_variables.h:245
#define OPTION_NOT_AUTOCOMMIT
Definition: query_options.h:73
#define OPTION_BIN_LOG
Definition: query_options.h:72
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
~Save_and_Restore_binlog_format_state()
Definition: thd_raii.h:168
bool skip_gtid_rollback
Definition: sql_class.h:3442
ulong m_global_binlog_format
Definition: thd_raii.h:177
bool m_save_is_operating_substatement_implicitly
Definition: thd_raii.h:105
sql_mode_t sql_mode
which non-standard SQL behaviour should be enabled
Definition: system_variables.h:202
Disable_sql_log_bin_guard(THD *thd)
Definition: thd_raii.h:132
~Disable_autocommit_guard()
Definition: thd_raii.h:65
~Swap_mem_root_guard()
Definition: thd_raii.h:239
RAII class which allows to temporary disable updating Gtid_state.
Definition: thd_raii.h:86
bool m_save_skip_gtid_rollback
Definition: thd_raii.h:106
ulonglong m_save_option_bits
Definition: thd_raii.h:79
#define MODE_PIPES_AS_CONCAT
Definition: system_variables.h:101
Definition: transaction_info.h:53
Disable_binlog_guard(THD *thd)
Definition: thd_raii.h:115
Disable_autocommit_guard(THD *thd)
Definition: thd_raii.h:50
THD * m_thd
Definition: thd_raii.h:242
RAII class to temporarily disable binlogging.
Definition: thd_raii.h:113
static MEM_ROOT mem_root
Definition: client_plugin.cc:107
MEM_ROOT * mem_root
Definition: sql_class.h:238
THD *const m_thd
Definition: thd_raii.h:126
MEM_ROOT * m_old_mem_root
Definition: thd_raii.h:243
const sql_mode_t m_old_sql_mode
Definition: thd_raii.h:226
bool is_operating_substatement_implicitly
Definition: sql_class.h:1941
#define OPTION_AUTOCOMMIT
always the opposite of OPTION_NOT_AUTOCOMMIT except when in fix_autocommit()
Definition: query_options.h:62
statement-based
Definition: system_variables.h:46
Sql_mode_parse_guard(THD *thd)
Definition: thd_raii.h:190
void clear_current_stmt_binlog_format_row()
Definition: sql_class.h:3033
ulonglong sql_mode_t
Definition: dd_event.h:36
~Disable_sql_log_bin_guard()
Definition: thd_raii.h:137
void set_current_stmt_binlog_format_row()
Definition: sql_class.h:3028
Swap_mem_root_guard(THD *thd, MEM_ROOT *mem_root)
Definition: thd_raii.h:234
static const char * sql_mode
Definition: mysqlslap.cc:193
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:77
const bool m_binlog_disabled
Definition: thd_raii.h:127
unsigned long ulong
Definition: my_inttypes.h:48
RAII class to temporarily turn off SQL modes that affect parsing of expressions.
Definition: thd_raii.h:188
#define MODE_IGNORE_SPACE
Definition: system_variables.h:103
THD * m_thd
Definition: thd_raii.h:78
Disable_gtid_state_update_guard(THD *thd)
Definition: thd_raii.h:88
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778
Definition: thd_raii.h:130
enum_binlog_format m_current_stmt_binlog_format
Definition: thd_raii.h:178