sql_rename.cc 4.21 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* 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"


unknown's avatar
unknown committed
24 25
static TABLE_LIST *rename_tables(THD *thd, TABLE_LIST *table_list,
				 bool skip_error);
unknown's avatar
unknown committed
26 27 28 29 30 31 32 33

/*
  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)
{
unknown's avatar
unknown committed
34
  bool error=1,got_all_locks=1;
unknown's avatar
unknown committed
35
  TABLE_LIST *lock_table,*ren_table=0;
unknown's avatar
unknown committed
36
  DBUG_ENTER("mysql_rename_tables");
unknown's avatar
unknown committed
37

38 39 40 41
  /*
    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
  */
unknown's avatar
unknown committed
42 43 44 45 46 47

  if (thd->locked_tables || thd->active_transaction())
  {
    my_error(ER_LOCK_OR_ACTIVE_TRANSACTION,MYF(0));
    DBUG_RETURN(1);
  }
unknown's avatar
unknown committed
48

unknown's avatar
unknown committed
49 50 51 52 53 54 55 56 57
  VOID(pthread_mutex_lock(&LOCK_open));
  for (lock_table=table_list ; lock_table ; lock_table=lock_table->next)
  {
    int got_lock;
    if ((got_lock=lock_table_name(thd,lock_table)) < 0)
      goto end;
    if (got_lock)
      got_all_locks=0;
  }
unknown's avatar
unknown committed
58

unknown's avatar
unknown committed
59 60 61
  if (!got_all_locks && wait_for_locked_table_names(thd,table_list))
    goto end;

unknown's avatar
unknown committed
62
  if (!(ren_table=rename_tables(thd,table_list,0)))
unknown's avatar
unknown committed
63
    error=0;
unknown's avatar
unknown committed
64

unknown's avatar
unknown committed
65 66 67 68 69
end:
  if (ren_table)
  {
    /* Rename didn't succeed;  rename back the tables in reverse order */
    TABLE_LIST *prev=0,*table;
unknown's avatar
unknown committed
70 71
    /* Reverse the table list */

unknown's avatar
unknown committed
72 73
    while (table_list)
    {
unknown's avatar
unknown committed
74 75
      TABLE_LIST *next=table_list->next;
      table_list->next=prev;
unknown's avatar
unknown committed
76 77 78 79 80 81
      prev=table_list;
      table_list=next;
    }
    table_list=prev;

    /* Find the last renamed table */
unknown's avatar
unknown committed
82 83
    for (table=table_list ;
	 table->next != ren_table ;
unknown's avatar
unknown committed
84
	 table=table->next->next) ;
unknown's avatar
unknown committed
85
    table=table->next->next;			// Skip error table
unknown's avatar
unknown committed
86
    /* Revert to old names */
unknown's avatar
unknown committed
87
    rename_tables(thd, table, 1);
unknown's avatar
unknown committed
88 89
    /* Note that lock_table == 0 here, so the unlock loop will work */
  }
90 91

  /* Lets hope this doesn't fail as the result will be messy */ 
unknown's avatar
unknown committed
92
  if (!error)
unknown's avatar
unknown committed
93
  {
94 95 96
    mysql_update_log.write(thd,thd->query,thd->query_length);
    if (mysql_bin_log.is_open())
    {
97
      Query_log_event qinfo(thd, thd->query, thd->query_length, 0);
98 99
      mysql_bin_log.write(&qinfo);
    }
100
    send_ok(thd);
unknown's avatar
unknown committed
101
  }
102

unknown's avatar
unknown committed
103 104 105 106 107 108 109 110 111 112
  for (TABLE_LIST *table=table_list ; table != lock_table ; table=table->next)
    unlock_table_name(thd,table);
  pthread_cond_broadcast(&COND_refresh);
  pthread_mutex_unlock(&LOCK_open);
  DBUG_RETURN(error);
}


/*
  Rename all tables in list; Return pointer to wrong entry if something goes
unknown's avatar
unknown committed
113
  wrong.  Note that the table_list may be empty!
unknown's avatar
unknown committed
114 115 116
*/

static TABLE_LIST *
unknown's avatar
unknown committed
117
rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error)
unknown's avatar
unknown committed
118
{
unknown's avatar
unknown committed
119 120 121 122
  TABLE_LIST *ren_table,*new_table;
  DBUG_ENTER("rename_tables");

  for (ren_table=table_list ; ren_table ; ren_table=new_table->next)
unknown's avatar
unknown committed
123 124 125
  {
    db_type table_type;
    char name[FN_REFLEN];
unknown's avatar
unknown committed
126
    new_table=ren_table->next;
unknown's avatar
unknown committed
127

unknown's avatar
unknown committed
128
    sprintf(name,"%s/%s/%s%s",mysql_data_home,
129
	    new_table->db,new_table->real_name,
unknown's avatar
unknown committed
130 131 132 133
	    reg_ext);
    if (!access(name,F_OK))
    {
      my_error(ER_TABLE_EXISTS_ERROR,MYF(0),name);
unknown's avatar
unknown committed
134
      DBUG_RETURN(ren_table);			// This can't be skipped
unknown's avatar
unknown committed
135
    }
unknown's avatar
unknown committed
136
    sprintf(name,"%s/%s/%s%s",mysql_data_home,
137
	    ren_table->db,ren_table->real_name,
unknown's avatar
unknown committed
138
	    reg_ext);
unknown's avatar
unknown committed
139 140 141 142
    if ((table_type=get_table_type(name)) == DB_TYPE_UNKNOWN)
    {
      my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno);
      if (!skip_error)
unknown's avatar
unknown committed
143
	DBUG_RETURN(ren_table);
unknown's avatar
unknown committed
144 145
    }
    else if (mysql_rename_table(table_type,
146 147
				ren_table->db, ren_table->real_name,
				new_table->db, new_table->real_name))
unknown's avatar
unknown committed
148 149
    {
      if (!skip_error)
150
	DBUG_RETURN(ren_table);
unknown's avatar
unknown committed
151 152
    }
  }
unknown's avatar
unknown committed
153
  DBUG_RETURN(0);
unknown's avatar
unknown committed
154
}