~ubuntu-branches/ubuntu/trusty/mysql-5.6/trusty

« back to all changes in this revision

Viewing changes to sql/sql_data_change.h

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-12 11:54:27 UTC
  • Revision ID: package-import@ubuntu.com-20140212115427-oq6tfsqxl1wuwehi
Tags: upstream-5.6.15
ImportĀ upstreamĀ versionĀ 5.6.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
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 as published by
 
7
   the Free Software Foundation; version 2 of the License.
 
8
 
 
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.
 
13
 
 
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 */
 
17
 
 
18
/**
 
19
  @file sql_data_change.h
 
20
 
 
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} 
 
24
*/
 
25
 
 
26
#include "sql_list.h"
 
27
#include "my_base.h"
 
28
#include "my_bitmap.h"
 
29
#include "table.h"
 
30
 
 
31
enum enum_duplicates { DUP_ERROR, DUP_REPLACE, DUP_UPDATE };
 
32
 
 
33
/**
 
34
   This class encapsulates a data change operation. There are three such
 
35
   operations.
 
36
 
 
37
   -# Insert statements, i.e. INSERT INTO .. VALUES
 
38
 
 
39
   -# Update statements. UPDATE <table> SET ...
 
40
 
 
41
   -# Delete statements. Currently this class is not used for delete statements
 
42
      and thus has not yet been adapted to handle it.
 
43
 
 
44
   @todo Rename this class.
 
45
 
 
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
 
48
  UPDATE code:
 
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.
 
56
*/
 
57
class COPY_INFO: public Sql_alloc
 
58
{
 
59
public:
 
60
  class Statistics
 
61
  {
 
62
  public:
 
63
    Statistics() :
 
64
      records(0), deleted(0), updated(0), copied(0), error_count(0), touched(0)
 
65
    {}
 
66
 
 
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 */
 
71
    ha_rows error_count;
 
72
    ha_rows touched; /* Number of touched records */
 
73
  };
 
74
 
 
75
  enum operation_type { INSERT_OPERATION, UPDATE_OPERATION };
 
76
 
 
77
private:
 
78
  COPY_INFO(const COPY_INFO &other);            ///< undefined
 
79
  void operator=(COPY_INFO &);                  ///< undefined
 
80
 
 
81
  /// Describes the data change operation that this object represents.
 
82
  const operation_type m_optype;
 
83
 
 
84
  /**
 
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".
 
88
  */
 
89
  List<Item> *m_changed_columns;
 
90
 
 
91
  /**
 
92
     A second list of columns like m_changed_columns. See the constructor
 
93
     specific of LOAD DATA INFILE, below.
 
94
  */
 
95
  List<Item> *m_changed_columns2;
 
96
 
 
97
 
 
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;
 
102
 
 
103
protected:
 
104
 
 
105
  /**
 
106
     Policy for handling insertion of duplicate values. Protected for legacy
 
107
     reasons.
 
108
 
 
109
     @see Delayable_insert_operation::set_dup_and_ignore()
 
110
  */
 
111
  enum enum_duplicates handle_duplicates;
 
112
 
 
113
  /**
 
114
     Policy for whether certain errors should be ignored. Protected for legacy
 
115
     reasons.
 
116
 
 
117
     @see Delayable_insert_operation::set_dup_and_ignore()
 
118
  */
 
119
  bool ignore;
 
120
 
 
121
  /**
 
122
     This function will, unless done already, calculate and keep the set of
 
123
     function default columns.
 
124
 
 
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.
 
128
 
 
129
     Calling this function, without error, is a prerequisite for calling
 
130
     COPY_INFO::set_function_defaults().
 
131
 
 
132
     @param table The table to be used for instantiating the column set.
 
133
 
 
134
     @retval false Success.
 
135
     @retval true Memory allocation error.
 
136
  */
 
137
  bool get_function_default_columns(TABLE *table);
 
138
 
 
139
  /**
 
140
     The column bitmap which has been cached for this data change operation.
 
141
     @see COPY_INFO::get_function_default_columns()
 
142
 
 
143
     @return The cached bitmap, or NULL if no bitmap was cached.
 
144
   */
 
145
  MY_BITMAP *get_cached_bitmap() const { return m_function_default_columns; }
 
146
 
 
147
public:
 
148
  Statistics stats;
 
149
  int escape_char, last_errno;
 
150
  /** Values for UPDATE; needed by write_record() if INSERT with DUP_UPDATE */
 
151
  List<Item> *update_values;
 
152
 
 
153
  /**
 
154
     Initializes this data change operation as an SQL @c INSERT (with all
 
155
     possible syntaxes and variants).
 
156
 
 
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
 
161
                             means "empty list".
 
162
     @param manage_defaults  Whether this object should manage function
 
163
                             defaults.
 
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
 
169
                             the member.
 
170
 
 
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.
 
187
  */
 
188
  COPY_INFO(operation_type optype,
 
189
            List<Item> *inserted_columns,
 
190
            bool manage_defaults,
 
191
            enum_duplicates duplicate_handling,
 
192
            bool ignore_errors) :
 
193
    m_optype(optype),
 
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),
 
