1 /* Copyright (c) 2006, 2020, 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_DELETE_INCLUDED
24 #define SQL_DELETE_INCLUDED
25 
26 #include <stddef.h>
27 #include <sys/types.h>
28 
29 #include "my_base.h"  // ha_rows
30 #include "my_sqlcommand.h"
31 #include "my_table_map.h"
32 #include "sql/query_result.h"  // Query_result_interceptor
33 #include "sql/sql_cmd_dml.h"   // Sql_cmd_dml
34 
35 class Item;
36 class SELECT_LEX_UNIT;
37 class Select_lex_visitor;
38 class THD;
39 class Unique;
40 struct TABLE;
41 struct TABLE_LIST;
42 template <class T>
43 class List;
44 template <typename T>
45 class SQL_I_List;
46 
47 class Query_result_delete final : public Query_result_interceptor {
48   /// Pointers to temporary files used for delayed deletion of rows
49   Unique **tempfiles;
50   /// Pointers to table objects matching tempfiles
51   TABLE **tables;
52   /// Number of tables being deleted from
53   uint delete_table_count;
54   /// Number of rows produced by the join
55   ha_rows found_rows;
56   /// Number of rows deleted
57   ha_rows deleted_rows;
58   /// Handler error status for the operation.
59   int error;
60   /// Map of all tables to delete rows from
61   table_map delete_table_map;
62   /// Map of tables to delete from immediately
63   table_map delete_immediate;
64   // Map of transactional tables to be deleted from
65   table_map transactional_table_map;
66   /// Map of non-transactional tables to be deleted from
67   table_map non_transactional_table_map;
68   /// True if the full delete operation is complete
69   bool delete_completed;
70   /// True if some actual delete operation against non-transactional table done
71   bool non_transactional_deleted;
72   /*
73      error handling (rollback and binlogging) can happen in send_eof()
74      so that afterward send_error() needs to find out that.
75   */
76   bool error_handled;
77 
78  public:
Query_result_delete()79   Query_result_delete()
80       : Query_result_interceptor(),
81         tempfiles(nullptr),
82         tables(nullptr),
83         delete_table_count(0),
84         found_rows(0),
85         deleted_rows(0),
86         error(0),
87         delete_table_map(0),
88         delete_immediate(0),
89         transactional_table_map(0),
90         non_transactional_table_map(0),
91         delete_completed(false),
92         non_transactional_deleted(false),
93         error_handled(false) {}
need_explain_interceptor()94   bool need_explain_interceptor() const override { return true; }
95   bool prepare(THD *thd, List<Item> &list, SELECT_LEX_UNIT *u) override;
96   bool send_data(THD *thd, List<Item> &items) override;
97   void send_error(THD *thd, uint errcode, const char *err) override;
98   bool optimize() override;
start_execution(THD *)99   bool start_execution(THD *) override {
100     delete_completed = false;
101     return false;
102   }
103   int do_deletes(THD *thd);
104   int do_table_deletes(THD *thd, TABLE *table);
105   bool send_eof(THD *thd) override;
num_deleted()106   inline ha_rows num_deleted() { return deleted_rows; }
107   void abort_result_set(THD *thd) override;
108   void cleanup(THD *thd) override;
109   bool immediate_update(TABLE_LIST *t) const override;
110 };
111 
112 class Sql_cmd_delete final : public Sql_cmd_dml {
113  public:
Sql_cmd_delete(bool multitable_arg,SQL_I_List<TABLE_LIST> * delete_tables_arg)114   Sql_cmd_delete(bool multitable_arg, SQL_I_List<TABLE_LIST> *delete_tables_arg)
115       : multitable(multitable_arg), delete_tables(delete_tables_arg) {}
116 
sql_command_code()117   enum_sql_command sql_command_code() const override {
118     return multitable ? SQLCOM_DELETE_MULTI : SQLCOM_DELETE;
119   }
120 
is_single_table_plan()121   bool is_single_table_plan() const override { return !multitable; }
122 
123   virtual bool accept(THD *thd, Select_lex_visitor *visitor) override;
124 
125  protected:
126   bool precheck(THD *thd) override;
127 
128   bool prepare_inner(THD *thd) override;
129 
130   bool execute_inner(THD *thd) override;
131 
132  private:
133   bool delete_from_single_table(THD *thd);
134 
135   bool multitable;
136   /**
137     References to tables that are deleted from in a multitable delete statement.
138     Only used to track such tables from the parser. In preparation and
139     optimization, use the TABLE_LIST::updating property instead.
140   */
141   SQL_I_List<TABLE_LIST> *delete_tables;
142 };
143 
144 #endif /* SQL_DELETE_INCLUDED */
145