MySQL  8.0.20
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),
134  bulk_insert_started(false),
135  autoinc_value_of_last_inserted_row(0),
136  info(COPY_INFO::INSERT_OPERATION, target_columns,
137  // manage_defaults
138  (target_columns == nullptr || target_columns->elements != 0),
139  duplic),
140  update(COPY_INFO::UPDATE_OPERATION, update_fields, update_values),
141  insert_into_view(table_list_par && table_list_par->is_view()) {
142  DBUG_ASSERT(target_or_source_columns != nullptr);
143  DBUG_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, List<Item> &list, SELECT_LEX_UNIT *u) override;
150  bool start_execution(THD *thd) override;
151  bool send_data(THD *thd, List<Item> &items) override;
152  virtual void store_values(THD *thd, List<Item> &values);
153  void send_error(THD *thd, uint errcode, const char *err) override;
154  bool send_eof(THD *thd) override;
155  void abort_result_set(THD *thd) override;
156  void cleanup(THD *thd) 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 */
177  /* lock data for tmp table */
179  /* m_lock or thd->extra_lock */
181  /**
182  If table being created has SE supporting atomic DDL, pointer to SE's
183  handlerton object to be used for calling SE post-DDL hook, nullptr -
184  otherwise.
185  */
187 
188  public:
189  Query_result_create(TABLE_LIST *table_arg, HA_CREATE_INFO *create_info_par,
190  Alter_info *alter_info_arg, List<Item> &select_fields,
191  enum_duplicates duplic, TABLE_LIST *select_tables_arg);
192 
193  bool prepare(THD *thd, List<Item> &list, SELECT_LEX_UNIT *u) override;
194  void store_values(THD *thd, List<Item> &values) override;
195  void send_error(THD *thd, uint errcode, const char *err) override;
196  bool send_eof(THD *thd) override;
197  void abort_result_set(THD *thd) override;
198  bool start_execution(THD *thd) override;
199 
200  private:
201  bool stmt_binlog_is_trans() const override;
202  int binlog_show_create_table(THD *thd);
203  void drop_open_table(THD *thd);
204 };
205 
206 /**
207  Base class for all INSERT and REPLACE statements. Abstract class that
208  is inherited by Sql_cmd_insert_values and Sql_cmd_insert_select.
209 */
210 
212  protected:
213  virtual bool precheck(THD *thd) override;
214 
215  virtual bool prepare_inner(THD *thd) override;
216 
217  private:
218  bool resolve_update_expressions(THD *thd);
219  bool prepare_values_table(THD *thd);
220  bool resolve_values_table_columns(THD *thd);
221 
222  public:
223  /*
224  field_list was created for view and should be removed before PS/SP
225  rexecuton
226  */
228 
229  protected:
230  const bool is_replace;
231 
232  public:
233  /**
234  Field list to insert/replace
235 
236  One of two things:
237  1. For the INSERT/REPLACE ... (col1, ... colN) VALUES ... syntax
238  this is a list of col1, ..., colN fields.
239  2. For the INSERT/REPLACE ... SET col1=x1, ... colM=xM syntax extension
240  this is a list of col1, ... colM fields as well.
241  */
243  /**
244  Row data to insert/replace
245 
246  One of two things:
247  1. For the INSERT/REPLACE ... VALUES (row1), (row2), ... (rowN) syntax
248  the list contains N List_item lists: one List_item per row.
249  2. For the INSERT/REPLACE ... SET col1=x1, ... colM=xM syntax extension
250  this list contains only 1 List_item of M data values: this way we
251  emulate this syntax:
252  INSERT/REPLACE ... (col1, ... colM) VALUE (x1, ..., xM);
253  */
255  /// Number of values per row in insert_many_values, available after resolving
257 
258  /// ON DUPLICATE KEY UPDATE field list
260 
261  /// ON DUPLICATE KEY UPDATE data value list
263 
264  /**
265  ON DUPLICATE KEY UPDATE reference to VALUES.. as a derived table.
266  */
267  TABLE_LIST *values_table{nullptr};
268  Create_col_name_list *values_column_list{nullptr};
269 
270  /**
271  Field list for VALUES derived table. If no insert_field exists (e.g. INSERT
272  INTO t0 ..), we have to create one to create Item_insert_values for ODKU
273  statements.
274  */
276 
278 
279  explicit Sql_cmd_insert_base(bool is_replace_arg,
280  enum_duplicates duplicates_arg)
281  : empty_field_list_on_rset(false),
282  is_replace(is_replace_arg),
283  value_count(0),
284  duplicates(duplicates_arg) {}
285 
286  virtual void cleanup(THD *) override {
287  if (empty_field_list_on_rset) {
288  empty_field_list_on_rset = false;
289  insert_field_list.empty();
290  }
291  }
292 
293  virtual bool accept(THD *thd, Select_lex_visitor *visitor) override;
294 };
295 
296 /**
297  Class that implements INSERT ... VALUES and REPLACE ... VALUES statements.
298 */
299 
301  public:
302  explicit Sql_cmd_insert_values(bool is_replace_arg,
303  enum_duplicates duplicates_arg)
304  : Sql_cmd_insert_base(is_replace_arg, duplicates_arg) {}
305 
307  return is_replace ? SQLCOM_REPLACE : SQLCOM_INSERT;
308  }
309 
310  virtual bool is_single_table_plan() const { return true; }
311 
312  protected:
313  virtual bool execute_inner(THD *thd);
314 };
315 
316 /**
317  Class that implements INSERT ... SELECT and REPLACE ... SELECT statements.
318 */
319 
321  public:
322  explicit Sql_cmd_insert_select(bool is_replace_arg,
323  enum_duplicates duplicates_arg)
324  : Sql_cmd_insert_base(is_replace_arg, duplicates_arg) {}
325 
327  return is_replace ? SQLCOM_REPLACE_SELECT : SQLCOM_INSERT_SELECT;
328  }
329 };
330 
331 #endif /* SQL_INSERT_INCLUDED */
virtual enum_sql_command sql_command_code() const
Return the command code for this statement.
Definition: sql_insert.h:326
unsigned long long int ulonglong
Definition: my_inttypes.h:55
virtual bool is_single_table_plan() const
Definition: sql_insert.h:310
List< List_item > insert_many_values
Row data to insert/replace.
Definition: sql_insert.h:254
Definition: sql_insert.h:55
Definition: sql_insert.h:171
const enum_duplicates duplicates
Definition: sql_insert.h:277
Definition: my_sqlcommand.h:52
Class that implements INSERT ...
Definition: sql_insert.h:300
Some integer typedefs for easier portability.
virtual void store_values(THD *thd, List< Item > &values)
Definition: sql_insert.cc:2346
bool empty_field_list_on_rset
Definition: sql_insert.h:227
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:2205
Definition: field.h:694
virtual void cleanup(THD *) override
Command-specific reinitialization before execution of prepared statement.
Definition: sql_insert.h:286
enum_duplicates
Definition: sql_data_change.h:45
Definition: my_sqlcommand.h:86
Abstract base class for traversing the SELECT_LEX tree.
Definition: select_lex_visitor.h:39
void abort_result_set(THD *thd) override
Definition: sql_insert.cc:2469
enum_sql_command
Definition: my_sqlcommand.h:45
List< Item > List_item
Definition: sql_insert.h:44
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:1743
Sql_cmd_insert_values(bool is_replace_arg, enum_duplicates duplicates_arg)
Definition: sql_insert.h:302
Field ** field
Definition: sql_insert.h:176
void send_error(THD *thd, uint errcode, const char *err) override
Definition: sql_insert.cc:2360
class udf_list * list
bool bulk_insert_started
ha_start_bulk_insert has been called. Never cleared.
Definition: sql_insert.h:71
Definition: table.h:1306
handlerton * m_post_ddl_ht
If table being created has SE supporting atomic DDL, pointer to SE&#39;s handlerton object to be used for...
Definition: sql_insert.h:186
TABLE_LIST * select_tables
Definition: sql_insert.h:174
List< Item > values_field_list
Field list for VALUES derived table.
Definition: sql_insert.h:275
void empty()
Definition: sql_list.h:141
#define DBUG_ASSERT(A)
Definition: my_dbug.h:199
bool send_data(THD *thd, List< Item > &items) override
Definition: sql_insert.cc:2280
COPY_INFO info
Definition: sql_insert.h:75
List< Item > insert_field_list
Field list to insert/replace.
Definition: sql_insert.h:242
void cleanup(THD *thd) override
Cleanup after this execution.
Definition: sql_insert.cc:2271
Definition: item.h:740
unsigned int uint
Definition: uca-dump.cc:29
#define final(a, b, c)
Definition: hash.c:109
Alter_info * alter_info
Definition: sql_insert.h:175
TABLE_LIST * create_table
Definition: sql_insert.h:172
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:908
bool check_that_all_fields_are_given_values(THD *thd, TABLE *entry, TABLE_LIST *table_list)
Check that all fields with arn&#39;t null_fields are used.
Definition: sql_insert.cc:2168
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2269
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:587
static uint update
Definition: myisamlog.cc:90
Sql_cmd_insert_base(bool is_replace_arg, enum_duplicates duplicates_arg)
Definition: sql_insert.h:279
bool insert_into_view
Definition: sql_insert.h:77
static int is_view(cargo_type x)
Definition: xcom_base.c:1866
Definition: my_sqlcommand.h:87
uint value_count
Number of values per row in insert_many_values, available after resolving.
Definition: sql_insert.h:256
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)
Creates a Query_result_insert for routing a result set to an existing table.
Definition: sql_insert.h:125
Definition: query_result.h:177
virtual enum_sql_command sql_command_code() const
Return the command code for this statement.
Definition: sql_insert.h:306
MYSQL_LOCK * m_lock
Definition: sql_insert.h:178
This class encapsulates a data change operation.
Definition: sql_data_change.h:71
Base class for all INSERT and REPLACE statements.
Definition: sql_insert.h:211
TABLE_LIST * table_list
The table used for insertion of rows.
Definition: sql_insert.h:58
TABLE * table
Definition: sql_insert.h:59
Contains classes representing SQL-data change statements.
Data describing the table being created by CREATE TABLE or altered by ALTER TABLE.
Definition: sql_alter.h:189
bool need_explain_interceptor() const override
Definition: sql_insert.h:148
List< Item > update_field_list
ON DUPLICATE KEY UPDATE field list.
Definition: sql_insert.h:259
MYSQL_LOCK ** m_plock
Definition: sql_insert.h:180
Definition: sql_cmd_dml.h:32
Definition: handler.h:2625
void prepare_triggers_for_insert_stmt(THD *thd, TABLE *table)
Prepare triggers for INSERT-like statement.
Definition: sql_insert.cc:328
HA_CREATE_INFO * create_info
Definition: sql_insert.h:173
Log info(cout, "NOTE")
bool start_execution(THD *thd) override
Set up the target table for execution.
Definition: sql_insert.cc:2260
Definition: my_sqlcommand.h:51
Definition: table.h:2481
COPY_INFO update
the UPDATE part of "info"
Definition: sql_insert.h:76
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
const bool is_replace
Definition: sql_insert.h:230
Sql_cmd_insert_select(bool is_replace_arg, enum_duplicates duplicates_arg)
Definition: sql_insert.h:322
Definition: lock.h:38
Class that implements INSERT ...
Definition: sql_insert.h:320
bool send_eof(THD *thd) override
Definition: sql_insert.cc:2370
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
Definition: completion_hash.h:34
#define false
Definition: config_static.h:43
Definition: items.h:34
List< Item > update_value_list
ON DUPLICATE KEY UPDATE data value list.
Definition: sql_insert.h:262
ulonglong autoinc_value_of_last_inserted_row
Definition: sql_insert.h:74
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:765
Dialog Client Authentication nullptr
Definition: dialog.cc:353
virtual bool stmt_binlog_is_trans() const
Indicates whether this statement should be written to binary log&#39;s transactional cache in statement m...
Definition: sql_insert.cc:2366
TABLE ** table
Definition: lock.h:39