MySQL 8.3.0
Source Code Documentation
sql_insert.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 2023, 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_INSERT_INCLUDED
24#define SQL_INSERT_INCLUDED
25
26#include <assert.h>
27#include <sys/types.h>
28
29#include "mem_root_deque.h"
30#include "my_inttypes.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_data_change.h" // enum_duplicates
35#include "sql/table.h"
36#include "sql/thr_malloc.h"
37
38class Alter_info;
39class Field;
40class Item;
43class THD;
44struct HA_CREATE_INFO;
45struct handlerton;
47struct MY_BITMAP;
48
50struct MYSQL_LOCK;
51
53 Table_ref *table_list);
57
59 public:
60 /// The table used for insertion of rows
62 TABLE *table{nullptr};
63
64 private:
65 /**
66 The columns of the table to be inserted into, *or* the columns of the
67 table from which values are selected. For legacy reasons both are
68 allowed.
69 */
71
72 protected:
73 /// ha_start_bulk_insert has been called. Never cleared.
75
76 public:
79 COPY_INFO update; ///< the UPDATE part of "info"
81
82 /**
83 Creates a Query_result_insert for routing a result set to an existing
84 table.
85
86 @param table_list_par The table reference for the destination table.
87 @param target_columns See details.
88 @param target_or_source_columns See details.
89 @param update_fields The columns to be updated in case of duplicate
90 keys. May be NULL.
91 @param update_values The values to be assigned in case of duplicate
92 keys. May be NULL.
93 @param duplic The policy for handling duplicates.
94
95 @todo This constructor takes 8 arguments, 6 of which are used to
96 immediately construct a COPY_INFO object. Obviously the constructor
97 should take the COPY_INFO object as argument instead. Also, some
98 Query_result_insert members initialized here are totally redundant, as they
99are found inside the COPY_INFO.
100
101 The target_columns and target_or_source_columns arguments are set by
102 callers as follows:
103 @li if CREATE SELECT:
104 - target_columns == NULL,
105 - target_or_source_columns == expressions listed after SELECT, as in
106 CREATE ... SELECT expressions
107 @li if INSERT SELECT:
108 target_columns
109 == target_or_source_columns
110 == columns listed between INSERT and SELECT, as in
111 INSERT INTO t (columns) SELECT ...
112
113 We set the manage_defaults argument of info's constructor as follows
114 ([...] denotes something optional):
115 @li If target_columns==NULL, the statement is
116@verbatim
117 CREATE TABLE a_table [(columns1)] SELECT expressions2
118@endverbatim
119 so 'info' must manage defaults of columns1.
120 @li Otherwise it is:
121@verbatim
122 INSERT INTO a_table [(columns1)] SELECT ...
123@endverbatim
124 target_columns is columns1, if not empty then 'info' must manage defaults
125 of other columns than columns1.
126 */
128 mem_root_deque<Item *> *target_columns,
129 mem_root_deque<Item *> *target_or_source_columns,
130 mem_root_deque<Item *> *update_fields,
131 mem_root_deque<Item *> *update_values,
132 enum_duplicates duplic)
134 table_list(table_list_par),
135 fields(target_or_source_columns),
136 info(COPY_INFO::INSERT_OPERATION, target_columns,
137 // manage_defaults
138 (target_columns == nullptr || !target_columns->empty()), duplic),
139 update(COPY_INFO::UPDATE_OPERATION, update_fields, update_values),
140 insert_into_view(table_list_par && table_list_par->is_view()) {
141 assert(target_or_source_columns != nullptr);
142 assert(target_columns == target_or_source_columns ||
143 target_columns == nullptr);
144 }
145
146 public:
147 bool need_explain_interceptor() const override { return true; }
148 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
149 Query_expression *u) override;
150 bool start_execution(THD *thd) override;
151 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
152 virtual void store_values(THD *thd, const mem_root_deque<Item *> &values);
153 bool send_eof(THD *thd) override;
154 void abort_result_set(THD *thd) override;
155 void cleanup() override;
156
157 private:
158 /**
159 Indicates whether this statement should be written to binary log's
160 transactional cache in statement mode.
161 */
162 virtual bool stmt_binlog_is_trans() const;
163};
164
165/**
166 @todo This class inherits a class which is non-abstract. This is not in
167 line with good programming practices and the inheritance should be broken
168 up.
169*/
171 /// Handle for table to be created
173 /// Contains further information for table creation
175 /// Contains further information for table creation
178 /// List of tables that are select from
180 /// Pointer to first field in table generated from query expression
181 Field **table_fields{nullptr};
182 /// lock data for tmp table
184 /// m_lock or thd->extra_lock
185 MYSQL_LOCK **m_plock{nullptr};
186 /**
187 If table being created has SE supporting atomic DDL, pointer to SE's
188 handlerton object to be used for calling SE post-DDL hook, nullptr -
189 otherwise.
190 */
192
193 public:
194 Query_result_create(Table_ref *create_table_arg,
196 Table_ref *select_tables_arg);
197
198 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
199 Query_expression *u) override;
200 void store_values(THD *thd, const mem_root_deque<Item *> &values) override;
201 bool send_eof(THD *thd) override;
202 void abort_result_set(THD *thd) override;
203 bool create_table_for_query_block(THD *thd) override;
204 bool start_execution(THD *thd) override;
205 void set_two_fields(HA_CREATE_INFO *create_info_arg,
206 Alter_info *alter_info_arg) {
207 create_info = create_info_arg;
208 alter_info = alter_info_arg;
209 }
210
211 private:
212 bool stmt_binlog_is_trans() const override;
214 void drop_open_table(THD *thd);
215};
216
217/**
218 Base class for all INSERT and REPLACE statements. Abstract class that
219 is inherited by Sql_cmd_insert_values and Sql_cmd_insert_select.
220*/
221
223 protected:
224 bool precheck(THD *thd) override;
225 bool check_privileges(THD *thd) override;
226 bool prepare_inner(THD *thd) override;
227 bool restore_cmd_properties(THD *thd) override;
228
229 private:
231 bool prepare_values_table(THD *thd);
234 MY_BITMAP **m_function_default_columns);
235
236 protected:
237 /// true when REPLACE statement, false when INSERT statement
238 const bool is_replace;
239
240 public:
241 /**
242 Field list to insert/replace
243
244 One of two things:
245 1. For the INSERT/REPLACE ... (col1, ... colN) VALUES ... syntax
246 this is a list of col1, ..., colN fields.
247 2. For the INSERT/REPLACE ... SET col1=x1, ... colM=xM syntax extension
248 this is a list of col1, ... colM fields as well.
249 */
251 /**
252 Row data to insert/replace
253
254 One of two things:
255 1. For the INSERT/REPLACE ... VALUES (row1), (row2), ... (rowN) syntax
256 the list contains N List_item lists: one List_item per row.
257 2. For the INSERT/REPLACE ... SET col1=x1, ... colM=xM syntax extension
258 this list contains only 1 List_item of M data values: this way we
259 emulate this syntax:
260 INSERT/REPLACE ... (col1, ... colM) VALUE (x1, ..., xM);
261 */
263
264 /// True if VALUES clause contain column references that need privilege check
266
267 /// Number of columns in original insert column list
269
270 /// Number of values per row in insert_many_values, available after resolving
272
273 /// ON DUPLICATE KEY UPDATE field list
275
276 /// ON DUPLICATE KEY UPDATE data value list
278
279 /**
280 ON DUPLICATE KEY UPDATE reference to VALUES.. as a derived table.
281 */
284
285 /**
286 Field list for VALUES derived table. If no insert_field exists (e.g. INSERT
287 INTO t0 ..), we have to create one to create Item_insert_values for ODKU
288 statements.
289 */
291
293
294 explicit Sql_cmd_insert_base(bool is_replace_arg,
295 enum_duplicates duplicates_arg)
296 : is_replace(is_replace_arg),
299 column_count(0),
300 value_count(0),
304 duplicates(duplicates_arg) {}
305
306 bool accept(THD *thd, Select_lex_visitor *visitor) override;
307};
308
309/**
310 Class that implements INSERT ... VALUES and REPLACE ... VALUES statements.
311*/
312
314 public:
315 explicit Sql_cmd_insert_values(bool is_replace_arg,
316 enum_duplicates duplicates_arg)
317 : Sql_cmd_insert_base(is_replace_arg, duplicates_arg) {}
318
321 }
322
323 bool is_single_table_plan() const override { return true; }
324
325 protected:
326 bool execute_inner(THD *thd) override;
327};
328
329/**
330 Class that implements INSERT ... SELECT and REPLACE ... SELECT statements.
331*/
332
334 public:
335 explicit Sql_cmd_insert_select(bool is_replace_arg,
336 enum_duplicates duplicates_arg)
337 : Sql_cmd_insert_base(is_replace_arg, duplicates_arg) {}
338
341 }
343 THD *thd) const override;
344};
345
346#endif /* SQL_INSERT_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:250
Data describing the table being created by CREATE TABLE or altered by ALTER TABLE.
Definition: sql_alter.h:204
This class encapsulates a data change operation.
Definition: sql_data_change.h:73
Definition: field.h:574
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:933
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:624
Definition: sql_insert.h:170
Field ** field
Definition: sql_insert.h:177
HA_CREATE_INFO * create_info
Contains further information for table creation.
Definition: sql_insert.h:174
Table_ref * select_tables
List of tables that are select from.
Definition: sql_insert.h:179
Table_ref * create_table
Handle for table to be created.
Definition: sql_insert.h:172
Field ** table_fields
Pointer to first field in table generated from query expression.
Definition: sql_insert.h:181
handlerton * m_post_ddl_ht
If table being created has SE supporting atomic DDL, pointer to SE's handlerton object to be used for...
Definition: sql_insert.h:191
void abort_result_set(THD *thd) override
Definition: sql_insert.cc:3262
Alter_info * alter_info
Contains further information for table creation.
Definition: sql_insert.h:176
int binlog_show_create_table(THD *thd)
Definition: sql_insert.cc:2971
bool start_execution(THD *thd) override
Lock the newly created table and prepare it for insertion.
Definition: sql_insert.cc:2878
bool stmt_binlog_is_trans() const override
Indicates whether this statement should be written to binary log's transactional cache in statement m...
Definition: sql_insert.cc:3067
bool send_eof(THD *thd) override
Definition: sql_insert.cc:3075
void store_values(THD *thd, const mem_root_deque< Item * > &values) override
Definition: sql_insert.cc:3048
Query_result_create(Table_ref *create_table_arg, mem_root_deque< Item * > *fields, enum_duplicates duplic, Table_ref *select_tables_arg)
Definition: sql_insert.cc:2805
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_insert.cc:2818
MYSQL_LOCK * m_lock
lock data for tmp table
Definition: sql_insert.h:183
void set_two_fields(HA_CREATE_INFO *create_info_arg, Alter_info *alter_info_arg)
Definition: sql_insert.h:205
MYSQL_LOCK ** m_plock
m_lock or thd->extra_lock
Definition: sql_insert.h:185
void drop_open_table(THD *thd)
Close and drop just created table in CREATE TABLE ... SELECT in case of error.
Definition: sql_insert.cc:3202
bool create_table_for_query_block(THD *thd) override
Create new table.
Definition: sql_insert.cc:2834
Definition: sql_insert.h:58
virtual void store_values(THD *thd, const mem_root_deque< Item * > &values)
Definition: sql_insert.cc:2417
COPY_INFO update
the UPDATE part of "info"
Definition: sql_insert.h:79
mem_root_deque< Item * > * fields
The columns of the table to be inserted into, or the columns of the table from which values are selec...
Definition: sql_insert.h:70
bool start_execution(THD *thd) override
Set up the target table for execution.
Definition: sql_insert.cc:2283
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_insert.cc:2254
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: sql_insert.cc:2342
virtual bool stmt_binlog_is_trans() const
Indicates whether this statement should be written to binary log's transactional cache in statement m...
Definition: sql_insert.cc:2432
Query_result_insert(Table_ref *table_list_par, mem_root_deque< Item * > *target_columns, mem_root_deque< Item * > *target_or_source_columns, mem_root_deque< Item * > *update_fields, mem_root_deque< Item * > *update_values, enum_duplicates duplic)
Definition: sql_insert.h:127
bool send_eof(THD *thd) override
Definition: sql_insert.cc:2436
void abort_result_set(THD *thd) override
Definition: sql_insert.cc:2538
ulonglong autoinc_value_of_last_inserted_row
Definition: sql_insert.h:77
TABLE * table
Definition: sql_insert.h:62
void cleanup() override
Cleanup after this execution.
Definition: sql_insert.cc:2326
bool need_explain_interceptor() const override
Definition: sql_insert.h:147
bool insert_into_view
Definition: sql_insert.h:80
COPY_INFO info
Definition: sql_insert.h:78
bool bulk_insert_started
ha_start_bulk_insert has been called. Never cleared.
Definition: sql_insert.h:74
Table_ref * table_list
The table used for insertion of rows.
Definition: sql_insert.h:61
Definition: query_result.h:180
Abstract base class for traversing the Query_block tree.
Definition: select_lex_visitor.h:39
Definition: sql_cmd_dml.h:34
Base class for all INSERT and REPLACE statements.
Definition: sql_insert.h:222
mem_root_deque< Item * > values_field_list
Field list for VALUES derived table.
Definition: sql_insert.h:290
bool resolve_values_table_columns(THD *thd)
Resolve the columns of the optional VALUES table to the insert_values of the table inserted into.
Definition: sql_insert.cc:1631
bool prepare_values_table(THD *thd)
Prepare the derived table created as a VALUES alias.
Definition: sql_insert.cc:1559
Sql_cmd_insert_base(bool is_replace_arg, enum_duplicates duplicates_arg)
Definition: sql_insert.h:294
mem_root_deque< Item * > update_field_list
ON DUPLICATE KEY UPDATE field list.
Definition: sql_insert.h:274
Create_col_name_list * values_column_list
Definition: sql_insert.h:283
bool restore_cmd_properties(THD *thd) override
Restore command properties before execution.
Definition: sql_insert.cc:1722
mem_root_deque< List_item * > insert_many_values
Row data to insert/replace.
Definition: sql_insert.h:262
bool resolve_update_expressions(THD *thd)
Resolve ON DUPLICATE KEY UPDATE expressions.
Definition: sql_insert.cc:1670
bool precheck(THD *thd) override
Perform a precheck of table privileges for the specific operation.
Definition: sql_insert.cc:428
bool prepare_inner(THD *thd) override
Prepare items in INSERT statement.
Definition: sql_insert.cc:994
Table_ref * values_table
ON DUPLICATE KEY UPDATE reference to VALUES.
Definition: sql_insert.h:282
uint column_count
Number of columns in original insert column list.
Definition: sql_insert.h:268
bool get_default_columns(THD *thd, TABLE *table, MY_BITMAP **m_function_default_columns)
Definition: sql_insert.cc:939
const bool is_replace
true when REPLACE statement, false when INSERT statement
Definition: sql_insert.h:238
uint value_count
Number of values per row in insert_many_values, available after resolving.
Definition: sql_insert.h:271
const enum_duplicates duplicates
Definition: sql_insert.h:292
mem_root_deque< Item * > insert_field_list
Field list to insert/replace.
Definition: sql_insert.h:250
mem_root_deque< Item * > update_value_list
ON DUPLICATE KEY UPDATE data value list.
Definition: sql_insert.h:277
bool check_privileges(THD *thd) override
Check privileges on a prepared statement, called at start of execution of the statement.
Definition: sql_insert.cc:442
bool accept(THD *thd, Select_lex_visitor *visitor) override
Definition: sql_insert.cc:3312
bool values_need_privilege_check
True if VALUES clause contain column references that need privilege check.
Definition: sql_insert.h:265
Class that implements INSERT ... SELECT and REPLACE ... SELECT statements.
Definition: sql_insert.h:333
Sql_cmd_insert_select(bool is_replace_arg, enum_duplicates duplicates_arg)
Definition: sql_insert.h:335
const MYSQL_LEX_CSTRING * eligible_secondary_storage_engine(THD *thd) const override
Is this statement of a type and on a form that makes it eligible for execution in a secondary storage...
Definition: sql_insert.cc:3344
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_insert.h:339
Class that implements INSERT ... VALUES and REPLACE ... VALUES statements.
Definition: sql_insert.h:313
bool execute_inner(THD *thd) override
Insert one or more rows from a VALUES list into a table.
Definition: sql_insert.cc:477
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_insert.h:319
Sql_cmd_insert_values(bool is_replace_arg, enum_duplicates duplicates_arg)
Definition: sql_insert.h:315
bool is_single_table_plan() const override
Definition: sql_insert.h:323
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:35
Definition: table.h:2853
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:110
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
enum_sql_command
Definition: my_sqlcommand.h:45
@ SQLCOM_INSERT
Definition: my_sqlcommand.h:51
@ SQLCOM_INSERT_SELECT
Definition: my_sqlcommand.h:52
@ SQLCOM_REPLACE
Definition: my_sqlcommand.h:86
@ SQLCOM_REPLACE_SELECT
Definition: my_sqlcommand.h:87
static uint update
Definition: myisamlog.cc:93
thread_local MEM_ROOT ** THR_MALLOC
Definition: mysqld.cc:1571
static PFS_engine_table_share_proxy table
Definition: pfs.cc:60
bool empty(const Histogram &histogram)
Return true if 'histogram' was built on an empty table.
Definition: histogram.h:672
std::list< T, ut::allocator< T > > list
Specialization of list which uses ut_allocator.
Definition: ut0new.h:2877
Contains classes representing SQL-data change statements.
enum_duplicates
Definition: sql_data_change.h:47
bool validate_default_values_of_unset_fields(THD *thd, TABLE *table)
Validates default value of fields which are not specified in the column list of INSERT statement.
Definition: sql_insert.cc:308
bool check_that_all_fields_are_given_values(THD *thd, TABLE *entry, Table_ref *table_list)
Check that all fields with aren't null_fields are used.
Definition: sql_insert.cc:2217
void prepare_triggers_for_insert_stmt(THD *thd, TABLE *table)
Prepare triggers for INSERT-like statement.
Definition: sql_insert.cc:334
bool write_record(THD *thd, TABLE *table, COPY_INFO *info, COPY_INFO *update)
Write a record to table with optional deletion of conflicting records, invoke proper triggers if need...
Definition: sql_insert.cc:1791
Struct to hold information about the table that should be created.
Definition: handler.h:3177
Definition: mysql_lex_string.h:39
Definition: lock.h:38
Definition: my_bitmap.h:42
Definition: table.h:1403
Definition: completion_hash.h:34
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2718
static int is_view(cargo_type x)
Definition: xcom_base.cc:2159