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