~drizzle-trunk/drizzle/development

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