sql_rename.cc 5.35 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
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list);

unknown's avatar
unknown committed
29 30 31 32 33 34 35
/*
  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)
{
36
  bool error= 1;
37
  TABLE_LIST *ren_table= 0;
unknown's avatar
unknown committed
38
  DBUG_ENTER("mysql_rename_tables");
unknown's avatar
unknown committed
39

40 41 42 43
  /*
    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
44 45 46

  if (thd->locked_tables || thd->active_transaction())
  {
unknown's avatar
unknown committed
47 48
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
               ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
unknown's avatar
unknown committed
49 50
    DBUG_RETURN(1);
  }
unknown's avatar
unknown committed
51

unknown's avatar
unknown committed
52
  if (wait_if_global_read_lock(thd,0,1))
unknown's avatar
unknown committed
53
    DBUG_RETURN(1);
unknown's avatar
unknown committed
54
  VOID(pthread_mutex_lock(&LOCK_open));
55 56
  if (lock_table_names(thd, table_list))
    goto err;
57 58
  
  error=0;
59
  if ((ren_table=rename_tables(thd,table_list,0)))
unknown's avatar
unknown committed
60 61
  {
    /* Rename didn't succeed;  rename back the tables in reverse order */
62
    TABLE_LIST *table;
unknown's avatar
unknown committed
63

64 65
    /* Reverse the table list */
    table_list= reverse_table_list(table_list);
unknown's avatar
unknown committed
66 67

    /* Find the last renamed table */
unknown's avatar
VIEW  
unknown committed
68 69 70 71
    for (table= table_list;
	 table->next_local != ren_table ;
	 table= table->next_local->next_local) ;
    table= table->next_local->next_local;		// Skip error table
unknown's avatar
unknown committed
72
    /* Revert to old names */
unknown's avatar
unknown committed
73
    rename_tables(thd, table, 1);
74 75 76 77

    /* Revert the table list (for prepared statements) */
    table_list= reverse_table_list(table_list);

78
    error= 1;
unknown's avatar
unknown committed
79
  }
80 81

  /* Lets hope this doesn't fail as the result will be messy */ 
unknown's avatar
unknown committed
82
  if (!error)
unknown's avatar
unknown committed
83
  {
84 85
    if (mysql_bin_log.is_open())
    {
unknown's avatar
unknown committed
86
      thd->clear_error();
87 88
      thd->binlog_query(THD::STMT_QUERY_TYPE,
                        thd->query, thd->query_length, FALSE, FALSE);
89
    }
90
    send_ok(thd);
unknown's avatar
unknown committed
91
  }
92

unknown's avatar
unknown committed
93
  unlock_table_names(thd, table_list, (TABLE_LIST*) 0);
94 95

err:
unknown's avatar
unknown committed
96
  pthread_mutex_unlock(&LOCK_open);
unknown's avatar
unknown committed
97
  start_waiting_global_read_lock(thd);
unknown's avatar
unknown committed
98 99 100 101
  DBUG_RETURN(error);
}


102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
/*
  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)
  {
unknown's avatar
VIEW  
unknown committed
118 119
    TABLE_LIST *next= table_list->next_local;
    table_list->next_local= prev;
120 121 122 123 124 125 126
    prev= table_list;
    table_list= next;
  }
  return (prev);
}


unknown's avatar
unknown committed
127 128
/*
  Rename all tables in list; Return pointer to wrong entry if something goes
unknown's avatar
unknown committed
129
  wrong.  Note that the table_list may be empty!
unknown's avatar
unknown committed
130 131 132
*/

static TABLE_LIST *
unknown's avatar
unknown committed
133
rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error)
unknown's avatar
unknown committed
134
{
unknown's avatar
unknown committed
135
  TABLE_LIST *ren_table,*new_table;
136
  frm_type_enum frm_type;
unknown's avatar
unknown committed
137
  enum legacy_db_type table_type;
138

unknown's avatar
unknown committed
139 140
  DBUG_ENTER("rename_tables");

unknown's avatar
VIEW  
unknown committed
141
  for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
unknown's avatar
unknown committed
142
  {
143
    int rc= 1;
unknown's avatar
unknown committed
144
    char name[FN_REFLEN];
unknown's avatar
unknown committed
145
    const char *new_alias, *old_alias;
unknown's avatar
unknown committed
146

unknown's avatar
VIEW  
unknown committed
147
    new_table= ren_table->next_local;
unknown's avatar
unknown committed
148 149 150 151 152 153 154
    if (lower_case_table_names == 2)
    {
      old_alias= ren_table->alias;
      new_alias= new_table->alias;
    }
    else
    {
155 156
      old_alias= ren_table->table_name;
      new_alias= new_table->table_name;
unknown's avatar
unknown committed
157
    }
unknown's avatar
unknown committed
158
    sprintf(name,"%s/%s/%s%s",mysql_data_home,
unknown's avatar
unknown committed
159 160
	    new_table->db, new_alias, reg_ext);
    unpack_filename(name, name);
unknown's avatar
unknown committed
161 162
    if (!access(name,F_OK))
    {
163
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
unknown's avatar
unknown committed
164
      DBUG_RETURN(ren_table);			// This can't be skipped
unknown's avatar
unknown committed
165
    }
unknown's avatar
unknown committed
166
    sprintf(name,"%s/%s/%s%s",mysql_data_home,
unknown's avatar
unknown committed
167
	    ren_table->db, old_alias,
unknown's avatar
unknown committed
168
	    reg_ext);
unknown's avatar
unknown committed
169
    unpack_filename(name, name);
170

171
    frm_type= mysql_frm_type(thd, name, &table_type);
172
    switch (frm_type)
unknown's avatar
unknown committed
173
    {
174
      case FRMTYPE_TABLE:
175
      {
176
        if (table_type == DB_TYPE_UNKNOWN) 
177 178
          my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno);
        else
unknown's avatar
unknown committed
179 180
          rc= mysql_rename_table(ha_resolve_by_legacy_type(thd, table_type),
                                 ren_table->db, old_alias,
181
                                 new_table->db, new_alias);
182
        break;
183
      }
184 185 186 187 188 189 190 191 192 193 194 195 196
      case FRMTYPE_VIEW:
        /* change of schema is not allowed */
        if (strcmp(ren_table->db, new_table->db))
          my_error(ER_FORBID_SCHEMA_CHANGE, MYF(0), ren_table->db, 
                   new_table->db);
        else
          rc= mysql_rename_view(thd, new_alias, ren_table);
        break;
      default:
        DBUG_ASSERT(0); // should never happen
      case FRMTYPE_ERROR:
        my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno);
        break;
unknown's avatar
unknown committed
197
    }
198 199
    if (rc && !skip_error)
      DBUG_RETURN(ren_table);
unknown's avatar
unknown committed
200
  }
unknown's avatar
unknown committed
201
  DBUG_RETURN(0);
unknown's avatar
unknown committed
202
}