~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_rename.cc

  • Committer: Monty Taylor
  • Date: 2008-08-05 19:01:20 UTC
  • mto: (266.1.1 codestyle)
  • mto: This revision was merged to the branch mainline in revision 266.
  • Revision ID: monty@inaugust.com-20080805190120-tsuziqz2mfqcw7pe
Removed libmysyslt.la, made mysys a noinst_ and made everything use it. It's
not a standalone lib, there's no reason to pretend otherwise.

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
 
 
20
#include "mysql_priv.h"
 
21
#include <drizzled/drizzled_error_messages.h>
 
22
 
 
23
static TABLE_LIST *rename_tables(THD *thd, TABLE_LIST *table_list,
 
24
                                 bool skip_error);
 
25
 
 
26
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list);
 
27
 
 
28
/*
 
29
  Every second entry in the table_list is the original name and every
 
30
  second entry is the new name.
 
31
*/
 
32
 
 
33
bool mysql_rename_tables(THD *thd, TABLE_LIST *table_list, bool silent)
 
34
{
 
35
  bool error= 1;
 
36
  TABLE_LIST *ren_table= 0;
 
37
 
 
38
  /*
 
39
    Avoid problems with a rename on a table that we have locked or
 
40
    if the user is trying to to do this in a transcation context
 
41
  */
 
42
  if (thd->locked_tables || thd->active_transaction())
 
43
  {
 
44
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
 
45
               ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
 
46
    return(1);
 
47
  }
 
48
 
 
49
  mysql_ha_rm_tables(thd, table_list, false);
 
50
 
 
51
  if (wait_if_global_read_lock(thd,0,1))
 
52
    return(1);
 
53
 
 
54
  pthread_mutex_lock(&LOCK_open);
 
55
  if (lock_table_names_exclusively(thd, table_list))
 
56
  {
 
57
    pthread_mutex_unlock(&LOCK_open);
 
58
    goto err;
 
59
  }
 
60
 
 
61
  error=0;
 
62
  if ((ren_table=rename_tables(thd,table_list,0)))
 
63
  {
 
64
    /* Rename didn't succeed;  rename back the tables in reverse order */
 
65
    TABLE_LIST *table;
 
66
 
 
67
    /* Reverse the table list */
 
68
    table_list= reverse_table_list(table_list);
 
69
 
 
70
    /* Find the last renamed table */
 
71
    for (table= table_list;
 
72
         table->next_local != ren_table ;
 
73
         table= table->next_local->next_local) ;
 
74
    table= table->next_local->next_local;               // Skip error table
 
75
    /* Revert to old names */
 
76
    rename_tables(thd, table, 1);
 
77
 
 
78
    /* Revert the table list (for prepared statements) */
 
79
    table_list= reverse_table_list(table_list);
 
80
 
 
81
    error= 1;
 
82
  }
 
83
  /*
 
84
    An exclusive lock on table names is satisfactory to ensure
 
85
    no other thread accesses this table.
 
86
    We still should unlock LOCK_open as early as possible, to provide
 
87
    higher concurrency - query_cache_invalidate can take minutes to
 
88
    complete.
 
89
  */
 
90
  pthread_mutex_unlock(&LOCK_open);
 
91
 
 
92
  /* Lets hope this doesn't fail as the result will be messy */ 
 
93
  if (!silent && !error)
 
94
  {
 
95
    write_bin_log(thd, true, thd->query, thd->query_length);
 
96
    my_ok(thd);
 
97
  }
 
98
 
 
99
  pthread_mutex_lock(&LOCK_open);
 
100
  unlock_table_names(thd, table_list, (TABLE_LIST*) 0);
 
101
  pthread_mutex_unlock(&LOCK_open);
 
102
 
 
103
err:
 
104
  start_waiting_global_read_lock(thd);
 
105
  return(error);
 
106
}
 
