~drizzle-trunk/drizzle/development

1100.3.63 by Padraig O'Sullivan
Extracted the ALTER TABLE command into its own class and implementation
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
1100.3.63 by Padraig O'Sullivan
Extracted the ALTER TABLE command into its own class and implementation
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
22
23
#include <fcntl.h>
24
1241.9.17 by Monty Taylor
Removed more bits from server_includes.
25
#include <sstream>
26
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
27
#include <drizzled/show.h>
28
#include <drizzled/lock.h>
29
#include <drizzled/session.h>
30
#include <drizzled/statement/alter_table.h>
2281.5.1 by Muhammad Umair
Merged charset declarations of global_charset_info.h and charset_info.h into charset.h header file.
31
#include <drizzled/charset.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
32
#include <drizzled/gettext.h>
33
#include <drizzled/data_home.h>
34
#include <drizzled/sql_table.h>
35
#include <drizzled/table_proto.h>
36
#include <drizzled/optimizer/range.h>
37
#include <drizzled/time_functions.h>
38
#include <drizzled/records.h>
39
#include <drizzled/pthread_globals.h>
40
#include <drizzled/internal/my_sys.h>
41
#include <drizzled/internal/iocache.h>
42
#include <drizzled/plugin/storage_engine.h>
2154.2.11 by Brian Aker
Further strip out includes.
43
#include <drizzled/copy_field.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
44
#include <drizzled/transaction_services.h>
45
#include <drizzled/filesort.h>
46
#include <drizzled/message.h>
2200.2.13 by Stewart Smith
make ALTER TABLE RENAME and DISABLE/ENABLE keys use the AlterTable protobuf message constructed in the parser, with asserts for the old AlterInfo structure matching
47
#include <drizzled/message/alter_table.pb.h>
2198.1.1 by Olaf van der Spek
Remove unnecessary alter* includes
48
#include <drizzled/alter_column.h>
49
#include <drizzled/alter_info.h>
2239.1.9 by Olaf van der Spek
Refactor includes
50
#include <drizzled/util/test.h>
2263.3.11 by Olaf van der Spek
Open Tables
51
#include <drizzled/open_tables_state.h>
2272.1.1 by Olaf van der Spek
Refactor includes
52
#include <drizzled/table/cache.h>
2318.8.3 by Olaf van der Spek
CreateField includes
53
#include <drizzled/create_field.h>
1802.12.2 by Brian Aker
Correcting alter schema to fix bug around schema not altering collate
54
1130.3.6 by Monty Taylor
Removed use of using namespace std; from a header.
55
using namespace std;
1100.3.63 by Padraig O'Sullivan
Extracted the ALTER TABLE command into its own class and implementation
56
2272.1.1 by Olaf van der Spek
Refactor includes
57
namespace drizzled {
1130.3.13 by Monty Taylor
Finished cleaning namespaces in drizzled/statement
58
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
59
extern pid_t current_pid;
60
1578.4.11 by Brian Aker
PAss through the code removing current_session
61
static int copy_data_between_tables(Session *session,
62
                                    Table *from,Table *to,
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
63
                                    List<CreateField> &create,
64
                                    bool ignore,
65
                                    uint32_t order_num,
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
66
                                    Order *order,
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
67
                                    ha_rows *copied,
68
                                    ha_rows *deleted,
2200.2.16 by Stewart Smith
remove enum_enable_or_disable which was only used for alter_info.keys_onoff. We can just use the message::AlterTable now
69
                                    message::AlterTable &alter_table_message,
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
70
                                    bool error_if_not_empty);
1223.4.13 by Brian Aker
Small shift in alter table (break the baby down...).
71
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
72
static bool prepare_alter_table(Session *session,
2200.2.15 by Stewart Smith
remove enum ha_build_method and instead use the build method in message::AlterTable.
73
                                Table *table,
74
                                HA_CREATE_INFO *create_info,
75
                                const message::Table &original_proto,
76
                                message::Table &table_message,
77
                                message::AlterTable &alter_table_message,
78
                                AlterInfo *alter_info);
1223.4.13 by Brian Aker
Small shift in alter table (break the baby down...).
79
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
80
static Table *open_alter_table(Session *session, Table *table, identifier::Table &identifier);
1223.4.13 by Brian Aker
Small shift in alter table (break the baby down...).
81
2200.2.13 by Stewart Smith
make ALTER TABLE RENAME and DISABLE/ENABLE keys use the AlterTable protobuf message constructed in the parser, with asserts for the old AlterInfo structure matching
82
static int apply_online_alter_keys_onoff(Session *session,
83
                                         Table* table,
84
                                         const message::AlterTable::AlterKeysOnOff &op);
85
86
static int apply_online_rename_table(Session *session,
87
                                     Table *table,
88
                                     plugin::StorageEngine *original_engine,
89
                                     identifier::Table &original_table_identifier,
90
                                     identifier::Table &new_table_identifier,
91
                                     const message::AlterTable::RenameTable &alter_operation);
92
2096.1.4 by Brian Aker
Clean up errors and pass information in creation of statement.
93
namespace statement {
94
2200.2.25 by Stewart Smith
Merge refactoring of default values work
95
AlterTable::AlterTable(Session *in_session, Table_ident *) :
2096.1.4 by Brian Aker
Clean up errors and pass information in creation of statement.
96
  CreateTable(in_session)
2224.2.9 by Olaf van der Spek
Statement::set_command
97
{
98
  set_command(SQLCOM_ALTER_TABLE);
2096.1.4 by Brian Aker
Clean up errors and pass information in creation of statement.
99
}
100
101
} // namespace statement
102
1100.3.63 by Padraig O'Sullivan
Extracted the ALTER TABLE command into its own class and implementation
103
bool statement::AlterTable::execute()
104
{
2224.2.8 by Olaf van der Spek
Statement::lex()
105
  TableList *first_table= (TableList *) lex().select_lex.table_list.first;
106
  TableList *all_tables= lex().query_tables;
1100.3.63 by Padraig O'Sullivan
Extracted the ALTER TABLE command into its own class and implementation
107
  assert(first_table == all_tables && first_table != 0);
2224.2.8 by Olaf van der Spek
Statement::lex()
108
  Select_Lex *select_lex= &lex().select_lex;
1100.3.63 by Padraig O'Sullivan
Extracted the ALTER TABLE command into its own class and implementation
109
2029.1.2 by Brian Aker
Merge in refactor of LIKE up to its own calling pointer in the parser.
110
  is_engine_set= not createTableMessage().engine().name().empty();
111
1222.1.3 by Brian Aker
Remove used flag for engine.
112
  if (is_engine_set)
1160.1.5 by Brian Aker
Final parser SE removal.
113
  {
2224.2.9 by Olaf van der Spek
Statement::set_command
114
    create_info().db_type=
2227.4.2 by Olaf van der Spek
Statement::session()
115
      plugin::StorageEngine::findByName(session(), createTableMessage().engine().name());
1160.1.5 by Brian Aker
Final parser SE removal.
116
2029.1.2 by Brian Aker
Merge in refactor of LIKE up to its own calling pointer in the parser.
117
    if (create_info().db_type == NULL)
1160.1.5 by Brian Aker
Final parser SE removal.
118
    {
2096.1.4 by Brian Aker
Clean up errors and pass information in creation of statement.
119
      my_error(createTableMessage().engine().name(), ER_UNKNOWN_STORAGE_ENGINE, MYF(0));
1160.1.5 by Brian Aker
Final parser SE removal.
120
121
      return true;
122
    }
123
  }
124
1100.3.63 by Padraig O'Sullivan
Extracted the ALTER TABLE command into its own class and implementation
125
  /* Must be set in the parser */
126
  assert(select_lex->db);
127
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
128
  /* Chicken/Egg... we need to search for the table, to know if the table exists, so we can build a full identifier from it */
1938.4.2 by Brian Aker
Fix style issue around table for message (though this is imperfect,...)
129
  message::table::shared_ptr original_table_message;
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
130
  {
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
131
    identifier::Table identifier(first_table->getSchemaName(), first_table->getTableName());
2227.4.2 by Olaf van der Spek
Statement::session()
132
    if (not (original_table_message= plugin::StorageEngine::getTableMessage(session(), identifier)))
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
133
    {
2096.1.4 by Brian Aker
Clean up errors and pass information in creation of statement.
134
      my_error(ER_BAD_TABLE_ERROR, identifier);
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
135
      return true;
136
    }
1502.1.30 by Brian Aker
First pass on cleanup of Stewart's patch, plus re-engineer to make it work a
137
2029.1.2 by Brian Aker
Merge in refactor of LIKE up to its own calling pointer in the parser.
138
    if (not  create_info().db_type)
1502.1.30 by Brian Aker
First pass on cleanup of Stewart's patch, plus re-engineer to make it work a
139
    {
2224.2.9 by Olaf van der Spek
Statement::set_command
140
      create_info().db_type=
2227.4.2 by Olaf van der Spek
Statement::session()
141
        plugin::StorageEngine::findByName(session(), original_table_message->engine().name());
1502.1.30 by Brian Aker
First pass on cleanup of Stewart's patch, plus re-engineer to make it work a
142
2029.1.2 by Brian Aker
Merge in refactor of LIKE up to its own calling pointer in the parser.
143
      if (not create_info().db_type)
1502.1.30 by Brian Aker
First pass on cleanup of Stewart's patch, plus re-engineer to make it work a
144
      {
2096.1.4 by Brian Aker
Clean up errors and pass information in creation of statement.
145
        my_error(ER_BAD_TABLE_ERROR, identifier);
1502.1.30 by Brian Aker
First pass on cleanup of Stewart's patch, plus re-engineer to make it work a
146
        return true;
147
      }
148
    }
149
  }
150
151
  if (not validateCreateTableOption())
152
    return true;
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
153
2227.4.1 by Olaf van der Spek
Statement::session()
154
  if (session().inTransaction())
1890.2.30 by Stewart Smith
throw ER_TRANSACTIONAL_DDL_NOT_SUPPORTED in ALTER TABLE if there is an ongoing txn
155
  {
156
    my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
157
    return true;
1890.2.30 by Stewart Smith
throw ER_TRANSACTIONAL_DDL_NOT_SUPPORTED in ALTER TABLE if there is an ongoing txn
158
  }
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
159
2318.8.6 by Olaf van der Spek
Add const
160
  if (session().wait_if_global_read_lock(0, 1))
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
161
    return true;
162
163
  bool res;
1910.2.15 by Brian Aker
Update so that we use shared_ptr from the cache throughout more of the
164
  if (original_table_message->type() == message::Table::STANDARD )
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
165
  {
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
166
    identifier::Table identifier(first_table->getSchemaName(), first_table->getTableName());
167
    identifier::Table new_identifier(select_lex->db ? select_lex->db : first_table->getSchemaName(),
2430.1.8 by Olaf van der Spek
Use assign(), data() and size()
168
                                   lex().name.data() ? lex().name.data() : first_table->getTableName());
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
169
2227.4.2 by Olaf van der Spek
Statement::session()
170
    res= alter_table(&session(),
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
171
                     identifier,
1395.1.2 by Brian Aker
More logic pulling from ALTER TABLE
172
                     new_identifier,
2029.1.2 by Brian Aker
Merge in refactor of LIKE up to its own calling pointer in the parser.
173
                     &create_info(),
1910.2.15 by Brian Aker
Update so that we use shared_ptr from the cache throughout more of the
174
                     *original_table_message,
2029.1.2 by Brian Aker
Merge in refactor of LIKE up to its own calling pointer in the parser.
175
                     createTableMessage(),
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
176
                     first_table,
177
                     &alter_info,
2183.2.20 by Olaf van der Spek
Use List::size()
178
                     select_lex->order_list.size(),
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
179
                     (Order *) select_lex->order_list.first,
2224.2.8 by Olaf van der Spek
Statement::lex()
180
                     lex().ignore);
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
181
  }
182
  else
183
  {
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
184
    identifier::Table catch22(first_table->getSchemaName(), first_table->getTableName());
2263.3.3 by Olaf van der Spek
Use open_tables
185
    Table *table= session().open_tables.find_temporary_table(catch22);
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
186
    assert(table);
187
    {
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
188
      identifier::Table identifier(first_table->getSchemaName(), first_table->getTableName(), table->getMutableShare()->getPath());
189
      identifier::Table new_identifier(select_lex->db ? select_lex->db : first_table->getSchemaName(),
2430.1.8 by Olaf van der Spek
Use assign(), data() and size()
190
                                       lex().name.data() ? lex().name.data() : first_table->getTableName(),
2098.4.1 by Brian Aker
Make session encapsulated.
191
                                       table->getMutableShare()->getPath());
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
192
2227.4.2 by Olaf van der Spek
Statement::session()
193
      res= alter_table(&session(),
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
194
                       identifier,
1395.1.2 by Brian Aker
More logic pulling from ALTER TABLE
195
                       new_identifier,
2029.1.2 by Brian Aker
Merge in refactor of LIKE up to its own calling pointer in the parser.
196
                       &create_info(),
1910.2.15 by Brian Aker
Update so that we use shared_ptr from the cache throughout more of the
197
                       *original_table_message,
2029.1.2 by Brian Aker
Merge in refactor of LIKE up to its own calling pointer in the parser.
198
                       createTableMessage(),
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
199
                       first_table,
200
                       &alter_info,
2183.2.20 by Olaf van der Spek
Use List::size()
201
                       select_lex->order_list.size(),
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
202
                       (Order *) select_lex->order_list.first,
2224.2.8 by Olaf van der Spek
Statement::lex()
203
                       lex().ignore);
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
204
    }
205
  }
206
1100.3.63 by Padraig O'Sullivan
Extracted the ALTER TABLE command into its own class and implementation
207
  /*
208
     Release the protection against the global read lock and wake
209
     everyone, who might want to set a global read lock.
210
   */
2227.4.1 by Olaf van der Spek
Statement::session()
211
  session().startWaitingGlobalReadLock();
1910.2.2 by Brian Aker
First pass through the global lock refactor merge.
212
1100.3.63 by Padraig O'Sullivan
Extracted the ALTER TABLE command into its own class and implementation
213
  return res;
214
}
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
215
1130.3.12 by Monty Taylor
using namespace drizzled; to namespace drizzled { in statement/
216
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
217
/**
218
  Prepare column and key definitions for CREATE TABLE in ALTER Table.
219
220
  This function transforms parse output of ALTER Table - lists of
221
  columns and keys to add, drop or modify into, essentially,
222
  CREATE TABLE definition - a list of columns and keys of the new
223
  table. While doing so, it also performs some (bug not all)
224
  semantic checks.
225
226
  This function is invoked when we know that we're going to
227
  perform ALTER Table via a temporary table -- i.e. fast ALTER Table
228
  is not possible, perhaps because the ALTER statement contains
229
  instructions that require change in table data, not only in
230
  table definition or indexes.
231
232
  @param[in,out]  session         thread handle. Used as a memory pool
233
                              and source of environment information.
234
  @param[in]      table       the source table, open and locked
235
                              Used as an interface to the storage engine
236
                              to acquire additional information about
237
                              the original table.
238
  @param[in,out]  create_info A blob with CREATE/ALTER Table
239
                              parameters
240
  @param[in,out]  alter_info  Another blob with ALTER/CREATE parameters.
241
                              Originally create_info was used only in
242
                              CREATE TABLE and alter_info only in ALTER Table.
243
                              But since ALTER might end-up doing CREATE,
244
                              this distinction is gone and we just carry
245
                              around two structures.
246
247
  @return
248
    Fills various create_info members based on information retrieved
249
    from the storage engine.
250
    Sets create_info->varchar if the table has a VARCHAR column.
251
    Prepares alter_info->create_list and alter_info->key_list with
252
    columns and keys of the new table.
253
  @retval true   error, out of memory or a semantical error in ALTER
254
                 Table instructions
255
  @retval false  success
256
*/
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
257
static bool prepare_alter_table(Session *session,
2098.4.1 by Brian Aker
Make session encapsulated.
258
                                Table *table,
259
                                HA_CREATE_INFO *create_info,
260
                                const message::Table &original_proto,
261
                                message::Table &table_message,
2200.2.15 by Stewart Smith
remove enum ha_build_method and instead use the build method in message::AlterTable.
262
                                message::AlterTable &alter_table_message,
2098.4.1 by Brian Aker
Make session encapsulated.
263
                                AlterInfo *alter_info)
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
264
{
265
  uint32_t used_fields= create_info->used_fields;
2200.2.19 by Stewart Smith
use the AlterTable protobuf message list of colmuns, keys and foreign keys to drop inside the ALTER TABLE code instead of the list in alter_info
266
  vector<string> drop_keys;
267
  vector<string> drop_columns;
268
  vector<string> drop_fkeys;
269
270
  /* we use drop_(keys|columns|fkey) below to check that we can do all
271
     operations we've been asked to do */
272
  for (int operationnr= 0; operationnr < alter_table_message.operations_size();
273
       operationnr++)
274
  {
275
    const message::AlterTable::AlterTableOperation &operation=
276
      alter_table_message.operations(operationnr);
277
278
    switch (operation.operation())
279
    {
280
    case message::AlterTable::AlterTableOperation::DROP_KEY:
281
      drop_keys.push_back(operation.drop_name());
282
      break;
283
    case message::AlterTable::AlterTableOperation::DROP_COLUMN:
284
      drop_columns.push_back(operation.drop_name());
285
      break;
286
    case message::AlterTable::AlterTableOperation::DROP_FOREIGN_KEY:
287
      drop_fkeys.push_back(operation.drop_name());
288
      break;
289
    default:
290
      break;
291
    }
292
  }
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
293
294
  /* Let new create options override the old ones */
2179.7.9 by Olaf van der Spek
Refactor
295
  message::Table::TableOptions *table_options= table_message.mutable_options();
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
296
2068.6.6 by Brian Aker
style cleanup
297
  if (not (used_fields & HA_CREATE_USED_DEFAULT_CHARSET))
1574 by Brian Aker
Rollup patch for hiding tableshare.
298
    create_info->default_table_charset= table->getShare()->table_charset;
2068.6.6 by Brian Aker
style cleanup
299
300
  if (not (used_fields & HA_CREATE_USED_AUTO) && table->found_next_number_field)
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
301
  {
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
302
    /* Table has an autoincrement, copy value to new table */
1208.3.2 by brian
Update for Cursor renaming.
303
    table->cursor->info(HA_STATUS_AUTO);
304
    create_info->auto_increment_value= table->cursor->stats.auto_increment_value;
1638.10.116 by Stewart Smith
only display AUTO_INCREMENT value if explicitly set by user (and preserve across ALTER TABLE).
305
    if (create_info->auto_increment_value != original_proto.options().auto_increment_value())
306
      table_options->set_has_user_set_auto_increment_value(false);
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
307
  }
2068.6.6 by Brian Aker
style cleanup
308
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
309
  table->restoreRecordAsDefault(); /* Empty record for DEFAULT */
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
310
2179.7.8 by Olaf van der Spek
Refactor
311
  List<CreateField> new_create_list;
2179.7.9 by Olaf van der Spek
Refactor
312
  List<Key> new_key_list;
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
313
  /* First collect all fields from table which isn't in drop_list */
314
  Field *field;
2068.6.1 by Brian Aker
Cut down the number of keywords/small style cleanup.
315
  for (Field **f_ptr= table->getFields(); (field= *f_ptr); f_ptr++)
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
316
  {
317
    /* Check if field should be dropped */
2200.2.19 by Stewart Smith
use the AlterTable protobuf message list of colmuns, keys and foreign keys to drop inside the ALTER TABLE code instead of the list in alter_info
318
    vector<string>::iterator it= drop_columns.begin();
2385.3.30 by Olaf van der Spek
cppcheck
319
    while (it != drop_columns.end())
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
320
    {
2385.3.30 by Olaf van der Spek
cppcheck
321
      if (not my_strcasecmp(system_charset_info, field->field_name, it->c_str()))
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
322
      {
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
323
        /* Reset auto_increment value if it was dropped */
324
        if (MTYP_TYPENR(field->unireg_check) == Field::NEXT_NUMBER &&
2179.7.1 by Olaf van der Spek
x
325
            not (used_fields & HA_CREATE_USED_AUTO))
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
326
        {
327
          create_info->auto_increment_value= 0;
328
          create_info->used_fields|= HA_CREATE_USED_AUTO;
329
        }
330
        break;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
331
      }
2200.2.19 by Stewart Smith
use the AlterTable protobuf message list of colmuns, keys and foreign keys to drop inside the ALTER TABLE code instead of the list in alter_info
332
      it++;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
333
    }
2068.6.1 by Brian Aker
Cut down the number of keywords/small style cleanup.
334
2200.2.19 by Stewart Smith
use the AlterTable protobuf message list of colmuns, keys and foreign keys to drop inside the ALTER TABLE code instead of the list in alter_info
335
    if (it != drop_columns.end())
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
336
    {
2200.2.19 by Stewart Smith
use the AlterTable protobuf message list of colmuns, keys and foreign keys to drop inside the ALTER TABLE code instead of the list in alter_info
337
      drop_columns.erase(it);
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
338
      continue;
339
    }
2224.2.9 by Olaf van der Spek
Statement::set_command
340
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
341
    /* Mark that we will read the field */
342
    field->setReadSet();
343
2179.7.8 by Olaf van der Spek
Refactor
344
    CreateField *def;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
345
    /* Check if field is changed */
2179.7.9 by Olaf van der Spek
Refactor
346
    List<CreateField>::iterator def_it= alter_info->create_list.begin();
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
347
    while ((def= def_it++))
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
348
    {
349
      if (def->change &&
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
350
          ! my_strcasecmp(system_charset_info, field->field_name, def->change))
351
	      break;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
352
    }
2068.6.1 by Brian Aker
Cut down the number of keywords/small style cleanup.
353
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
354
    if (def)
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
355
    {
356
      /* Field is changed */
357
      def->field= field;
358
      if (! def->after)
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
359
      {
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
360
        new_create_list.push_back(def);
361
        def_it.remove();
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
362
      }
363
    }
364
    else
365
    {
366
      /*
367
        This field was not dropped and not changed, add it to the list
368
        for the new table.
369
      */
370
      def= new CreateField(field, field);
371
      new_create_list.push_back(def);
2179.7.4 by Olaf van der Spek
Use std::list
372
      AlterInfo::alter_list_t::iterator alter(alter_info->alter_list.begin());
2008.2.4 by Brian Aker
Merge in additional fixes for sign, plus alter table, plus TIME on
373
2179.7.4 by Olaf van der Spek
Use std::list
374
      for (; alter != alter_info->alter_list.end(); alter++)
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
375
      {
2179.7.4 by Olaf van der Spek
Use std::list
376
        if (not my_strcasecmp(system_charset_info,field->field_name, alter->name))
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
377
          break;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
378
      }
2008.2.4 by Brian Aker
Merge in additional fixes for sign, plus alter table, plus TIME on
379
2179.7.4 by Olaf van der Spek
Use std::list
380
      if (alter != alter_info->alter_list.end())
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
381
      {
2221.12.7 by Stewart Smith
in ALTER TABLE SET DEFAULT code path, use the new CreateField::setDefaultValue method - this means we have the same logic for setting default values for a CreateField as in CREATE TABLE
382
        def->setDefaultValue(alter->def, NULL);
2008.2.4 by Brian Aker
Merge in additional fixes for sign, plus alter table, plus TIME on
383
2179.7.4 by Olaf van der Spek
Use std::list
384
        alter_info->alter_list.erase(alter);
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
385
      }
386
    }
387
  }
2068.6.1 by Brian Aker
Cut down the number of keywords/small style cleanup.
388
2179.7.8 by Olaf van der Spek
Refactor
389
  CreateField *def;
2179.7.9 by Olaf van der Spek
Refactor
390
  List<CreateField>::iterator def_it= alter_info->create_list.begin();
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
391
  while ((def= def_it++)) /* Add new columns */
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
392
  {
393
    if (def->change && ! def->field)
394
    {
1574 by Brian Aker
Rollup patch for hiding tableshare.
395
      my_error(ER_BAD_FIELD_ERROR, MYF(0), def->change, table->getMutableShare()->getTableName());
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
396
      return true;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
397
    }
398
    /*
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
399
      If we have been given a field which has no default value, and is not null then we need to bail.
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
400
    */
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
401
    if (not (~def->flags & (NO_DEFAULT_VALUE_FLAG | NOT_NULL_FLAG)) and not def->change)
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
402
    {
403
      alter_info->error_if_not_empty= true;
404
    }
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
405
    if (! def->after)
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
406
    {
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
407
      new_create_list.push_back(def);
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
408
    }
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
409
    else if (def->after == first_keyword)
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
410
    {
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
411
      new_create_list.push_front(def);
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
412
    }
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
413
    else
414
    {
415
      CreateField *find;
2179.7.8 by Olaf van der Spek
Refactor
416
      List<CreateField>::iterator find_it= new_create_list.begin();
2068.6.6 by Brian Aker
style cleanup
417
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
418
      while ((find= find_it++)) /* Add new columns */
419
      {
2068.6.6 by Brian Aker
style cleanup
420
        if (not my_strcasecmp(system_charset_info,def->after, find->field_name))
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
421
          break;
422
      }
2068.6.6 by Brian Aker
style cleanup
423
424
      if (not find)
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
425
      {
1574 by Brian Aker
Rollup patch for hiding tableshare.
426
        my_error(ER_BAD_FIELD_ERROR, MYF(0), def->after, table->getMutableShare()->getTableName());
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
427
        return true;
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
428
      }
2068.6.6 by Brian Aker
style cleanup
429
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
430
      find_it.after(def); /* Put element after this */
2068.6.6 by Brian Aker
style cleanup
431
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
432
      /*
433
        XXX: hack for Bug#28427.
434
        If column order has changed, force OFFLINE ALTER Table
435
        without querying engine capabilities.  If we ever have an
436
        engine that supports online ALTER Table CHANGE COLUMN
437
        <name> AFTER <name1> (Falcon?), this fix will effectively
438
        disable the capability.
439
        TODO: detect the situation in compare_tables, behave based
440
        on engine capabilities.
441
      */
2200.2.15 by Stewart Smith
remove enum ha_build_method and instead use the build method in message::AlterTable.
442
      if (alter_table_message.build_method() == message::AlterTable::BUILD_ONLINE)
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
443
      {
2096.1.4 by Brian Aker
Clean up errors and pass information in creation of statement.
444
        my_error(*session->getQueryString(), ER_NOT_SUPPORTED_YET);
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
445
        return true;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
446
      }
447
    }
448
  }
2068.6.1 by Brian Aker
Cut down the number of keywords/small style cleanup.
449
2179.7.2 by Olaf van der Spek
x
450
  if (not alter_info->alter_list.empty())
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
451
  {
2179.7.9 by Olaf van der Spek
Refactor
452
    my_error(ER_BAD_FIELD_ERROR, MYF(0), alter_info->alter_list.front().name, table->getMutableShare()->getTableName());
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
453
    return true;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
454
  }
2068.6.1 by Brian Aker
Cut down the number of keywords/small style cleanup.
455
2179.7.10 by Olaf van der Spek
Refactor
456
  if (new_create_list.is_empty())
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
457
  {
2179.7.9 by Olaf van der Spek
Refactor
458
    my_message(ER_CANT_REMOVE_ALL_FIELDS, ER(ER_CANT_REMOVE_ALL_FIELDS), MYF(0));
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
459
    return true;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
460
  }
461
462
  /*
463
    Collect all keys which isn't in drop list. Add only those
464
    for which some fields exists.
465
  */
2179.7.9 by Olaf van der Spek
Refactor
466
  KeyInfo *key_info= table->key_info;
1578.2.10 by Brian Aker
keys and fields partial encapsulation.
467
  for (uint32_t i= 0; i < table->getShare()->sizeKeys(); i++, key_info++)
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
468
  {
469
    char *key_name= key_info->name;
2068.6.6 by Brian Aker
style cleanup
470
2200.2.19 by Stewart Smith
use the AlterTable protobuf message list of colmuns, keys and foreign keys to drop inside the ALTER TABLE code instead of the list in alter_info
471
    vector<string>::iterator it= drop_keys.begin();
2385.3.19 by Olaf van der Spek
Refactor
472
    while (it != drop_keys.end())
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
473
    {
2385.3.19 by Olaf van der Spek
Refactor
474
      if (not my_strcasecmp(system_charset_info, key_name, (*it).c_str()))
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
475
        break;
2200.2.19 by Stewart Smith
use the AlterTable protobuf message list of colmuns, keys and foreign keys to drop inside the ALTER TABLE code instead of the list in alter_info
476
      it++;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
477
    }
2068.6.6 by Brian Aker
style cleanup
478
2200.2.19 by Stewart Smith
use the AlterTable protobuf message list of colmuns, keys and foreign keys to drop inside the ALTER TABLE code instead of the list in alter_info
479
    if (it != drop_keys.end())
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
480
    {
2200.2.19 by Stewart Smith
use the AlterTable protobuf message list of colmuns, keys and foreign keys to drop inside the ALTER TABLE code instead of the list in alter_info
481
      drop_keys.erase(it);
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
482
      continue;
483
    }
484
1534 by Brian Aker
Remove of KeyPartInfo
485
    KeyPartInfo *key_part= key_info->key_part;
2179.7.9 by Olaf van der Spek
Refactor
486
    List<Key_part_spec> key_parts;
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
487
    for (uint32_t j= 0; j < key_info->key_parts; j++, key_part++)
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
488
    {
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
489
      if (! key_part->field)
490
	      continue;	/* Wrong field (from UNIREG) */
491
492
      const char *key_part_name= key_part->field->field_name;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
493
      CreateField *cfield;
2179.7.8 by Olaf van der Spek
Refactor
494
      List<CreateField>::iterator field_it= new_create_list.begin();
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
495
      while ((cfield= field_it++))
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
496
      {
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
497
        if (cfield->change)
498
        {
2068.6.6 by Brian Aker
style cleanup
499
          if (not my_strcasecmp(system_charset_info, key_part_name, cfield->change))
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
500
            break;
501
        }
2068.6.6 by Brian Aker
style cleanup
502
        else if (not my_strcasecmp(system_charset_info, key_part_name, cfield->field_name))
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
503
          break;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
504
      }
2068.6.6 by Brian Aker
style cleanup
505
506
      if (not cfield)
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
507
	      continue; /* Field is removed */
2224.2.9 by Olaf van der Spek
Statement::set_command
508
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
509
      uint32_t key_part_length= key_part->length;
510
      if (cfield->field) /* Not new field */
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
511
      {
512
        /*
513
          If the field can't have only a part used in a key according to its
514
          new type, or should not be used partially according to its
515
          previous type, or the field length is less than the key part
516
          length, unset the key part length.
517
518
          We also unset the key part length if it is the same as the
519
          old field's length, so the whole new field will be used.
520
521
          BLOBs may have cfield->length == 0, which is why we test it before
522
          checking whether cfield->length < key_part_length (in chars).
523
         */
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
524
        if (! Field::type_can_have_key_part(cfield->field->type()) ||
525
            ! Field::type_can_have_key_part(cfield->sql_type) ||
1119.9.17 by Stewart Smith
merge with trunk
526
            (cfield->field->field_length == key_part_length) ||
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
527
            (cfield->length &&
528
             (cfield->length < key_part_length / key_part->field->charset()->mbmaxlen)))
529
          key_part_length= 0; /* Use whole field */
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
530
      }
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
531
      key_part_length/= key_part->field->charset()->mbmaxlen;
2420.2.1 by Olaf van der Spek
Refactor
532
      key_parts.push_back(new Key_part_spec(cfield->field_name, strlen(cfield->field_name), key_part_length));
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
533
    }
2183.2.20 by Olaf van der Spek
Use List::size()
534
    if (key_parts.size())
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
535
    {
2420.2.1 by Olaf van der Spek
Refactor
536
      KEY_CREATE_INFO key_create_info= default_key_create_info;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
537
      key_create_info.algorithm= key_info->algorithm;
2096.1.5 by Brian Aker
Fix key init, stop using memset.
538
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
539
      if (key_info->flags & HA_USES_BLOCK_SIZE)
540
        key_create_info.block_size= key_info->block_size;
2096.1.5 by Brian Aker
Fix key init, stop using memset.
541
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
542
      if (key_info->flags & HA_USES_COMMENT)
543
        key_create_info.comment= key_info->comment;
544
2385.3.29 by Olaf van der Spek
Refactor
545
      Key::Keytype key_type;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
546
      if (key_info->flags & HA_NOSAME)
547
      {
2385.3.29 by Olaf van der Spek
Refactor
548
        key_type= is_primary_key(key_name) ? Key::PRIMARY : Key::UNIQUE;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
549
      }
550
      else
2096.1.5 by Brian Aker
Fix key init, stop using memset.
551
      {
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
552
        key_type= Key::MULTIPLE;
2096.1.5 by Brian Aker
Fix key init, stop using memset.
553
      }
2385.3.29 by Olaf van der Spek
Refactor
554
      new_key_list.push_back(new Key(key_type, key_name, strlen(key_name), &key_create_info, test(key_info->flags & HA_GENERATED_KEY), key_parts));
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
555
    }
556
  }
1638.10.80 by Stewart Smith
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.
557
558
  /* Copy over existing foreign keys */
2068.6.7 by Brian Aker
Style cleanup around locks for alter table.
559
  for (int32_t j= 0; j < original_proto.fk_constraint_size(); j++)
1638.10.80 by Stewart Smith
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.
560
  {
2200.2.19 by Stewart Smith
use the AlterTable protobuf message list of colmuns, keys and foreign keys to drop inside the ALTER TABLE code instead of the list in alter_info
561
    vector<string>::iterator it= drop_fkeys.begin();
2385.3.30 by Olaf van der Spek
cppcheck
562
    while (it != drop_fkeys.end())
1638.10.80 by Stewart Smith
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.
563
    {
2385.3.30 by Olaf van der Spek
cppcheck
564
      if (! my_strcasecmp(system_charset_info, original_proto.fk_constraint(j).name().c_str(), it->c_str()))
1638.10.80 by Stewart Smith
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.
565
      {
566
        break;
567
      }
2200.2.19 by Stewart Smith
use the AlterTable protobuf message list of colmuns, keys and foreign keys to drop inside the ALTER TABLE code instead of the list in alter_info
568
      it++;
1638.10.80 by Stewart Smith
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.
569
    }
2200.2.19 by Stewart Smith
use the AlterTable protobuf message list of colmuns, keys and foreign keys to drop inside the ALTER TABLE code instead of the list in alter_info
570
571
    if (it != drop_fkeys.end())
1638.10.80 by Stewart Smith
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.
572
    {
2200.2.19 by Stewart Smith
use the AlterTable protobuf message list of colmuns, keys and foreign keys to drop inside the ALTER TABLE code instead of the list in alter_info
573
      drop_fkeys.erase(it);
1638.10.80 by Stewart Smith
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.
574
      continue;
575
    }
576
577
    message::Table::ForeignKeyConstraint *pfkey= table_message.add_fk_constraint();
578
    *pfkey= original_proto.fk_constraint(j);
579
  }
580
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
581
  {
582
    Key *key;
2179.7.9 by Olaf van der Spek
Refactor
583
    List<Key>::iterator key_it(alter_info->key_list.begin());
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
584
    while ((key= key_it++)) /* Add new keys */
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
585
    {
1638.10.80 by Stewart Smith
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.
586
      if (key->type == Key::FOREIGN_KEY)
587
      {
588
        if (((Foreign_key *)key)->validate(new_create_list))
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
589
        {
590
          return true;
591
        }
1638.10.80 by Stewart Smith
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.
592
593
        Foreign_key *fkey= (Foreign_key*)key;
594
        add_foreign_key_to_table_message(&table_message,
2430.1.8 by Olaf van der Spek
Use assign(), data() and size()
595
                                         fkey->name.data(),
1638.10.80 by Stewart Smith
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.
596
                                         fkey->columns,
597
                                         fkey->ref_table,
598
                                         fkey->ref_columns,
599
                                         fkey->delete_opt,
600
                                         fkey->update_opt,
601
                                         fkey->match_opt);
602
      }
603
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
604
      if (key->type != Key::FOREIGN_KEY)
605
        new_key_list.push_back(key);
1638.10.80 by Stewart Smith
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.
606
2430.1.8 by Olaf van der Spek
Use assign(), data() and size()
607
      if (key->name.data() && is_primary_key(key->name.data()))
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
608
      {
2430.1.8 by Olaf van der Spek
Use assign(), data() and size()
609
        my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), key->name.data());
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
610
        return true;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
611
      }
612
    }
613
  }
614
1638.10.80 by Stewart Smith
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.
615
  /* Fix names of foreign keys being added */
616
  for (int j= 0; j < table_message.fk_constraint_size(); j++)
617
  {
618
    if (! table_message.fk_constraint(j).has_name())
619
    {
620
      std::string name(table->getMutableShare()->getTableName());
621
      char number[20];
622
623
      name.append("_ibfk_");
624
      snprintf(number, sizeof(number), "%d", j+1);
625
      name.append(number);
626
627
      message::Table::ForeignKeyConstraint *pfkey= table_message.mutable_fk_constraint(j);
628
      pfkey->set_name(name);
629
    }
630
  }
631
2200.2.19 by Stewart Smith
use the AlterTable protobuf message list of colmuns, keys and foreign keys to drop inside the ALTER TABLE code instead of the list in alter_info
632
  if (drop_keys.size())
633
  {
634
    my_error(ER_CANT_DROP_FIELD_OR_KEY,
635
             MYF(0),
636
             drop_keys.front().c_str());
637
    return true;
638
  }
639
640
  if (drop_columns.size())
641
  {
642
    my_error(ER_CANT_DROP_FIELD_OR_KEY,
643
             MYF(0),
644
             drop_columns.front().c_str());
645
    return true;
646
  }
647
648
  if (drop_fkeys.size())
649
  {
650
    my_error(ER_CANT_DROP_FIELD_OR_KEY,
651
             MYF(0),
652
             drop_fkeys.front().c_str());
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
653
    return true;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
654
  }
2008.2.4 by Brian Aker
Merge in additional fixes for sign, plus alter table, plus TIME on
655
2179.7.2 by Olaf van der Spek
x
656
  if (not alter_info->alter_list.empty())
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
657
  {
1126.3.5 by Jay Pipes
Clean up the style and spacing in mysql_prepare_alter_table() before I lose my friggin mind.
658
    my_error(ER_CANT_DROP_FIELD_OR_KEY,
659
             MYF(0),
2179.7.2 by Olaf van der Spek
x
660
             alter_info->alter_list.front().name);
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
661
    return true;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
662
  }
663
1340.1.4 by Brian Aker
A first pass through adding a timestamp to our proto.
664
  if (not table_message.options().has_comment()
1574 by Brian Aker
Rollup patch for hiding tableshare.
665
      && table->getMutableShare()->hasComment())
2068.6.7 by Brian Aker
Style cleanup around locks for alter table.
666
  {
1574 by Brian Aker
Rollup patch for hiding tableshare.
667
    table_options->set_comment(table->getMutableShare()->getComment());
2068.6.7 by Brian Aker
Style cleanup around locks for alter table.
668
  }
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
669
1608.2.3 by Brian Aker
Encapsulate type for TableShare.
670
  if (table->getShare()->getType())
1222.2.1 by Brian Aker
Takes the majority usage of parser based HA_CREATE_INFO usage around
671
  {
1340.1.4 by Brian Aker
A first pass through adding a timestamp to our proto.
672
    table_message.set_type(message::Table::TEMPORARY);
1222.2.1 by Brian Aker
Takes the majority usage of parser based HA_CREATE_INFO usage around
673
  }
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
674
2134.1.6 by Brian Aker
Merge in encapsulation of the table message from the main class.
675
  table_message.set_creation_timestamp(table->getShare()->getTableMessage()->creation_timestamp());
676
  table_message.set_version(table->getShare()->getTableMessage()->version());
677
  table_message.set_uuid(table->getShare()->getTableMessage()->uuid());
1340.1.4 by Brian Aker
A first pass through adding a timestamp to our proto.
678
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
679
  alter_info->create_list.swap(new_create_list);
680
  alter_info->key_list.swap(new_key_list);
1502.1.30 by Brian Aker
First pass on cleanup of Stewart's patch, plus re-engineer to make it work a
681
682
  size_t num_engine_options= table_message.engine().options_size();
683
  size_t original_num_engine_options= original_proto.engine().options_size();
684
  for (size_t x= 0; x < original_num_engine_options; ++x)
685
  {
686
    bool found= false;
687
688
    for (size_t y= 0; y < num_engine_options; ++y)
689
    {
690
      found= not table_message.engine().options(y).name().compare(original_proto.engine().options(x).name());
2224.2.9 by Olaf van der Spek
Statement::set_command
691
1502.1.30 by Brian Aker
First pass on cleanup of Stewart's patch, plus re-engineer to make it work a
692
      if (found)
693
        break;
694
    }
695
696
    if (not found)
697
    {
1502.1.31 by Brian Aker
Merge engine options for schema/table.
698
      message::Engine::Option *opt= table_message.mutable_engine()->add_options();
1502.1.30 by Brian Aker
First pass on cleanup of Stewart's patch, plus re-engineer to make it work a
699
700
      opt->set_name(original_proto.engine().options(x).name());
701
      opt->set_state(original_proto.engine().options(x).state());
702
    }
703
  }
704
1802.12.2 by Brian Aker
Correcting alter schema to fix bug around schema not altering collate
705
  drizzled::message::update(table_message);
706
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
707
  return false;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
708
}
709
710
/* table_list should contain just one table */
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
711
static int discard_or_import_tablespace(Session *session,
2200.2.6 by Stewart Smith
switch DISCARD/IMPORT TABLESPACE to only use the alter table protobuf message
712
                                        TableList *table_list,
713
                                        bool discard)
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
714
{
715
  /*
716
    Note that DISCARD/IMPORT TABLESPACE always is the only operation in an
717
    ALTER Table
718
  */
719
  session->set_proc_info("discard_or_import_tablespace");
720
721
 /*
722
   We set this flag so that ha_innobase::open and ::external_lock() do
723
   not complain when we lock the table
724
 */
2151.7.9 by Stewart Smith
switch doing_tablespace_operation to doingTablespaceOperation and set_doing_tablespace_operation to setDoingTablespaceOperation
725
  session->setDoingTablespaceOperation(true);
2179.7.10 by Olaf van der Spek
Refactor
726
  Table* table= session->openTableLock(table_list, TL_WRITE);
727
  if (not table)
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
728
  {
2151.7.9 by Stewart Smith
switch doing_tablespace_operation to doingTablespaceOperation and set_doing_tablespace_operation to setDoingTablespaceOperation
729
    session->setDoingTablespaceOperation(false);
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
730
    return -1;
731
  }
732
2068.6.7 by Brian Aker
Style cleanup around locks for alter table.
733
  int error;
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
734
  do {
735
    error= table->cursor->ha_discard_or_import_tablespace(discard);
736
737
    session->set_proc_info("end");
738
739
    if (error)
740
      break;
741
742
    /* The ALTER Table is always in its own transaction */
2318.6.62 by Olaf van der Spek
Refactor
743
    error= TransactionServices::autocommitOrRollback(*session, false);
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
744
    if (not session->endActiveTransaction())
745
      error= 1;
746
747
    if (error)
748
      break;
749
2318.6.62 by Olaf van der Spek
Refactor
750
    TransactionServices::rawStatement(*session,
2211.2.1 by David Shrewsbury
Add raw_sql_schema member to Statement protobuf definition and set it in ALTER TABLE code path to current value associated with Session executing it.
751
                                      *session->getQueryString(),
752
                                      *session->schema());
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
753
754
  } while(0);
755
2318.6.62 by Olaf van der Spek
Refactor
756
  (void) TransactionServices::autocommitOrRollback(*session, error);
2151.7.9 by Stewart Smith
switch doing_tablespace_operation to doingTablespaceOperation and set_doing_tablespace_operation to setDoingTablespaceOperation
757
  session->setDoingTablespaceOperation(false);
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
758
759
  if (error == 0)
760
  {
761
    session->my_ok();
762
    return 0;
763
  }
764
1216.1.1 by Brian Aker
Move print_error up to Engine.
765
  table->print_error(error, MYF(0));
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
766
767
  return -1;
768
}
769
1126.3.3 by Jay Pipes
Moves Alter_info out into its own header and source file, cleans up some related include mess in sql_lex.h, and renames Alter_info to AlterInfo.
770
/**
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
771
  Manages enabling/disabling of indexes for ALTER Table
772
773
  SYNOPSIS
774
    alter_table_manage_keys()
775
      table                  Target table
776
      indexes_were_disabled  Whether the indexes of the from table
777
                             were disabled
778
      keys_onoff             ENABLE | DISABLE | LEAVE_AS_IS
779
780
  RETURN VALUES
781
    false  OK
782
    true   Error
783
*/
1578.4.11 by Brian Aker
PAss through the code removing current_session
784
static bool alter_table_manage_keys(Session *session,
785
                                    Table *table, int indexes_were_disabled,
2200.2.16 by Stewart Smith
remove enum_enable_or_disable which was only used for alter_info.keys_onoff. We can just use the message::AlterTable now
786
                                    const message::AlterTable &alter_table_message)
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
787
{
788
  int error= 0;
2200.2.16 by Stewart Smith
remove enum_enable_or_disable which was only used for alter_info.keys_onoff. We can just use the message::AlterTable now
789
  if (alter_table_message.has_alter_keys_onoff()
790
      && alter_table_message.alter_keys_onoff().enable())
791
  {
1208.3.2 by brian
Update for Cursor renaming.
792
    error= table->cursor->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
2200.2.16 by Stewart Smith
remove enum_enable_or_disable which was only used for alter_info.keys_onoff. We can just use the message::AlterTable now
793
  }
794
795
  if ((! alter_table_message.has_alter_keys_onoff() && indexes_were_disabled)
796
      || (alter_table_message.has_alter_keys_onoff()
797
          && ! alter_table_message.alter_keys_onoff().enable()))
798
  {
1208.3.2 by brian
Update for Cursor renaming.
799
    error= table->cursor->ha_disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
800
  }
801
802
  if (error == HA_ERR_WRONG_COMMAND)
803
  {
1578.4.11 by Brian Aker
PAss through the code removing current_session
804
    push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
805
                        ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
1574 by Brian Aker
Rollup patch for hiding tableshare.
806
                        table->getMutableShare()->getTableName());
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
807
    error= 0;
2046.2.3 by Brian Aker
Add basic tests for microtime.
808
  }
