MySQL 9.1.0
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 enum enum_sql_cmd_type sql_cmd_type() const override {
83 /*
84 Somewhat unsurprisingly, anything sub-classed to Sql_cmd_dml
85 identifies as DML by default.
86 */
87 return SQL_CMD_DML;
88 }
89
90 virtual bool may_use_cursor() const { return false; }
91
92 bool is_single_table_plan() const override { return false; }
93
94 /// @return the query result associated with a prepared query
96
97 /// Set query result object for this query statement
99
100 /// Signal that root result object needs preparing in next execution
102
103 /// Signal that DML statement can have dynamic parameters
104 bool are_dynamic_parameters_allowed() const final { return true; }
105
106 protected:
108 : Sql_cmd(),
109 lex(nullptr),
111 m_empty_query(false),
112 m_lazy_result(false) {}
113
114 /// @return true if query is guaranteed to return no data
115 /**
116 @todo Also check this for the following cases:
117 - Empty source for multi-table UPDATE and DELETE.
118 - Check empty query expression for INSERT
119 */
120 bool is_empty_query() const {
121 assert(is_prepared());
122 return m_empty_query;
123 }
124
125 /// Set statement as returning no data
127
128 /**
129 Perform a precheck of table privileges for the specific operation.
130
131 @details
132 Check that user has some relevant privileges for all tables involved in
133 the statement, e.g. SELECT privileges for tables selected from, INSERT
134 privileges for tables inserted into, etc. This function will also populate
135 Table_ref::grant with all privileges the user has for each table,
136 which is later used during checking of column privileges. Note that at
137 preparation time, views are not expanded yet. Privilege checking is thus
138 rudimentary and must be complemented with later calls to
139 Query_block::check_view_privileges().
140 The reason to call this function at such an early stage is to be able to
141 quickly reject statements for which the user obviously has insufficient
142 privileges.
143 This function is called before preparing the statement.
144 The function must also be complemented with proper privilege checks for all
145 involved columns (e.g. check_column_grant_*).
146 @see also the function comment of Query_block::prepare().
147 During execution of a prepared statement, call check_privileges() instead.
148
149 @param thd thread handler
150
151 @returns false if success, true if false
152 */
153 virtual bool precheck(THD *thd) = 0;
154
155 /**
156 Check privileges on a prepared statement, called at start of execution
157 of the statement.
158
159 @details
160 Check that user has all relevant privileges to the statement,
161 ie. INSERT privilege for columns inserted into, UPDATE privilege
162 for columns that are updated, DELETE privilege for tables that are
163 deleted from, SELECT privilege for columns that are referenced, etc.
164
165 @param thd thread handler
166
167 @returns false if success, true if false
168 */
169 virtual bool check_privileges(THD *thd) = 0;
170
171 /**
172 Read and check privileges for all tables in a DML statement.
173
174 @param thd thread handler
175
176 @returns false if success, true if false
177
178 */
180
181 /**
182 Perform the command-specific parts of DML command preparation,
183 to be called from prepare()
184
185 @param thd the current thread
186
187 @returns false if success, true if error
188 */
189 virtual bool prepare_inner(THD *thd) = 0;
190
191 /**
192 The inner parts of query optimization and execution.
193 Single-table DML operations needs to reimplement this.
194
195 @param thd Thread handler
196
197 @returns false on success, true on error
198 */
199 virtual bool execute_inner(THD *thd);
200
201 /**
202 Restore command properties before execution
203 - Bind metadata for tables and fields
204 - Restore clauses (e.g ORDER BY, GROUP BY) that were destroyed in
205 last optimization.
206 */
207 virtual bool restore_cmd_properties(THD *thd);
208
209 /// Save command properties, such as prepared query details and table props
210 virtual bool save_cmd_properties(THD *thd);
211
212 /**
213 Helper function that checks if the command is eligible for secondary engine
214 and if that's true returns the name of that eligible secondary storage
215 engine.
216
217 @return nullptr if not eligible or the name of the engine otherwise
218 */
220
221 protected:
222 LEX *lex; ///< Pointer to LEX for this statement
223 Query_result *result; ///< Pointer to object for handling of the result
224 bool m_empty_query; ///< True if query will produce no rows
225 bool m_lazy_result; ///< True: prepare query result on next execution
226};
227
228#endif /* SQL_CMD_DML_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Definition: query_result.h:58
Definition: sql_cmd_dml.h:35
bool is_empty_query() const
Definition: sql_cmd_dml.h:120
virtual bool precheck(THD *thd)=0
Perform a precheck of table privileges for the specific operation.
bool are_dynamic_parameters_allowed() const final
Signal that DML statement can have dynamic parameters.
Definition: sql_cmd_dml.h:104
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:222
Sql_cmd_dml()
Definition: sql_cmd_dml.h:107
virtual bool may_use_cursor() const
Definition: sql_cmd_dml.h:90
enum enum_sql_cmd_type sql_cmd_type() const override
Definition: sql_cmd_dml.h:82
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:126
void set_lazy_result()
Signal that root result object needs preparing in next execution.
Definition: sql_cmd_dml.h:101
bool m_lazy_result
True: prepare query result on next execution.
Definition: sql_cmd_dml.h:225
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:224
Query_result * result
Pointer to object for handling of the result.
Definition: sql_cmd_dml.h:223
bool is_single_table_plan() const override
Definition: sql_cmd_dml.h:92
Representation of an SQL command.
Definition: sql_cmd.h:83
bool is_prepared() const
Definition: sql_cmd.h:111
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
bool prepare(THD *thd) override
Command-specific resolving (doesn't include LEX::prepare())
Definition: sql_select.cc:512
bool check_all_table_privileges(THD *thd)
Read and check privileges for all tables in a DML statement.
Definition: sql_select.cc:1270
bool execute(THD *thd) override
Execute a DML statement.
Definition: sql_select.cc:685
const MYSQL_LEX_CSTRING * get_eligible_secondary_engine(THD *thd) const
Helper function that checks if the command is eligible for secondary engine and if that's true return...
Definition: sql_select.cc:1309
Query_result * query_result() const
Definition: sql_select.cc:1140
void set_query_result(Query_result *result)
Set query result object for this query statement.
Definition: sql_select.cc:1147
virtual bool save_cmd_properties(THD *thd)
Save command properties, such as prepared query details and table props.
Definition: sql_select.cc:1136
virtual bool restore_cmd_properties(THD *thd)
Restore command properties before execution.
Definition: sql_select.cc:1129
virtual bool execute_inner(THD *thd)
The inner parts of query optimization and execution.
Definition: sql_select.cc:1091
Representation of an SQL command.
enum_sql_cmd_type
What type of Sql_cmd we're dealing with (DML, DDL, ...).
Definition: sql_cmd.h:53
@ SQL_CMD_DML
Definition: sql_cmd.h:56
The LEX object currently serves three different purposes:
Definition: sql_lex.h:3844
Definition: mysql_lex_string.h:40
Definition: result.h:30