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