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