MySQL  8.0.27
Source Code Documentation
sql_delete.h
Go to the documentation of this file.
1 /* Copyright (c) 2006, 2021, 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_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 Query_expression;
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 
48  /// Pointers to temporary files used for delayed deletion of rows
49  Unique **tempfiles{nullptr};
50  /// Pointers to table objects matching tempfiles
51  TABLE **tables{nullptr};
52  /// Number of tables being deleted from
54  /// Number of rows produced by the join
56  /// Number of rows deleted
58  /// Handler error status for the operation.
59  int delete_error{0};
60  /// Map of all tables to delete rows from
62  /// Map of tables to delete from immediately
64  // Map of transactional tables to be deleted from
66  /// Map of non-transactional tables to be deleted from
68  /// True if the full delete operation is complete
69  bool delete_completed{false};
70  /// True if some actual delete operation against non-transactional table done
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{false};
77 
78  public:
80  bool need_explain_interceptor() const override { return true; }
81  bool prepare(THD *thd, const mem_root_deque<Item *> &list,
82  Query_expression *u) override;
83  bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
84  void send_error(THD *thd, uint errcode, const char *err) override;
85  bool optimize() override;
86  bool start_execution(THD *) override {
87  delete_completed = false;
88  return false;
89  }
90  int do_deletes(THD *thd);
91  int do_table_deletes(THD *thd, TABLE *table);
92  bool send_eof(THD *thd) override;
93  inline ha_rows num_deleted() { return deleted_rows; }
94  void abort_result_set(THD *thd) override;
95  void cleanup(THD *thd) override;
96  bool immediate_update(TABLE_LIST *t) const override;
97 };
98 
99 class Sql_cmd_delete final : public Sql_cmd_dml {
100  public:
101  Sql_cmd_delete(bool multitable_arg, SQL_I_List<TABLE_LIST> *delete_tables_arg)
102  : multitable(multitable_arg), delete_tables(delete_tables_arg) {}
103 
106  }
107 
108  bool is_single_table_plan() const override { return !multitable; }
109 
110  bool accept(THD *thd, Select_lex_visitor *visitor) override;
111 
112  protected:
113  bool precheck(THD *thd) override;
114  bool check_privileges(THD *thd) override;
115 
116  bool prepare_inner(THD *thd) override;
117 
118  bool execute_inner(THD *thd) override;
119 
120  private:
121  bool delete_from_single_table(THD *thd);
122 
124  /**
125  References to tables that are deleted from in a multitable delete statement.
126  Only used to track such tables from the parser. In preparation and
127  optimization, use the TABLE_LIST::updating property instead.
128  */
130 };
131 
132 #endif /* SQL_DELETE_INCLUDED */
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:802
Definition: sql_list.h:431
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:629
Definition: sql_delete.h:47
int do_table_deletes(THD *thd, TABLE *table)
Implements the inner loop of nested-loops join within multi-DELETE execution.
Definition: sql_delete.cc:1186
void cleanup(THD *thd) override
Cleanup after this execution.
Definition: sql_delete.cc:997
uint delete_table_count
Number of tables being deleted from.
Definition: sql_delete.h:53
table_map delete_table_map
Map of all tables to delete rows from.
Definition: sql_delete.h:61
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: sql_delete.cc:1015
bool prepare(THD *thd, const mem_root_deque< Item * > &list, Query_expression *u) override
Perform preparation specific to the query expression or DML statement.
Definition: sql_delete.cc:871
bool send_eof(THD *thd) override
Send ok to the client.
Definition: sql_delete.cc:1264
bool error_handled
Definition: sql_delete.h:76
table_map delete_immediate
Map of tables to delete from immediately.
Definition: sql_delete.h:63
ha_rows deleted_rows
Number of rows deleted.
Definition: sql_delete.h:57
bool immediate_update(TABLE_LIST *t) const override
Definition: sql_delete.cc:1302
void send_error(THD *thd, uint errcode, const char *err) override
Definition: sql_delete.cc:1097
bool need_explain_interceptor() const override
Definition: sql_delete.h:80
bool delete_completed
True if the full delete operation is complete.
Definition: sql_delete.h:69
void abort_result_set(THD *thd) override
Definition: sql_delete.cc:1104
bool non_transactional_deleted
True if some actual delete operation against non-transactional table done.
Definition: sql_delete.h:71
Unique ** tempfiles
Pointers to temporary files used for delayed deletion of rows.
Definition: sql_delete.h:49
table_map non_transactional_table_map
Map of non-transactional tables to be deleted from.
Definition: sql_delete.h:67
ha_rows num_deleted()
Definition: sql_delete.h:93
bool start_execution(THD *) override
Prepare for execution of the query expression or DML statement.
Definition: sql_delete.h:86
int do_deletes(THD *thd)
Do delete from other tables.
Definition: sql_delete.cc:1148
TABLE ** tables
Pointers to table objects matching tempfiles.
Definition: sql_delete.h:51
table_map transactional_table_map
Definition: sql_delete.h:65
ha_rows found_rows
Number of rows produced by the join.
Definition: sql_delete.h:55
Query_result_delete()
Definition: sql_delete.h:79
int delete_error
Handler error status for the operation.
Definition: sql_delete.h:59
bool optimize() override
Optimize for deletion from one or more tables in a multi-table DELETE.
Definition: sql_delete.cc:903
Definition: query_result.h:200
Simple intrusive linked list.
Definition: sql_list.h:45
Abstract base class for traversing the Query_block tree.
Definition: select_lex_visitor.h:39
Definition: sql_delete.h:99
SQL_I_List< TABLE_LIST > * delete_tables
References to tables that are deleted from in a multitable delete statement.
Definition: sql_delete.h:129
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_delete.h:104
bool execute_inner(THD *thd) override
Execute a DELETE statement.
Definition: sql_delete.cc:845
bool prepare_inner(THD *thd) override
Prepare a DELETE statement.
Definition: sql_delete.cc:651
bool delete_from_single_table(THD *thd)
Delete a set of rows from a single table.
Definition: sql_delete.cc:142
Sql_cmd_delete(bool multitable_arg, SQL_I_List< TABLE_LIST > *delete_tables_arg)
Definition: sql_delete.h:101
bool precheck(THD *thd) override
Perform a precheck of table privileges for the specific operation.
Definition: sql_delete.cc:90
bool multitable
Definition: sql_delete.h:123
bool check_privileges(THD *thd) override
Check privileges on a prepared statement, called at start of execution of the statement.
Definition: sql_delete.cc:120
bool accept(THD *thd, Select_lex_visitor *visitor) override
Definition: sql_delete.cc:1306
bool is_single_table_plan() const override
Definition: sql_delete.h:108
Definition: sql_cmd_dml.h:34
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
Unique – class for unique (removing of duplicates).
Definition: uniques.h:52
This file includes constants used by all storage engines.
my_off_t ha_rows
Definition: my_base.h:1138
enum_sql_command
Definition: my_sqlcommand.h:45
@ SQLCOM_DELETE
Definition: my_sqlcommand.h:53
@ SQLCOM_DELETE_MULTI
Definition: my_sqlcommand.h:120
uint64_t table_map
Definition: my_table_map.h:29
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:908
Definition: table.h:2694
Definition: table.h:1394
unsigned int uint
Definition: uca-dump.cc:29