~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/rename.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-03-21 01:02:23 UTC
  • mto: (960.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 961.
  • Revision ID: osullivan.padraig@gmail.com-20090321010223-j8cph7eeyt1u3xol
Fixed function object to ensure it correctly returns a boolean type since
memcmp returns an integer. Added some more comments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
  Atomic rename of table;  RENAME TABLE t1 to t2, tmp to t1 [,...]
18
18
*/
19
19
#include <drizzled/server_includes.h>
20
 
#include <drizzled/drizzled_error_messages.h>
 
20
#include <drizzled/error.h>
 
21
#include <drizzled/table_list.h>
 
22
#include <drizzled/session.h>
 
23
#include <drizzled/lock.h>
21
24
 
22
 
static TableList *rename_tables(THD *thd, TableList *table_list,
23
 
                                 bool skip_error);
 
25
static TableList *rename_tables(Session *session, TableList *table_list,
 
26
                                bool skip_error);
24
27
 
25
28
static TableList *reverse_table_list(TableList *table_list);
26
29
 
29
32
  second entry is the new name.
30
33
*/
31
34
 
32
 
bool mysql_rename_tables(THD *thd, TableList *table_list, bool silent)
 
35
bool drizzle_rename_tables(Session *session, TableList *table_list, bool silent)
33
36
{
34
37
  bool error= 1;
35
38
  TableList *ren_table= 0;
38
41
    Avoid problems with a rename on a table that we have locked or
39
42
    if the user is trying to to do this in a transcation context
40
43
  */
41
 
  if (thd->locked_tables || thd->active_transaction())
 
44
  if (session->locked_tables || session->active_transaction())
42
45
  {
43
46
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
44
47
               ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
45
48
    return(1);
46
49
  }
47
50
 
48
 
  mysql_ha_rm_tables(thd, table_list, false);
49
 
 
50
 
  if (wait_if_global_read_lock(thd,0,1))
 
51
  if (wait_if_global_read_lock(session,0,1))
51
52
    return(1);
52
53
 
53
54
  pthread_mutex_lock(&LOCK_open);
54
 
  if (lock_table_names_exclusively(thd, table_list))
 
55
  if (lock_table_names_exclusively(session, table_list))
55
56
  {
56
57
    pthread_mutex_unlock(&LOCK_open);
57
58
    goto err;
58
59
  }
59
60
 
60
61
  error=0;
61
 
  if ((ren_table=rename_tables(thd,table_list,0)))
 
62
  if ((ren_table=rename_tables(session,table_list,0)))
62
63
  {
63
64
    /* Rename didn't succeed;  rename back the tables in reverse order */
64
65
    TableList *table;
72
73
         table= table->next_local->next_local) ;
73
74
    table= table->next_local->next_local;               // Skip error table
74
75
    /* Revert to old names */
75
 
    rename_tables(thd, table, 1);
 
76
    rename_tables(session, table, 1);
76
77
 
77
78
    /* Revert the table list (for prepared statements) */
78
79
    table_list= reverse_table_list(table_list);
88
89
  */
89
90
  pthread_mutex_unlock(&LOCK_open);
90
91
 
91
 
  /* Lets hope this doesn't fail as the result will be messy */ 
 
92
  /* Lets hope this doesn't fail as the result will be messy */
92
93
  if (!silent && !error)
93
94
  {
94
 
    write_bin_log(thd, true, thd->query, thd->query_length);
95
 
    my_ok(thd);
 
95
    write_bin_log(session, true, session->query, session->query_length);
 
96
    session->my_ok();
96
97
  }
97
98
 
98
99
  pthread_mutex_lock(&LOCK_open);
99
 
  unlock_table_names(thd, table_list, (TableList*) 0);
 
100
  unlock_table_names(session, table_list, (TableList*) 0);
100
101
  pthread_mutex_unlock(&LOCK_open);
101
102
 
102
103
err:
103
 
  start_waiting_global_read_lock(thd);
 
104
  start_waiting_global_read_lock(session);
104
105
  return(error);
105
106
}
106
107
 
135
136
 
136
137
  SYNPOSIS
137
138
    do_rename()
138
 
      thd               Thread handle
 
139
      session               Thread handle
139
140
      ren_table         A table/view to be renamed
140
141
      new_db            The database to which the table to be moved to
141
142
      new_table_name    The new table/view name
151
152
*/
152
153
 
153
154
bool
154
 
do_rename(THD *thd, TableList *ren_table, char *new_db, char *new_table_name,
 
155
do_rename(Session *session, TableList *ren_table, char *new_db, char *new_table_name,
155
156
          char *new_table_alias, bool skip_error)
156
157
{
157
158
  int rc= 1;
158
 
  char name[FN_REFLEN];
159
159
  const char *new_alias, *old_alias;
160
 
  /* TODO: What should this really be set to - it doesn't
161
 
     get set anywhere before it's used? */
162
 
  enum legacy_db_type table_type=DB_TYPE_UNKNOWN;
163
160
 
164
161
  if (lower_case_table_names == 2)
165
162
  {
171
168
    old_alias= ren_table->table_name;
172
169
    new_alias= new_table_name;
173
170
  }
174
 
  build_table_filename(name, sizeof(name),
175
 
                       new_db, new_alias, reg_ext, 0);
176
 
  if (!access(name,F_OK))
 
171
 
 
172
  handlerton *hton= NULL;
 
173
 
 
174
  if(ha_table_exists_in_engine(session, ren_table->db, old_alias, &hton)
 
175
     != HA_ERR_TABLE_EXIST)
 
176
  {
 
177
    my_error(ER_NO_SUCH_TABLE, MYF(0), ren_table->db, old_alias);
 
178
    return(1);
 
179
  }
 
180
 
 
181
  if (ha_table_exists_in_engine(session, new_db, new_alias)
 
182
      !=HA_ERR_NO_SUCH_TABLE)
177
183
  {
178
184
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
179
185
    return(1);                  // This can't be skipped
180
186
  }
181
 
  build_table_filename(name, sizeof(name),
182
 
                       ren_table->db, old_alias, reg_ext, 0);
183
187
 
184
 
  rc= mysql_rename_table(ha_resolve_by_legacy_type(thd, table_type),
185
 
                               ren_table->db, old_alias,
186
 
                               new_db, new_alias, 0);
 
188
  rc= mysql_rename_table(hton,
 
189
                         ren_table->db, old_alias,
 
190
                         new_db, new_alias, 0);
187
191
  if (rc && !skip_error)
188
192
    return(1);
189
193
 
200
204
 
201
205
  SYNPOSIS
202
206
    rename_tables()
203
 
      thd               Thread handle
 
207
      session               Thread handle
204
208
      table_list        List of tables to rename
205
209
      skip_error        Whether to skip errors
206
210
 
215
219
*/
216
220
 
217
221
static TableList *
218
 
rename_tables(THD *thd, TableList *table_list, bool skip_error)
 
222
rename_tables(Session *session, TableList *table_list, bool skip_error)
219
223
{
220
224
  TableList *ren_table, *new_table;
221
225
 
222
226
  for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
223
227
  {
224
228
    new_table= ren_table->next_local;
225
 
    if (do_rename(thd, ren_table, new_table->db, new_table->table_name,
 
229
    if (do_rename(session, ren_table, new_table->db, new_table->table_name,
226
230
                  new_table->alias, skip_error))
227
231
      return(ren_table);
228
232
  }