18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
22
#include <drizzled/show.h>
23
23
#include <drizzled/lock.h>
24
24
#include <drizzled/session.h>
25
25
#include <drizzled/statement/rename_table.h>
26
#include <drizzled/sql_table.h>
27
#include <drizzled/pthread_globals.h>
28
#include <drizzled/plugin/storage_engine.h>
26
#include "drizzled/sql_table.h"
27
#include "drizzled/pthread_globals.h"
33
32
bool statement::RenameTable::execute()
35
TableList *first_table= (TableList *) getSession()->getLex()->select_lex.table_list.first;
36
TableList *all_tables= getSession()->getLex()->query_tables;
34
TableList *first_table= (TableList *) session->lex->select_lex.table_list.first;
35
TableList *all_tables= session->lex->query_tables;
37
36
assert(first_table == all_tables && first_table != 0);
40
if (getSession()->inTransaction())
42
my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
46
38
for (table= first_table; table; table= table->next_local->next_local)
48
40
TableList old_list, new_list;
71
62
Avoid problems with a rename on a table that we have locked or
72
63
if the user is trying to to do this in a transcation context
74
if (getSession()->inTransaction())
65
if (session->inTransaction())
76
67
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
80
if (getSession()->wait_if_global_read_lock(false, true))
71
if (session->wait_if_global_read_lock(false, true))
84
75
boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex()); /* Rename table lock for exclusive access */
86
if (not getSession()->lock_table_names_exclusively(table_list))
77
if (not session->lock_table_names_exclusively(table_list))
89
80
ren_table= renameTablesInList(table_list, false);
116
107
/* Lets hope this doesn't fail as the result will be messy */
119
write_bin_log(getSession(), *getSession()->getQueryString());
120
getSession()->my_ok();
110
write_bin_log(session, *session->getQueryString());
123
getSession()->startWaitingGlobalReadLock();
114
session->startWaitingGlobalReadLock();
155
146
plugin::StorageEngine *engine= NULL;
156
message::table::shared_ptr table_message;
147
message::table::shared_ptr table_proto;
158
149
identifier::Table old_identifier(ren_table->getSchemaName(), old_alias, message::Table::STANDARD);
160
if (not (table_message= plugin::StorageEngine::getTableMessage(*getSession(), old_identifier)))
151
if (plugin::StorageEngine::getTableDefinition(*session, old_identifier, table_proto) != EEXIST)
162
my_error(ER_TABLE_UNKNOWN, old_identifier);
153
my_error(ER_NO_SUCH_TABLE, MYF(0), ren_table->getSchemaName(), old_alias);
166
engine= plugin::StorageEngine::findByName(*getSession(), table_message->engine().name());
157
engine= plugin::StorageEngine::findByName(*session, table_proto->engine().name());
168
159
identifier::Table new_identifier(new_db, new_alias, message::Table::STANDARD);
169
if (plugin::StorageEngine::doesTableExist(*getSession(), new_identifier))
160
if (plugin::StorageEngine::doesTableExist(*session, new_identifier))
171
my_error(ER_TABLE_EXISTS_ERROR, new_identifier);
162
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
172
163
return 1; // This can't be skipped
175
rc= rename_table(*getSession(), engine, old_identifier, new_identifier);
166
rc= rename_table(*session, engine, old_identifier, new_identifier);
176
167
if (rc && ! skip_error)