1
#ifndef SQL_DATA_CHANGE_INCLUDED
2
#define SQL_DATA_CHANGE_INCLUDED
3
/* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software Foundation,
16
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
19
@file sql_data_change.h
21
Contains classes representing SQL-data change statements. The
22
actual implementions of the functionality are found in files
23
sql_{insert, update}.{h,cc}
28
#include "my_bitmap.h"
31
enum enum_duplicates { DUP_ERROR, DUP_REPLACE, DUP_UPDATE };
34
This class encapsulates a data change operation. There are three such
37
-# Insert statements, i.e. INSERT INTO .. VALUES
39
-# Update statements. UPDATE <table> SET ...
41
-# Delete statements. Currently this class is not used for delete statements
42
and thus has not yet been adapted to handle it.
44
@todo Rename this class.
46
The COPY_INFO structure is used by INSERT/REPLACE code.
47
The schema of the row counting by the INSERT/INSERT ... ON DUPLICATE KEY
49
If a row is inserted then the copied variable is incremented.
50
If a row is updated by the INSERT ... ON DUPLICATE KEY UPDATE and the
51
new data differs from the old one then the copied and the updated
52
variables are incremented.
53
The touched variable is incremented if a row was touched by the update part
54
of the INSERT ... ON DUPLICATE KEY UPDATE no matter whether the row
55
was actually changed or not.
57
class COPY_INFO: public Sql_alloc
64
records(0), deleted(0), updated(0), copied(0), error_count(0), touched(0)
67
ha_rows records; /**< Number of processed records */
68
ha_rows deleted; /**< Number of deleted records */
69
ha_rows updated; /**< Number of updated records */
70
ha_rows copied; /**< Number of copied records */
72
ha_rows touched; /* Number of touched records */
75
enum operation_type { INSERT_OPERATION, UPDATE_OPERATION };
78
COPY_INFO(const COPY_INFO &other); ///< undefined
79
void operator=(COPY_INFO &); ///< undefined
81
/// Describes the data change operation that this object represents.
82
const operation_type m_optype;
85
List of columns of the target table which the statement will explicitely
86
fill; and thus we must not set a function default for them.
87
NULL means "empty list".
89
List<Item> *m_changed_columns;
92
A second list of columns like m_changed_columns. See the constructor
93
specific of LOAD DATA INFILE, below.
95
List<Item> *m_changed_columns2;
98
/** Whether this object must manage function defaults */
99
const bool m_manage_defaults;
100
/** Bitmap: bit is set if we should set column #i to its function default */
101
MY_BITMAP *m_function_default_columns;
106
Policy for handling insertion of duplicate values. Protected for legacy
109
@see Delayable_insert_operation::set_dup_and_ignore()
111
enum enum_duplicates handle_duplicates;
114
Policy for whether certain errors should be ignored. Protected for legacy
117
@see Delayable_insert_operation::set_dup_and_ignore()
122
This function will, unless done already, calculate and keep the set of
123
function default columns.
125
Function default columns are those columns declared DEFAULT <function>
126
and/or ON UPDATE <function>. These will store the return value of
127
<function> when the relevant operation is applied on the table.
129
Calling this function, without error, is a prerequisite for calling
130
COPY_INFO::set_function_defaults().
132
@param table The table to be used for instantiating the column set.
134
@retval false Success.
135
@retval true Memory allocation error.
137
bool get_function_default_columns(TABLE *table);
140
The column bitmap which has been cached for this data change operation.
141
@see COPY_INFO::get_function_default_columns()
143
@return The cached bitmap, or NULL if no bitmap was cached.
145
MY_BITMAP *get_cached_bitmap() const { return m_function_default_columns; }
149
int escape_char, last_errno;
150
/** Values for UPDATE; needed by write_record() if INSERT with DUP_UPDATE */
151
List<Item> *update_values;
154
Initializes this data change operation as an SQL @c INSERT (with all
155
possible syntaxes and variants).
157
@param optype The data change operation type.
158
@param inserted_columns List of columns of the target table which
159
the statement will explicitely fill; COPY_INFO
160
must not set a function default for them. NULL
162
@param manage_defaults Whether this object should manage function
164
@param duplicate_handling The policy for handling duplicates.
165
@param ignore_errors Whether certain ignorable errors should be
166
ignored. A proper documentation has never existed
167
for this member, so the following has been
168
compiled by examining how clients actually use
171
- Ignore non-fatal errors, except duplicate key error, during this insert
172
operation (this constructor can only construct an insert operation).
173
- If the insert operation spawns an update operation (as in ON DUPLICATE
174
KEY UPDATE), tell the layer below
175
(fill_record_n_invoke_before_triggers) to 'ignore errors'. (More
176
detailed documentation is not available).
177
- Let @i v be a view for which WITH CHECK OPTION applies. This can happen
178
either if @i v is defined with WITH ... CHECK OPTION, or if @i v is
179
being inserted into by a cascaded insert and an outer view is defined
180
with "WITH CASCADED CHECK OPTION".
181
If the insert operation on @i v spawns an update operation (as in ON
182
DUPLICATE KEY UPDATE) for a certain row, and hence the @i v is being
183
updated, ignore whether the WHERE clause was true for this row or
184
not. I.e. if ignore is true, WITH CHECK OPTION can be ignored.
185
- If the insert operation spawns an update operation (as in ON DUPLICATE
186
KEY UPDATE) that fails, ignore this error.
188
COPY_INFO(operation_type optype,
189
List<Item> *inserted_columns,
190
bool manage_defaults,
191
enum_duplicates duplicate_handling,
192
bool ignore_errors) :
194
m_changed_columns(inserted_columns),
195
m_changed_columns2(NULL),
196
m_manage_defaults(manage_defaults),
197
m_function_default_columns(NULL),
198
handle_duplicates(duplicate_handling),
199
ignore(ignore_errors),
205
DBUG_ASSERT(optype == INSERT_OPERATION);
209
Initializes this data change operation as an SQL @c LOAD @c DATA @c
211
Note that this statement has its inserted columns spread over two
214
LOAD DATA INFILE a_file
215
INTO TABLE a_table (col1, col2) < first list (col1, col2)
216
SET col3=val; < second list (col3)
219
@param optype The data change operation type.
220
@param inserted_columns List of columns of the target table which
221
the statement will explicitely fill; COPY_INFO
222
must not set a function default for them. NULL
224
@param inserted_columns2 A second list like inserted_columns
225
@param manage_defaults Whether this object should manage function
227
@param ignore_duplicates Whether duplicate rows are ignored.
228
@param duplicates_handling How to handle duplicates.
229
@param escape_character The escape character.
231
COPY_INFO(operation_type optype,
232
List<Item> *inserted_columns,
233
List<Item> *inserted_columns2,
234
bool manage_defaults,
235
enum_duplicates duplicates_handling,
236
bool ignore_duplicates,
237
int escape_character) :
239
m_changed_columns(inserted_columns),
240
m_changed_columns2(inserted_columns2),
241
m_manage_defaults(manage_defaults),
242
m_function_default_columns(NULL),
243
handle_duplicates(duplicates_handling),
244
ignore(ignore_duplicates),
246
escape_char(escape_character),
250
DBUG_ASSERT(optype == INSERT_OPERATION);
254
Initializes this data change operation as an SQL @c UPDATE (multi- or
257
@param fields The column objects that are to be updated.
258
@param values The values to be assigned to the fields.
259
@note that UPDATE always lists columns, so non-listed columns may need a
260
default thus m_manage_defaults is always true.
262
COPY_INFO(operation_type optype, List<Item> *fields, List<Item> *values) :
264
m_changed_columns(fields),
265
m_changed_columns2(NULL),
266
m_manage_defaults(true),
267
m_function_default_columns(NULL),
268
handle_duplicates(DUP_ERROR),
273
update_values(values)
275
DBUG_ASSERT(optype == UPDATE_OPERATION);
278
operation_type get_operation_type() const { return m_optype; }
280
List<Item> *get_changed_columns() const { return m_changed_columns; }
282
const List<Item> *get_changed_columns2() const { return m_changed_columns2; }
284
bool get_manage_defaults() const { return m_manage_defaults; }
286
enum_duplicates get_duplicate_handling() const { return handle_duplicates; }
288
bool get_ignore_errors() const { return ignore; }
291
Assigns function default values to columns of the supplied table. This
292
function cannot fail, but COPY_INFO::get_function_default_columns() must
293
be called beforehand.
295
@note COPY_INFO::add_function_default_columns() must be called prior to
296
invoking this function.
298
@param table The table to which columns belong.
300
@note It is assumed that all columns in this COPY_INFO are resolved to the
303
virtual void set_function_defaults(TABLE *table);
306
Adds the columns that are bound to receive default values from a function
307
(e.g. CURRENT_TIMESTAMP) to the set columns. Uses lazy instantiation of the set
308
of function default columns.
310
@param table The table on which the operation is performed.
311
@param[out] columns The function default columns are added to this set.
313
@retval false Success.
314
@retval true Memory allocation error during lazy instantiation.
316
bool add_function_default_columns(TABLE *table, MY_BITMAP *columns)
318
if (get_function_default_columns(table))
320
bitmap_union(columns, m_function_default_columns);
325
True if this operation will set some fields to function default result
326
values when invoked on the table.
328
@note COPY_INFO::add_function_default_columns() must be called prior to
329
invoking this function.
331
bool function_defaults_apply(const TABLE *table) const
333
DBUG_ASSERT(m_function_default_columns != NULL);
334
return !bitmap_is_clear_all(m_function_default_columns);
338
True if any of the columns set in the bitmap have default functions
339
that may set the column.
341
bool function_defaults_apply_on_columns(MY_BITMAP *map)
343
DBUG_ASSERT(m_function_default_columns != NULL);
344
return bitmap_is_overlapping(m_function_default_columns, map);
348
Tells the object to not manage function defaults for the last 'count'
350
@retval false if success
352
bool ignore_last_columns(TABLE *table, uint count);
355
This class allocates its memory in a MEM_ROOT, so there's nothing to
358
virtual ~COPY_INFO() {}
362
#endif // SQL_DATA_CHANGE_INCLUDED