21
21
#include <drizzled/server_includes.h>
22
22
#include <drizzled/sql_select.h>
23
#include <drizzled/drizzled_error_messages.h>
23
#include <drizzled/error.h>
24
#include <drizzled/probes.h>
25
#include <drizzled/sql_parse.h>
26
#include <drizzled/sql_base.h>
27
#include <drizzled/lock.h>
26
30
Implement DELETE SQL word.
30
34
end of dispatch_command().
33
bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
37
bool mysql_delete(Session *session, TableList *table_list, COND *conds,
34
38
SQL_LIST *order, ha_rows limit, uint64_t options,
35
39
bool reset_auto_increment)
38
42
int error, loc_error;
40
44
SQL_SELECT *select=0;
42
46
bool using_limit=limit != HA_POS_ERROR;
43
47
bool transactional_table, safe_update, const_cond;
44
48
bool const_cond_result;
45
49
ha_rows deleted= 0;
46
uint usable_index= MAX_KEY;
47
SELECT_LEX *select_lex= &thd->lex->select_lex;
48
THD::killed_state killed_status= THD::NOT_KILLED;
51
if (open_and_lock_tables(thd, table_list))
53
/* TODO look at this error */
54
if (!(table= table_list->table))
56
my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0), "", "");
59
thd_proc_info(thd, "init");
50
uint32_t usable_index= MAX_KEY;
51
SELECT_LEX *select_lex= &session->lex->select_lex;
52
Session::killed_state killed_status= Session::NOT_KILLED;
55
if (open_and_lock_tables(session, table_list))
58
table= table_list->table;
61
session->set_proc_info("init");
62
if (mysql_prepare_delete(thd, table_list, &conds))
64
if (mysql_prepare_delete(session, table_list, &conds))
65
67
/* check ORDER BY even if it can be ignored */
66
68
if (order && order->elements)
70
72
List<Item> all_fields;
73
75
tables.table = table;
74
76
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,
78
fields, all_fields, (ORDER*) order->first))
78
if (select_lex->setup_ref_array(session, order->elements) ||
79
setup_order(session, select_lex->ref_pointer_array, &tables,
80
fields, all_fields, (order_st*) order->first))
81
free_underlaid_joins(thd, &thd->lex->select_lex);
83
free_underlaid_joins(session, &session->lex->select_lex);
86
88
const_cond= (!conds || conds->const_item());
87
safe_update=test(thd->options & OPTION_SAFE_UPDATES);
89
safe_update=test(session->options & OPTION_SAFE_UPDATES);
88
90
if (safe_update && const_cond)
90
92
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
120
122
- We should not be binlogging this statement row-based, and
121
123
- there should be no delete triggers associated with the table.
123
if (!using_limit && const_cond_result &&
124
!(specialflag & (SPECIAL_NO_NEW_FUNC | SPECIAL_SAFE_MODE)) &&
125
(thd->lex->sql_command == SQLCOM_TRUNCATE ||
126
(!thd->current_stmt_binlog_row_based)))
125
if (!using_limit && const_cond_result)
128
127
/* Update the table->file->stats.records number */
129
128
table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
158
157
select=make_select(table, 0, 0, conds, 0, &error);
161
if ((select && select->check_quick(thd, safe_update, limit)) || !limit)
160
if ((select && select->check_quick(session, safe_update, limit)) || !limit)
164
free_underlaid_joins(thd, select_lex);
165
thd->row_count_func= 0;
163
free_underlaid_joins(session, select_lex);
164
session->row_count_func= 0;
166
165
DRIZZLE_DELETE_END();
167
my_ok(thd, (ha_rows) thd->row_count_func);
166
my_ok(session, (ha_rows) session->row_count_func);
169
168
We don't need to call reset_auto_increment in this case, because
170
169
mysql_truncate always gives a NULL conds argument, hence we never
176
175
/* If running in safe sql mode, don't allow updates without keys */
177
176
if (table->quick_keys.is_clear_all())
179
thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
178
session->server_status|=SERVER_QUERY_NO_INDEX_USED;
180
179
if (safe_update && !using_limit)
183
free_underlaid_joins(thd, select_lex);
182
free_underlaid_joins(session, select_lex);
184
183
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
185
184
ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
192
191
if (order && order->elements)
195
194
SORT_FIELD *sortorder;
196
195
ha_rows examined_rows;
198
197
if ((!select || table->quick_keys.is_clear_all()) && limit != HA_POS_ERROR)
199
usable_index= get_index_for_order(table, (ORDER*)(order->first), limit);
198
usable_index= get_index_for_order(table, (order_st*)(order->first), limit);
201
200
if (usable_index == MAX_KEY)
203
table->sort.io_cache= (IO_CACHE *) my_malloc(sizeof(IO_CACHE),
204
MYF(MY_FAE | MY_ZEROFILL));
206
if (!(sortorder= make_unireg_sortorder((ORDER*) order->first,
202
table->sort.io_cache= new IO_CACHE;
203
memset(table->sort.io_cache, 0, sizeof(IO_CACHE));
206
if (!(sortorder= make_unireg_sortorder((order_st*) order->first,
207
207
&length, NULL)) ||
208
(table->sort.found_records = filesort(thd, table, sortorder, length,
208
(table->sort.found_records = filesort(session, table, sortorder, length,
209
209
select, HA_POS_ERROR, 1,
214
free_underlaid_joins(thd, &thd->lex->select_lex);
214
free_underlaid_joins(session, &session->lex->select_lex);
228
228
if (select && select->quick && select->quick->reset())
231
free_underlaid_joins(thd, select_lex);
231
free_underlaid_joins(session, select_lex);
234
234
if (usable_index==MAX_KEY)
235
init_read_record(&info,thd,table,select,1,1);
235
init_read_record(&info,session,table,select,1,1);
237
init_read_record_idx(&info, thd, table, 1, usable_index);
237
init_read_record_idx(&info, session, table, 1, usable_index);
239
thd_proc_info(thd, "updating");
239
session->set_proc_info("updating");
241
241
will_batch= !table->file->start_bulk_delete();
244
244
table->mark_columns_needed_for_delete();
246
while (!(error=info.read_record(&info)) && !thd->killed &&
246
while (!(error=info.read_record(&info)) && !session->killed &&
247
! session->is_error())
249
// thd->is_error() is tested to disallow delete row on error
250
if (!(select && select->skip_record())&& ! thd->is_error() )
249
// session->is_error() is tested to disallow delete row on error
250
if (!(select && select->skip_record())&& ! session->is_error() )
252
252
if (!(error= table->file->ha_delete_row(table->record[0])))
311
311
transactional_table= table->file->has_transactions();
313
313
if (!transactional_table && deleted > 0)
314
thd->transaction.stmt.modified_non_trans_table= true;
314
session->transaction.stmt.modified_non_trans_table= true;
316
316
/* See similar binlogging code in sql_update.cc, for comments */
317
if ((error < 0) || thd->transaction.stmt.modified_non_trans_table)
317
if ((error < 0) || session->transaction.stmt.modified_non_trans_table)
319
if (mysql_bin_log.is_open())
324
[binlog]: If 'handler::delete_all_rows()' was called and the
325
storage engine does not inject the rows itself, we replicate
326
statement-based; otherwise, 'ha_delete_row()' was used to
327
delete specific rows which we might log row-based.
329
int log_result= thd->binlog_query(THD::ROW_QUERY_TYPE,
330
thd->query, thd->query_length,
331
transactional_table, false, killed_status);
333
if (log_result && transactional_table)
338
if (thd->transaction.stmt.modified_non_trans_table)
339
thd->transaction.all.modified_non_trans_table= true;
319
if (session->transaction.stmt.modified_non_trans_table)
320
session->transaction.all.modified_non_trans_table= true;
341
assert(transactional_table || !deleted || thd->transaction.stmt.modified_non_trans_table);
342
free_underlaid_joins(thd, select_lex);
322
assert(transactional_table || !deleted || session->transaction.stmt.modified_non_trans_table);
323
free_underlaid_joins(session, select_lex);
344
325
DRIZZLE_DELETE_END();
345
if (error < 0 || (thd->lex->ignore && !thd->is_fatal_error))
326
if (error < 0 || (session->lex->ignore && !session->is_fatal_error))
347
thd->row_count_func= deleted;
348
my_ok(thd, (ha_rows) thd->row_count_func);
328
session->row_count_func= deleted;
329
my_ok(session, (ha_rows) session->row_count_func);
350
return(error >= 0 || thd->is_error());
331
return(error >= 0 || session->is_error());
353
334
DRIZZLE_DELETE_END();
371
int mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, Item **conds)
352
int mysql_prepare_delete(Session *session, TableList *table_list, Item **conds)
373
SELECT_LEX *select_lex= &thd->lex->select_lex;
354
SELECT_LEX *select_lex= &session->lex->select_lex;
375
356
List<Item> all_fields;
378
Statement-based replication of DELETE ... LIMIT is not safe as order of
379
rows is not defined, so in mixed mode we go to row-based.
381
Note that we may consider a statement as safe if ORDER BY primary_key
382
is present. However it may confuse users to see very similiar statements
383
replicated differently.
385
if (thd->lex->current_select->select_limit)
387
thd->lex->set_stmt_unsafe();
388
thd->set_current_stmt_binlog_row_based_if_mixed();
390
thd->lex->allow_sum_func= 0;
391
if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
392
&thd->lex->select_lex.top_join_list,
358
session->lex->allow_sum_func= 0;
359
if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
360
&session->lex->select_lex.top_join_list,
394
362
&select_lex->leaf_tables, false) ||
395
setup_conds(thd, table_list, select_lex->leaf_tables, conds))
363
setup_conds(session, table_list, select_lex->leaf_tables, conds))
398
TABLE_LIST *duplicate;
399
if ((duplicate= unique_table(thd, table_list, table_list->next_global, 0)))
366
TableList *duplicate;
367
if ((duplicate= unique_table(session, table_list, table_list->next_global, 0)))
401
369
update_non_unique_table_error(table_list, "DELETE", duplicate);
414
382
/***************************************************************************
415
Delete multiple tables from join
383
Delete multiple tables from join
416
384
***************************************************************************/
418
#define MEM_STRIP_BUF_SIZE current_thd->variables.sortbuff_size
386
#define MEM_STRIP_BUF_SIZE current_session->variables.sortbuff_size
420
388
extern "C" int refpos_order_cmp(void* arg, const void *a,const void *b)
422
390
handler *file= (handler*)arg;
423
return file->cmp_ref((const uchar*)a, (const uchar*)b);
391
return file->cmp_ref((const unsigned char*)a, (const unsigned char*)b);
449
417
lex->query_tables also point on local list of DELETE SELECT_LEX
451
if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
452
&thd->lex->select_lex.top_join_list,
419
if (setup_tables_and_check_access(session, &session->lex->select_lex.context,
420
&session->lex->select_lex.top_join_list,
453
421
lex->query_tables,
454
422
&lex->select_lex.leaf_tables, false))
462
430
lex->select_lex.exclude_from_table_unique_test= true;
463
431
/* Fix tables-to-be-deleted-from list to point at opened tables */
464
for (target_tbl= (TABLE_LIST*) aux_tables;
432
for (target_tbl= (TableList*) aux_tables;
466
434
target_tbl= target_tbl->next_local)
468
if (!(target_tbl->table= target_tbl->correspondent_table->table))
470
assert(target_tbl->correspondent_table->merge_underlying_list &&
471
target_tbl->correspondent_table->merge_underlying_list->
473
my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0), "", "");
436
target_tbl->table= target_tbl->correspondent_table->table;
437
assert(target_tbl->table);
478
440
Check that table from which we delete is not used somewhere
479
441
inside subqueries/view.
482
TABLE_LIST *duplicate;
483
if ((duplicate= unique_table(thd, target_tbl->correspondent_table,
444
TableList *duplicate;
445
if ((duplicate= unique_table(session, target_tbl->correspondent_table,
484
446
lex->query_tables, 0)))
486
448
update_non_unique_table_error(target_tbl->correspondent_table,
496
multi_delete::multi_delete(TABLE_LIST *dt, uint num_of_tables_arg)
458
multi_delete::multi_delete(TableList *dt, uint32_t num_of_tables_arg)
497
459
: delete_tables(dt), deleted(0), found(0),
498
460
num_of_tables(num_of_tables_arg), error(0),
499
461
do_delete(0), transactional_tables(0), normal_tables(0), error_handled(0)
602
bool multi_delete::send_data(List<Item> &values __attribute__((unused)))
563
bool multi_delete::send_data(List<Item> &)
604
565
int secure_counter= delete_while_scanning ? -1 : 0;
605
TABLE_LIST *del_table;
566
TableList *del_table;
608
569
for (del_table= delete_tables;
610
571
del_table= del_table->next_local, secure_counter++)
612
TABLE *table= del_table->table;
573
Table *table= del_table->table;
614
575
/* Check if we are using outer join and we didn't find the row */
615
576
if (table->status & (STATUS_NULL_ROW | STATUS_DELETED))
688
649
assert(error_handled);
692
if (thd->transaction.stmt.modified_non_trans_table)
653
if (session->transaction.stmt.modified_non_trans_table)
695
there is only side effects; to binlog with the error
697
if (mysql_bin_log.is_open())
699
thd->binlog_query(THD::ROW_QUERY_TYPE,
700
thd->query, thd->query_length,
701
transactional_tables, false);
703
thd->transaction.all.modified_non_trans_table= true;
655
session->transaction.all.modified_non_trans_table= true;
742
694
READ_RECORD info;
743
init_read_record(&info,thd,table,NULL,0,1);
695
init_read_record(&info,session,table,NULL,0,1);
745
697
Ignore any rows not found in reference tables as they may already have
746
698
been deleted by foreign key handling
748
700
info.ignore_not_found_rows= 1;
749
701
will_batch= !table->file->start_bulk_delete();
750
while (!(local_error=info.read_record(&info)) && !thd->killed)
702
while (!(local_error=info.read_record(&info)) && !session->killed)
752
704
if ((local_error=table->file->ha_delete_row(table->record[0])))
786
738
bool multi_delete::send_eof()
788
THD::killed_state killed_status= THD::NOT_KILLED;
789
thd_proc_info(thd, "deleting from reference tables");
740
Session::killed_state killed_status= Session::NOT_KILLED;
741
session->set_proc_info("deleting from reference tables");
791
743
/* Does deletes for the last n - 1 tables, returns 0 if ok */
792
744
int local_error= do_deletes(); // returns 0 if success
794
746
/* compute a total error to know if something failed */
795
747
local_error= local_error || error;
796
killed_status= (local_error == 0)? THD::NOT_KILLED : thd->killed;
748
killed_status= (local_error == 0)? Session::NOT_KILLED : session->killed;
797
749
/* reset used flags */
798
thd_proc_info(thd, "end");
750
session->set_proc_info("end");
800
if ((local_error == 0) || thd->transaction.stmt.modified_non_trans_table)
752
if ((local_error == 0) || session->transaction.stmt.modified_non_trans_table)
802
if (mysql_bin_log.is_open())
804
if (local_error == 0)
806
if (thd->binlog_query(THD::ROW_QUERY_TYPE,
807
thd->query, thd->query_length,
808
transactional_tables, false, killed_status) &&
811
local_error=1; // Log write failed: roll back the SQL statement
814
if (thd->transaction.stmt.modified_non_trans_table)
815
thd->transaction.all.modified_non_trans_table= true;
754
if (session->transaction.stmt.modified_non_trans_table)
755
session->transaction.all.modified_non_trans_table= true;
817
757
if (local_error != 0)
818
758
error_handled= true; // to force early leave from ::send_error()
820
760
if (!local_error)
822
thd->row_count_func= deleted;
823
::my_ok(thd, (ha_rows) thd->row_count_func);
762
session->row_count_func= deleted;
763
::my_ok(session, (ha_rows) session->row_count_func);
829
769
/***************************************************************************
831
771
****************************************************************************/
842
782
- If we want to have a name lock on the table on exit without errors.
845
bool mysql_truncate(THD *thd, TABLE_LIST *table_list, bool dont_send_ok)
785
bool mysql_truncate(Session *session, TableList *table_list, bool dont_send_ok)
847
787
HA_CREATE_INFO create_info;
848
788
char path[FN_REFLEN];
791
uint32_t path_length;
854
794
memset(&create_info, 0, sizeof(create_info));
855
795
/* If it is a temporary table, close and regenerate it */
856
if (!dont_send_ok && (table= find_temporary_table(thd, table_list)))
796
if (!dont_send_ok && (table= find_temporary_table(session, table_list)))
858
798
handlerton *table_type= table->s->db_type();
859
799
TABLE_SHARE *share= table->s;
860
bool frm_only= (share->tmp_table == TMP_TABLE_FRM_FILE_ONLY);
862
if (!ha_check_storage_engine_flag(table_type, HTON_CAN_RECREATE))
801
if (!ha_check_storage_engine_flag(table_type, HTON_BIT_CAN_RECREATE))
863
802
goto trunc_by_del;
865
804
table->file->info(HA_STATUS_AUTO | HA_STATUS_NO_LOCK);
867
close_temporary_table(thd, table, 0, 0); // Don't free share
868
ha_create_table(thd, share->normalized_path.str,
806
close_temporary_table(session, table, 0, 0); // Don't free share
807
ha_create_table(session, share->normalized_path.str,
869
808
share->db.str, share->table_name.str, &create_info, 1);
870
809
// We don't need to call invalidate() because this table is not in cache
871
if ((error= (int) !(open_temporary_table(thd, share->path.str,
810
if ((error= (int) !(open_temporary_table(session, share->path.str,
873
812
share->table_name.str, 1,
875
(void) rm_temporary_table(table_type, path, frm_only);
814
(void) rm_temporary_table(table_type, path);
876
815
free_table_share(share);
877
my_free((char*) table,MYF(0));
879
818
If we return here we will not have logged the truncation to the bin log
880
819
and we will not my_ok() to the client.
886
825
table_list->table_name, reg_ext, 0);
888
827
if (!dont_send_ok)
890
enum legacy_db_type table_type;
891
mysql_frm_type(thd, path, &table_type);
892
if (table_type == DB_TYPE_UNKNOWN)
894
my_error(ER_NO_SUCH_TABLE, MYF(0),
895
table_list->db, table_list->table_name);
899
if (!ha_check_storage_engine_flag(ha_resolve_by_legacy_type(thd, table_type),
903
if (lock_and_wait_for_table_name(thd, table_list))
907
830
// Remove the .frm extension AIX 5.2 64-bit compiler bug (BUG#16155): this
908
831
// crashes, replacement works. *(path + path_length - reg_ext_length)=
910
833
path[path_length - reg_ext_length] = 0;
911
VOID(pthread_mutex_lock(&LOCK_open));
912
error= ha_create_table(thd, path, table_list->db, table_list->table_name,
834
pthread_mutex_lock(&LOCK_open);
835
error= ha_create_table(session, path, table_list->db, table_list->table_name,
913
836
&create_info, 1);
914
VOID(pthread_mutex_unlock(&LOCK_open));
837
pthread_mutex_unlock(&LOCK_open);
917
840
if (!dont_send_ok)
922
845
TRUNCATE must always be statement-based binlogged (not row-based) so
923
846
we don't test current_stmt_binlog_row_based.
925
write_bin_log(thd, true, thd->query, thd->query_length);
926
my_ok(thd); // This should return record count
848
write_bin_log(session, true, session->query, session->query_length);
849
my_ok(session); // This should return record count
928
VOID(pthread_mutex_lock(&LOCK_open));
929
unlock_table_name(thd, table_list);
930
VOID(pthread_mutex_unlock(&LOCK_open));
851
pthread_mutex_lock(&LOCK_open);
852
unlock_table_name(session, table_list);
853
pthread_mutex_unlock(&LOCK_open);
934
VOID(pthread_mutex_lock(&LOCK_open));
935
unlock_table_name(thd, table_list);
936
VOID(pthread_mutex_unlock(&LOCK_open));
857
pthread_mutex_lock(&LOCK_open);
858
unlock_table_name(session, table_list);
859
pthread_mutex_unlock(&LOCK_open);
941
864
/* Probably InnoDB table */
942
uint64_t save_options= thd->options;
865
uint64_t save_options= session->options;
943
866
table_list->lock_type= TL_WRITE;
944
thd->options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
945
ha_enable_transaction(thd, false);
946
mysql_init_select(thd->lex);
947
bool save_binlog_row_based= thd->current_stmt_binlog_row_based;
948
thd->clear_current_stmt_binlog_row_based();
949
error= mysql_delete(thd, table_list, (COND*) 0, (SQL_LIST*) 0,
950
HA_POS_ERROR, 0LL, true);
951
ha_enable_transaction(thd, true);
867
session->options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
868
ha_enable_transaction(session, false);
869
mysql_init_select(session->lex);
870
error= mysql_delete(session, table_list, (COND*) 0, (SQL_LIST*) 0,
871
HA_POS_ERROR, 0L, true);
872
ha_enable_transaction(session, true);
953
874
Safety, in case the engine ignored ha_enable_transaction(false)
954
above. Also clears thd->transaction.*.
875
above. Also clears session->transaction.*.
956
error= ha_autocommit_or_rollback(thd, error);
958
thd->options= save_options;
959
thd->current_stmt_binlog_row_based= save_binlog_row_based;
877
error= ha_autocommit_or_rollback(session, error);
879
session->options= save_options;