30
30
end of dispatch_command().
33
bool mysql_delete(THD *thd, TableList *table_list, COND *conds,
34
SQL_LIST *order, ha_rows limit, uint64_t options,
33
bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
34
SQL_LIST *order, ha_rows limit, ulonglong options,
35
35
bool reset_auto_increment)
38
38
int error, loc_error;
40
40
SQL_SELECT *select=0;
42
42
bool using_limit=limit != HA_POS_ERROR;
43
43
bool transactional_table, safe_update, const_cond;
44
44
bool const_cond_result;
45
45
ha_rows deleted= 0;
46
uint32_t usable_index= MAX_KEY;
46
uint usable_index= MAX_KEY;
47
47
SELECT_LEX *select_lex= &thd->lex->select_lex;
48
48
THD::killed_state killed_status= THD::NOT_KILLED;
51
51
if (open_and_lock_tables(thd, 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
thd_proc_info(thd, "init");
62
62
if (mysql_prepare_delete(thd, table_list, &conds))
65
65
/* check ORDER BY even if it can be ignored */
66
66
if (order && order->elements)
70
70
List<Item> all_fields;
72
memset(&tables, 0, sizeof(tables));
72
bzero((char*) &tables,sizeof(tables));
73
73
tables.table = table;
74
74
tables.alias = table_list->alias;
76
76
if (select_lex->setup_ref_array(thd, order->elements) ||
77
77
setup_order(thd, select_lex->ref_pointer_array, &tables,
78
fields, all_fields, (order_st*) order->first))
78
fields, all_fields, (ORDER*) order->first))
81
81
free_underlaid_joins(thd, &thd->lex->select_lex);
121
121
- there should be no delete triggers associated with the table.
123
123
if (!using_limit && const_cond_result &&
124
!(specialflag & (SPECIAL_NO_NEW_FUNC | SPECIAL_SAFE_MODE)) &&
124
125
(thd->lex->sql_command == SQLCOM_TRUNCATE ||
125
126
(!thd->current_stmt_binlog_row_based)))
191
192
if (order && order->elements)
194
195
SORT_FIELD *sortorder;
195
196
ha_rows examined_rows;
197
198
if ((!select || table->quick_keys.is_clear_all()) && limit != HA_POS_ERROR)
198
usable_index= get_index_for_order(table, (order_st*)(order->first), limit);
199
usable_index= get_index_for_order(table, (ORDER*)(order->first), limit);
200
201
if (usable_index == MAX_KEY)
202
203
table->sort.io_cache= (IO_CACHE *) my_malloc(sizeof(IO_CACHE),
203
204
MYF(MY_FAE | MY_ZEROFILL));
205
if (!(sortorder= make_unireg_sortorder((order_st*) order->first,
206
if (!(sortorder= make_unireg_sortorder((ORDER*) order->first,
206
207
&length, NULL)) ||
207
208
(table->sort.found_records = filesort(thd, table, sortorder, length,
208
209
select, HA_POS_ERROR, 1,
236
237
init_read_record_idx(&info, thd, table, 1, usable_index);
238
thd->set_proc_info("updating");
239
thd_proc_info(thd, "updating");
240
241
will_batch= !table->file->start_bulk_delete();
284
285
table->file->print_error(loc_error,MYF(0));
287
thd->set_proc_info("end");
288
thd_proc_info(thd, "end");
288
289
end_read_record(&info);
289
290
if (options & OPTION_QUICK)
290
291
(void) table->file->extra(HA_EXTRA_NORMAL);
310
311
transactional_table= table->file->has_transactions();
312
313
if (!transactional_table && deleted > 0)
313
thd->transaction.stmt.modified_non_trans_table= true;
314
thd->transaction.stmt.modified_non_trans_table= TRUE;
315
316
/* See similar binlogging code in sql_update.cc, for comments */
316
317
if ((error < 0) || thd->transaction.stmt.modified_non_trans_table)
328
329
int log_result= thd->binlog_query(THD::ROW_QUERY_TYPE,
329
330
thd->query, thd->query_length,
330
transactional_table, false, killed_status);
331
transactional_table, FALSE, killed_status);
332
333
if (log_result && transactional_table)
337
338
if (thd->transaction.stmt.modified_non_trans_table)
338
thd->transaction.all.modified_non_trans_table= true;
339
thd->transaction.all.modified_non_trans_table= TRUE;
340
341
assert(transactional_table || !deleted || thd->transaction.stmt.modified_non_trans_table);
341
342
free_underlaid_joins(thd, select_lex);
343
DRIZZLE_DELETE_END();
344
345
if (error < 0 || (thd->lex->ignore && !thd->is_fatal_error))
346
347
thd->row_count_func= deleted;
393
395
&select_lex->leaf_tables, false) ||
394
396
setup_conds(thd, table_list, select_lex->leaf_tables, conds))
397
TableList *duplicate;
399
TABLE_LIST *duplicate;
398
400
if ((duplicate= unique_table(thd, table_list, table_list->next_global, 0)))
400
402
update_non_unique_table_error(table_list, "DELETE", duplicate);
419
422
extern "C" int refpos_order_cmp(void* arg, const void *a,const void *b)
421
424
handler *file= (handler*)arg;
422
return file->cmp_ref((const unsigned char*)a, (const unsigned char*)b);
425
return file->cmp_ref((const uchar*)a, (const uchar*)b);
451
454
&thd->lex->select_lex.top_join_list,
452
455
lex->query_tables,
453
456
&lex->select_lex.leaf_tables, false))
458
461
Multi-delete can't be constructed over-union => we always have
459
462
single SELECT on top and have to check underlying SELECTs of it
461
lex->select_lex.exclude_from_table_unique_test= true;
464
lex->select_lex.exclude_from_table_unique_test= TRUE;
462
465
/* Fix tables-to-be-deleted-from list to point at opened tables */
463
for (target_tbl= (TableList*) aux_tables;
466
for (target_tbl= (TABLE_LIST*) aux_tables;
465
468
target_tbl= target_tbl->next_local)
478
481
inside subqueries/view.
481
TableList *duplicate;
484
TABLE_LIST *duplicate;
482
485
if ((duplicate= unique_table(thd, target_tbl->correspondent_table,
483
486
lex->query_tables, 0)))
485
488
update_non_unique_table_error(target_tbl->correspondent_table,
486
489
"DELETE", duplicate);
495
multi_delete::multi_delete(TableList *dt, uint32_t num_of_tables_arg)
498
multi_delete::multi_delete(TABLE_LIST *dt, uint num_of_tables_arg)
496
499
: delete_tables(dt), deleted(0), found(0),
497
500
num_of_tables(num_of_tables_arg), error(0),
498
501
do_delete(0), transactional_tables(0), normal_tables(0), error_handled(0)
505
multi_delete::prepare(List<Item> &values __attribute__((unused)),
508
multi_delete::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
511
thd->set_proc_info("deleting from main table");
513
thd_proc_info(thd, "deleting from main table");
586
588
table_being_deleted;
587
589
table_being_deleted= table_being_deleted->next_local)
589
Table *table= table_being_deleted->table;
591
TABLE *table= table_being_deleted->table;
590
592
table->no_keyread=0;
593
for (uint32_t counter= 0; counter < num_of_tables; counter++)
595
for (uint counter= 0; counter < num_of_tables; counter++)
595
597
if (tempfiles[counter])
596
598
delete tempfiles[counter];
601
bool multi_delete::send_data(List<Item> &values __attribute__((unused)))
603
bool multi_delete::send_data(List<Item> &values)
603
605
int secure_counter= delete_while_scanning ? -1 : 0;
604
TableList *del_table;
606
TABLE_LIST *del_table;
607
609
for (del_table= delete_tables;
609
611
del_table= del_table->next_local, secure_counter++)
611
Table *table= del_table->table;
613
TABLE *table= del_table->table;
613
615
/* Check if we are using outer join and we didn't find the row */
614
616
if (table->status & (STATUS_NULL_ROW | STATUS_DELETED))
698
700
thd->binlog_query(THD::ROW_QUERY_TYPE,
699
701
thd->query, thd->query_length,
700
transactional_tables, false);
702
transactional_tables, FALSE);
702
704
thd->transaction.all.modified_non_trans_table= true;
731
733
table_being_deleted= table_being_deleted->next_local, counter++)
733
735
ha_rows last_deleted= deleted;
734
Table *table = table_being_deleted->table;
736
TABLE *table = table_being_deleted->table;
735
737
if (tempfiles[counter]->get(table))
785
787
bool multi_delete::send_eof()
787
789
THD::killed_state killed_status= THD::NOT_KILLED;
788
thd->set_proc_info("deleting from reference tables");
790
thd_proc_info(thd, "deleting from reference tables");
790
792
/* Does deletes for the last n - 1 tables, returns 0 if ok */
791
793
int local_error= do_deletes(); // returns 0 if success
794
796
local_error= local_error || error;
795
797
killed_status= (local_error == 0)? THD::NOT_KILLED : thd->killed;
796
798
/* reset used flags */
797
thd->set_proc_info("end");
799
thd_proc_info(thd, "end");
799
801
if ((local_error == 0) || thd->transaction.stmt.modified_non_trans_table)
804
806
thd->clear_error();
805
807
if (thd->binlog_query(THD::ROW_QUERY_TYPE,
806
808
thd->query, thd->query_length,
807
transactional_tables, false, killed_status) &&
809
transactional_tables, FALSE, killed_status) &&
810
812
local_error=1; // Log write failed: roll back the SQL statement
813
815
if (thd->transaction.stmt.modified_non_trans_table)
814
thd->transaction.all.modified_non_trans_table= true;
816
thd->transaction.all.modified_non_trans_table= TRUE;
816
818
if (local_error != 0)
817
error_handled= true; // to force early leave from ::send_error()
819
error_handled= TRUE; // to force early leave from ::send_error()
819
821
if (!local_error)
841
843
- If we want to have a name lock on the table on exit without errors.
844
bool mysql_truncate(THD *thd, TableList *table_list, bool dont_send_ok)
846
bool mysql_truncate(THD *thd, TABLE_LIST *table_list, bool dont_send_ok)
846
848
HA_CREATE_INFO create_info;
847
849
char path[FN_REFLEN];
850
uint32_t path_length;
853
memset(&create_info, 0, sizeof(create_info));
855
bzero((char*) &create_info,sizeof(create_info));
854
856
/* If it is a temporary table, close and regenerate it */
855
857
if (!dont_send_ok && (table= find_temporary_table(thd, table_list)))
874
876
(void) rm_temporary_table(table_type, path, frm_only);
875
877
free_table_share(share);
878
my_free((char*) table,MYF(0));
878
880
If we return here we will not have logged the truncation to the bin log
879
881
and we will not my_ok() to the client.
885
887
table_list->table_name, reg_ext, 0);
887
889
if (!dont_send_ok)
891
enum legacy_db_type table_type;
892
mysql_frm_type(thd, path, &table_type);
893
if (table_type == DB_TYPE_UNKNOWN)
895
my_error(ER_NO_SUCH_TABLE, MYF(0),
896
table_list->db, table_list->table_name);
900
if (!ha_check_storage_engine_flag(ha_resolve_by_legacy_type(thd, table_type),
904
if (lock_and_wait_for_table_name(thd, table_list))
890
908
// Remove the .frm extension AIX 5.2 64-bit compiler bug (BUG#16155): this
891
909
// crashes, replacement works. *(path + path_length - reg_ext_length)=
893
911
path[path_length - reg_ext_length] = 0;
894
pthread_mutex_lock(&LOCK_open);
912
VOID(pthread_mutex_lock(&LOCK_open));
895
913
error= ha_create_table(thd, path, table_list->db, table_list->table_name,
896
914
&create_info, 1);
897
pthread_mutex_unlock(&LOCK_open);
915
VOID(pthread_mutex_unlock(&LOCK_open));
900
918
if (!dont_send_ok)
905
923
TRUNCATE must always be statement-based binlogged (not row-based) so
906
924
we don't test current_stmt_binlog_row_based.
908
write_bin_log(thd, true, thd->query, thd->query_length);
926
write_bin_log(thd, TRUE, thd->query, thd->query_length);
909
927
my_ok(thd); // This should return record count
911
pthread_mutex_lock(&LOCK_open);
929
VOID(pthread_mutex_lock(&LOCK_open));
912
930
unlock_table_name(thd, table_list);
913
pthread_mutex_unlock(&LOCK_open);
931
VOID(pthread_mutex_unlock(&LOCK_open));
917
pthread_mutex_lock(&LOCK_open);
935
VOID(pthread_mutex_lock(&LOCK_open));
918
936
unlock_table_name(thd, table_list);
919
pthread_mutex_unlock(&LOCK_open);
937
VOID(pthread_mutex_unlock(&LOCK_open));
924
942
/* Probably InnoDB table */
925
uint64_t save_options= thd->options;
943
ulonglong save_options= thd->options;
926
944
table_list->lock_type= TL_WRITE;
927
945
thd->options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
928
ha_enable_transaction(thd, false);
946
ha_enable_transaction(thd, FALSE);
929
947
mysql_init_select(thd->lex);
930
948
bool save_binlog_row_based= thd->current_stmt_binlog_row_based;
931
949
thd->clear_current_stmt_binlog_row_based();
932
950
error= mysql_delete(thd, table_list, (COND*) 0, (SQL_LIST*) 0,
933
HA_POS_ERROR, 0L, true);
934
ha_enable_transaction(thd, true);
951
HA_POS_ERROR, 0LL, TRUE);
952
ha_enable_transaction(thd, TRUE);
936
Safety, in case the engine ignored ha_enable_transaction(false)
954
Safety, in case the engine ignored ha_enable_transaction(FALSE)
937
955
above. Also clears thd->transaction.*.
939
957
error= ha_autocommit_or_rollback(thd, error);