809
  else if (error)
810
  {
1216.1.1 by Brian Aker
Move print_error up to Engine.
811
    table->print_error(error, MYF(0));
2046.2.3 by Brian Aker
Add basic tests for microtime.
812
  }
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
813
814
  return(error);
815
}
816
1395.1.2 by Brian Aker
More logic pulling from ALTER TABLE
817
static bool lockTableIfDifferent(Session &session,
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
818
                                 identifier::Table &original_table_identifier,
819
                                 identifier::Table &new_table_identifier,
1395.1.2 by Brian Aker
More logic pulling from ALTER TABLE
820
                                 Table *name_lock)
821
{
822
  /* Check that we are not trying to rename to an existing table */
823
  if (not (original_table_identifier == new_table_identifier))
824
  {
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
825
    if (original_table_identifier.isTmp())
1395.1.2 by Brian Aker
More logic pulling from ALTER TABLE
826
    {
2263.3.3 by Olaf van der Spek
Use open_tables
827
      if (session.open_tables.find_temporary_table(new_table_identifier))
1395.1.2 by Brian Aker
More logic pulling from ALTER TABLE
828
      {
2096.1.4 by Brian Aker
Clean up errors and pass information in creation of statement.
829
        my_error(ER_TABLE_EXISTS_ERROR, new_table_identifier);
1395.1.2 by Brian Aker
More logic pulling from ALTER TABLE
830
        return false;
831
      }
832
    }
833
    else
834
    {
2263.9.1 by Olaf van der Spek
Table Cache
835
      name_lock= session.lock_table_name_if_not_cached(new_table_identifier);
1395.1.2 by Brian Aker
More logic pulling from ALTER TABLE
836
      if (not name_lock)
837
      {
2068.6.6 by Brian Aker
style cleanup
838
        my_error(ER_TABLE_EXISTS_ERROR, new_table_identifier);
1395.1.2 by Brian Aker
More logic pulling from ALTER TABLE
839
        return false;
840
      }
841
842
      if (plugin::StorageEngine::doesTableExist(session, new_table_identifier))
843
      {
844
        /* Table will be closed by Session::executeCommand() */
2068.6.6 by Brian Aker
style cleanup
845
        my_error(ER_TABLE_EXISTS_ERROR, new_table_identifier);
1395.1.2 by Brian Aker
More logic pulling from ALTER TABLE
846
2068.6.6 by Brian Aker
style cleanup
847
        {
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
848
          boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
2068.6.6 by Brian Aker
style cleanup
849
          session.unlink_open_table(name_lock);
850
        }
1395.1.2 by Brian Aker
More logic pulling from ALTER TABLE
851
852
        return false;
853
      }
854
    }
855
  }
856
857
  return true;
858
}
859
1126.3.3 by Jay Pipes
Moves Alter_info out into its own header and source file, cleans up some related include mess in sql_lex.h, and renames Alter_info to AlterInfo.
860
/**
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
861
  Alter table
862
863
  SYNOPSIS
1130.3.13 by Monty Taylor
Finished cleaning namespaces in drizzled/statement
864
    alter_table()
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
865
      session              Thread handle
866
      new_db           If there is a RENAME clause
867
      new_name         If there is a RENAME clause
868
      create_info      Information from the parsing phase about new
869
                       table properties.
870
      table_list       The table to change.
871
      alter_info       Lists of fields, keys to be changed, added
872
                       or dropped.
1273.2.13 by Stewart Smith
fix accidental mangling of comment: s/order_st BY/ORDER BY/. in drizzled/statement/alter_table.cc
873
      order_num        How many ORDER BY fields has been specified.
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
874
      order            List of fields to order_st BY.
875
      ignore           Whether we have ALTER IGNORE Table
876
877
  DESCRIPTION
878
    This is a veery long function and is everything but the kitchen sink :)
879
    It is used to alter a table and not only by ALTER Table but also
880
    CREATE|DROP INDEX are mapped on this function.
881
882
    When the ALTER Table statement just does a RENAME or ENABLE|DISABLE KEYS,
883
    or both, then this function short cuts its operation by renaming
884
    the table and/or enabling/disabling the keys. In this case, the FRM is
1130.3.13 by Monty Taylor
Finished cleaning namespaces in drizzled/statement
885
    not changed, directly by alter_table. However, if there is a
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
886
    RENAME + change of a field, or an index, the short cut is not used.
887
    See how `create_list` is used to generate the new FRM regarding the
888
    structure of the fields. The same is done for the indices of the table.
889
890
    Important is the fact, that this function tries to do as little work as
891
    possible, by finding out whether a intermediate table is needed to copy
892
    data into and when finishing the altering to use it as the original table.
893
    For this reason the function compare_tables() is called, which decides
894
    based on all kind of data how similar are the new and the original
895
    tables.
896
897
  RETURN VALUES
898
    false  OK
899
    true   Error
900
*/
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
901
902
static bool internal_alter_table(Session *session,
903
                                 Table *table,
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
904
                                 identifier::Table &original_table_identifier,
905
                                 identifier::Table &new_table_identifier,
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
906
                                 HA_CREATE_INFO *create_info,
1502.1.30 by Brian Aker
First pass on cleanup of Stewart's patch, plus re-engineer to make it work a
907
                                 const message::Table &original_proto,
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
908
                                 message::Table &create_proto,
2200.2.13 by Stewart Smith
make ALTER TABLE RENAME and DISABLE/ENABLE keys use the AlterTable protobuf message constructed in the parser, with asserts for the old AlterInfo structure matching
909
                                 message::AlterTable &alter_table_message,
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
910
                                 TableList *table_list,
911
                                 AlterInfo *alter_info,
912
                                 uint32_t order_num,
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
913
                                 Order *order,
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
914
                                 bool ignore)
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
915
{
916
  int error= 0;
917
  char tmp_name[80];
918
  char old_name[32];
919
  ha_rows copied= 0;
920
  ha_rows deleted= 0;
921
1992.4.1 by Brian Aker
Update code for testing identifiers/table/schema name correctness.
922
  if (not original_table_identifier.isValid())
923
    return true;
924
925
  if (not new_table_identifier.isValid())
926
    return true;
927
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
928
  session->set_proc_info("init");
929
930
  table->use_all_columns();
931
1395.1.4 by Brian Aker
Last of the goto are gone.
932
  plugin::StorageEngine *new_engine;
933
  plugin::StorageEngine *original_engine;
934
1574 by Brian Aker
Rollup patch for hiding tableshare.
935
  original_engine= table->getMutableShare()->getEngine();
1395.1.4 by Brian Aker
Last of the goto are gone.
936
937
  if (not create_info->db_type)
938
  {
939
    create_info->db_type= original_engine;
940
  }
941
  new_engine= create_info->db_type;
942
943
1395.1.9 by Brian Aker
Small cleanup around how we do types internally.
944
  create_proto.set_schema(new_table_identifier.getSchemaName());
945
  create_proto.set_type(new_table_identifier.getType());
1395.1.4 by Brian Aker
Last of the goto are gone.
946
947
  /**
2224.2.9 by Olaf van der Spek
Statement::set_command
948
    @todo Have a check on the table definition for FK in the future
1395.1.4 by Brian Aker
Last of the goto are gone.
949
    to remove the need for the cursor. (aka can_switch_engines())
950
  */
951
  if (new_engine != original_engine &&
952
      not table->cursor->can_switch_engines())
953
  {
954
    assert(0);
955
    my_error(ER_ROW_IS_REFERENCED, MYF(0));
956
957
    return true;
958
  }
959
960
  if (original_engine->check_flag(HTON_BIT_ALTER_NOT_SUPPORTED) ||
961
      new_engine->check_flag(HTON_BIT_ALTER_NOT_SUPPORTED))
962
  {
2096.1.4 by Brian Aker
Clean up errors and pass information in creation of statement.
963
    my_error(ER_ILLEGAL_HA, new_table_identifier);
1395.1.4 by Brian Aker
Last of the goto are gone.
964
965
    return true;
966
  }
967
968
  session->set_proc_info("setup");
969
2200.2.13 by Stewart Smith
make ALTER TABLE RENAME and DISABLE/ENABLE keys use the AlterTable protobuf message constructed in the parser, with asserts for the old AlterInfo structure matching
970
  /* First we try for operations that do not require a copying
971
     ALTER TABLE.
972
973
     In future there should be more operations, currently it's just a couple.
974
  */
975
976
  if ((alter_table_message.has_rename()
977
       || alter_table_message.has_alter_keys_onoff())
978
      && alter_table_message.operations_size() == 0)
1395.1.4 by Brian Aker
Last of the goto are gone.
979
  {
2200.2.13 by Stewart Smith
make ALTER TABLE RENAME and DISABLE/ENABLE keys use the AlterTable protobuf message constructed in the parser, with asserts for the old AlterInfo structure matching
980
    /*
981
     * test if no other bits except ALTER_RENAME and ALTER_KEYS_ONOFF are set
982
     */
1395.1.4 by Brian Aker
Last of the goto are gone.
983
    bitset<32> tmp;
984
985
    tmp.set();
986
    tmp.reset(ALTER_RENAME);
987
    tmp.reset(ALTER_KEYS_ONOFF);
988
    tmp&= alter_info->flags;
989
2200.2.13 by Stewart Smith
make ALTER TABLE RENAME and DISABLE/ENABLE keys use the AlterTable protobuf message constructed in the parser, with asserts for the old AlterInfo structure matching
990
    if((not (tmp.any()) && not table->getShare()->getType())) // no need to touch frm
1395.1.4 by Brian Aker
Last of the goto are gone.
991
    {
2200.2.13 by Stewart Smith
make ALTER TABLE RENAME and DISABLE/ENABLE keys use the AlterTable protobuf message constructed in the parser, with asserts for the old AlterInfo structure matching
992
      if (alter_table_message.has_alter_keys_onoff())
993
      {
994
        error= apply_online_alter_keys_onoff(session, table,
995
                                       alter_table_message.alter_keys_onoff());
996
997
        if (error == HA_ERR_WRONG_COMMAND)
998
        {
999
          error= EE_OK;
1000
          push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1001
                              ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
1002
                              table->getAlias());
1003
        }
1004
      }
1005
1006
      if (alter_table_message.has_rename())
1007
      {
1008
        error= apply_online_rename_table(session,
1009
                                         table,
1010
                                         original_engine,
1011
                                         original_table_identifier,
1012
                                         new_table_identifier,
1013
                                         alter_table_message.rename());
1014
1015
        if (error == HA_ERR_WRONG_COMMAND)
1016
        {
1017
          error= EE_OK;
1018
          push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1019
                              ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
1020
                              table->getAlias());
1021
        }
1395.1.4 by Brian Aker
Last of the goto are gone.
1022
      }
1023
2068.6.4 by Brian Aker
Merge in lock/error update for alter table.
1024
      if (not error)
1395.1.4 by Brian Aker
Last of the goto are gone.
1025
      {
2318.6.62 by Olaf van der Spek
Refactor
1026
        TransactionServices::allocateNewTransactionId();
1027
        TransactionServices::rawStatement(*session,
2211.2.1 by David Shrewsbury
Add raw_sql_schema member to Statement protobuf definition and set it in ALTER TABLE code path to current value associated with Session executing it.
1028
                                          *session->getQueryString(),
2224.2.9 by Olaf van der Spek
Statement::set_command
1029
                                          *session->schema());
1395.1.4 by Brian Aker
Last of the goto are gone.
1030
        session->my_ok();
1031
      }
2068.6.10 by Brian Aker
Scope an error in alter table.
1032
      else if (error > EE_OK) // If we have already set the error, we pass along -1
1395.1.4 by Brian Aker
Last of the goto are gone.
1033
      {
1034
        table->print_error(error, MYF(0));
1035
      }
1036
1037
      table_list->table= NULL;
1038
1039
      return error;
1372.1.1 by Brian Aker
Removed/rewrite to remove goto in alter table.
1040
    }