107
 
 
108
 
 
109
/*
 
110
  reverse table list
 
111
 
 
112
  SYNOPSIS
 
113
    reverse_table_list()
 
114
    table_list pointer to table _list
 
115
 
 
116
  RETURN
 
117
    pointer to new (reversed) list
 
118
*/
 
119
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list)
 
120
{
 
121
  TABLE_LIST *prev= 0;
 
122
 
 
123
  while (table_list)
 
124
  {
 
125
    TABLE_LIST *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
 
 
134
/*
 
135
  Rename a single table or a view
 
136
 
 
137
  SYNPOSIS
 
138
    do_rename()
 
139
      thd               Thread handle
 
140
      ren_table         A table/view to be renamed
 
141
      new_db            The database to which the table to be moved to
 
142
      new_table_name    The new table/view name
 
143
      new_table_alias   The new table/view alias
 
144
      skip_error        Whether to skip error
 
145
 
 
146
  DESCRIPTION
 
147
    Rename a single table or a view.
 
148
 
 
149
  RETURN
 
150
    false     Ok
 
151
    true      rename failed
 
152
*/
 
153
 
 
154
bool
 
155
do_rename(THD *thd, TABLE_LIST *ren_table, char *new_db, char *new_table_name,
 
156
          char *new_table_alias, bool skip_error)
 
157
{
 
158
  int rc= 1;
 
159
  char name[FN_REFLEN];
 
160
  const char *new_alias, *old_alias;
 
161
  /* TODO: What should this really be set to - it doesn't
 
162
     get set anywhere before it's used? */
 
163
  enum legacy_db_type table_type=DB_TYPE_UNKNOWN;
 
164
 
 
165
  if (lower_case_table_names == 2)
 
166
  {
 
167
    old_alias= ren_table->alias;
 
168
    new_alias= new_table_alias;
 
169
  }
 
170
  else
 
171
  {
 
172
    old_alias= ren_table->table_name;
 
173
    new_alias= new_table_name;
 
174
  }
 
175
  build_table_filename(name, sizeof(name),
 
176
                       new_db, new_alias, reg_ext, 0);
 
177
  if (!access(name,F_OK))
 
178
  {
 
179
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
 
180
    return(1);                  // This can't be skipped
 
181
  }
 
182
  build_table_filename(name, sizeof(name),
 
183
                       ren_table->db, old_alias, reg_ext, 0);
 
184
 
 
185
  rc= mysql_rename_table(ha_resolve_by_legacy_type(thd, table_type),
 
186
                               ren_table->db, old_alias,
 
187
                               new_db, new_alias, 0);
 
188
  if (rc && !skip_error)
 
189
    return(1);
 
190
 
 
191
  return(0);
 
192
 
 
193
}
 
194
/*
 
195
  Rename all tables in list; Return pointer to wrong entry if something goes
 
196
  wrong.  Note that the table_list may be empty!
 
197
*/
 
198
 
 
199
/*
 
200
  Rename tables/views in the list
 
201
 
 
202
  SYNPOSIS
 
203
    rename_tables()
 
204
      thd               Thread handle
 
205
      table_list        List of tables to rename
 
206
      skip_error        Whether to skip errors
 
207
 
 
208
  DESCRIPTION
 
209
    Take a table/view name from and odd list element and rename it to a
 
210
    the name taken from list element+1. Note that the table_list may be
 
211
    empty.
 
212
 
 
213
  RETURN
 
214
    false     Ok
 
215
    true      rename failed
 
216
*/
 
217
 
 
218
static TABLE_LIST *
 
219
rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error)
 
220
{
 
221
  TABLE_LIST *ren_table, *new_table;
 
222
 
 
223
  for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
 
224
  {
 
225
    new_table= ren_table->next_local;
 
226
    if (do_rename(thd, ren_table, new_table->db, new_table->table_name,
 
227
                  new_table->alias, skip_error))
 
228
      return(ren_table);
 
229
  }
 
230
  return(0);
 
231
}