18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
22
#include <drizzled/show.h>
23
23
#include <drizzled/lock.h>
24
24
#include <drizzled/session.h>
25
25
#include <drizzled/statement/rename_table.h>
26
#include "drizzled/sql_table.h"
27
#include "drizzled/pthread_globals.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>
32
34
bool statement::RenameTable::execute()
34
TableList *first_table= (TableList *) session->lex->select_lex.table_list.first;
35
TableList *all_tables= session->lex->query_tables;
36
TableList *first_table= (TableList *) lex().select_lex.table_list.first;
37
TableList *all_tables= lex().query_tables;
36
38
assert(first_table == all_tables && first_table != 0);
41
if (session().inTransaction())
43
my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
38
47
for (table= first_table; table; table= table->next_local->next_local)
40
49
TableList old_list, new_list;
62
72
Avoid problems with a rename on a table that we have locked or
63
73
if the user is trying to to do this in a transcation context
65
if (session->inTransaction())
75
if (session().inTransaction())
67
77
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))
81
if (session().wait_if_global_read_lock(false, true))
75
85
boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex()); /* Rename table lock for exclusive access */
77
if (not session->lock_table_names_exclusively(table_list))
87
if (not session().lock_table_names_exclusively(table_list))
80
90
ren_table= renameTablesInList(table_list, false);
107
117
/* Lets hope this doesn't fail as the result will be messy */
110
write_bin_log(session, *session->getQueryString());
120
TransactionServices &transaction_services= TransactionServices::singleton();
121
transaction_services.rawStatement(session(),
122
*session().getQueryString(),
123
*session().schema());
114
session->startWaitingGlobalReadLock();
127
session().startWaitingGlobalReadLock();
146
159
plugin::StorageEngine *engine= NULL;
147
message::table::shared_ptr table_proto;
149
TableIdentifier old_identifier(ren_table->getSchemaName(), old_alias, message::Table::STANDARD);
151
if (plugin::StorageEngine::getTableDefinition(*session, old_identifier, table_proto) != EEXIST)
160
message::table::shared_ptr table_message;
162
identifier::Table old_identifier(ren_table->getSchemaName(), old_alias, message::Table::STANDARD);
164
if (not (table_message= plugin::StorageEngine::getTableMessage(session(), old_identifier)))
153
my_error(ER_NO_SUCH_TABLE, MYF(0), ren_table->getSchemaName(), old_alias);
166
my_error(ER_TABLE_UNKNOWN, old_identifier);
157
engine= plugin::StorageEngine::findByName(*session, table_proto->engine().name());
170
engine= plugin::StorageEngine::findByName(session(), table_message->engine().name());
159
TableIdentifier new_identifier(new_db, new_alias, message::Table::STANDARD);
160
if (plugin::StorageEngine::doesTableExist(*session, new_identifier))
172
identifier::Table new_identifier(new_db, new_alias, message::Table::STANDARD);
173
if (plugin::StorageEngine::doesTableExist(session(), new_identifier))
162
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
175
my_error(ER_TABLE_EXISTS_ERROR, new_identifier);
163
176
return 1; // This can't be skipped
166
rc= mysql_rename_table(*session, engine, old_identifier, new_identifier);
179
rc= rename_table(session(), engine, old_identifier, new_identifier);
167
180
if (rc && ! skip_error)