139 uint32_t order_num,
Order *order,
140 ha_rows limit,
enum enum_duplicates,
143 bool using_limit= limit != HA_POS_ERROR;
144 bool used_key_is_modified;
145 bool transactional_table;
147 uint used_index= MAX_KEY, dup_key_found;
148 bool need_sort=
true;
149 ha_rows updated, found;
150 key_map old_covering_keys;
154 Select_Lex *select_lex= &session->lex().select_lex;
157 Session::killed_state_t killed_status= Session::NOT_KILLED;
159 DRIZZLE_UPDATE_START(session->getQueryString()->c_str());
160 if (session->openTablesLock(table_list))
162 DRIZZLE_UPDATE_DONE(1, 0, 0);
166 session->set_proc_info(
"init");
167 table= table_list->
table;
170 table->covering_keys= table->getShare()->keys_in_use;
171 table->quick_keys.reset();
173 if (prepare_update(session, table_list, &conds, order_num, order))
175 DRIZZLE_UPDATE_DONE(1, 0, 0);
179 old_covering_keys= table->covering_keys;
181 if (setup_fields_with_no_wrap(session, 0, fields, MARK_COLUMNS_WRITE, 0, 0))
183 DRIZZLE_UPDATE_DONE(1, 0, 0);
187 if (table->timestamp_field)
190 if (table->timestamp_field->isWriteSet())
192 table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
196 if (table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_UPDATE ||
197 table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH)
199 table->setWriteSet(table->timestamp_field->position());
204 if (setup_fields(session, 0, values, MARK_COLUMNS_READ, 0, 0))
207 DRIZZLE_UPDATE_DONE(1, 0, 0);
212 if (select_lex->inner_refs_list.size() &&
213 fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
215 DRIZZLE_UPDATE_DONE(1, 0, 0);
221 Item::cond_result cond_value;
223 if (cond_value == Item::COND_FALSE)
232 if (table->cursor->getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ) &&
233 table->timestamp_field &&
234 (table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_UPDATE ||
235 table->timestamp_field_type == TIMESTAMP_AUTO_SET_ON_BOTH))
237 *table->read_set|= *table->write_set;
240 table->covering_keys.reset();
243 table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
245 select= optimizer::make_select(table, 0, 0, conds, 0, &error);
246 if (error || !limit ||
247 (select && select->check_quick(session,
false, limit)))
254 session->main_da().reset_diagnostics_area();
256 if (error || session->is_error())
258 DRIZZLE_UPDATE_DONE(1, 0, 0);
261 DRIZZLE_UPDATE_DONE(0, 0, 0);
265 if (!select && limit != HA_POS_ERROR)
267 if ((used_index= optimizer::get_index_for_order(table, order, limit)) != MAX_KEY)
271 if (table->quick_keys.none())
273 session->server_status|=SERVER_QUERY_NO_INDEX_USED;
276 table->mark_columns_needed_for_update();
280 if (select && select->
quick)
283 used_key_is_modified= (!select->
quick->unique_key_range() &&
288 used_key_is_modified= 0;
289 if (used_index == MAX_KEY)
290 used_index= table->cursor->key_used_on_scan;
291 if (used_index != MAX_KEY)
292 used_key_is_modified= is_key_used(table, used_index, *table->write_set);
296 if (used_key_is_modified || order)
302 if (used_index < MAX_KEY && old_covering_keys.test(used_index))
305 table->mark_columns_used_by_index(used_index);
309 table->use_all_columns();
313 if (order && (need_sort || used_key_is_modified))
322 ha_rows examined_rows;
326 sortorder=make_unireg_sortorder(order, &length, NULL);
328 if ((table->sort.found_records= filesort.
run(table, sortorder, length, select, limit, 1, examined_rows)) == HA_POS_ERROR)
345 if (tempfile.open_cached_file(drizzle_tmpdir.c_str(),TEMP_PREFIX, DISK_BUFFER_SIZE, MYF(MY_WME)))
353 table->cursor->try_semi_consistent_read(1);
366 if (used_index == MAX_KEY || (select && select->
quick))
368 if ((error= info.init_read_record(session, table, select, 0,
true)))
377 session->set_proc_info(
"Searching rows for update");
378 ha_rows tmp_limit= limit;
380 while (not(error= info.read_record(&info)) && not session->getKilled())
382 if (!(select && select->skip_record()))
384 if (table->cursor->was_semi_consistent_read())
387 table->cursor->position(table->getInsertRecord());
388 if (tempfile.write(table->cursor->ref, table->cursor->ref_length))
393 if (!--limit && using_limit)
400 table->cursor->unlock_row();
402 if (session->getKilled() && not error)
405 table->cursor->try_semi_consistent_read(0);
406 info.end_read_record();
411 safe_delete(select->
quick);
412 if (select->free_cond)
424 memcpy(
static_cast<void *
>(select->
file),
425 static_cast<const void *
>(&tempfile),
sizeof(tempfile));
430 table->restore_column_maps_after_mark_index();
434 table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
438 table->cursor->try_semi_consistent_read(1);
439 if ((error= info.init_read_record(session, table, select, 0,
true)))
453 session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
454 session->cuted_fields= 0L;
455 session->set_proc_info(
"Updating");
457 transactional_table= table->cursor->has_transactions();
458 session->setAbortOnWarning(test(!ignore));
464 if (table->cursor->getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ))
465 table->prepare_for_position();
467 while (not (error=info.read_record(&info)) && not session->getKilled())
469 if (not (select && select->skip_record()))
471 if (table->cursor->was_semi_consistent_read())
474 table->storeRecord();
475 if (fill_record(session, fields, values))
480 if (! table->records_are_comparable() || table->compare_records())
483 error= table->cursor->updateRecord(table->getUpdateRecord(),
484 table->getInsertRecord());
486 table->auto_increment_field_not_null=
false;
488 if (!error || error == HA_ERR_RECORD_IS_THE_SAME)
490 if (error != HA_ERR_RECORD_IS_THE_SAME)
496 table->cursor->is_fatal_error(error, HA_CHECK_DUP_KEY))
504 if (table->cursor->is_fatal_error(error, HA_CHECK_DUP_KEY))
505 flags|= ME_FATALERROR;
508 table->print_error(error,MYF(flags));
514 if (!--limit && using_limit)
521 table->cursor->unlock_row();
522 session->row_count++;
531 killed_status= session->getKilled();
533 error= (killed_status == Session::NOT_KILLED)? error : 1;
535 updated-= dup_key_found;
536 table->cursor->try_semi_consistent_read(0);
538 if (!transactional_table && updated > 0)
539 session->transaction.stmt.markModifiedNonTransData();
541 info.end_read_record();
543 session->set_proc_info(
"end");
544 table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
553 if ((error < 0) || session->transaction.stmt.hasModifiedNonTransData())
555 if (session->transaction.stmt.hasModifiedNonTransData())
556 session->transaction.all.markModifiedNonTransData();
558 assert(transactional_table || !updated || session->transaction.stmt.hasModifiedNonTransData());
562 id= session->arg_of_last_insert_id_function ?
563 session->first_successful_insert_id_in_prev_stmt : 0;
567 char buff[STRING_BUFFER_USUAL_SIZE];
568 snprintf(buff,
sizeof(buff), ER(ER_UPDATE_INFO), (ulong) found, (ulong) updated,
569 (ulong) session->cuted_fields);
570 session->row_count_func= updated;
575 session->main_da().reset_diagnostics_area();
576 session->my_ok((ulong) session->rowCount(), found,
id, buff);
577 session->status_var.updated_row_count+= session->rowCount();
579 session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
580 session->setAbortOnWarning(
false);
581 DRIZZLE_UPDATE_DONE((error >= 0 || session->is_error()), found, updated);
582 return ((error >= 0 || session->is_error()) ? 1 : 0);
586 table->print_error(error,MYF(0));
593 table->cursor->extra(HA_EXTRA_NO_KEYREAD);
595 session->setAbortOnWarning(
false);
597 DRIZZLE_UPDATE_DONE(1, 0, 0);