~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/rename_table.cc

  • Committer: Brian Aker
  • Date: 2009-09-26 04:00:11 UTC
  • mfrom: (1126.12.1 trunk-nodebug)
  • Revision ID: brian@gaz-20090926040011-2qzxdcbpm1ibpkhl
Merge Lee

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
 
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 <drizzled/server_includes.h>
 
22
#include <drizzled/show.h>
 
23
#include <drizzled/lock.h>
 
24
#include <drizzled/session.h>
 
25
#include <drizzled/statement/rename_table.h>
 
26
 
 
27
using namespace drizzled;
 
28
 
 
29
bool statement::RenameTable::execute()
 
30
{
 
31
  TableList *first_table= (TableList *) session->lex->select_lex.table_list.first;
 
32
  TableList *all_tables= session->lex->query_tables;
 
33
  assert(first_table == all_tables && first_table != 0);
 
34
  TableList *table;
 
35
  for (table= first_table; table; table= table->next_local->next_local)
 
36
  {
 
37
    TableList old_list, new_list;
 
38
    /*
 
39
       we do not need initialize old_list and new_list because we will
 
40
       come table[0] and table->next[0] there
 
41
     */
 
42
    old_list= table[0];
 
43
    new_list= table->next_local[0];
 
44
  }
 
45
 
 
46
  if (! session->endActiveTransaction() || renameTables(first_table))
 
47
  {
 
48
    return true;
 
49
  }
 
50
  return false;
 
51
}
 
52
 
 
53
bool statement::RenameTable::renameTables(TableList *table_list)
 
54
{
 
55
  bool error= true;
 
56
  TableList *ren_table= NULL;
 
57
 
 
58
  /*
 
59
    Avoid problems with a rename on a table that we have locked or
 
60
    if the user is trying to to do this in a transcation context
 
61
  */
 
62
  if (session->inTransaction())
 
63
  {
 
64
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
 
65
    return true;
 
66
  }
 
67
 
 
68
  if (wait_if_global_read_lock(session, 0, 1))
 
69
    return true;
 
70
 
 
71
  pthread_mutex_lock(&LOCK_open); /* Rename table lock for exclusive access */
 
72
  if (lock_table_names_exclusively(session, table_list))
 
73
  {
 
74
    pthread_mutex_unlock(&LOCK_open);
 
75
    goto err;
 
76
  }
 
77
 
 
78
  error= false;
 
79
  ren_table= renameTablesInList(table_list, 0);
 
80
  if (ren_table)
 
81
  {
 
82
    /* Rename didn't succeed;  rename back the tables in reverse order */
 
83
    TableList *table;
 
84
 
 
85
    /* Reverse the table list */
 
86
    table_list= reverseTableList(table_list);
 
87
 
 
88
    /* Find the last renamed table */
 
89
    for (table= table_list;
 
90
         table->next_local != ren_table;
 
91
         table= table->next_local->next_local) 
 
92
    { /* do nothing */ }
 
93
    table= table->next_local->next_local;               // Skip error table
 
94
    /* Revert to old names */
 
95
    renameTablesInList(table, 1);
 
96
    error= true;
 
97
  }
 
98
  /*
 
99
    An exclusive lock on table names is satisfactory to ensure
 
100
    no other thread accesses this table.
 
101
    We still should unlock LOCK_open as early as possible, to provide
 
102
    higher concurrency - query_cache_invalidate can take minutes to
 
103
    complete.
 
104
  */
 
105
  pthread_mutex_unlock(&LOCK_open);
 
106
 
 
107
  /* Lets hope this doesn't fail as the result will be messy */
 
108
  if (! error)
 
109
  {
 
110
    write_bin_log(session, true, session->query, session->query_length);
 
111
    session->my_ok();
 
112
  }
 
113
 
 
114
  pthread_mutex_lock(&LOCK_open); /* unlock all tables held */
 
115
  unlock_table_names(table_list, NULL);
 
116
  pthread_mutex_unlock(&LOCK_open);
 
117
 
 
118
err:
 
119
  start_waiting_global_read_lock(session);
 
120
 
 
121
  return error;
 
122
}
 
123
 
 
124
TableList *statement::RenameTable::reverseTableList(TableList *table_list)
 
125
{
 
126
  TableList *prev= NULL;
 
127
 
 
128
  while (table_list)
 
129
  {
 
130
    TableList *next= table_list->next_local;
 
131
    table_list->next_local= prev;
 
132
    prev= table_list;
 
133
    table_list= next;
 
134
  }
 
135
  return prev;
 
136
}
 
137
 
 
138
bool statement::RenameTable::rename(TableList *ren_table,
 
139
                                    const char *new_db,
 
140
                                    const char *new_table_name,
 
141
                                    bool skip_error)
 
142
{
 
143
  bool rc= true;
 
144
  const char *new_alias, *old_alias;
 
145
 
 
146
  {
 
147
    old_alias= ren_table->table_name;
 
148
    new_alias= new_table_name;
 
149
  }
 
150
 
 
151
  StorageEngine *engine= NULL;
 
152
  message::Table table_proto;
 
153
  char path[FN_REFLEN];
 
154
  size_t length;
 
155
 
 
156
  length= build_table_filename(path, sizeof(path),
 
157
                               ren_table->db, old_alias, false);
 
158
 
 
159
  if (StorageEngine::getTableProto(path, &table_proto)!= EEXIST)
 
160
  {
 
161
    my_error(ER_NO_SUCH_TABLE, MYF(0), ren_table->db, old_alias);
 
162
    return true;
 
163
  }
 
164
 
 
165
  engine= ha_resolve_by_name(session, table_proto.engine().name());
 
166
 
 
167
  length= build_table_filename(path, sizeof(path),
 
168
                               new_db, new_alias, false);
 
169
 
 
170
  if (StorageEngine::getTableProto(path, NULL)!=ENOENT)
 
171
  {
 
172
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
 
173
    return 1; // This can't be skipped
 
174
  }
 
175
 
 
176
  rc= mysql_rename_table(engine,
 
177
                         ren_table->db, old_alias,
 
178
                         new_db, new_alias, 0);
 
179
  if (rc && ! skip_error)
 
180
    return true;
 
181
 
 
182
  return false;
 
183
}
 
184
 
 
185
TableList *statement::RenameTable::renameTablesInList(TableList *table_list,
 
186
                                                      bool skip_error)
 
187
{
 
188
  TableList *ren_table, *new_table;
 
189
 
 
190
  for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
 
191
  {
 
192
    new_table= ren_table->next_local;
 
193
    if (rename(ren_table, new_table->db, new_table->table_name, skip_error))
 
194
      return ren_table;
 
195
  }
 
196
  return 0;
 
197
}