MySQL  8.0.19
Source Code Documentation
sql_insert.h
Go to the documentation of this file.
1 /* Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
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 <stddef.h>
27 #include <sys/types.h>
28 
29 #include "my_dbug.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/sql_list.h"
36 #include "sql/table.h"
37 
38 class Alter_info;
39 class Field;
40 class Item;
41 class SELECT_LEX_UNIT;
42 class THD;
43 struct HA_CREATE_INFO;
44 struct handlerton;
45 
46 typedef List<Item> List_item;
47 struct MYSQL_LOCK;
48 
50  TABLE_LIST *table_list);
54 
56  public:
57  /// The table used for insertion of rows
60 
61  private:
62  /**
63  The columns of the table to be inserted into, *or* the columns of the
64  table from which values are selected. For legacy reasons both are
65  allowed.
66  */
68 
69  protected:
70  /// ha_start_bulk_insert has been called. Never cleared.
72 
73  public:
76  COPY_INFO update; ///< the UPDATE part of "info"
78 
79  /**
80  Creates a Query_result_insert for routing a result set to an existing
81  table.
82 
83  @param table_list_par The table reference for the destination table.
84  @param table_par The destination table. May be NULL.
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
97 are 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  */
125  Query_result_insert(TABLE_LIST *table_list_par, TABLE *table_par,
126  List<Item> *target_columns,
127  List<Item> *target_or_source_columns,
128  List<Item> *update_fields, List<Item> *update_values,
129  enum_duplicates duplic)
131  table_list(table_list_par),
132  table(table_par),
133  fields(target_or_source_columns),
136  info(COPY_INFO::INSERT_OPERATION, target_columns,
137  // manage_defaults
138  (target_columns == NULL || target_columns->elements != 0), duplic),
139  update(COPY_INFO::UPDATE_OPERATION, update_fields, update_values),
140  insert_into_view(table_list_par && table_list_par->is_view()) {
141  DBUG_ASSERT(target_or_source_columns != NULL);
142  DBUG_ASSERT(target_columns == target_or_source_columns ||
143  target_columns == NULL);
144  }
145 
146  public:
147  bool need_explain_interceptor() const override { return true; }
148  bool prepare(THD *thd, List<Item> &list, SELECT_LEX_UNIT *u) override;
149  bool start_execution(THD *thd) override;
150  bool send_data(THD *thd, List<Item> &items) override;
151  virtual void store_values(THD *thd, List<Item> &values);
152  void send_error(THD *thd, uint errcode, const char *err) override;
153  bool send_eof(THD *thd) override;
154  void abort_result_set(THD *thd) override;
155  void cleanup(THD *thd) 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 */
176  /* lock data for tmp table */
178  /* m_lock or thd->extra_lock */
180  /**
181  If table being created has SE supporting atomic DDL, pointer to SE's
182  handlerton object to be used for calling SE post-DDL hook, nullptr -
183  otherwise.
184  */
186 
187  public:
188  Query_result_create(TABLE_LIST *table_arg, HA_CREATE_INFO *create_info_par,
189  Alter_info *alter_info_arg, List<Item> &select_fields,
190  enum_duplicates duplic, TABLE_LIST *select_tables_arg);
191 
192  bool prepare(THD *thd, List<Item> &list, SELECT_LEX_UNIT *u) override;
193  void store_values(THD *thd, List<Item> &values) override;
194  void send_error(THD *thd, uint errcode, const char *err) override;
195  bool send_eof(THD *thd) override;
196  void abort_result_set(THD *thd) override;
197  bool start_execution(THD *thd) override;
198 
199  private:
200  bool stmt_binlog_is_trans() const override;
201  int binlog_show_create_table(THD *thd);
202  void drop_open_table(THD *thd);
203 };
204 
205 /**
206  Base class for all INSERT and REPLACE statements. Abstract class that
207  is inherited by Sql_cmd_insert_values and Sql_cmd_insert_select.
208 */
209 
211  protected:
212  virtual bool precheck(THD *thd) override;
213 
214  virtual bool prepare_inner(THD *thd) override;
215 
216  private:
217  bool resolve_update_expressions(THD *thd);
218  bool prepare_values_table(THD *thd);
220 
221  public:
222  /*
223  field_list was created for view and should be removed before PS/SP
224  rexecuton
225  */
227 
228  protected:
229  const bool is_replace;
230 
231  public:
232  /**
233  Field list to insert/replace
234 
235  One of two things:
236  1. For the INSERT/REPLACE ... (col1, ... colN) VALUES ... syntax
237  this is a list of col1, ..., colN fields.
238  2. For the INSERT/REPLACE ... SET col1=x1, ... colM=xM syntax extension
239  this is a list of col1, ... colM fields as well.
240  */
242  /**
243  Row data to insert/replace
244 
245  One of two things:
246  1. For the INSERT/REPLACE ... VALUES (row1), (row2), ... (rowN) syntax
247  the list contains N List_item lists: one List_item per row.
248  2. For the INSERT/REPLACE ... SET col1=x1, ... colM=xM syntax extension
249  this list contains only 1 List_item of M data values: this way we
250  emulate this syntax:
251  INSERT/REPLACE ... (col1, ... colM) VALUE (x1, ..., xM);
252  */
254  /// Number of values per row in insert_many_values, available after resolving
256 
257  /// ON DUPLICATE KEY UPDATE field list
259 
260  /// ON DUPLICATE KEY UPDATE data value list
262 
263  /**
264  ON DUPLICATE KEY UPDATE reference to VALUES.. as a derived table.
265  */
268 
269  /**
270  Field list for VALUES derived table. If no insert_field exists (e.g. INSERT
271  INTO t0 ..), we have to create one to create Item_insert_values for ODKU
272  statements.
273  */
275 
277 
278  explicit Sql_cmd_insert_base(bool is_replace_arg,
279  enum_duplicates duplicates_arg)
281  is_replace(is_replace_arg),
282  value_count(0),
283  duplicates(duplicates_arg) {}
284 
285  virtual void cleanup(THD *) override {
287  empty_field_list_on_rset = false;
288  insert_field_list.empty();
289  }
290  }
291 
292  virtual bool accept(THD *thd, Select_lex_visitor *visitor) override;
293 };
294 
295 /**
296  Class that implements INSERT ... VALUES and REPLACE ... VALUES statements.
297 */
298 
300  public:
301  explicit Sql_cmd_insert_values(bool is_replace_arg,
302  enum_duplicates duplicates_arg)
303  : Sql_cmd_insert_base(is_replace_arg, duplicates_arg) {}
304 
307  }
308 
309  virtual bool is_single_table_plan() const { return true; }
310 
311  protected:
312  virtual bool execute_inner(THD *thd);
313 };
314 
315 /**
316  Class that implements INSERT ... SELECT and REPLACE ... SELECT statements.
317 */
318 
320  public:
321  explicit Sql_cmd_insert_select(bool is_replace_arg,
322  enum_duplicates duplicates_arg)
323  : Sql_cmd_insert_base(is_replace_arg, duplicates_arg) {}
324 
327  }
328 };
329 
330 #endif /* SQL_INSERT_INCLUDED */
SQLCOM_REPLACE
@ SQLCOM_REPLACE
Definition: my_sqlcommand.h:86
Item
Definition: item.h:665
THD
Definition: sql_class.h:764
Query_result_insert::store_values
virtual void store_values(THD *thd, List< Item > &values)
Definition: sql_insert.cc:2342
is_view
static int is_view(cargo_type x)
Definition: xcom_base.c:1868
Query_result_create::alter_info
Alter_info * alter_info
Definition: sql_insert.h:174
Query_result_insert::table
TABLE * table
Definition: sql_insert.h:59
Sql_cmd_dml
Definition: sql_cmd_dml.h:32
Alter_info
Data describing the table being created by CREATE TABLE or altered by ALTER TABLE.
Definition: sql_alter.h:189
NULL
#define NULL
Definition: types.h:55
Sql_cmd_insert_base::value_count
uint value_count
Number of values per row in insert_many_values, available after resolving.
Definition: sql_insert.h:255
Query_result_insert::stmt_binlog_is_trans
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:2362
Query_result_create::drop_open_table
void drop_open_table(THD *thd)
Close and drop just created table in CREATE TABLE ...
Definition: sql_insert.cc:3131
prepare_triggers_for_insert_stmt
void prepare_triggers_for_insert_stmt(THD *thd, TABLE *table)
Prepare triggers for INSERT-like statement.
Definition: sql_insert.cc:328
my_sqlcommand.h
Sql_cmd_insert_base::resolve_update_expressions
bool resolve_update_expressions(THD *thd)
Resolve ON DUPLICATE KEY UPDATE expressions.
Definition: sql_insert.cc:1632
Sql_cmd_insert_values::Sql_cmd_insert_values
Sql_cmd_insert_values(bool is_replace_arg, enum_duplicates duplicates_arg)
Definition: sql_insert.h:301
Sql_cmd_insert_base::Sql_cmd_insert_base
Sql_cmd_insert_base(bool is_replace_arg, enum_duplicates duplicates_arg)
Definition: sql_insert.h:278
handlerton
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2271
my_dbug.h
TABLE
Definition: table.h:1305
Query_result_insert::cleanup
void cleanup(THD *thd) override
Cleanup after this execution.
Definition: sql_insert.cc:2267
Query_result_insert::update
COPY_INFO update
the UPDATE part of "info"
Definition: sql_insert.h:76
Sql_cmd_insert_base::update_field_list
List< Item > update_field_list
ON DUPLICATE KEY UPDATE field list.
Definition: sql_insert.h:258
query_result.h
Sql_cmd_insert_base::precheck
virtual bool precheck(THD *thd) override
Perform a precheck of table privileges for the specific operation.
Definition: sql_insert.cc:423
Query_result_insert::fields
List< 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:67
Query_result_create::send_error
void send_error(THD *thd, uint errcode, const char *err) override
Definition: sql_insert.cc:2964
Sql_cmd_insert_base::accept
virtual bool accept(THD *thd, Select_lex_visitor *visitor) override
Definition: sql_insert.cc:3241
Query_result_create::send_eof
bool send_eof(THD *thd) override
Definition: sql_insert.cc:2996
Sql_cmd_insert_base::prepare_inner
virtual bool prepare_inner(THD *thd) override
Prepare items in INSERT statement.
Definition: sql_insert.cc:989
Sql_cmd_insert_base
Base class for all INSERT and REPLACE statements.
Definition: sql_insert.h:210
SELECT_LEX_UNIT
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:569
Sql_cmd_insert_values::is_single_table_plan
virtual bool is_single_table_plan() const
Definition: sql_insert.h:309
Query_result_interceptor
Definition: query_result.h:177
Query_result_insert::abort_result_set
void abort_result_set(THD *thd) override
Definition: sql_insert.cc:2465
json_binary::err
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:908
Sql_cmd_insert_base::is_replace
const bool is_replace
Definition: sql_insert.h:229
Query_result_create::abort_result_set
void abort_result_set(THD *thd) override
Definition: sql_insert.cc:3191
Query_result_insert::Query_result_insert
Query_result_insert(TABLE_LIST *table_list_par, TABLE *table_par, List< Item > *target_columns, List< Item > *target_or_source_columns, List< Item > *update_fields, List< Item > *update_values, enum_duplicates duplic)
Definition: sql_insert.h:125
write_record
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:1740
validate_default_values_of_unset_fields
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:302
Sql_cmd_insert_base::update_value_list
List< Item > update_value_list
ON DUPLICATE KEY UPDATE data value list.
Definition: sql_insert.h:261
my_inttypes.h
Query_result_insert::table_list
TABLE_LIST * table_list
The table used for insertion of rows.
Definition: sql_insert.h:58
Sql_cmd_insert_select::sql_command_code
virtual enum_sql_command sql_command_code() const
Return the command code for this statement.
Definition: sql_insert.h:325
enum_sql_command
enum_sql_command
Definition: my_sqlcommand.h:45
List_item
List< Item > List_item
Definition: sql_insert.h:44
Sql_cmd_insert_base::insert_many_values
List< List_item > insert_many_values
Row data to insert/replace.
Definition: sql_insert.h:253
Query_result_create::create_table
TABLE_LIST * create_table
Definition: sql_insert.h:171
uint
unsigned int uint
Definition: uca-dump.cc:29
MYSQL_LOCK::table
TABLE ** table
Definition: lock.h:39
Mem_root_array_YY< LEX_CSTRING >
Query_result_create::m_plock
MYSQL_LOCK ** m_plock
Definition: sql_insert.h:179
Sql_cmd_insert_base::prepare_values_table
bool prepare_values_table(THD *thd)
Prepare the derived table created as a VALUES alias.
Definition: sql_insert.cc:1521
SQLCOM_REPLACE_SELECT
@ SQLCOM_REPLACE_SELECT
Definition: my_sqlcommand.h:87
Query_result_create::start_execution
bool start_execution(THD *thd) override
Lock the newly created table and prepare it for insertion.
Definition: sql_insert.cc:2784
list
class udf_list * list
Query_result_create::Query_result_create
Query_result_create(TABLE_LIST *table_arg, HA_CREATE_INFO *create_info_par, Alter_info *alter_info_arg, List< Item > &select_fields, enum_duplicates duplic, TABLE_LIST *select_tables_arg)
Definition: sql_insert.cc:2710
TABLE_LIST
Definition: table.h:2467
Query_result_insert::info
COPY_INFO info
Definition: sql_insert.h:75
Query_result_create::store_values
void store_values(THD *thd, List< Item > &values) override
Definition: sql_insert.cc:2947
Sql_cmd_insert_base::duplicates
const enum_duplicates duplicates
Definition: sql_insert.h:276
sql_cmd_dml.h
Query_result_create::stmt_binlog_is_trans
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:2988
update
static uint update
Definition: myisamlog.cc:88
Sql_cmd_insert_select
Class that implements INSERT ...
Definition: sql_insert.h:319
Query_result_create::select_tables
TABLE_LIST * select_tables
Definition: sql_insert.h:173
Select_lex_visitor
Abstract base class for traversing the SELECT_LEX tree.
Definition: select_lex_visitor.h:39
Query_result_create::m_post_ddl_ht
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:185
Query_result_insert::need_explain_interceptor
bool need_explain_interceptor() const override
Definition: sql_insert.h:147
Sql_cmd_insert_base::cleanup
virtual void cleanup(THD *) override
Command-specific reinitialization before execution of prepared statement.
Definition: sql_insert.h:285
Query_result_create::prepare
bool prepare(THD *thd, List< Item > &list, SELECT_LEX_UNIT *u) override
Create the new table from the selected items.
Definition: sql_insert.cc:2740
Query_result_insert::autoinc_value_of_last_inserted_row
ulonglong autoinc_value_of_last_inserted_row
Definition: sql_insert.h:74
_entry
Definition: completion_hash.h:34
SQLCOM_INSERT
@ SQLCOM_INSERT
Definition: my_sqlcommand.h:51
Sql_cmd_insert_base::empty_field_list_on_rset
bool empty_field_list_on_rset
Definition: sql_insert.h:226
Sql_cmd_insert_base::resolve_values_table_columns
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:1593
sql_list.h
DBUG_ASSERT
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
Sql_cmd_insert_values::execute_inner
virtual bool execute_inner(THD *thd)
Insert one or more rows from a VALUES list into a table.
Definition: sql_insert.cc:444
Query_result_insert::send_data
bool send_data(THD *thd, List< Item > &items) override
Definition: sql_insert.cc:2276
Query_result_create::field
Field ** field
Definition: sql_insert.h:175
Query_result_insert::insert_into_view
bool insert_into_view
Definition: sql_insert.h:77
Query_result_create::m_lock
MYSQL_LOCK * m_lock
Definition: sql_insert.h:177
items
Definition: items.h:34
Sql_cmd_insert_select::Sql_cmd_insert_select
Sql_cmd_insert_select(bool is_replace_arg, enum_duplicates duplicates_arg)
Definition: sql_insert.h:321
SQLCOM_INSERT_SELECT
@ SQLCOM_INSERT_SELECT
Definition: my_sqlcommand.h:52
Query_result_insert::send_eof
bool send_eof(THD *thd) override
Definition: sql_insert.cc:2366
Sql_cmd_insert_base::values_column_list
Create_col_name_list * values_column_list
Definition: sql_insert.h:267
table.h
Query_result_create::binlog_show_create_table
int binlog_show_create_table(THD *thd)
Definition: sql_insert.cc:2878
ulonglong
unsigned long long int ulonglong
Definition: my_inttypes.h:55
COPY_INFO
This class encapsulates a data change operation.
Definition: sql_data_change.h:71
List< Item >
Query_result_insert::prepare
bool prepare(THD *thd, List< Item > &list, SELECT_LEX_UNIT *u) override
Perform preparation specific to the query expression or DML statement.
Definition: sql_insert.cc:2202
enum_duplicates
enum_duplicates
Definition: sql_data_change.h:45
Sql_cmd_insert_values
Class that implements INSERT ...
Definition: sql_insert.h:299
Field
Definition: field.h:695
check_that_all_fields_are_given_values
bool check_that_all_fields_are_given_values(THD *thd, TABLE *entry, TABLE_LIST *table_list)
Check that all fields with arn't null_fields are used.
Definition: sql_insert.cc:2165
Sql_cmd_insert_values::sql_command_code
virtual enum_sql_command sql_command_code() const
Return the command code for this statement.
Definition: sql_insert.h:305
final
#define final(a, b, c)
Definition: hash.c:109
MYSQL_LOCK
Definition: lock.h:38
Query_result_create
Definition: sql_insert.h:170
Query_result_create::create_info
HA_CREATE_INFO * create_info
Definition: sql_insert.h:172
Sql_cmd_insert_base::values_field_list
List< Item > values_field_list
Field list for VALUES derived table.
Definition: sql_insert.h:274
sql_data_change.h
Sql_cmd_insert_base::values_table
TABLE_LIST * values_table
ON DUPLICATE KEY UPDATE reference to VALUES.
Definition: sql_insert.h:266
Query_result_insert::send_error
void send_error(THD *thd, uint errcode, const char *err) override
Definition: sql_insert.cc:2356
info
Log info(cout, "NOTE")
Sql_cmd_insert_base::insert_field_list
List< Item > insert_field_list
Field list to insert/replace.
Definition: sql_insert.h:241
HA_CREATE_INFO
Definition: handler.h:2628
Query_result_insert::bulk_insert_started
bool bulk_insert_started
ha_start_bulk_insert has been called. Never cleared.
Definition: sql_insert.h:71
Query_result_insert
Definition: sql_insert.h:55
Query_result_insert::start_execution
bool start_execution(THD *thd) override
Set up the target table for execution.
Definition: sql_insert.cc:2256
false
#define false
Definition: config_static.h:43