1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2009 Sun Microsystems, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <drizzled/show.h>
#include <drizzled/lock.h>
#include <drizzled/session.h>
#include <drizzled/statement/rename_table.h>
#include "drizzled/sql_table.h"
#include "drizzled/pthread_globals.h"
#include "drizzled/plugin/storage_engine.h"
namespace drizzled
{
bool statement::RenameTable::execute()
{
TableList *first_table= (TableList *) getSession()->lex->select_lex.table_list.first;
TableList *all_tables= getSession()->lex->query_tables;
assert(first_table == all_tables && first_table != 0);
TableList *table;
if (getSession()->inTransaction())
{
my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
return true;
}
for (table= first_table; table; table= table->next_local->next_local)
{
TableList old_list, new_list;
/*
we do not need initialize old_list and new_list because we will
come table[0] and table->next[0] there
*/
old_list= table[0];
new_list= table->next_local[0];
}
if (renameTables(first_table))
{
return true;
}
return false;
}
bool statement::RenameTable::renameTables(TableList *table_list)
{
bool error= true;
TableList *ren_table= NULL;
/*
Avoid problems with a rename on a table that we have locked or
if the user is trying to to do this in a transcation context
*/
if (getSession()->inTransaction())
{
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
return true;
}
if (getSession()->wait_if_global_read_lock(false, true))
return true;
{
boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex()); /* Rename table lock for exclusive access */
if (not getSession()->lock_table_names_exclusively(table_list))
{
error= false;
ren_table= renameTablesInList(table_list, false);
if (ren_table)
{
/* Rename didn't succeed; rename back the tables in reverse order */
TableList *table;
/* Reverse the table list */
table_list= reverseTableList(table_list);
/* Find the last renamed table */
for (table= table_list;
table->next_local != ren_table;
table= table->next_local->next_local)
{ /* do nothing */ }
table= table->next_local->next_local; // Skip error table
/* Revert to old names */
renameTablesInList(table, true);
error= true;
}
table_list->unlock_table_names();
}
}
/* Lets hope this doesn't fail as the result will be messy */
if (not error)
{
write_bin_log(getSession(), *getSession()->getQueryString());
getSession()->my_ok();
}
getSession()->startWaitingGlobalReadLock();
return error;
}
TableList *statement::RenameTable::reverseTableList(TableList *table_list)
{
TableList *prev= NULL;
while (table_list)
{
TableList *next= table_list->next_local;
table_list->next_local= prev;
prev= table_list;
table_list= next;
}
return prev;
}
bool statement::RenameTable::rename(TableList *ren_table,
const char *new_db,
const char *new_table_name,
bool skip_error)
{
bool rc= true;
const char *new_alias, *old_alias;
{
old_alias= ren_table->getTableName();
new_alias= new_table_name;
}
plugin::StorageEngine *engine= NULL;
message::table::shared_ptr table_proto;
identifier::Table old_identifier(ren_table->getSchemaName(), old_alias, message::Table::STANDARD);
if (plugin::StorageEngine::getTableDefinition(*getSession(), old_identifier, table_proto) != EEXIST)
{
my_error(ER_TABLE_UNKNOWN, old_identifier);
return true;
}
engine= plugin::StorageEngine::findByName(*getSession(), table_proto->engine().name());
identifier::Table new_identifier(new_db, new_alias, message::Table::STANDARD);
if (plugin::StorageEngine::doesTableExist(*getSession(), new_identifier))
{
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
return 1; // This can't be skipped
}
rc= rename_table(*getSession(), engine, old_identifier, new_identifier);
if (rc && ! skip_error)
return true;
return false;
}
TableList *statement::RenameTable::renameTablesInList(TableList *table_list,
bool skip_error)
{
TableList *ren_table, *new_table;
for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
{
new_table= ren_table->next_local;
if (rename(ren_table, new_table->getSchemaName(), new_table->getTableName(), skip_error))
return ren_table;
}
return 0;
}
} /* namespace drizzled */
|