~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/rename_table.cc

  • Committer: Monty Taylor
  • Date: 2010-10-08 17:32:00 UTC
  • mto: This revision was merged to the branch mainline in revision 1833.
  • Revision ID: mordred@inaugust.com-20101008173200-iq22jo2nic48noa3
Updated pandora-build files to version 0.157

Show diffs side-by-side

added added

removed removed

Lines of Context:
68
68
    return true;
69
69
  }
70
70
 
71
 
  if (session->wait_if_global_read_lock(false, true))
 
71
  if (wait_if_global_read_lock(session, 0, 1))
72
72
    return true;
73
73
 
74
 
  {
75
 
    boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex()); /* Rename table lock for exclusive access */
76
 
 
77
 
    if (not session->lock_table_names_exclusively(table_list))
78
 
    {
79
 
      error= false;
80
 
      ren_table= renameTablesInList(table_list, false);
81
 
 
82
 
      if (ren_table)
83
 
      {
84
 
        /* Rename didn't succeed;  rename back the tables in reverse order */
85
 
        TableList *table;
86
 
 
87
 
        /* Reverse the table list */
88
 
        table_list= reverseTableList(table_list);
89
 
 
90
 
        /* Find the last renamed table */
91
 
        for (table= table_list;
92
 
             table->next_local != ren_table;
93
 
             table= table->next_local->next_local) 
94
 
        { /* do nothing */ }
95
 
 
96
 
        table= table->next_local->next_local;           // Skip error table
97
 
 
98
 
        /* Revert to old names */
99
 
        renameTablesInList(table, true);
100
 
        error= true;
101
 
      }
102
 
 
103
 
      table_list->unlock_table_names();
104
 
    }
105
 
  }
 
74
  LOCK_open.lock(); /* Rename table lock for exclusive access */
 
75
  if (lock_table_names_exclusively(session, table_list))
 
76
  {
 
77
    LOCK_open.unlock();
 
78
    goto err;
 
79
  }
 
80
 
 
81
  error= false;
 
82
  ren_table= renameTablesInList(table_list, 0);
 
83
  if (ren_table)
 
84
  {
 
85
    /* Rename didn't succeed;  rename back the tables in reverse order */
 
86
    TableList *table;
 
87
 
 
88
    /* Reverse the table list */
 
89
    table_list= reverseTableList(table_list);
 
90
 
 
91
    /* Find the last renamed table */
 
92
    for (table= table_list;
 
93
         table->next_local != ren_table;
 
94
         table= table->next_local->next_local) 
 
95
    { /* do nothing */ }
 
96
    table= table->next_local->next_local;               // Skip error table
 
97
    /* Revert to old names */
 
98
    renameTablesInList(table, 1);
 
99
    error= true;
 
100
  }
 
101
  /*
 
102
    An exclusive lock on table names is satisfactory to ensure
 
103
    no other thread accesses this table.
 
104
    We still should unlock LOCK_open as early as possible, to provide
 
105
    higher concurrency - query_cache_invalidate can take minutes to
 
106
    complete.
 
107
  */
 
108
  LOCK_open.unlock();
106
109
 
107
110
  /* Lets hope this doesn't fail as the result will be messy */
108
 
  if (not error)
 
111
  if (! error)
109
112
  {
110
 
    write_bin_log(session, *session->getQueryString());
 
113
    write_bin_log(session, session->query.c_str());
111
114
    session->my_ok();
112
115
  }
113
116
 
114
 
  session->startWaitingGlobalReadLock();
 
117
  LOCK_open.lock(); /* unlock all tables held */
 
118
  unlock_table_names(table_list, NULL);
 
119
  LOCK_open.unlock();
 
120
 
 
121
err:
 
122
  start_waiting_global_read_lock(session);
115
123
 
116
124
  return error;
117
125
}
139
147
  const char *new_alias, *old_alias;
140
148
 
141
149
  {
142
 
    old_alias= ren_table->getTableName();
 
150
    old_alias= ren_table->table_name;
143
151
    new_alias= new_table_name;
144
152
  }
145
153
 
146
154
  plugin::StorageEngine *engine= NULL;
147
 
  message::table::shared_ptr table_proto;
 
155
  message::Table table_proto;
148
156
 
149
 
  TableIdentifier old_identifier(ren_table->getSchemaName(), old_alias, message::Table::STANDARD);
 
157
  TableIdentifier old_identifier(ren_table->db, old_alias, message::Table::STANDARD);
150
158
 
151
159
  if (plugin::StorageEngine::getTableDefinition(*session, old_identifier, table_proto) != EEXIST)
152
160
  {
153
 
    my_error(ER_NO_SUCH_TABLE, MYF(0), ren_table->getSchemaName(), old_alias);
 
161
    my_error(ER_NO_SUCH_TABLE, MYF(0), ren_table->db, old_alias);
154
162
    return true;
155
163
  }
156
164
 
157
 
  engine= plugin::StorageEngine::findByName(*session, table_proto->engine().name());
 
165
  engine= plugin::StorageEngine::findByName(*session, table_proto.engine().name());
158
166
 
159
167
  TableIdentifier new_identifier(new_db, new_alias, message::Table::STANDARD);
160
168
  if (plugin::StorageEngine::doesTableExist(*session, new_identifier))
178
186
  for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
179
187
  {
180
188
    new_table= ren_table->next_local;
181
 
    if (rename(ren_table, new_table->getSchemaName(), new_table->getTableName(), skip_error))
 
189
    if (rename(ren_table, new_table->db, new_table->table_name, skip_error))
182
190
      return ren_table;
183
191
  }
184
192
  return 0;