32
32
#include <drizzled/transaction_services.h>
33
33
#include <drizzled/filesort.h>
34
34
#include <drizzled/sql_lex.h>
35
#include <drizzled/diagnostics_area.h>
36
#include <drizzled/statistics_variables.h>
37
#include <drizzled/session/transactions.h>
40
42
Implement DELETE SQL word.
53
55
optimizer::SqlSelect *select= NULL;
55
57
bool using_limit=limit != HA_POS_ERROR;
56
bool transactional_table, const_cond;
57
bool const_cond_result;
58
58
ha_rows deleted= 0;
59
59
uint32_t usable_index= MAX_KEY;
60
60
Select_Lex *select_lex= &session->lex().select_lex;
88
88
tables.table = table;
89
89
tables.alias = table_list->alias;
91
if (select_lex->setup_ref_array(session, order->elements) ||
92
setup_order(session, select_lex->ref_pointer_array, &tables,
93
fields, all_fields, (Order*) order->first))
96
free_underlaid_joins(session, &session->lex().select_lex);
97
DRIZZLE_DELETE_DONE(1, 0);
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))
95
free_underlaid_joins(session, &session->lex().select_lex);
96
DRIZZLE_DELETE_DONE(1, 0);
103
const_cond= (!conds || conds->const_item());
102
bool const_cond= not conds || conds->const_item();
105
104
select_lex->no_error= session->lex().ignore;
107
const_cond_result= const_cond && (!conds || conds->val_int());
106
bool const_cond_result= const_cond && (!conds || conds->val_int());
108
107
if (session->is_error())
110
109
/* Error evaluating val_int(). */
125
124
- There is no limit clause
126
125
- The condition is constant
127
126
- If there is a condition, then it it produces a non-zero value
128
- If the current command is DELETE FROM with no where clause
129
(i.e., not TRUNCATE) then:
130
- We should not be binlogging this statement row-based, and
131
- there should be no delete triggers associated with the table.
133
128
if (!using_limit && const_cond_result)
181
176
* Resetting the Diagnostic area to prevent
184
session->main_da.reset_diagnostics_area();
179
session->main_da().reset_diagnostics_area();
185
180
session->my_ok((ha_rows) session->rowCount());
187
182
We don't need to call reset_auto_increment in this case, because
200
195
if (order && order->elements)
203
SortField *sortorder;
204
ha_rows examined_rows;
206
197
if ((!select || table->quick_keys.none()) && limit != HA_POS_ERROR)
207
198
usable_index= optimizer::get_index_for_order(table, (Order*)(order->first), limit);
209
200
if (usable_index == MAX_KEY)
211
202
FileSort filesort(*session);
212
table->sort.io_cache= new internal::IO_CACHE;
215
if (not (sortorder= make_unireg_sortorder((Order*) order->first, &length, NULL)) ||
216
(table->sort.found_records = filesort.run(table, sortorder, length,
217
select, HA_POS_ERROR, 1,
218
examined_rows)) == HA_POS_ERROR)
203
table->sort.io_cache= new internal::io_cache_st;
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)
221
210
free_underlaid_joins(session, &session->lex().select_lex);
287
276
table->print_error(error,MYF(0));
289
In < 4.0.14 we set the error number to 0 here, but that
290
was not sensible, because then MySQL would not roll back the
291
failed DELETE, and also wrote it to the binlog. For MyISAM
292
tables a DELETE probably never should fail (?), but for
293
InnoDB it can fail in a FOREIGN KEY error or an
294
out-of-tablespace error.
328
transactional_table= table->cursor->has_transactions();
309
bool transactional_table= table->cursor->has_transactions();
330
311
if (!transactional_table && deleted > 0)
331
312
session->transaction.stmt.markModifiedNonTransData();
333
/* See similar binlogging code in sql_update.cc, for comments */
334
314
if ((error < 0) || session->transaction.stmt.hasModifiedNonTransData())
336
316
if (session->transaction.stmt.hasModifiedNonTransData())
347
327
* Resetting the Diagnostic area to prevent
350
session->main_da.reset_diagnostics_area();
330
session->main_da().reset_diagnostics_area();
351
331
session->my_ok((ha_rows) session->rowCount());
353
333
session->status_var.deleted_row_count+= deleted;
372
352
int prepare_delete(Session *session, TableList *table_list, Item **conds)
374
354
Select_Lex *select_lex= &session->lex().select_lex;
376
List<Item> all_fields;
378
355
session->lex().allow_sum_func= 0;
379
if (setup_tables_and_check_access(session, &session->lex().select_lex.context,
380
&session->lex().select_lex.top_join_list,
382
&select_lex->leaf_tables, false) ||
356
if (setup_tables_and_check_access(session, &session->lex().select_lex.context, &select_lex->top_join_list,
357
table_list, &select_lex->leaf_tables, false) ||
383
358
session->setup_conds(table_list, conds))
361
if (unique_table(table_list, table_list->next_global))
386
TableList *duplicate;
387
if ((duplicate= unique_table(table_list, table_list->next_global)))
389
my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->alias);
363
my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->alias);
394
if (select_lex->inner_refs_list.size() &&
395
fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
366
List<Item> all_fields;
367
if (select_lex->inner_refs_list.size() && fix_inner_refs(session, all_fields, select_lex, select_lex->ref_pointer_array))
411
382
bool truncate(Session& session, TableList *table_list)
414
TransactionServices &transaction_services= TransactionServices::singleton();
416
384
uint64_t save_options= session.options;
417
385
table_list->lock_type= TL_WRITE;
418
386
session.options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
419
387
init_select(&session.lex());
420
error= delete_query(&session, table_list, (COND*) 0, (SQL_LIST*) 0,
421
HA_POS_ERROR, 0L, true);
388
int error= delete_query(&session, table_list, (COND*) 0, (SQL_LIST*) 0, HA_POS_ERROR, 0L, true);
423
390
Safety, in case the engine ignored ha_enable_transaction(false)
424
391
above. Also clears session->transaction.*.
426
error= transaction_services.autocommitOrRollback(session, error);
393
error= TransactionServices::autocommitOrRollback(session, error);
427
394
session.options= save_options;