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