23 #include <drizzled/lock.h>
24 #include <drizzled/session.h>
25 #include <drizzled/statement/rename_table.h>
26 #include <drizzled/pthread_globals.h>
27 #include <drizzled/plugin/storage_engine.h>
28 #include <drizzled/transaction_services.h>
29 #include <drizzled/sql_lex.h>
30 #include <drizzled/table/cache.h>
37 TableList *all_tables= lex().query_tables;
38 assert(first_table == all_tables && first_table != 0);
41 if (session().inTransaction())
43 my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
58 if (renameTables(first_table))
66 bool statement::RenameTable::renameTables(
TableList *table_list)
75 if (session().inTransaction())
77 my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
81 if (session().wait_if_global_read_lock(
false,
true))
85 boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
87 if (not session().lock_table_names_exclusively(table_list))
90 ren_table= renameTablesInList(table_list,
false);
98 table_list= reverseTableList(table_list);
101 for (table= table_list;
109 renameTablesInList(table,
true);
124 session().startWaitingGlobalReadLock();
129 TableList *statement::RenameTable::reverseTableList(TableList *table_list)
131 TableList *prev= NULL;
135 TableList *next= table_list->next_local;
136 table_list->next_local= prev;
143 bool statement::RenameTable::rename(TableList *ren_table,
145 const char *new_table_name,
149 const char *new_alias, *old_alias;
152 old_alias= ren_table->getTableName();
153 new_alias= new_table_name;
156 plugin::StorageEngine *engine= NULL;
157 message::table::shared_ptr table_message;
159 identifier::Table old_identifier(ren_table->getSchemaName(), old_alias, message::Table::STANDARD);
161 if (not (table_message= plugin::StorageEngine::getTableMessage(session(), old_identifier)))
163 my_error(ER_TABLE_UNKNOWN, old_identifier);
167 engine= plugin::StorageEngine::findByName(session(), table_message->engine().name());
169 identifier::Table new_identifier(new_db, new_alias, message::Table::STANDARD);
172 my_error(ER_TABLE_EXISTS_ERROR, new_identifier);
176 rc= rename_table(session(), engine, old_identifier, new_identifier);
177 if (rc && ! skip_error)
183 TableList *statement::RenameTable::renameTablesInList(TableList *table_list,
186 TableList *ren_table, *new_table;
188 for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
190 new_table= ren_table->next_local;
191 if (rename(ren_table, new_table->getSchemaName(), new_table->getTableName(), skip_error))