1395.1.4 by Brian Aker
Last of the goto are gone.
1041
  }
1042
2200.2.15 by Stewart Smith
remove enum ha_build_method and instead use the build method in message::AlterTable.
1043
  if (alter_table_message.build_method() == message::AlterTable::BUILD_ONLINE)
2200.2.14 by Stewart Smith
throw an error if ONLINE ALTER TABLE is asked for that we cannot do without a copying alter table.
1044
  {
1045
    my_error(*session->getQueryString(), ER_NOT_SUPPORTED_YET);
1046
    return true;
1047
  }
1048
1395.1.4 by Brian Aker
Last of the goto are gone.
1049
  /* We have to do full alter table. */
1050
  new_engine= create_info->db_type;
1051
2200.2.15 by Stewart Smith
remove enum ha_build_method and instead use the build method in message::AlterTable.
1052
  if (prepare_alter_table(session, table, create_info, original_proto, create_proto, alter_table_message, alter_info))
1395.1.4 by Brian Aker
Last of the goto are gone.
1053
  {
1054
    return true;
1055
  }
1056
1057
  set_table_default_charset(create_info, new_table_identifier.getSchemaName().c_str());
1058
1059
  snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%"PRIx64, TMP_FILE_PREFIX, (unsigned long) current_pid, session->thread_id);
