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