MySQL 8.4.3
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 protected:
105 : Sql_cmd(),
106 lex(nullptr),
108 m_empty_query(false),
109 m_lazy_result(false) {}
110
111 /// @return true if query is guaranteed to return no data
112 /**
113 @todo Also check this for the following cases:
114 - Empty source for multi-table UPDATE and DELETE.
115 - Check empty query expression for INSERT
116 */
117 bool is_empty_query() const {
118 assert(is_prepared());
119 return m_empty_query;
120 }
121
122 /// Set statement as returning no data
124
125 /**
126 Perform a precheck of table privileges for the specific operation.
127
128 @details
129 Check that user has some relevant privileges for all tables involved in
130 the statement, e.g. SELECT privileges for tables selected from, INSERT
131 privileges for tables inserted into, etc. This function will also populate
132 Table_ref::grant with all privileges the user has for each table,
133 which is later used during checking of column privileges. Note that at
134 preparation time, views are not expanded yet. Privilege checking is thus
135 rudimentary and must be complemented with later calls to
136 Query_block::check_view_privileges().
137 The reason to call this function at such an early stage is to be able to
138 quickly reject statements for which the user obviously has insufficient
139 privileges.
140 This function is called before preparing the statement.
141 The function must also be complemented with proper privilege checks for all
142 involved columns (e.g. check_column_grant_*).
143 @see also the function comment of Query_block::prepare().
144 During execution of a prepared statement, call check_privileges() instead.
145
146 @param thd thread handler
147
148 @returns false if success, true if false
149 */
150 virtual bool precheck(THD *thd) = 0;
151
152 /**
153 Check privileges on a prepared statement, called at start of execution
154 of the statement.
155
156 @details
157 Check that user has all relevant privileges to the statement,
158 ie. INSERT privilege for columns inserted into, UPDATE privilege
159 for columns that are updated, DELETE privilege for tables that are
160 deleted from, SELECT privilege for columns that are referenced, etc.
161
162 @param thd thread handler
163
164 @returns false if success, true if false
165 */
166 virtual bool check_privileges(THD *thd) = 0;
167
168 /**
169 Read and check privileges for all tables in a DML statement.
170
171 @param thd thread handler
172
173 @returns false if success, true if false
174
175 */
177
178 /**
179 Perform the command-specific parts of DML command preparation,
180 to be called from prepare()
181
182 @param thd the current thread
183
184 @returns false if success, true if error
185 */
186 virtual bool prepare_inner(THD *thd) = 0;
187
188 /**
189 The inner parts of query optimization and execution.
190 Single-table DML operations needs to reimplement this.
191
192 @param thd Thread handler
193
194 @returns false on success, true on error
195 */
196 virtual bool execute_inner(THD *thd);
197
198 /**
199 Restore command properties before execution
200 - Bind metadata for tables and fields
201 - Restore clauses (e.g ORDER BY, GROUP BY) that were destroyed in
202 last optimization.
203 */
204 virtual bool restore_cmd_properties(THD *thd);
205
206 /// Save command properties, such as prepared query details and table props
207 virtual bool save_cmd_properties(THD *thd);
208
209 /**
210 Helper function that checks if the command is eligible for secondary engine
211 and if that's true returns the name of that eligible secondary storage
212 engine.
213
214 @return nullptr if not eligible or the name of the engine otherwise
215 */
217
218 protected:
219 LEX *lex; ///< Pointer to LEX for this statement
220 Query_result *result; ///< Pointer to object for handling of the result
221 bool m_empty_query; ///< True if query will produce no rows
222 bool m_lazy_result; ///< True: prepare query result on next execution
223};
224
225#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:117
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:219
Sql_cmd_dml()
Definition: sql_cmd_dml.h:104
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:123
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:222
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:221
Query_result * result
Pointer to object for handling of the result.
Definition: sql_cmd_dml.h:220
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:490
bool check_all_table_privileges(THD *thd)
Read and check privileges for all tables in a DML statement.
Definition: sql_select.cc:1208
bool execute(THD *thd) override
Execute a DML statement.
Definition: sql_select.cc:676
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:1247
Query_result * query_result() const
Definition: sql_select.cc:1078
void set_query_result(Query_result *result)
Set query result object for this query statement.
Definition: sql_select.cc:1085
virtual bool save_cmd_properties(THD *thd)
Save command properties, such as prepared query details and table props.
Definition: sql_select.cc:1074
virtual bool restore_cmd_properties(THD *thd)
Restore command properties before execution.
Definition: sql_select.cc:1067
virtual bool execute_inner(THD *thd)
The inner parts of query optimization and execution.
Definition: sql_select.cc:1031
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:3823
Definition: mysql_lex_string.h:40
Definition: result.h:30