~ubuntu-branches/ubuntu/saucy/drizzle/saucy-proposed

« back to all changes in this revision

Viewing changes to .pc/debian-changes-2010.12.06-0ubuntu4/drizzled/statement/rename_table.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2011-01-04 09:31:58 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20110104093158-smhgvkfdi2y9au3i
Tags: 2011.01.07-0ubuntu1
New upstream release.

Show diffs side-by-side

added added

removed removed

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