MySQL 8.1.0
Source Code Documentation
sql_cmd.h
Go to the documentation of this file.
1/* Copyright (c) 2009, 2023, Oracle and/or its affiliates.
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/**
24 @file sql/sql_cmd.h
25 Representation of an SQL command.
26*/
27
28#ifndef SQL_CMD_INCLUDED
29#define SQL_CMD_INCLUDED
30
31#include <assert.h>
32
33#include "my_sqlcommand.h"
35
36class THD;
38struct handlerton;
39struct MYSQL_LEX_STRING;
41
42/**
43 What type of Sql_cmd we're dealing with (DML, DDL, other).
44
45 "Other" may be used for commands that are neither DML or DDL, such as
46 shutdown.
47
48 Theoretically, a command can run through code paths of both DDL and DML
49 (e.g. CREATE TABLE ... AS SELECT ...), but at this point, a command
50 must identify as only one thing.
51*/
57};
58
59/**
60 Representation of an SQL command.
61
62 This class is an interface between the parser and the runtime.
63 The parser builds the appropriate derived classes of Sql_cmd
64 to represent a SQL statement in the parsed tree.
65 The execute() method in the derived classes of Sql_cmd contain the runtime
66 implementation.
67 Note that this interface is used for SQL statements recently implemented,
68 the code for older statements tend to load the LEX structure with more
69 attributes instead.
70 Implement new statements by sub-classing Sql_cmd, as this improves
71 code modularity (see the 'big switch' in dispatch_command()), and decreases
72 the total size of the LEX structure (therefore saving memory in stored
73 programs).
74 The recommended name of a derived class of Sql_cmd is Sql_cmd_<derived>.
75
76 Notice that the Sql_cmd class should not be confused with the Statement class.
77 Statement is a class that is used to manage an SQL command or a set
78 of SQL commands. When the SQL statement text is analyzed, the parser will
79 create one or more Sql_cmd objects to represent the actual SQL commands.
80*/
81class Sql_cmd {
82 private:
83 Sql_cmd(const Sql_cmd &); // No copy constructor wanted
84 void operator=(Sql_cmd &); // No assignment operator wanted
85
86 public:
87 /**
88 @brief Return the command code for this statement
89 */
91
92 /**
93 @return true if object represents a preparable statement, ie. a query
94 that is prepared with a PREPARE statement and executed with an EXECUTE
95 statement. False is returned for regular statements (non-preparable
96 statements) that are executed directly. Also false if statement is part
97 of a stored procedure.
98 */
100 return m_owner != nullptr && !m_part_of_sp;
101 }
102 /**
103 @return true if statement is regular, ie not prepared statement and not
104 part of stored procedure.
105 */
106 bool is_regular() const { return m_owner == nullptr && !m_part_of_sp; }
107
108 /// @return true if this statement is prepared
109 bool is_prepared() const { return m_prepared; }
110
111 /**
112 Prepare this SQL statement.
113
114 param thd the current thread
115
116 @returns false if success, true if error
117 */
118 virtual bool prepare(THD *) {
119 // Default behavior for a statement is to have no preparation code.
120 /* purecov: begin inspected */
121 assert(!is_prepared());
122 set_prepared();
123 return false;
124 /* purecov: end */
125 }
126
127 /**
128 Execute this SQL statement.
129 @param thd the current thread.
130 @returns false if success, true if error
131 */
132 virtual bool execute(THD *thd) = 0;
133
134 /**
135 Command-specific reinitialization before execution of prepared statement
136
137 param thd Current THD.
138 */
139 virtual void cleanup(THD *) { m_secondary_engine = nullptr; }
140
141 /// Set the owning prepared statement
143 assert(!m_part_of_sp);
144 m_owner = stmt;
145 }
146
147 /// Get the owning prepared statement
148 Prepared_statement *owner() const { return m_owner; }
149
150 /**
151 Mark statement as part of procedure. Such statements can be executed
152 multiple times, the first execute() call will also prepare it.
153 */
155 assert(!m_part_of_sp && m_owner == nullptr);
156 m_part_of_sp = true;
157 }
158 /// @returns true if statement is part of a stored procedure
159 bool is_part_of_sp() const { return m_part_of_sp; }
160
161 /// @return SQL command type (DML, DDL, ... -- "undetermined" by default)
162 virtual enum enum_sql_cmd_type sql_cmd_type() const {
164 }
165
166 /// @return true if implemented as single table plan, DML statement only
167 virtual bool is_single_table_plan() const {
168 /* purecov: begin inspected */
169 assert(sql_cmd_type() == SQL_CMD_DML);
170 return false;
171 /* purecov: end */
172 }
173
174 virtual bool accept(THD *, Select_lex_visitor *) { return false; }
175
176 /**
177 Is this statement of a type and on a form that makes it eligible
178 for execution in a secondary storage engine?
179
180 @return the name of the secondary storage engine, or nullptr if
181 the statement is not eligible for execution in a secondary storage
182 engine
183 */
185 return nullptr;
186 }
187
188 /**
189 Disable use of secondary storage engines in this statement. After
190 a call to this function, the statement will not try to use a
191 secondary storage engine until it is reprepared.
192 */
194 assert(m_secondary_engine == nullptr);
196 }
197
198 /**
199 Has use of secondary storage engines been disabled for this statement?
200 */
203 }
204
205 /**
206 Mark the current statement as using a secondary storage engine.
207 This function must be called before the statement starts opening
208 tables in a secondary engine.
209 */
212 m_secondary_engine = hton;
213 }
214
215 /**
216 Is this statement using a secondary storage engine?
217 @note that this is reliable during optimization and afterwards; during
218 preparation, if this is an explicit preparation (SQL PREPARE, C API
219 PREPARE, and automatic repreparation), it may be false as RAPID tables have
220 not yet been opened. Therefore, during preparation, it is safer to test
221 THD::secondary_engine_optimization().
222 */
224 return m_secondary_engine != nullptr;
225 }
226
227 /**
228 Get the handlerton of the secondary engine that is used for
229 executing this statement, or nullptr if a secondary engine is not
230 used.
231 */
233
236 }
237
240 }
241
242 protected:
244
245 virtual ~Sql_cmd() {
246 /*
247 Sql_cmd objects are allocated in thd->mem_root.
248 In MySQL, the C++ destructor is never called, the underlying MEM_ROOT is
249 simply destroyed instead.
250 Do not rely on the destructor for any cleanup.
251 */
252 assert(false);
253 }
254
255 /// Set this statement as prepared
256 void set_prepared() { m_prepared = true; }
257
258 private:
259 Prepared_statement *m_owner; /// Owning prepared statement, NULL if non-prep.
260 bool m_part_of_sp; /// True when statement is part of stored proc.
261 bool m_prepared; /// True when statement has been prepared
262
263 /**
264 Tells if a secondary storage engine can be used for this
265 statement. If it is false, use of a secondary storage engine will
266 not be considered for executing this statement.
267 */
269
270 /**
271 Keeps track of whether the statement was prepared optional
272 transformation.
273 */
275
276 /**
277 The secondary storage engine to use for execution of this
278 statement, if any, or nullptr if the primary engine is used.
279 This property is reset at the start of each execution.
280 */
282};
283
284#endif // SQL_CMD_INCLUDED
Prepared_statement: a statement that can contain placeholders.
Definition: sql_prepare.h:345
Abstract base class for traversing the Query_block tree.
Definition: select_lex_visitor.h:39
Representation of an SQL command.
Definition: sql_cmd.h:81
bool m_part_of_sp
Owning prepared statement, NULL if non-prep.
Definition: sql_cmd.h:260
virtual bool prepare(THD *)
Prepare this SQL statement.
Definition: sql_cmd.h:118
void use_secondary_storage_engine(const handlerton *hton)
Mark the current statement as using a secondary storage engine.
Definition: sql_cmd.h:210
virtual bool execute(THD *thd)=0
Execute this SQL statement.
const handlerton * m_secondary_engine
The secondary storage engine to use for execution of this statement, if any, or nullptr if the primar...
Definition: sql_cmd.h:281
void set_as_part_of_sp()
Mark statement as part of procedure.
Definition: sql_cmd.h:154
Sql_cmd(const Sql_cmd &)
bool using_secondary_storage_engine() const
Is this statement using a secondary storage engine?
Definition: sql_cmd.h:223
void operator=(Sql_cmd &)
virtual enum enum_sql_cmd_type sql_cmd_type() const
Definition: sql_cmd.h:162
virtual void cleanup(THD *)
Command-specific reinitialization before execution of prepared statement.
Definition: sql_cmd.h:139
virtual const MYSQL_LEX_CSTRING * eligible_secondary_storage_engine() const
Is this statement of a type and on a form that makes it eligible for execution in a secondary storage...
Definition: sql_cmd.h:184
bool is_optional_transform_prepared()
Definition: sql_cmd.h:238
bool m_prepared_with_optional_transform
Keeps track of whether the statement was prepared optional transformation.
Definition: sql_cmd.h:274
bool is_part_of_sp() const
Definition: sql_cmd.h:159
Sql_cmd()
Definition: sql_cmd.h:243
bool is_regular() const
Definition: sql_cmd.h:106
virtual ~Sql_cmd()
Definition: sql_cmd.h:245
void set_prepared()
Set this statement as prepared.
Definition: sql_cmd.h:256
virtual bool is_single_table_plan() const
Definition: sql_cmd.h:167
Prepared_statement * owner() const
Get the owning prepared statement.
Definition: sql_cmd.h:148
bool m_secondary_engine_enabled
True when statement has been prepared.
Definition: sql_cmd.h:268
virtual bool accept(THD *, Select_lex_visitor *)
Definition: sql_cmd.h:174
void disable_secondary_storage_engine()
Disable use of secondary storage engines in this statement.
Definition: sql_cmd.h:193
bool is_prepared() const
Definition: sql_cmd.h:109
const handlerton * secondary_engine() const
Get the handlerton of the secondary engine that is used for executing this statement,...
Definition: sql_cmd.h:232
virtual enum_sql_command sql_command_code() const =0
Return the command code for this statement.
void set_optional_transform_prepared(bool value)
Definition: sql_cmd.h:234
bool needs_explicit_preparation() const
Definition: sql_cmd.h:99
bool m_prepared
True when statement is part of stored proc.
Definition: sql_cmd.h:261
bool secondary_storage_engine_disabled() const
Has use of secondary storage engines been disabled for this statement?
Definition: sql_cmd.h:201
Prepared_statement * m_owner
Definition: sql_cmd.h:259
void set_owner(Prepared_statement *stmt)
Set the owning prepared statement.
Definition: sql_cmd.h:142
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:33
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:221
enum_sql_command
Definition: my_sqlcommand.h:45
Visitor interface for parse trees.
enum_sql_cmd_type
What type of Sql_cmd we're dealing with (DML, DDL, other).
Definition: sql_cmd.h:52
@ SQL_CMD_DDL
Definition: sql_cmd.h:54
@ SQL_CMD_OTHER
Definition: sql_cmd.h:56
@ SQL_CMD_UNDETERMINED
Definition: sql_cmd.h:53
@ SQL_CMD_DML
Definition: sql_cmd.h:55
Definition: mysql_lex_string.h:39
Definition: mysql_lex_string.h:34
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2661