~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/rename_table.cc

  • Committer: Lee Bieber
  • Date: 2010-11-07 19:34:48 UTC
  • mfrom: (1910.1.2 build)
  • Revision ID: kalebral@gmail.com-20101107193448-64kdu912qej354sh
Merge Stewart - including adapting and expanding the "differences from mysql" page from the wiki.
Merge Stewart - fix bug 668143: drizzleslap with --commit runs second iteration data load in a transaction

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
25
25
#include <drizzled/statement/rename_table.h>
26
26
#include "drizzled/sql_table.h"
27
27
#include "drizzled/pthread_globals.h"
28
 
#include "drizzled/plugin/storage_engine.h"
29
28
 
30
29
namespace drizzled
31
30
{
32
31
 
33
32
bool statement::RenameTable::execute()
34
33
{
35
 
  TableList *first_table= (TableList *) getSession()->lex->select_lex.table_list.first;
36
 
  TableList *all_tables= getSession()->lex->query_tables;
 
34
  TableList *first_table= (TableList *) session->lex->select_lex.table_list.first;
 
35
  TableList *all_tables= session->lex->query_tables;
37
36
  assert(first_table == all_tables && first_table != 0);
38
37
  TableList *table;
39
 
 
40
 
  if (getSession()->inTransaction())
41
 
  {
42
 
    my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
43
 
    return true;
44
 
  }
45
 
 
46
38
  for (table= first_table; table; table= table->next_local->next_local)
47
39
  {
48
40
    TableList old_list, new_list;
54
46
    new_list= table->next_local[0];
55
47
  }
56
48
 
57
 
  if (renameTables(first_table))
 
49
  if (! session->endActiveTransaction() || renameTables(first_table))
58
50
  {
59
51
    return true;
60
52
  }
61
 
 
62
53
  return false;
63
54
}
64
55
 
71
62
    Avoid problems with a rename on a table that we have locked or
72
63
    if the user is trying to to do this in a transcation context
73
64
  */
74
 
  if (getSession()->inTransaction())
 
65
  if (session->inTransaction())
75
66
  {
76
67
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
77
68
    return true;
78
69
  }
79
70
 
80
 
  if (getSession()->wait_if_global_read_lock(false, true))
 
71
  if (wait_if_global_read_lock(session, 0, 1))
81
72
    return true;
82
73
 
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
 
  }
 
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();
115
109
 
116
110
  /* Lets hope this doesn't fail as the result will be messy */
117
 
  if (not error)
 
111
  if (! error)
118
112
  {
119
 
    write_bin_log(getSession(), *getSession()->getQueryString());
120
 
    getSession()->my_ok();
 
113
    write_bin_log(session, session->query.c_str());
 
114
    session->my_ok();
121
115
  }
122
116
 
123
 
  getSession()->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);
124
123
 
125
124
  return error;
126
125
}
153
152
  }
154
153
 
155
154
  plugin::StorageEngine *engine= NULL;
156
 
  message::table::shared_ptr table_proto;
157
 
 
158
 
  identifier::Table old_identifier(ren_table->getSchemaName(), old_alias, message::Table::STANDARD);
159
 
 
160
 
  if (plugin::StorageEngine::getTableDefinition(*getSession(), old_identifier, table_proto) != EEXIST)
 
155
  message::Table table_proto;
 
156
 
 
157
  TableIdentifier old_identifier(ren_table->getSchemaName(), old_alias, message::Table::STANDARD);
 
158
 
 
159
  if (plugin::StorageEngine::getTableDefinition(*session, old_identifier, table_proto) != EEXIST)
161
160
  {
162
 
    my_error(ER_TABLE_UNKNOWN, old_identifier);
 
161
    my_error(ER_NO_SUCH_TABLE, MYF(0), ren_table->getSchemaName(), old_alias);
163
162
    return true;
164
163
  }
165
164
 
166
 
  engine= plugin::StorageEngine::findByName(*getSession(), table_proto->engine().name());
 
165
  engine= plugin::StorageEngine::findByName(*session, table_proto.engine().name());
167
166
 
168
 
  identifier::Table new_identifier(new_db, new_alias, message::Table::STANDARD);
169
 
  if (plugin::StorageEngine::doesTableExist(*getSession(), new_identifier))
 
167
  TableIdentifier new_identifier(new_db, new_alias, message::Table::STANDARD);
 
168
  if (plugin::StorageEngine::doesTableExist(*session, new_identifier))
170
169
  {
171
170
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
172
171
    return 1; // This can't be skipped
173
172
  }
174
173
 
175
 
  rc= rename_table(*getSession(), engine, old_identifier, new_identifier);
 
174
  rc= mysql_rename_table(*session, engine, old_identifier, new_identifier);
176
175
  if (rc && ! skip_error)
177
176
    return true;
178
177