~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/rename_table.cc

  • Committer: Brian Aker
  • Date: 2009-02-21 00:18:15 UTC
  • Revision ID: brian@tangent.org-20090221001815-x20e8h71e984lvs1
Completion (?) of uint conversion.

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
 
#include "drizzled/plugin/storage_engine.h"
29
 
 
30
 
namespace drizzled
31
 
{
32
 
 
33
 
bool statement::RenameTable::execute()
34
 
{
35
 
  TableList *first_table= (TableList *) getSession()->lex->select_lex.table_list.first;
36
 
  TableList *all_tables= getSession()->lex->query_tables;
37
 
  assert(first_table == all_tables && first_table != 0);
38
 
  TableList *table;
39
 
 
40
 
  if (getSession()->inTransaction())
41
 
  {
42
 
    my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
43
 
    return true;
44
 
  }
45
 
 
46
 
  for (table= first_table; table; table= table->next_local->next_local)
47
 
  {
48
 
    TableList old_list, new_list;
49
 
    /*
50
 
       we do not need initialize old_list and new_list because we will
51
 
       come table[0] and table->next[0] there
52
 
     */
53
 
    old_list= table[0];
54
 
    new_list= table->next_local[0];
55
 
  }
56
 
 
57
 
  if (renameTables(first_table))
58
 
  {
59
 
    return true;
60
 
  }
61
 
 
62
 
  return false;
63
 
}
64
 
 
65
 
bool statement::RenameTable::renameTables(TableList *table_list)
66
 
{
67
 
  bool error= true;
68
 
  TableList *ren_table= NULL;
69
 
 
70
 
  /*
71
 
    Avoid problems with a rename on a table that we have locked or
72
 
    if the user is trying to to do this in a transcation context
73
 
  */
74
 
  if (getSession()->inTransaction())
75
 
  {
76
 
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
77
 
    return true;
78
 
  }
79
 
 
80
 
  if (getSession()->wait_if_global_read_lock(false, true))
81
 
    return true;
82
 
 
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
 
  }
115
 
 
116
 
  /* Lets hope this doesn't fail as the result will be messy */
117
 
  if (not error)
118
 
  {
119
 
    write_bin_log(getSession(), *getSession()->getQueryString());
120
 
    getSession()->my_ok();
121
 
  }
122
 
 
123
 
  getSession()->startWaitingGlobalReadLock();
124
 
 
125
 
  return error;
126
 
}
127
 
 
128
 
TableList *statement::RenameTable::reverseTableList(TableList *table_list)
129
 
{
130
 
  TableList *prev= NULL;
131
 
 
132
 
  while (table_list)
133
 
  {
134
 
    TableList *next= table_list->next_local;
135
 
    table_list->next_local= prev;
136
 
    prev= table_list;
137
 
    table_list= next;
138
 
  }
139
 
  return prev;
140
 
}
141
 
 
142
 
bool statement::RenameTable::rename(TableList *ren_table,
143
 
                                    const char *new_db,
144
 
                                    const char *new_table_name,
145
 
                                    bool skip_error)
146
 
{
147
 
  bool rc= true;
148
 
  const char *new_alias, *old_alias;
149
 
 
150
 
  {
151
 
    old_alias= ren_table->getTableName();
152
 
    new_alias= new_table_name;
153
 
  }
154
 
 
155
 
  plugin::StorageEngine *engine= NULL;
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)
161
 
  {
162
 
    my_error(ER_TABLE_UNKNOWN, old_identifier);
163
 
    return true;
164
 
  }
165
 
 
166
 
  engine= plugin::StorageEngine::findByName(*getSession(), table_proto->engine().name());
167
 
 
168
 
  identifier::Table new_identifier(new_db, new_alias, message::Table::STANDARD);
169
 
  if (plugin::StorageEngine::doesTableExist(*getSession(), new_identifier))
170
 
  {
171
 
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
172
 
    return 1; // This can't be skipped
173
 
  }
174
 
 
175
 
  rc= rename_table(*getSession(), engine, old_identifier, new_identifier);
176
 
  if (rc && ! skip_error)
177
 
    return true;
178
 
 
179
 
  return false;
180
 
}
181
 
 
182
 
TableList *statement::RenameTable::renameTablesInList(TableList *table_list,
183
 
                                                      bool skip_error)
184
 
{
185
 
  TableList *ren_table, *new_table;
186
 
 
187
 
  for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
188
 
  {
189
 
    new_table= ren_table->next_local;
190
 
    if (rename(ren_table, new_table->getSchemaName(), new_table->getTableName(), skip_error))
191
 
      return ren_table;
192
 
  }
193
 
  return 0;
194
 
}
195
 
 
196
 
} /* namespace drizzled */