12
12
You should have received a copy of the GNU General Public License
13
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
17
Delete of records and truncate of tables.
19
19
Multi-table deletes were introduced by Monty and Sinisa
22
#include "drizzled/sql_select.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"
28
#include "drizzled/probes.h"
29
#include "drizzled/optimizer/range.h"
30
#include "drizzled/records.h"
31
#include "drizzled/internal/iocache.h"
32
#include "drizzled/transaction_services.h"
33
#include "drizzled/filesort.h"
21
#include <drizzled/server_includes.h>
22
#include <drizzled/sql_select.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>
28
#include "drizzled/probes.h"
30
using namespace drizzled;
39
33
Implement DELETE SQL word.
43
37
end of dispatch_command().
46
bool delete_query(Session *session, TableList *table_list, COND *conds,
47
SQL_LIST *order, ha_rows limit, uint64_t,
40
bool mysql_delete(Session *session, TableList *table_list, COND *conds,
41
SQL_LIST *order, ha_rows limit, uint64_t options,
48
42
bool reset_auto_increment)
52
optimizer::SqlSelect *select= NULL;
54
49
bool using_limit=limit != HA_POS_ERROR;
55
bool transactional_table, const_cond;
50
bool transactional_table, safe_update, const_cond;
56
51
bool const_cond_result;
57
52
ha_rows deleted= 0;
58
53
uint32_t usable_index= MAX_KEY;
59
54
Select_Lex *select_lex= &session->lex->select_lex;
60
Session::killed_state_t killed_status= Session::NOT_KILLED;
55
Session::killed_state killed_status= Session::NOT_KILLED;
62
57
if (session->openTablesLock(table_list))
85
77
List<Item> all_fields;
79
memset(&tables, 0, sizeof(tables));
87
80
tables.table = table;
88
81
tables.alias = table_list->alias;
90
83
if (select_lex->setup_ref_array(session, order->elements) ||
91
84
setup_order(session, select_lex->ref_pointer_array, &tables,
92
fields, all_fields, (Order*) order->first))
95
free_underlaid_joins(session, &session->lex->select_lex);
96
DRIZZLE_DELETE_DONE(1, 0);
85
fields, all_fields, (order_st*) order->first))
88
free_underlaid_joins(session, &session->lex->select_lex);
102
93
const_cond= (!conds || conds->const_item());
94
safe_update=test(session->options & OPTION_SAFE_UPDATES);
95
if (safe_update && const_cond)
97
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
98
ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
104
102
select_lex->no_error= session->lex->ignore;
159
/* Update the table->cursor->stats.records number */
160
table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
157
/* Update the table->file->stats.records number */
158
table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
162
160
table->covering_keys.reset();
163
161
table->quick_keys.reset(); // Can't use 'only index'
164
select= optimizer::make_select(table, 0, 0, conds, 0, &error);
162
select=make_select(table, 0, 0, conds, 0, &error);
167
DRIZZLE_DELETE_DONE(1, 0);
171
if ((select && select->check_quick(session, false, limit)) || !limit)
165
if ((select && select->check_quick(session, safe_update, limit)) || !limit)
174
168
free_underlaid_joins(session, select_lex);
175
169
session->row_count_func= 0;
176
if (session->is_error())
178
170
DRIZZLE_DELETE_DONE(0, 0);
180
172
* Resetting the Diagnostic area to prevent
183
175
session->main_da.reset_diagnostics_area();
184
session->my_ok((ha_rows) session->rowCount());
176
session->my_ok((ha_rows) session->row_count_func);
186
178
We don't need to call reset_auto_increment in this case, because
187
179
mysql_truncate always gives a NULL conds argument, hence we never
194
186
if (table->quick_keys.none())
196
188
session->server_status|=SERVER_QUERY_NO_INDEX_USED;
189
if (safe_update && !using_limit)
192
free_underlaid_joins(session, select_lex);
193
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
194
ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
198
if (options & OPTION_QUICK)
199
(void) table->file->extra(HA_EXTRA_QUICK);
199
201
if (order && order->elements)
201
203
uint32_t length= 0;
202
SortField *sortorder;
204
SORT_FIELD *sortorder;
203
205
ha_rows examined_rows;
205
207
if ((!select || table->quick_keys.none()) && limit != HA_POS_ERROR)
206
usable_index= optimizer::get_index_for_order(table, (Order*)(order->first), limit);
208
usable_index= get_index_for_order(table, (order_st*)(order->first), limit);
208
210
if (usable_index == MAX_KEY)
210
FileSort filesort(*session);
211
table->sort.io_cache= new internal::IO_CACHE;
214
if (not (sortorder= make_unireg_sortorder((Order*) order->first, &length, NULL)) ||
215
(table->sort.found_records = filesort.run(table, sortorder, length,
216
select, HA_POS_ERROR, 1,
217
examined_rows)) == HA_POS_ERROR)
212
table->sort.io_cache= new IO_CACHE;
213
memset(table->sort.io_cache, 0, sizeof(IO_CACHE));
216
if (!(sortorder= make_unireg_sortorder((order_st*) order->first,
218
(table->sort.found_records = filesort(session, table, sortorder, length,
219
select, HA_POS_ERROR, 1,
220
224
free_underlaid_joins(session, &session->lex->select_lex);
222
DRIZZLE_DELETE_DONE(1, 0);
226
228
Filesort has already found and selected the rows we want to delete,
239
241
free_underlaid_joins(session, select_lex);
240
DRIZZLE_DELETE_DONE(1, 0);
244
244
if (usable_index==MAX_KEY)
246
if ((error= info.init_read_record(session,table,select,1,1)))
248
table->print_error(error, MYF(0));
250
free_underlaid_joins(session, select_lex);
245
init_read_record(&info,session,table,select,1,1);
256
if ((error= info.init_read_record_idx(session, table, 1, usable_index)))
258
table->print_error(error, MYF(0));
260
free_underlaid_joins(session, select_lex);
247
init_read_record_idx(&info, session, table, 1, usable_index);
265
249
session->set_proc_info("updating");
251
will_batch= !table->file->start_bulk_delete();
267
254
table->mark_columns_needed_for_delete();
269
while (!(error=info.read_record(&info)) && !session->getKilled() &&
256
while (!(error=info.read_record(&info)) && !session->killed &&
270
257
! session->is_error())
272
259
// session->is_error() is tested to disallow delete row on error
273
260
if (!(select && select->skip_record())&& ! session->is_error() )
275
if (!(error= table->cursor->deleteRecord(table->getInsertRecord())))
262
if (!(error= table->file->ha_delete_row(table->record[0])))
278
265
if (!--limit && using_limit)
314
306
We're really doing a truncate and need to reset the table's
315
307
auto-increment counter.
317
int error2= table->cursor->ha_reset_auto_increment(0);
309
int error2= table->file->ha_reset_auto_increment(0);
319
311
if (error2 && (error2 != HA_ERR_WRONG_COMMAND))
321
table->print_error(error2, MYF(0));
313
table->file->print_error(error2, MYF(0));
327
transactional_table= table->cursor->has_transactions();
321
transactional_table= table->file->has_transactions();
329
323
if (!transactional_table && deleted > 0)
330
session->transaction.stmt.markModifiedNonTransData();
324
session->transaction.stmt.modified_non_trans_table= true;
332
326
/* See similar binlogging code in sql_update.cc, for comments */
333
if ((error < 0) || session->transaction.stmt.hasModifiedNonTransData())
327
if ((error < 0) || session->transaction.stmt.modified_non_trans_table)
335
if (session->transaction.stmt.hasModifiedNonTransData())
336
session->transaction.all.markModifiedNonTransData();
329
if (session->transaction.stmt.modified_non_trans_table)
330
session->transaction.all.modified_non_trans_table= true;
338
assert(transactional_table || !deleted || session->transaction.stmt.hasModifiedNonTransData());
332
assert(transactional_table || !deleted || session->transaction.stmt.modified_non_trans_table);
339
333
free_underlaid_joins(session, select_lex);
341
335
DRIZZLE_DELETE_DONE((error >= 0 || session->is_error()), deleted);
406
402
Optimize delete of all rows by doing a full generate of the table
407
403
This will work even if the .ISM and .ISD tables are destroyed
405
dont_send_ok should be set if:
406
- We should always wants to generate the table (even if the table type
407
normally can't safely do this.
408
- We don't want an ok to be sent to the end user.
409
- We don't want to log the truncate command
410
- If we want to have a name lock on the table on exit without errors.
410
bool truncate(Session& session, TableList *table_list)
413
bool mysql_truncate(Session& session, TableList *table_list, bool dont_send_ok)
415
HA_CREATE_INFO create_info;
416
char path[FN_REFLEN];
413
TransactionServices &transaction_services= TransactionServices::singleton();
419
uint32_t path_length;
420
message::Table tmp_table;
423
memset(&create_info, 0, sizeof(create_info));
424
/* If it is a temporary table, close and regenerate it */
425
if (!dont_send_ok && (table= session.find_temporary_table(table_list)))
427
message::Table::StorageEngine *engine= tmp_table.mutable_engine();
428
plugin::StorageEngine *table_type= table->s->db_type();
429
TableShare *share= table->s;
431
if (!table_type->check_flag(HTON_BIT_CAN_RECREATE))
434
table->file->info(HA_STATUS_AUTO | HA_STATUS_NO_LOCK);
437
Because we bypass "all" of what it takes to create a Table we are on shakey ground in this
438
execution path. Not everything will be properly created in order to use the table
443
assert(share->storage_engine);
444
session.close_temporary_table(table, false, false); // Don't free share
445
assert(share->storage_engine);
446
engine->set_name(share->db_type()->getName());
447
plugin::StorageEngine::createTable(session, share->normalized_path.str,
448
share->db.str, share->table_name.str, create_info,
449
true, tmp_table, false);
450
// We don't need to call invalidate() because this table is not in cache
451
if ((error= (int) !(session.open_temporary_table(share->path.str,
453
share->table_name.str, 1,
455
(void) session.rm_temporary_table(table_type, path);
456
share->free_table_share();
459
If we return here we will not have logged the truncation to the bin log
460
and we will not my_ok() to the client.
465
path_length= build_table_filename(path, sizeof(path), table_list->db,
466
table_list->table_name, 0);
471
pthread_mutex_lock(&LOCK_open); /* Recreate table for truncate */
472
error= plugin::StorageEngine::createTable(session, path, table_list->db, table_list->table_name,
473
create_info, true, tmp_table, false);
474
pthread_mutex_unlock(&LOCK_open);
482
TRUNCATE must always be statement-based binlogged (not row-based) so
483
we don't test current_stmt_binlog_row_based.
485
write_bin_log(&session, session.query, session.query_length);
486
session.my_ok(); // This should return record count
488
pthread_mutex_lock(&LOCK_open); /* For truncate delete from hash when finished */
489
unlock_table_name(table_list);
490
pthread_mutex_unlock(&LOCK_open);
494
pthread_mutex_lock(&LOCK_open); /* For truncate delete from hash when finished */
495
unlock_table_name(table_list);
496
pthread_mutex_unlock(&LOCK_open);
501
/* Probably InnoDB table */
415
502
uint64_t save_options= session.options;
416
503
table_list->lock_type= TL_WRITE;
417
504
session.options&= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
418
init_select(session.lex);
419
error= delete_query(&session, table_list, (COND*) 0, (SQL_LIST*) 0,
505
ha_enable_transaction(&session, false);
506
mysql_init_select(session.lex);
507
error= mysql_delete(&session, table_list, (COND*) 0, (SQL_LIST*) 0,
420
508
HA_POS_ERROR, 0L, true);
509
ha_enable_transaction(&session, true);
422
511
Safety, in case the engine ignored ha_enable_transaction(false)
423
512
above. Also clears session->transaction.*.
425
error= transaction_services.autocommitOrRollback(session, error);
514
error= ha_autocommit_or_rollback(&session, error);
426
516
session.options= save_options;
431
} /* namespace drizzled */