MySQL 8.0.29
Source Code Documentation
sql_insert.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_INSERT_INCLUDED
24#define SQL_INSERT_INCLUDED
25
26#include <assert.h>
27#include <stddef.h>
28#include <sys/types.h>
29
30#include "my_inttypes.h"
31#include "my_sqlcommand.h"
32#include "sql/current_thd.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/sql_list.h"
37#include "sql/table.h"
38
39class Alter_info;
40class Field;
41class Item;
43class THD;
44struct HA_CREATE_INFO;
45struct handlerton;
46
48struct MYSQL_LOCK;
49
51 TABLE_LIST *table_list);
55
57 public:
58 /// The table used for insertion of rows
60 TABLE *table{nullptr};
61
62 private:
63 /**
64 The columns of the table to be inserted into, *or* the columns of the
65 table from which values are selected. For legacy reasons both are
66 allowed.
67 */
69
70 protected:
71 /// ha_start_bulk_insert has been called. Never cleared.
73
74 public:
77 COPY_INFO update; ///< the UPDATE part of "info"
79
80 /**
81 Creates a Query_result_insert for routing a result set to an existing
82 table.
83
84 @param table_list_par The table reference for the destination table.
85 @param target_columns See details.
86 @param target_or_source_columns See details.
87 @param update_fields The columns to be updated in case of duplicate
88 keys. May be NULL.
89 @param update_values The values to be assigned in case of duplicate
90 keys. May be NULL.
91 @param duplic The policy for handling duplicates.
92
93 @todo This constructor takes 8 arguments, 6 of which are used to
94 immediately construct a COPY_INFO object. Obviously the constructor
95 should take the COPY_INFO object as argument instead. Also, some
96 Query_result_insert members initialized here are totally redundant, as they
97are found inside the COPY_INFO.
98
99 The target_columns and target_or_source_columns arguments are set by
100 callers as follows:
101 @li if CREATE SELECT:
102 - target_columns == NULL,
103 - target_or_source_columns == expressions listed after SELECT, as in
104 CREATE ... SELECT expressions
105 @li if INSERT SELECT:
106 target_columns
107 == target_or_source_columns
108 == columns listed between INSERT and SELECT, as in
109 INSERT INTO t (columns) SELECT ...
110
111 We set the manage_defaults argument of info's constructor as follows
112 ([...] denotes something optional):
113 @li If target_columns==NULL, the statement is
114@verbatim
115 CREATE TABLE a_table [(columns1)] SELECT expressions2
116@endverbatim
117 so 'info' must manage defaults of columns1.
118 @li Otherwise it is:
119@verbatim
120 INSERT INTO a_table [(columns1)] SELECT ...
121@endverbatim
122 target_columns is columns1, if not empty then 'info' must manage defaults
123 of other columns than columns1.
124 */
126 mem_root_deque<Item *> *target_columns,
127 mem_root_deque<Item *> *target_or_source_columns,
128 mem_root_deque<Item *> *update_fields,
129 mem_root_deque<Item *> *update_values,
130 enum_duplicates duplic)
132 table_list(table_list_par),
133 fields(target_or_source_columns),
134 info(COPY_INFO::INSERT_OPERATION, target_columns,
135 // manage_defaults
136 (target_columns == nullptr || !target_columns->empty()), duplic),
137 update(COPY_INFO::UPDATE_OPERATION, update_fields, update_values),
138 insert_into_view(table_list_par && table_list_par->is_view()) {
139 assert(target_or_source_columns != nullptr);
140 assert(target_columns == target_or_source_columns ||
141 target_columns == nullptr);
142 }
143
144 public:
145 bool need_explain_interceptor() const override { return true; }
146 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
147 Query_expression *u) override;
148 bool start_execution(THD *thd) override;
149 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
150 virtual void store_values(THD *thd, const mem_root_deque<Item *> &values);
151 void send_error(THD *thd, uint errcode, const char *err) override;
152 bool send_eof(THD *thd) override;
153 void abort_result_set(THD *thd) override;
154 void cleanup(THD *thd) override;
155
156 private:
157 /**
158 Indicates whether this statement should be written to binary log's
159 transactional cache in statement mode.
160 */
161 virtual bool stmt_binlog_is_trans() const;
162};
163
164/**
165 @todo This class inherits a class which is non-abstract. This is not in
166 line with good programming practices and the inheritance should be broken
167 up.
168*/
170 /// Handle for table to be created
172 /// Contains further information for table creation
174 /// Contains further information for table creation
177 /// List of tables that are select from
179 /// Pointer to first field in table generated from query expression
180 Field **table_fields{nullptr};
181 /// lock data for tmp table
183 /// m_lock or thd->extra_lock
184 MYSQL_LOCK **m_plock{nullptr};
185 /**
186 If table being created has SE supporting atomic DDL, pointer to SE's
187 handlerton object to be used for calling SE post-DDL hook, nullptr -
188 otherwise.
189 */
191
192 public:
193 Query_result_create(TABLE_LIST *create_table_arg,
195 TABLE_LIST *select_tables_arg);
196
197 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
198 Query_expression *u) override;
199 void store_values(THD *thd, const mem_root_deque<Item *> &values) override;
200 void send_error(THD *thd, uint errcode, const char *err) 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);
233 bool get_default_columns(THD *thd, TABLE *table,
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};
344
345#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:73
Definition: field.h:573
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:802
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:629
Definition: sql_insert.h:169
Field ** field
Definition: sql_insert.h:176
HA_CREATE_INFO * create_info
Contains further information for table creation.
Definition: sql_insert.h:173
TABLE_LIST * select_tables
List of tables that are select from.
Definition: sql_insert.h:178
void send_error(THD *thd, uint errcode, const char *err) override
Definition: sql_insert.cc:3073
Field ** table_fields
Pointer to first field in table generated from query expression.
Definition: sql_insert.h:180
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:190
void abort_result_set(THD *thd) override
Definition: sql_insert.cc:3291
Query_result_create(TABLE_LIST *create_table_arg, mem_root_deque< Item * > *fields, enum_duplicates duplic, TABLE_LIST *select_tables_arg)
Definition: sql_insert.cc:2812
Alter_info * alter_info
Contains further information for table creation.
Definition: sql_insert.h:175
int binlog_show_create_table(THD *thd)
Definition: sql_insert.cc:2978
bool start_execution(THD *thd) override
Lock the newly created table and prepare it for insertion.
Definition: sql_insert.cc:2885
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:3097
bool send_eof(THD *thd) override
Definition: sql_insert.cc:3105
void store_values(THD *thd, const mem_root_deque< Item * > &values) override
Definition: sql_insert.cc:3054
TABLE_LIST * create_table
Handle for table to be created.
Definition: sql_insert.h:171
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:2825
MYSQL_LOCK * m_lock
lock data for tmp table
Definition: sql_insert.h:182
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:184
void drop_open_table(THD *thd)
Close and drop just created table in CREATE TABLE ... SELECT in case of error.
Definition: sql_insert.cc:3231
bool create_table_for_query_block(THD *thd) override
Create new table.
Definition: sql_insert.cc:2841
Definition: sql_insert.h:56
virtual void store_values(THD *thd, const mem_root_deque< Item * > &values)
Definition: sql_insert.cc:2430
COPY_INFO update
the UPDATE part of "info"
Definition: sql_insert.h:77
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:68
bool start_execution(THD *thd) override
Set up the target table for execution.
Definition: sql_insert.cc:2287
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:2258
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: sql_insert.cc:2356
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:2451
bool send_eof(THD *thd) override
Definition: sql_insert.cc:2455
void abort_result_set(THD *thd) override
Definition: sql_insert.cc:2557
ulonglong autoinc_value_of_last_inserted_row
Definition: sql_insert.h:75
Query_result_insert(TABLE_LIST *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:125
TABLE * table
Definition: sql_insert.h:60
bool need_explain_interceptor() const override
Definition: sql_insert.h:145
void send_error(THD *thd, uint errcode, const char *err) override
Definition: sql_insert.cc:2445
bool insert_into_view
Definition: sql_insert.h:78
COPY_INFO info
Definition: sql_insert.h:76
bool bulk_insert_started
ha_start_bulk_insert has been called. Never cleared.
Definition: sql_insert.h:72
void cleanup(THD *thd) override
Cleanup after this execution.
Definition: sql_insert.cc:2340
TABLE_LIST * table_list
The table used for insertion of rows.
Definition: sql_insert.h:59
Definition: query_result.h:200
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:1635
bool prepare_values_table(THD *thd)
Prepare the derived table created as a VALUES alias.
Definition: sql_insert.cc:1563
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:1726
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:1674
bool precheck(THD *thd) override
Perform a precheck of table privileges for the specific operation.
Definition: sql_insert.cc:423
bool prepare_inner(THD *thd) override
Prepare items in INSERT statement.
Definition: sql_insert.cc:1001
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:946
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
TABLE_LIST * values_table
ON DUPLICATE KEY UPDATE reference to VALUES.
Definition: sql_insert.h:282
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:437
bool accept(THD *thd, Select_lex_visitor *visitor) override
Definition: sql_insert.cc:3341
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
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:3373
Sql_cmd_insert_select(bool is_replace_arg, enum_duplicates duplicates_arg)
Definition: sql_insert.h:335
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:472
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_class.h:945
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:109
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:221
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:90
Log info(cout, "NOTE")
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
Contains classes representing SQL-data change statements.
enum_duplicates
Definition: sql_data_change.h:47
bool check_that_all_fields_are_given_values(THD *thd, TABLE *entry, TABLE_LIST *table_list)
Check that all fields with aren't null_fields are used.
Definition: sql_insert.cc:2221
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:303
void prepare_triggers_for_insert_stmt(THD *thd, TABLE *table)
Prepare triggers for INSERT-like statement.
Definition: sql_insert.cc:329
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:1795
Definition: handler.h:2790
Definition: mysql_lex_string.h:39
Definition: lock.h:38
TABLE ** table
Definition: lock.h:39
Definition: my_bitmap.h:42
Definition: table.h:2684
Definition: table.h:1394
Definition: completion_hash.h:34
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2416
unsigned int uint
Definition: uca-dump.cc:29
static int is_view(cargo_type x)
Definition: xcom_base.cc:2150