1060
1061
  /* Create a temporary table with the new format */
1062
  /**
1063
    @note we make an internal temporary table unless the table is a temporary table. In last
1064
    case we just use it as is. Neither of these tables require locks in order to  be
1065
    filled.
1066
  */
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
1067
  identifier::Table new_table_as_temporary(original_table_identifier.getSchemaName(),
1395.1.4 by Brian Aker
Last of the goto are gone.
1068
                                         tmp_name,
1395.1.7 by Brian Aker
Removed the internal table type in favor of the protobuf one.
1069
                                         create_proto.type() != message::Table::TEMPORARY ? message::Table::INTERNAL :
1070
                                         message::Table::TEMPORARY);
1395.1.4 by Brian Aker
Last of the goto are gone.
1071
2068.6.3 by Brian Aker
Remove useless function.
1072
  /*
1073
    Create a table with a temporary name.
1074
    We don't log the statement, it will be logged later.
1075
  */
1076
  create_proto.set_name(new_table_as_temporary.getTableName());
1077
  create_proto.mutable_engine()->set_name(create_info->db_type->getName());
1078
1079
  error= create_table(session,
1080
                      new_table_as_temporary,
1081
                      create_info, create_proto, alter_info, true, 0, false);
1395.1.4 by Brian Aker
Last of the goto are gone.
1082
1083
  if (error != 0)
