MySQL 8.0.39
Source Code Documentation
sql_insert.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 2024, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SQL_INSERT_INCLUDED
25#define SQL_INSERT_INCLUDED
26
27#include <assert.h>
28#include <sys/types.h>
29
30#include "mem_root_deque.h"
31#include "my_inttypes.h"
32#include "my_sqlcommand.h"
33#include "sql/query_result.h" // Query_result_interceptor
34#include "sql/sql_cmd_dml.h" // Sql_cmd_dml
35#include "sql/sql_data_change.h" // enum_duplicates
36#include "sql/table.h"
37#include "sql/thr_malloc.h"
38
39class Alter_info;
40class Field;
41class Item;
44class THD;
45struct HA_CREATE_INFO;
46struct handlerton;
48struct MY_BITMAP;
49
51struct MYSQL_LOCK;
52
54 Table_ref *table_list);
58
60 public:
61 /// The table used for insertion of rows
63 TABLE *table{nullptr};
64
65 private:
66 /**
67 The columns of the table to be inserted into, *or* the columns of the
68 table from which values are selected. For legacy reasons both are
69 allowed.
70 */
72
73 protected:
74 /// ha_start_bulk_insert has been called. Never cleared.
76
77 public:
80 COPY_INFO update; ///< the UPDATE part of "info"
82
83 /**
84 Creates a Query_result_insert for routing a result set to an existing
85 table.
86
87 @param table_list_par The table reference for the destination table.
88 @param target_columns See details.
89 @param target_or_source_columns See details.
90 @param update_fields The columns to be updated in case of duplicate
91 keys. May be NULL.
92 @param update_values The values to be assigned in case of duplicate
93 keys. May be NULL.
94 @param duplic The policy for handling duplicates.
95
96 @todo This constructor takes 8 arguments, 6 of which are used to
97 immediately construct a COPY_INFO object. Obviously the constructor
98 should take the COPY_INFO object as argument instead. Also, some
99 Query_result_insert members initialized here are totally redundant, as they
100are found inside the COPY_INFO.
101
102 The target_columns and target_or_source_columns arguments are set by
103 callers as follows:
104 @li if CREATE SELECT:
105 - target_columns == NULL,
106 - target_or_source_columns == expressions listed after SELECT, as in
107 CREATE ... SELECT expressions
108 @li if INSERT SELECT:
109 target_columns
110 == target_or_source_columns
111 == columns listed between INSERT and SELECT, as in
112 INSERT INTO t (columns) SELECT ...
113
114 We set the manage_defaults argument of info's constructor as follows
115 ([...] denotes something optional):
116 @li If target_columns==NULL, the statement is
117@verbatim
118 CREATE TABLE a_table [(columns1)] SELECT expressions2
119@endverbatim
120 so 'info' must manage defaults of columns1.
121 @li Otherwise it is:
122@verbatim
123 INSERT INTO a_table [(columns1)] SELECT ...
124@endverbatim
125 target_columns is columns1, if not empty then 'info' must manage defaults
126 of other columns than columns1.
127 */
129 mem_root_deque<Item *> *target_columns,
130 mem_root_deque<Item *> *target_or_source_columns,
131 mem_root_deque<Item *> *update_fields,
132 mem_root_deque<Item *> *update_values,
133 enum_duplicates duplic)
135 table_list(table_list_par),
136 fields(target_or_source_columns),
137 info(COPY_INFO::INSERT_OPERATION, target_columns,
138 // manage_defaults
139 (target_columns == nullptr || !target_columns->empty()), duplic),
140 update(COPY_INFO::UPDATE_OPERATION, update_fields, update_values),
141 insert_into_view(table_list_par && table_list_par->is_view()) {
142 assert(target_or_source_columns != nullptr);
143 assert(target_columns == target_or_source_columns ||
144 target_columns == nullptr);
145 }
146
147 public:
148 bool need_explain_interceptor() const override { return true; }
149 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
150 Query_expression *u) override;
151 bool start_execution(THD *thd) override;
152 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
153 virtual void store_values(THD *thd, const mem_root_deque<Item *> &values);
154 bool send_eof(THD *thd) override;
155 void abort_result_set(THD *thd) override;
156 void cleanup() override;
157
158 private:
159 /**
160 Indicates whether this statement should be written to binary log's
161 transactional cache in statement mode.
162 */
163 virtual bool stmt_binlog_is_trans() const;
164};
165
166/**
167 @todo This class inherits a class which is non-abstract. This is not in
168 line with good programming practices and the inheritance should be broken
169 up.
170*/
172 /// Handle for table to be created
174 /// Contains further information for table creation
176 /// Contains further information for table creation
179 /// List of tables that are select from
181 /// Pointer to first field in table generated from query expression
182 Field **table_fields{nullptr};
183 /// lock data for tmp table
185 /// m_lock or thd->extra_lock
186 MYSQL_LOCK **m_plock{nullptr};
187 /**
188 If table being created has SE supporting atomic DDL, pointer to SE's
189 handlerton object to be used for calling SE post-DDL hook, nullptr -
190 otherwise.
191 */
193
194 public:
195 Query_result_create(Table_ref *create_table_arg,
197 Table_ref *select_tables_arg);
198
199 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
200 Query_expression *u) override;
201 void store_values(THD *thd, const mem_root_deque<Item *> &values) override;
202 bool send_eof(THD *thd) override;
203 void abort_result_set(THD *thd) override;
204 bool create_table_for_query_block(THD *thd) override;
205 bool start_execution(THD *thd) override;
206 void set_two_fields(HA_CREATE_INFO *create_info_arg,
207 Alter_info *alter_info_arg) {
208 create_info = create_info_arg;
209 alter_info = alter_info_arg;
210 }
211
212 private:
213 bool stmt_binlog_is_trans() const override;
215 void drop_open_table(THD *thd);
216};
217
218/**
219 Base class for all INSERT and REPLACE statements. Abstract class that
220 is inherited by Sql_cmd_insert_values and Sql_cmd_insert_select.
221*/
222
224 protected:
225 bool precheck(THD *thd) override;
226 bool check_privileges(THD *thd) override;
227 bool prepare_inner(THD *thd) override;
228 bool restore_cmd_properties(THD *thd) override;
229
230 private:
232 bool prepare_values_table(THD *thd);
234 bool get_default_columns(THD *thd, TABLE *table,
235 MY_BITMAP **m_function_default_columns);
236
237 protected:
238 /// true when REPLACE statement, false when INSERT statement
239 const bool is_replace;
240
241 public:
242 /**
243 Field list to insert/replace
244
245 One of two things:
246 1. For the INSERT/REPLACE ... (col1, ... colN) VALUES ... syntax
247 this is a list of col1, ..., colN fields.
248 2. For the INSERT/REPLACE ... SET col1=x1, ... colM=xM syntax extension
249 this is a list of col1, ... colM fields as well.
250 */
252 /**
253 Row data to insert/replace
254
255 One of two things:
256 1. For the INSERT/REPLACE ... VALUES (row1), (row2), ... (rowN) syntax
257 the list contains N List_item lists: one List_item per row.
258 2. For the INSERT/REPLACE ... SET col1=x1, ... colM=xM syntax extension
259 this list contains only 1 List_item of M data values: this way we
260 emulate this syntax:
261 INSERT/REPLACE ... (col1, ... colM) VALUE (x1, ..., xM);
262 */
264
265 /// True if VALUES clause contain column references that need privilege check
267
268 /// Number of columns in original insert column list
270
271 /// Number of values per row in insert_many_values, available after resolving
273
274 /// ON DUPLICATE KEY UPDATE field list
276
277 /// ON DUPLICATE KEY UPDATE data value list
279
280 /**
281 ON DUPLICATE KEY UPDATE reference to VALUES.. as a derived table.
282 */
285
286 /**
287 Field list for VALUES derived table. If no insert_field exists (e.g. INSERT
288 INTO t0 ..), we have to create one to create Item_insert_values for ODKU
289 statements.
290 */
292
294
295 explicit Sql_cmd_insert_base(bool is_replace_arg,
296 enum_duplicates duplicates_arg)
297 : is_replace(is_replace_arg),
300 column_count(0),
301 value_count(0),
305 duplicates(duplicates_arg) {}
306
307 bool accept(THD *thd, Select_lex_visitor *visitor) override;
308};
309
310/**
311 Class that implements INSERT ... VALUES and REPLACE ... VALUES statements.
312*/
313
315 public:
316 explicit Sql_cmd_insert_values(bool is_replace_arg,
317 enum_duplicates duplicates_arg)
318 : Sql_cmd_insert_base(is_replace_arg, duplicates_arg) {}
319
322 }
323
324 bool is_single_table_plan() const override { return true; }
325
326 protected:
327 bool execute_inner(THD *thd) override;
328};
329
330/**
331 Class that implements INSERT ... SELECT and REPLACE ... SELECT statements.
332*/
333
335 public:
336 explicit Sql_cmd_insert_select(bool is_replace_arg,
337 enum_duplicates duplicates_arg)
338 : Sql_cmd_insert_base(is_replace_arg, duplicates_arg) {}
339
342 }
344};
345
346#endif /* SQL_INSERT_INCLUDED */
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:74
Definition: field.h:575
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:853
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:623
Definition: sql_insert.h:171
Field ** field
Definition: sql_insert.h:178
HA_CREATE_INFO * create_info
Contains further information for table creation.
Definition: sql_insert.h:175
Table_ref * select_tables
List of tables that are select from.
Definition: sql_insert.h:180
Table_ref * create_table
Handle for table to be created.
Definition: sql_insert.h:173
Field ** table_fields
Pointer to first field in table generated from query expression.
Definition: sql_insert.h:182
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:192
void abort_result_set(THD *thd) override
Definition: sql_insert.cc:3260
Alter_info * alter_info
Contains further information for table creation.
Definition: sql_insert.h:177
int binlog_show_create_table(THD *thd)
Definition: sql_insert.cc:2970
bool start_execution(THD *thd) override
Lock the newly created table and prepare it for insertion.
Definition: sql_insert.cc:2877
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:3066
bool send_eof(THD *thd) override
Definition: sql_insert.cc:3074
void store_values(THD *thd, const mem_root_deque< Item * > &values) override
Definition: sql_insert.cc:3047
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:2804
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:2817
MYSQL_LOCK * m_lock
lock data for tmp table
Definition: sql_insert.h:184
void set_two_fields(HA_CREATE_INFO *create_info_arg, Alter_info *alter_info_arg)
Definition: sql_insert.h:206
MYSQL_LOCK ** m_plock
m_lock or thd->extra_lock
Definition: sql_insert.h:186
void drop_open_table(THD *thd)
Close and drop just created table in CREATE TABLE ... SELECT in case of error.
Definition: sql_insert.cc:3200
bool create_table_for_query_block(THD *thd) override
Create new table.
Definition: sql_insert.cc:2833
Definition: sql_insert.h:59
virtual void store_values(THD *thd, const mem_root_deque< Item * > &values)
Definition: sql_insert.cc:2416
COPY_INFO update
the UPDATE part of "info"
Definition: sql_insert.h:80
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:71
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:2431
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:128
bool send_eof(THD *thd) override
Definition: sql_insert.cc:2435
void abort_result_set(THD *thd) override
Definition: sql_insert.cc:2537
ulonglong autoinc_value_of_last_inserted_row
Definition: sql_insert.h:78
TABLE * table
Definition: sql_insert.h:63
void cleanup() override
Cleanup after this execution.
Definition: sql_insert.cc:2326
bool need_explain_interceptor() const override
Definition: sql_insert.h:148
bool insert_into_view
Definition: sql_insert.h:81
COPY_INFO info
Definition: sql_insert.h:79
bool bulk_insert_started
ha_start_bulk_insert has been called. Never cleared.
Definition: sql_insert.h:75
Table_ref * table_list
The table used for insertion of rows.
Definition: sql_insert.h:62
Definition: query_result.h:174
Abstract base class for traversing the Query_block tree.
Definition: select_lex_visitor.h:40
Definition: sql_cmd_dml.h:35
Base class for all INSERT and REPLACE statements.
Definition: sql_insert.h:223
mem_root_deque< Item * > values_field_list
Field list for VALUES derived table.
Definition: sql_insert.h:291
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:295
mem_root_deque< Item * > update_field_list
ON DUPLICATE KEY UPDATE field list.
Definition: sql_insert.h:275
Create_col_name_list * values_column_list
Definition: sql_insert.h:284
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:263
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:283
uint column_count
Number of columns in original insert column list.
Definition: sql_insert.h:269
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:239
uint value_count
Number of values per row in insert_many_values, available after resolving.
Definition: sql_insert.h:272
const enum_duplicates duplicates
Definition: sql_insert.h:293
mem_root_deque< Item * > insert_field_list
Field list to insert/replace.
Definition: sql_insert.h:251
mem_root_deque< Item * > update_value_list
ON DUPLICATE KEY UPDATE data value list.
Definition: sql_insert.h:278
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:3310
bool values_need_privilege_check
True if VALUES clause contain column references that need privilege check.
Definition: sql_insert.h:266
Class that implements INSERT ... SELECT and REPLACE ... SELECT statements.
Definition: sql_insert.h:334
const MYSQL_LEX_CSTRING * eligible_secondary_storage_engine() 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:3342
Sql_cmd_insert_select(bool is_replace_arg, enum_duplicates duplicates_arg)
Definition: sql_insert.h:336
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_insert.h:340
Class that implements INSERT ... VALUES and REPLACE ... VALUES statements.
Definition: sql_insert.h:314
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:320
Sql_cmd_insert_values(bool is_replace_arg, enum_duplicates duplicates_arg)
Definition: sql_insert.h:316
bool is_single_table_plan() const override
Definition: sql_insert.h:324
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
Definition: table.h:2791
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:110
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
enum_sql_command
Definition: my_sqlcommand.h:46
@ SQLCOM_INSERT
Definition: my_sqlcommand.h:52
@ SQLCOM_INSERT_SELECT
Definition: my_sqlcommand.h:53
@ SQLCOM_REPLACE
Definition: my_sqlcommand.h:87
@ SQLCOM_REPLACE_SELECT
Definition: my_sqlcommand.h:88
static uint update
Definition: myisamlog.cc:91
Log info(cout, "NOTE")
thread_local MEM_ROOT ** THR_MALLOC
Definition: mysqld.cc:1560
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:2878
Contains classes representing SQL-data change statements.
enum_duplicates
Definition: sql_data_change.h:48
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:3045
Definition: mysql_lex_string.h:40
Definition: lock.h:39
TABLE ** table
Definition: lock.h:40
Definition: my_bitmap.h:43
Definition: table.h:1399
Definition: completion_hash.h:35
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2622
unsigned int uint
Definition: uca9-dump.cc:75
static int is_view(cargo_type x)
Definition: xcom_base.cc:2156