200
    stats(),
 
201
    escape_char(0),
 
202
    last_errno(0),
 
203
    update_values(NULL)
 
204
  {
 
205
    DBUG_ASSERT(optype == INSERT_OPERATION);
 
206
  }
 
207
 
 
208
  /**
 
209
     Initializes this data change operation as an SQL @c LOAD @c DATA @c
 
210
     INFILE.
 
211
     Note that this statement has its inserted columns spread over two
 
212
     lists:
 
213
@verbatim
 
214
     LOAD DATA INFILE a_file
 
215
     INTO TABLE a_table (col1, col2)   < first list (col1, col2)
 
216
     SET col3=val;                     < second list (col3)
 
217
@endverbatim
 
218
 
 
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
 
223
                             means "empty list".
 
224
     @param inserted_columns2 A second list like inserted_columns
 
225
     @param manage_defaults   Whether this object should manage function
 
226
                              defaults.
 
227
     @param ignore_duplicates   Whether duplicate rows are ignored.
 
228
     @param duplicates_handling How to handle duplicates.
 
229
     @param escape_character    The escape character.
 
230
  */
 
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) :
 
238
    m_optype(optype),
 
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),
 
245
    stats(),
 
246
    escape_char(escape_character),
 
247
    last_errno(0),
 
248
    update_values(NULL)
 
249
  {
 
250
    DBUG_ASSERT(optype == INSERT_OPERATION);
 
251
  }
 
252
 
 
253
  /**
 
254
     Initializes this data change operation as an SQL @c UPDATE (multi- or
 
255
     not).
 
256
 
 
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.
 
261
  */
 
262
  COPY_INFO(operation_type optype, List<Item> *fields, List<Item> *values) :
 
263
    m_optype(optype),
 
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),
 
269
    ignore(false),
 
270
    stats(),
 
271
    escape_char(0),
 
272
    last_errno(0),
 
273
    update_values(values)
 
274
  {
 
275
    DBUG_ASSERT(optype == UPDATE_OPERATION);
 
276
  }
 
277
 
 
278
  operation_type get_operation_type() const { return m_optype; }
 
279
 
 
280
  List<Item> *get_changed_columns() const { return m_changed_columns; }
 
281
 
 
282
  const List<Item> *get_changed_columns2() const { return m_changed_columns2; }
 
283
 
 
284
  bool get_manage_defaults() const { return m_manage_defaults; }
 
285
 
 
286
  enum_duplicates get_duplicate_handling() const { return handle_duplicates; }
 
287
 
 
288
  bool get_ignore_errors() const { return ignore; }
 
289
 
 
290
  /**
 
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.
 
294
 
 
295
     @note COPY_INFO::add_function_default_columns() must be called prior to
 
296
     invoking this function.
 
297
 
 
298
     @param table  The table to which columns belong.
 
299
 
 
300
     @note It is assumed that all columns in this COPY_INFO are resolved to the
 
301
     table.
 
302
  */
 
303
  virtual void set_function_defaults(TABLE *table);
 
304
 
 
305
  /**
 
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.
 
309
 
 
310
     @param      table    The table on which the operation is performed.
 
311
     @param[out] columns  The function default columns are added to this set.
 
312
 
 
313
     @retval false Success.
 
314
     @retval true Memory allocation error during lazy instantiation.
 
315
  */
 
316
  bool add_function_default_columns(TABLE *table, MY_BITMAP *columns)
 
317
  {
 
318
    if (get_function_default_columns(table))
 
319
      return true;
 
320
    bitmap_union(columns, m_function_default_columns);
 
321
    return false;
 
322
  }
 
323
 
 
324
  /**
 
325
     True if this operation will set some fields to function default result
 
326
     values when invoked on the table.
 
327
 
 
328
     @note COPY_INFO::add_function_default_columns() must be called prior to
 
329
     invoking this function.
 
330
  */
 
331
  bool function_defaults_apply(const TABLE *table) const
 
332
  {
 
333
    DBUG_ASSERT(m_function_default_columns != NULL);
 
334
    return !bitmap_is_clear_all(m_function_default_columns);
 
335
  }
 
336
 
 
337
  /**
 
338
    True if any of the columns set in the bitmap have default functions
 
339
    that may set the column.
 
340
  */
 
341
  bool function_defaults_apply_on_columns(MY_BITMAP *map)
 
342
  {
 
343
    DBUG_ASSERT(m_function_default_columns != NULL);
 
344
    return bitmap_is_overlapping(m_function_default_columns, map);
 
345
  }
 
346
 
 
347
  /**
 
348
     Tells the object to not manage function defaults for the last 'count'
 
349
     columns of 'table'.
 
350
     @retval false if success
 
351
  */
 
352
  bool ignore_last_columns(TABLE *table, uint count);
 
353
 
 
354
  /**
 
355
     This class allocates its memory in a MEM_ROOT, so there's nothing to
 
356
     delete.
 
357
  */
 
358
  virtual ~COPY_INFO() {}
 
359
};
 
360
 
 
361
 
 
362
#endif // SQL_DATA_CHANGE_INCLUDED