~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/rename_table.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems
 
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
5
5
 *
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"
28
29
 
29
30
namespace drizzled
30
31
{
31
32
 
32
33
bool statement::RenameTable::execute()
33
34
{
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);
37
38
  TableList *table;
 
39
 
 
40
  if (getSession()->inTransaction())
 
41
  {
 
42
    my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
 
43
    return true;
 
44
  }
 
45
 
38
46
  for (table= first_table; table; table= table->next_local->next_local)
39
47
  {
40
48
    TableList old_list, new_list;
46
54
    new_list= table->next_local[0];
47
55
  }
48
56
 
49
 
  if (! session->endActiveTransaction() || renameTables(first_table))
 
57
  if (renameTables(first_table))
50
58
  {
51
59
    return true;
52
60
  }
 
61
 
53
62
  return false;
54
63
}
55
64
 
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
64
73
  */
65
 
  if (session->inTransaction())
 
74
  if (getSession()->inTransaction())
66
75
  {
67
76
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
68
77
    return true;
69
78
  }
70
79
 
71
 
  if (session->wait_if_global_read_lock(false, true))
 
80
  if (getSession()->wait_if_global_read_lock(false, true))
72
81
    return true;
73
82
 
74
 
  LOCK_open.lock(); /* Rename table lock for exclusive access */
75
 
  if (session->lock_table_names_exclusively(table_list))
76
 
  {
77
 
    LOCK_open.unlock();
78
 
    goto err;
79
 
  }
80
 
 
81
 
  error= false;
82
 
  ren_table= renameTablesInList(table_list, 0);
83
 
  if (ren_table)
84
 
  {
85
 
    /* Rename didn't succeed;  rename back the tables in reverse order */
86
 
    TableList *table;
87
 
 
88
 
    /* Reverse the table list */
89
 
    table_list= reverseTableList(table_list);
90
 
 
91
 
    /* Find the last renamed table */
92
 
    for (table= table_list;
93
 
         table->next_local != ren_table;
94
 
         table= table->next_local->next_local) 
95
 
    { /* do nothing */ }
96
 
    table= table->next_local->next_local;               // Skip error table
97
 
    /* Revert to old names */
98
 
    renameTablesInList(table, 1);
99
 
    error= true;
100
 
  }
101
 
  /*
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
106
 
    complete.
107
 
  */
108
 
  LOCK_open.unlock();
 
83
  {
 
84
    boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex()); /* Rename table lock for exclusive access */
 
85
 
 
86
    if (not getSession()->lock_table_names_exclusively(table_list))
 
87
    {
 
88
      error= false;
 
89
      ren_table= renameTablesInList(table_list, false);
 
90
 
 
91
      if (ren_table)
 
92
      {
 
93
        /* Rename didn't succeed;  rename back the tables in reverse order */
 
94
        TableList *table;
 
95
 
 
96
        /* Reverse the table list */
 
97
        table_list= reverseTableList(table_list);
 
98
 
 
99
        /* Find the last renamed table */
 
100
        for (table= table_list;
 
101
             table->next_local != ren_table;
 
102
             table= table->next_local->next_local) 
 
103
        { /* do nothing */ }
 
104
 
 
105
        table= table->next_local->next_local;           // Skip error table
 
106
 
 
107
        /* Revert to old names */
 
108
        renameTablesInList(table, true);
 
109
        error= true;
 
110
      }
 
111
 
 
112
      table_list->unlock_table_names();
 
113
    }
 
114
  }
109
115
 
110
116
  /* Lets hope this doesn't fail as the result will be messy */
111
117
  if (not error)
112
118
  {
113
 
    write_bin_log(session, *session->getQueryString());
114
 
    session->my_ok();
 
119
    write_bin_log(getSession(), *getSession()->getQueryString());
 
120
    getSession()->my_ok();
115
121
  }
116
122
 
117
 
  LOCK_open.lock(); /* unlock all tables held */
118
 
  table_list->unlock_table_names();
119
 
  LOCK_open.unlock();
120
 
 
121
 
err:
122
 
  session->startWaitingGlobalReadLock();
 
123
  getSession()->startWaitingGlobalReadLock();
123
124
 
124
125
  return error;
125
126
}
152
153
  }
153
154
 
154
155
  plugin::StorageEngine *engine= NULL;
155
 
  message::TablePtr table_proto;
156
 
 
157
 
  TableIdentifier old_identifier(ren_table->getSchemaName(), old_alias, message::Table::STANDARD);
158
 
 
159
 
  if (plugin::StorageEngine::getTableDefinition(*session, old_identifier, table_proto) != EEXIST)
 
156
  message::table::shared_ptr table_proto;
 
157
 
 
158
  identifier::Table old_identifier(ren_table->getSchemaName(), old_alias, message::Table::STANDARD);
 
159
 
 
160
  if (plugin::StorageEngine::getTableDefinition(*getSession(), old_identifier, table_proto) != EEXIST)
160
161
  {
161
 
    my_error(ER_NO_SUCH_TABLE, MYF(0), ren_table->getSchemaName(), old_alias);
 
162
    my_error(ER_TABLE_UNKNOWN, old_identifier);
162
163
    return true;
163
164
  }
164
165
 
165
 
  engine= plugin::StorageEngine::findByName(*session, table_proto->engine().name());
 
166
  engine= plugin::StorageEngine::findByName(*getSession(), table_proto->engine().name());
166
167
 
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))
169
170
  {
170
171
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
171
172
    return 1; // This can't be skipped
172
173
  }
173
174
 
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)
176
177
    return true;
177
178