1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems, Inc.
4
* Copyright (C) 2009 Sun Microsystems
6
6
* This program is free software; you can redistribute it and/or modify
7
7
* it under the terms of the GNU General Public License as published by
25
25
#include <drizzled/statement/rename_table.h>
26
26
#include "drizzled/sql_table.h"
27
27
#include "drizzled/pthread_globals.h"
28
#include "drizzled/plugin/storage_engine.h"
33
32
bool statement::RenameTable::execute()
35
TableList *first_table= (TableList *) getSession()->lex->select_lex.table_list.first;
36
TableList *all_tables= getSession()->lex->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 (wait_if_global_read_lock(session, 0, 1))
84
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))
89
ren_table= renameTablesInList(table_list, false);
93
/* Rename didn't succeed; rename back the tables in reverse order */
96
/* Reverse the table list */
97
table_list= reverseTableList(table_list);
99
/* Find the last renamed table */
100
for (table= table_list;
101
table->next_local != ren_table;
102
table= table->next_local->next_local)
105
table= table->next_local->next_local; // Skip error table
107
/* Revert to old names */
108
renameTablesInList(table, true);
112
table_list->unlock_table_names();
74
LOCK_open.lock(); /* Rename table lock for exclusive access */
75
if (lock_table_names_exclusively(session, table_list))
82
ren_table= renameTablesInList(table_list, 0);
85
/* Rename didn't succeed; rename back the tables in reverse order */
88
/* Reverse the table list */
89
table_list= reverseTableList(table_list);
91
/* Find the last renamed table */
92
for (table= table_list;
93
table->next_local != ren_table;
94
table= table->next_local->next_local)
96
table= table->next_local->next_local; // Skip error table
97
/* Revert to old names */
98
renameTablesInList(table, 1);
102
An exclusive lock on table names is satisfactory to ensure
103
no other thread accesses this table.
104
We still should unlock LOCK_open as early as possible, to provide
105
higher concurrency - query_cache_invalidate can take minutes to
116
110
/* Lets hope this doesn't fail as the result will be messy */
119
write_bin_log(getSession(), *getSession()->getQueryString());
120
getSession()->my_ok();
113
write_bin_log(session, session->query.c_str());
123
getSession()->startWaitingGlobalReadLock();
117
LOCK_open.lock(); /* unlock all tables held */
118
unlock_table_names(table_list, NULL);
122
start_waiting_global_read_lock(session);
155
154
plugin::StorageEngine *engine= NULL;
156
message::table::shared_ptr table_proto;
158
identifier::Table old_identifier(ren_table->getSchemaName(), old_alias, message::Table::STANDARD);
160
if (plugin::StorageEngine::getTableDefinition(*getSession(), old_identifier, table_proto) != EEXIST)
155
message::Table table_proto;
157
TableIdentifier old_identifier(ren_table->getSchemaName(), old_alias, message::Table::STANDARD);
159
if (plugin::StorageEngine::getTableDefinition(*session, old_identifier, table_proto) != EEXIST)
162
my_error(ER_TABLE_UNKNOWN, old_identifier);
161
my_error(ER_NO_SUCH_TABLE, MYF(0), ren_table->getSchemaName(), old_alias);
166
engine= plugin::StorageEngine::findByName(*getSession(), table_proto->engine().name());
165
engine= plugin::StorageEngine::findByName(*session, table_proto.engine().name());
168
identifier::Table new_identifier(new_db, new_alias, message::Table::STANDARD);
169
if (plugin::StorageEngine::doesTableExist(*getSession(), new_identifier))
167
TableIdentifier new_identifier(new_db, new_alias, message::Table::STANDARD);
168
if (plugin::StorageEngine::doesTableExist(*session, new_identifier))
171
170
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
172
171
return 1; // This can't be skipped
175
rc= rename_table(*getSession(), engine, old_identifier, new_identifier);
174
rc= mysql_rename_table(*session, engine, old_identifier, new_identifier);
176
175
if (rc && ! skip_error)