~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/rename_table.cc

Refactor

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