1084
  {
1085
    return true;
1086
  }
1087
1088
  /* Open the table so we need to copy the data to it. */
1669.2.4 by Brian Aker
Fix temp tables to use new over malloc.
1089
  Table *new_table= open_alter_table(session, table, new_table_as_temporary);
1090
1395.1.4 by Brian Aker
Last of the goto are gone.
1091
1092
  if (not new_table)
1093
  {
1954.2.4 by Brian Aker
Remove quick_rm_table().
1094
    plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
1395.1.4 by Brian Aker
Last of the goto are gone.
1095
    return true;
1096
  }
1097
1098
  /* Copy the data if necessary. */
1099
  {
1633.4.8 by Brian Aker
Update for count_cuted_fields.
1100
    /* We must not ignore bad input! */
1101
    session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;	// calc cuted fields
1372.1.1 by Brian Aker
Removed/rewrite to remove goto in alter table.
1102
    session->cuted_fields= 0L;
1103
    session->set_proc_info("copy to tmp table");
1104
    copied= deleted= 0;
1105
1106
    /* We don't want update TIMESTAMP fields during ALTER Table. */
1107
    new_table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
1108
    new_table->next_number_field= new_table->found_next_number_field;
1578.4.11 by Brian Aker
PAss through the code removing current_session
1109
    error= copy_data_between_tables(session,
1110
                                    table,
1372.1.1 by Brian Aker
Removed/rewrite to remove goto in alter table.
1111
                                    new_table,
1112
                                    alter_info->create_list,
1113
                                    ignore,
1114
                                    order_num,
1115
                                    order,
1116
                                    &copied,
1117
                                    &deleted,
2200.2.16 by Stewart Smith
remove enum_enable_or_disable which was only used for alter_info.keys_onoff. We can just use the message::AlterTable now
1118
                                    alter_table_message,
1372.1.1 by Brian Aker
Removed/rewrite to remove goto in alter table.
1119
                                    alter_info->error_if_not_empty);
1120
1121
    /* We must not ignore bad input! */
1637.1.1 by Brian Aker
Add in assert for detecting changes during alter.
1122
    assert(session->count_cuted_fields == CHECK_FIELD_ERROR_FOR_NULL);
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1123
  }
