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, 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 Foundation, 23 51 Franklin Street, Suite 500, Boston, MA 02110-1335 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 "sql_list.h" 34 #include "my_base.h" 35 #include "my_bitmap.h" 36 #include "table.h" 37 38 enum enum_duplicates { DUP_ERROR, DUP_REPLACE, DUP_UPDATE }; 39 40 /** 41 This class encapsulates a data change operation. There are three such 42 operations. 43 44 -# Insert statements, i.e. INSERT INTO .. VALUES 45 46 -# Update statements. UPDATE <table> SET ... 47 48 -# Delete statements. Currently this class is not used for delete statements 49 and thus has not yet been adapted to handle it. 50 51 @todo Rename this class. 52 53 The COPY_INFO structure is used by INSERT/REPLACE code. 54 The schema of the row counting by the INSERT/INSERT ... ON DUPLICATE KEY 55 UPDATE code: 56 If a row is inserted then the copied variable is incremented. 57 If a row is updated by the INSERT ... ON DUPLICATE KEY UPDATE and the 58 new data differs from the old one then the copied and the updated 59 variables are incremented. 60 The touched variable is incremented if a row was touched by the update part 61 of the INSERT ... ON DUPLICATE KEY UPDATE no matter whether the row 62 was actually changed or not. 63 */ 64 class COPY_INFO: public Sql_alloc 65 { 66 public: 67 class Statistics 68 { 69 public: Statistics()70 Statistics() : 71 records(0), deleted(0), updated(0), copied(0), error_count(0), touched(0) 72 {} 73 74 ha_rows records; /**< Number of processed records */ 75 ha_rows deleted; /**< Number of deleted records */ 76 ha_rows updated; /**< Number of updated records */ 77 ha_rows copied; /**< Number of copied records */ 78 ha_rows error_count; 79 ha_rows touched; /* Number of touched records */ 80 }; 81 82 enum operation_type { INSERT_OPERATION, UPDATE_OPERATION }; 83 84 private: 85 COPY_INFO(const COPY_INFO &other); ///< undefined 86 void operator=(COPY_INFO &); ///< undefined 87 88 /// Describes the data change operation that this object represents. 89 const operation_type m_optype; 90 91 /** 92 List of columns of the target table which the statement will explicitely 93 fill; and thus we must not set a function default for them. 94 NULL means "empty list". 95 */ 96 List<Item> *m_changed_columns; 97 98 /** 99 A second list of columns like m_changed_columns. See the constructor 100 specific of LOAD DATA INFILE, below. 101 */ 102 List<Item> *m_changed_columns2; 103 104 105 /** Whether this object must manage function defaults */ 106 const bool m_manage_defaults; 107 /** Bitmap: bit is set if we should set column #i to its function default */ 108 MY_BITMAP *m_function_default_columns; 109 110 protected: 111 112 /** 113 Policy for handling insertion of duplicate values. Protected for legacy 114 reasons. 115 116 @see Delayable_insert_operation::set_dup_and_ignore() 117 */ 118 enum enum_duplicates handle_duplicates; 119 120 /** 121 Policy for whether certain errors should be ignored. Protected for legacy 122 reasons. 123 124 @see Delayable_insert_operation::set_dup_and_ignore() 125 */ 126 bool ignore; 127 128 /** 129 This function will, unless done already, calculate and keep the set of 130 function default columns. 131 132 Function default columns are those columns declared DEFAULT <function> 133 and/or ON UPDATE <function>. These will store the return value of 134 <function> when the relevant operation is applied on the table. 135 136 Calling this function, without error, is a prerequisite for calling 137 COPY_INFO::set_function_defaults(). 138 139 @param table The table to be used for instantiating the column set. 140 141 @retval false Success. 142 @retval true Memory allocation error. 143 */ 144 bool get_function_default_columns(TABLE *table); 145 146 /** 147 The column bitmap which has been cached for this data change operation. 148 @see COPY_INFO::get_function_default_columns() 149 150 @return The cached bitmap, or NULL if no bitmap was cached. 151 */ get_cached_bitmap()152 MY_BITMAP *get_cached_bitmap() const { return m_function_default_columns; } 153 154 public: 155 Statistics stats; 156 int escape_char, last_errno; 157 /** Values for UPDATE; needed by write_record() if INSERT with DUP_UPDATE */ 158 List<Item> *update_values; 159 160 /** 161 Initializes this data change operation as an SQL @c INSERT (with all 162 possible syntaxes and variants). 163 164 @param optype The data change operation type. 165 @param inserted_columns List of columns of the target table which 166 the statement will explicitely fill; COPY_INFO 167 must not set a function default for them. NULL 168 means "empty list". 169 @param manage_defaults Whether this object should manage function 170 defaults. 171 @param duplicate_handling The policy for handling duplicates. 172 @param ignore_errors Whether certain ignorable errors should be 173 ignored. A proper documentation has never existed 174 for this member, so the following has been 175 compiled by examining how clients actually use 176 the member. 177 178 - Ignore non-fatal errors, except duplicate key error, during this insert 179 operation (this constructor can only construct an insert operation). 180 - If the insert operation spawns an update operation (as in ON DUPLICATE 181 KEY UPDATE), tell the layer below 182 (fill_record_n_invoke_before_triggers) to 'ignore errors'. (More 183 detailed documentation is not available). 184 - Let @i v be a view for which WITH CHECK OPTION applies. This can happen 185 either if @i v is defined with WITH ... CHECK OPTION, or if @i v is 186 being inserted into by a cascaded insert and an outer view is defined 187 with "WITH CASCADED CHECK OPTION". 188 If the insert operation on @i v spawns an update operation (as in ON 189 DUPLICATE KEY UPDATE) for a certain row, and hence the @i v is being 190 updated, ignore whether the WHERE clause was true for this row or 191 not. I.e. if ignore is true, WITH CHECK OPTION can be ignored. 192 - If the insert operation spawns an update operation (as in ON DUPLICATE 193 KEY UPDATE) that fails, ignore this error. 194 */ COPY_INFO(operation_type optype,List<Item> * inserted_columns,bool manage_defaults,enum_duplicates duplicate_handling,bool ignore_errors)195 COPY_INFO(operation_type optype, 196 List<Item> *inserted_columns, 197 bool manage_defaults, 198 enum_duplicates duplicate_handling, 199 bool ignore_errors) : 200 m_optype(optype), 201 m_changed_columns(inserted_columns), 202 m_changed_columns2(NULL), 203 m_manage_defaults(manage_defaults), 204 m_function_default_columns(NULL), 205 handle_duplicates(duplicate_handling), 206 ignore(ignore_errors), 207 stats(), 208 escape_char(0), 209 last_errno(0), 210 update_values(NULL) 211 { 212 DBUG_ASSERT(optype == INSERT_OPERATION); 213 } 214 215 /** 216 Initializes this data change operation as an SQL @c LOAD @c DATA @c 217 INFILE. 218 Note that this statement has its inserted columns spread over two 219 lists: 220 @verbatim 221 LOAD DATA INFILE a_file 222 INTO TABLE a_table (col1, col2) < first list (col1, col2) 223 SET col3=val; < second list (col3) 224 @endverbatim 225 226 @param optype The data change operation type. 227 @param inserted_columns List of columns of the target table which 228 the statement will explicitely fill; COPY_INFO 229 must not set a function default for them. NULL 230 means "empty list". 231 @param inserted_columns2 A second list like inserted_columns 232 @param manage_defaults Whether this object should manage function 233 defaults. 234 @param ignore_duplicates Whether duplicate rows are ignored. 235 @param duplicates_handling How to handle duplicates. 236 @param escape_character The escape character. 237 */ COPY_INFO(operation_type optype,List<Item> * inserted_columns,List<Item> * inserted_columns2,bool manage_defaults,enum_duplicates duplicates_handling,bool ignore_duplicates,int escape_character)238 COPY_INFO(operation_type optype, 239 List<Item> *inserted_columns, 240 List<Item> *inserted_columns2, 241 bool manage_defaults, 242 enum_duplicates duplicates_handling, 243 bool ignore_duplicates, 244 int escape_character) : 245 m_optype(optype), 246 m_changed_columns(inserted_columns), 247 m_changed_columns2(inserted_columns2), 248 m_manage_defaults(manage_defaults), 249 m_function_default_columns(NULL), 250 handle_duplicates(duplicates_handling), 251 ignore(ignore_duplicates), 252 stats(), 253 escape_char(escape_character), 254 last_errno(0), 255 update_values(NULL) 256 { 257 DBUG_ASSERT(optype == INSERT_OPERATION); 258 } 259 260 /** 261 Initializes this data change operation as an SQL @c UPDATE (multi- or 262 not). 263 264 @param fields The column objects that are to be updated. 265 @param values The values to be assigned to the fields. 266 @note that UPDATE always lists columns, so non-listed columns may need a 267 default thus m_manage_defaults is always true. 268 */ COPY_INFO(operation_type optype,List<Item> * fields,List<Item> * values)269 COPY_INFO(operation_type optype, List<Item> *fields, List<Item> *values) : 270 m_optype(optype), 271 m_changed_columns(fields), 272 m_changed_columns2(NULL), 273 m_manage_defaults(true), 274 m_function_default_columns(NULL), 275 handle_duplicates(DUP_ERROR), 276 ignore(false), 277 stats(), 278 escape_char(0), 279 last_errno(0), 280 update_values(values) 281 { 282 DBUG_ASSERT(optype == UPDATE_OPERATION); 283 } 284 get_operation_type()285 operation_type get_operation_type() const { return m_optype; } 286 get_changed_columns()287 List<Item> *get_changed_columns() const { return m_changed_columns; } 288 get_changed_columns2()289 const List<Item> *get_changed_columns2() const { return m_changed_columns2; } 290 get_manage_defaults()291 bool get_manage_defaults() const { return m_manage_defaults; } 292 get_duplicate_handling()293 enum_duplicates get_duplicate_handling() const { return handle_duplicates; } 294 get_ignore_errors()295 bool get_ignore_errors() const { return ignore; } 296 297 /** 298 Assigns function default values to columns of the supplied table. This 299 function cannot fail, but COPY_INFO::get_function_default_columns() must 300 be called beforehand. 301 302 @note COPY_INFO::add_function_default_columns() must be called prior to 303 invoking this function. 304 305 @param table The table to which columns belong. 306 307 @note It is assumed that all columns in this COPY_INFO are resolved to the 308 table. 309 */ 310 virtual void set_function_defaults(TABLE *table); 311 312 /** 313 Adds the columns that are bound to receive default values from a function 314 (e.g. CURRENT_TIMESTAMP) to the set columns. Uses lazy instantiation of the set 315 of function default columns. 316 317 @param table The table on which the operation is performed. 318 @param[out] columns The function default columns are added to this set. 319 320 @retval false Success. 321 @retval true Memory allocation error during lazy instantiation. 322 */ add_function_default_columns(TABLE * table,MY_BITMAP * columns)323 bool add_function_default_columns(TABLE *table, MY_BITMAP *columns) 324 { 325 if (get_function_default_columns(table)) 326 return true; 327 bitmap_union(columns, m_function_default_columns); 328 return false; 329 } 330 331 /** 332 True if this operation will set some fields to function default result 333 values when invoked on the table. 334 335 @note COPY_INFO::add_function_default_columns() must be called prior to 336 invoking this function. 337 */ function_defaults_apply(const TABLE * table)338 bool function_defaults_apply(const TABLE *table) const 339 { 340 DBUG_ASSERT(m_function_default_columns != NULL); 341 return !bitmap_is_clear_all(m_function_default_columns); 342 } 343 344 /** 345 True if any of the columns set in the bitmap have default functions 346 that may set the column. 347 */ function_defaults_apply_on_columns(MY_BITMAP * map)348 bool function_defaults_apply_on_columns(MY_BITMAP *map) 349 { 350 DBUG_ASSERT(m_function_default_columns != NULL); 351 return bitmap_is_overlapping(m_function_default_columns, map); 352 } 353 354 /** 355 Tells the object to not manage function defaults for the last 'count' 356 columns of 'table'. 357 @retval false if success 358 */ 359 bool ignore_last_columns(TABLE *table, uint count); 360 361 /** 362 This class allocates its memory in a MEM_ROOT, so there's nothing to 363 delete. 364 */ ~COPY_INFO()365 virtual ~COPY_INFO() {} 366 }; 367 368 369 #endif // SQL_DATA_CHANGE_INCLUDED 370