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