30
30
end of dispatch_command().
33
bool mysql_delete(Session *thd, TableList *table_list, COND *conds,
33
bool mysql_delete(Session *session, TableList *table_list, COND *conds,
34
34
SQL_LIST *order, ha_rows limit, uint64_t options,
35
35
bool reset_auto_increment)
44
44
bool const_cond_result;
45
45
ha_rows deleted= 0;
46
46
uint32_t usable_index= MAX_KEY;
47
SELECT_LEX *select_lex= &thd->lex->select_lex;
47
SELECT_LEX *select_lex= &session->lex->select_lex;
48
48
Session::killed_state killed_status= Session::NOT_KILLED;
51
if (open_and_lock_tables(thd, table_list))
51
if (open_and_lock_tables(session, table_list))
53
53
/* TODO look at this error */
54
54
if (!(table= table_list->table))
56
56
my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0), "", "");
59
thd->set_proc_info("init");
59
session->set_proc_info("init");
62
if (mysql_prepare_delete(thd, table_list, &conds))
62
if (mysql_prepare_delete(session, table_list, &conds))
65
65
/* check ORDER BY even if it can be ignored */
73
73
tables.table = table;
74
74
tables.alias = table_list->alias;
76
if (select_lex->setup_ref_array(thd, order->elements) ||
77
setup_order(thd, select_lex->ref_pointer_array, &tables,
76
if (select_lex->setup_ref_array(session, order->elements) ||
77
setup_order(session, select_lex->ref_pointer_array, &tables,
78
78
fields, all_fields, (order_st*) order->first))
81
free_underlaid_joins(thd, &thd->lex->select_lex);
81
free_underlaid_joins(session, &session->lex->select_lex);
86
86
const_cond= (!conds || conds->const_item());
87
safe_update=test(thd->options & OPTION_SAFE_UPDATES);
87
safe_update=test(session->options & OPTION_SAFE_UPDATES);
88
88
if (safe_update && const_cond)
90
90
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
121
121
- there should be no delete triggers associated with the table.
123
123
if (!using_limit && const_cond_result &&
124
(thd->lex->sql_command == SQLCOM_TRUNCATE ||
125
(!thd->current_stmt_binlog_row_based)))
124
(session->lex->sql_command == SQLCOM_TRUNCATE ||
125
(!session->current_stmt_binlog_row_based)))
127
127
/* Update the table->file->stats.records number */
128
128
table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
157
157
select=make_select(table, 0, 0, conds, 0, &error);
160
if ((select && select->check_quick(thd, safe_update, limit)) || !limit)
160
if ((select && select->check_quick(session, safe_update, limit)) || !limit)
163
free_underlaid_joins(thd, select_lex);
164
thd->row_count_func= 0;
163
free_underlaid_joins(session, select_lex);
164
session->row_count_func= 0;
165
165
DRIZZLE_DELETE_END();
166
my_ok(thd, (ha_rows) thd->row_count_func);
166
my_ok(session, (ha_rows) session->row_count_func);
168
168
We don't need to call reset_auto_increment in this case, because
169
169
mysql_truncate always gives a NULL conds argument, hence we never
175
175
/* If running in safe sql mode, don't allow updates without keys */
176
176
if (table->quick_keys.is_clear_all())
178
thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
178
session->server_status|=SERVER_QUERY_NO_INDEX_USED;
179
179
if (safe_update && !using_limit)
182
free_underlaid_joins(thd, select_lex);
182
free_underlaid_joins(session, select_lex);
183
183
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
184
184
ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
205
205
if (!(sortorder= make_unireg_sortorder((order_st*) order->first,
206
206
&length, NULL)) ||
207
(table->sort.found_records = filesort(thd, table, sortorder, length,
207
(table->sort.found_records = filesort(session, table, sortorder, length,
208
208
select, HA_POS_ERROR, 1,
213
free_underlaid_joins(thd, &thd->lex->select_lex);
213
free_underlaid_joins(session, &session->lex->select_lex);
227
227
if (select && select->quick && select->quick->reset())
230
free_underlaid_joins(thd, select_lex);
230
free_underlaid_joins(session, select_lex);
233
233
if (usable_index==MAX_KEY)
234
init_read_record(&info,thd,table,select,1,1);
234
init_read_record(&info,session,table,select,1,1);
236
init_read_record_idx(&info, thd, table, 1, usable_index);
236
init_read_record_idx(&info, session, table, 1, usable_index);
238
thd->set_proc_info("updating");
238
session->set_proc_info("updating");
240
240
will_batch= !table->file->start_bulk_delete();
243
243
table->mark_columns_needed_for_delete();
245
while (!(error=info.read_record(&info)) && !thd->killed &&
245
while (!(error=info.read_record(&info)) && !session->killed &&
246
! session->is_error())
248
// thd->is_error() is tested to disallow delete row on error
249
if (!(select && select->skip_record())&& ! thd->is_error() )
248
// session->is_error() is tested to disallow delete row on error
249
if (!(select && select->skip_record())&& ! session->is_error() )
251
251
if (!(error= table->file->ha_delete_row(table->record[0])))
310
310
transactional_table= table->file->has_transactions();
312
312
if (!transactional_table && deleted > 0)
313
thd->transaction.stmt.modified_non_trans_table= true;
313
session->transaction.stmt.modified_non_trans_table= true;
315
315
/* See similar binlogging code in sql_update.cc, for comments */
316
if ((error < 0) || thd->transaction.stmt.modified_non_trans_table)
316
if ((error < 0) || session->transaction.stmt.modified_non_trans_table)
318
318
if (mysql_bin_log.is_open())
321
session->clear_error();
323
323
[binlog]: If 'handler::delete_all_rows()' was called and the
324
324
storage engine does not inject the rows itself, we replicate
325
325
statement-based; otherwise, 'ha_delete_row()' was used to
326
326
delete specific rows which we might log row-based.
328
int log_result= thd->binlog_query(Session::ROW_QUERY_TYPE,
329
thd->query, thd->query_length,
328
int log_result= session->binlog_query(Session::ROW_QUERY_TYPE,
329
session->query, session->query_length,
330
330
transactional_table, false, killed_status);
332
332
if (log_result && transactional_table)
337
if (thd->transaction.stmt.modified_non_trans_table)
338
thd->transaction.all.modified_non_trans_table= true;
337
if (session->transaction.stmt.modified_non_trans_table)
338
session->transaction.all.modified_non_trans_table= true;
340
assert(transactional_table || !deleted || thd->transaction.stmt.modified_non_trans_table);
341
free_underlaid_joins(thd, select_lex);
340
assert(transactional_table || !deleted || session->transaction.stmt.modified_non_trans_table);
341
free_underlaid_joins(session, select_lex);
343
343
DRIZZLE_DELETE_END();
344
if (error < 0 || (thd->lex->ignore && !thd->is_fatal_error))
344
if (error < 0 || (session->lex->ignore && !session->is_fatal_error))
346
thd->row_count_func= deleted;
347
my_ok(thd, (ha_rows) thd->row_count_func);
346
session->row_count_func= deleted;
347
my_ok(session, (ha_rows) session->row_count_func);
349
return(error >= 0 || thd->is_error());
349
return(error >= 0 || session->is_error());
352
352
DRIZZLE_DELETE_END();
381
381
is present. However it may confuse users to see very similiar statements
382
382
replicated differently.
384
if (thd->lex->current_select->select_limit)
384
if (session->lex->current_select->select_limit)
386
thd->lex->set_stmt_unsafe();
387
thd->set_current_stmt_binlog_row_based_if_mixed();
386
session->lex->set_stmt_unsafe();
387
session->set_current_stmt_binlog_row_based_if_mixed();
389
thd->lex->allow_sum_func= 0;
390
if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
391
&thd->lex->select_lex.top_join_list,
389
session->lex->allow_sum_func= 0;
390
if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
391
&session->lex->select_lex.top_join_list,
393
393
&select_lex->leaf_tables, false) ||
394
setup_conds(thd, table_list, select_lex->leaf_tables, conds))
394
setup_conds(session, table_list, select_lex->leaf_tables, conds))
397
397
TableList *duplicate;
398
if ((duplicate= unique_table(thd, table_list, table_list->next_global, 0)))
398
if ((duplicate= unique_table(session, table_list, table_list->next_global, 0)))
400
400
update_non_unique_table_error(table_list, "DELETE", duplicate);
448
448
lex->query_tables also point on local list of DELETE SELECT_LEX
450
if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
451
&thd->lex->select_lex.top_join_list,
450
if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
451
&session->lex->select_lex.top_join_list,
452
452
lex->query_tables,
453
453
&lex->select_lex.leaf_tables, false))
691
if (thd->transaction.stmt.modified_non_trans_table)
691
if (session->transaction.stmt.modified_non_trans_table)
694
694
there is only side effects; to binlog with the error
696
696
if (mysql_bin_log.is_open())
698
thd->binlog_query(Session::ROW_QUERY_TYPE,
699
thd->query, thd->query_length,
698
session->binlog_query(Session::ROW_QUERY_TYPE,
699
session->query, session->query_length,
700
700
transactional_tables, false);
702
thd->transaction.all.modified_non_trans_table= true;
702
session->transaction.all.modified_non_trans_table= true;
741
741
READ_RECORD info;
742
init_read_record(&info,thd,table,NULL,0,1);
742
init_read_record(&info,session,table,NULL,0,1);
744
744
Ignore any rows not found in reference tables as they may already have
745
745
been deleted by foreign key handling
747
747
info.ignore_not_found_rows= 1;
748
748
will_batch= !table->file->start_bulk_delete();
749
while (!(local_error=info.read_record(&info)) && !thd->killed)
749
while (!(local_error=info.read_record(&info)) && !session->killed)
751
751
if ((local_error=table->file->ha_delete_row(table->record[0])))
785
785
bool multi_delete::send_eof()
787
787
Session::killed_state killed_status= Session::NOT_KILLED;
788
thd->set_proc_info("deleting from reference tables");
788
session->set_proc_info("deleting from reference tables");
790
790
/* Does deletes for the last n - 1 tables, returns 0 if ok */
791
791
int local_error= do_deletes(); // returns 0 if success
793
793
/* compute a total error to know if something failed */
794
794
local_error= local_error || error;
795
killed_status= (local_error == 0)? Session::NOT_KILLED : thd->killed;
795
killed_status= (local_error == 0)? Session::NOT_KILLED : session->killed;
796
796
/* reset used flags */
797
thd->set_proc_info("end");
797
session->set_proc_info("end");
799
if ((local_error == 0) || thd->transaction.stmt.modified_non_trans_table)
799
if ((local_error == 0) || session->transaction.stmt.modified_non_trans_table)
801
801
if (mysql_bin_log.is_open())
803
803
if (local_error == 0)
805
if (thd->binlog_query(Session::ROW_QUERY_TYPE,
806
thd->query, thd->query_length,
804
session->clear_error();
805
if (session->binlog_query(Session::ROW_QUERY_TYPE,
806
session->query, session->query_length,
807
807
transactional_tables, false, killed_status) &&
810
810
local_error=1; // Log write failed: roll back the SQL statement
813
if (thd->transaction.stmt.modified_non_trans_table)
814
thd->transaction.all.modified_non_trans_table= true;
813
if (session->transaction.stmt.modified_non_trans_table)
814
session->transaction.all.modified_non_trans_table= true;
816
816
if (local_error != 0)
817
817
error_handled= true; // to force early leave from ::send_error()
819
819
if (!local_error)
821
thd->row_count_func= deleted;
822
::my_ok(thd, (ha_rows) thd->row_count_func);
821
session->row_count_func= deleted;
822
::my_ok(session, (ha_rows) session->row_count_func);
853
853
memset(&create_info, 0, sizeof(create_info));
854
854
/* If it is a temporary table, close and regenerate it */
855
if (!dont_send_ok && (table= find_temporary_table(thd, table_list)))
855
if (!dont_send_ok && (table= find_temporary_table(session, table_list)))
857
857
handlerton *table_type= table->s->db_type();
858
858
TABLE_SHARE *share= table->s;
864
864
table->file->info(HA_STATUS_AUTO | HA_STATUS_NO_LOCK);
866
close_temporary_table(thd, table, 0, 0); // Don't free share
867
ha_create_table(thd, share->normalized_path.str,
866
close_temporary_table(session, table, 0, 0); // Don't free share
867
ha_create_table(session, share->normalized_path.str,
868
868
share->db.str, share->table_name.str, &create_info, 1);
869
869
// We don't need to call invalidate() because this table is not in cache
870
if ((error= (int) !(open_temporary_table(thd, share->path.str,
870
if ((error= (int) !(open_temporary_table(session, share->path.str,
872
872
share->table_name.str, 1,
893
893
path[path_length - reg_ext_length] = 0;
894
894
pthread_mutex_lock(&LOCK_open);
895
error= ha_create_table(thd, path, table_list->db, table_list->table_name,
895
error= ha_create_table(session, path, table_list->db, table_list->table_name,
896
896
&create_info, 1);
897
897
pthread_mutex_unlock(&LOCK_open);
905
905
TRUNCATE must always be statement-based binlogged (not row-based) so
906
906
we don't test current_stmt_binlog_row_based.
908
write_bin_log(thd, true, thd->query, thd->query_length);
909
my_ok(thd); // This should return record count
908
write_bin_log(session, true, session->query, session->query_length);
909
my_ok(session); // This should return record count
911
911
pthread_mutex_lock(&LOCK_open);
912
unlock_table_name(thd, table_list);
912
unlock_table_name(session, table_list);
913
913
pthread_mutex_unlock(&LOCK_open);
917
917
pthread_mutex_lock(&LOCK_open);
918
unlock_table_name(thd, table_list);
918
unlock_table_name(session, table_list);
919
919
pthread_mutex_unlock(&LOCK_open);
924
924
/* Probably InnoDB table */
925
uint64_t save_options= thd->options;
925
uint64_t save_options= session->options;
926
926
table_list->lock_type= TL_WRITE;
927
thd->options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
928
ha_enable_transaction(thd, false);
929
mysql_init_select(thd->lex);
930
bool save_binlog_row_based= thd->current_stmt_binlog_row_based;
931
thd->clear_current_stmt_binlog_row_based();
932
error= mysql_delete(thd, table_list, (COND*) 0, (SQL_LIST*) 0,
927
session->options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
928
ha_enable_transaction(session, false);
929
mysql_init_select(session->lex);
930
bool save_binlog_row_based= session->current_stmt_binlog_row_based;
931
session->clear_current_stmt_binlog_row_based();
932
error= mysql_delete(session, table_list, (COND*) 0, (SQL_LIST*) 0,
933
933
HA_POS_ERROR, 0L, true);
934
ha_enable_transaction(thd, true);
934
ha_enable_transaction(session, true);
936
936
Safety, in case the engine ignored ha_enable_transaction(false)
937
above. Also clears thd->transaction.*.
937
above. Also clears session->transaction.*.
939
error= ha_autocommit_or_rollback(thd, error);
941
thd->options= save_options;
942
thd->current_stmt_binlog_row_based= save_binlog_row_based;
939
error= ha_autocommit_or_rollback(session, error);
941
session->options= save_options;
942
session->current_stmt_binlog_row_based= save_binlog_row_based;