17
17
Atomic rename of table; RENAME TABLE t1 to t2, tmp to t1 [,...]
19
#include <drizzled/server_includes.h>
20
#include <drizzled/drizzled_error_messages.h>
22
static TableList *rename_tables(THD *thd, TableList *table_list,
20
#include "mysql_priv.h"
22
static TABLE_LIST *rename_tables(THD *thd, TABLE_LIST *table_list,
25
static TableList *reverse_table_list(TableList *table_list);
25
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list);
28
28
Every second entry in the table_list is the original name and every
29
29
second entry is the new name.
32
bool mysql_rename_tables(THD *thd, TableList *table_list, bool silent)
32
bool mysql_rename_tables(THD *thd, TABLE_LIST *table_list, bool silent)
35
TableList *ren_table= 0;
35
TABLE_LIST *ren_table= 0;
36
DBUG_ENTER("mysql_rename_tables");
38
39
Avoid problems with a rename on a table that we have locked or
39
40
if the user is trying to to do this in a transcation context
41
43
if (thd->locked_tables || thd->active_transaction())
43
45
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
44
46
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
48
mysql_ha_rm_tables(thd, table_list, false);
50
mysql_ha_rm_tables(thd, table_list, FALSE);
50
52
if (wait_if_global_read_lock(thd,0,1))
53
55
pthread_mutex_lock(&LOCK_open);
54
56
if (lock_table_names_exclusively(thd, table_list))
61
63
if ((ren_table=rename_tables(thd,table_list,0)))
63
65
/* Rename didn't succeed; rename back the tables in reverse order */
66
68
/* Reverse the table list */
67
69
table_list= reverse_table_list(table_list);
91
93
/* Lets hope this doesn't fail as the result will be messy */
92
94
if (!silent && !error)
94
write_bin_log(thd, true, thd->query, thd->query_length);
96
write_bin_log(thd, TRUE, thd->query, thd->query_length);
98
100
pthread_mutex_lock(&LOCK_open);
99
unlock_table_names(thd, table_list, (TableList*) 0);
101
unlock_table_names(thd, table_list, (TABLE_LIST*) 0);
100
102
pthread_mutex_unlock(&LOCK_open);
103
105
start_waiting_global_read_lock(thd);
116
118
pointer to new (reversed) list
118
static TableList *reverse_table_list(TableList *table_list)
120
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list)
122
124
while (table_list)
124
TableList *next= table_list->next_local;
126
TABLE_LIST *next= table_list->next_local;
125
127
table_list->next_local= prev;
126
128
prev= table_list;
127
129
table_list= next;
154
do_rename(THD *thd, TableList *ren_table, char *new_db, char *new_table_name,
156
do_rename(THD *thd, TABLE_LIST *ren_table, char *new_db, char *new_table_name,
155
157
char *new_table_alias, bool skip_error)
158
160
char name[FN_REFLEN];
159
161
const char *new_alias, *old_alias;
160
/* TODO: What should this really be set to - it doesn't
161
get set anywhere before it's used? */
162
enum legacy_db_type table_type=DB_TYPE_UNKNOWN;
162
enum legacy_db_type table_type;
164
DBUG_ENTER("do_rename");
164
166
if (lower_case_table_names == 2)
176
178
if (!access(name,F_OK))
178
180
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
179
return(1); // This can't be skipped
181
DBUG_RETURN(1); // This can't be skipped
181
183
build_table_filename(name, sizeof(name),
182
184
ren_table->db, old_alias, reg_ext, 0);
184
rc= mysql_rename_table(ha_resolve_by_legacy_type(thd, table_type),
186
rc= mysql_rename_table(ha_resolve_by_legacy_type(thd, table_type),
185
187
ren_table->db, old_alias,
186
188
new_db, new_alias, 0);
187
189
if (rc && !skip_error)
214
216
true rename failed
218
rename_tables(THD *thd, TableList *table_list, bool skip_error)
220
rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error)
220
TableList *ren_table, *new_table;
222
TABLE_LIST *ren_table, *new_table;
224
DBUG_ENTER("rename_tables");
222
226
for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
224
228
new_table= ren_table->next_local;
225
229
if (do_rename(thd, ren_table, new_table->db, new_table->table_name,
226
230
new_table->alias, skip_error))
231
DBUG_RETURN(ren_table);