~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/rename.cc

  • Committer: Monty Taylor
  • Date: 2009-04-14 19:16:51 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 994.
  • Revision ID: mordred@inaugust.com-20090414191651-ltbww6hpqks8k7qk
Clarified instructions in README.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000-2006 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/*
 
17
  Atomic rename of table;  RENAME TABLE t1 to t2, tmp to t1 [,...]
 
18
*/
 
19
#include <drizzled/server_includes.h>
 
20
#include <drizzled/error.h>
 
21
#include <drizzled/table_list.h>
 
22
#include <drizzled/session.h>
 
23
#include <drizzled/lock.h>
 
24
 
 
25
static TableList *rename_tables(Session *session, TableList *table_list,
 
26
                                bool skip_error);
 
27
 
 
28
static TableList *reverse_table_list(TableList *table_list);
 
29
 
 
30
/*
 
31
  Every second entry in the table_list is the original name and every
 
32
  second entry is the new name.
 
33
*/
 
34
bool drizzle_rename_tables(Session *session, TableList *table_list, bool silent)
 
35
{
 
36
  bool error= 1;
 
37
  TableList *ren_table= 0;
 
38
 
 
39
  /*
 
40
    Avoid problems with a rename on a table that we have locked or
 
41
    if the user is trying to to do this in a transcation context
 
42
  */
 
43
  if (session->locked_tables || session->inTransaction())
 
44
  {
 
45
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
 
46
    return(1);
 
47
  }
 
48
 
 
49
  if (wait_if_global_read_lock(session,0,1))
 
50
    return(1);
 
51
 
 
52
  pthread_mutex_lock(&LOCK_open);
 
53
  if (lock_table_names_exclusively(session, table_list))
 
54
  {
 
55
    pthread_mutex_unlock(&LOCK_open);
 
56
    goto err;
 
57
  }
 
58
 
 
59
  error=0;
 
60
  if ((ren_table=rename_tables(session,table_list,0)))
 
61
  {
 
62
    /* Rename didn't succeed;  rename back the tables in reverse order */
 
63
    TableList *table;
 
64
 
 
65
    /* Reverse the table list */
 
66
    table_list= reverse_table_list(table_list);
 
67
 
 
68
    /* Find the last renamed table */
 
69
    for (table= table_list;
 
70
         table->next_local != ren_table ;
 
71
         table= table->next_local->next_local) ;
 
72
    table= table->next_local->next_local;               // Skip error table
 
73
    /* Revert to old names */
 
74
    rename_tables(session, table, 1);
 
75
 
 
76
    /* Revert the table list (for prepared statements) */
 
77
    table_list= reverse_table_list(table_list);
 
78
 
 
79
    error= 1;
 
80
  }
 
81
  /*
 
82
    An exclusive lock on table names is satisfactory to ensure
 
83
    no other thread accesses this table.
 
84
    We still should unlock LOCK_open as early as possible, to provide
 
85
    higher concurrency - query_cache_invalidate can take minutes to
 
86
    complete.
 
87
  */
 
88
  pthread_mutex_unlock(&LOCK_open);
 
89
 
 
90
  /* Lets hope this doesn't fail as the result will be messy */
 
91
  if (!silent && !error)
 
92
  {
 
93
    write_bin_log(session, true, session->query, session->query_length);
 
94
    session->my_ok();
 
95
  }
 
96
 
 
97
  pthread_mutex_lock(&LOCK_open);
 
98
  unlock_table_names(session, table_list, (TableList*) 0);
 
99
  pthread_mutex_unlock(&LOCK_open);
 
100
 
 
101
err:
 
102
  start_waiting_global_read_lock(session);
 
103
  return(error);
 
104
}
 
105
 
 
106
 
 
107
/*
 
108
  reverse table list
 
109
 
 
110
  SYNOPSIS
 
111
    reverse_table_list()
 
112
    table_list pointer to table _list
 
113
 
 
114
  RETURN
 
115
    pointer to new (reversed) list
 
116
*/
 
