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