1124
1395.1.4 by Brian Aker
Last of the goto are gone.
1125
  /* Now we need to resolve what just happened with the data copy. */
1126
1127
  if (error)
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1128
  {
1376 by Brian Aker
Fix for goto that could be removed.
1129
1395.1.4 by Brian Aker
Last of the goto are gone.
1130
    /*
2068.6.8 by Brian Aker
Add in code for additional locking.
1131
      No default value was provided for new fields.
1395.1.4 by Brian Aker
Last of the goto are gone.
1132
    */
1133
    if (alter_info->error_if_not_empty && session->row_count)
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1134
    {
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
1135
      my_error(ER_INVALID_ALTER_TABLE_FOR_NOT_NULL, MYF(0));
1395.1.4 by Brian Aker
Last of the goto are gone.
1136
    }
1137
1138
    if (original_table_identifier.isTmp())
1139
    {
1140
      if (new_table)
1141
      {
1142
        /* close_temporary_table() frees the new_table pointer. */
2263.3.9 by Olaf van der Spek
Open Tables
1143
        session->open_tables.close_temporary_table(new_table);
1395.1.4 by Brian Aker
Last of the goto are gone.
1144
      }
1145
      else
1146
      {
1954.2.4 by Brian Aker
Remove quick_rm_table().
1147
        plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
1395.1.4 by Brian Aker
Last of the goto are gone.
1148
      }
1149
1150
      return true;
1151
    }
1152
    else
1153
    {
1154
      if (new_table)
1155
      {
1156
        /*
1157
          Close the intermediate table that will be the new table.
1158
          Note that MERGE tables do not have their children attached here.
1159
        */
1160
        new_table->intern_close_table();
1574 by Brian Aker
Rollup patch for hiding tableshare.
1161
        if (new_table->hasShare())
1502.1.13 by Brian Aker
Next bit of TableShare to c++.
1162
        {
1827.2.1 by Brian Aker
Encapsulate Table (remove its direct access of the share it contains)
1163
          delete new_table->getMutableShare();
1502.1.13 by Brian Aker
Next bit of TableShare to c++.
1164
        }
1165
1669.2.4 by Brian Aker
Fix temp tables to use new over malloc.
1166
        delete new_table;
1395.1.4 by Brian Aker
Last of the goto are gone.
1167
      }
1168
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
1169
      boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
1395.1.4 by Brian Aker
Last of the goto are gone.
1170
1954.2.4 by Brian Aker
Remove quick_rm_table().
1171
      plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
1395.1.4 by Brian Aker
Last of the goto are gone.
1172
1173
      return true;
1174
    }
1175
  }
1176
  // Temporary table and success
1177
  else if (original_table_identifier.isTmp())
1178
  {
1179
    /* Close lock if this is a transactional table */
2263.3.3 by Olaf van der Spek
Use open_tables
1180
    if (session->open_tables.lock)
1395.1.4 by Brian Aker
Last of the goto are gone.
1181
    {
2263.3.3 by Olaf van der Spek
Use open_tables
1182
      session->unlockTables(session->open_tables.lock);
1183
      session->open_tables.lock= 0;
1395.1.4 by Brian Aker
Last of the goto are gone.
1184
    }
1185
1186
    /* Remove link to old table and rename the new one */
2263.3.9 by Olaf van der Spek
Open Tables
1187
    session->open_tables.close_temporary_table(table);
1395.1.4 by Brian Aker
Last of the goto are gone.
1188
1189
    /* Should pass the 'new_name' as we store table name in the cache */
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
1190
    new_table->getMutableShare()->setIdentifier(new_table_identifier);
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
1191
1192
    new_table_identifier.setPath(new_table_as_temporary.getPath());
1193
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
1194
    if (rename_table(*session, new_engine, new_table_as_temporary, new_table_identifier) != 0)
1395.1.14 by Brian Aker
Fix for Innodb dfe files.
1195
    {
1196
      return true;
1197
    }
1395.1.4 by Brian Aker
Last of the goto are gone.
1198
  }
1199
  // Normal table success
1200
  else
1201
  {
1202
    if (new_table)
1203
    {
1204
      /*
1205
        Close the intermediate table that will be the new table.
1206
        Note that MERGE tables do not have their children attached here.
1207
      */
1208
      new_table->intern_close_table();
1502.1.12 by Brian Aker
Second pass through TableShare to new(). Partial hack in using bool for the
1209
1574 by Brian Aker
Rollup patch for hiding tableshare.
1210
      if (new_table->hasShare())
1502.1.13 by Brian Aker
Next bit of TableShare to c++.
1211
      {
1827.2.1 by Brian Aker
Encapsulate Table (remove its direct access of the share it contains)
1212
        delete new_table->getMutableShare();
1502.1.12 by Brian Aker
Second pass through TableShare to new(). Partial hack in using bool for the
1213
      }
1214
1669.2.4 by Brian Aker
Fix temp tables to use new over malloc.
1215
      delete new_table;
1395.1.4 by Brian Aker
Last of the goto are gone.
1216
    }
1217
2068.6.9 by Brian Aker
Fix scoping on lock.
1218
    {
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
1219
      boost::mutex::scoped_lock scopedLock(table::Cache::mutex()); /* ALTER TABLE */
2068.6.9 by Brian Aker
Fix scoping on lock.
1220
      /*
1221
        Data is copied. Now we:
1222
        1) Wait until all other threads close old version of table.
1223
        2) Close instances of table open by this thread and replace them
1224
        with exclusive name-locks.
1225
        3) Rename the old table to a temp name, rename the new one to the
1226
        old name.
1227
        4) If we are under LOCK TABLES and don't do ALTER Table ... RENAME
1228
        we reopen new version of table.
1229
        5) Write statement to the binary log.
1230
        6) If we are under LOCK TABLES and do ALTER Table ... RENAME we
1231
        remove name-locks from list of open tables and table cache.
1232
        7) If we are not not under LOCK TABLES we rely on close_thread_tables()
1233
        call to remove name-locks from table cache and list of open table.
1234
      */
1235
1236
      session->set_proc_info("rename result table");
1237
1238
      snprintf(old_name, sizeof(old_name), "%s2-%lx-%"PRIx64, TMP_FILE_PREFIX, (unsigned long) current_pid, session->thread_id);
1239
1240
      my_casedn_str(files_charset_info, old_name);
1241
1242
      wait_while_table_is_used(session, table, HA_EXTRA_PREPARE_FOR_RENAME);
1243
      session->close_data_files_and_morph_locks(original_table_identifier);
1244
2068.6.10 by Brian Aker
Scope an error in alter table.
1245
      assert(not error);
2068.6.9 by Brian Aker
Fix scoping on lock.
1246
1247
      /*
1248
        This leads to the storage engine (SE) not being notified for renames in
1249
        rename_table(), because we just juggle with the FRM and nothing
1250
        more. If we have an intermediate table, then we notify the SE that
1251
        it should become the actual table. Later, we will recycle the old table.
1252
        However, in case of ALTER Table RENAME there might be no intermediate
1253
        table. This is when the old and new tables are compatible, according to
1254
        compare_table(). Then, we need one additional call to
1255
      */
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
1256
      identifier::Table original_table_to_drop(original_table_identifier.getSchemaName(),
2068.6.9 by Brian Aker
Fix scoping on lock.
1257
                                             old_name, create_proto.type() != message::Table::TEMPORARY ? message::Table::INTERNAL :
1258
                                             message::Table::TEMPORARY);
1259
2068.6.10 by Brian Aker
Scope an error in alter table.
1260
      drizzled::error_t rename_error= EE_OK;
2068.6.9 by Brian Aker
Fix scoping on lock.
1261
      if (rename_table(*session, original_engine, original_table_identifier, original_table_to_drop))
1395.1.4 by Brian Aker
Last of the goto are gone.
1262
      {
2068.6.10 by Brian Aker
Scope an error in alter table.
1263
        error= ER_ERROR_ON_RENAME;
1954.2.4 by Brian Aker
Remove quick_rm_table().
1264
        plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
1395.1.4 by Brian Aker
Last of the goto are gone.
1265
      }
1266
      else
1267
      {
2068.6.9 by Brian Aker
Fix scoping on lock.
1268
        if (rename_table(*session, new_engine, new_table_as_temporary, new_table_identifier) != 0)
1269
        {
1270
          /* Try to get everything back. */
2068.6.10 by Brian Aker
Scope an error in alter table.
1271
          rename_error= ER_ERROR_ON_RENAME;
2068.6.9 by Brian Aker
Fix scoping on lock.
1272
1273
          plugin::StorageEngine::dropTable(*session, new_table_identifier);
1274
1275
          plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
1276
1277
          rename_table(*session, original_engine, original_table_to_drop, original_table_identifier);
1278
        }
1279
        else
1280
        {
1281
          plugin::StorageEngine::dropTable(*session, original_table_to_drop);
1282
        }
1283
      }
1284
2068.6.10 by Brian Aker
Scope an error in alter table.
1285
      if (rename_error)
2068.6.9 by Brian Aker
Fix scoping on lock.
1286
      {
1287
        /*
1288
          An error happened while we were holding exclusive name-lock on table
1289
          being altered. To be safe under LOCK TABLES we should remove placeholders
1290
          from list of open tables list and table cache.
1291
        */
1292
        session->unlink_open_table(table);
1293
1294
        return true;
1295
      }
1296
    }
1395.1.4 by Brian Aker
Last of the goto are gone.
1297
1298
    session->set_proc_info("end");
1299
2318.6.62 by Olaf van der Spek
Refactor
1300
    TransactionServices::rawStatement(*session,
2211.2.1 by David Shrewsbury
Add raw_sql_schema member to Statement protobuf definition and set it in ALTER TABLE code path to current value associated with Session executing it.
1301
                                      *session->getQueryString(),
2224.2.9 by Olaf van der Spek
Statement::set_command
1302
                                      *session->schema());
1395.1.4 by Brian Aker
Last of the goto are gone.
1303
    table_list->table= NULL;
1304
  }
