~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/rename_table.cc

edit

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include <config.h>
 
21
#include "config.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>
 
26
#include "drizzled/sql_table.h"
 
27
#include "drizzled/pthread_globals.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()->getLex()->select_lex.table_list.first;
36
 
  TableList *all_tables= getSession()->getLex()->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 (session->wait_if_global_read_lock(false, true))
81
72
    return true;
82
73
 
83
74
  {
84
75
    boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex()); /* Rename table lock for exclusive access */
85
76
 
86
 
    if (not getSession()->lock_table_names_exclusively(table_list))
 
77
    if (not session->lock_table_names_exclusively(table_list))
87
78
    {
88
79
      error= false;
89
80
      ren_table= renameTablesInList(table_list, false);
116
107
  /* Lets hope this doesn't fail as the result will be messy */
117
108
  if (not error)
118
109
  {
119
 
    write_bin_log(getSession(), *getSession()->getQueryString());
120
 
    getSession()->my_ok();
 
110
    write_bin_log(session, *session->getQueryString());
 
111
    session->my_ok();
121
112
  }
122
113
 
123
 
  getSession()->startWaitingGlobalReadLock();
 
114
  session->startWaitingGlobalReadLock();
124
115
 
125
116
  return error;
126
117
}
153
144
  }
154
145
 
155
146
  plugin::StorageEngine *engine= NULL;
156
 
  message::table::shared_ptr table_message;
 
147
  message::table::shared_ptr table_proto;
157
148
 
158
149
  identifier::Table old_identifier(ren_table->getSchemaName(), old_alias, message::Table::STANDARD);
159
150
 
160
 
  if (not (table_message= plugin::StorageEngine::getTableMessage(*getSession(), old_identifier)))
 
151
  if (plugin::StorageEngine::getTableDefinition(*session, old_identifier, table_proto) != EEXIST)
161
152
  {
162
 
    my_error(ER_TABLE_UNKNOWN, old_identifier);
 
153
    my_error(ER_NO_SUCH_TABLE, MYF(0), ren_table->getSchemaName(), old_alias);
163
154
    return true;
164
155
  }
165
156
 
166
 
  engine= plugin::StorageEngine::findByName(*getSession(), table_message->engine().name());
 
157
  engine= plugin::StorageEngine::findByName(*session, table_proto->engine().name());
167
158
 
168
159
  identifier::Table new_identifier(new_db, new_alias, message::Table::STANDARD);
169
 
  if (plugin::StorageEngine::doesTableExist(*getSession(), new_identifier))
 
160
  if (plugin::StorageEngine::doesTableExist(*session, new_identifier))
170
161
  {
171
 
    my_error(ER_TABLE_EXISTS_ERROR, new_identifier);
 
162
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
172
163
    return 1; // This can't be skipped
173
164
  }
174
165
 
175
 
  rc= rename_table(*getSession(), engine, old_identifier, new_identifier);
 
166
  rc= rename_table(*session, engine, old_identifier, new_identifier);
176
167
  if (rc && ! skip_error)
177
168
    return true;
178
169