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>
31 #include <drizzled/catalog/instance.h>
38 TableList *all_tables= lex().query_tables;
39 assert(first_table == all_tables && first_table != 0);
42 if (session().inTransaction())
44 my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
59 if (renameTables(first_table))
67 bool statement::RenameTable::renameTables(
TableList *table_list)
76 if (session().inTransaction())
78 my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
82 if (session().wait_if_global_read_lock(
false,
true))
86 boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
88 if (not session().lock_table_names_exclusively(table_list))
91 ren_table= renameTablesInList(table_list,
false);
99 table_list= reverseTableList(table_list);
102 for (table= table_list;
110 renameTablesInList(table,
true);
125 session().startWaitingGlobalReadLock();
130 TableList *statement::RenameTable::reverseTableList(TableList *table_list)
132 TableList *prev= NULL;
136 TableList *next= table_list->next_local;
137 table_list->next_local= prev;
144 bool statement::RenameTable::rename(TableList *ren_table,
146 const char *new_table_name,
150 const char *new_alias, *old_alias;
153 old_alias= ren_table->getTableName();
154 new_alias= new_table_name;
157 plugin::StorageEngine *engine= NULL;
158 message::table::shared_ptr table_message;
160 identifier::Table old_identifier(session().catalog().identifier(),
161 ren_table->getSchemaName(),
163 message::Table::STANDARD);
165 if (not (table_message= plugin::StorageEngine::getTableMessage(session(), old_identifier)))
167 my_error(ER_TABLE_UNKNOWN, old_identifier);
171 engine= plugin::StorageEngine::findByName(session(), table_message->engine().name());
173 identifier::Table new_identifier(session().catalog().identifier(),
174 new_db, new_alias, message::Table::STANDARD);
177 my_error(ER_TABLE_EXISTS_ERROR, new_identifier);
181 rc= rename_table(session(), engine, old_identifier, new_identifier);
182 if (rc && ! skip_error)
188 TableList *statement::RenameTable::renameTablesInList(TableList *table_list,
191 TableList *ren_table, *new_table;
193 for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
195 new_table= ren_table->next_local;
196 if (rename(ren_table, new_table->getSchemaName(), new_table->getTableName(), skip_error))
void my_ok(ha_rows affected_rows=0, ha_rows found_rows_arg=0, uint64_t passed_id=0, const char *message=NULL)
static void rawStatement(Session &, const std::string &query, const std::string &schema)
void unlock_table_names(TableList *last_table=NULL)
static bool doesTableExist(Session &session, const drizzled::identifier::Table &identifier, bool include_temporary_tables=true)