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