1305
1306
  /*
2224.2.9 by Olaf van der Spek
Statement::set_command
1307
   * Field::store() may have called my_error().  If this is
1308
   * the case, we must not send an ok packet, since
1395.1.4 by Brian Aker
Last of the goto are gone.
1309
   * Diagnostics_area::is_set() will fail an assert.
1310
 */
1311
  if (session->is_error())
1312
  {
1313
    /* my_error() was called.  Return true (which means error...) */
1314
    return true;
1315
  }
1316
1317
  snprintf(tmp_name, sizeof(tmp_name), ER(ER_INSERT_INFO),
1318
           (ulong) (copied + deleted), (ulong) deleted,
1319
           (ulong) session->cuted_fields);
1320
  session->my_ok(copied + deleted, 0, 0L, tmp_name);
1321
  return false;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1322
}
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
1323
2200.2.13 by Stewart Smith
make ALTER TABLE RENAME and DISABLE/ENABLE keys use the AlterTable protobuf message constructed in the parser, with asserts for the old AlterInfo structure matching
1324
static int apply_online_alter_keys_onoff(Session *session,
1325
                                         Table* table,
1326
                                         const message::AlterTable::AlterKeysOnOff &op)
1327
{
1328
  int error= 0;
1329
1330
  if (op.enable())
1331
  {
1332
    /*
1333
      wait_while_table_is_used() ensures that table being altered is
1334
      opened only by this thread and that Table::TableShare::version
1335
      of Table object corresponding to this table is 0.
1336
      The latter guarantees that no DML statement will open this table
1337
      until ALTER Table finishes (i.e. until close_thread_tables())
1338
      while the fact that the table is still open gives us protection
1339
      from concurrent DDL statements.
1340
    */
1341
    {
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
1342
      boost::mutex::scoped_lock scopedLock(table::Cache::mutex()); /* DDL wait for/blocker */
2200.2.13 by Stewart Smith
make ALTER TABLE RENAME and DISABLE/ENABLE keys use the AlterTable protobuf message constructed in the parser, with asserts for the old AlterInfo structure matching
1343
      wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
1344
    }
1345
    error= table->cursor->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
1346
1347
    /* COND_refresh will be signaled in close_thread_tables() */
1348
  }
1349
  else
1350
  {
1351
    {
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
1352
      boost::mutex::scoped_lock scopedLock(table::Cache::mutex()); /* DDL wait for/blocker */
2200.2.13 by Stewart Smith
make ALTER TABLE RENAME and DISABLE/ENABLE keys use the AlterTable protobuf message constructed in the parser, with asserts for the old AlterInfo structure matching
1353
      wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
1354
    }
1355
    error= table->cursor->ha_disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
1356
1357
    /* COND_refresh will be signaled in close_thread_tables() */
1358
  }
1359
1360
  return error;
1361
}
1362
1363
static int apply_online_rename_table(Session *session,
1364
                                     Table *table,
1365
                                     plugin::StorageEngine *original_engine,
1366
                                     identifier::Table &original_table_identifier,
1367
                                     identifier::Table &new_table_identifier,
1368
                                     const message::AlterTable::RenameTable &alter_operation)
1369
{
1370
  int error= 0;
1371
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
1372
  boost::mutex::scoped_lock scopedLock(table::Cache::mutex()); /* Lock to remove all instances of table from table cache before ALTER */
2200.2.13 by Stewart Smith
make ALTER TABLE RENAME and DISABLE/ENABLE keys use the AlterTable protobuf message constructed in the parser, with asserts for the old AlterInfo structure matching
1373
  /*
1374
    Unlike to the above case close_cached_table() below will remove ALL
1375
    instances of Table from table cache (it will also remove table lock
1376
    held by this thread). So to make actual table renaming and writing
1377
    to binlog atomic we have to put them into the same critical section
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
1378
    protected by table::Cache::mutex() mutex. This also removes gap for races between
2200.2.13 by Stewart Smith
make ALTER TABLE RENAME and DISABLE/ENABLE keys use the AlterTable protobuf message constructed in the parser, with asserts for the old AlterInfo structure matching
1379
    access() and rename_table() calls.
1380
  */
1381
1382
  if (not (original_table_identifier == new_table_identifier))
1383
  {
1384
    assert(alter_operation.to_name() == new_table_identifier.getTableName());
1385
    assert(alter_operation.to_schema() == new_table_identifier.getSchemaName());
1386
1387
    session->set_proc_info("rename");
1388
    /*
1389
      Then do a 'simple' rename of the table. First we need to close all
1390
      instances of 'source' table.
1391
    */
1392
    session->close_cached_table(table);
1393
    /*
1394
      Then, we want check once again that target table does not exist.
1395
      Actually the order of these two steps does not matter since
1396
      earlier we took name-lock on the target table, so we do them
1397
      in this particular order only to be consistent with 5.0, in which
1398
      we don't take this name-lock and where this order really matters.
1399
      @todo Investigate if we need this access() check at all.
1400
    */
1401
    if (plugin::StorageEngine::doesTableExist(*session, new_table_identifier))
1402
    {
1403
      my_error(ER_TABLE_EXISTS_ERROR, new_table_identifier);
1404
      error= -1;
1405
    }
1406
    else
1407
    {
1408
      if (rename_table(*session, original_engine, original_table_identifier, new_table_identifier))
1409
      {
1410
        error= -1;
1411
      }
1412
    }
1413
  }
1414
  return error;
1415
}
1416
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
1417
bool alter_table(Session *session,
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
1418
                 identifier::Table &original_table_identifier,
1419
                 identifier::Table &new_table_identifier,
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
1420
                 HA_CREATE_INFO *create_info,
1502.1.30 by Brian Aker
First pass on cleanup of Stewart's patch, plus re-engineer to make it work a
1421
                 const message::Table &original_proto,
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
1422
                 message::Table &create_proto,
1423
                 TableList *table_list,
1424
                 AlterInfo *alter_info,
1425
                 uint32_t order_num,
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
1426
                 Order *order,
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
1427
                 bool ignore)
1428
{
1429
  bool error;
1430
  Table *table;
2200.2.25 by Stewart Smith
Merge refactoring of default values work
1431
  message::AlterTable *alter_table_message= session->lex().alter_table();
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
1432
2200.2.13 by Stewart Smith
make ALTER TABLE RENAME and DISABLE/ENABLE keys use the AlterTable protobuf message constructed in the parser, with asserts for the old AlterInfo structure matching
1433
  alter_table_message->set_catalog(original_table_identifier.getCatalogName());
1434
  alter_table_message->set_schema(original_table_identifier.getSchemaName());
1435
  alter_table_message->set_name(original_table_identifier.getTableName());
1436
2200.2.6 by Stewart Smith
switch DISCARD/IMPORT TABLESPACE to only use the alter table protobuf message
1437
  if (alter_table_message->operations_size()
1438
      && (alter_table_message->operations(0).operation()
1439
          == message::AlterTable::AlterTableOperation::DISCARD_TABLESPACE
1440
          || alter_table_message->operations(0).operation()
1441
          == message::AlterTable::AlterTableOperation::IMPORT_TABLESPACE))
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
1442
  {
2200.2.6 by Stewart Smith
switch DISCARD/IMPORT TABLESPACE to only use the alter table protobuf message
1443
    bool discard= (alter_table_message->operations(0).operation() ==
1444
                   message::AlterTable::AlterTableOperation::DISCARD_TABLESPACE);
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
1445
    /* DISCARD/IMPORT TABLESPACE is always alone in an ALTER Table */
2200.2.6 by Stewart Smith
switch DISCARD/IMPORT TABLESPACE to only use the alter table protobuf message
1446
    return discard_or_import_tablespace(session, table_list, discard);
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
1447
  }
1448
1449
  session->set_proc_info("init");
1450
1451
  if (not (table= session->openTableLock(table_list, TL_WRITE_ALLOW_READ)))
1452
    return true;
1453
1454
  session->set_proc_info("gained write lock on table");
1455
2224.2.9 by Olaf van der Spek
Statement::set_command
1456
  /*
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
1457
    Check that we are not trying to rename to an existing table,
1458
    if one existed we get a lock, if we can't we error.
1459
  */
1460
  {
1461
    Table *name_lock= NULL;
1462
1463
    if (not lockTableIfDifferent(*session, original_table_identifier, new_table_identifier, name_lock))
1464
    {
1465
      return true;
1466
    }
1467
1468
    error= internal_alter_table(session,
1469
                                table,
1470
                                original_table_identifier,
1471
                                new_table_identifier,
1472
                                create_info,
1502.1.30 by Brian Aker
First pass on cleanup of Stewart's patch, plus re-engineer to make it work a
1473
                                original_proto,
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
1474
                                create_proto,
2200.2.13 by Stewart Smith
make ALTER TABLE RENAME and DISABLE/ENABLE keys use the AlterTable protobuf message constructed in the parser, with asserts for the old AlterInfo structure matching
1475
                                *alter_table_message,
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
1476
                                table_list,
1477
                                alter_info,
1478
                                order_num,
1479
                                order,
1480
                                ignore);
1481
1482
    if (name_lock)
1483
    {
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
1484
      boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
1395.1.3 by Brian Aker
Pass through on refactoring, locks now are wrapped up in layer above.
1485
      session->unlink_open_table(name_lock);
1486
    }
1487
  }
1488
1489
  return error;
1490
}
1130.3.13 by Monty Taylor
Finished cleaning namespaces in drizzled/statement
1491
/* alter_table */
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1492
1493
static int
1578.4.11 by Brian Aker
PAss through the code removing current_session
1494
copy_data_between_tables(Session *session,
1495
                         Table *from, Table *to,
1216.1.1 by Brian Aker
Move print_error up to Engine.
1496
                         List<CreateField> &create,
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1497
                         bool ignore,
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
1498
                         uint32_t order_num, Order *order,
1216.1.1 by Brian Aker
Move print_error up to Engine.
1499
                         ha_rows *copied,
1500
                         ha_rows *deleted,
2200.2.16 by Stewart Smith
remove enum_enable_or_disable which was only used for alter_info.keys_onoff. We can just use the message::AlterTable now
1501
                         message::AlterTable &alter_table_message,
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1502
                         bool error_if_not_empty)
