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
4
* Copyright (C) 2009 Sun Microsystems, Inc.
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"
32
33
bool statement::RenameTable::execute()
34
TableList *first_table= (TableList *) session->lex->select_lex.table_list.first;
35
TableList *all_tables= session->lex->query_tables;
35
TableList *first_table= (TableList *) getSession()->lex->select_lex.table_list.first;
36
TableList *all_tables= getSession()->lex->query_tables;
36
37
assert(first_table == all_tables && first_table != 0);
40
if (getSession()->inTransaction())
42
my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
38
46
for (table= first_table; table; table= table->next_local->next_local)
40
48
TableList old_list, new_list;
62
71
Avoid problems with a rename on a table that we have locked or
63
72
if the user is trying to to do this in a transcation context
65
if (session->inTransaction())
74
if (getSession()->inTransaction())
67
76
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
71
if (session->wait_if_global_read_lock(false, true))
80
if (getSession()->wait_if_global_read_lock(false, true))
74
LOCK_open.lock(); /* Rename table lock for exclusive access */
75
if (session->lock_table_names_exclusively(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
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();
110
116
/* Lets hope this doesn't fail as the result will be messy */
113
write_bin_log(session, *session->getQueryString());
119
write_bin_log(getSession(), *getSession()->getQueryString());
120
getSession()->my_ok();
117
LOCK_open.lock(); /* unlock all tables held */
118
table_list->unlock_table_names();
122
session->startWaitingGlobalReadLock();
123
getSession()->startWaitingGlobalReadLock();
154
155
plugin::StorageEngine *engine= NULL;
155
message::TablePtr 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)
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)
161
my_error(ER_NO_SUCH_TABLE, MYF(0), ren_table->getSchemaName(), old_alias);
162
my_error(ER_TABLE_UNKNOWN, old_identifier);
165
engine= plugin::StorageEngine::findByName(*session, table_proto->engine().name());
166
engine= plugin::StorageEngine::findByName(*getSession(), table_proto->engine().name());
167
TableIdentifier new_identifier(new_db, new_alias, message::Table::STANDARD);
168
if (plugin::StorageEngine::doesTableExist(*session, new_identifier))
168
identifier::Table new_identifier(new_db, new_alias, message::Table::STANDARD);
169
if (plugin::StorageEngine::doesTableExist(*getSession(), new_identifier))
170
171
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
171
172
return 1; // This can't be skipped
174
rc= mysql_rename_table(*session, engine, old_identifier, new_identifier);
175
rc= rename_table(*getSession(), engine, old_identifier, new_identifier);
175
176
if (rc && ! skip_error)