MySQL  8.0.27
Source Code Documentation
sql_data_change.h
Go to the documentation of this file.
1 #ifndef SQL_DATA_CHANGE_INCLUDED
2 #define SQL_DATA_CHANGE_INCLUDED
3 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License, version 2.0,
7  as published by the Free Software Foundation.
8 
9  This program is also distributed with certain software (including
10  but not limited to OpenSSL) that is licensed under separate terms,
11  as designated in a particular file or component or in included license
12  documentation. The authors of MySQL hereby grant you an additional
13  permission to link the program and your derivative works with the
14  separately licensed software that they have included with MySQL.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License, version 2.0, for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24 
25 /**
26  @file sql_data_change.h
27 
28  Contains classes representing SQL-data change statements. The
29  actual implementions of the functionality are found in files
30  sql_{insert, update}.{h,cc}
31 */
32 
33 #include <assert.h>
34 #include <stddef.h>
35 #include <sys/types.h>
36 
37 #include "my_base.h" // ha_rows
38 #include "my_bitmap.h" // MY_BITMAP
39 
40 class Item;
41 struct TABLE;
42 template <class T>
43 class List;
44 template <class T>
45 class mem_root_deque;
46 
48 
49 /**
50  This class encapsulates a data change operation. There are three such
51  operations.
52 
53  -# Insert statements, i.e. INSERT INTO .. VALUES
54 
55  -# Update statements. UPDATE @<table@> SET ...
56 
57  -# Delete statements. Currently this class is not used for delete statements
58  and thus has not yet been adapted to handle it.
59 
60  @todo Rename this class.
61 
62  The COPY_INFO structure is used by INSERT/REPLACE code.
63  The schema of the row counting by the INSERT/INSERT ... ON DUPLICATE KEY
64  UPDATE code:
65  If a row is inserted then the copied variable is incremented.
66  If a row is updated by the INSERT ... ON DUPLICATE KEY UPDATE and the
67  new data differs from the old one then the copied and the updated
68  variables are incremented.
69  The touched variable is incremented if a row was touched by the update part
70  of the INSERT ... ON DUPLICATE KEY UPDATE no matter whether the row
71  was actually changed or not.
72 */
73 class COPY_INFO {
74  public:
75  class Statistics {
76  public:
78  : records(0),
79  deleted(0),
80  updated(0),
81  copied(0),
82  error_count(0),
83  touched(0) {}
84 
85  ha_rows records; /**< Number of processed records */
86  ha_rows deleted; /**< Number of deleted records */
87  ha_rows updated; /**< Number of updated records */
88  ha_rows copied; /**< Number of copied records */
90  ha_rows touched; /* Number of touched records */
91  };
92 
94 
95  private:
96  COPY_INFO(const COPY_INFO &other); ///< undefined
97  void operator=(COPY_INFO &); ///< undefined
98 
99  /// Describes the data change operation that this object represents.
101 
102  /**
103  List of columns of the target table which the statement will explicitely
104  fill; and thus we must not set a function default for them.
105  NULL means "empty list".
106  */
108 
109  /**
110  A second list of columns like m_changed_columns. See the constructor
111  specific of LOAD DATA INFILE, below.
112  */
114 
115  /** Whether this object must manage function defaults */
116  const bool m_manage_defaults;
117  /** Bitmap: bit is set if we should set column number i to its function
118  * default */
120 
121  /// Policy for handling insertion of duplicate values.
123 
124  protected:
125  /**
126  This function will, unless done already, calculate and keep the set of
127  function default columns.
128 
129  Function default columns are those columns declared DEFAULT @<function@>
130  and/or ON UPDATE @<function@>. These will store the return value of
131  @<function@> when the relevant operation is applied on the table.
132 
133  Calling this function, without error, is a prerequisite for calling
134  COPY_INFO::set_function_defaults().
135 
136  @param table The table to be used for instantiating the column set.
137 
138  @retval false Success.
139  @retval true Memory allocation error.
140  */
141  bool get_function_default_columns(TABLE *table);
142 
143  /**
144  The column bitmap which has been cached for this data change operation.
145  @see COPY_INFO::get_function_default_columns()
146 
147  @return The cached bitmap, or NULL if no bitmap was cached.
148  */
150 
151  public:
154  /** Values for UPDATE; needed by write_record() if INSERT with DUP_UPDATE */
156 
157  /**
158  Initializes this data change operation as an SQL @c INSERT (with all
159  possible syntaxes and variants).
160 
161  @param optype The data change operation type.
162  @param inserted_columns List of columns of the target table which
163  the statement will explicitely fill; COPY_INFO
164  must not set a function default for them. NULL
165  means "empty list".
166  @param manage_defaults Whether this object should manage function
167  defaults.
168  @param duplicate_handling The policy for handling duplicates.
169 
170  */
172  bool manage_defaults, enum_duplicates duplicate_handling)
173  : m_optype(optype),
174  m_changed_columns(inserted_columns),
176  m_manage_defaults(manage_defaults),
178  handle_duplicates(duplicate_handling),
179  stats(),
180  escape_char(0),
181  last_errno(0),
183  assert(optype == INSERT_OPERATION);
184  }
185 
186  /**
187  Initializes this data change operation as an SQL @c LOAD @c DATA @c
188  INFILE.
189  Note that this statement has its inserted columns spread over two
190  lists:
191 @verbatim
192  LOAD DATA INFILE a_file
193  INTO TABLE a_table (col1, col2) < first list (col1, col2)
194  SET col3=val; < second list (col3)
195 @endverbatim
196 
197  @param optype The data change operation type.
198  @param inserted_columns List of columns of the target table which
199  the statement will explicitely fill; COPY_INFO
200  must not set a function default for them. NULL
201  means "empty list".
202  @param inserted_columns2 A second list like inserted_columns
203  @param manage_defaults Whether this object should manage function
204  defaults.
205  @param duplicates_handling How to handle duplicates.
206  @param escape_character The escape character.
207  */
209  mem_root_deque<Item *> *inserted_columns2, bool manage_defaults,
210  enum_duplicates duplicates_handling, int escape_character)
211  : m_optype(optype),
212  m_changed_columns(inserted_columns),
213  m_changed_columns2(inserted_columns2),
214  m_manage_defaults(manage_defaults),
216  handle_duplicates(duplicates_handling),
217  stats(),
219  last_errno(0),
221  assert(optype == INSERT_OPERATION);
222  }
223 
224  /**
225  Initializes this data change operation as an SQL @c UPDATE (multi- or
226  not).
227 
228  @param optype The data change operation type.
229  @param fields The column objects that are to be updated.
230  @param values The values to be assigned to the fields.
231  @note that UPDATE always lists columns, so non-listed columns may need a
232  default thus m_manage_defaults is always true.
233  */
235  mem_root_deque<Item *> *values)
236  : m_optype(optype),
237  m_changed_columns(fields),
239  m_manage_defaults(true),
242  stats(),
243  escape_char(0),
244  last_errno(0),
245  update_values(values) {
246  assert(optype == UPDATE_OPERATION);
247  }
248 
250 
252  return m_changed_columns;
253  }
254 
256  return m_changed_columns2;
257  }
258 
259  bool get_manage_defaults() const { return m_manage_defaults; }
260 
262 
263  /**
264  Assigns function default values to columns of the supplied table.
265 
266  @note COPY_INFO::get_function_default_columns() and
267  COPY_INFO::add_function_default_columns() must be called prior to invoking
268  this function.
269 
270  @param table The table to which columns belong.
271 
272  @note It is assumed that all columns in this COPY_INFO are resolved to the
273  table.
274 
275  @retval false Success.
276  @retval true Some error happened while executing the default expression.
277  my_error has already been called so the calling function
278  only needs to bail out.
279  */
280  [[nodiscard]] bool set_function_defaults(TABLE *table);
281 
282  /**
283  Adds the columns that are bound to receive default values from a function
284  (e.g. CURRENT_TIMESTAMP) to the set columns. Uses lazy instantiation of the
285  set of function default columns.
286 
287  @param table The table on which the operation is performed.
288  @param[out] columns The function default columns are added to this set.
289 
290  @retval false Success.
291  @retval true Memory allocation error during lazy instantiation.
292  */
294  if (get_function_default_columns(table)) return true;
296  return false;
297  }
298 
299  /**
300  True if this operation will set some fields to function default result
301  values when invoked on the table.
302 
303  @note COPY_INFO::add_function_default_columns() must be called prior to
304  invoking this function.
305  */
306  bool function_defaults_apply(const TABLE *) const {
307  assert(m_function_default_columns != nullptr);
309  }
310 
311  /**
312  True if any of the columns set in the bitmap have default functions
313  that may set the column.
314  */
316  assert(m_function_default_columns != nullptr);
318  }
319 
320  /// Reset counters before the next execution
321  void reset_counters() {
322  stats.records = 0;
323  stats.deleted = 0;
324  stats.updated = 0;
325  stats.copied = 0;
326  stats.error_count = 0;
327  stats.touched = 0;
328  }
329 
330  /// Cleanup memory allocated by this object.
331  void cleanup() { m_function_default_columns = nullptr; }
332 
333  /**
334  Tells the object to not manage function defaults for the last 'count'
335  columns of 'table'.
336  @retval false if success
337  */
338  bool ignore_last_columns(TABLE *table, uint count);
339 
340  /**
341  This class allocates its memory in a MEM_ROOT, so there's nothing to
342  delete.
343  */
344  virtual ~COPY_INFO() = default;
345 };
346 
347 #endif // SQL_DATA_CHANGE_INCLUDED
Definition: sql_data_change.h:75
ha_rows deleted
Number of deleted records.
Definition: sql_data_change.h:86
ha_rows records
Number of processed records.
Definition: sql_data_change.h:85
ha_rows copied
Number of copied records.
Definition: sql_data_change.h:88
ha_rows touched
Definition: sql_data_change.h:90
Statistics()
Definition: sql_data_change.h:77
ha_rows error_count
Definition: sql_data_change.h:89
ha_rows updated
Number of updated records.
Definition: sql_data_change.h:87
This class encapsulates a data change operation.
Definition: sql_data_change.h:73
MY_BITMAP * m_function_default_columns
Bitmap: bit is set if we should set column number i to its function default.
Definition: sql_data_change.h:119
COPY_INFO(const COPY_INFO &other)
undefined
Statistics stats
Definition: sql_data_change.h:152
operation_type
Definition: sql_data_change.h:93
@ UPDATE_OPERATION
Definition: sql_data_change.h:93
@ INSERT_OPERATION
Definition: sql_data_change.h:93
enum enum_duplicates handle_duplicates
Policy for handling insertion of duplicate values.
Definition: sql_data_change.h:122
const mem_root_deque< Item * > * get_changed_columns2() const
Definition: sql_data_change.h:255
bool get_function_default_columns(TABLE *table)
This function will, unless done already, calculate and keep the set of function default columns.
Definition: sql_data_change.cc:72
MY_BITMAP * get_cached_bitmap() const
The column bitmap which has been cached for this data change operation.
Definition: sql_data_change.h:149
const operation_type m_optype
Describes the data change operation that this object represents.
Definition: sql_data_change.h:100
COPY_INFO(operation_type optype, mem_root_deque< Item * > *fields, mem_root_deque< Item * > *values)
Initializes this data change operation as an SQL UPDATE (multi- or not).
Definition: sql_data_change.h:234
operation_type get_operation_type() const
Definition: sql_data_change.h:249
const bool m_manage_defaults
Whether this object must manage function defaults.
Definition: sql_data_change.h:116
void reset_counters()
Reset counters before the next execution.
Definition: sql_data_change.h:321
enum_duplicates get_duplicate_handling() const
Definition: sql_data_change.h:261
int escape_char
Definition: sql_data_change.h:153
bool ignore_last_columns(TABLE *table, uint count)
Tells the object to not manage function defaults for the last 'count' columns of 'table'.
Definition: sql_data_change.cc:171
COPY_INFO(operation_type optype, mem_root_deque< Item * > *inserted_columns, mem_root_deque< Item * > *inserted_columns2, bool manage_defaults, enum_duplicates duplicates_handling, int escape_character)
Definition: sql_data_change.h:208
void operator=(COPY_INFO &)
undefined
virtual ~COPY_INFO()=default
This class allocates its memory in a MEM_ROOT, so there's nothing to delete.
mem_root_deque< Item * > * m_changed_columns
List of columns of the target table which the statement will explicitely fill; and thus we must not s...
Definition: sql_data_change.h:107
mem_root_deque< Item * > * m_changed_columns2
A second list of columns like m_changed_columns.
Definition: sql_data_change.h:113
mem_root_deque< Item * > * update_values
Values for UPDATE; needed by write_record() if INSERT with DUP_UPDATE.
Definition: sql_data_change.h:155
COPY_INFO(operation_type optype, mem_root_deque< Item * > *inserted_columns, bool manage_defaults, enum_duplicates duplicate_handling)
Initializes this data change operation as an SQL INSERT (with all possible syntaxes and variants).
Definition: sql_data_change.h:171
int last_errno
Definition: sql_data_change.h:153
bool function_defaults_apply(const TABLE *) const
True if this operation will set some fields to function default result values when invoked on the tab...
Definition: sql_data_change.h:306
mem_root_deque< Item * > * get_changed_columns() const
Definition: sql_data_change.h:251
void cleanup()
Cleanup memory allocated by this object.
Definition: sql_data_change.h:331
bool add_function_default_columns(TABLE *table, MY_BITMAP *columns)
Adds the columns that are bound to receive default values from a function (e.g.
Definition: sql_data_change.h:293
bool function_defaults_apply_on_columns(MY_BITMAP *map)
True if any of the columns set in the bitmap have default functions that may set the column.
Definition: sql_data_change.h:315
bool get_manage_defaults() const
Definition: sql_data_change.h:259
bool set_function_defaults(TABLE *table)
Assigns function default values to columns of the supplied table.
Definition: sql_data_change.cc:131
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:802
Definition: sql_list.h:431
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:109
Dialog Client Authentication nullptr
Definition: dialog.cc:352
static bool escape_character(char c, String *buf)
Escape a special character in a JSON string, as described in double_quote(), and append it to a buffe...
Definition: json_dom.cc:1183
This file includes constants used by all storage engines.
my_off_t ha_rows
Definition: my_base.h:1138
bool bitmap_is_overlapping(const MY_BITMAP *map1, const MY_BITMAP *map2)
Definition: my_bitmap.cc:300
bool bitmap_is_clear_all(const MY_BITMAP *map)
Definition: my_bitmap.cc:267
void bitmap_union(MY_BITMAP *map, const MY_BITMAP *map2)
Definition: my_bitmap.cc:372
static int count
Definition: myisam_ftdump.cc:42
enum_duplicates
Definition: sql_data_change.h:47
@ DUP_REPLACE
Definition: sql_data_change.h:47
@ DUP_UPDATE
Definition: sql_data_change.h:47
@ DUP_ERROR
Definition: sql_data_change.h:47
Definition: my_bitmap.h:41
Definition: table.h:1394
Definition: mysqlslap.cc:234
unsigned int uint
Definition: uca-dump.cc:29