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;
49
DBUG_ENTER("mysql_delete");
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)))
127
128
/* Update the table->file->stats.records number */
128
129
table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
129
130
ha_rows const maybe_deleted= table->file->stats.records;
131
DBUG_PRINT("debug", ("Trying to use delete_all_rows()"));
130
132
if (!(error=table->file->ha_delete_all_rows()))
163
165
free_underlaid_joins(thd, select_lex);
164
166
thd->row_count_func= 0;
165
DRIZZLE_DELETE_END();
166
168
my_ok(thd, (ha_rows) thd->row_count_func);
168
170
We don't need to call reset_auto_increment in this case, because
169
171
mysql_truncate always gives a NULL conds argument, hence we never
172
return(0); // Nothing to delete
174
DBUG_RETURN(0); // Nothing to delete
175
177
/* If running in safe sql mode, don't allow updates without keys */
191
193
if (order && order->elements)
194
196
SORT_FIELD *sortorder;
195
197
ha_rows examined_rows;
197
199
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);
200
usable_index= get_index_for_order(table, (ORDER*)(order->first), limit);
200
202
if (usable_index == MAX_KEY)
202
204
table->sort.io_cache= (IO_CACHE *) my_malloc(sizeof(IO_CACHE),
203
205
MYF(MY_FAE | MY_ZEROFILL));
205
if (!(sortorder= make_unireg_sortorder((order_st*) order->first,
207
if (!(sortorder= make_unireg_sortorder((ORDER*) order->first,
206
208
&length, NULL)) ||
207
209
(table->sort.found_records = filesort(thd, table, sortorder, length,
208
210
select, HA_POS_ERROR, 1,
236
238
init_read_record_idx(&info, thd, table, 1, usable_index);
238
thd->set_proc_info("updating");
240
thd_proc_info(thd, "updating");
240
242
will_batch= !table->file->start_bulk_delete();
284
286
table->file->print_error(loc_error,MYF(0));
287
thd->set_proc_info("end");
289
thd_proc_info(thd, "end");
288
290
end_read_record(&info);
289
291
if (options & OPTION_QUICK)
290
292
(void) table->file->extra(HA_EXTRA_NORMAL);
310
312
transactional_table= table->file->has_transactions();
312
314
if (!transactional_table && deleted > 0)
313
thd->transaction.stmt.modified_non_trans_table= true;
315
thd->transaction.stmt.modified_non_trans_table= TRUE;
315
317
/* See similar binlogging code in sql_update.cc, for comments */
316
318
if ((error < 0) || thd->transaction.stmt.modified_non_trans_table)
328
330
int log_result= thd->binlog_query(THD::ROW_QUERY_TYPE,
329
331
thd->query, thd->query_length,
330
transactional_table, false, killed_status);
332
transactional_table, FALSE, killed_status);
332
334
if (log_result && transactional_table)
337
339
if (thd->transaction.stmt.modified_non_trans_table)
338
thd->transaction.all.modified_non_trans_table= true;
340
thd->transaction.all.modified_non_trans_table= TRUE;
340
assert(transactional_table || !deleted || thd->transaction.stmt.modified_non_trans_table);
342
DBUG_ASSERT(transactional_table || !deleted || thd->transaction.stmt.modified_non_trans_table);
341
343
free_underlaid_joins(thd, select_lex);
343
DRIZZLE_DELETE_END();
344
346
if (error < 0 || (thd->lex->ignore && !thd->is_fatal_error))
346
348
thd->row_count_func= deleted;
347
349
my_ok(thd, (ha_rows) thd->row_count_func);
350
DBUG_PRINT("info",("%ld records deleted",(long) deleted));
349
return(error >= 0 || thd->is_error());
352
DBUG_RETURN(error >= 0 || thd->is_error());
352
DRIZZLE_DELETE_END();
364
367
conds - conditions
370
int mysql_prepare_delete(THD *thd, TableList *table_list, Item **conds)
373
int mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, Item **conds)
372
376
SELECT_LEX *select_lex= &thd->lex->select_lex;
377
DBUG_ENTER("mysql_prepare_delete");
374
378
List<Item> all_fields;
393
397
&select_lex->leaf_tables, false) ||
394
398
setup_conds(thd, table_list, select_lex->leaf_tables, conds))
397
TableList *duplicate;
401
TABLE_LIST *duplicate;
398
402
if ((duplicate= unique_table(thd, table_list, table_list->next_global, 0)))
400
404
update_non_unique_table_error(table_list, "DELETE", duplicate);
405
409
if (select_lex->inner_refs_list.elements &&
406
410
fix_inner_refs(thd, all_fields, select_lex, select_lex->ref_pointer_array))
413
select_lex->fix_prepare_information(thd, conds, &fake_conds);
419
424
extern "C" int refpos_order_cmp(void* arg, const void *a,const void *b)
421
426
handler *file= (handler*)arg;
422
return file->cmp_ref((const unsigned char*)a, (const unsigned char*)b);
427
return file->cmp_ref((const uchar*)a, (const uchar*)b);
451
456
&thd->lex->select_lex.top_join_list,
452
457
lex->query_tables,
453
458
&lex->select_lex.leaf_tables, false))
458
463
Multi-delete can't be constructed over-union => we always have
459
464
single SELECT on top and have to check underlying SELECTs of it
461
lex->select_lex.exclude_from_table_unique_test= true;
466
lex->select_lex.exclude_from_table_unique_test= TRUE;
462
467
/* Fix tables-to-be-deleted-from list to point at opened tables */
463
for (target_tbl= (TableList*) aux_tables;
468
for (target_tbl= (TABLE_LIST*) aux_tables;
465
470
target_tbl= target_tbl->next_local)
467
472
if (!(target_tbl->table= target_tbl->correspondent_table->table))
469
assert(target_tbl->correspondent_table->merge_underlying_list &&
474
DBUG_ASSERT(target_tbl->correspondent_table->merge_underlying_list &&
470
475
target_tbl->correspondent_table->merge_underlying_list->
472
477
my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0), "", "");
478
483
inside subqueries/view.
481
TableList *duplicate;
486
TABLE_LIST *duplicate;
482
487
if ((duplicate= unique_table(thd, target_tbl->correspondent_table,
483
488
lex->query_tables, 0)))
485
490
update_non_unique_table_error(target_tbl->correspondent_table,
486
491
"DELETE", duplicate);
495
multi_delete::multi_delete(TableList *dt, uint32_t num_of_tables_arg)
500
multi_delete::multi_delete(TABLE_LIST *dt, uint num_of_tables_arg)
496
501
: delete_tables(dt), deleted(0), found(0),
497
502
num_of_tables(num_of_tables_arg), error(0),
498
503
do_delete(0), transactional_tables(0), normal_tables(0), error_handled(0)
505
multi_delete::prepare(List<Item> &values __attribute__((unused)),
510
multi_delete::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
512
DBUG_ENTER("multi_delete::prepare");
511
thd->set_proc_info("deleting from main table");
515
thd_proc_info(thd, "deleting from main table");
517
521
multi_delete::initialize_tables(JOIN *join)
520
524
Unique **tempfiles_ptr;
525
DBUG_ENTER("initialize_tables");
523
527
if ((thd->options & OPTION_SAFE_UPDATES) && error_if_full_join(join))
526
530
table_map tables_to_delete_from=0;
527
531
for (walk= delete_tables; walk; walk= walk->next_local)
571
575
for (;walk ;walk= walk->next_local)
573
Table *table=walk->table;
577
TABLE *table=walk->table;
574
578
*tempfiles_ptr++= new Unique (refpos_order_cmp,
575
579
(void *) table->file,
576
580
table->file->ref_length,
577
581
MEM_STRIP_BUF_SIZE);
579
return(thd->is_fatal_error != 0);
583
DBUG_RETURN(thd->is_fatal_error != 0);
586
590
table_being_deleted;
587
591
table_being_deleted= table_being_deleted->next_local)
589
Table *table= table_being_deleted->table;
593
TABLE *table= table_being_deleted->table;
590
594
table->no_keyread=0;
593
for (uint32_t counter= 0; counter < num_of_tables; counter++)
597
for (uint counter= 0; counter < num_of_tables; counter++)
595
599
if (tempfiles[counter])
596
600
delete tempfiles[counter];
601
bool multi_delete::send_data(List<Item> &values __attribute__((unused)))
605
bool multi_delete::send_data(List<Item> &values)
603
607
int secure_counter= delete_while_scanning ? -1 : 0;
604
TableList *del_table;
608
TABLE_LIST *del_table;
609
DBUG_ENTER("multi_delete::send_data");
607
611
for (del_table= delete_tables;
609
613
del_table= del_table->next_local, secure_counter++)
611
Table *table= del_table->table;
615
TABLE *table= del_table->table;
613
617
/* Check if we are using outer join and we didn't find the row */
614
618
if (table->status & (STATUS_NULL_ROW | STATUS_DELETED))
620
624
if (secure_counter < 0)
622
626
/* We are scanning the current table */
623
assert(del_table == table_being_deleted);
627
DBUG_ASSERT(del_table == table_being_deleted);
624
628
table->status|= STATUS_DELETED;
625
629
if (!(error=table->file->ha_delete_row(table->record[0])))
628
632
if (!table->file->has_transactions())
629
thd->transaction.stmt.modified_non_trans_table= true;
633
thd->transaction.stmt.modified_non_trans_table= TRUE;
633
637
table->file->print_error(error,MYF(0));
642
646
error= 1; // Fatal error
651
void multi_delete::send_error(uint32_t errcode,const char *err)
655
void multi_delete::send_error(uint errcode,const char *err)
657
DBUG_ENTER("multi_delete::send_error");
655
659
/* First send error what ever it is ... */
656
660
my_message(errcode, err, MYF(0));
662
666
void multi_delete::abort()
668
DBUG_ENTER("multi_delete::abort");
666
670
/* the error was handled or nothing deleted and no side effects return */
667
671
if (error_handled ||
668
672
(!thd->transaction.stmt.modified_non_trans_table && !deleted))
672
676
If rows from the first table only has been deleted and it is
698
702
thd->binlog_query(THD::ROW_QUERY_TYPE,
699
703
thd->query, thd->query_length,
700
transactional_tables, false);
704
transactional_tables, FALSE);
702
706
thd->transaction.all.modified_non_trans_table= true;
731
735
table_being_deleted= table_being_deleted->next_local, counter++)
733
737
ha_rows last_deleted= deleted;
734
Table *table = table_being_deleted->table;
738
TABLE *table = table_being_deleted->table;
735
739
if (tempfiles[counter]->get(table))
766
770
if (last_deleted != deleted && !table->file->has_transactions())
767
thd->transaction.stmt.modified_non_trans_table= true;
771
thd->transaction.stmt.modified_non_trans_table= TRUE;
768
772
end_read_record(&info);
769
773
if (thd->killed && !local_error)
771
775
if (local_error == -1) // End of file
778
DBUG_RETURN(local_error);
785
789
bool multi_delete::send_eof()
787
791
THD::killed_state killed_status= THD::NOT_KILLED;
788
thd->set_proc_info("deleting from reference tables");
792
thd_proc_info(thd, "deleting from reference tables");
790
794
/* Does deletes for the last n - 1 tables, returns 0 if ok */
791
795
int local_error= do_deletes(); // returns 0 if success
794
798
local_error= local_error || error;
795
799
killed_status= (local_error == 0)? THD::NOT_KILLED : thd->killed;
796
800
/* reset used flags */
797
thd->set_proc_info("end");
801
thd_proc_info(thd, "end");
799
803
if ((local_error == 0) || thd->transaction.stmt.modified_non_trans_table)
804
808
thd->clear_error();
805
809
if (thd->binlog_query(THD::ROW_QUERY_TYPE,
806
810
thd->query, thd->query_length,
807
transactional_tables, false, killed_status) &&
811
transactional_tables, FALSE, killed_status) &&
810
814
local_error=1; // Log write failed: roll back the SQL statement
813
817
if (thd->transaction.stmt.modified_non_trans_table)
814
thd->transaction.all.modified_non_trans_table= true;
818
thd->transaction.all.modified_non_trans_table= TRUE;
816
820
if (local_error != 0)
817
error_handled= true; // to force early leave from ::send_error()
821
error_handled= TRUE; // to force early leave from ::send_error()
819
823
if (!local_error)
841
845
- 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)
848
bool mysql_truncate(THD *thd, TABLE_LIST *table_list, bool dont_send_ok)
846
850
HA_CREATE_INFO create_info;
847
851
char path[FN_REFLEN];
850
uint32_t path_length;
855
DBUG_ENTER("mysql_truncate");
853
memset(&create_info, 0, sizeof(create_info));
857
bzero((char*) &create_info,sizeof(create_info));
854
858
/* If it is a temporary table, close and regenerate it */
855
859
if (!dont_send_ok && (table= find_temporary_table(thd, table_list)))
885
889
table_list->table_name, reg_ext, 0);
887
891
if (!dont_send_ok)
893
enum legacy_db_type table_type;
894
mysql_frm_type(thd, path, &table_type);
895
if (table_type == DB_TYPE_UNKNOWN)
897
my_error(ER_NO_SUCH_TABLE, MYF(0),
898
table_list->db, table_list->table_name);
902
if (!ha_check_storage_engine_flag(ha_resolve_by_legacy_type(thd, table_type),
906
if (lock_and_wait_for_table_name(thd, table_list))
890
910
// Remove the .frm extension AIX 5.2 64-bit compiler bug (BUG#16155): this
891
911
// crashes, replacement works. *(path + path_length - reg_ext_length)=
893
913
path[path_length - reg_ext_length] = 0;
894
pthread_mutex_lock(&LOCK_open);
914
VOID(pthread_mutex_lock(&LOCK_open));
895
915
error= ha_create_table(thd, path, table_list->db, table_list->table_name,
896
916
&create_info, 1);
897
pthread_mutex_unlock(&LOCK_open);
917
VOID(pthread_mutex_unlock(&LOCK_open));
900
920
if (!dont_send_ok)
905
925
TRUNCATE must always be statement-based binlogged (not row-based) so
906
926
we don't test current_stmt_binlog_row_based.
908
write_bin_log(thd, true, thd->query, thd->query_length);
928
write_bin_log(thd, TRUE, thd->query, thd->query_length);
909
929
my_ok(thd); // This should return record count
911
pthread_mutex_lock(&LOCK_open);
931
VOID(pthread_mutex_lock(&LOCK_open));
912
932
unlock_table_name(thd, table_list);
913
pthread_mutex_unlock(&LOCK_open);
933
VOID(pthread_mutex_unlock(&LOCK_open));
917
pthread_mutex_lock(&LOCK_open);
937
VOID(pthread_mutex_lock(&LOCK_open));
918
938
unlock_table_name(thd, table_list);
919
pthread_mutex_unlock(&LOCK_open);
939
VOID(pthread_mutex_unlock(&LOCK_open));
924
944
/* Probably InnoDB table */
925
uint64_t save_options= thd->options;
945
ulonglong save_options= thd->options;
926
946
table_list->lock_type= TL_WRITE;
927
947
thd->options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
928
ha_enable_transaction(thd, false);
948
ha_enable_transaction(thd, FALSE);
929
949
mysql_init_select(thd->lex);
930
950
bool save_binlog_row_based= thd->current_stmt_binlog_row_based;
931
951
thd->clear_current_stmt_binlog_row_based();
932
952
error= mysql_delete(thd, table_list, (COND*) 0, (SQL_LIST*) 0,
933
HA_POS_ERROR, 0L, true);
934
ha_enable_transaction(thd, true);
953
HA_POS_ERROR, LL(0), TRUE);
954
ha_enable_transaction(thd, TRUE);
936
Safety, in case the engine ignored ha_enable_transaction(false)
956
Safety, in case the engine ignored ha_enable_transaction(FALSE)
937
957
above. Also clears thd->transaction.*.
939
959
error= ha_autocommit_or_rollback(thd, error);
941
961
thd->options= save_options;
942
962
thd->current_stmt_binlog_row_based= save_binlog_row_based;