MySQL 8.0.29
Source Code Documentation
sql_update.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_UPDATE_INCLUDED
24#define SQL_UPDATE_INCLUDED
25
26#include <sys/types.h>
27
28#include <cstddef>
29
30#include "my_base.h"
31#include "my_sqlcommand.h"
32#include "sql/query_result.h" // Query_result_interceptor
33#include "sql/sql_cmd_dml.h" // Sql_cmd_dml
34#include "sql/sql_list.h"
35
36class COPY_INFO;
37class Copy_field;
38class Item;
39class Query_block;
42class THD;
44struct TABLE;
45struct TABLE_LIST;
46
48bool compare_records(const TABLE *table);
50 const Query_block *select,
51 const TABLE_LIST *table_list);
52
54 /// Number of tables being updated
56 /// Pointer to list of updated tables, linked via 'next_local'
58 /// Array of references to temporary tables used to store cached updates
59 TABLE **tmp_tables{nullptr};
60 /// Array of parameter structs for creation of temporary tables
62 /// The first table in the join operation
63 TABLE *main_table{nullptr};
64 /**
65 In a multi-table update, this is equal to the first table in the join
66 operation (#main_table) if that table can be updated on the fly while
67 scanning it. It is `nullptr` otherwise.
68
69 @see safe_update_on_fly
70 */
72 /// Number of rows found that matches join and WHERE conditions
74 /// Number of rows actually updated, in all affected tables
76 /// List of pointers to fields to update, in order from statement
78 /// List of pointers to values to update with, in order from statement
80 /// The fields list decomposed into separate lists per table
82 /// The values list decomposed into separate lists per table
84 /**
85 List of tables referenced in the CHECK OPTION condition of
86 the updated view excluding the updated table.
87 */
89 /// ???
91 /// Length of the copy_field array.
92 size_t max_fields{0};
93 /// True if the full update operation is complete
94 bool update_completed{false};
95 /// True if all tables to be updated are transactional.
96 bool trans_safe{true};
97 /// True if the update operation has made a change in a transactional table
99 /**
100 error handling (rollback and binlogging) can happen in send_eof()
101 so that afterward send_error() needs to find out that.
102 */
103 bool error_handled{false};
104
105 /**
106 Array of update operations, arranged per _updated_ table. For each
107 _updated_ table in the multiple table update statement, a COPY_INFO
108 pointer is present at the table's position in this array.
109
110 The array is allocated and populated during Query_result_update::prepare().
111 The position that each table is assigned is also given here and is stored
112 in the member TABLE::pos_in_table_list::shared. However, this is a publicly
113 available field, so nothing can be trusted about its integrity.
114
115 This member is NULL when the Query_result_update is created.
116
117 @see Query_result_update::prepare
118 */
120
121 public:
123 mem_root_deque<Item *> *value_list)
124 : Query_result_interceptor(), fields(field_list), values(value_list) {}
125 bool need_explain_interceptor() const override { return true; }
126 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
127 Query_expression *u) override;
128 bool optimize() override;
129 bool start_execution(THD *thd) override;
130 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
131 void send_error(THD *thd, uint errcode, const char *err) override;
132 bool do_updates(THD *thd);
133 bool send_eof(THD *thd) override;
134 void abort_result_set(THD *thd) override;
135 void cleanup(THD *thd) override;
136
137 bool immediate_update(TABLE_LIST *t) const override;
138};
139
140class Sql_cmd_update final : public Sql_cmd_dml {
141 public:
142 Sql_cmd_update(bool multitable_arg, mem_root_deque<Item *> *update_values)
143 : multitable(multitable_arg),
145 update_value_list(update_values) {}
146
149 }
150
151 bool is_single_table_plan() const override { return !multitable; }
152
153 protected:
154 bool precheck(THD *thd) override;
155 bool check_privileges(THD *thd) override;
156
157 bool prepare_inner(THD *thd) override;
158
159 bool execute_inner(THD *thd) override;
160
161 private:
162 bool update_single_table(THD *thd);
163
165
166 /// Bitmap of all tables which are to be updated
168
169 bool accept(THD *thd, Select_lex_visitor *visitor) override;
170
171 /// Convert list of fields to update to base table fields
173
174 public:
175 /// The original list of fields to update, used for privilege checking
177 /// The values used to update fields
179};
180
181#endif /* SQL_UPDATE_INCLUDED */
This class encapsulates a data change operation.
Definition: sql_data_change.h:73
Definition: field.h:4585
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:802
Definition: sql_list.h:433
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1124
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:629
Definition: query_result.h:200
Definition: sql_update.h:53
void abort_result_set(THD *thd) override
Definition: sql_update.cc:2493
Copy_field * copy_field
???
Definition: sql_update.h:90
bool do_updates(THD *thd)
Definition: sql_update.cc:2535
bool error_handled
error handling (rollback and binlogging) can happen in send_eof() so that afterward send_error() need...
Definition: sql_update.h:103
bool update_completed
True if the full update operation is complete.
Definition: sql_update.h:94
mem_root_deque< Item * > * fields
List of pointers to fields to update, in order from statement.
Definition: sql_update.h:77
bool trans_safe
True if all tables to be updated are transactional.
Definition: sql_update.h:96
ha_rows updated_rows
Number of rows actually updated, in all affected tables.
Definition: sql_update.h:75
mem_root_deque< Item * > * values
List of pointers to values to update with, in order from statement.
Definition: sql_update.h:79
bool transactional_tables
True if the update operation has made a change in a transactional table.
Definition: sql_update.h:98
ha_rows found_rows
Number of rows found that matches join and WHERE conditions.
Definition: sql_update.h:73
TABLE * table_to_update
In a multi-table update, this is equal to the first table in the join operation (main_table) if that ...
Definition: sql_update.h:71
bool need_explain_interceptor() const override
Definition: sql_update.h:125
mem_root_deque< Item * > ** fields_for_table
The fields list decomposed into separate lists per table.
Definition: sql_update.h:81
Temp_table_param * tmp_table_param
Array of parameter structs for creation of temporary tables.
Definition: sql_update.h:61
bool optimize() override
Set up data structures for multi-table UPDATE.
Definition: sql_update.cc:2004
void cleanup(THD *thd) override
Cleanup after this execution.
Definition: sql_update.cc:2279
size_t max_fields
Length of the copy_field array.
Definition: sql_update.h:92
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_update.cc:1779
bool immediate_update(TABLE_LIST *t) const override
If this Query_result performs modifications to tables: tells if it modifies the given table's row as ...
Definition: sql_update.cc:2882
TABLE ** tmp_tables
Array of references to temporary tables used to store cached updates.
Definition: sql_update.h:59
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: sql_update.cc:2272
List< TABLE > unupdated_check_opt_tables
List of tables referenced in the CHECK OPTION condition of the updated view excluding the updated tab...
Definition: sql_update.h:88
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: sql_update.cc:2320
COPY_INFO ** update_operations
Array of update operations, arranged per updated table.
Definition: sql_update.h:119
bool send_eof(THD *thd) override
Definition: sql_update.cc:2810
void send_error(THD *thd, uint errcode, const char *err) override
Definition: sql_update.cc:2488
TABLE_LIST * update_tables
Pointer to list of updated tables, linked via 'next_local'.
Definition: sql_update.h:57
TABLE * main_table
The first table in the join operation.
Definition: sql_update.h:63
uint update_table_count
Number of tables being updated.
Definition: sql_update.h:55
mem_root_deque< Item * > ** values_for_table
The values list decomposed into separate lists per table.
Definition: sql_update.h:83
Query_result_update(mem_root_deque< Item * > *field_list, mem_root_deque< Item * > *value_list)
Definition: sql_update.h:122
Abstract base class for traversing the Query_block tree.
Definition: select_lex_visitor.h:39
Definition: sql_cmd_dml.h:34
Definition: sql_update.h:140
bool precheck(THD *thd) override
Perform a precheck of table privileges for the specific operation.
Definition: sql_update.cc:123
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_update.h:147
bool prepare_inner(THD *thd) override
Perform the command-specific parts of DML command preparation, to be called from prepare()
Definition: sql_update.cc:1436
Sql_cmd_update(bool multitable_arg, mem_root_deque< Item * > *update_values)
Definition: sql_update.h:142
bool check_privileges(THD *thd) override
Check privileges on a prepared statement, called at start of execution of the statement.
Definition: sql_update.cc:189
mem_root_deque< Item * > * update_value_list
The values used to update fields.
Definition: sql_update.h:178
table_map tables_for_update
Bitmap of all tables which are to be updated.
Definition: sql_update.h:167
bool multitable
Definition: sql_update.h:164
bool execute_inner(THD *thd) override
The inner parts of query optimization and execution.
Definition: sql_update.cc:1756
mem_root_deque< Item * > original_fields
The original list of fields to update, used for privilege checking.
Definition: sql_update.h:176
bool update_single_table(THD *thd)
Perform an update to a set of rows in a single table.
Definition: sql_update.cc:361
bool is_single_table_plan() const override
Definition: sql_update.h:151
bool make_base_table_fields(THD *thd, mem_root_deque< Item * > *items)
Convert list of fields to update to base table fields.
Definition: sql_update.cc:305
bool accept(THD *thd, Select_lex_visitor *visitor) override
Definition: sql_update.cc:2886
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:945
Object containing parameters used when creating and using temporary tables.
Definition: temp_table_param.h:98
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:109
This file includes constants used by all storage engines.
my_off_t ha_rows
Definition: my_base.h:1139
enum_sql_command
Definition: my_sqlcommand.h:45
@ SQLCOM_UPDATE
Definition: my_sqlcommand.h:50
@ SQLCOM_UPDATE_MULTI
Definition: my_sqlcommand.h:121
uint64_t table_map
Definition: my_table_map.h:29
thread_local MEM_ROOT ** THR_MALLOC
Definition: mysqld.cc:1538
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:908
bool records_are_comparable(const TABLE *table)
True if the table's input and output record buffers are comparable using compare_records(TABLE*).
Definition: sql_update.cc:231
bool compare_records(const TABLE *table)
Compares the input and outbut record buffers of the table to see if a row has changed.
Definition: sql_update.cc:249
bool should_switch_to_multi_table_if_subqueries(const THD *thd, const Query_block *select, const TABLE_LIST *table_list)
Decides if a single-table UPDATE/DELETE statement should switch to the multi-table code path,...
Definition: sql_update.cc:1403
Definition: table.h:2684
TABLE * table
Definition: table.h:3458
Definition: table.h:1394
unsigned int uint
Definition: uca-dump.cc:29