MySQL  8.0.17
Source Code Documentation
sql_insert.h
Go to the documentation of this file.
1 /* Copyright (c) 2006, 2018, 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 
219  public:
220  /*
221  field_list was created for view and should be removed before PS/SP
222  rexecuton
223  */
225 
226  protected:
227  const bool is_replace;
228 
229  public:
230  /**
231  Field list to insert/replace
232 
233  One of two things:
234  1. For the INSERT/REPLACE ... (col1, ... colN) VALUES ... syntax
235  this is a list of col1, ..., colN fields.
236  2. For the INSERT/REPLACE ... SET col1=x1, ... colM=xM syntax extension
237  this is a list of col1, ... colM fields as well.
238  */
240  /**
241  Row data to insert/replace
242 
243  One of two things:
244  1. For the INSERT/REPLACE ... VALUES (row1), (row2), ... (rowN) syntax
245  the list contains N List_item lists: one List_item per row.
246  2. For the INSERT/REPLACE ... SET col1=x1, ... colM=xM syntax extension
247  this list contains only 1 List_item of M data values: this way we
248  emulate this syntax:
249  INSERT/REPLACE ... (col1, ... colM) VALUE (x1, ..., xM);
250  */
252  /// Number of values per row in insert_many_values, available after resolving
254 
255  /// ON DUPLICATE KEY UPDATE field list
257 
258  /// ON DUPLICATE KEY UPDATE data value list
260 
262 
263  explicit Sql_cmd_insert_base(bool is_replace_arg,
264  enum_duplicates duplicates_arg)
266  is_replace(is_replace_arg),
267  value_count(0),
268  duplicates(duplicates_arg) {}
269 
270  virtual void cleanup(THD *) override {
272  empty_field_list_on_rset = false;
274  }
275  }
276 
277  virtual bool accept(THD *thd, Select_lex_visitor *visitor) override;
278 };
279 
280 /**
281  Class that implements INSERT ... VALUES and REPLACE ... VALUES statements.
282 */
283 
285  public:
286  explicit Sql_cmd_insert_values(bool is_replace_arg,
287  enum_duplicates duplicates_arg)
288  : Sql_cmd_insert_base(is_replace_arg, duplicates_arg) {}
289 
292  }
293 
294  virtual bool is_single_table_plan() const { return true; }
295 
296  protected:
297  virtual bool execute_inner(THD *thd);
298 };
299 
300 /**
301  Class that implements INSERT ... SELECT and REPLACE ... SELECT statements.
302 */
303 
305  public:
306  explicit Sql_cmd_insert_select(bool is_replace_arg,
307  enum_duplicates duplicates_arg)
308  : Sql_cmd_insert_base(is_replace_arg, duplicates_arg) {}
309 
312  }
313 };
314 
315 #endif /* SQL_INSERT_INCLUDED */
virtual enum_sql_command sql_command_code() const
Return the command code for this statement.
Definition: sql_insert.h:310
unsigned long long int ulonglong
Definition: my_inttypes.h:74
virtual bool is_single_table_plan() const
Definition: sql_insert.h:294
List< List_item > insert_many_values
Row data to insert/replace.
Definition: sql_insert.h:251
Definition: sql_insert.h:55
Definition: sql_insert.h:170
const enum_duplicates duplicates
Definition: sql_insert.h:261
Definition: my_sqlcommand.h:52
Class that implements INSERT ...
Definition: sql_insert.h:284
Some integer typedefs for easier portability.
virtual void store_values(THD *thd, List< Item > &values)
Definition: sql_insert.cc:2162
bool empty_field_list_on_rset
Definition: sql_insert.h:224
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:2018
Definition: field.h:709
virtual void cleanup(THD *) override
Command-specific reinitialization before execution of prepared statement.
Definition: sql_insert.h:270
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:2286
enum_sql_command
Definition: my_sqlcommand.h:45
List< Item > List_item
Definition: sql_insert.h:44
Sergei Dialog Client Authentication NULL
Definition: dialog.cc:352
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:1585
Sql_cmd_insert_values(bool is_replace_arg, enum_duplicates duplicates_arg)
Definition: sql_insert.h:286
Field ** field
Definition: sql_insert.h:175
void send_error(THD *thd, uint errcode, const char *err) override
Definition: sql_insert.cc:2175
class udf_list * list
bool start_execution(THD *thd) override
Lock the newly created table and prepare it for insertion.
Definition: sql_insert.cc:2590
bool bulk_insert_started
ha_start_bulk_insert has been called. Never cleared.
Definition: sql_insert.h:71
Definition: table.h:1294
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:185
TABLE_LIST * select_tables
Definition: sql_insert.h:173
void empty()
Definition: sql_list.h:141
#define DBUG_ASSERT(A)
Definition: my_dbug.h:183
bool send_data(THD *thd, List< Item > &items) override
Definition: sql_insert.cc:2095
void drop_open_table(THD *thd)
Close and drop just created table in CREATE TABLE ...
Definition: sql_insert.cc:2928
COPY_INFO info
Definition: sql_insert.h:75
List< Item > insert_field_list
Field list to insert/replace.
Definition: sql_insert.h:239
void cleanup(THD *thd) override
Cleanup after this execution.
Definition: sql_insert.cc:2084
int binlog_show_create_table(THD *thd)
Definition: sql_insert.cc:2685
Definition: item.h:666
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:174
TABLE_LIST * create_table
Definition: sql_insert.h:171
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:1981
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2275
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:539
static uint update
Definition: myisamlog.cc:88
Sql_cmd_insert_base(bool is_replace_arg, enum_duplicates duplicates_arg)
Definition: sql_insert.h:263
bool insert_into_view
Definition: sql_insert.h:77
bool resolve_update_expressions(THD *thd)
Resolve ON DUPLICATE KEY UPDATE expressions.
Definition: sql_insert.cc:1482
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:2516
Definition: my_sqlcommand.h:87
void abort_result_set(THD *thd) override
Definition: sql_insert.cc:2989
uint value_count
Number of values per row in insert_many_values, available after resolving.
Definition: sql_insert.h:253
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:290
MYSQL_LOCK * m_lock
Definition: sql_insert.h:177
This class encapsulates a data change operation.
Definition: sql_data_change.h:71
virtual bool accept(THD *thd, Select_lex_visitor *visitor) override
Definition: sql_insert.cc:3042
Base class for all INSERT and REPLACE statements.
Definition: sql_insert.h:210
virtual bool prepare_inner(THD *thd) override
Prepare items in INSERT statement.
Definition: sql_insert.cc:984
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:188
bool need_explain_interceptor() const override
Definition: sql_insert.h:147
List< Item > update_field_list
ON DUPLICATE KEY UPDATE field list.
Definition: sql_insert.h:256
MYSQL_LOCK ** m_plock
Definition: sql_insert.h:179
virtual bool precheck(THD *thd) override
Perform a precheck of table privileges for the specific operation.
Definition: sql_insert.cc:417
Definition: sql_cmd_dml.h:32
Definition: handler.h:2607
void store_values(THD *thd, List< Item > &values) override
Definition: sql_insert.cc:2754
void prepare_triggers_for_insert_stmt(THD *thd, TABLE *table)
Prepare triggers for INSERT-like statement.
Definition: sql_insert.cc:322
HA_CREATE_INFO * create_info
Definition: sql_insert.h:172
bool stmt_binlog_is_trans() const override
Indicates whether this statement should be written to binary log&#39;s transactional cache in statement m...
Definition: sql_insert.cc:2785
Log info(cout, "NOTE")
bool start_execution(THD *thd) override
Set up the target table for execution.
Definition: sql_insert.cc:2073
Definition: my_sqlcommand.h:51
Definition: table.h:2442
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:295
const bool is_replace
Definition: sql_insert.h:227
Sql_cmd_insert_select(bool is_replace_arg, enum_duplicates duplicates_arg)
Definition: sql_insert.h:306
Definition: lock.h:38
Class that implements INSERT ...
Definition: sql_insert.h:304
bool prepare(THD *thd, List< Item > &list, SELECT_LEX_UNIT *u) override
Create the new table from the selected items.
Definition: sql_insert.cc:2546
bool send_eof(THD *thd) override
Definition: sql_insert.cc:2187
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
virtual bool execute_inner(THD *thd)
Insert one or more rows from a VALUES list into a table.
Definition: sql_insert.cc:438
void send_error(THD *thd, uint errcode, const char *err) override
Definition: sql_insert.cc:2759
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:259
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:777
bool send_eof(THD *thd) override
Definition: sql_insert.cc:2793
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:2183
TABLE ** table
Definition: lock.h:39