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