1 /* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
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 "my_dbug.h"
27 #include "sql/sql_cmd.h"
28 
29 struct LEX;
30 class Query_result;
31 
32 class Sql_cmd_dml : public Sql_cmd {
33  public:
34   /// @return true if data change statement, false if not (SELECT statement)
is_data_change_stmt()35   virtual bool is_data_change_stmt() const { return true; }
36 
37   /**
38     Command-specific resolving (doesn't include LEX::prepare())
39 
40     @param thd  Current THD.
41 
42     @returns false on success, true on error
43   */
44   virtual bool prepare(THD *thd);
45 
46   /**
47     Execute this query once.
48 
49     @param thd Thread handler
50 
51     @returns false on success, true on error
52   */
53   virtual bool execute(THD *thd);
54 
is_dml()55   virtual bool is_dml() const { return true; }
56 
is_single_table_plan()57   virtual bool is_single_table_plan() const { return false; }
58 
59  protected:
Sql_cmd_dml()60   Sql_cmd_dml()
61       : Sql_cmd(), lex(nullptr), result(nullptr), m_empty_query(false) {}
62 
63   /// @return true if query is guaranteed to return no data
64   /**
65     @todo Also check this for the following cases:
66           - Empty source for multi-table UPDATE and DELETE.
67           - Check empty query expression for INSERT
68   */
is_empty_query()69   bool is_empty_query() const {
70     DBUG_ASSERT(is_prepared());
71     return m_empty_query;
72   }
73 
74   /// Set statement as returning no data
set_empty_query()75   void set_empty_query() { m_empty_query = true; }
76 
77   /**
78     Perform a precheck of table privileges for the specific operation.
79 
80     @details
81     Check that user has some relevant privileges for all tables involved in
82     the statement, e.g. SELECT privileges for tables selected from, INSERT
83     privileges for tables inserted into, etc. This function will also populate
84     TABLE_LIST::grant with all privileges the user has for each table, which
85     is later used during checking of column privileges.
86     Note that at preparation time, views are not expanded yet. Privilege
87     checking is thus rudimentary and must be complemented with later calls to
88     SELECT_LEX::check_view_privileges().
89     The reason to call this function at such an early stage is to be able to
90     quickly reject statements for which the user obviously has insufficient
91     privileges.
92 
93     @param thd thread handler
94 
95     @returns false if success, true if false
96   */
97   virtual bool precheck(THD *thd) = 0;
98 
99   /**
100     Perform the command-specific parts of DML command preparation,
101     to be called from prepare()
102 
103     @param thd the current thread
104 
105     @returns false if success, true if error
106   */
107   virtual bool prepare_inner(THD *thd) = 0;
108 
109   /**
110     The inner parts of query optimization and execution.
111     Single-table DML operations needs to reimplement this.
112 
113     @param thd Thread handler
114 
115     @returns false on success, true on error
116   */
117   virtual bool execute_inner(THD *thd);
118 
119  protected:
120   LEX *lex;              ///< Pointer to LEX for this statement
121   Query_result *result;  ///< Pointer to object for handling of the result
122   bool m_empty_query;    ///< True if query will produce no rows
123 };
124 
125 #endif /* SQL_CMD_DML_INCLUDED */
126