50 SQL_LIST *order, ha_rows limit, uint64_t,
51 bool reset_auto_increment)
57 bool using_limit=limit != HA_POS_ERROR;
59 uint32_t usable_index= MAX_KEY;
60 Select_Lex *select_lex= &session->lex().select_lex;
61 Session::killed_state_t killed_status= Session::NOT_KILLED;
63 if (session->openTablesLock(table_list))
65 DRIZZLE_DELETE_DONE(1, 0);
69 table= table_list->
table;
72 session->set_proc_info(
"init");
75 if (prepare_delete(session, table_list, &conds))
77 DRIZZLE_DELETE_DONE(1, 0);
82 if (order && order->elements)
89 tables.alias = table_list->alias;
91 select_lex->setup_ref_array(session, order->elements);
92 if (
setup_order(session, select_lex->ref_pointer_array, &tables, fields, all_fields, (
Order*) order->first))
96 DRIZZLE_DELETE_DONE(1, 0);
102 bool const_cond= not conds || conds->
const_item();
104 select_lex->no_error= session->lex().ignore;
106 bool const_cond_result= const_cond && (!conds || conds->
val_int());
107 if (session->is_error())
128 if (!using_limit && const_cond_result)
131 table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
132 ha_rows
const maybe_deleted= table->cursor->stats.records;
133 if (!(error=table->cursor->ha_delete_all_rows()))
136 deleted= maybe_deleted;
139 if (error != HA_ERR_WRONG_COMMAND)
141 table->print_error(error,MYF(0));
149 Item::cond_result result;
151 if (result == Item::COND_FALSE)
156 table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
158 table->covering_keys.reset();
159 table->quick_keys.reset();
160 select= optimizer::make_select(table, 0, 0, conds, 0, &error);
163 DRIZZLE_DELETE_DONE(1, 0);
167 if ((select && select->check_quick(session,
false, limit)) || !limit)
171 session->row_count_func= 0;
172 if (session->is_error())
174 DRIZZLE_DELETE_DONE(0, 0);
179 session->main_da().reset_diagnostics_area();
180 session->my_ok((ha_rows) session->rowCount());
190 if (table->quick_keys.none())
192 session->server_status|=SERVER_QUERY_NO_INDEX_USED;
195 if (order && order->elements)
197 if ((!select || table->
quick_keys.none()) && limit != HA_POS_ERROR)
198 usable_index= optimizer::get_index_for_order(table, (
Order*)(order->first), limit);
200 if (usable_index == MAX_KEY)
205 SortField* sortorder= make_unireg_sortorder((
Order*) order->first, &length, NULL);
206 ha_rows examined_rows;
207 if ((table->sort.found_records= filesort.
run(table, sortorder, length, select, HA_POS_ERROR, 1, examined_rows)) == HA_POS_ERROR)
212 DRIZZLE_DELETE_DONE(1, 0);
230 DRIZZLE_DELETE_DONE(1, 0);
234 if (usable_index==MAX_KEY)
236 if ((error= info.init_read_record(session,table,select,1,1)))
238 table->print_error(error, MYF(0));
248 table->print_error(error, MYF(0));
255 session->set_proc_info(
"updating");
257 table->mark_columns_needed_for_delete();
259 while (!(error=info.read_record(&info)) && !session->getKilled() &&
260 ! session->is_error())
263 if (!(select && select->skip_record())&& ! session->is_error() )
265 if (!(error= table->cursor->deleteRecord(table->getInsertRecord())))
268 if (!--limit && using_limit)
276 table->print_error(error,MYF(0));
282 table->cursor->unlock_row();
284 killed_status= session->getKilled();
285 if (killed_status != Session::NOT_KILLED || session->is_error())
288 session->set_proc_info(
"end");
289 info.end_read_record();
293 if (reset_auto_increment && (error < 0))
299 int error2= table->cursor->ha_reset_auto_increment(0);
301 if (error2 && (error2 != HA_ERR_WRONG_COMMAND))
303 table->print_error(error2, MYF(0));
309 bool transactional_table= table->cursor->has_transactions();
311 if (!transactional_table && deleted > 0)
312 session->transaction.stmt.markModifiedNonTransData();
314 if ((error < 0) || session->transaction.stmt.hasModifiedNonTransData())
316 if (session->transaction.stmt.hasModifiedNonTransData())
317 session->transaction.all.markModifiedNonTransData();
319 assert(transactional_table || !deleted || session->transaction.stmt.hasModifiedNonTransData());
322 DRIZZLE_DELETE_DONE((error >= 0 || session->is_error()), deleted);
323 if (error < 0 || (session->lex().ignore && !session->is_fatal_error))
325 session->row_count_func= deleted;
330 session->main_da().reset_diagnostics_area();
331 session->my_ok((ha_rows) session->rowCount());
333 session->status_var.deleted_row_count+= deleted;
335 return (error >= 0 || session->is_error());