117
static TableList *reverse_table_list(TableList *table_list)
 
118
{
 
119
  TableList *prev= 0;
 
120
 
 
121
  while (table_list)
 
122
  {
 
123
    TableList *next= table_list->next_local;
 
124
    table_list->next_local= prev;
 
125
    prev= table_list;
 
126
    table_list= next;
 
127
  }
 
128
  return (prev);
 
129
}
 
130
 
 
131
 
 
132
/*
 
133
  Rename a single table or a view
 
134
 
 
135
  SYNPOSIS
 
136
    do_rename()
 
137
      session               Thread handle
 
138
      ren_table         A table/view to be renamed
 
139
      new_db            The database to which the table to be moved to
 
140
      new_table_name    The new table/view name
 
141
      new_table_alias   The new table/view alias
 
142
      skip_error        Whether to skip error
 
143
 
 
144
  DESCRIPTION
 
145
    Rename a single table or a view.
 
146
 
 
147
  RETURN
 
148
    false     Ok
 
149
    true      rename failed
 
150
*/
 
151
 
 
152
bool
 
153
do_rename(Session *session, TableList *ren_table, char *new_db, char *new_table_name,
 
154
          char *new_table_alias, bool skip_error)
 
155
{
 
156
  int rc= 1;
 
157
  const char *new_alias, *old_alias;
 
158
 
 
159
  if (lower_case_table_names == 2)
 
160
  {
 
161
    old_alias= ren_table->alias;
 
162
    new_alias= new_table_alias;
 
163
  }
 
164
  else
 
165
  {
 
166
    old_alias= ren_table->table_name;
 
167
    new_alias= new_table_name;
 
168
  }
 
169
 
 
170
  StorageEngine *engine= NULL;
 
171
 
 
172
  if(ha_table_exists_in_engine(session, ren_table->db, old_alias, &engine)
 
173
     != HA_ERR_TABLE_EXIST)
 
174
  {
 
175
    my_error(ER_NO_SUCH_TABLE, MYF(0), ren_table->db, old_alias);
 
176
    return(1);
 
177
  }
 
178
 
 
179
  if (ha_table_exists_in_engine(session, new_db, new_alias)
 
180
      !=HA_ERR_NO_SUCH_TABLE)
 
181
  {
 
182
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
 
183
    return(1);                  // This can't be skipped
 
184
  }
 
185
 
 
186
  rc= mysql_rename_table(engine,
 
187
                         ren_table->db, old_alias,
 
188
                         new_db, new_alias, 0);
 
189
  if (rc && !skip_error)
 
190
    return(1);
 
191
 
 
192
  return(0);
 
193
 
 
194
}
 
195
/*
 
196
  Rename all tables in list; Return pointer to wrong entry if something goes
 
197
  wrong.  Note that the table_list may be empty!
 
198
*/
 
199
 
 
200
/*
 
201
  Rename tables/views in the list
 
202
 
 
203
  SYNPOSIS
 
204
    rename_tables()
 
205
      session               Thread handle
 
206
      table_list        List of tables to rename
 
207
      skip_error        Whether to skip errors
 
208
 
 
209
  DESCRIPTION
 
210
    Take a table/view name from and odd list element and rename it to a
 
211
    the name taken from list element+1. Note that the table_list may be
 
212
    empty.
 
213
 
 
214
  RETURN
 
215
    false     Ok
 
216
    true      rename failed
 
217
*/
 
218
 
 
219
static TableList *
 
220
rename_tables(Session *session, TableList *table_list, bool skip_error)
 
221
{
 
222
  TableList *ren_table, *new_table;
 
223
 
 
224
  for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
 
225
  {
 
226
    new_table= ren_table->next_local;
 
227
    if (do_rename(session, ren_table, new_table->db, new_table->table_name,
 
228
                  new_table->alias, skip_error))
 
229
      return(ren_table);
 
230
  }
 
231
  return(0);
 
232
}