1503
{
1273.1.3 by Jay Pipes
Removes pointless ha_enable_transaction call. This wasn't used anywhere to any effect.
1504
  int error= 0;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1505
  CopyField *copy,*copy_end;
1506
  ulong found_count,delete_count;
1507
  uint32_t length= 0;
1711.6.1 by Brian Aker
Style on structure cleanup
1508
  SortField *sortorder;
1538 by Brian Aker
Code shuffle on ReadRecord
1509
  ReadRecord info;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1510
  TableList   tables;
1511
  List<Item>   fields;
1512
  List<Item>   all_fields;
1513
  ha_rows examined_rows;
1514
  bool auto_increment_field_copied= 0;
1515
  uint64_t prev_insert_id;
1516
1517
  /*
1518
    Turn off recovery logging since rollback of an alter table is to
1519
    delete the new table so there is no need to log the changes to it.
1520
1521
    This needs to be done before external_lock
1522
  */
1523
2224.2.9 by Olaf van der Spek
Statement::set_command
1524
  /*
1525
   * LP Bug #552420
1510.4.2 by Jay Pipes
Fixes Bug #552420. Because the copied-to table in ALTER TABLE is temporary, mysql_lock_tables() is not called, and the automatic StorageEngine::startStatement() is also not called. So, we call it manually to ensure transactional engines have a notification of a new statement/transaction.
1526
   *
1910.2.7 by Brian Aker
Rename lock methods to be style + well make sense.
1527
   * Since open_temporary_table() doesn't invoke lockTables(), we
1510.4.2 by Jay Pipes
Fixes Bug #552420. Because the copied-to table in ALTER TABLE is temporary, mysql_lock_tables() is not called, and the automatic StorageEngine::startStatement() is also not called. So, we call it manually to ensure transactional engines have a notification of a new statement/transaction.
1528
   * don't get the usual automatic call to StorageEngine::startStatement(), so
1529
   * we manually call it here...
1530
   */
1827.2.1 by Brian Aker
Encapsulate Table (remove its direct access of the share it contains)
1531
  to->getMutableShare()->getEngine()->startStatement(session);
1510.4.2 by Jay Pipes
Fixes Bug #552420. Because the copied-to table in ALTER TABLE is temporary, mysql_lock_tables() is not called, and the automatic StorageEngine::startStatement() is also not called. So, we call it manually to ensure transactional engines have a notification of a new statement/transaction.
1532
2318.6.62 by Olaf van der Spek
Refactor
1533
  copy= new CopyField[to->getShare()->sizeFields()];
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1534
1208.3.2 by brian
Update for Cursor renaming.
1535
  if (to->cursor->ha_external_lock(session, F_WRLCK))
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1536
    return -1;
1537
1538
  /* We need external lock before we can disable/enable keys */
2200.2.16 by Stewart Smith
remove enum_enable_or_disable which was only used for alter_info.keys_onoff. We can just use the message::AlterTable now
1539
  alter_table_manage_keys(session, to, from->cursor->indexes_are_disabled(),
1540
                          alter_table_message);
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1541
1542
  /* We can abort alter table for any table type */
2114.5.1 by Brian Aker
Additional abstract around time (this also makes the abort_on_warnings in
1543
  session->setAbortOnWarning(not ignore);
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1544
1208.3.2 by brian
Update for Cursor renaming.
1545
  from->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
1546
  to->cursor->ha_start_bulk_insert(from->cursor->stats.records);
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1547
2183.2.13 by Olaf van der Spek
Use List::begin()
1548
  List<CreateField>::iterator it(create.begin());
2046.2.3 by Brian Aker
Add basic tests for microtime.
1549
  copy_end= copy;
1578.2.16 by Brian Aker
Merge in change to getTable() to private the field objects.
1550
  for (Field **ptr= to->getFields(); *ptr ; ptr++)
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1551
  {
2179.7.10 by Olaf van der Spek
Refactor
1552
    CreateField* def=it++;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1553
    if (def->field)
1554
    {
1555
      if (*ptr == to->next_number_field)
1556
        auto_increment_field_copied= true;
1557
1558
      (copy_end++)->set(*ptr,def->field,0);
1559
    }
1560
1561
  }
1562
1563
  found_count=delete_count=0;
1564
2068.6.12 by Brian Aker
Remove goto from alter table logic
1565
  do
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1566
  {
2068.6.12 by Brian Aker
Remove goto from alter table logic
1567
    if (order)
1568
    {
1569
      if (to->getShare()->hasPrimaryKey() && to->cursor->primary_key_is_clustered())
1395.1.4 by Brian Aker
Last of the goto are gone.
1570
      {
2068.6.12 by Brian Aker
Remove goto from alter table logic
1571
        char warn_buff[DRIZZLE_ERRMSG_SIZE];
1572
        snprintf(warn_buff, sizeof(warn_buff),
1573
                 _("order_st BY ignored because there is a user-defined clustered "
1574
                   "index in the table '%-.192s'"),
1575
                 from->getMutableShare()->getTableName());
1576
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
1577
                     warn_buff);
1395.1.4 by Brian Aker
Last of the goto are gone.
1578
      }
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1579
      else
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
1580
      {
2068.6.12 by Brian Aker
Remove goto from alter table logic
1581
        FileSort filesort(*session);
2296.1.1 by Mark Atwood
Merge in Fixes of Brian's IOCACHE.
1582
        from->sort.io_cache= new internal::io_cache_st;
2068.6.12 by Brian Aker
Remove goto from alter table logic
1583
1584
        tables.table= from;
2385.1.9 by Olaf van der Spek
Use const char* instead of str_ref
1585
        tables.setTableName(from->getMutableShare()->getTableName());
2151.2.10 by Olaf van der Spek
Const fixes
1586
        tables.alias= tables.getTableName();
2385.1.9 by Olaf van der Spek
Use const char* instead of str_ref
1587
        tables.setSchemaName(from->getMutableShare()->getSchemaName());
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
1588
        error= 1;
2068.6.12 by Brian Aker
Remove goto from alter table logic
1589
2318.6.31 by Olaf van der Spek
Refactor
1590
        session->lex().select_lex.setup_ref_array(session, order_num);
2318.6.36 by Olaf van der Spek
Refactor
1591
        if (setup_order(session, session->lex().select_lex.ref_pointer_array, &tables, fields, all_fields, order))
1592
          break;
1593
        sortorder= make_unireg_sortorder(order, &length, NULL);
1594
        if ((from->sort.found_records= filesort.run(from, sortorder, length, (optimizer::SqlSelect *) 0, HA_POS_ERROR, 1, examined_rows)) == HA_POS_ERROR)
1595
          break;
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
1596
      }
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1597
    }
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
1598
2068.6.12 by Brian Aker
Remove goto from alter table logic
1599
    /* Tell handler that we have values for all columns in the to table */
1600
    to->use_all_columns();
1601
2318.8.6 by Olaf van der Spek
Add const
1602
    error= info.init_read_record(session, from, NULL, 1, true);
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
1603
    if (error)
1604
    {
2068.6.12 by Brian Aker
Remove goto from alter table logic
1605
      to->print_error(errno, MYF(0));
1606
1976.6.1 by Brian Aker
This is a fix for bug lp:686197
1607
      break;
1608
    }
1609
2068.6.12 by Brian Aker
Remove goto from alter table logic
1610
    if (ignore)
1611
    {
1612
      to->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1613
    }
1614
1615
    session->row_count= 0;
1616
    to->restoreRecordAsDefault();        // Create empty record
1617
    while (not (error=info.read_record(&info)))
1618
    {
1619
      if (session->getKilled())
1620
      {
1621
        session->send_kill_message();
1622
        error= 1;
1623
        break;
1624
      }
1625
      session->row_count++;
1626
      /* Return error if source table isn't empty. */
1627
      if (error_if_not_empty)
1628
      {
1629
        error= 1;
1630
        break;
1631
      }
1632
      if (to->next_number_field)
1633
      {
1634
        if (auto_increment_field_copied)
1635
          to->auto_increment_field_not_null= true;
1636
        else
1637
          to->next_number_field->reset();
1638
      }
1639
1640
      for (CopyField *copy_ptr= copy; copy_ptr != copy_end ; copy_ptr++)
1641
      {
1642
        if (not copy->to_field->hasDefault() and copy->from_null_ptr and  *copy->from_null_ptr & copy->from_bit)
1643
        {
1644
          copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
1645
                                      ER_WARN_DATA_TRUNCATED, 1);
1646
          copy->to_field->reset();
1647
          error= 1;
1648
          break;
1649
        }
1650
1651
        copy_ptr->do_copy(copy_ptr);
1652
      }
1653
1654
      if (error)
1655
      {
1656
        break;
1657
      }
1658
1659
      prev_insert_id= to->cursor->next_insert_id;
1660
      error= to->cursor->insertRecord(to->record[0]);
1661
      to->auto_increment_field_not_null= false;
1662
1663
      if (error)
2224.2.9 by Olaf van der Spek
Statement::set_command
1664
      {
2068.6.12 by Brian Aker
Remove goto from alter table logic
1665
        if (!ignore || to->cursor->is_fatal_error(error, HA_CHECK_DUP))
2224.2.9 by Olaf van der Spek
Statement::set_command
1666
        {
2068.6.12 by Brian Aker
Remove goto from alter table logic
1667
          to->print_error(error, MYF(0));
1668
          break;
1669
        }
1670
        to->cursor->restore_auto_increment(prev_insert_id);
1671
        delete_count++;
1672
      }
1673
      else
1674
      {
1675
        found_count++;
1676
      }
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1677
    }
2068.6.12 by Brian Aker
Remove goto from alter table logic
1678
1679
    info.end_read_record();
1680
    from->free_io_cache();
2385.1.10 by Olaf van der Spek
Update .yy
1681
    delete[] copy;
2068.6.12 by Brian Aker
Remove goto from alter table logic
1682
1683
    if (to->cursor->ha_end_bulk_insert() && error <= 0)
1552.1.3 by Brian Aker
Fixes the assertion bug on handling of auto increment (sort of worthless,
1684
    {
2068.6.12 by Brian Aker
Remove goto from alter table logic
1685
      to->print_error(errno, MYF(0));
1686
      error= 1;
1552.1.3 by Brian Aker
Fixes the assertion bug on handling of auto increment (sort of worthless,
1687
    }
2068.6.12 by Brian Aker
Remove goto from alter table logic
1688
    to->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1689
1690
    /*
1691
      Ensure that the new table is saved properly to disk so that we
1692
      can do a rename
1693
    */
2318.6.62 by Olaf van der Spek
Refactor
1694
    if (TransactionServices::autocommitOrRollback(*session, false))
2068.6.12 by Brian Aker
Remove goto from alter table logic
1695
      error= 1;
1696
1697
    if (not session->endActiveTransaction())
1698
      error= 1;
1699
1700
  } while (0);
1701
2114.5.1 by Brian Aker
Additional abstract around time (this also makes the abort_on_warnings in
1702
  session->setAbortOnWarning(false);
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1703
  from->free_io_cache();
1704
  *copied= found_count;
1705
  *deleted=delete_count;
1208.3.2 by brian
Update for Cursor renaming.
1706
  to->cursor->ha_release_auto_increment();
2068.6.12 by Brian Aker
Remove goto from alter table logic
1707
1708
  if (to->cursor->ha_external_lock(session, F_UNLCK))
1709
  {
2385.1.5 by Olaf van der Spek
Refactor
1710
    error= 1;
2068.6.12 by Brian Aker
Remove goto from alter table logic
1711
  }
1552.1.3 by Brian Aker
Fixes the assertion bug on handling of auto increment (sort of worthless,
1712
2385.1.4 by Olaf van der Spek
Refactor setTableName
1713
  return error > 0 ? -1 : 0;
1126.3.2 by Jay Pipes
Simply moves alter_table specific stuff out of the sql_table.cc grab-bag and into drizzled/statement/alter_table.cc. Next step is to cleanup the code.
1714
}
1715
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
1716
static Table *open_alter_table(Session *session, Table *table, identifier::Table &identifier)
1223.4.13 by Brian Aker
Small shift in alter table (break the baby down...).
1717
{
1718
  /* Open the table so we need to copy the data to it. */
1608.2.3 by Brian Aker
Encapsulate type for TableShare.
1719
  if (table->getShare()->getType())
1223.4.13 by Brian Aker
Small shift in alter table (break the baby down...).
1720
  {
1721
    TableList tbl;
2385.1.9 by Olaf van der Spek
Use const char* instead of str_ref
1722
    tbl.setSchemaName(identifier.getSchemaName().c_str());
2318.8.5 by Olaf van der Spek
Refactor setSchemaName() and setTableName()
1723
    tbl.alias= identifier.getTableName().c_str();
2385.1.9 by Olaf van der Spek
Use const char* instead of str_ref
1724
    tbl.setTableName(identifier.getTableName().c_str());
1223.4.13 by Brian Aker
Small shift in alter table (break the baby down...).
1725
1726
    /* Table is in session->temporary_tables */
2318.8.5 by Olaf van der Spek
Refactor setSchemaName() and setTableName()
1727
    return session->openTable(&tbl, NULL, DRIZZLE_LOCK_IGNORE_FLUSH);
1223.4.13 by Brian Aker
Small shift in alter table (break the baby down...).
1728
  }
1729
  else
1730
  {
1731
    /* Open our intermediate table */
2179.7.10 by Olaf van der Spek
Refactor
1732
    return session->open_temporary_table(identifier, false);
1223.4.13 by Brian Aker
Small shift in alter table (break the baby down...).
1733
  }
1734
}
1735
1130.3.13 by Monty Taylor
Finished cleaning namespaces in drizzled/statement
1736
} /* namespace drizzled */