MySQL  8.0.18
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, 2018, Oracle and/or its affiliates. All rights reserved.
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 <stddef.h>
34 #include <sys/types.h>
35 
36 #include "my_base.h" // ha_rows
37 #include "my_bitmap.h" // MY_BITMAP
38 #include "my_dbug.h"
39 
40 class Item;
41 struct TABLE;
42 template <class T>
43 class List;
44 
46 
47 /**
48  This class encapsulates a data change operation. There are three such
49  operations.
50 
51  -# Insert statements, i.e. INSERT INTO .. VALUES
52 
53  -# Update statements. UPDATE @<table@> SET ...
54 
55  -# Delete statements. Currently this class is not used for delete statements
56  and thus has not yet been adapted to handle it.
57 
58  @todo Rename this class.
59 
60  The COPY_INFO structure is used by INSERT/REPLACE code.
61  The schema of the row counting by the INSERT/INSERT ... ON DUPLICATE KEY
62  UPDATE code:
63  If a row is inserted then the copied variable is incremented.
64  If a row is updated by the INSERT ... ON DUPLICATE KEY UPDATE and the
65  new data differs from the old one then the copied and the updated
66  variables are incremented.
67  The touched variable is incremented if a row was touched by the update part
68  of the INSERT ... ON DUPLICATE KEY UPDATE no matter whether the row
69  was actually changed or not.
70 */
71 class COPY_INFO {
72  public:
73  class Statistics {
74  public:
76  : records(0),
77  deleted(0),
78  updated(0),
79  copied(0),
80  error_count(0),
81  touched(0) {}
82 
83  ha_rows records; /**< Number of processed records */
84  ha_rows deleted; /**< Number of deleted records */
85  ha_rows updated; /**< Number of updated records */
86  ha_rows copied; /**< Number of copied records */
88  ha_rows touched; /* Number of touched records */
89  };
90 
92 
93  private:
94  COPY_INFO(const COPY_INFO &other); ///< undefined
95  void operator=(COPY_INFO &); ///< undefined
96 
97  /// Describes the data change operation that this object represents.
99 
100  /**
101  List of columns of the target table which the statement will explicitely
102  fill; and thus we must not set a function default for them.
103  NULL means "empty list".
104  */
106 
107  /**
108  A second list of columns like m_changed_columns. See the constructor
109  specific of LOAD DATA INFILE, below.
110  */
112 
113  /** Whether this object must manage function defaults */
114  const bool m_manage_defaults;
115  /** Bitmap: bit is set if we should set column number i to its function
116  * default */
118 
119  /// Policy for handling insertion of duplicate values.
121 
122  protected:
123  /**
124  This function will, unless done already, calculate and keep the set of
125  function default columns.
126 
127  Function default columns are those columns declared DEFAULT @<function@>
128  and/or ON UPDATE @<function@>. These will store the return value of
129  @<function@> when the relevant operation is applied on the table.
130 
131  Calling this function, without error, is a prerequisite for calling
132  COPY_INFO::set_function_defaults().
133 
134  @param table The table to be used for instantiating the column set.
135 
136  @retval false Success.
137  @retval true Memory allocation error.
138  */
139  bool get_function_default_columns(TABLE *table);
140 
141  /**
142  The column bitmap which has been cached for this data change operation.
143  @see COPY_INFO::get_function_default_columns()
144 
145  @return The cached bitmap, or NULL if no bitmap was cached.
146  */
148 
149  public:
152  /** Values for UPDATE; needed by write_record() if INSERT with DUP_UPDATE */
154 
155  /**
156  Initializes this data change operation as an SQL @c INSERT (with all
157  possible syntaxes and variants).
158 
159  @param optype The data change operation type.
160  @param inserted_columns List of columns of the target table which
161  the statement will explicitely fill; COPY_INFO
162  must not set a function default for them. NULL
163  means "empty list".
164  @param manage_defaults Whether this object should manage function
165  defaults.
166  @param duplicate_handling The policy for handling duplicates.
167 
168  */
169  COPY_INFO(operation_type optype, List<Item> *inserted_columns,
170  bool manage_defaults, enum_duplicates duplicate_handling)
171  : m_optype(optype),
172  m_changed_columns(inserted_columns),
174  m_manage_defaults(manage_defaults),
176  handle_duplicates(duplicate_handling),
177  stats(),
178  escape_char(0),
179  last_errno(0),
181  DBUG_ASSERT(optype == INSERT_OPERATION);
182  }
183 
184  /**
185  Initializes this data change operation as an SQL @c LOAD @c DATA @c
186  INFILE.
187  Note that this statement has its inserted columns spread over two
188  lists:
189 @verbatim
190  LOAD DATA INFILE a_file
191  INTO TABLE a_table (col1, col2) < first list (col1, col2)
192  SET col3=val; < second list (col3)
193 @endverbatim
194 
195  @param optype The data change operation type.
196  @param inserted_columns List of columns of the target table which
197  the statement will explicitely fill; COPY_INFO
198  must not set a function default for them. NULL
199  means "empty list".
200  @param inserted_columns2 A second list like inserted_columns
201  @param manage_defaults Whether this object should manage function
202  defaults.
203  @param duplicates_handling How to handle duplicates.
204  @param escape_character The escape character.
205  */
206  COPY_INFO(operation_type optype, List<Item> *inserted_columns,
207  List<Item> *inserted_columns2, bool manage_defaults,
208  enum_duplicates duplicates_handling, int escape_character)
209  : m_optype(optype),
210  m_changed_columns(inserted_columns),
211  m_changed_columns2(inserted_columns2),
212  m_manage_defaults(manage_defaults),
214  handle_duplicates(duplicates_handling),
215  stats(),
217  last_errno(0),
219  DBUG_ASSERT(optype == INSERT_OPERATION);
220  }
221 
222  /**
223  Initializes this data change operation as an SQL @c UPDATE (multi- or
224  not).
225 
226  @param optype The data change operation type.
227  @param fields The column objects that are to be updated.
228  @param values The values to be assigned to the fields.
229  @note that UPDATE always lists columns, so non-listed columns may need a
230  default thus m_manage_defaults is always true.
231  */
232  COPY_INFO(operation_type optype, List<Item> *fields, List<Item> *values)
233  : m_optype(optype),
234  m_changed_columns(fields),
239  stats(),
240  escape_char(0),
241  last_errno(0),
242  update_values(values) {
243  DBUG_ASSERT(optype == UPDATE_OPERATION);
244  }
245 
247 
249 
251 
252  bool get_manage_defaults() const { return m_manage_defaults; }
253 
255 
256  /**
257  Assigns function default values to columns of the supplied table.
258 
259  @note COPY_INFO::get_function_default_columns() and
260  COPY_INFO::add_function_default_columns() must be called prior to invoking
261  this function.
262 
263  @param table The table to which columns belong.
264 
265  @note It is assumed that all columns in this COPY_INFO are resolved to the
266  table.
267 
268  @retval false Success.
269  @retval true Some error happened while executing the default expression.
270  my_error has already been called so the calling function
271  only needs to bail out.
272  */
273  bool set_function_defaults(TABLE *table);
274 
275  /**
276  Adds the columns that are bound to receive default values from a function
277  (e.g. CURRENT_TIMESTAMP) to the set columns. Uses lazy instantiation of the
278  set of function default columns.
279 
280  @param table The table on which the operation is performed.
281  @param[out] columns The function default columns are added to this set.
282 
283  @retval false Success.
284  @retval true Memory allocation error during lazy instantiation.
285  */
287  if (get_function_default_columns(table)) return true;
289  return false;
290  }
291 
292  /**
293  True if this operation will set some fields to function default result
294  values when invoked on the table.
295 
296  @note COPY_INFO::add_function_default_columns() must be called prior to
297  invoking this function.
298  */
299  bool function_defaults_apply(const TABLE *) const {
302  }
303 
304  /**
305  True if any of the columns set in the bitmap have default functions
306  that may set the column.
307  */
311  }
312 
313  /**
314  Tells the object to not manage function defaults for the last 'count'
315  columns of 'table'.
316  @retval false if success
317  */
318  bool ignore_last_columns(TABLE *table, uint count);
319 
320  /**
321  This class allocates its memory in a MEM_ROOT, so there's nothing to
322  delete.
323  */
324  virtual ~COPY_INFO() {}
325 };
326 
327 #endif // SQL_DATA_CHANGE_INCLUDED
operation_type get_operation_type() const
Definition: sql_data_change.h:246
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:117
List< Item > * get_changed_columns() const
Definition: sql_data_change.h:248
ssize_t count
Definition: memcached.c:386
void bitmap_union(MY_BITMAP *map, const MY_BITMAP *map2)
Definition: my_bitmap.cc:455
ha_rows updated
Number of updated records.
Definition: sql_data_change.h:85
ha_rows copied
Number of copied records.
Definition: sql_data_change.h:86
enum enum_duplicates handle_duplicates
Policy for handling insertion of duplicate values.
Definition: sql_data_change.h:120
enum_duplicates
Definition: sql_data_change.h:45
bool set_function_defaults(TABLE *table)
Assigns function default values to columns of the supplied table.
Definition: sql_data_change.cc:132
enum_duplicates get_duplicate_handling() const
Definition: sql_data_change.h:254
COPY_INFO(operation_type optype, List< Item > *fields, List< Item > *values)
Initializes this data change operation as an SQL UPDATE (multi- or not).
Definition: sql_data_change.h:232
Statistics()
Definition: sql_data_change.h:75
List< Item > * update_values
Values for UPDATE; needed by write_record() if INSERT with DUP_UPDATE.
Definition: sql_data_change.h:153
ha_rows deleted
Number of deleted records.
Definition: sql_data_change.h:84
int last_errno
Definition: sql_data_change.h:151
const operation_type m_optype
Describes the data change operation that this object represents.
Definition: sql_data_change.h:98
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:299
const bool m_manage_defaults
Whether this object must manage function defaults.
Definition: sql_data_change.h:114
COPY_INFO(const COPY_INFO &other)
undefined
Statistics stats
Definition: sql_data_change.h:150
COPY_INFO(operation_type optype, List< 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:169
ha_rows records
Number of processed records.
Definition: sql_data_change.h:83
Definition: table.h:1301
This file includes constants used by all storage engines.
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
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:73
const List< Item > * get_changed_columns2() const
Definition: sql_data_change.h:250
operation_type
Definition: sql_data_change.h:91
Definition: sql_data_change.h:45
Definition: sql_data_change.h:91
Definition: aggregate_check.h:523
Definition: my_bitmap.h:42
ha_rows error_count
Definition: sql_data_change.h:87
void operator=(COPY_INFO &)
undefined
Definition: item.h:668
unsigned int uint
Definition: uca-dump.cc:29
#define true
Definition: config_static.h:44
Definition: sql_data_change.h:91
Definition: sql_data_change.h:45
Definition: sql_data_change.h:73
List< 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:105
ha_rows touched
Definition: sql_data_change.h:88
MY_BITMAP * get_cached_bitmap() const
The column bitmap which has been cached for this data change operation.
Definition: sql_data_change.h:147
This class encapsulates a data change operation.
Definition: sql_data_change.h:71
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:308
Global stats.
Definition: mysqlslap.cc:232
List< Item > * m_changed_columns2
A second list of columns like m_changed_columns.
Definition: sql_data_change.h:111
virtual ~COPY_INFO()
This class allocates its memory in a MEM_ROOT, so there&#39;s nothing to delete.
Definition: sql_data_change.h:324
#define NULL
Definition: types.h:55
int escape_char
Definition: sql_data_change.h:151
bool bitmap_is_overlapping(const MY_BITMAP *map1, const MY_BITMAP *map2)
Definition: my_bitmap.cc:383
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:1199
COPY_INFO(operation_type optype, List< Item > *inserted_columns, List< Item > *inserted_columns2, bool manage_defaults, enum_duplicates duplicates_handling, int escape_character)
Initializes this data change operation as an SQL LOAD DATA INFILE.
Definition: sql_data_change.h:206
bool ignore_last_columns(TABLE *table, uint count)
Tells the object to not manage function defaults for the last &#39;count&#39; columns of &#39;table&#39;.
Definition: sql_data_change.cc:172
bool get_manage_defaults() const
Definition: sql_data_change.h:252
bool bitmap_is_clear_all(const MY_BITMAP *map)
Definition: my_bitmap.cc:350
my_off_t ha_rows
Definition: my_base.h:1130
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:286
Definition: sql_data_change.h:45