MySQL 8.0.37
Source Code Documentation
sql_cmd_dml.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 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#ifndef SQL_CMD_DML_INCLUDED
25#define SQL_CMD_DML_INCLUDED
26
27#include <assert.h>
28
29#include "sql/sql_cmd.h"
30#include "sql/sql_prepare.h"
31
32struct LEX;
33class Query_result;
34
35class Sql_cmd_dml : public Sql_cmd {
36 public:
37 /// @return true if data change statement, false if not (SELECT statement)
38 virtual bool is_data_change_stmt() const { return true; }
39
40 /**
41 Command-specific resolving (doesn't include LEX::prepare())
42
43 @param thd Current THD.
44
45 @returns false on success, true on error
46 */
47 bool prepare(THD *thd) override;
48
49 /**
50 Execute a DML statement.
51
52 @param thd thread handler
53
54 @returns false if success, true if error
55
56 @details
57 Processing a statement goes through 6 phases (parsing is already done)
58 - Prelocking
59 - Preparation
60 - Locking of tables
61 - Optimization
62 - Execution or explain
63 - Cleanup
64
65 If the statement is already prepared, this step is skipped.
66
67 The queries handled by this function are:
68
69 SELECT
70 INSERT ... SELECT
71 INSERT ... VALUES
72 REPLACE ... SELECT
73 REPLACE ... VALUES
74 UPDATE (single-table and multi-table)
75 DELETE (single-table and multi-table)
76 DO
77
78 @todo make this function also handle SET.
79 */
80 bool execute(THD *thd) override;
81
82 bool is_dml() const override { return true; }
83
84 virtual bool may_use_cursor() const { return false; }
85
86 bool is_single_table_plan() const override { return false; }
87
88 /// @return the query result associated with a prepared query
90
91 /// Set query result object for this query statement
93
94 /// Signal that root result object needs preparing in next execution
95 void set_lazy_result() { m_lazy_result = true; }
96
97 protected:
99 : Sql_cmd(),
100 lex(nullptr),
102 m_empty_query(false),
103 m_lazy_result(false) {}
104
105 /// @return true if query is guaranteed to return no data
106 /**
107 @todo Also check this for the following cases:
108 - Empty source for multi-table UPDATE and DELETE.
109 - Check empty query expression for INSERT
110 */
111 bool is_empty_query() const {
112 assert(is_prepared());
113 return m_empty_query;
114 }
115
116 /// Set statement as returning no data
118
119 /**
120 Perform a precheck of table privileges for the specific operation.
121
122 @details
123 Check that user has some relevant privileges for all tables involved in
124 the statement, e.g. SELECT privileges for tables selected from, INSERT
125 privileges for tables inserted into, etc. This function will also populate
126 Table_ref::grant with all privileges the user has for each table,
127 which is later used during checking of column privileges. Note that at
128 preparation time, views are not expanded yet. Privilege checking is thus
129 rudimentary and must be complemented with later calls to
130 Query_block::check_view_privileges().
131 The reason to call this function at such an early stage is to be able to
132 quickly reject statements for which the user obviously has insufficient
133 privileges.
134 This function is called before preparing the statement.
135 The function must also be complemented with proper privilege checks for all
136 involved columns (e.g. check_column_grant_*).
137 @see also the function comment of Query_block::prepare().
138 During execution of a prepared statement, call check_privileges() instead.
139
140 @param thd thread handler
141
142 @returns false if success, true if false
143 */
144 virtual bool precheck(THD *thd) = 0;
145
146 /**
147 Check privileges on a prepared statement, called at start of execution
148 of the statement.
149
150 @details
151 Check that user has all relevant privileges to the statement,
152 ie. INSERT privilege for columns inserted into, UPDATE privilege
153 for columns that are updated, DELETE privilege for tables that are
154 deleted from, SELECT privilege for columns that are referenced, etc.
155
156 @param thd thread handler
157
158 @returns false if success, true if false
159 */
160 virtual bool check_privileges(THD *thd) = 0;
161
162 /**
163 Read and check privileges for all tables in a DML statement.
164
165 @param thd thread handler
166
167 @returns false if success, true if false
168
169 */
171
172 /**
173 Perform the command-specific parts of DML command preparation,
174 to be called from prepare()
175
176 @param thd the current thread
177
178 @returns false if success, true if error
179 */
180 virtual bool prepare_inner(THD *thd) = 0;
181
182 /**
183 The inner parts of query optimization and execution.
184 Single-table DML operations needs to reimplement this.
185
186 @param thd Thread handler
187
188 @returns false on success, true on error
189 */
190 virtual bool execute_inner(THD *thd);
191
192 /**
193 Restore command properties before execution
194 - Bind metadata for tables and fields
195 - Restore clauses (e.g ORDER BY, GROUP BY) that were destroyed in
196 last optimization.
197 */
198 virtual bool restore_cmd_properties(THD *thd);
199
200 /// Save command properties, such as prepared query details and table props
201 virtual bool save_cmd_properties(THD *thd);
202
203 /**
204 Helper function that checks if the command is eligible for secondary engine
205 and if that's true returns the name of that eligible secondary storage
206 engine.
207
208 @return nullptr if not eligible or the name of the engine otherwise
209 */
211
212 protected:
213 LEX *lex; ///< Pointer to LEX for this statement
214 Query_result *result; ///< Pointer to object for handling of the result
215 bool m_empty_query; ///< True if query will produce no rows
216 bool m_lazy_result; ///< True: prepare query result on next execution
217};
218
219#endif /* SQL_CMD_DML_INCLUDED */
Definition: query_result.h:54
Definition: sql_cmd_dml.h:35
bool is_empty_query() const
Definition: sql_cmd_dml.h:111
bool is_dml() const override
Definition: sql_cmd_dml.h:82
virtual bool precheck(THD *thd)=0
Perform a precheck of table privileges for the specific operation.
virtual bool prepare_inner(THD *thd)=0
Perform the command-specific parts of DML command preparation, to be called from prepare()
LEX * lex
Pointer to LEX for this statement.
Definition: sql_cmd_dml.h:213
Sql_cmd_dml()
Definition: sql_cmd_dml.h:98
virtual bool may_use_cursor() const
Definition: sql_cmd_dml.h:84
virtual bool is_data_change_stmt() const
Definition: sql_cmd_dml.h:38
void set_empty_query()
Set statement as returning no data.
Definition: sql_cmd_dml.h:117
void set_lazy_result()
Signal that root result object needs preparing in next execution.
Definition: sql_cmd_dml.h:95
bool m_lazy_result
True: prepare query result on next execution.
Definition: sql_cmd_dml.h:216
virtual bool check_privileges(THD *thd)=0
Check privileges on a prepared statement, called at start of execution of the statement.
bool m_empty_query
True if query will produce no rows.
Definition: sql_cmd_dml.h:215
Query_result * result
Pointer to object for handling of the result.
Definition: sql_cmd_dml.h:214
bool is_single_table_plan() const override
Definition: sql_cmd_dml.h:86
Representation of an SQL command.
Definition: sql_cmd.h:65
bool is_prepared() const
Definition: sql_cmd.h:93
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
bool prepare(THD *thd) override
Command-specific resolving (doesn't include LEX::prepare())
Definition: sql_select.cc:495
bool check_all_table_privileges(THD *thd)
Read and check privileges for all tables in a DML statement.
Definition: sql_select.cc:1169
bool execute(THD *thd) override
Execute a DML statement.
Definition: sql_select.cc:672
Query_result * query_result() const
Definition: sql_select.cc:1040
void set_query_result(Query_result *result)
Set query result object for this query statement.
Definition: sql_select.cc:1047
virtual bool save_cmd_properties(THD *thd)
Save command properties, such as prepared query details and table props.
Definition: sql_select.cc:1036
virtual bool restore_cmd_properties(THD *thd)
Restore command properties before execution.
Definition: sql_select.cc:1029
const MYSQL_LEX_CSTRING * get_eligible_secondary_engine() const
Helper function that checks if the command is eligible for secondary engine and if that's true return...
Definition: sql_select.cc:1208
virtual bool execute_inner(THD *thd)
The inner parts of query optimization and execution.
Definition: sql_select.cc:1005
Representation of an SQL command.
The LEX object currently serves three different purposes:
Definition: sql_lex.h:3709
Definition: mysql_lex_string.h:40
Definition: result.h:30