2001-07-10 14:53:08 +02:00
|
|
|
/* Copyright (C) 2000 MySQL AB
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
|
|
/*
|
2001-09-02 12:47:00 +02:00
|
|
|
Delete of records and truncate of tables.
|
2001-11-22 16:55:18 +01:00
|
|
|
|
2001-09-02 12:47:00 +02:00
|
|
|
Multi-table deletes were introduced by Monty and Sinisa
|
2000-07-31 21:29:14 +02:00
|
|
|
*/
|
|
|
|
|
2001-09-02 12:47:00 +02:00
|
|
|
#include "mysql_priv.h"
|
2006-04-13 22:49:29 +02:00
|
|
|
#ifdef WITH_INNOBASE_STORAGE_ENGINE
|
2002-01-12 14:42:54 +01:00
|
|
|
#include "ha_innodb.h"
|
2006-04-13 22:49:29 +02:00
|
|
|
#endif
|
2001-09-02 12:47:00 +02:00
|
|
|
#include "sql_select.h"
|
2004-09-07 14:29:46 +02:00
|
|
|
#include "sp_head.h"
|
|
|
|
#include "sql_trigger.h"
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
|
2005-08-30 11:39:20 +02:00
|
|
|
SQL_LIST *order, ha_rows limit, ulonglong options,
|
|
|
|
bool reset_auto_increment)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-05-07 14:06:07 +02:00
|
|
|
bool will_batch;
|
|
|
|
int error, loc_error;
|
2000-07-31 21:29:14 +02:00
|
|
|
TABLE *table;
|
2001-09-02 12:47:00 +02:00
|
|
|
SQL_SELECT *select=0;
|
2000-07-31 21:29:14 +02:00
|
|
|
READ_RECORD info;
|
2005-01-27 22:38:56 +01:00
|
|
|
bool using_limit=limit != HA_POS_ERROR;
|
|
|
|
bool transactional_table, safe_update, const_cond;
|
2006-03-29 13:27:36 +02:00
|
|
|
ha_rows deleted= 0;
|
2005-09-30 13:21:37 +02:00
|
|
|
uint usable_index= MAX_KEY;
|
2004-11-11 20:16:46 +01:00
|
|
|
SELECT_LEX *select_lex= &thd->lex->select_lex;
|
2006-01-05 10:52:58 +01:00
|
|
|
bool ha_delete_all_rows= 0;
|
2006-01-09 11:23:15 +01:00
|
|
|
ulonglong const saved_options= thd->options;
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_ENTER("mysql_delete");
|
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
if (open_and_lock_tables(thd, table_list))
|
|
|
|
DBUG_RETURN(TRUE);
|
2004-09-15 22:42:56 +02:00
|
|
|
if (!(table= table_list->table))
|
|
|
|
{
|
|
|
|
my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0),
|
|
|
|
table_list->view_db.str, table_list->view_name.str);
|
2005-05-20 15:14:35 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-09-15 22:42:56 +02:00
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->proc_info="init";
|
|
|
|
table->map=1;
|
2004-04-10 00:14:32 +02:00
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
if (mysql_prepare_delete(thd, table_list, &conds))
|
|
|
|
DBUG_RETURN(TRUE);
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2002-11-20 20:44:32 +01:00
|
|
|
const_cond= (!conds || conds->const_item());
|
|
|
|
safe_update=test(thd->options & OPTION_SAFE_UPDATES);
|
|
|
|
if (safe_update && const_cond)
|
|
|
|
{
|
2004-11-12 13:34:00 +01:00
|
|
|
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
|
|
|
|
ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2002-11-20 20:44:32 +01:00
|
|
|
}
|
|
|
|
|
2005-01-03 22:04:52 +01:00
|
|
|
select_lex->no_error= thd->lex->ignore;
|
2003-11-17 21:45:07 +01:00
|
|
|
|
2004-11-12 15:04:07 +01:00
|
|
|
/*
|
|
|
|
Test if the user wants to delete all rows and deletion doesn't have
|
|
|
|
any side-effects (because of triggers), so we can use optimized
|
|
|
|
handler::delete_all_rows() method.
|
|
|
|
*/
|
2003-05-19 15:35:49 +02:00
|
|
|
if (!using_limit && const_cond && (!conds || conds->val_int()) &&
|
2004-11-12 15:04:07 +01:00
|
|
|
!(specialflag & (SPECIAL_NO_NEW_FUNC | SPECIAL_SAFE_MODE)) &&
|
|
|
|
!(table->triggers && table->triggers->has_delete_triggers()))
|
2001-09-02 12:47:00 +02:00
|
|
|
{
|
2006-02-06 16:33:39 +01:00
|
|
|
/* Update the table->file->records number */
|
|
|
|
table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
|
2005-12-22 06:39:02 +01:00
|
|
|
ha_rows const maybe_deleted= table->file->records;
|
2006-01-09 09:37:21 +01:00
|
|
|
/*
|
2006-03-15 11:21:36 +01:00
|
|
|
If all rows shall be deleted, we (almost) always log this
|
|
|
|
statement-based (see [binlog], below), so we set this flag and
|
|
|
|
test it below.
|
2006-01-09 09:37:21 +01:00
|
|
|
*/
|
2006-01-05 10:52:58 +01:00
|
|
|
ha_delete_all_rows= 1;
|
2001-09-02 12:47:00 +02:00
|
|
|
if (!(error=table->file->delete_all_rows()))
|
|
|
|
{
|
|
|
|
error= -1; // ok
|
2005-12-22 06:39:02 +01:00
|
|
|
deleted= maybe_deleted;
|
2001-09-02 12:47:00 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (error != HA_ERR_WRONG_COMMAND)
|
|
|
|
{
|
|
|
|
table->file->print_error(error,MYF(0));
|
|
|
|
error=0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
/* Handler didn't support fast delete; Delete rows one by one */
|
|
|
|
}
|
|
|
|
|
2006-01-26 18:17:17 +01:00
|
|
|
#ifdef WITH_PARTITION_STORAGE_ENGINE
|
|
|
|
if (prune_partitions(thd, table, conds))
|
|
|
|
{
|
|
|
|
free_underlaid_joins(thd, select_lex);
|
|
|
|
thd->row_count_func= 0;
|
|
|
|
send_ok(thd); // No matching records
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
#endif
|
2006-02-06 16:33:39 +01:00
|
|
|
/* Update the table->file->records number */
|
|
|
|
table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
|
2006-01-26 18:17:17 +01:00
|
|
|
|
2003-10-11 13:06:55 +02:00
|
|
|
table->used_keys.clear_all();
|
|
|
|
table->quick_keys.clear_all(); // Can't use 'only index'
|
2005-03-16 15:11:01 +01:00
|
|
|
select=make_select(table, 0, 0, conds, 0, &error);
|
2000-07-31 21:29:14 +02:00
|
|
|
if (error)
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2003-10-13 14:50:30 +02:00
|
|
|
if ((select && select->check_quick(thd, safe_update, limit)) || !limit)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
delete select;
|
2004-11-11 20:16:46 +01:00
|
|
|
free_underlaid_joins(thd, select_lex);
|
2004-05-04 13:45:20 +02:00
|
|
|
thd->row_count_func= 0;
|
2002-10-02 12:33:08 +02:00
|
|
|
send_ok(thd,0L);
|
2005-08-17 10:00:20 +02:00
|
|
|
/*
|
|
|
|
We don't need to call reset_auto_increment in this case, because
|
|
|
|
mysql_truncate always gives a NULL conds argument, hence we never
|
|
|
|
get here.
|
|
|
|
*/
|
2001-07-16 02:04:30 +02:00
|
|
|
DBUG_RETURN(0); // Nothing to delete
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If running in safe sql mode, don't allow updates without keys */
|
2003-10-11 13:06:55 +02:00
|
|
|
if (table->quick_keys.is_clear_all())
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2003-12-06 23:21:09 +01:00
|
|
|
thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
|
2002-11-15 15:37:44 +01:00
|
|
|
if (safe_update && !using_limit)
|
2000-11-20 01:57:02 +01:00
|
|
|
{
|
|
|
|
delete select;
|
2004-11-11 20:16:46 +01:00
|
|
|
free_underlaid_joins(thd, select_lex);
|
2004-11-12 13:34:00 +01:00
|
|
|
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
|
|
|
|
ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2000-11-20 01:57:02 +01:00
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2000-09-20 03:54:10 +02:00
|
|
|
if (options & OPTION_QUICK)
|
2001-04-11 13:04:03 +02:00
|
|
|
(void) table->file->extra(HA_EXTRA_QUICK);
|
|
|
|
|
2003-12-19 19:16:26 +01:00
|
|
|
if (order && order->elements)
|
2001-04-11 13:04:03 +02:00
|
|
|
{
|
|
|
|
uint length;
|
|
|
|
SORT_FIELD *sortorder;
|
|
|
|
TABLE_LIST tables;
|
|
|
|
List<Item> fields;
|
|
|
|
List<Item> all_fields;
|
2001-05-11 01:08:29 +02:00
|
|
|
ha_rows examined_rows;
|
2001-04-11 13:04:03 +02:00
|
|
|
|
|
|
|
bzero((char*) &tables,sizeof(tables));
|
|
|
|
tables.table = table;
|
2005-02-16 03:45:42 +01:00
|
|
|
tables.alias = table_list->alias;
|
2001-04-11 13:04:03 +02:00
|
|
|
|
2004-11-11 20:16:46 +01:00
|
|
|
if (select_lex->setup_ref_array(thd, order->elements) ||
|
|
|
|
setup_order(thd, select_lex->ref_pointer_array, &tables,
|
2005-09-30 13:21:37 +02:00
|
|
|
fields, all_fields, (ORDER*) order->first))
|
|
|
|
{
|
|
|
|
delete select;
|
|
|
|
free_underlaid_joins(thd, &thd->lex->select_lex);
|
2005-10-19 10:41:30 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2005-09-30 13:21:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!select && limit != HA_POS_ERROR)
|
|
|
|
usable_index= get_index_for_order(table, (ORDER*)(order->first), limit);
|
|
|
|
|
|
|
|
if (usable_index == MAX_KEY)
|
|
|
|
{
|
|
|
|
table->sort.io_cache= (IO_CACHE *) my_malloc(sizeof(IO_CACHE),
|
|
|
|
MYF(MY_FAE | MY_ZEROFILL));
|
|
|
|
|
2005-10-27 14:15:01 +02:00
|
|
|
if (!(sortorder= make_unireg_sortorder((ORDER*) order->first,
|
|
|
|
&length)) ||
|
2003-04-26 14:58:39 +02:00
|
|
|
(table->sort.found_records = filesort(thd, table, sortorder, length,
|
2005-10-27 14:15:01 +02:00
|
|
|
select, HA_POS_ERROR,
|
|
|
|
&examined_rows))
|
2003-04-09 17:22:17 +02:00
|
|
|
== HA_POS_ERROR)
|
2005-09-30 13:21:37 +02:00
|
|
|
{
|
|
|
|
delete select;
|
|
|
|
free_underlaid_joins(thd, &thd->lex->select_lex);
|
2005-10-27 14:15:01 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2005-09-30 13:21:37 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
Filesort has already found and selected the rows we want to delete,
|
|
|
|
so we don't need the where clause
|
|
|
|
*/
|
2001-04-11 13:04:03 +02:00
|
|
|
delete select;
|
2004-11-11 20:16:46 +01:00
|
|
|
free_underlaid_joins(thd, select_lex);
|
2005-09-30 13:21:37 +02:00
|
|
|
select= 0;
|
2001-04-11 13:04:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-15 21:11:58 +01:00
|
|
|
/* If quick select is used, initialize it before retrieving rows. */
|
|
|
|
if (select && select->quick && select->quick->reset())
|
|
|
|
{
|
|
|
|
delete select;
|
2004-11-11 20:16:46 +01:00
|
|
|
free_underlaid_joins(thd, select_lex);
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-03-15 21:11:58 +01:00
|
|
|
}
|
2005-09-30 13:21:37 +02:00
|
|
|
if (usable_index==MAX_KEY)
|
|
|
|
init_read_record(&info,thd,table,select,1,1);
|
|
|
|
else
|
|
|
|
init_read_record_idx(&info, thd, table, 1, usable_index);
|
|
|
|
|
2004-11-11 20:16:46 +01:00
|
|
|
init_ftfuncs(thd, select_lex, 1);
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->proc_info="updating";
|
2005-05-09 10:30:25 +02:00
|
|
|
will_batch= !table->file->start_bulk_delete();
|
2006-01-05 10:52:58 +01:00
|
|
|
|
|
|
|
/*
|
2006-01-09 11:23:15 +01:00
|
|
|
We saved the thread options above before clearing the
|
|
|
|
OPTION_BIN_LOG, and will restore below, effectively disabling the
|
|
|
|
binary log (unless it was already disabled, of course).
|
2006-01-05 10:52:58 +01:00
|
|
|
*/
|
|
|
|
if (ha_delete_all_rows)
|
|
|
|
thd->options&= ~static_cast<ulonglong>(OPTION_BIN_LOG);
|
|
|
|
|
2002-11-30 23:11:22 +01:00
|
|
|
while (!(error=info.read_record(&info)) && !thd->killed &&
|
|
|
|
!thd->net.report_error)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2002-11-30 23:11:22 +01:00
|
|
|
// thd->net.report_error is tested to disallow delete row on error
|
2004-02-02 17:25:39 +01:00
|
|
|
if (!(select && select->skip_record())&& !thd->net.report_error )
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-09-07 14:29:46 +02:00
|
|
|
|
2005-05-24 20:19:33 +02:00
|
|
|
if (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
|
|
|
|
TRG_ACTION_BEFORE, FALSE))
|
|
|
|
{
|
|
|
|
error= 1;
|
|
|
|
break;
|
|
|
|
}
|
2004-09-07 14:29:46 +02:00
|
|
|
|
2005-12-22 06:39:02 +01:00
|
|
|
if (!(error= table->file->ha_delete_row(table->record[0])))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
deleted++;
|
2005-05-24 20:19:33 +02:00
|
|
|
if (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
|
|
|
|
TRG_ACTION_AFTER, FALSE))
|
|
|
|
{
|
|
|
|
error= 1;
|
|
|
|
break;
|
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
if (!--limit && using_limit)
|
|
|
|
{
|
|
|
|
error= -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
table->file->print_error(error,MYF(0));
|
2003-07-08 23:55:07 +02:00
|
|
|
/*
|
|
|
|
In < 4.0.14 we set the error number to 0 here, but that
|
|
|
|
was not sensible, because then MySQL would not roll back the
|
|
|
|
failed DELETE, and also wrote it to the binlog. For MyISAM
|
|
|
|
tables a DELETE probably never should fail (?), but for
|
|
|
|
InnoDB it can fail in a FOREIGN KEY error or an
|
|
|
|
out-of-tablespace error.
|
|
|
|
*/
|
|
|
|
error= 1;
|
2000-07-31 21:29:14 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-08-21 19:06:00 +02:00
|
|
|
else
|
|
|
|
table->file->unlock_row(); // Row failed selection, release lock on it
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2004-03-04 17:16:10 +01:00
|
|
|
if (thd->killed && !error)
|
|
|
|
error= 1; // Aborted
|
2005-05-07 14:06:07 +02:00
|
|
|
if (will_batch && (loc_error= table->file->end_bulk_delete()))
|
|
|
|
{
|
|
|
|
if (error != 1)
|
|
|
|
table->file->print_error(loc_error,MYF(0));
|
|
|
|
error=1;
|
|
|
|
}
|
2005-05-09 10:30:25 +02:00
|
|
|
thd->proc_info= "end";
|
2000-07-31 21:29:14 +02:00
|
|
|
end_read_record(&info);
|
2002-01-16 22:02:26 +01:00
|
|
|
free_io_cache(table); // Will not do any harm
|
2000-09-20 03:54:10 +02:00
|
|
|
if (options & OPTION_QUICK)
|
2001-04-11 13:04:03 +02:00
|
|
|
(void) table->file->extra(HA_EXTRA_NORMAL);
|
2001-09-02 12:47:00 +02:00
|
|
|
|
2005-08-30 11:39:20 +02:00
|
|
|
if (reset_auto_increment && (error < 0))
|
2005-08-17 10:00:20 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
We're really doing a truncate and need to reset the table's
|
|
|
|
auto-increment counter.
|
|
|
|
*/
|
2005-08-30 11:39:20 +02:00
|
|
|
int error2= table->file->reset_auto_increment(0);
|
2005-08-17 10:00:20 +02:00
|
|
|
|
|
|
|
if (error2 && (error2 != HA_ERR_WRONG_COMMAND))
|
|
|
|
{
|
|
|
|
table->file->print_error(error2, MYF(0));
|
2005-08-30 11:39:20 +02:00
|
|
|
error= 1;
|
2005-08-17 10:00:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-02 12:47:00 +02:00
|
|
|
cleanup:
|
2003-05-26 18:10:43 +02:00
|
|
|
/*
|
|
|
|
Invalidate the table in the query cache if something changed. This must
|
|
|
|
be before binlog writing and ha_autocommit_...
|
|
|
|
*/
|
|
|
|
if (deleted)
|
|
|
|
{
|
|
|
|
query_cache_invalidate3(thd, table_list, 1);
|
|
|
|
}
|
|
|
|
|
2004-06-23 12:29:05 +02:00
|
|
|
delete select;
|
2002-11-07 03:02:37 +01:00
|
|
|
transactional_table= table->file->has_transactions();
|
2006-01-05 10:52:58 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Restore the saved value of the OPTION_BIN_LOG bit in the thread
|
|
|
|
options before executing binlog_query() below.
|
|
|
|
*/
|
|
|
|
thd->options|= (saved_options & OPTION_BIN_LOG);
|
|
|
|
|
2005-09-20 17:41:47 +02:00
|
|
|
/* See similar binlogging code in sql_update.cc, for comments */
|
|
|
|
if ((error < 0) || (deleted && !transactional_table))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2000-09-16 03:27:21 +02:00
|
|
|
if (mysql_bin_log.is_open())
|
|
|
|
{
|
2005-09-20 17:41:47 +02:00
|
|
|
if (error < 0)
|
2003-12-16 11:10:50 +01:00
|
|
|
thd->clear_error();
|
2005-12-22 06:39:02 +01:00
|
|
|
|
|
|
|
/*
|
2006-03-15 11:21:36 +01:00
|
|
|
[binlog]: If 'handler::delete_all_rows()' was called and the
|
|
|
|
storage engine does not inject the rows itself, we replicate
|
|
|
|
statement-based; otherwise, 'ha_delete_row()' was used to
|
|
|
|
delete specific rows which we might log row-based.
|
2005-12-22 06:39:02 +01:00
|
|
|
*/
|
|
|
|
THD::enum_binlog_query_type const
|
2006-03-15 11:21:36 +01:00
|
|
|
query_type(ha_delete_all_rows && !table->file->is_injective() ?
|
2006-01-05 10:52:58 +01:00
|
|
|
THD::STMT_QUERY_TYPE :
|
|
|
|
THD::ROW_QUERY_TYPE);
|
2005-12-22 06:39:02 +01:00
|
|
|
int log_result= thd->binlog_query(query_type,
|
|
|
|
thd->query, thd->query_length,
|
|
|
|
transactional_table, FALSE);
|
|
|
|
|
|
|
|
if (log_result && transactional_table)
|
|
|
|
{
|
2000-12-07 13:08:48 +01:00
|
|
|
error=1;
|
2005-12-22 06:39:02 +01:00
|
|
|
}
|
2000-09-16 03:27:21 +02:00
|
|
|
}
|
2005-01-27 22:38:56 +01:00
|
|
|
if (!transactional_table)
|
2000-11-24 00:51:18 +01:00
|
|
|
thd->options|=OPTION_STATUS_NO_TRANS_UPDATE;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2005-12-12 19:11:56 +01:00
|
|
|
free_underlaid_joins(thd, select_lex);
|
2002-11-16 19:19:10 +01:00
|
|
|
if (transactional_table)
|
|
|
|
{
|
|
|
|
if (ha_autocommit_or_rollback(thd,error >= 0))
|
|
|
|
error=1;
|
|
|
|
}
|
2002-11-21 23:33:15 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
if (thd->lock)
|
|
|
|
{
|
|
|
|
mysql_unlock_tables(thd, thd->lock);
|
|
|
|
thd->lock=0;
|
|
|
|
}
|
2004-10-20 03:04:37 +02:00
|
|
|
if (error < 0)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-05-04 13:45:20 +02:00
|
|
|
thd->row_count_func= deleted;
|
2002-10-02 12:33:08 +02:00
|
|
|
send_ok(thd,deleted);
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_PRINT("info",("%d records deleted",deleted));
|
|
|
|
}
|
2004-11-12 13:34:00 +01:00
|
|
|
DBUG_RETURN(error >= 0 || thd->net.report_error);
|
2004-04-10 00:14:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Prepare items in DELETE statement
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mysql_prepare_delete()
|
|
|
|
thd - thread handler
|
2004-07-16 00:15:55 +02:00
|
|
|
table_list - global/local table list
|
2004-04-10 00:14:32 +02:00
|
|
|
conds - conditions
|
|
|
|
|
|
|
|
RETURN VALUE
|
2004-10-20 03:04:37 +02:00
|
|
|
FALSE OK
|
|
|
|
TRUE error
|
2004-04-10 00:14:32 +02:00
|
|
|
*/
|
2004-10-20 03:04:37 +02:00
|
|
|
bool mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, Item **conds)
|
2004-04-10 00:14:32 +02:00
|
|
|
{
|
2004-05-20 01:02:49 +02:00
|
|
|
SELECT_LEX *select_lex= &thd->lex->select_lex;
|
2004-04-12 02:26:32 +02:00
|
|
|
DBUG_ENTER("mysql_prepare_delete");
|
2004-04-10 00:14:32 +02:00
|
|
|
|
2005-10-15 23:32:37 +02:00
|
|
|
thd->lex->allow_sum_func= 0;
|
2005-07-01 06:05:42 +02:00
|
|
|
if (setup_tables(thd, &thd->lex->select_lex.context,
|
2005-08-12 16:57:19 +02:00
|
|
|
&thd->lex->select_lex.top_join_list,
|
2005-07-01 06:05:42 +02:00
|
|
|
table_list, conds, &select_lex->leaf_tables,
|
|
|
|
FALSE) ||
|
2004-09-14 18:28:29 +02:00
|
|
|
setup_conds(thd, table_list, select_lex->leaf_tables, conds) ||
|
2004-05-20 01:02:49 +02:00
|
|
|
setup_ftfuncs(select_lex))
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-07-16 00:15:55 +02:00
|
|
|
if (!table_list->updatable || check_key_in_view(thd, table_list))
|
2004-04-10 00:14:32 +02:00
|
|
|
{
|
2004-07-16 00:15:55 +02:00
|
|
|
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias, "DELETE");
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2004-05-20 01:02:49 +02:00
|
|
|
{
|
2005-08-02 21:54:49 +02:00
|
|
|
TABLE_LIST *duplicate;
|
2005-12-22 13:48:00 +01:00
|
|
|
if ((duplicate= unique_table(thd, table_list, table_list->next_global)))
|
2005-08-02 21:54:49 +02:00
|
|
|
{
|
|
|
|
update_non_unique_table_error(table_list, "DELETE", duplicate);
|
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
2004-05-20 01:02:49 +02:00
|
|
|
}
|
2004-07-16 00:15:55 +02:00
|
|
|
select_lex->fix_prepare_information(thd, conds);
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(FALSE);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-03 16:07:26 +02:00
|
|
|
/***************************************************************************
|
2002-06-11 10:20:31 +02:00
|
|
|
Delete multiple tables from join
|
2001-06-03 16:07:26 +02:00
|
|
|
***************************************************************************/
|
|
|
|
|
2002-06-28 18:30:09 +02:00
|
|
|
#define MEM_STRIP_BUF_SIZE current_thd->variables.sortbuff_size
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2004-05-12 23:38:40 +02:00
|
|
|
extern "C" int refpos_order_cmp(void* arg, const void *a,const void *b)
|
2001-06-07 13:10:58 +02:00
|
|
|
{
|
2004-05-12 23:38:40 +02:00
|
|
|
handler *file= (handler*)arg;
|
|
|
|
return file->cmp_ref((const byte*)a, (const byte*)b);
|
2001-06-07 13:10:58 +02:00
|
|
|
}
|
2001-06-03 16:07:26 +02:00
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
/*
|
|
|
|
make delete specific preparation and checks after opening tables
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mysql_multi_delete_prepare()
|
|
|
|
thd thread handler
|
|
|
|
|
|
|
|
RETURN
|
2004-10-20 03:04:37 +02:00
|
|
|
FALSE OK
|
|
|
|
TRUE Error
|
2004-07-16 00:15:55 +02:00
|
|
|
*/
|
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
bool mysql_multi_delete_prepare(THD *thd)
|
2004-07-16 00:15:55 +02:00
|
|
|
{
|
|
|
|
LEX *lex= thd->lex;
|
|
|
|
TABLE_LIST *aux_tables= (TABLE_LIST *)lex->auxilliary_table_list.first;
|
|
|
|
TABLE_LIST *target_tbl;
|
|
|
|
DBUG_ENTER("mysql_multi_delete_prepare");
|
|
|
|
|
|
|
|
/*
|
|
|
|
setup_tables() need for VIEWs. JOIN::prepare() will not do it second
|
|
|
|
time.
|
|
|
|
|
|
|
|
lex->query_tables also point on local list of DELETE SELECT_LEX
|
|
|
|
*/
|
2005-07-01 06:05:42 +02:00
|
|
|
if (setup_tables(thd, &thd->lex->select_lex.context,
|
2005-08-12 16:57:19 +02:00
|
|
|
&thd->lex->select_lex.top_join_list,
|
2005-07-01 06:05:42 +02:00
|
|
|
lex->query_tables, &lex->select_lex.where,
|
2005-05-30 18:54:37 +02:00
|
|
|
&lex->select_lex.leaf_tables, FALSE))
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-07-16 00:15:55 +02:00
|
|
|
|
2005-03-28 14:13:31 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Multi-delete can't be constructed over-union => we always have
|
|
|
|
single SELECT on top and have to check underlying SELECTs of it
|
|
|
|
*/
|
|
|
|
lex->select_lex.exclude_from_table_unique_test= TRUE;
|
2004-07-16 00:15:55 +02:00
|
|
|
/* Fix tables-to-be-deleted-from list to point at opened tables */
|
|
|
|
for (target_tbl= (TABLE_LIST*) aux_tables;
|
|
|
|
target_tbl;
|
|
|
|
target_tbl= target_tbl->next_local)
|
|
|
|
{
|
2005-03-28 14:13:31 +02:00
|
|
|
if (!(target_tbl->table= target_tbl->correspondent_table->table))
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(target_tbl->correspondent_table->view &&
|
2005-10-27 23:18:23 +02:00
|
|
|
target_tbl->correspondent_table->merge_underlying_list &&
|
|
|
|
target_tbl->correspondent_table->merge_underlying_list->
|
|
|
|
next_local);
|
2005-03-28 14:13:31 +02:00
|
|
|
my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0),
|
|
|
|
target_tbl->correspondent_table->view_db.str,
|
|
|
|
target_tbl->correspondent_table->view_name.str);
|
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
if (!target_tbl->correspondent_table->updatable ||
|
|
|
|
check_key_in_view(thd, target_tbl->correspondent_table))
|
|
|
|
{
|
2004-11-13 18:35:51 +01:00
|
|
|
my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
|
2005-01-06 12:00:13 +01:00
|
|
|
target_tbl->table_name, "DELETE");
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-07-16 00:15:55 +02:00
|
|
|
}
|
|
|
|
/*
|
2005-03-28 14:13:31 +02:00
|
|
|
Check that table from which we delete is not used somewhere
|
|
|
|
inside subqueries/view.
|
2004-07-16 00:15:55 +02:00
|
|
|
*/
|
|
|
|
{
|
2005-08-02 21:54:49 +02:00
|
|
|
TABLE_LIST *duplicate;
|
2005-12-22 13:48:00 +01:00
|
|
|
if ((duplicate= unique_table(thd, target_tbl->correspondent_table,
|
2005-08-02 21:54:49 +02:00
|
|
|
lex->query_tables)))
|
|
|
|
{
|
|
|
|
update_non_unique_table_error(target_tbl->correspondent_table,
|
|
|
|
"DELETE", duplicate);
|
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
2004-07-16 00:15:55 +02:00
|
|
|
}
|
|
|
|
}
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(FALSE);
|
2004-07-16 00:15:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-09 22:23:56 +02:00
|
|
|
multi_delete::multi_delete(TABLE_LIST *dt, uint num_of_tables_arg)
|
|
|
|
: delete_tables(dt), deleted(0), found(0),
|
2002-11-16 19:19:10 +01:00
|
|
|
num_of_tables(num_of_tables_arg), error(0),
|
2005-01-27 22:38:56 +01:00
|
|
|
do_delete(0), transactional_tables(0), normal_tables(0)
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2005-05-30 19:48:40 +02:00
|
|
|
tempfiles= (Unique **) sql_calloc(sizeof(Unique *) * num_of_tables);
|
2001-06-15 04:03:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2002-05-08 22:14:40 +02:00
|
|
|
multi_delete::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
|
2001-06-15 04:03:15 +02:00
|
|
|
{
|
|
|
|
DBUG_ENTER("multi_delete::prepare");
|
2002-05-08 22:14:40 +02:00
|
|
|
unit= u;
|
2002-11-29 15:40:18 +01:00
|
|
|
do_delete= 1;
|
2001-06-15 04:03:15 +02:00
|
|
|
thd->proc_info="deleting from main table";
|
2001-06-03 16:07:26 +02:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
2001-07-01 12:20:53 +02:00
|
|
|
|
2002-11-29 15:40:18 +01:00
|
|
|
bool
|
2001-07-01 12:20:53 +02:00
|
|
|
multi_delete::initialize_tables(JOIN *join)
|
|
|
|
{
|
2001-07-10 14:53:08 +02:00
|
|
|
TABLE_LIST *walk;
|
2002-11-29 15:40:18 +01:00
|
|
|
Unique **tempfiles_ptr;
|
|
|
|
DBUG_ENTER("initialize_tables");
|
|
|
|
|
|
|
|
if ((thd->options & OPTION_SAFE_UPDATES) && error_if_full_join(join))
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
|
2001-07-10 14:53:08 +02:00
|
|
|
table_map tables_to_delete_from=0;
|
2004-07-16 00:15:55 +02:00
|
|
|
for (walk= delete_tables; walk; walk= walk->next_local)
|
2001-07-10 14:53:08 +02:00
|
|
|
tables_to_delete_from|= walk->table->map;
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-07-10 14:53:08 +02:00
|
|
|
walk= delete_tables;
|
2005-05-30 19:48:40 +02:00
|
|
|
delete_while_scanning= 1;
|
2001-07-01 12:20:53 +02:00
|
|
|
for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
|
|
|
|
tab < end;
|
|
|
|
tab++)
|
|
|
|
{
|
2001-07-10 14:53:08 +02:00
|
|
|
if (tab->table->map & tables_to_delete_from)
|
2001-07-01 12:20:53 +02:00
|
|
|
{
|
2001-07-10 14:53:08 +02:00
|
|
|
/* We are going to delete from this table */
|
2002-06-11 21:45:51 +02:00
|
|
|
TABLE *tbl=walk->table=tab->table;
|
2004-07-16 00:15:55 +02:00
|
|
|
walk= walk->next_local;
|
2002-06-12 22:54:52 +02:00
|
|
|
/* Don't use KEYREAD optimization on this table */
|
2002-06-11 21:45:51 +02:00
|
|
|
tbl->no_keyread=1;
|
2004-09-19 13:15:01 +02:00
|
|
|
/* Don't use record cache */
|
|
|
|
tbl->no_cache= 1;
|
2003-10-11 13:06:55 +02:00
|
|
|
tbl->used_keys.clear_all();
|
2002-11-07 03:02:37 +01:00
|
|
|
if (tbl->file->has_transactions())
|
2005-01-27 22:38:56 +01:00
|
|
|
transactional_tables= 1;
|
2002-11-07 03:02:37 +01:00
|
|
|
else
|
|
|
|
normal_tables= 1;
|
2001-07-01 12:20:53 +02:00
|
|
|
}
|
2005-05-30 19:48:40 +02:00
|
|
|
else if ((tab->type != JT_SYSTEM && tab->type != JT_CONST) &&
|
|
|
|
walk == delete_tables)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
We are not deleting from the table we are scanning. In this
|
|
|
|
case send_data() shouldn't delete any rows a we may touch
|
|
|
|
the rows in the deleted table many times
|
|
|
|
*/
|
|
|
|
delete_while_scanning= 0;
|
|
|
|
}
|
2001-07-01 12:20:53 +02:00
|
|
|
}
|
2002-07-31 18:53:06 +02:00
|
|
|
walk= delete_tables;
|
2002-11-29 15:40:18 +01:00
|
|
|
tempfiles_ptr= tempfiles;
|
2005-05-30 19:48:40 +02:00
|
|
|
if (delete_while_scanning)
|
|
|
|
{
|
|
|
|
table_being_deleted= delete_tables;
|
|
|
|
walk= walk->next_local;
|
|
|
|
}
|
|
|
|
for (;walk ;walk= walk->next_local)
|
2002-07-31 18:53:06 +02:00
|
|
|
{
|
|
|
|
TABLE *table=walk->table;
|
2004-05-12 23:38:40 +02:00
|
|
|
*tempfiles_ptr++= new Unique (refpos_order_cmp,
|
|
|
|
(void *) table->file,
|
2002-11-29 15:40:18 +01:00
|
|
|
table->file->ref_length,
|
|
|
|
MEM_STRIP_BUF_SIZE);
|
2002-07-31 18:53:06 +02:00
|
|
|
}
|
2003-08-26 11:51:09 +02:00
|
|
|
init_ftfuncs(thd, thd->lex->current_select, 1);
|
2003-01-30 21:15:44 +01:00
|
|
|
DBUG_RETURN(thd->is_fatal_error != 0);
|
2001-07-01 12:20:53 +02:00
|
|
|
}
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2001-07-10 14:53:08 +02:00
|
|
|
|
2001-06-03 16:07:26 +02:00
|
|
|
multi_delete::~multi_delete()
|
|
|
|
{
|
2004-07-16 00:15:55 +02:00
|
|
|
for (table_being_deleted= delete_tables;
|
|
|
|
table_being_deleted;
|
|
|
|
table_being_deleted= table_being_deleted->next_local)
|
2002-01-12 18:51:10 +01:00
|
|
|
{
|
2005-05-30 19:48:40 +02:00
|
|
|
TABLE *table= table_being_deleted->table;
|
|
|
|
free_io_cache(table); // Alloced by unique
|
|
|
|
table->no_keyread=0;
|
2002-01-12 18:51:10 +01:00
|
|
|
}
|
2001-07-10 14:53:08 +02:00
|
|
|
|
2005-05-30 19:48:40 +02:00
|
|
|
for (uint counter= 0; counter < num_of_tables; counter++)
|
2001-06-15 04:03:15 +02:00
|
|
|
{
|
2001-06-03 16:07:26 +02:00
|
|
|
if (tempfiles[counter])
|
2001-06-15 04:03:15 +02:00
|
|
|
delete tempfiles[counter];
|
|
|
|
}
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
|
|
|
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2001-06-03 16:07:26 +02:00
|
|
|
bool multi_delete::send_data(List<Item> &values)
|
|
|
|
{
|
2005-05-30 19:48:40 +02:00
|
|
|
int secure_counter= delete_while_scanning ? -1 : 0;
|
|
|
|
TABLE_LIST *del_table;
|
2002-01-29 17:32:16 +01:00
|
|
|
DBUG_ENTER("multi_delete::send_data");
|
|
|
|
|
2005-05-30 19:48:40 +02:00
|
|
|
for (del_table= delete_tables;
|
|
|
|
del_table;
|
|
|
|
del_table= del_table->next_local, secure_counter++)
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2005-05-30 19:48:40 +02:00
|
|
|
TABLE *table= del_table->table;
|
2001-06-15 04:03:15 +02:00
|
|
|
|
|
|
|
/* Check if we are using outer join and we didn't find the row */
|
|
|
|
if (table->status & (STATUS_NULL_ROW | STATUS_DELETED))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
table->file->position(table->record[0]);
|
2003-12-12 22:14:59 +01:00
|
|
|
found++;
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-06-15 04:03:15 +02:00
|
|
|
if (secure_counter < 0)
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2005-05-30 19:48:40 +02:00
|
|
|
/* We are scanning the current table */
|
|
|
|
DBUG_ASSERT(del_table == table_being_deleted);
|
2005-05-24 20:19:33 +02:00
|
|
|
if (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
|
|
|
|
TRG_ACTION_BEFORE, FALSE))
|
|
|
|
DBUG_RETURN(1);
|
2001-06-15 04:03:15 +02:00
|
|
|
table->status|= STATUS_DELETED;
|
2005-12-22 06:39:02 +01:00
|
|
|
if (!(error=table->file->ha_delete_row(table->record[0])))
|
2005-05-24 20:19:33 +02:00
|
|
|
{
|
2001-06-15 04:03:15 +02:00
|
|
|
deleted++;
|
2005-05-24 20:19:33 +02:00
|
|
|
if (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
|
|
|
|
TRG_ACTION_AFTER, FALSE))
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
2005-05-30 19:48:40 +02:00
|
|
|
else
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2001-06-15 04:03:15 +02:00
|
|
|
table->file->print_error(error,MYF(0));
|
2002-01-29 17:32:16 +01:00
|
|
|
DBUG_RETURN(1);
|
2001-06-15 04:03:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-09-15 15:22:34 +02:00
|
|
|
error=tempfiles[secure_counter]->unique_add((char*) table->file->ref);
|
2001-06-15 04:03:15 +02:00
|
|
|
if (error)
|
|
|
|
{
|
2005-05-30 19:48:40 +02:00
|
|
|
error= 1; // Fatal error
|
2002-01-29 17:32:16 +01:00
|
|
|
DBUG_RETURN(1);
|
2001-06-15 04:03:15 +02:00
|
|
|
}
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
|
|
|
}
|
2002-01-29 17:32:16 +01:00
|
|
|
DBUG_RETURN(0);
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
|
|
|
|
2004-03-04 17:16:10 +01:00
|
|
|
|
2001-06-03 16:07:26 +02:00
|
|
|
void multi_delete::send_error(uint errcode,const char *err)
|
|
|
|
{
|
2002-01-29 17:32:16 +01:00
|
|
|
DBUG_ENTER("multi_delete::send_error");
|
|
|
|
|
2001-06-15 04:03:15 +02:00
|
|
|
/* First send error what ever it is ... */
|
2004-10-20 03:04:37 +02:00
|
|
|
my_message(errcode, err, MYF(0));
|
2001-10-04 20:52:41 +02:00
|
|
|
|
2001-06-15 04:03:15 +02:00
|
|
|
/* If nothing deleted return */
|
|
|
|
if (!deleted)
|
2002-01-29 17:32:16 +01:00
|
|
|
DBUG_VOID_RETURN;
|
2002-01-16 22:02:26 +01:00
|
|
|
|
2002-06-11 10:20:31 +02:00
|
|
|
/* Something already deleted so we have to invalidate cache */
|
2002-04-28 23:33:52 +02:00
|
|
|
query_cache_invalidate3(thd, delete_tables, 1);
|
|
|
|
|
2001-06-15 04:03:15 +02:00
|
|
|
/*
|
2002-01-16 22:02:26 +01:00
|
|
|
If rows from the first table only has been deleted and it is
|
|
|
|
transactional, just do rollback.
|
2001-06-15 04:03:15 +02:00
|
|
|
The same if all tables are transactional, regardless of where we are.
|
|
|
|
In all other cases do attempt deletes ...
|
|
|
|
*/
|
2005-05-30 19:48:40 +02:00
|
|
|
if ((table_being_deleted == delete_tables &&
|
|
|
|
table_being_deleted->table->file->has_transactions()) ||
|
|
|
|
!normal_tables)
|
2001-10-19 16:43:30 +02:00
|
|
|
ha_rollback_stmt(thd);
|
2001-06-03 16:07:26 +02:00
|
|
|
else if (do_delete)
|
2002-01-29 17:32:16 +01:00
|
|
|
{
|
2005-05-30 19:48:40 +02:00
|
|
|
/*
|
|
|
|
We have to execute the recorded do_deletes() and write info into the
|
|
|
|
error log
|
|
|
|
*/
|
|
|
|
error= 1;
|
|
|
|
send_eof();
|
2002-01-29 17:32:16 +01:00
|
|
|
}
|
|
|
|
DBUG_VOID_RETURN;
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
|
|
|
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2001-12-13 01:31:19 +01:00
|
|
|
/*
|
|
|
|
Do delete from other tables.
|
|
|
|
Returns values:
|
|
|
|
0 ok
|
|
|
|
1 error
|
|
|
|
*/
|
|
|
|
|
2005-05-30 19:48:40 +02:00
|
|
|
int multi_delete::do_deletes()
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2005-05-07 14:06:07 +02:00
|
|
|
int local_error= 0, counter= 0, error;
|
|
|
|
bool will_batch;
|
2003-02-17 01:14:37 +01:00
|
|
|
DBUG_ENTER("do_deletes");
|
2005-05-30 19:48:40 +02:00
|
|
|
DBUG_ASSERT(do_delete);
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2005-05-30 19:48:40 +02:00
|
|
|
do_delete= 0; // Mark called
|
2003-12-12 22:14:59 +01:00
|
|
|
if (!found)
|
2003-12-11 23:55:48 +01:00
|
|
|
DBUG_RETURN(0);
|
2005-05-30 19:48:40 +02:00
|
|
|
|
|
|
|
table_being_deleted= (delete_while_scanning ? delete_tables->next_local :
|
|
|
|
delete_tables);
|
|
|
|
|
|
|
|
for (; table_being_deleted;
|
2004-07-16 00:15:55 +02:00
|
|
|
table_being_deleted= table_being_deleted->next_local, counter++)
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2001-06-15 04:03:15 +02:00
|
|
|
TABLE *table = table_being_deleted->table;
|
|
|
|
if (tempfiles[counter]->get(table))
|
|
|
|
{
|
2002-11-07 02:54:00 +01:00
|
|
|
local_error=1;
|
2001-06-15 04:03:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
READ_RECORD info;
|
2003-02-17 01:14:37 +01:00
|
|
|
init_read_record(&info,thd,table,NULL,0,1);
|
|
|
|
/*
|
|
|
|
Ignore any rows not found in reference tables as they may already have
|
|
|
|
been deleted by foreign key handling
|
|
|
|
*/
|
|
|
|
info.ignore_not_found_rows= 1;
|
2005-05-09 10:30:25 +02:00
|
|
|
will_batch= !table->file->start_bulk_delete();
|
2002-11-07 11:49:01 +01:00
|
|
|
while (!(local_error=info.read_record(&info)) && !thd->killed)
|
2001-06-15 04:03:15 +02:00
|
|
|
{
|
2005-05-24 20:19:33 +02:00
|
|
|
if (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
|
|
|
|
TRG_ACTION_BEFORE, FALSE))
|
|
|
|
{
|
|
|
|
local_error= 1;
|
|
|
|
break;
|
|
|
|
}
|
2005-12-22 06:39:02 +01:00
|
|
|
if ((local_error=table->file->ha_delete_row(table->record[0])))
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2002-11-07 02:54:00 +01:00
|
|
|
table->file->print_error(local_error,MYF(0));
|
2001-06-15 04:03:15 +02:00
|
|
|
break;
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
2001-12-13 01:31:19 +01:00
|
|
|
deleted++;
|
2005-05-24 20:19:33 +02:00
|
|
|
if (table->triggers &&
|
|
|
|
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
|
|
|
|
TRG_ACTION_AFTER, FALSE))
|
|
|
|
{
|
|
|
|
local_error= 1;
|
|
|
|
break;
|
|
|
|
}
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
2005-05-09 10:30:25 +02:00
|
|
|
if (will_batch && (error= table->file->end_bulk_delete()))
|
2005-05-07 14:06:07 +02:00
|
|
|
{
|
|
|
|
if (!local_error)
|
|
|
|
{
|
|
|
|
local_error= error;
|
|
|
|
table->file->print_error(local_error,MYF(0));
|
|
|
|
}
|
|
|
|
}
|
2001-06-15 04:03:15 +02:00
|
|
|
end_read_record(&info);
|
2004-03-04 17:16:10 +01:00
|
|
|
if (thd->killed && !local_error)
|
|
|
|
local_error= 1;
|
2002-11-07 02:54:00 +01:00
|
|
|
if (local_error == -1) // End of file
|
|
|
|
local_error = 0;
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
2003-02-17 01:14:37 +01:00
|
|
|
DBUG_RETURN(local_error);
|
2001-06-03 16:07:26 +02:00
|
|
|
}
|
|
|
|
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2002-01-16 22:02:26 +01:00
|
|
|
/*
|
2002-06-11 10:20:31 +02:00
|
|
|
Send ok to the client
|
|
|
|
|
2002-01-16 22:02:26 +01:00
|
|
|
return: 0 sucess
|
|
|
|
1 error
|
|
|
|
*/
|
|
|
|
|
2001-06-03 16:07:26 +02:00
|
|
|
bool multi_delete::send_eof()
|
|
|
|
{
|
2002-01-16 22:02:26 +01:00
|
|
|
thd->proc_info="deleting from reference tables";
|
2001-10-25 13:41:49 +02:00
|
|
|
|
|
|
|
/* Does deletes for the last n - 1 tables, returns 0 if ok */
|
2005-05-30 19:48:40 +02:00
|
|
|
int local_error= do_deletes(); // returns 0 if success
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2005-09-20 17:41:47 +02:00
|
|
|
/* compute a total error to know if something failed */
|
|
|
|
local_error= local_error || error;
|
|
|
|
|
2001-10-04 20:52:41 +02:00
|
|
|
/* reset used flags */
|
2001-06-03 16:07:26 +02:00
|
|
|
thd->proc_info="end";
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2003-05-26 19:48:40 +02:00
|
|
|
/*
|
|
|
|
We must invalidate the query cache before binlog writing and
|
|
|
|
ha_autocommit_...
|
|
|
|
*/
|
2003-05-26 18:10:43 +02:00
|
|
|
if (deleted)
|
2003-12-21 01:07:45 +01:00
|
|
|
{
|
2003-05-26 18:10:43 +02:00
|
|
|
query_cache_invalidate3(thd, delete_tables, 1);
|
2003-12-21 01:07:45 +01:00
|
|
|
}
|
2001-06-15 04:03:15 +02:00
|
|
|
|
2005-09-20 17:41:47 +02:00
|
|
|
if ((local_error == 0) || (deleted && normal_tables))
|
2001-06-03 16:07:26 +02:00
|
|
|
{
|
2001-12-13 01:31:19 +01:00
|
|
|
if (mysql_bin_log.is_open())
|
|
|
|
{
|
2005-09-20 17:41:47 +02:00
|
|
|
if (local_error == 0)
|
2003-12-16 11:10:50 +01:00
|
|
|
thd->clear_error();
|
2005-12-22 06:39:02 +01:00
|
|
|
if (thd->binlog_query(THD::ROW_QUERY_TYPE,
|
|
|
|
thd->query, thd->query_length,
|
|
|
|
transactional_tables, FALSE) &&
|
|
|
|
!normal_tables)
|
|
|
|
{
|
2002-11-07 02:54:00 +01:00
|
|
|
local_error=1; // Log write failed: roll back the SQL statement
|
2005-12-22 06:39:02 +01:00
|
|
|
}
|
2001-12-13 01:31:19 +01:00
|
|
|
}
|
2005-01-27 22:38:56 +01:00
|
|
|
if (!transactional_tables)
|
2002-11-29 15:40:18 +01:00
|
|
|
thd->options|=OPTION_STATUS_NO_TRANS_UPDATE;
|
2002-04-29 11:24:14 +02:00
|
|
|
}
|
2005-01-27 22:38:56 +01:00
|
|
|
/* Commit or rollback the current SQL statement */
|
2002-11-29 15:40:18 +01:00
|
|
|
if (transactional_tables)
|
|
|
|
if (ha_autocommit_or_rollback(thd,local_error > 0))
|
|
|
|
local_error=1;
|
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
if (!local_error)
|
2004-05-04 13:45:20 +02:00
|
|
|
{
|
|
|
|
thd->row_count_func= deleted;
|
2002-11-21 14:56:48 +01:00
|
|
|
::send_ok(thd, deleted);
|
2004-05-04 13:45:20 +02:00
|
|
|
}
|
2001-06-03 16:07:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2001-09-02 12:47:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
2002-06-11 10:20:31 +02:00
|
|
|
TRUNCATE TABLE
|
2001-09-02 12:47:00 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Optimize delete of all rows by doing a full generate of the table
|
|
|
|
This will work even if the .ISM and .ISD tables are destroyed
|
|
|
|
|
|
|
|
dont_send_ok should be set if:
|
|
|
|
- We should always wants to generate the table (even if the table type
|
|
|
|
normally can't safely do this.
|
|
|
|
- We don't want an ok to be sent to the end user.
|
|
|
|
- We don't want to log the truncate command
|
2001-09-03 04:16:15 +02:00
|
|
|
- If we want to have a name lock on the table on exit without errors.
|
2001-09-02 12:47:00 +02:00
|
|
|
*/
|
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
bool mysql_truncate(THD *thd, TABLE_LIST *table_list, bool dont_send_ok)
|
2001-09-02 12:47:00 +02:00
|
|
|
{
|
|
|
|
HA_CREATE_INFO create_info;
|
|
|
|
char path[FN_REFLEN];
|
2005-11-23 21:45:02 +01:00
|
|
|
TABLE *table;
|
2004-10-20 03:04:37 +02:00
|
|
|
bool error;
|
2006-01-19 03:56:06 +01:00
|
|
|
uint closed_log_tables= 0, lock_logger= 0;
|
2005-12-31 06:01:26 +01:00
|
|
|
uint path_length;
|
2001-09-02 12:47:00 +02:00
|
|
|
DBUG_ENTER("mysql_truncate");
|
|
|
|
|
2004-08-23 16:15:57 +02:00
|
|
|
bzero((char*) &create_info,sizeof(create_info));
|
2001-09-02 12:47:00 +02:00
|
|
|
/* If it is a temporary table, close and regenerate it */
|
2005-11-23 21:45:02 +01:00
|
|
|
if (!dont_send_ok && (table= find_temporary_table(thd, table_list)))
|
2001-09-02 12:47:00 +02:00
|
|
|
{
|
2005-12-21 19:18:40 +01:00
|
|
|
handlerton *table_type= table->s->db_type;
|
2005-11-23 21:45:02 +01:00
|
|
|
TABLE_SHARE *share= table->s;
|
2005-10-01 01:26:48 +02:00
|
|
|
if (!ha_check_storage_engine_flag(table_type, HTON_CAN_RECREATE))
|
2005-08-29 17:01:46 +02:00
|
|
|
goto trunc_by_del;
|
2005-11-23 21:45:02 +01:00
|
|
|
|
|
|
|
table->file->info(HA_STATUS_AUTO | HA_STATUS_NO_LOCK);
|
|
|
|
|
|
|
|
close_temporary_table(thd, table, 0, 0); // Don't free share
|
|
|
|
ha_create_table(thd, share->normalized_path.str,
|
|
|
|
share->db.str, share->table_name.str, &create_info, 1);
|
2001-12-02 13:34:01 +01:00
|
|
|
// We don't need to call invalidate() because this table is not in cache
|
2005-11-23 21:45:02 +01:00
|
|
|
if ((error= (int) !(open_temporary_table(thd, share->path.str,
|
|
|
|
share->db.str,
|
|
|
|
share->table_name.str, 1))))
|
2001-09-02 12:47:00 +02:00
|
|
|
(void) rm_temporary_table(table_type, path);
|
2005-11-23 21:45:02 +01:00
|
|
|
free_table_share(share);
|
|
|
|
my_free((char*) table,MYF(0));
|
2002-06-11 10:20:31 +02:00
|
|
|
/*
|
2002-08-30 11:40:40 +02:00
|
|
|
If we return here we will not have logged the truncation to the bin log
|
|
|
|
and we will not send_ok() to the client.
|
2002-04-09 02:20:24 +02:00
|
|
|
*/
|
2005-02-16 17:34:02 +01:00
|
|
|
goto end;
|
2001-09-02 12:47:00 +02:00
|
|
|
}
|
|
|
|
|
2005-12-31 06:01:26 +01:00
|
|
|
path_length= build_table_filename(path, sizeof(path), table_list->db,
|
|
|
|
table_list->table_name, reg_ext);
|
2001-09-02 12:47:00 +02:00
|
|
|
|
|
|
|
if (!dont_send_ok)
|
|
|
|
{
|
2005-12-21 19:18:40 +01:00
|
|
|
enum legacy_db_type table_type;
|
2005-11-03 15:10:11 +01:00
|
|
|
mysql_frm_type(thd, path, &table_type);
|
|
|
|
if (table_type == DB_TYPE_UNKNOWN)
|
2001-09-02 12:47:00 +02:00
|
|
|
{
|
2004-11-13 18:35:51 +01:00
|
|
|
my_error(ER_NO_SUCH_TABLE, MYF(0),
|
2005-01-06 12:00:13 +01:00
|
|
|
table_list->db, table_list->table_name);
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2001-09-02 12:47:00 +02:00
|
|
|
}
|
2005-12-21 19:18:40 +01:00
|
|
|
if (!ha_check_storage_engine_flag(ha_resolve_by_legacy_type(thd, table_type),
|
2006-04-06 13:44:26 +02:00
|
|
|
HTON_CAN_RECREATE))
|
2005-08-29 17:01:46 +02:00
|
|
|
goto trunc_by_del;
|
2006-01-19 03:56:06 +01:00
|
|
|
|
2001-09-02 12:47:00 +02:00
|
|
|
if (lock_and_wait_for_table_name(thd, table_list))
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2001-09-02 12:47:00 +02:00
|
|
|
}
|
|
|
|
|
2006-01-19 03:56:06 +01:00
|
|
|
/* close log tables in use */
|
|
|
|
if (!my_strcasecmp(system_charset_info, table_list->db, "mysql"))
|
|
|
|
{
|
|
|
|
if (!my_strcasecmp(system_charset_info, table_list->table_name,
|
|
|
|
"general_log"))
|
|
|
|
{
|
|
|
|
lock_logger= 1;
|
|
|
|
logger.lock();
|
2006-01-20 13:23:24 +01:00
|
|
|
logger.close_log_table(QUERY_LOG_GENERAL, FALSE);
|
|
|
|
closed_log_tables= closed_log_tables | QUERY_LOG_GENERAL;
|
2006-01-19 03:56:06 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (!my_strcasecmp(system_charset_info, table_list->table_name,
|
|
|
|
"slow_log"))
|
|
|
|
{
|
|
|
|
lock_logger= 1;
|
|
|
|
logger.lock();
|
2006-01-20 13:23:24 +01:00
|
|
|
logger.close_log_table(QUERY_LOG_SLOW, FALSE);
|
|
|
|
closed_log_tables= closed_log_tables | QUERY_LOG_SLOW;
|
2006-01-19 03:56:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the .frm extension AIX 5.2 64-bit compiler bug (BUG#16155): this
|
|
|
|
// crashes, replacement works. *(path + path_length - reg_ext_length)=
|
|
|
|
// '\0';
|
2006-01-06 17:02:57 +01:00
|
|
|
path[path_length - reg_ext_length] = 0;
|
2005-11-23 21:45:02 +01:00
|
|
|
error= ha_create_table(thd, path, table_list->db, table_list->table_name,
|
|
|
|
&create_info, 1);
|
2005-01-27 22:38:56 +01:00
|
|
|
query_cache_invalidate3(thd, table_list, 0);
|
2002-11-07 03:02:37 +01:00
|
|
|
|
2002-04-09 02:20:24 +02:00
|
|
|
end:
|
2001-09-03 04:16:15 +02:00
|
|
|
if (!dont_send_ok)
|
2001-09-02 12:47:00 +02:00
|
|
|
{
|
2001-09-03 04:16:15 +02:00
|
|
|
if (!error)
|
2001-09-02 12:47:00 +02:00
|
|
|
{
|
2001-09-03 04:16:15 +02:00
|
|
|
if (mysql_bin_log.is_open())
|
|
|
|
{
|
2005-12-22 06:39:02 +01:00
|
|
|
/*
|
|
|
|
TRUNCATE must always be statement-based binlogged (not row-based) so
|
WL#2977 and WL#2712 global and session-level variable to set the binlog format (row/statement),
and new binlog format called "mixed" (which is statement-based except if only row-based is correct,
in this cset it means if UDF or UUID is used; more cases could be added in later 5.1 release):
SET GLOBAL|SESSION BINLOG_FORMAT=row|statement|mixed|default;
the global default is statement unless cluster is enabled (then it's row) as in 5.1-alpha.
It's not possible to use SET on this variable if a session is currently in row-based mode and has open temporary tables (because CREATE
TEMPORARY TABLE was not binlogged so temp table is not known on slave), or if NDB is enabled (because
NDB does not support such change on-the-fly, though it will later), of if in a stored function (see below).
The added tests test the possibility or impossibility to SET, their effects, and the mixed mode,
including in prepared statements and in stored procedures and functions.
Caveats:
a) The mixed mode will not work for stored functions: in mixed mode, a stored function will
always be binlogged as one call and in a statement-based way (e.g. INSERT VALUES(myfunc()) or SELECT myfunc()).
b) for the same reason, changing the thread's binlog format inside a stored function is
refused with an error message.
c) the same problems apply to triggers; implementing b) for triggers will be done later (will ask
Dmitri).
Additionally, as the binlog format is now changeable by each user for his session, I remove the implication
which was done at startup, where row-based automatically set log-bin-trust-routine-creators to 1
(not possible anymore as a user can now switch to stmt-based and do nasty things again), and automatically
set --innodb-locks-unsafe-for-binlog to 1 (was anyway theoretically incorrect as it disabled
phantom protection).
Plus fixes for compiler warnings.
2006-02-25 22:21:03 +01:00
|
|
|
we don't test current_stmt_binlog_row_based.
|
2005-12-22 06:39:02 +01:00
|
|
|
*/
|
2003-12-16 11:10:50 +01:00
|
|
|
thd->clear_error();
|
2005-12-22 06:39:02 +01:00
|
|
|
thd->binlog_query(THD::STMT_QUERY_TYPE,
|
|
|
|
thd->query, thd->query_length, FALSE, FALSE);
|
2001-09-03 04:16:15 +02:00
|
|
|
}
|
2002-10-02 12:33:08 +02:00
|
|
|
send_ok(thd); // This should return record count
|
2001-09-02 12:47:00 +02:00
|
|
|
}
|
2002-06-04 21:59:12 +02:00
|
|
|
VOID(pthread_mutex_lock(&LOCK_open));
|
2001-09-03 04:16:15 +02:00
|
|
|
unlock_table_name(thd, table_list);
|
2002-06-04 21:59:12 +02:00
|
|
|
VOID(pthread_mutex_unlock(&LOCK_open));
|
2006-01-19 03:56:06 +01:00
|
|
|
|
2006-01-20 13:23:24 +01:00
|
|
|
if (closed_log_tables & QUERY_LOG_SLOW)
|
|
|
|
logger.reopen_log_table(QUERY_LOG_SLOW);
|
2006-01-19 03:56:06 +01:00
|
|
|
|
2006-01-20 13:23:24 +01:00
|
|
|
if (closed_log_tables & QUERY_LOG_GENERAL)
|
|
|
|
logger.reopen_log_table(QUERY_LOG_GENERAL);
|
2006-01-19 03:56:06 +01:00
|
|
|
if (lock_logger)
|
|
|
|
logger.unlock();
|
2001-09-02 12:47:00 +02:00
|
|
|
}
|
2001-09-03 04:16:15 +02:00
|
|
|
else if (error)
|
2002-06-04 21:59:12 +02:00
|
|
|
{
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_open));
|
2001-09-03 04:16:15 +02:00
|
|
|
unlock_table_name(thd, table_list);
|
2002-06-04 21:59:12 +02:00
|
|
|
VOID(pthread_mutex_unlock(&LOCK_open));
|
|
|
|
}
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(error);
|
2005-08-29 17:01:46 +02:00
|
|
|
|
2005-11-23 21:45:02 +01:00
|
|
|
trunc_by_del:
|
2005-08-29 17:01:46 +02:00
|
|
|
/* Probably InnoDB table */
|
|
|
|
ulong save_options= thd->options;
|
|
|
|
table_list->lock_type= TL_WRITE;
|
|
|
|
thd->options&= ~(ulong) (OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
|
|
|
|
ha_enable_transaction(thd, FALSE);
|
2005-08-29 18:18:30 +02:00
|
|
|
mysql_init_select(thd->lex);
|
2005-08-29 17:01:46 +02:00
|
|
|
error= mysql_delete(thd, table_list, (COND*) 0, (SQL_LIST*) 0,
|
2005-08-30 12:35:37 +02:00
|
|
|
HA_POS_ERROR, LL(0), TRUE);
|
2005-08-29 17:01:46 +02:00
|
|
|
ha_enable_transaction(thd, TRUE);
|
|
|
|
thd->options= save_options;
|
|
|
|
DBUG_RETURN(error);
|
2001-09-02 12:47:00 +02:00
|
|
|
}
|