Drizzled Public API Documentation

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 
32 namespace drizzled {
33 
35 {
36  TableList *first_table= (TableList *) lex().select_lex.table_list.first;
37  TableList *all_tables= lex().query_tables;
38  assert(first_table == all_tables && first_table != 0);
39  TableList *table;
40 
41  if (session().inTransaction())
42  {
43  my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
44  return true;
45  }
46 
47  for (table= first_table; table; table= table->next_local->next_local)
48  {
49  TableList old_list, new_list;
50  /*
51  we do not need initialize old_list and new_list because we will
52  come table[0] and table->next[0] there
53  */
54  old_list= table[0];
55  new_list= table->next_local[0];
56  }
57 
58  if (renameTables(first_table))
59  {
60  return true;
61  }
62 
63  return false;
64 }
65 
66 bool statement::RenameTable::renameTables(TableList *table_list)
67 {
68  bool error= true;
69  TableList *ren_table= NULL;
70 
71  /*
72  Avoid problems with a rename on a table that we have locked or
73  if the user is trying to to do this in a transcation context
74  */
75  if (session().inTransaction())
76  {
77  my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
78  return true;
79  }
80 
81  if (session().wait_if_global_read_lock(false, true))
82  return true;
83 
84  {
85  boost::mutex::scoped_lock scopedLock(table::Cache::mutex()); /* Rename table lock for exclusive access */
86 
87  if (not session().lock_table_names_exclusively(table_list))
88  {
89  error= false;
90  ren_table= renameTablesInList(table_list, false);
91 
92  if (ren_table)
93  {
94  /* Rename didn't succeed; rename back the tables in reverse order */
95  TableList *table;
96 
97  /* Reverse the table list */
98  table_list= reverseTableList(table_list);
99 
100  /* Find the last renamed table */
101  for (table= table_list;
102  table->next_local != ren_table;
103  table= table->next_local->next_local)
104  { /* do nothing */ }
105 
106  table= table->next_local->next_local; // Skip error table
107 
108  /* Revert to old names */
109  renameTablesInList(table, true);
110  error= true;
111  }
112 
113  table_list->unlock_table_names();
114  }
115  }
116 
117  /* Lets hope this doesn't fail as the result will be messy */
118  if (not error)
119  {
120  TransactionServices::rawStatement(session(), *session().getQueryString(), *session().schema());
121  session().my_ok();
122  }
123 
124  session().startWaitingGlobalReadLock();
125 
126  return error;
127 }
128 
129 TableList *statement::RenameTable::reverseTableList(TableList *table_list)
130 {
131  TableList *prev= NULL;
132 
133  while (table_list)
134  {
135  TableList *next= table_list->next_local;
136  table_list->next_local= prev;
137  prev= table_list;
138  table_list= next;
139  }
140  return prev;
141 }
142 
143 bool statement::RenameTable::rename(TableList *ren_table,
144  const char *new_db,
145  const char *new_table_name,
146  bool skip_error)
147 {
148  bool rc= true;
149  const char *new_alias, *old_alias;
150 
151  {
152  old_alias= ren_table->getTableName();
153  new_alias= new_table_name;
154  }
155 
156  plugin::StorageEngine *engine= NULL;
157  message::table::shared_ptr table_message;
158 
159  identifier::Table old_identifier(ren_table->getSchemaName(), old_alias, message::Table::STANDARD);
160 
161  if (not (table_message= plugin::StorageEngine::getTableMessage(session(), old_identifier)))
162  {
163  my_error(ER_TABLE_UNKNOWN, old_identifier);
164  return true;
165  }
166 
167  engine= plugin::StorageEngine::findByName(session(), table_message->engine().name());
168 
169  identifier::Table new_identifier(new_db, new_alias, message::Table::STANDARD);
170  if (plugin::StorageEngine::doesTableExist(session(), new_identifier))
171  {
172  my_error(ER_TABLE_EXISTS_ERROR, new_identifier);
173  return 1; // This can't be skipped
174  }
175 
176  rc= rename_table(session(), engine, old_identifier, new_identifier);
177  if (rc && ! skip_error)
178  return true;
179 
180  return false;
181 }
182 
183 TableList *statement::RenameTable::renameTablesInList(TableList *table_list,
184  bool skip_error)
185 {
186  TableList *ren_table, *new_table;
187 
188  for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
189  {
190  new_table= ren_table->next_local;
191  if (rename(ren_table, new_table->getSchemaName(), new_table->getTableName(), skip_error))
192  return ren_table;
193  }
194  return 0;
195 }
196 
197 } /* namespace drizzled */