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())
|
|
|
|
{
|
2004-11-12 13:34:00 +01:00
|
|
|
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
|
|
|
|
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
|
2000-08-21 02:07:54 +02:00
|
|
|
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 */
|
2004-07-16 00:15:55 +02:00
|
|
|
for (table= table_list;
|
|
|
|
table->next_local != ren_table ;
|
|
|
|
table= table->next_local->next_local) ;
|
|
|
|
table= table->next_local->next_local; // 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
|
|
|
if (mysql_bin_log.is_open())
|
|
|
|
{
|
2003-12-16 11:10:50 +01:00
|
|
|
thd->clear_error();
|
2004-12-03 12:13:51 +01:00
|
|
|
Query_log_event qinfo(thd, thd->query, thd->query_length, 0, FALSE);
|
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
|
|
|
|
2005-03-16 15:11:01 +01:00
|
|
|
unlock_table_names(thd, table_list, (TABLE_LIST*) 0);
|
2003-03-03 19:42:49 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2004-07-16 00:15:55 +02:00
|
|
|
TABLE_LIST *next= table_list->next_local;
|
|
|
|
table_list->next_local= prev;
|
2004-04-05 23:10:43 +02:00
|
|
|
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");
|
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
|
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
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
new_table= ren_table->next_local;
|
2003-12-30 12:14:21 +01:00
|
|
|
if (lower_case_table_names == 2)
|
|
|
|
{
|
|
|
|
old_alias= ren_table->alias;
|
|
|
|
new_alias= new_table->alias;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-01-06 12:00:13 +01:00
|
|
|
old_alias= ren_table->table_name;
|
|
|
|
new_alias= new_table->table_name;
|
2003-12-30 12:14:21 +01:00
|
|
|
}
|
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))
|
|
|
|
{
|
now my_printf_error is not better then my_error, but my_error call is shorter
used only one implementation of format parser of (printf)
fixed multistatement
include/mysqld_error.h:
newerror messages
mysql-test/t/key.test:
unknown error replaced with real error
mysys/my_error.c:
my_error & my_printf_error use my_vsprintf
sql/field_conv.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/ha_innodb.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/handler.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/item.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/item_cmpfunc.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/item_func.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/item_strfunc.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/lock.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/log.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/parse_file.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/procedure.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/protocol.cc:
no need reset thd->lex->found_colon to break multiline sequance now, send_error called too late
sql/repl_failsafe.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/set_var.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/share/czech/errmsg.txt:
new errors converted from unknown error
sql/share/danish/errmsg.txt:
new errors converted from unknown error
sql/share/dutch/errmsg.txt:
new errors converted from unknown error
sql/share/english/errmsg.txt:
new errors converted from unknown error
sql/share/estonian/errmsg.txt:
new errors converted from unknown error
sql/share/french/errmsg.txt:
new errors converted from unknown error
sql/share/german/errmsg.txt:
new errors converted from unknown error
sql/share/greek/errmsg.txt:
new errors converted from unknown error
sql/share/hungarian/errmsg.txt:
new errors converted from unknown error
sql/share/italian/errmsg.txt:
new errors converted from unknown error
sql/share/japanese/errmsg.txt:
new errors converted from unknown error
sql/share/korean/errmsg.txt:
new errors converted from unknown error
sql/share/norwegian-ny/errmsg.txt:
new errors converted from unknown error
sql/share/norwegian/errmsg.txt:
new errors converted from unknown error
sql/share/polish/errmsg.txt:
new errors converted from unknown error
sql/share/portuguese/errmsg.txt:
new errors converted from unknown error
sql/share/romanian/errmsg.txt:
new errors converted from unknown error
sql/share/russian/errmsg.txt:
new errors converted from unknown error
sql/share/serbian/errmsg.txt:
new errors converted from unknown error
sql/share/slovak/errmsg.txt:
new errors converted from unknown error
sql/share/spanish/errmsg.txt:
new errors converted from unknown error
sql/share/swedish/errmsg.txt:
new errors converted from unknown error
sql/share/ukrainian/errmsg.txt:
new errors converted from unknown error
sql/slave.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sp.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sp_head.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_acl.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_analyse.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_base.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_class.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_db.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_delete.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_handler.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_insert.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_load.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_map.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_parse.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
multi-row command fixed
sql/sql_prepare.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
remover send_error ingected from 4.1
sql/sql_rename.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_repl.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_select.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_show.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_table.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_trigger.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_udf.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_update.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_view.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_yacc.yy:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/table.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
strings/my_vsnprintf.c:
* format support added to my_vsprint
2004-11-13 18:35:51 +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);
|
2005-06-17 23:14:44 +02:00
|
|
|
if ((table_type=get_table_type(thd, name)) == DB_TYPE_UNKNOWN)
|
2000-08-21 23:18:32 +02:00
|
|
|
{
|
now my_printf_error is not better then my_error, but my_error call is shorter
used only one implementation of format parser of (printf)
fixed multistatement
include/mysqld_error.h:
newerror messages
mysql-test/t/key.test:
unknown error replaced with real error
mysys/my_error.c:
my_error & my_printf_error use my_vsprintf
sql/field_conv.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/ha_innodb.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/handler.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/item.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/item_cmpfunc.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/item_func.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/item_strfunc.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/lock.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/log.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/parse_file.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/procedure.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/protocol.cc:
no need reset thd->lex->found_colon to break multiline sequance now, send_error called too late
sql/repl_failsafe.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/set_var.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/share/czech/errmsg.txt:
new errors converted from unknown error
sql/share/danish/errmsg.txt:
new errors converted from unknown error
sql/share/dutch/errmsg.txt:
new errors converted from unknown error
sql/share/english/errmsg.txt:
new errors converted from unknown error
sql/share/estonian/errmsg.txt:
new errors converted from unknown error
sql/share/french/errmsg.txt:
new errors converted from unknown error
sql/share/german/errmsg.txt:
new errors converted from unknown error
sql/share/greek/errmsg.txt:
new errors converted from unknown error
sql/share/hungarian/errmsg.txt:
new errors converted from unknown error
sql/share/italian/errmsg.txt:
new errors converted from unknown error
sql/share/japanese/errmsg.txt:
new errors converted from unknown error
sql/share/korean/errmsg.txt:
new errors converted from unknown error
sql/share/norwegian-ny/errmsg.txt:
new errors converted from unknown error
sql/share/norwegian/errmsg.txt:
new errors converted from unknown error
sql/share/polish/errmsg.txt:
new errors converted from unknown error
sql/share/portuguese/errmsg.txt:
new errors converted from unknown error
sql/share/romanian/errmsg.txt:
new errors converted from unknown error
sql/share/russian/errmsg.txt:
new errors converted from unknown error
sql/share/serbian/errmsg.txt:
new errors converted from unknown error
sql/share/slovak/errmsg.txt:
new errors converted from unknown error
sql/share/spanish/errmsg.txt:
new errors converted from unknown error
sql/share/swedish/errmsg.txt:
new errors converted from unknown error
sql/share/ukrainian/errmsg.txt:
new errors converted from unknown error
sql/slave.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sp.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sp_head.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_acl.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_analyse.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_base.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_class.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_db.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_delete.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_handler.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_insert.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_load.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_map.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_parse.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
multi-row command fixed
sql/sql_prepare.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
remover send_error ingected from 4.1
sql/sql_rename.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_repl.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_select.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_show.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_table.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_trigger.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_udf.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_update.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_view.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/sql_yacc.yy:
now my_printf_error is not better then my_error, but my_error call is shorter
sql/table.cc:
now my_printf_error is not better then my_error, but my_error call is shorter
strings/my_vsnprintf.c:
* format support added to my_vsprint
2004-11-13 18:35:51 +01:00
|
|
|
my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno);
|
2000-08-21 23:18:32 +02:00
|
|
|
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
|
|
|
}
|