1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
Atomic rename of table; RENAME TABLE t1 to t2, tmp to t1 [,...]
19
#include <drizzled/server_includes.h>
20
#include <drizzled/error.h>
21
#include <drizzled/table_list.h>
22
#include <drizzled/session.h>
23
#include <drizzled/lock.h>
24
#include "drizzled/rename.h"
26
using namespace drizzled;
28
static TableList *rename_tables(Session *session, TableList *table_list,
31
static TableList *reverse_table_list(TableList *table_list);
34
Every second entry in the table_list is the original name and every
35
second entry is the new name.
37
bool drizzle_rename_tables(Session *session, TableList *table_list)
40
TableList *ren_table= NULL;
43
Avoid problems with a rename on a table that we have locked or
44
if the user is trying to to do this in a transcation context
46
if (session->inTransaction())
48
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
52
if (wait_if_global_read_lock(session,0,1))
55
pthread_mutex_lock(&LOCK_open); /* Rename table lock for exclusive access */
56
if (lock_table_names_exclusively(session, table_list))
58
pthread_mutex_unlock(&LOCK_open);
63
if ((ren_table=rename_tables(session,table_list,0)))
65
/* Rename didn't succeed; rename back the tables in reverse order */
68
/* Reverse the table list */
69
table_list= reverse_table_list(table_list);
71
/* Find the last renamed table */
72
for (table= table_list;
73
table->next_local != ren_table ;
74
table= table->next_local->next_local) ;
75
table= table->next_local->next_local; // Skip error table
76
/* Revert to old names */
77
rename_tables(session, table, 1);
79
/* Revert the table list (for prepared statements) */
80
table_list= reverse_table_list(table_list);
85
An exclusive lock on table names is satisfactory to ensure
86
no other thread accesses this table.
87
We still should unlock LOCK_open as early as possible, to provide
88
higher concurrency - query_cache_invalidate can take minutes to
91
pthread_mutex_unlock(&LOCK_open);
93
/* Lets hope this doesn't fail as the result will be messy */
96
write_bin_log(session, true, session->query, session->query_length);
100
pthread_mutex_lock(&LOCK_open); /* unlock all tables held */
101
unlock_table_names(table_list, NULL);
102
pthread_mutex_unlock(&LOCK_open);
105
start_waiting_global_read_lock(session);
116
table_list pointer to table _list
119
pointer to new (reversed) list
121
static TableList *reverse_table_list(TableList *table_list)
123
TableList *prev= NULL;
127
TableList *next= table_list->next_local;
128
table_list->next_local= prev;
137
Rename a single table or a view
141
session Thread handle
142
ren_table A table/view to be renamed
143
new_db The database to which the table to be moved to
144
new_table_name The new table/view name
145
skip_error Whether to skip error
148
Rename a single table or a view.
156
do_rename(Session *session, TableList *ren_table, const char *new_db, const char *new_table_name, bool skip_error)
159
const char *new_alias, *old_alias;
162
old_alias= ren_table->table_name;
163
new_alias= new_table_name;
166
StorageEngine *engine= NULL;
167
message::Table table_proto;
168
char path[FN_REFLEN];
171
length= build_table_filename(path, sizeof(path),
172
ren_table->db, old_alias, false);
174
if (StorageEngine::getTableProto(path, &table_proto)!= EEXIST)
176
my_error(ER_NO_SUCH_TABLE, MYF(0), ren_table->db, old_alias);
180
engine= ha_resolve_by_name(session, table_proto.engine().name());
182
length= build_table_filename(path, sizeof(path),
183
new_db, new_alias, false);
185
if (StorageEngine::getTableProto(path, NULL)!=ENOENT)
187
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
188
return(1); // This can't be skipped
191
rc= mysql_rename_table(engine,
192
ren_table->db, old_alias,
193
new_db, new_alias, 0);
194
if (rc && !skip_error)
201
Rename all tables in list; Return pointer to wrong entry if something goes
202
wrong. Note that the table_list may be empty!
206
Rename tables/views in the list
210
session Thread handle
211
table_list List of tables to rename
212
skip_error Whether to skip errors
215
Take a table/view name from and odd list element and rename it to a
216
the name taken from list element+1. Note that the table_list may be
225
rename_tables(Session *session, TableList *table_list, bool skip_error)
227
TableList *ren_table, *new_table;
229
for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
231
new_table= ren_table->next_local;
232
if (do_rename(session, ren_table, new_table->db, new_table->table_name, skip_error))