MySQL 8.4.0
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 Command-specific reinitialization before execution of prepared statement
138
139 param thd Current THD.
140 */
141 virtual void cleanup(THD *) { m_secondary_engine = nullptr; }
142
143 /// Set the owning prepared statement
145 assert(!m_part_of_sp);
146 m_owner = stmt;
147 }
148
149 /// Get the owning prepared statement
150 Prepared_statement *owner() const { return m_owner; }
151
152 /**
153 Mark statement as part of procedure. Such statements can be executed
154 multiple times, the first execute() call will also prepare it.
155 */
157 assert(!m_part_of_sp && m_owner == nullptr);
158 m_part_of_sp = true;
159 }
160 /// @returns true if statement is part of a stored procedure
161 bool is_part_of_sp() const { return m_part_of_sp; }
162
163 /// @return SQL command type (DML, DDL, ... -- "undetermined" by default)
164 virtual enum enum_sql_cmd_type sql_cmd_type() const {
166 }
167
168 /// @return true if implemented as single table plan, DML statement only
169 virtual bool is_single_table_plan() const {
170 /* purecov: begin inspected */
171 assert(sql_cmd_type() == SQL_CMD_DML);
172 return false;
173 /* purecov: end */
174 }
175
176 virtual bool accept(THD *, Select_lex_visitor *) { return false; }
177
178 /**
179 Is this statement of a type and on a form that makes it eligible
180 for execution in a secondary storage engine?
181
182 @return the name of the secondary storage engine, or nullptr if
183 the statement is not eligible for execution in a secondary storage
184 engine
185 */
187 THD *) const {
188 return nullptr;
189 }
190
191 /** @return true if the operation is BULK LOAD. */
192 virtual bool is_bulk_load() const { return false; }
193
194 /**
195 Disable use of secondary storage engines in this statement. After
196 a call to this function, the statement will not try to use a
197 secondary storage engine until it is reprepared.
198 */
200 assert(m_secondary_engine == nullptr);
202 }
203
205
206 /**
207 Has use of secondary storage engines been disabled for this statement?
208 */
211 }
212
213 /**
214 Mark the current statement as using a secondary storage engine.
215 This function must be called before the statement starts opening
216 tables in a secondary engine.
217 */
220 m_secondary_engine = hton;
221 }
222
223 /**
224 Is this statement using a secondary storage engine?
225 @note that this is reliable during optimization and afterwards; during
226 preparation, if this is an explicit preparation (SQL PREPARE, C API
227 PREPARE, and automatic repreparation), it may be false as RAPID tables have
228 not yet been opened. Therefore, during preparation, it is safer to test
229 THD::secondary_engine_optimization().
230 */
232 return m_secondary_engine != nullptr;
233 }
234
235 /**
236 Get the handlerton of the secondary engine that is used for
237 executing this statement, or nullptr if a secondary engine is not
238 used.
239 */
241
244 }
245
248 }
249
250 protected:
252
253 virtual ~Sql_cmd() {
254 /*
255 Sql_cmd objects are allocated in thd->mem_root.
256 In MySQL, the C++ destructor is never called, the underlying MEM_ROOT is
257 simply destroyed instead.
258 Do not rely on the destructor for any cleanup.
259 */
260 assert(false);
261 }
262
263 /// Set this statement as prepared
264 void set_prepared() { m_prepared = true; }
265
266 private:
267 Prepared_statement *m_owner; /// Owning prepared statement, NULL if non-prep.
268 bool m_part_of_sp; /// True when statement is part of stored proc.
269 bool m_prepared; /// True when statement has been prepared
270
271 /**
272 Tells if a secondary storage engine can be used for this
273 statement. If it is false, use of a secondary storage engine will
274 not be considered for executing this statement.
275 */
277
278 /**
279 Keeps track of whether the statement was prepared optional
280 transformation.
281 */
283
284 /**
285 The secondary storage engine to use for execution of this
286 statement, if any, or nullptr if the primary engine is used.
287 This property is reset at the start of each execution.
288 */
290};
291
292#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:268
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:218
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:289
void set_as_part_of_sp()
Mark statement as part of procedure.
Definition: sql_cmd.h:156
Sql_cmd(const Sql_cmd &)
bool using_secondary_storage_engine() const
Is this statement using a secondary storage engine?
Definition: sql_cmd.h:231
void operator=(Sql_cmd &)
virtual enum enum_sql_cmd_type sql_cmd_type() const
Definition: sql_cmd.h:164
virtual void cleanup(THD *)
Command-specific reinitialization before execution of prepared statement.
Definition: sql_cmd.h:141
virtual bool is_bulk_load() const
Definition: sql_cmd.h:192
bool is_optional_transform_prepared()
Definition: sql_cmd.h:246
bool m_prepared_with_optional_transform
Keeps track of whether the statement was prepared optional transformation.
Definition: sql_cmd.h:282
bool is_part_of_sp() const
Definition: sql_cmd.h:161
Sql_cmd()
Definition: sql_cmd.h:251
bool is_regular() const
Definition: sql_cmd.h:108
virtual ~Sql_cmd()
Definition: sql_cmd.h:253
void set_prepared()
Set this statement as prepared.
Definition: sql_cmd.h:264
void enable_secondary_storage_engine()
Definition: sql_cmd.h:204
virtual bool is_single_table_plan() const
Definition: sql_cmd.h:169
Prepared_statement * owner() const
Get the owning prepared statement.
Definition: sql_cmd.h:150
bool m_secondary_engine_enabled
True when statement has been prepared.
Definition: sql_cmd.h:276
virtual bool accept(THD *, Select_lex_visitor *)
Definition: sql_cmd.h:176
void disable_secondary_storage_engine()
Disable use of secondary storage engines in this statement.
Definition: sql_cmd.h:199
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:240
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:186
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:242
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:269
bool secondary_storage_engine_disabled() const
Has use of secondary storage engines been disabled for this statement?
Definition: sql_cmd.h:209
Prepared_statement * m_owner
Definition: sql_cmd.h:267
void set_owner(Prepared_statement *stmt)
Set the owning prepared statement.
Definition: sql_cmd.h:144
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:2733