~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/rename_table.cc

  • Committer: Monty Taylor
  • Date: 2009-10-06 19:40:45 UTC
  • mto: This revision was merged to the branch mainline in revision 1184.
  • Revision ID: mordred@inaugust.com-20091006194045-ojptaq2sx6ck6q63
No more server_includes.h in headers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2009 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include <config.h>
 
21
#include <drizzled/server_includes.h>
22
22
#include <drizzled/show.h>
23
23
#include <drizzled/lock.h>
24
24
#include <drizzled/session.h>
25
25
#include <drizzled/statement/rename_table.h>
26
 
#include <drizzled/sql_table.h>
27
 
#include <drizzled/pthread_globals.h>
28
 
#include <drizzled/plugin/storage_engine.h>
29
26
 
30
27
namespace drizzled
31
28
{
32
29
 
33
30
bool statement::RenameTable::execute()
34
31
{
35
 
  TableList *first_table= (TableList *) getSession()->getLex()->select_lex.table_list.first;
36
 
  TableList *all_tables= getSession()->getLex()->query_tables;
 
32
  TableList *first_table= (TableList *) session->lex->select_lex.table_list.first;
 
33
  TableList *all_tables= session->lex->query_tables;
37
34
  assert(first_table == all_tables && first_table != 0);
38
35
  TableList *table;
39
 
 
40
 
  if (getSession()->inTransaction())
41
 
  {
42
 
    my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
43
 
    return true;
44
 
  }
45
 
 
46
36
  for (table= first_table; table; table= table->next_local->next_local)
47
37
  {
48
38
    TableList old_list, new_list;
54
44
    new_list= table->next_local[0];
55
45
  }
56
46
 
57
 
  if (renameTables(first_table))
 
47
  if (! session->endActiveTransaction() || renameTables(first_table))
58
48
  {
59
49
    return true;
60
50
  }
61
 
 
62
51
  return false;
63
52
}
64
53
 
71
60
    Avoid problems with a rename on a table that we have locked or
72
61
    if the user is trying to to do this in a transcation context
73
62
  */
74
 
  if (getSession()->inTransaction())
 
63
  if (session->inTransaction())
75
64
  {
76
65
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
77
66
    return true;
78
67
  }
79
68
 
80
 
  if (getSession()->wait_if_global_read_lock(false, true))
 
69
  if (wait_if_global_read_lock(session, 0, 1))
81
70
    return true;
82
71
 
83
 
  {
84
 
    boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex()); /* Rename table lock for exclusive access */
85
 
 
86
 
    if (not getSession()->lock_table_names_exclusively(table_list))
87
 
    {
88
 
      error= false;
89
 
      ren_table= renameTablesInList(table_list, false);
90
 
 
91
 
      if (ren_table)
92
 
      {
93
 
        /* Rename didn't succeed;  rename back the tables in reverse order */
94
 
        TableList *table;
95
 
 
96
 
        /* Reverse the table list */
97
 
        table_list= reverseTableList(table_list);
98
 
 
99
 
        /* Find the last renamed table */
100
 
        for (table= table_list;
101
 
             table->next_local != ren_table;
102
 
             table= table->next_local->next_local) 
103
 
        { /* do nothing */ }
104
 
 
105
 
        table= table->next_local->next_local;           // Skip error table
106
 
 
107
 
        /* Revert to old names */
108
 
        renameTablesInList(table, true);
109
 
        error= true;
110
 
      }
111
 
 
112
 
      table_list->unlock_table_names();
113
 
    }
114
 
  }
 
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;
 
80
  ren_table= renameTablesInList(table_list, 0);
 
81
  if (ren_table)
 
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;
 
91
         table->next_local != ren_table;
 
92
         table= table->next_local->next_local) 
 
93
    { /* do nothing */ }
 
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);
115
107
 
116
108
  /* Lets hope this doesn't fail as the result will be messy */
117
 
  if (not error)
 
109
  if (! error)
118
110
  {
119
 
    write_bin_log(getSession(), *getSession()->getQueryString());
120
 
    getSession()->my_ok();
 
111
    write_bin_log(session, true, session->query, session->query_length);
 
112
    session->my_ok();
121
113
  }
122
114
 
123
 
  getSession()->startWaitingGlobalReadLock();
 
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);
124
121
 
125
122
  return error;
126
123
}
148
145
  const char *new_alias, *old_alias;
149
146
 
150
147
  {
151
 
    old_alias= ren_table->getTableName();
 
148
    old_alias= ren_table->table_name;
152
149
    new_alias= new_table_name;
153
150
  }
154
151
 
155
152
  plugin::StorageEngine *engine= NULL;
156
 
  message::table::shared_ptr table_message;
157
 
 
158
 
  identifier::Table old_identifier(ren_table->getSchemaName(), old_alias, message::Table::STANDARD);
159
 
 
160
 
  if (not (table_message= plugin::StorageEngine::getTableMessage(*getSession(), old_identifier)))
 
153
  message::Table table_proto;
 
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
 
 
160
  if (plugin::StorageEngine::getTableProto(path, &table_proto) != EEXIST)
161
161
  {
162
 
    my_error(ER_TABLE_UNKNOWN, old_identifier);
 
162
    my_error(ER_NO_SUCH_TABLE, MYF(0), ren_table->db, old_alias);
163
163
    return true;
164
164
  }
165
165
 
166
 
  engine= plugin::StorageEngine::findByName(*getSession(), table_message->engine().name());
167
 
 
168
 
  identifier::Table new_identifier(new_db, new_alias, message::Table::STANDARD);
169
 
  if (plugin::StorageEngine::doesTableExist(*getSession(), new_identifier))
 
166
  engine= plugin::StorageEngine::findByName(session, table_proto.engine().name());
 
167
 
 
168
  length= build_table_filename(path, sizeof(path),
 
169
                               new_db, new_alias, false);
 
170
 
 
171
  if (plugin::StorageEngine::getTableProto(path, NULL) != ENOENT)
170
172
  {
171
 
    my_error(ER_TABLE_EXISTS_ERROR, new_identifier);
 
173
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
172
174
    return 1; // This can't be skipped
173
175
  }
174
176
 
175
 
  rc= rename_table(*getSession(), engine, old_identifier, new_identifier);
 
177
  rc= mysql_rename_table(engine,
 
178
                         ren_table->db, old_alias,
 
179
                         new_db, new_alias, 0);
176
180
  if (rc && ! skip_error)
177
181
    return true;
178
182
 
187
191
  for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
188
192
  {
189
193
    new_table= ren_table->next_local;
190
 
    if (rename(ren_table, new_table->getSchemaName(), new_table->getTableName(), skip_error))
 
194
    if (rename(ren_table, new_table->db, new_table->table_name, skip_error))
191
195
      return ren_table;
192
196
  }
193
197
  return 0;
194
 
}
 
198
195
199
 
196
200
} /* namespace drizzled */