MySQL  8.0.17
Source Code Documentation
thd_raii.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, 2018, 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 
130 /**
131  RAII class which allows to save, clear and store binlog format state
132  There are two variables in THD class that will decide the binlog
133  format of a statement
134  i) THD::current_stmt_binlog_format
135  ii) THD::variables.binlog_format
136  Saving or Clearing or Storing of binlog format state should be done
137  for these two variables together all the time.
138 */
140  public:
142  : m_thd(thd),
143  m_global_binlog_format(thd->variables.binlog_format),
147 
150  }
151 
157  }
158 
159  private:
163 };
164 
165 /**
166  RAII class to temporarily turn off SQL modes that affect parsing
167  of expressions. Can also be used when printing expressions even
168  if it turns off more SQL modes than strictly necessary for it
169  (these extra modes are harmless as they do not affect expression
170  printing).
171 */
173  public:
175  : m_thd(thd), m_old_sql_mode(thd->variables.sql_mode) {
176  /*
177  Switch off modes which can prevent normal parsing of expressions:
178 
179  - MODE_REAL_AS_FLOAT affect only CREATE TABLE parsing
180  + MODE_PIPES_AS_CONCAT affect expression parsing
181  + MODE_ANSI_QUOTES affect expression parsing
182  + MODE_IGNORE_SPACE affect expression parsing
183  - MODE_NOT_USED not used :)
184  * MODE_ONLY_FULL_GROUP_BY affect execution
185  * MODE_NO_UNSIGNED_SUBTRACTION affect execution
186  - MODE_NO_DIR_IN_CREATE affect table creation only
187  - MODE_POSTGRESQL compounded from other modes
188  - MODE_ORACLE compounded from other modes
189  - MODE_MSSQL compounded from other modes
190  - MODE_DB2 compounded from other modes
191  - MODE_MAXDB affect only CREATE TABLE parsing
192  - MODE_NO_KEY_OPTIONS affect only SHOW
193  - MODE_NO_TABLE_OPTIONS affect only SHOW
194  - MODE_NO_FIELD_OPTIONS affect only SHOW
195  - MODE_MYSQL323 affect only SHOW
196  - MODE_MYSQL40 affect only SHOW
197  - MODE_ANSI compounded from other modes
198  (+ transaction mode)
199  ? MODE_NO_AUTO_VALUE_ON_ZERO affect UPDATEs
200  + MODE_NO_BACKSLASH_ESCAPES affect expression parsing
201  */
204  }
205 
207 
208  private:
211 };
212 
213 /**
214  RAII class to temporarily swap thd->mem_root to a different mem_root.
215 */
217  public:
219  : m_thd(thd), m_old_mem_root(thd->mem_root) {
220  thd->mem_root = mem_root;
221  }
222 
224 
225  private:
228 };
229 
230 #endif // THD_RAII_INCLUDED
RAII class to temporarily swap thd->mem_root to a different mem_root.
Definition: thd_raii.h:216
unsigned long long int ulonglong
Definition: my_inttypes.h:74
~Sql_mode_parse_guard()
Definition: thd_raii.h:206
enum_binlog_format
Definition: system_variables.h:44
THD * m_thd
Definition: thd_raii.h:209
int is_current_stmt_binlog_format_row() const
Determine the binlog format of the current statement.
Definition: sql_class.h:1422
~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:160
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:956
bool is_empty(enum_trx_scope scope) const
Definition: transaction_info.h:337
Transaction_ctx * get_transaction()
Definition: sql_class.h:1700
RAII class which allows to save, clear and store binlog format state There are two variables in THD c...
Definition: thd_raii.h:139
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:141
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:183
~Save_and_Restore_binlog_format_state()
Definition: thd_raii.h:152
bool skip_gtid_rollback
Definition: sql_class.h:3447
ulong m_global_binlog_format
Definition: thd_raii.h:161
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_autocommit_guard()
Definition: thd_raii.h:65
~Swap_mem_root_guard()
Definition: thd_raii.h:223
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:226
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:237
THD *const m_thd
Definition: thd_raii.h:126
MEM_ROOT * m_old_mem_root
Definition: thd_raii.h:227
const sql_mode_t m_old_sql_mode
Definition: thd_raii.h:210
bool is_operating_substatement_implicitly
Definition: sql_class.h:1927
#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:174
void clear_current_stmt_binlog_format_row()
Definition: sql_class.h:3038
ulonglong sql_mode_t
Definition: dd_event.h:36
void set_current_stmt_binlog_format_row()
Definition: sql_class.h:3033
Swap_mem_root_guard(THD *thd, MEM_ROOT *mem_root)
Definition: thd_raii.h:218
static const char * sql_mode
Definition: mysqlslap.cc:189
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:172
#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:777
enum_binlog_format m_current_stmt_binlog_format
Definition: thd_raii.h:162