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