2000-08-21 02:07:54 +02:00
|
|
|
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Atomic rename of table; RENAME TABLE t1 to t2, tmp to t1 [,...]
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mysql_priv.h"
|
|
|
|
|
|
|
|
|
2000-08-21 23:18:32 +02:00
|
|
|
static TABLE_LIST *rename_tables(THD *thd, TABLE_LIST *table_list,
|
|
|
|
bool skip_error);
|
2000-08-21 02:07:54 +02:00
|
|
|
|
2004-04-05 23:10:43 +02:00
|
|
|
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list);
|
|
|
|
|
2000-08-21 02:07:54 +02:00
|
|
|
/*
|
|
|
|
Every second entry in the table_list is the original name and every
|
|
|
|
second entry is the new name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool mysql_rename_tables(THD *thd, TABLE_LIST *table_list)
|
|
|
|
{
|
2003-03-04 12:36:59 +01:00
|
|
|
bool error= 1;
|
2003-03-03 19:42:49 +01:00
|
|
|
TABLE_LIST *ren_table= 0;
|
2000-08-21 02:07:54 +02:00
|
|
|
DBUG_ENTER("mysql_rename_tables");
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2002-01-30 14:32:48 +01:00
|
|
|
/*
|
|
|
|
Avoid problems with a rename on a table that we have locked or
|
|
|
|
if the user is trying to to do this in a transcation context
|
|
|
|
*/
|
2000-08-21 02:07:54 +02:00
|
|
|
|
|
|
|
if (thd->locked_tables || thd->active_transaction())
|
|
|
|
{
|
|
|
|
my_error(ER_LOCK_OR_ACTIVE_TRANSACTION,MYF(0));
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2004-08-20 22:54:42 +02:00
|
|
|
if (wait_if_global_read_lock(thd,0,1))
|
2004-04-01 19:47:09 +02:00
|
|
|
DBUG_RETURN(1);
|
2000-08-21 02:07:54 +02:00
|
|
|
VOID(pthread_mutex_lock(&LOCK_open));
|
2003-03-03 19:42:49 +01:00
|
|
|
if (lock_table_names(thd, table_list))
|
|
|
|
goto err;
|
2003-03-04 12:36:59 +01:00
|
|
|
|
|
|
|
error=0;
|
2003-03-03 19:42:49 +01:00
|
|
|
if ((ren_table=rename_tables(thd,table_list,0)))
|
2000-08-21 02:07:54 +02:00
|
|
|
{
|
|
|
|
/* Rename didn't succeed; rename back the tables in reverse order */
|
2004-04-05 23:10:43 +02:00
|
|
|
TABLE_LIST *table;
|
2000-08-21 23:18:32 +02:00
|
|
|
|
2004-04-05 23:10:43 +02:00
|
|
|
/* Reverse the table list */
|
|
|
|
table_list= reverse_table_list(table_list);
|
2000-08-21 02:07:54 +02:00
|
|
|
|
|
|
|
/* Find the last renamed table */
|
2000-08-21 23:18:32 +02:00
|
|
|
for (table=table_list ;
|
|
|
|
table->next != ren_table ;
|
2000-08-21 02:07:54 +02:00
|
|
|
table=table->next->next) ;
|
2001-11-06 23:13:29 +01:00
|
|
|
table=table->next->next; // Skip error table
|
2000-08-21 02:07:54 +02:00
|
|
|
/* Revert to old names */
|
2000-08-21 23:18:32 +02:00
|
|
|
rename_tables(thd, table, 1);
|
2004-04-05 23:10:43 +02:00
|
|
|
|
|
|
|
/* Revert the table list (for prepared statements) */
|
|
|
|
table_list= reverse_table_list(table_list);
|
|
|
|
|
2003-03-03 19:42:49 +01:00
|
|
|
error= 1;
|
2000-08-21 02:07:54 +02:00
|
|
|
}
|
2001-03-21 21:34:16 +01:00
|
|
|
|
|
|
|
/* Lets hope this doesn't fail as the result will be messy */
|
2001-10-10 01:50:28 +02:00
|
|
|
if (!error)
|
2000-08-21 02:07:54 +02:00
|
|
|
{
|
2000-09-16 03:27:21 +02:00
|
|
|
mysql_update_log.write(thd,thd->query,thd->query_length);
|
|
|
|
if (mysql_bin_log.is_open())
|
|
|
|
{
|
2003-12-16 11:10:50 +01:00
|
|
|
thd->clear_error();
|
2002-11-07 03:02:37 +01:00
|
|
|
Query_log_event qinfo(thd, thd->query, thd->query_length, 0);
|
2000-09-16 03:27:21 +02:00
|
|
|
mysql_bin_log.write(&qinfo);
|
|
|
|
}
|
2002-10-02 12:33:08 +02:00
|
|
|
send_ok(thd);
|
2000-08-21 02:07:54 +02:00
|
|
|
}
|
2001-03-21 21:34:16 +01:00
|
|
|
|
2003-03-03 19:42:49 +01:00
|
|
|
unlock_table_names(thd,table_list);
|
|
|
|
|
|
|
|
err:
|
2000-08-21 02:07:54 +02:00
|
|
|
pthread_mutex_unlock(&LOCK_open);
|
2004-04-01 19:47:09 +02:00
|
|
|
start_waiting_global_read_lock(thd);
|
2000-08-21 02:07:54 +02:00
|
|
|
DBUG_RETURN(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-05 23:10:43 +02:00
|
|
|
/*
|
|
|
|
reverse table list
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
reverse_table_list()
|
|
|
|
table_list pointer to table _list
|
|
|
|
|
|
|
|
RETURN
|
|
|
|
pointer to new (reversed) list
|
|
|
|
*/
|
|
|
|
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list)
|
|
|
|
{
|
|
|
|
TABLE_LIST *prev= 0;
|
|
|
|
|
|
|
|
while (table_list)
|
|
|
|
{
|
|
|
|
TABLE_LIST *next= table_list->next;
|
|
|
|
table_list->next= prev;
|
|
|
|
prev= table_list;
|
|
|
|
table_list= next;
|
|
|
|
}
|
|
|
|
return (prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-21 02:07:54 +02:00
|
|
|
/*
|
|
|
|
Rename all tables in list; Return pointer to wrong entry if something goes
|
2000-08-21 23:18:32 +02:00
|
|
|
wrong. Note that the table_list may be empty!
|
2000-08-21 02:07:54 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
static TABLE_LIST *
|
2000-08-21 23:18:32 +02:00
|
|
|
rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error)
|
2000-08-21 02:07:54 +02:00
|
|
|
{
|
2000-08-21 23:18:32 +02:00
|
|
|
TABLE_LIST *ren_table,*new_table;
|
|
|
|
DBUG_ENTER("rename_tables");
|
|
|
|
|
|
|
|
for (ren_table=table_list ; ren_table ; ren_table=new_table->next)
|
2000-08-21 02:07:54 +02:00
|
|
|
{
|
|
|
|
db_type table_type;
|
|
|
|
char name[FN_REFLEN];
|
2003-12-30 12:14:21 +01:00
|
|
|
const char *new_alias, *old_alias;
|
2000-08-21 02:07:54 +02:00
|
|
|
|
2003-12-30 12:14:21 +01:00
|
|
|
new_table=ren_table->next;
|
|
|
|
if (lower_case_table_names == 2)
|
|
|
|
{
|
|
|
|
old_alias= ren_table->alias;
|
|
|
|
new_alias= new_table->alias;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
old_alias= ren_table->real_name;
|
|
|
|
new_alias= new_table->real_name;
|
|
|
|
}
|
2000-08-21 23:18:32 +02:00
|
|
|
sprintf(name,"%s/%s/%s%s",mysql_data_home,
|
2003-12-30 12:14:21 +01:00
|
|
|
new_table->db, new_alias, reg_ext);
|
|
|
|
unpack_filename(name, name);
|
2000-08-21 23:18:32 +02:00
|
|
|
if (!access(name,F_OK))
|
|
|
|
{
|
2003-12-30 12:14:21 +01:00
|
|
|
my_error(ER_TABLE_EXISTS_ERROR,MYF(0),new_alias);
|
2002-01-29 17:32:16 +01:00
|
|
|
DBUG_RETURN(ren_table); // This can't be skipped
|
2000-08-21 23:18:32 +02:00
|
|
|
}
|
2000-08-21 02:07:54 +02:00
|
|
|
sprintf(name,"%s/%s/%s%s",mysql_data_home,
|
2003-12-30 12:14:21 +01:00
|
|
|
ren_table->db, old_alias,
|
2000-08-21 02:07:54 +02:00
|
|
|
reg_ext);
|
2003-12-30 12:14:21 +01:00
|
|
|
unpack_filename(name, name);
|
2000-08-21 23:18:32 +02:00
|
|
|
if ((table_type=get_table_type(name)) == DB_TYPE_UNKNOWN)
|
|
|
|
{
|
|
|
|
my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno);
|
|
|
|
if (!skip_error)
|
2002-01-29 17:32:16 +01:00
|
|
|
DBUG_RETURN(ren_table);
|
2000-08-21 23:18:32 +02:00
|
|
|
}
|
|
|
|
else if (mysql_rename_table(table_type,
|
2003-12-30 12:14:21 +01:00
|
|
|
ren_table->db, old_alias,
|
|
|
|
new_table->db, new_alias))
|
2000-08-21 02:07:54 +02:00
|
|
|
{
|
|
|
|
if (!skip_error)
|
2002-03-21 22:06:48 +01:00
|
|
|
DBUG_RETURN(ren_table);
|
2000-08-21 02:07:54 +02:00
|
|
|
}
|
|
|
|
}
|
2000-08-21 23:18:32 +02:00
|
|
|
DBUG_RETURN(0);
|
2000-08-21 02:07:54 +02:00
|
|
|
}
|