22 #include "sql_rename.h"
23 #include "sql_cache.h"
24 #include "sql_table.h"
26 #include "sql_trigger.h"
29 #include "sql_handler.h"
42 bool mysql_rename_tables(THD *thd,
TABLE_LIST *table_list,
bool silent)
48 char *rename_log_table[2]= {NULL, NULL};
49 DBUG_ENTER(
"mysql_rename_tables");
56 if (thd->locked_tables_mode || thd->in_active_multi_stmt_transaction())
58 my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
59 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
63 mysql_ha_rm_tables(thd, table_list);
65 if (logger.is_log_table_enabled(QUERY_LOG_GENERAL) ||
66 logger.is_log_table_enabled(QUERY_LOG_SLOW))
79 for (to_table= 0, ren_table= table_list; ren_table;
80 to_table= 1 - to_table, ren_table= ren_table->next_local)
82 int log_table_rename= 0;
84 if ((log_table_rename=
85 check_if_log_table(ren_table->db_length, ren_table->db,
86 ren_table->table_name_length,
87 ren_table->table_name, 1)))
95 if (rename_log_table[log_table_rename])
98 rename_log_table[log_table_rename]= NULL;
105 my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), ren_table->table_name,
106 ren_table->table_name);
118 my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), ren_table->table_name,
119 ren_table->table_name);
125 rename_log_table[log_table_rename]= ren_table->table_name;
130 if (rename_log_table[0] || rename_log_table[1])
132 if (rename_log_table[0])
133 my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), rename_log_table[0],
134 rename_log_table[0]);
136 my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), rename_log_table[1],
137 rename_log_table[1]);
142 if (
lock_table_names(thd, table_list, 0, thd->variables.lock_wait_timeout, 0))
145 for (ren_table= table_list; ren_table; ren_table= ren_table->next_local)
147 ren_table->table_name, FALSE);
154 if ((ren_table=rename_tables(thd,table_list,0)))
160 table_list= reverse_table_list(table_list);
163 for (table= table_list;
164 table->next_local != ren_table ;
165 table= table->next_local->next_local) ;
166 table= table->next_local->next_local;
168 rename_tables(thd, table, 1);
171 table_list= reverse_table_list(table_list);
176 if (!silent && !error)
178 binlog_error= write_bin_log(thd, TRUE, thd->query(), thd->query_length());
184 query_cache_invalidate3(thd, table_list, 0);
187 DBUG_RETURN(error || binlog_error);
208 table_list->next_local= prev;
237 do_rename(THD *thd,
TABLE_LIST *ren_table,
char *new_db,
char *new_table_name,
238 char *new_table_alias,
bool skip_error)
241 char name[FN_REFLEN + 1];
242 const char *new_alias, *old_alias;
243 frm_type_enum frm_type;
244 enum legacy_db_type table_type;
246 DBUG_ENTER(
"do_rename");
248 if (lower_case_table_names == 2)
250 old_alias= ren_table->alias;
251 new_alias= new_table_alias;
255 old_alias= ren_table->table_name;
256 new_alias= new_table_name;
258 DBUG_ASSERT(new_alias);
260 build_table_filename(name,
sizeof(name) - 1,
261 new_db, new_alias, reg_ext, 0);
262 if (!access(name,F_OK))
264 my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
267 build_table_filename(name,
sizeof(name) - 1,
268 ren_table->db, old_alias, reg_ext, 0);
270 frm_type= dd_frm_type(thd, name, &table_type);
275 if (!(rc= mysql_rename_table(ha_resolve_by_legacy_type(thd,
277 ren_table->db, old_alias,
278 new_db, new_alias, 0)))
282 ren_table->table_name,
292 (void) mysql_rename_table(ha_resolve_by_legacy_type(thd,
295 ren_table->db, old_alias, NO_FK_CHECKS);
306 if (thd->lex->sql_command != SQLCOM_ALTER_DB_UPGRADE &&
307 strcmp(ren_table->db, new_db))
308 my_error(ER_FORBID_SCHEMA_CHANGE, MYF(0), ren_table->db,
311 rc= mysql_rename_view(thd, new_db, new_alias, ren_table);
317 char errbuf[MYSYS_STRERROR_SIZE];
318 my_error(ER_FILE_NOT_FOUND, MYF(0), name,
319 my_errno, my_strerror(errbuf,
sizeof(errbuf), my_errno));
324 thd->add_to_binlog_accessed_dbs(ren_table->db);
325 thd->add_to_binlog_accessed_dbs(new_db);
327 if (rc && !skip_error)
358 rename_tables(THD *thd,
TABLE_LIST *table_list,
bool skip_error)
362 DBUG_ENTER(
"rename_tables");
364 for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
366 new_table= ren_table->next_local;
367 if (do_rename(thd, ren_table, new_table->db, new_table->table_name,
368 new_table->alias, skip_error))
369 DBUG_RETURN(ren_table);