~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/alter_table.cc

Renamed namespace slot to namespace service.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2009 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include <config.h>
22
 
 
23
 
#include <fcntl.h>
24
 
 
25
 
#include <sstream>
26
 
 
 
21
#include <drizzled/server_includes.h>
27
22
#include <drizzled/show.h>
28
23
#include <drizzled/lock.h>
29
24
#include <drizzled/session.h>
30
25
#include <drizzled/statement/alter_table.h>
31
 
#include <drizzled/global_charset_info.h>
32
 
 
33
 
 
34
 
#include <drizzled/gettext.h>
35
 
#include <drizzled/data_home.h>
36
 
#include <drizzled/sql_table.h>
37
 
#include <drizzled/table_proto.h>
38
 
#include <drizzled/optimizer/range.h>
39
 
#include <drizzled/time_functions.h>
40
 
#include <drizzled/records.h>
41
 
#include <drizzled/pthread_globals.h>
42
 
#include <drizzled/internal/my_sys.h>
43
 
#include <drizzled/internal/iocache.h>
44
 
#include <drizzled/plugin/storage_engine.h>
45
 
#include <drizzled/copy_field.h>
46
 
 
47
 
#include <drizzled/transaction_services.h>
48
 
 
49
 
#include <drizzled/filesort.h>
50
 
 
51
 
#include <drizzled/message.h>
52
 
 
53
 
using namespace std;
54
 
 
55
 
namespace drizzled
56
 
{
57
 
 
58
 
extern pid_t current_pid;
59
 
 
60
 
static int copy_data_between_tables(Session *session,
61
 
                                    Table *from,Table *to,
 
26
 
 
27
#include "drizzled/gettext.h"
 
28
#include "drizzled/data_home.h"
 
29
#include "drizzled/sql_table.h"
 
30
#include "drizzled/table_proto.h"
 
31
 
 
32
using namespace drizzled;
 
33
 
 
34
static int copy_data_between_tables(Table *from,Table *to,
62
35
                                    List<CreateField> &create,
63
36
                                    bool ignore,
64
37
                                    uint32_t order_num,
65
 
                                    Order *order,
 
38
                                    order_st *order,
66
39
                                    ha_rows *copied,
67
40
                                    ha_rows *deleted,
68
41
                                    enum enum_enable_or_disable keys_onoff,
69
42
                                    bool error_if_not_empty);
70
 
 
71
 
static bool prepare_alter_table(Session *session,
 
43
static bool mysql_prepare_alter_table(Session *session,
72
44
                                      Table *table,
73
45
                                      HA_CREATE_INFO *create_info,
74
 
                                      const message::Table &original_proto,
75
 
                                      message::Table &table_message,
 
46
                                      message::Table *table_proto,
76
47
                                      AlterInfo *alter_info);
77
 
 
78
 
static Table *open_alter_table(Session *session, Table *table, identifier::Table &identifier);
79
 
 
80
 
namespace statement {
81
 
 
82
 
AlterTable::AlterTable(Session *in_session, Table_ident *ident, drizzled::ha_build_method build_arg) :
83
 
  CreateTable(in_session)
84
 
85
 
  in_session->getLex()->sql_command= SQLCOM_ALTER_TABLE;
86
 
  (void)ident;
87
 
  alter_info.build_method= build_arg;
88
 
}
89
 
 
90
 
} // namespace statement
 
48
static int create_temporary_table(Session *session,
 
49
                                  Table *table,
 
50
                                  char *new_db,
 
51
                                  char *tmp_name,
 
52
                                  HA_CREATE_INFO *create_info,
 
53
                                  message::Table *create_proto,
 
54
                                  AlterInfo *alter_info);
91
55
 
92
56
bool statement::AlterTable::execute()
93
57
{
94
 
  TableList *first_table= (TableList *) getSession()->getLex()->select_lex.table_list.first;
95
 
  TableList *all_tables= getSession()->getLex()->query_tables;
 
58
  TableList *first_table= (TableList *) session->lex->select_lex.table_list.first;
 
59
  TableList *all_tables= session->lex->query_tables;
96
60
  assert(first_table == all_tables && first_table != 0);
97
 
  Select_Lex *select_lex= &getSession()->getLex()->select_lex;
 
61
  Select_Lex *select_lex= &session->lex->select_lex;
98
62
  bool need_start_waiting= false;
99
63
 
100
 
  is_engine_set= not createTableMessage().engine().name().empty();
 
64
  /*
 
65
     Code in mysql_alter_table() may modify its HA_CREATE_INFO argument,
 
66
     so we have to use a copy of this structure to make execution
 
67
     prepared statement- safe. A shallow copy is enough as no memory
 
68
     referenced from this structure will be modified.
 
69
   */
 
70
  HA_CREATE_INFO create_info(session->lex->create_info);
 
71
  AlterInfo alter_info(session->lex->alter_info, session->mem_root);
101
72
 
102
 
  if (is_engine_set)
 
73
  if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
103
74
  {
104
 
    create_info().db_type= 
105
 
      plugin::StorageEngine::findByName(*getSession(), createTableMessage().engine().name());
106
 
 
107
 
    if (create_info().db_type == NULL)
108
 
    {
109
 
      my_error(createTableMessage().engine().name(), ER_UNKNOWN_STORAGE_ENGINE, MYF(0));
110
 
 
111
 
      return true;
112
 
    }
 
75
    return true;
113
76
  }
114
77
 
115
78
  /* Must be set in the parser */
116
79
  assert(select_lex->db);
117
80
 
118
 
  /* Chicken/Egg... we need to search for the table, to know if the table exists, so we can build a full identifier from it */
119
 
  message::table::shared_ptr original_table_message;
120
 
  {
121
 
    identifier::Table identifier(first_table->getSchemaName(), first_table->getTableName());
122
 
    if (not (original_table_message= plugin::StorageEngine::getTableMessage(*getSession(), identifier)))
123
 
    {
124
 
      my_error(ER_BAD_TABLE_ERROR, identifier);
125
 
      return true;
126
 
    }
127
 
 
128
 
    if (not  create_info().db_type)
129
 
    {
130
 
      create_info().db_type= 
131
 
        plugin::StorageEngine::findByName(*getSession(), original_table_message->engine().name());
132
 
 
133
 
      if (not create_info().db_type)
134
 
      {
135
 
        my_error(ER_BAD_TABLE_ERROR, identifier);
136
 
        return true;
137
 
      }
138
 
    }
139
 
  }
140
 
 
141
 
  if (not validateCreateTableOption())
142
 
    return true;
143
 
 
144
 
  if (getSession()->inTransaction())
145
 
  {
146
 
    my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
147
 
    return true;
148
 
  }
149
 
 
150
 
  if (not (need_start_waiting= not getSession()->wait_if_global_read_lock(0, 1)))
151
 
    return true;
152
 
 
153
 
  bool res;
154
 
  if (original_table_message->type() == message::Table::STANDARD )
155
 
  {
156
 
    identifier::Table identifier(first_table->getSchemaName(), first_table->getTableName());
157
 
    identifier::Table new_identifier(select_lex->db ? select_lex->db : first_table->getSchemaName(),
158
 
                                   getSession()->getLex()->name.str ? getSession()->getLex()->name.str : first_table->getTableName());
159
 
 
160
 
    res= alter_table(getSession(), 
161
 
                     identifier,
162
 
                     new_identifier,
163
 
                     &create_info(),
164
 
                     *original_table_message,
165
 
                     createTableMessage(),
166
 
                     first_table,
167
 
                     &alter_info,
168
 
                     select_lex->order_list.elements,
169
 
                     (Order *) select_lex->order_list.first,
170
 
                     getSession()->getLex()->ignore);
171
 
  }
172
 
  else
173
 
  {
174
 
    identifier::Table catch22(first_table->getSchemaName(), first_table->getTableName());
175
 
    Table *table= getSession()->find_temporary_table(catch22);
176
 
    assert(table);
177
 
    {
178
 
      identifier::Table identifier(first_table->getSchemaName(), first_table->getTableName(), table->getMutableShare()->getPath());
179
 
      identifier::Table new_identifier(select_lex->db ? select_lex->db : first_table->getSchemaName(),
180
 
                                       getSession()->getLex()->name.str ? getSession()->getLex()->name.str : first_table->getTableName(),
181
 
                                       table->getMutableShare()->getPath());
182
 
 
183
 
      res= alter_table(getSession(), 
184
 
                       identifier,
185
 
                       new_identifier,
186
 
                       &create_info(),
187
 
                       *original_table_message,
188
 
                       createTableMessage(),
189
 
                       first_table,
190
 
                       &alter_info,
191
 
                       select_lex->order_list.elements,
192
 
                       (Order *) select_lex->order_list.first,
193
 
                       getSession()->getLex()->ignore);
194
 
    }
195
 
  }
196
 
 
 
81
  /* ALTER TABLE ends previous transaction */
 
82
  if (! session->endActiveTransaction())
 
83
  {
 
84
    return true;
 
85
  }
 
86
 
 
87
  if (! (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
 
88
  {
 
89
    return true;
 
90
  }
 
91
 
 
92
  bool res= mysql_alter_table(session, 
 
93
                              select_lex->db, 
 
94
                              session->lex->name.str,
 
95
                              &create_info,
 
96
                              session->lex->create_table_proto,
 
97
                              first_table,
 
98
                              &alter_info,
 
99
                              select_lex->order_list.elements,
 
100
                              (order_st *) select_lex->order_list.first,
 
101
                              session->lex->ignore);
197
102
  /*
198
103
     Release the protection against the global read lock and wake
199
104
     everyone, who might want to set a global read lock.
200
105
   */
201
 
  getSession()->startWaitingGlobalReadLock();
202
 
 
 
106
  start_waiting_global_read_lock(session);
203
107
  return res;
204
108
}
205
109
 
206
 
 
207
110
/**
208
111
  Prepare column and key definitions for CREATE TABLE in ALTER Table.
209
112
 
244
147
                 Table instructions
245
148
  @retval false  success
246
149
*/
247
 
static bool prepare_alter_table(Session *session,
248
 
                                Table *table,
249
 
                                HA_CREATE_INFO *create_info,
250
 
                                const message::Table &original_proto,
251
 
                                message::Table &table_message,
252
 
                                AlterInfo *alter_info)
 
150
static bool mysql_prepare_alter_table(Session *session,
 
151
                                      Table *table,
 
152
                                      HA_CREATE_INFO *create_info,
 
153
                                      message::Table *table_proto,
 
154
                                      AlterInfo *alter_info)
253
155
{
254
156
  /* New column definitions are added here */
255
157
  List<CreateField> new_create_list;
256
158
  /* New key definitions are added here */
257
159
  List<Key> new_key_list;
258
 
  List<AlterDrop>::iterator drop_it(alter_info->drop_list.begin());
259
 
  List<CreateField>::iterator def_it(alter_info->create_list.begin());
260
 
  List<AlterColumn>::iterator alter_it(alter_info->alter_list.begin());
261
 
  List<Key>::iterator key_it(alter_info->key_list.begin());
262
 
  List<CreateField>::iterator find_it(new_create_list.begin());
263
 
  List<CreateField>::iterator field_it(new_create_list.begin());
 
160
  List_iterator<AlterDrop> drop_it(alter_info->drop_list);
 
161
  List_iterator<CreateField> def_it(alter_info->create_list);
 
162
  List_iterator<AlterColumn> alter_it(alter_info->alter_list);
 
163
  List_iterator<Key> key_it(alter_info->key_list);
 
164
  List_iterator<CreateField> find_it(new_create_list);
 
165
  List_iterator<CreateField> field_it(new_create_list);
264
166
  List<Key_part_spec> key_parts;
265
167
  uint32_t used_fields= create_info->used_fields;
266
 
  KeyInfo *key_info= table->key_info;
 
168
  KEY *key_info= table->key_info;
267
169
  bool rc= true;
268
170
 
269
171
  /* Let new create options override the old ones */
270
172
  message::Table::TableOptions *table_options;
271
 
  table_options= table_message.mutable_options();
272
 
 
273
 
  if (not (used_fields & HA_CREATE_USED_DEFAULT_CHARSET))
274
 
    create_info->default_table_charset= table->getShare()->table_charset;
275
 
 
276
 
  if (not (used_fields & HA_CREATE_USED_AUTO) && table->found_next_number_field)
 
173
  table_options= table_proto->mutable_options();
 
174
 
 
175
  if (! (used_fields & HA_CREATE_USED_BLOCK_SIZE))
 
176
    table_options->set_block_size(table->s->block_size);
 
177
  if (! (used_fields & HA_CREATE_USED_DEFAULT_CHARSET))
 
178
    create_info->default_table_charset= table->s->table_charset;
 
179
  if (! (used_fields & HA_CREATE_USED_AUTO) &&
 
180
      table->found_next_number_field)
277
181
  {
278
182
    /* Table has an autoincrement, copy value to new table */
279
 
    table->cursor->info(HA_STATUS_AUTO);
280
 
    create_info->auto_increment_value= table->cursor->stats.auto_increment_value;
281
 
    if (create_info->auto_increment_value != original_proto.options().auto_increment_value())
282
 
      table_options->set_has_user_set_auto_increment_value(false);
 
183
    table->file->info(HA_STATUS_AUTO);
 
184
    create_info->auto_increment_value= table->file->stats.auto_increment_value;
283
185
  }
 
186
  if (! (used_fields & HA_CREATE_USED_KEY_BLOCK_SIZE))
 
187
    create_info->key_block_size= table->s->key_block_size;
284
188
 
285
189
  table->restoreRecordAsDefault(); /* Empty record for DEFAULT */
286
190
  CreateField *def;
287
191
 
288
192
  /* First collect all fields from table which isn't in drop_list */
 
193
  Field **f_ptr;
289
194
  Field *field;
290
 
  for (Field **f_ptr= table->getFields(); (field= *f_ptr); f_ptr++)
 
195
  for (f_ptr= table->field; (field= *f_ptr); f_ptr++)
291
196
  {
292
197
    /* Check if field should be dropped */
293
198
    AlterDrop *drop;
294
 
    drop_it= alter_info->drop_list.begin();
 
199
    drop_it.rewind();
295
200
    while ((drop= drop_it++))
296
201
    {
297
202
      if (drop->type == AlterDrop::COLUMN &&
307
212
        break;
308
213
      }
309
214
    }
310
 
 
311
215
    if (drop)
312
216
    {
313
217
      drop_it.remove();
318
222
    field->setReadSet();
319
223
 
320
224
    /* Check if field is changed */
321
 
    def_it= alter_info->create_list.begin();
 
225
    def_it.rewind();
322
226
    while ((def= def_it++))
323
227
    {
324
228
      if (def->change &&
325
229
          ! my_strcasecmp(system_charset_info, field->field_name, def->change))
326
230
              break;
327
231
    }
328
 
 
329
232
    if (def)
330
233
    {
331
234
      /* Field is changed */
344
247
      */
345
248
      def= new CreateField(field, field);
346
249
      new_create_list.push_back(def);
347
 
      alter_it= alter_info->alter_list.begin(); /* Change default if ALTER */
 
250
      alter_it.rewind(); /* Change default if ALTER */
348
251
      AlterColumn *alter;
349
 
 
350
252
      while ((alter= alter_it++))
351
253
      {
352
254
        if (! my_strcasecmp(system_charset_info,field->field_name, alter->name))
353
255
          break;
354
256
      }
355
 
 
356
257
      if (alter)
357
258
      {
358
259
        if (def->sql_type == DRIZZLE_TYPE_BLOB)
359
260
        {
360
261
          my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0), def->change);
361
 
          return true;
 
262
                goto err;
362
263
        }
363
 
 
364
264
        if ((def->def= alter->def))
365
265
        {
366
266
          /* Use new default */
367
267
          def->flags&= ~NO_DEFAULT_VALUE_FLAG;
368
268
        }
369
269
        else
370
 
        {
371
270
          def->flags|= NO_DEFAULT_VALUE_FLAG;
372
 
        }
373
271
        alter_it.remove();
374
272
      }
375
273
    }
376
274
  }
377
 
 
378
 
  def_it= alter_info->create_list.begin();
 
275
  def_it.rewind();
379
276
  while ((def= def_it++)) /* Add new columns */
380
277
  {
381
278
    if (def->change && ! def->field)
382
279
    {
383
 
      my_error(ER_BAD_FIELD_ERROR, MYF(0), def->change, table->getMutableShare()->getTableName());
384
 
      return true;
 
280
      my_error(ER_BAD_FIELD_ERROR, MYF(0), def->change, table->s->table_name.str);
 
281
      goto err;
385
282
    }
386
283
    /*
387
 
      If we have been given a field which has no default value, and is not null then we need to bail.
 
284
      Check that the DATE/DATETIME not null field we are going to add is
 
285
      either has a default value or the '0000-00-00' is allowed by the
 
286
      set sql mode.
 
287
      If the '0000-00-00' value isn't allowed then raise the error_if_not_empty
 
288
      flag to allow ALTER Table only if the table to be altered is empty.
388
289
    */
389
 
    if (not (~def->flags & (NO_DEFAULT_VALUE_FLAG | NOT_NULL_FLAG)) and not def->change)
 
290
    if ((def->sql_type == DRIZZLE_TYPE_DATE ||
 
291
         def->sql_type == DRIZZLE_TYPE_DATETIME) &&
 
292
        ! alter_info->datetime_field &&
 
293
        ! (~def->flags & (NO_DEFAULT_VALUE_FLAG | NOT_NULL_FLAG)) &&
 
294
        session->variables.sql_mode & MODE_NO_ZERO_DATE)
390
295
    {
 
296
      alter_info->datetime_field= def;
391
297
      alter_info->error_if_not_empty= true;
392
298
    }
393
299
    if (! def->after)
394
 
    {
395
300
      new_create_list.push_back(def);
396
 
    }
397
301
    else if (def->after == first_keyword)
398
 
    {
399
302
      new_create_list.push_front(def);
400
 
    }
401
303
    else
402
304
    {
403
305
      CreateField *find;
404
 
      find_it= new_create_list.begin();
405
 
 
 
306
      find_it.rewind();
406
307
      while ((find= find_it++)) /* Add new columns */
407
308
      {
408
 
        if (not my_strcasecmp(system_charset_info,def->after, find->field_name))
 
309
        if (! my_strcasecmp(system_charset_info,def->after, find->field_name))
409
310
          break;
410
311
      }
411
 
 
412
 
      if (not find)
 
312
      if (! find)
413
313
      {
414
 
        my_error(ER_BAD_FIELD_ERROR, MYF(0), def->after, table->getMutableShare()->getTableName());
415
 
        return true;
 
314
        my_error(ER_BAD_FIELD_ERROR, MYF(0), def->after, table->s->table_name.str);
 
315
        goto err;
416
316
      }
417
 
 
418
317
      find_it.after(def); /* Put element after this */
419
 
 
420
318
      /*
421
319
        XXX: hack for Bug#28427.
422
320
        If column order has changed, force OFFLINE ALTER Table
429
327
      */
430
328
      if (alter_info->build_method == HA_BUILD_ONLINE)
431
329
      {
432
 
        my_error(*session->getQueryString(), ER_NOT_SUPPORTED_YET);
433
 
        return true;
 
330
        my_error(ER_NOT_SUPPORTED_YET, MYF(0), session->query);
 
331
        goto err;
434
332
      }
435
 
 
436
333
      alter_info->build_method= HA_BUILD_OFFLINE;
437
334
    }
438
335
  }
439
 
 
440
336
  if (alter_info->alter_list.elements)
441
337
  {
442
338
    my_error(ER_BAD_FIELD_ERROR,
443
339
             MYF(0),
444
340
             alter_info->alter_list.head()->name,
445
 
             table->getMutableShare()->getTableName());
446
 
    return true;
 
341
             table->s->table_name.str);
 
342
    goto err;
447
343
  }
448
 
 
449
 
  if (not new_create_list.elements)
 
344
  if (! new_create_list.elements)
450
345
  {
451
346
    my_message(ER_CANT_REMOVE_ALL_FIELDS,
452
347
               ER(ER_CANT_REMOVE_ALL_FIELDS),
453
348
               MYF(0));
454
 
    return true;
 
349
    goto err;
455
350
  }
456
351
 
457
352
  /*
458
353
    Collect all keys which isn't in drop list. Add only those
459
354
    for which some fields exists.
460
355
  */
461
 
  for (uint32_t i= 0; i < table->getShare()->sizeKeys(); i++, key_info++)
 
356
  for (uint32_t i= 0; i < table->s->keys; i++, key_info++)
462
357
  {
463
358
    char *key_name= key_info->name;
464
359
    AlterDrop *drop;
465
 
 
466
 
    drop_it= alter_info->drop_list.begin();
 
360
    drop_it.rewind();
467
361
    while ((drop= drop_it++))
468
362
    {
469
363
      if (drop->type == AlterDrop::KEY &&
470
364
          ! my_strcasecmp(system_charset_info, key_name, drop->name))
471
365
        break;
472
366
    }
473
 
 
474
367
    if (drop)
475
368
    {
476
369
      drop_it.remove();
477
370
      continue;
478
371
    }
479
372
 
480
 
    KeyPartInfo *key_part= key_info->key_part;
481
 
    key_parts.clear();
 
373
    KEY_PART_INFO *key_part= key_info->key_part;
 
374
    key_parts.empty();
482
375
    for (uint32_t j= 0; j < key_info->key_parts; j++, key_part++)
483
376
    {
484
377
      if (! key_part->field)
486
379
 
487
380
      const char *key_part_name= key_part->field->field_name;
488
381
      CreateField *cfield;
489
 
      field_it= new_create_list.begin();
 
382
      field_it.rewind();
490
383
      while ((cfield= field_it++))
491
384
      {
492
385
        if (cfield->change)
493
386
        {
494
 
          if (not my_strcasecmp(system_charset_info, key_part_name, cfield->change))
 
387
          if (! my_strcasecmp(system_charset_info, key_part_name, cfield->change))
495
388
            break;
496
389
        }
497
 
        else if (not my_strcasecmp(system_charset_info, key_part_name, cfield->field_name))
 
390
        else if (! my_strcasecmp(system_charset_info, key_part_name, cfield->field_name))
498
391
          break;
499
392
      }
500
 
 
501
 
      if (not cfield)
 
393
      if (! cfield)
502
394
              continue; /* Field is removed */
503
395
      
504
396
      uint32_t key_part_length= key_part->length;
518
410
         */
519
411
        if (! Field::type_can_have_key_part(cfield->field->type()) ||
520
412
            ! Field::type_can_have_key_part(cfield->sql_type) ||
521
 
            (cfield->field->field_length == key_part_length) ||
 
413
            (cfield->field->field_length == key_part_length &&
 
414
             ! f_is_blob(key_part->key_type)) ||
522
415
            (cfield->length &&
523
416
             (cfield->length < key_part_length / key_part->field->charset()->mbmaxlen)))
524
417
          key_part_length= 0; /* Use whole field */
530
423
    }
531
424
    if (key_parts.elements)
532
425
    {
533
 
      key_create_information_st key_create_info= default_key_create_info;
 
426
      KEY_CREATE_INFO key_create_info;
534
427
      Key *key;
535
 
      Key::Keytype key_type;
 
428
      enum Key::Keytype key_type;
 
429
      memset(&key_create_info, 0, sizeof(key_create_info));
536
430
 
537
431
      key_create_info.algorithm= key_info->algorithm;
538
 
 
539
432
      if (key_info->flags & HA_USES_BLOCK_SIZE)
540
433
        key_create_info.block_size= key_info->block_size;
541
 
 
542
434
      if (key_info->flags & HA_USES_COMMENT)
543
435
        key_create_info.comment= key_info->comment;
544
436
 
550
442
          key_type= Key::UNIQUE;
551
443
      }
552
444
      else
553
 
      {
554
445
        key_type= Key::MULTIPLE;
555
 
      }
556
446
 
557
447
      key= new Key(key_type,
558
448
                   key_name,
563
453
      new_key_list.push_back(key);
564
454
    }
565
455
  }
566
 
 
567
 
  /* Copy over existing foreign keys */
568
 
  for (int32_t j= 0; j < original_proto.fk_constraint_size(); j++)
569
 
  {
570
 
    AlterDrop *drop;
571
 
    drop_it= alter_info->drop_list.begin();
572
 
    while ((drop= drop_it++))
573
 
    {
574
 
      if (drop->type == AlterDrop::FOREIGN_KEY &&
575
 
          ! my_strcasecmp(system_charset_info, original_proto.fk_constraint(j).name().c_str(), drop->name))
576
 
      {
577
 
        break;
578
 
      }
579
 
    }
580
 
    if (drop)
581
 
    {
582
 
      drop_it.remove();
583
 
      continue;
584
 
    }
585
 
 
586
 
    message::Table::ForeignKeyConstraint *pfkey= table_message.add_fk_constraint();
587
 
    *pfkey= original_proto.fk_constraint(j);
588
 
  }
589
 
 
590
456
  {
591
457
    Key *key;
592
458
    while ((key= key_it++)) /* Add new keys */
593
459
    {
594
 
      if (key->type == Key::FOREIGN_KEY)
595
 
      {
596
 
        if (((Foreign_key *)key)->validate(new_create_list))
597
 
        {
598
 
          return true;
599
 
        }
600
 
 
601
 
        Foreign_key *fkey= (Foreign_key*)key;
602
 
        add_foreign_key_to_table_message(&table_message,
603
 
                                         fkey->name.str,
604
 
                                         fkey->columns,
605
 
                                         fkey->ref_table,
606
 
                                         fkey->ref_columns,
607
 
                                         fkey->delete_opt,
608
 
                                         fkey->update_opt,
609
 
                                         fkey->match_opt);
610
 
      }
611
 
 
 
460
      if (key->type == Key::FOREIGN_KEY &&
 
461
          ((Foreign_key *)key)->validate(new_create_list))
 
462
        goto err;
612
463
      if (key->type != Key::FOREIGN_KEY)
613
464
        new_key_list.push_back(key);
614
 
 
615
465
      if (key->name.str && is_primary_key_name(key->name.str))
616
466
      {
617
467
        my_error(ER_WRONG_NAME_FOR_INDEX,
618
468
                 MYF(0),
619
469
                 key->name.str);
620
 
        return true;
 
470
        goto err;
621
471
      }
622
472
    }
623
473
  }
624
474
 
625
 
  /* Fix names of foreign keys being added */
626
 
  for (int j= 0; j < table_message.fk_constraint_size(); j++)
627
 
  {
628
 
    if (! table_message.fk_constraint(j).has_name())
629
 
    {
630
 
      std::string name(table->getMutableShare()->getTableName());
631
 
      char number[20];
632
 
 
633
 
      name.append("_ibfk_");
634
 
      snprintf(number, sizeof(number), "%d", j+1);
635
 
      name.append(number);
636
 
 
637
 
      message::Table::ForeignKeyConstraint *pfkey= table_message.mutable_fk_constraint(j);
638
 
      pfkey->set_name(name);
639
 
    }
640
 
  }
641
 
 
642
475
  if (alter_info->drop_list.elements)
643
476
  {
644
477
    my_error(ER_CANT_DROP_FIELD_OR_KEY,
645
478
             MYF(0),
646
479
             alter_info->drop_list.head()->name);
647
 
    return true;
 
480
    goto err;
648
481
  }
649
 
 
650
482
  if (alter_info->alter_list.elements)
651
483
  {
652
484
    my_error(ER_CANT_DROP_FIELD_OR_KEY,
653
485
             MYF(0),
654
486
             alter_info->alter_list.head()->name);
655
 
    return true;
656
 
  }
657
 
 
658
 
  if (not table_message.options().has_comment()
659
 
      && table->getMutableShare()->hasComment())
660
 
  {
661
 
    table_options->set_comment(table->getMutableShare()->getComment());
662
 
  }
663
 
 
664
 
  if (table->getShare()->getType())
665
 
  {
666
 
    table_message.set_type(message::Table::TEMPORARY);
667
 
  }
668
 
 
669
 
  table_message.set_creation_timestamp(table->getShare()->getTableMessage()->creation_timestamp());
670
 
  table_message.set_version(table->getShare()->getTableMessage()->version());
671
 
  table_message.set_uuid(table->getShare()->getTableMessage()->uuid());
 
487
    goto err;
 
488
  }
 
489
 
 
490
  if (! table_proto->options().has_comment()
 
491
      && table->s->hasComment())
 
492
    table_options->set_comment(table->s->getComment());
 
493
 
 
494
  if (table->s->tmp_table)
 
495
    create_info->options|= HA_LEX_CREATE_TMP_TABLE;
672
496
 
673
497
  rc= false;
674
498
  alter_info->create_list.swap(new_create_list);
675
499
  alter_info->key_list.swap(new_key_list);
676
 
 
677
 
  size_t num_engine_options= table_message.engine().options_size();
678
 
  size_t original_num_engine_options= original_proto.engine().options_size();
679
 
  for (size_t x= 0; x < original_num_engine_options; ++x)
680
 
  {
681
 
    bool found= false;
682
 
 
683
 
    for (size_t y= 0; y < num_engine_options; ++y)
684
 
    {
685
 
      found= not table_message.engine().options(y).name().compare(original_proto.engine().options(x).name());
686
 
      
687
 
      if (found)
688
 
        break;
689
 
    }
690
 
 
691
 
    if (not found)
692
 
    {
693
 
      message::Engine::Option *opt= table_message.mutable_engine()->add_options();
694
 
 
695
 
      opt->set_name(original_proto.engine().options(x).name());
696
 
      opt->set_state(original_proto.engine().options(x).state());
697
 
    }
698
 
  }
699
 
 
700
 
  drizzled::message::update(table_message);
701
 
 
702
 
  return false;
 
500
err:
 
501
  return rc;
703
502
}
704
503
 
705
504
/* table_list should contain just one table */
706
 
static int discard_or_import_tablespace(Session *session,
 
505
static int mysql_discard_or_import_tablespace(Session *session,
707
506
                                              TableList *table_list,
708
507
                                              enum tablespace_op_type tablespace_op)
709
508
{
710
509
  Table *table;
711
510
  bool discard;
 
511
  int error;
712
512
 
713
513
  /*
714
514
    Note that DISCARD/IMPORT TABLESPACE always is the only operation in an
715
515
    ALTER Table
716
516
  */
717
 
  TransactionServices &transaction_services= TransactionServices::singleton();
 
517
 
718
518
  session->set_proc_info("discard_or_import_tablespace");
719
519
 
720
520
  discard= test(tablespace_op == DISCARD_TABLESPACE);
723
523
   We set this flag so that ha_innobase::open and ::external_lock() do
724
524
   not complain when we lock the table
725
525
 */
726
 
  session->setDoingTablespaceOperation(true);
727
 
  if (not (table= session->openTableLock(table_list, TL_WRITE)))
 
526
  session->tablespace_op= true;
 
527
  if (!(table= session->openTableLock(table_list, TL_WRITE)))
728
528
  {
729
 
    session->setDoingTablespaceOperation(false);
 
529
    session->tablespace_op= false;
730
530
    return -1;
731
531
  }
732
532
 
733
 
  int error;
734
 
  do {
735
 
    error= table->cursor->ha_discard_or_import_tablespace(discard);
736
 
 
737
 
    session->set_proc_info("end");
738
 
 
739
 
    if (error)
740
 
      break;
741
 
 
742
 
    /* The ALTER Table is always in its own transaction */
743
 
    error= transaction_services.autocommitOrRollback(*session, false);
744
 
    if (not session->endActiveTransaction())
745
 
      error= 1;
746
 
 
747
 
    if (error)
748
 
      break;
749
 
 
750
 
    write_bin_log(session, *session->getQueryString());
751
 
 
752
 
  } while(0);
753
 
 
754
 
  (void) transaction_services.autocommitOrRollback(*session, error);
755
 
  session->setDoingTablespaceOperation(false);
 
533
  error= table->file->ha_discard_or_import_tablespace(discard);
 
534
 
 
535
  session->set_proc_info("end");
 
536
 
 
537
  if (error)
 
538
    goto err;
 
539
 
 
540
  /* The ALTER Table is always in its own transaction */
 
541
  error = ha_autocommit_or_rollback(session, 0);
 
542
  if (! session->endActiveTransaction())
 
543
    error=1;
 
544
  if (error)
 
545
    goto err;
 
546
  write_bin_log(session, false, session->query, session->query_length);
 
547
 
 
548
err:
 
549
  ha_autocommit_or_rollback(session, error);
 
550
  session->tablespace_op=false;
756
551
 
757
552
  if (error == 0)
758
553
  {
760
555
    return 0;
761
556
  }
762
557
 
763
 
  table->print_error(error, MYF(0));
 
558
  table->file->print_error(error, MYF(0));
764
559
 
765
560
  return -1;
766
561
}
779
574
    false  OK
780
575
    true   Error
781
576
*/
782
 
static bool alter_table_manage_keys(Session *session,
783
 
                                    Table *table, int indexes_were_disabled,
784
 
                                    enum enum_enable_or_disable keys_onoff)
 
577
static bool alter_table_manage_keys(Table *table, int indexes_were_disabled,
 
578
                             enum enum_enable_or_disable keys_onoff)
785
579
{
786
580
  int error= 0;
787
581
  switch (keys_onoff) {
788
582
  case ENABLE:
789
 
    error= table->cursor->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
 
583
    error= table->file->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
790
584
    break;
791
585
  case LEAVE_AS_IS:
792
 
    if (not indexes_were_disabled)
 
586
    if (!indexes_were_disabled)
793
587
      break;
794
588
    /* fall-through: disabled indexes */
795
589
  case DISABLE:
796
 
    error= table->cursor->ha_disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
 
590
    error= table->file->ha_disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
797
591
  }
798
592
 
799
593
  if (error == HA_ERR_WRONG_COMMAND)
800
594
  {
801
 
    push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
595
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
802
596
                        ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
803
 
                        table->getMutableShare()->getTableName());
 
597
                        table->s->table_name.str);
804
598
    error= 0;
805
 
  }
806
 
  else if (error)
807
 
  {
808
 
    table->print_error(error, MYF(0));
809
 
  }
 
599
  } else if (error)
 
600
    table->file->print_error(error, MYF(0));
810
601
 
811
602
  return(error);
812
603
}
813
604
 
814
 
static bool lockTableIfDifferent(Session &session,
815
 
                                 identifier::Table &original_table_identifier,
816
 
                                 identifier::Table &new_table_identifier,
817
 
                                 Table *name_lock)
818
 
{
819
 
  /* Check that we are not trying to rename to an existing table */
820
 
  if (not (original_table_identifier == new_table_identifier))
821
 
  {
822
 
    if (original_table_identifier.isTmp())
823
 
    {
824
 
 
825
 
      if (session.find_temporary_table(new_table_identifier))
826
 
      {
827
 
        my_error(ER_TABLE_EXISTS_ERROR, new_table_identifier);
828
 
        return false;
829
 
      }
830
 
    }
831
 
    else
832
 
    {
833
 
      if (session.lock_table_name_if_not_cached(new_table_identifier, &name_lock))
834
 
      {
835
 
        return false;
836
 
      }
837
 
 
838
 
      if (not name_lock)
839
 
      {
840
 
        my_error(ER_TABLE_EXISTS_ERROR, new_table_identifier);
841
 
        return false;
842
 
      }
843
 
 
844
 
      if (plugin::StorageEngine::doesTableExist(session, new_table_identifier))
845
 
      {
846
 
        /* Table will be closed by Session::executeCommand() */
847
 
        my_error(ER_TABLE_EXISTS_ERROR, new_table_identifier);
848
 
 
849
 
        {
850
 
          boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
851
 
          session.unlink_open_table(name_lock);
852
 
        }
853
 
 
854
 
        return false;
855
 
      }
856
 
    }
857
 
  }
858
 
 
859
 
  return true;
860
 
}
861
 
 
862
605
/**
863
606
  Alter table
864
607
 
865
608
  SYNOPSIS
866
 
    alter_table()
 
609
    mysql_alter_table()
867
610
      session              Thread handle
868
611
      new_db           If there is a RENAME clause
869
612
      new_name         If there is a RENAME clause
872
615
      table_list       The table to change.
873
616
      alter_info       Lists of fields, keys to be changed, added
874
617
                       or dropped.
875
 
      order_num        How many ORDER BY fields has been specified.
 
618
      order_num        How many order_st BY fields has been specified.
876
619
      order            List of fields to order_st BY.
877
620
      ignore           Whether we have ALTER IGNORE Table
878
621
 
884
627
    When the ALTER Table statement just does a RENAME or ENABLE|DISABLE KEYS,
885
628
    or both, then this function short cuts its operation by renaming
886
629
    the table and/or enabling/disabling the keys. In this case, the FRM is
887
 
    not changed, directly by alter_table. However, if there is a
 
630
    not changed, directly by mysql_alter_table. However, if there is a
888
631
    RENAME + change of a field, or an index, the short cut is not used.
889
632
    See how `create_list` is used to generate the new FRM regarding the
890
633
    structure of the fields. The same is done for the indices of the table.
900
643
    false  OK
901
644
    true   Error
902
645
*/
903
 
 
904
 
static bool internal_alter_table(Session *session,
905
 
                                 Table *table,
906
 
                                 identifier::Table &original_table_identifier,
907
 
                                 identifier::Table &new_table_identifier,
908
 
                                 HA_CREATE_INFO *create_info,
909
 
                                 const message::Table &original_proto,
910
 
                                 message::Table &create_proto,
911
 
                                 TableList *table_list,
912
 
                                 AlterInfo *alter_info,
913
 
                                 uint32_t order_num,
914
 
                                 Order *order,
915
 
                                 bool ignore)
 
646
bool mysql_alter_table(Session *session,
 
647
                       char *new_db,
 
648
                       char *new_name,
 
649
                       HA_CREATE_INFO *create_info,
 
650
                       message::Table *create_proto,
 
651
                       TableList *table_list,
 
652
                       AlterInfo *alter_info,
 
653
                       uint32_t order_num,
 
654
                       order_st *order,
 
655
                       bool ignore)
916
656
{
 
657
  Table *table;
 
658
  Table *new_table= NULL;
 
659
  Table *name_lock= NULL;
 
660
  string new_name_str;
917
661
  int error= 0;
918
662
  char tmp_name[80];
919
663
  char old_name[32];
 
664
  char new_name_buff[FN_REFLEN];
 
665
  char new_alias_buff[FN_REFLEN];
 
666
  char *table_name;
 
667
  char *db;
 
668
  const char *new_alias;
 
669
  char path[FN_REFLEN];
920
670
  ha_rows copied= 0;
921
671
  ha_rows deleted= 0;
922
 
 
923
 
  if (not original_table_identifier.isValid())
924
 
    return true;
925
 
 
926
 
  if (not new_table_identifier.isValid())
927
 
    return true;
 
672
  plugin::StorageEngine *old_db_type;
 
673
  plugin::StorageEngine *new_db_type;
 
674
  plugin::StorageEngine *save_old_db_type;
 
675
  bitset<32> tmp;
 
676
 
 
677
  new_name_buff[0]= '\0';
 
678
 
 
679
  if (table_list && table_list->schema_table)
 
680
  {
 
681
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.c_str());
 
682
    return true;
 
683
  }
928
684
 
929
685
  session->set_proc_info("init");
930
686
 
 
687
  /*
 
688
    Assign variables table_name, new_name, db, new_db, path
 
689
    to simplify further comparisons: we want to see if it's a RENAME
 
690
    later just by comparing the pointers, avoiding the need for strcmp.
 
691
  */
 
692
  table_name= table_list->table_name;
 
693
  db= table_list->db;
 
694
  if (! new_db || ! my_strcasecmp(table_alias_charset, new_db, db))
 
695
    new_db= db;
 
696
 
 
697
  if (alter_info->tablespace_op != NO_TABLESPACE_OP)
 
698
  {
 
699
    /* DISCARD/IMPORT TABLESPACE is always alone in an ALTER Table */
 
700
    return mysql_discard_or_import_tablespace(session, table_list, alter_info->tablespace_op);
 
701
  }
 
702
 
 
703
  build_table_filename(path, sizeof(path), db, table_name, false);
 
704
 
 
705
  ostringstream oss;
 
706
  oss << drizzle_data_home << "/" << db << "/" << table_name;
 
707
 
 
708
  (void) unpack_filename(new_name_buff, oss.str().c_str());
 
709
 
 
710
  /*
 
711
    If this is just a rename of a view, short cut to the
 
712
    following scenario: 1) lock LOCK_open 2) do a RENAME
 
713
    2) unlock LOCK_open.
 
714
    This is a copy-paste added to make sure
 
715
    ALTER (sic:) Table .. RENAME works for views. ALTER VIEW is handled
 
716
    as an independent branch in mysql_execute_command. The need
 
717
    for a copy-paste arose because the main code flow of ALTER Table
 
718
    ... RENAME tries to use openTableLock, which does not work for views
 
719
    (openTableLock was never modified to merge table lists of child tables
 
720
    into the main table list, like open_tables does).
 
721
    This code is wrong and will be removed, please do not copy.
 
722
  */
 
723
 
 
724
  if (!(table= session->openTableLock(table_list, TL_WRITE_ALLOW_READ)))
 
725
    return true;
 
726
  
931
727
  table->use_all_columns();
932
728
 
933
 
  plugin::StorageEngine *new_engine;
934
 
  plugin::StorageEngine *original_engine;
935
 
 
936
 
  original_engine= table->getMutableShare()->getEngine();
937
 
 
938
 
  if (not create_info->db_type)
939
 
  {
940
 
    create_info->db_type= original_engine;
941
 
  }
942
 
  new_engine= create_info->db_type;
943
 
 
944
 
 
945
 
  create_proto.set_schema(new_table_identifier.getSchemaName());
946
 
  create_proto.set_type(new_table_identifier.getType());
947
 
 
948
 
  /**
949
 
    @todo Have a check on the table definition for FK in the future 
950
 
    to remove the need for the cursor. (aka can_switch_engines())
951
 
  */
952
 
  if (new_engine != original_engine &&
953
 
      not table->cursor->can_switch_engines())
 
729
  /* Check that we are not trying to rename to an existing table */
 
730
  if (new_name)
 
731
  {
 
732
    strcpy(new_name_buff, new_name);
 
733
    strcpy(new_alias_buff, new_name);
 
734
    new_alias= new_alias_buff;
 
735
 
 
736
    my_casedn_str(files_charset_info, new_name_buff);
 
737
    new_alias= new_name; // Create lower case table name
 
738
    my_casedn_str(files_charset_info, new_name);
 
739
 
 
740
    if (new_db == db &&
 
741
        ! my_strcasecmp(table_alias_charset, new_name_buff, table_name))
 
742
    {
 
743
      /*
 
744
        Source and destination table names are equal: make later check
 
745
        easier.
 
746
      */
 
747
      new_alias= new_name= table_name;
 
748
    }
 
749
    else
 
750
    {
 
751
      if (table->s->tmp_table != NO_TMP_TABLE)
 
752
      {
 
753
        if (session->find_temporary_table(new_db, new_name_buff))
 
754
        {
 
755
          my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name_buff);
 
756
          return true;
 
757
        }
 
758
      }
 
759
      else
 
760
      {
 
761
        if (session->lock_table_name_if_not_cached(new_db, new_name, &name_lock))
 
762
          return true;
 
763
 
 
764
        if (! name_lock)
 
765
        {
 
766
          my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
 
767
          return true;
 
768
        }
 
769
 
 
770
        build_table_filename(new_name_buff, sizeof(new_name_buff), new_db, new_name_buff, false);
 
771
 
 
772
        plugin::Registry &plugins= plugin::Registry::singleton();
 
773
        if (plugins.storage_engine.getTableProto(new_name_buff, NULL) == EEXIST)
 
774
        {
 
775
          /* Table will be closed by Session::executeCommand() */
 
776
          my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
 
777
          goto err;
 
778
        }
 
779
      }
 
780
    }
 
781
  }
 
782
  else
 
783
  {
 
784
    new_alias= table_name;
 
785
    new_name= table_name;
 
786
  }
 
787
 
 
788
  old_db_type= table->s->db_type();
 
789
  if (! create_info->db_type)
 
790
  {
 
791
    create_info->db_type= old_db_type;
 
792
  }
 
793
 
 
794
  if (table->s->tmp_table != NO_TMP_TABLE)
 
795
    create_info->options|= HA_LEX_CREATE_TMP_TABLE;
 
796
 
 
797
  if (check_engine(session, new_name, create_info))
 
798
    goto err;
 
799
 
 
800
  new_db_type= create_info->db_type;
 
801
 
 
802
  if (new_db_type != old_db_type &&
 
803
      !table->file->can_switch_engines())
954
804
  {
955
805
    assert(0);
956
806
    my_error(ER_ROW_IS_REFERENCED, MYF(0));
957
 
 
958
 
    return true;
 
807
    goto err;
959
808
  }
960
809
 
961
 
  if (original_engine->check_flag(HTON_BIT_ALTER_NOT_SUPPORTED) ||
962
 
      new_engine->check_flag(HTON_BIT_ALTER_NOT_SUPPORTED))
 
810
  if (create_info->row_type == ROW_TYPE_NOT_USED)
 
811
    create_info->row_type= table->s->row_type;
 
812
 
 
813
  if (old_db_type->check_flag(HTON_BIT_ALTER_NOT_SUPPORTED) ||
 
814
      new_db_type->check_flag(HTON_BIT_ALTER_NOT_SUPPORTED))
963
815
  {
964
 
    my_error(ER_ILLEGAL_HA, new_table_identifier);
965
 
 
966
 
    return true;
 
816
    my_error(ER_ILLEGAL_HA, MYF(0), table_name);
 
817
    goto err;
967
818
  }
968
819
 
969
820
  session->set_proc_info("setup");
970
 
 
 
821
  
971
822
  /*
972
823
   * test if no other bits except ALTER_RENAME and ALTER_KEYS_ONOFF are set
973
 
 */
 
824
   */
 
825
  tmp.set();
 
826
  tmp.reset(ALTER_RENAME);
 
827
  tmp.reset(ALTER_KEYS_ONOFF);
 
828
  tmp&= alter_info->flags;
 
829
  if (! (tmp.any()) &&
 
830
      ! table->s->tmp_table) // no need to touch frm
974
831
  {
975
 
    bitset<32> tmp;
976
 
 
977
 
    tmp.set();
978
 
    tmp.reset(ALTER_RENAME);
979
 
    tmp.reset(ALTER_KEYS_ONOFF);
980
 
    tmp&= alter_info->flags;
981
 
 
982
 
    if (not (tmp.any()) && not table->getShare()->getType()) // no need to touch frm
983
 
    {
984
 
      switch (alter_info->keys_onoff)
985
 
      {
986
 
      case LEAVE_AS_IS:
987
 
        break;
988
 
 
989
 
      case ENABLE:
990
 
        /*
991
 
          wait_while_table_is_used() ensures that table being altered is
992
 
          opened only by this thread and that Table::TableShare::version
993
 
          of Table object corresponding to this table is 0.
994
 
          The latter guarantees that no DML statement will open this table
995
 
          until ALTER Table finishes (i.e. until close_thread_tables())
996
 
          while the fact that the table is still open gives us protection
997
 
          from concurrent DDL statements.
998
 
        */
999
 
        {
1000
 
          boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex()); /* DDL wait for/blocker */
1001
 
          wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
1002
 
        }
1003
 
        error= table->cursor->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
1004
 
 
1005
 
        /* COND_refresh will be signaled in close_thread_tables() */
1006
 
        break;
1007
 
 
1008
 
      case DISABLE:
1009
 
        {
1010
 
          boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex()); /* DDL wait for/blocker */
1011
 
          wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
1012
 
        }
1013
 
        error= table->cursor->ha_disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
1014
 
 
1015
 
        /* COND_refresh will be signaled in close_thread_tables() */
1016
 
        break;
1017
 
      }
1018
 
 
1019
 
      if (error == HA_ERR_WRONG_COMMAND)
1020
 
      {
1021
 
        error= EE_OK;
1022
 
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1023
 
                            ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
1024
 
                            table->getAlias());
1025
 
      }
1026
 
 
1027
 
      boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex()); /* Lock to remove all instances of table from table cache before ALTER */
1028
 
      /*
1029
 
        Unlike to the above case close_cached_table() below will remove ALL
1030
 
        instances of Table from table cache (it will also remove table lock
1031
 
        held by this thread). So to make actual table renaming and writing
1032
 
        to binlog atomic we have to put them into the same critical section
1033
 
        protected by table::Cache::singleton().mutex() mutex. This also removes gap for races between
1034
 
        access() and rename_table() calls.
1035
 
      */
1036
 
 
1037
 
      if (not error &&  not (original_table_identifier == new_table_identifier))
1038
 
      {
1039
 
        session->set_proc_info("rename");
1040
 
        /*
1041
 
          Then do a 'simple' rename of the table. First we need to close all
1042
 
          instances of 'source' table.
1043
 
        */
1044
 
        session->close_cached_table(table);
1045
 
        /*
1046
 
          Then, we want check once again that target table does not exist.
1047
 
          Actually the order of these two steps does not matter since
1048
 
          earlier we took name-lock on the target table, so we do them
1049
 
          in this particular order only to be consistent with 5.0, in which
1050
 
          we don't take this name-lock and where this order really matters.
1051
 
          @todo Investigate if we need this access() check at all.
1052
 
        */
1053
 
        if (plugin::StorageEngine::doesTableExist(*session, new_table_identifier))
1054
 
        {
1055
 
          my_error(ER_TABLE_EXISTS_ERROR, new_table_identifier);
 
832
    switch (alter_info->keys_onoff)
 
833
    {
 
834
    case LEAVE_AS_IS:
 
835
      break;
 
836
    case ENABLE:
 
837
      /*
 
838
        wait_while_table_is_used() ensures that table being altered is
 
839
        opened only by this thread and that Table::TableShare::version
 
840
        of Table object corresponding to this table is 0.
 
841
        The latter guarantees that no DML statement will open this table
 
842
        until ALTER Table finishes (i.e. until close_thread_tables())
 
843
        while the fact that the table is still open gives us protection
 
844
        from concurrent DDL statements.
 
845
      */
 
846
      pthread_mutex_lock(&LOCK_open); /* DDL wait for/blocker */
 
847
      wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
 
848
      pthread_mutex_unlock(&LOCK_open);
 
849
      error= table->file->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
 
850
      /* COND_refresh will be signaled in close_thread_tables() */
 
851
      break;
 
852
    case DISABLE:
 
853
      pthread_mutex_lock(&LOCK_open); /* DDL wait for/blocker */
 
854
      wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
 
855
      pthread_mutex_unlock(&LOCK_open);
 
856
      error=table->file->ha_disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
 
857
      /* COND_refresh will be signaled in close_thread_tables() */
 
858
      break;
 
859
    default:
 
860
      assert(false);
 
861
      error= 0;
 
862
      break;
 
863
    }
 
864
 
 
865
    if (error == HA_ERR_WRONG_COMMAND)
 
866
    {
 
867
      error= 0;
 
868
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
869
                          ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
 
870
                          table->alias);
 
871
    }
 
872
 
 
873
    pthread_mutex_lock(&LOCK_open); /* Lock to remove all instances of table from table cache before ALTER */
 
874
    /*
 
875
      Unlike to the above case close_cached_table() below will remove ALL
 
876
      instances of Table from table cache (it will also remove table lock
 
877
      held by this thread). So to make actual table renaming and writing
 
878
      to binlog atomic we have to put them into the same critical section
 
879
      protected by LOCK_open mutex. This also removes gap for races between
 
880
      access() and mysql_rename_table() calls.
 
881
    */
 
882
 
 
883
    if (error == 0 && 
 
884
        (new_name != table_name || new_db != db))
 
885
    {
 
886
      session->set_proc_info("rename");
 
887
      /*
 
888
        Then do a 'simple' rename of the table. First we need to close all
 
889
        instances of 'source' table.
 
890
      */
 
891
      session->close_cached_table(table);
 
892
      /*
 
893
        Then, we want check once again that target table does not exist.
 
894
        Actually the order of these two steps does not matter since
 
895
        earlier we took name-lock on the target table, so we do them
 
896
        in this particular order only to be consistent with 5.0, in which
 
897
        we don't take this name-lock and where this order really matters.
 
898
        TODO: Investigate if we need this access() check at all.
 
899
      */
 
900
      plugin::Registry &plugins= plugin::Registry::singleton();
 
901
      if (plugins.storage_engine.getTableProto(new_name, NULL) == EEXIST)
 
902
      {
 
903
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name);
 
904
        error= -1;
 
905
      }
 
906
      else
 
907
      {
 
908
        *fn_ext(new_name)= 0;
 
909
        if (mysql_rename_table(old_db_type, db, table_name, new_db, new_alias, 0))
1056
910
          error= -1;
1057
 
        }
1058
 
        else
1059
 
        {
1060
 
          if (rename_table(*session, original_engine, original_table_identifier, new_table_identifier))
1061
 
          {
1062
 
            error= -1;
1063
 
          }
1064
 
        }
1065
 
      }
1066
 
 
1067
 
      if (error == HA_ERR_WRONG_COMMAND)
1068
 
      {
1069
 
        error= EE_OK;
1070
 
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1071
 
                            ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
1072
 
                            table->getAlias());
1073
 
      }
1074
 
 
1075
 
      if (not error)
1076
 
      {
1077
 
        TransactionServices &transaction_services= TransactionServices::singleton();
1078
 
        transaction_services.allocateNewTransactionId();
1079
 
        write_bin_log(session, *session->getQueryString());
1080
 
        session->my_ok();
1081
 
      }
1082
 
      else if (error > EE_OK) // If we have already set the error, we pass along -1
1083
 
      {
1084
 
        table->print_error(error, MYF(0));
1085
 
      }
1086
 
 
1087
 
      table_list->table= NULL;
1088
 
 
1089
 
      return error;
1090
 
    }
 
911
      }
 
912
    }
 
913
 
 
914
    if (error == HA_ERR_WRONG_COMMAND)
 
915
    {
 
916
      error= 0;
 
917
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
918
                          ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
 
919
                          table->alias);
 
920
    }
 
921
 
 
922
    if (error == 0)
 
923
    {
 
924
      write_bin_log(session, true, session->query, session->query_length);
 
925
      session->my_ok();
 
926
    }
 
927
    else if (error > 0)
 
928
    {
 
929
      table->file->print_error(error, MYF(0));
 
930
      error= -1;
 
931
    }
 
932
 
 
933
    if (name_lock)
 
934
      session->unlink_open_table(name_lock);
 
935
 
 
936
    pthread_mutex_unlock(&LOCK_open);
 
937
    table_list->table= NULL;
 
938
    return error;
1091
939
  }
1092
940
 
1093
941
  /* We have to do full alter table. */
1094
 
  new_engine= create_info->db_type;
1095
 
 
1096
 
  if (prepare_alter_table(session, table, create_info, original_proto, create_proto, alter_info))
1097
 
  {
1098
 
    return true;
1099
 
  }
1100
 
 
1101
 
  set_table_default_charset(create_info, new_table_identifier.getSchemaName().c_str());
 
942
 
 
943
  /*
 
944
    If the old table had partitions and we are doing ALTER Table ...
 
945
    engine= <new_engine>, the new table must preserve the original
 
946
    partitioning. That means that the new engine is still the
 
947
    partitioning engine, not the engine specified in the parser.
 
948
    This is discovered  in prep_alter_part_table, which in such case
 
949
    updates create_info->db_type.
 
950
    Now we need to update the stack copy of create_info->db_type,
 
951
    as otherwise we won't be able to correctly move the files of the
 
952
    temporary table to the result table files.
 
953
  */
 
954
  new_db_type= create_info->db_type;
 
955
 
 
956
  if (mysql_prepare_alter_table(session, table, create_info, create_proto,
 
957
                                alter_info))
 
958
      goto err;
 
959
 
 
960
  set_table_default_charset(create_info, db);
1102
961
 
1103
962
  alter_info->build_method= HA_BUILD_OFFLINE;
1104
963
 
1105
964
  snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%"PRIx64, TMP_FILE_PREFIX, (unsigned long) current_pid, session->thread_id);
 
965
  
 
966
  /* Safety fix for innodb */
 
967
  my_casedn_str(files_charset_info, tmp_name);
1106
968
 
1107
969
  /* Create a temporary table with the new format */
1108
 
  /**
1109
 
    @note we make an internal temporary table unless the table is a temporary table. In last
1110
 
    case we just use it as is. Neither of these tables require locks in order to  be
1111
 
    filled.
1112
 
  */
1113
 
  identifier::Table new_table_as_temporary(original_table_identifier.getSchemaName(),
1114
 
                                         tmp_name,
1115
 
                                         create_proto.type() != message::Table::TEMPORARY ? message::Table::INTERNAL :
1116
 
                                         message::Table::TEMPORARY);
1117
 
 
1118
 
  /*
1119
 
    Create a table with a temporary name.
1120
 
    We don't log the statement, it will be logged later.
1121
 
  */
1122
 
  create_proto.set_name(new_table_as_temporary.getTableName());
1123
 
  create_proto.mutable_engine()->set_name(create_info->db_type->getName());
1124
 
 
1125
 
  error= create_table(session,
1126
 
                      new_table_as_temporary,
1127
 
                      create_info, create_proto, alter_info, true, 0, false);
1128
 
 
 
970
  error= create_temporary_table(session, table, new_db, tmp_name, create_info, create_proto, alter_info);
1129
971
  if (error != 0)
1130
 
  {
1131
 
    return true;
1132
 
  }
 
972
    goto err;
1133
973
 
1134
974
  /* Open the table so we need to copy the data to it. */
1135
 
  Table *new_table= open_alter_table(session, table, new_table_as_temporary);
1136
 
 
1137
 
 
1138
 
  if (not new_table)
1139
 
  {
1140
 
    plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
1141
 
    return true;
1142
 
  }
 
975
  if (table->s->tmp_table)
 
976
  {
 
977
    TableList tbl;
 
978
    tbl.db= new_db;
 
979
    tbl.alias= tmp_name;
 
980
    tbl.table_name= tmp_name;
 
981
 
 
982
    /* Table is in session->temporary_tables */
 
983
    new_table= session->openTable(&tbl, (bool*) 0, DRIZZLE_LOCK_IGNORE_FLUSH);
 
984
  }
 
985
  else
 
986
  {
 
987
    char tmp_path[FN_REFLEN];
 
988
    /* table is a normal table: Create temporary table in same directory */
 
989
    build_table_filename(tmp_path, sizeof(tmp_path), new_db, tmp_name, true);
 
990
    /* Open our intermediate table */
 
991
    new_table= session->open_temporary_table(tmp_path, new_db, tmp_name, 0, OTM_OPEN);
 
992
  }
 
993
 
 
994
  if (new_table == NULL)
 
995
    goto err1;
1143
996
 
1144
997
  /* Copy the data if necessary. */
1145
 
  {
1146
 
    /* We must not ignore bad input! */
1147
 
    session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;    // calc cuted fields
1148
 
    session->cuted_fields= 0L;
1149
 
    session->set_proc_info("copy to tmp table");
1150
 
    copied= deleted= 0;
1151
 
 
1152
 
    /* We don't want update TIMESTAMP fields during ALTER Table. */
1153
 
    new_table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
1154
 
    new_table->next_number_field= new_table->found_next_number_field;
1155
 
    error= copy_data_between_tables(session,
1156
 
                                    table,
1157
 
                                    new_table,
1158
 
                                    alter_info->create_list,
1159
 
                                    ignore,
1160
 
                                    order_num,
1161
 
                                    order,
1162
 
                                    &copied,
1163
 
                                    &deleted,
1164
 
                                    alter_info->keys_onoff,
1165
 
                                    alter_info->error_if_not_empty);
1166
 
 
1167
 
    /* We must not ignore bad input! */
1168
 
    assert(session->count_cuted_fields == CHECK_FIELD_ERROR_FOR_NULL);
1169
 
  }
1170
 
 
1171
 
  /* Now we need to resolve what just happened with the data copy. */
1172
 
 
1173
 
  if (error)
1174
 
  {
1175
 
 
1176
 
    /*
1177
 
      No default value was provided for new fields.
1178
 
    */
1179
 
    if (alter_info->error_if_not_empty && session->row_count)
1180
 
    {
1181
 
      my_error(ER_INVALID_ALTER_TABLE_FOR_NOT_NULL, MYF(0));
1182
 
    }
1183
 
 
1184
 
    if (original_table_identifier.isTmp())
1185
 
    {
1186
 
      if (new_table)
1187
 
      {
1188
 
        /* close_temporary_table() frees the new_table pointer. */
1189
 
        session->close_temporary_table(new_table);
1190
 
      }
1191
 
      else
1192
 
      {
1193
 
        plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
1194
 
      }
1195
 
 
1196
 
      return true;
1197
 
    }
1198
 
    else
1199
 
    {
1200
 
      if (new_table)
1201
 
      {
1202
 
        /*
1203
 
          Close the intermediate table that will be the new table.
1204
 
          Note that MERGE tables do not have their children attached here.
1205
 
        */
1206
 
        new_table->intern_close_table();
1207
 
        if (new_table->hasShare())
1208
 
        {
1209
 
          delete new_table->getMutableShare();
1210
 
        }
1211
 
 
1212
 
        delete new_table;
1213
 
      }
1214
 
 
1215
 
      boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
1216
 
 
1217
 
      plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
1218
 
 
1219
 
      return true;
1220
 
    }
1221
 
  }
1222
 
  // Temporary table and success
1223
 
  else if (original_table_identifier.isTmp())
1224
 
  {
 
998
  session->count_cuted_fields= CHECK_FIELD_WARN;        // calc cuted fields
 
999
  session->cuted_fields= 0L;
 
1000
  session->set_proc_info("copy to tmp table");
 
1001
  copied= deleted= 0;
 
1002
 
 
1003
  assert(new_table);
 
1004
 
 
1005
  /* We don't want update TIMESTAMP fields during ALTER Table. */
 
1006
  new_table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
 
1007
  new_table->next_number_field= new_table->found_next_number_field;
 
1008
  error= copy_data_between_tables(table, 
 
1009
                                  new_table,
 
1010
                                  alter_info->create_list, 
 
1011
                                  ignore,
 
1012
                                  order_num, 
 
1013
                                  order, 
 
1014
                                  &copied, 
 
1015
                                  &deleted,
 
1016
                                  alter_info->keys_onoff,
 
1017
                                  alter_info->error_if_not_empty);
 
1018
 
 
1019
  /* We must not ignore bad input! */
 
1020
  session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
 
1021
 
 
1022
  if (table->s->tmp_table != NO_TMP_TABLE)
 
1023
  {
 
1024
    /* We changed a temporary table */
 
1025
    if (error)
 
1026
      goto err1;
 
1027
 
1225
1028
    /* Close lock if this is a transactional table */
1226
1029
    if (session->lock)
1227
1030
    {
1228
 
      session->unlockTables(session->lock);
 
1031
      mysql_unlock_tables(session, session->lock);
1229
1032
      session->lock= 0;
1230
1033
    }
1231
1034
 
1232
1035
    /* Remove link to old table and rename the new one */
1233
 
    session->close_temporary_table(table);
 
1036
    session->close_temporary_table(table, true, true);
1234
1037
 
1235
1038
    /* Should pass the 'new_name' as we store table name in the cache */
1236
 
    new_table->getMutableShare()->setIdentifier(new_table_identifier);
1237
 
 
1238
 
    new_table_identifier.setPath(new_table_as_temporary.getPath());
1239
 
 
1240
 
    if (rename_table(*session, new_engine, new_table_as_temporary, new_table_identifier) != 0)
1241
 
    {
1242
 
      return true;
1243
 
    }
1244
 
  }
1245
 
  // Normal table success
 
1039
    if (new_table->rename_temporary_table(new_db, new_name))
 
1040
      goto err1;
 
1041
    
 
1042
    goto end_temporary;
 
1043
  }
 
1044
 
 
1045
  if (new_table)
 
1046
  {
 
1047
    /*
 
1048
      Close the intermediate table that will be the new table.
 
1049
      Note that MERGE tables do not have their children attached here.
 
1050
    */
 
1051
    new_table->intern_close_table();
 
1052
    free(new_table);
 
1053
  }
 
1054
 
 
1055
  pthread_mutex_lock(&LOCK_open); /* ALTER TABLE */
 
1056
  
 
1057
  if (error)
 
1058
  {
 
1059
    quick_rm_table(new_db_type, new_db, tmp_name, true);
 
1060
    pthread_mutex_unlock(&LOCK_open);
 
1061
    goto err;
 
1062
  }
 
1063
 
 
1064
  /*
 
1065
    Data is copied. Now we:
 
1066
    1) Wait until all other threads close old version of table.
 
1067
    2) Close instances of table open by this thread and replace them
 
1068
       with exclusive name-locks.
 
1069
    3) Rename the old table to a temp name, rename the new one to the
 
1070
       old name.
 
1071
    4) If we are under LOCK TABLES and don't do ALTER Table ... RENAME
 
1072
       we reopen new version of table.
 
1073
    5) Write statement to the binary log.
 
1074
    6) If we are under LOCK TABLES and do ALTER Table ... RENAME we
 
1075
       remove name-locks from list of open tables and table cache.
 
1076
    7) If we are not not under LOCK TABLES we rely on close_thread_tables()
 
1077
       call to remove name-locks from table cache and list of open table.
 
1078
  */
 
1079
 
 
1080
  session->set_proc_info("rename result table");
 
1081
 
 
1082
  snprintf(old_name, sizeof(old_name), "%s2-%lx-%"PRIx64, TMP_FILE_PREFIX, (unsigned long) current_pid, session->thread_id);
 
1083
 
 
1084
  my_casedn_str(files_charset_info, old_name);
 
1085
 
 
1086
  wait_while_table_is_used(session, table, HA_EXTRA_PREPARE_FOR_RENAME);
 
1087
  session->close_data_files_and_morph_locks(db, table_name);
 
1088
 
 
1089
  error= 0;
 
1090
  save_old_db_type= old_db_type;
 
1091
 
 
1092
  /*
 
1093
    This leads to the storage engine (SE) not being notified for renames in
 
1094
    mysql_rename_table(), because we just juggle with the FRM and nothing
 
1095
    more. If we have an intermediate table, then we notify the SE that
 
1096
    it should become the actual table. Later, we will recycle the old table.
 
1097
    However, in case of ALTER Table RENAME there might be no intermediate
 
1098
    table. This is when the old and new tables are compatible, according to
 
1099
    compare_table(). Then, we need one additional call to
 
1100
    mysql_rename_table() with flag NO_FRM_RENAME, which does nothing else but
 
1101
    actual rename in the SE and the FRM is not touched. Note that, if the
 
1102
    table is renamed and the SE is also changed, then an intermediate table
 
1103
    is created and the additional call will not take place.
 
1104
  */
 
1105
  if (mysql_rename_table(old_db_type, db, table_name, db, old_name, FN_TO_IS_TMP))
 
1106
  {
 
1107
    error= 1;
 
1108
    quick_rm_table(new_db_type, new_db, tmp_name, true);
 
1109
  }
1246
1110
  else
1247
1111
  {
1248
 
    if (new_table)
1249
 
    {
1250
 
      /*
1251
 
        Close the intermediate table that will be the new table.
1252
 
        Note that MERGE tables do not have their children attached here.
1253
 
      */
1254
 
      new_table->intern_close_table();
1255
 
 
1256
 
      if (new_table->hasShare())
1257
 
      {
1258
 
        delete new_table->getMutableShare();
1259
 
      }
1260
 
 
1261
 
      delete new_table;
1262
 
    }
1263
 
 
1264
 
    {
1265
 
      boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex()); /* ALTER TABLE */
1266
 
      /*
1267
 
        Data is copied. Now we:
1268
 
        1) Wait until all other threads close old version of table.
1269
 
        2) Close instances of table open by this thread and replace them
1270
 
        with exclusive name-locks.
1271
 
        3) Rename the old table to a temp name, rename the new one to the
1272
 
        old name.
1273
 
        4) If we are under LOCK TABLES and don't do ALTER Table ... RENAME
1274
 
        we reopen new version of table.
1275
 
        5) Write statement to the binary log.
1276
 
        6) If we are under LOCK TABLES and do ALTER Table ... RENAME we
1277
 
        remove name-locks from list of open tables and table cache.
1278
 
        7) If we are not not under LOCK TABLES we rely on close_thread_tables()
1279
 
        call to remove name-locks from table cache and list of open table.
1280
 
      */
1281
 
 
1282
 
      session->set_proc_info("rename result table");
1283
 
 
1284
 
      snprintf(old_name, sizeof(old_name), "%s2-%lx-%"PRIx64, TMP_FILE_PREFIX, (unsigned long) current_pid, session->thread_id);
1285
 
 
1286
 
      my_casedn_str(files_charset_info, old_name);
1287
 
 
1288
 
      wait_while_table_is_used(session, table, HA_EXTRA_PREPARE_FOR_RENAME);
1289
 
      session->close_data_files_and_morph_locks(original_table_identifier);
1290
 
 
1291
 
      assert(not error);
1292
 
 
1293
 
      /*
1294
 
        This leads to the storage engine (SE) not being notified for renames in
1295
 
        rename_table(), because we just juggle with the FRM and nothing
1296
 
        more. If we have an intermediate table, then we notify the SE that
1297
 
        it should become the actual table. Later, we will recycle the old table.
1298
 
        However, in case of ALTER Table RENAME there might be no intermediate
1299
 
        table. This is when the old and new tables are compatible, according to
1300
 
        compare_table(). Then, we need one additional call to
1301
 
      */
1302
 
      identifier::Table original_table_to_drop(original_table_identifier.getSchemaName(),
1303
 
                                             old_name, create_proto.type() != message::Table::TEMPORARY ? message::Table::INTERNAL :
1304
 
                                             message::Table::TEMPORARY);
1305
 
 
1306
 
      drizzled::error_t rename_error= EE_OK;
1307
 
      if (rename_table(*session, original_engine, original_table_identifier, original_table_to_drop))
1308
 
      {
1309
 
        error= ER_ERROR_ON_RENAME;
1310
 
        plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
1311
 
      }
1312
 
      else
1313
 
      {
1314
 
        if (rename_table(*session, new_engine, new_table_as_temporary, new_table_identifier) != 0)
1315
 
        {
1316
 
          /* Try to get everything back. */
1317
 
          rename_error= ER_ERROR_ON_RENAME;
1318
 
 
1319
 
          plugin::StorageEngine::dropTable(*session, new_table_identifier);
1320
 
 
1321
 
          plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
1322
 
 
1323
 
          rename_table(*session, original_engine, original_table_to_drop, original_table_identifier);
1324
 
        }
1325
 
        else
1326
 
        {
1327
 
          plugin::StorageEngine::dropTable(*session, original_table_to_drop);
1328
 
        }
1329
 
      }
1330
 
 
1331
 
      if (rename_error)
1332
 
      {
1333
 
        /*
1334
 
          An error happened while we were holding exclusive name-lock on table
1335
 
          being altered. To be safe under LOCK TABLES we should remove placeholders
1336
 
          from list of open tables list and table cache.
1337
 
        */
1338
 
        session->unlink_open_table(table);
1339
 
 
1340
 
        return true;
1341
 
      }
1342
 
    }
1343
 
 
1344
 
    session->set_proc_info("end");
1345
 
 
1346
 
    write_bin_log(session, *session->getQueryString());
1347
 
    table_list->table= NULL;
1348
 
  }
1349
 
 
 
1112
    if (mysql_rename_table(new_db_type, new_db, tmp_name, new_db, new_alias, FN_FROM_IS_TMP) != 0)
 
1113
    {
 
1114
      /* Try to get everything back. */
 
1115
      error= 1;
 
1116
      quick_rm_table(new_db_type, new_db, new_alias, false);
 
1117
      quick_rm_table(new_db_type, new_db, tmp_name, true);
 
1118
      mysql_rename_table(old_db_type, db, old_name, db, table_name, FN_FROM_IS_TMP);
 
1119
    }
 
1120
  }
 
1121
 
 
1122
  if (error)
 
1123
  {
 
1124
    /* This shouldn't happen. But let us play it safe. */
 
1125
    goto err_with_placeholders;
 
1126
  }
 
1127
 
 
1128
  quick_rm_table(old_db_type, db, old_name, true);
 
1129
 
 
1130
  pthread_mutex_unlock(&LOCK_open);
 
1131
 
 
1132
  session->set_proc_info("end");
 
1133
 
 
1134
  write_bin_log(session, true, session->query, session->query_length);
 
1135
 
 
1136
  if (old_db_type->check_flag(HTON_BIT_FLUSH_AFTER_RENAME))
 
1137
  {
 
1138
    /*
 
1139
      For the alter table to be properly flushed to the logs, we
 
1140
      have to open the new table.  If not, we get a problem on server
 
1141
      shutdown. But we do not need to attach MERGE children.
 
1142
    */
 
1143
    char table_path[FN_REFLEN];
 
1144
    Table *t_table;
 
1145
    build_table_filename(table_path, sizeof(table_path), new_db, table_name, false);
 
1146
    t_table= session->open_temporary_table(table_path, new_db, tmp_name, false, OTM_OPEN);
 
1147
    if (t_table)
 
1148
    {
 
1149
      t_table->intern_close_table();
 
1150
      free(t_table);
 
1151
    }
 
1152
    else
 
1153
      errmsg_printf(ERRMSG_LVL_WARN, _("Could not open table %s.%s after rename\n"), new_db, table_name);
 
1154
 
 
1155
    ha_flush_logs(old_db_type);
 
1156
  }
 
1157
  table_list->table= NULL;
 
1158
 
 
1159
end_temporary:
1350
1160
  /*
1351
1161
   * Field::store() may have called my_error().  If this is 
1352
1162
   * the case, we must not send an ok packet, since 
1353
1163
   * Diagnostics_area::is_set() will fail an assert.
1354
 
 */
1355
 
  if (session->is_error())
 
1164
   */
 
1165
  if (! session->is_error())
 
1166
  {
 
1167
    snprintf(tmp_name, sizeof(tmp_name), ER(ER_INSERT_INFO),
 
1168
            (ulong) (copied + deleted), (ulong) deleted,
 
1169
            (ulong) session->cuted_fields);
 
1170
    session->my_ok(copied + deleted, 0, 0L, tmp_name);
 
1171
    session->some_tables_deleted=0;
 
1172
    return false;
 
1173
  }
 
1174
  else
1356
1175
  {
1357
1176
    /* my_error() was called.  Return true (which means error...) */
1358
1177
    return true;
1359
1178
  }
1360
1179
 
1361
 
  snprintf(tmp_name, sizeof(tmp_name), ER(ER_INSERT_INFO),
1362
 
           (ulong) (copied + deleted), (ulong) deleted,
1363
 
           (ulong) session->cuted_fields);
1364
 
  session->my_ok(copied + deleted, 0, 0L, tmp_name);
1365
 
  session->some_tables_deleted= false;
1366
 
 
1367
 
  return false;
1368
 
}
1369
 
 
1370
 
bool alter_table(Session *session,
1371
 
                 identifier::Table &original_table_identifier,
1372
 
                 identifier::Table &new_table_identifier,
1373
 
                 HA_CREATE_INFO *create_info,
1374
 
                 const message::Table &original_proto,
1375
 
                 message::Table &create_proto,
1376
 
                 TableList *table_list,
1377
 
                 AlterInfo *alter_info,
1378
 
                 uint32_t order_num,
1379
 
                 Order *order,
1380
 
                 bool ignore)
1381
 
{
1382
 
  bool error;
1383
 
  Table *table;
1384
 
 
1385
 
  if (alter_info->tablespace_op != NO_TABLESPACE_OP)
1386
 
  {
1387
 
    /* DISCARD/IMPORT TABLESPACE is always alone in an ALTER Table */
1388
 
    return discard_or_import_tablespace(session, table_list, alter_info->tablespace_op);
1389
 
  }
1390
 
 
1391
 
  session->set_proc_info("init");
1392
 
 
1393
 
  if (not (table= session->openTableLock(table_list, TL_WRITE_ALLOW_READ)))
1394
 
    return true;
1395
 
 
1396
 
  session->set_proc_info("gained write lock on table");
1397
 
 
1398
 
  /* 
1399
 
    Check that we are not trying to rename to an existing table,
1400
 
    if one existed we get a lock, if we can't we error.
1401
 
  */
1402
 
  {
1403
 
    Table *name_lock= NULL;
1404
 
 
1405
 
    if (not lockTableIfDifferent(*session, original_table_identifier, new_table_identifier, name_lock))
1406
 
    {
1407
 
      return true;
1408
 
    }
1409
 
 
1410
 
    error= internal_alter_table(session,
1411
 
                                table,
1412
 
                                original_table_identifier,
1413
 
                                new_table_identifier,
1414
 
                                create_info,
1415
 
                                original_proto,
1416
 
                                create_proto,
1417
 
                                table_list,
1418
 
                                alter_info,
1419
 
                                order_num,
1420
 
                                order,
1421
 
                                ignore);
1422
 
 
1423
 
    if (name_lock)
1424
 
    {
1425
 
      boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
1426
 
      session->unlink_open_table(name_lock);
1427
 
    }
1428
 
  }
1429
 
 
1430
 
  return error;
1431
 
}
1432
 
/* alter_table */
 
1180
err1:
 
1181
  if (new_table)
 
1182
  {
 
1183
    /* close_temporary_table() frees the new_table pointer. */
 
1184
    session->close_temporary_table(new_table, true, true);
 
1185
  }
 
1186
  else
 
1187
    quick_rm_table(new_db_type, new_db, tmp_name, true);
 
1188
 
 
1189
err:
 
1190
  /*
 
1191
    No default value was provided for a DATE/DATETIME field, the
 
1192
    current sql_mode doesn't allow the '0000-00-00' value and
 
1193
    the table to be altered isn't empty.
 
1194
    Report error here.
 
1195
  */
 
1196
  if (alter_info->error_if_not_empty && session->row_count)
 
1197
  {
 
1198
    const char *f_val= 0;
 
1199
    enum enum_drizzle_timestamp_type t_type= DRIZZLE_TIMESTAMP_DATE;
 
1200
    switch (alter_info->datetime_field->sql_type)
 
1201
    {
 
1202
      case DRIZZLE_TYPE_DATE:
 
1203
        f_val= "0000-00-00";
 
1204
        t_type= DRIZZLE_TIMESTAMP_DATE;
 
1205
        break;
 
1206
      case DRIZZLE_TYPE_DATETIME:
 
1207
        f_val= "0000-00-00 00:00:00";
 
1208
        t_type= DRIZZLE_TIMESTAMP_DATETIME;
 
1209
        break;
 
1210
      default:
 
1211
        /* Shouldn't get here. */
 
1212
        assert(0);
 
1213
    }
 
1214
    bool save_abort_on_warning= session->abort_on_warning;
 
1215
    session->abort_on_warning= true;
 
1216
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
1217
                                 f_val, strlength(f_val), t_type,
 
1218
                                 alter_info->datetime_field->field_name);
 
1219
    session->abort_on_warning= save_abort_on_warning;
 
1220
  }
 
1221
  if (name_lock)
 
1222
  {
 
1223
    pthread_mutex_lock(&LOCK_open); /* ALTER TABLe */
 
1224
    session->unlink_open_table(name_lock);
 
1225
    pthread_mutex_unlock(&LOCK_open);
 
1226
  }
 
1227
  return true;
 
1228
 
 
1229
err_with_placeholders:
 
1230
  /*
 
1231
    An error happened while we were holding exclusive name-lock on table
 
1232
    being altered. To be safe under LOCK TABLES we should remove placeholders
 
1233
    from list of open tables list and table cache.
 
1234
  */
 
1235
  session->unlink_open_table(table);
 
1236
  if (name_lock)
 
1237
    session->unlink_open_table(name_lock);
 
1238
  pthread_mutex_unlock(&LOCK_open);
 
1239
  return true;
 
1240
}
 
1241
/* mysql_alter_table */
1433
1242
 
1434
1243
static int
1435
 
copy_data_between_tables(Session *session,
1436
 
                         Table *from, Table *to,
1437
 
                         List<CreateField> &create,
 
1244
copy_data_between_tables(Table *from,Table *to,
 
1245
                         List<CreateField> &create,
1438
1246
                         bool ignore,
1439
 
                         uint32_t order_num, Order *order,
1440
 
                         ha_rows *copied,
1441
 
                         ha_rows *deleted,
 
1247
                         uint32_t order_num, order_st *order,
 
1248
                         ha_rows *copied,
 
1249
                         ha_rows *deleted,
1442
1250
                         enum enum_enable_or_disable keys_onoff,
1443
1251
                         bool error_if_not_empty)
1444
1252
{
1445
 
  int error= 0;
 
1253
  int error;
1446
1254
  CopyField *copy,*copy_end;
1447
1255
  ulong found_count,delete_count;
 
1256
  Session *session= current_session;
1448
1257
  uint32_t length= 0;
1449
 
  SortField *sortorder;
1450
 
  ReadRecord info;
 
1258
  SORT_FIELD *sortorder;
 
1259
  READ_RECORD info;
1451
1260
  TableList   tables;
1452
1261
  List<Item>   fields;
1453
1262
  List<Item>   all_fields;
1461
1270
 
1462
1271
    This needs to be done before external_lock
1463
1272
  */
1464
 
  TransactionServices &transaction_services= TransactionServices::singleton();
1465
 
 
1466
 
  /* 
1467
 
   * LP Bug #552420 
1468
 
   *
1469
 
   * Since open_temporary_table() doesn't invoke lockTables(), we
1470
 
   * don't get the usual automatic call to StorageEngine::startStatement(), so
1471
 
   * we manually call it here...
1472
 
   */
1473
 
  to->getMutableShare()->getEngine()->startStatement(session);
1474
 
 
1475
 
  if (!(copy= new CopyField[to->getShare()->sizeFields()]))
1476
 
    return -1;
1477
 
 
1478
 
  if (to->cursor->ha_external_lock(session, F_WRLCK))
 
1273
  error= ha_enable_transaction(session, false);
 
1274
  if (error)
 
1275
    return -1;
 
1276
 
 
1277
  if (!(copy= new CopyField[to->s->fields]))
 
1278
    return -1;
 
1279
 
 
1280
  if (to->file->ha_external_lock(session, F_WRLCK))
1479
1281
    return -1;
1480
1282
 
1481
1283
  /* We need external lock before we can disable/enable keys */
1482
 
  alter_table_manage_keys(session, to, from->cursor->indexes_are_disabled(), keys_onoff);
 
1284
  alter_table_manage_keys(to, from->file->indexes_are_disabled(), keys_onoff);
1483
1285
 
1484
1286
  /* We can abort alter table for any table type */
1485
 
  session->setAbortOnWarning(not ignore);
1486
 
 
1487
 
  from->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
1488
 
  to->cursor->ha_start_bulk_insert(from->cursor->stats.records);
1489
 
 
1490
 
  List<CreateField>::iterator it(create.begin());
 
1287
  session->abort_on_warning= !ignore;
 
1288
 
 
1289
  from->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
1290
  to->file->ha_start_bulk_insert(from->file->stats.records);
 
1291
 
 
1292
  List_iterator<CreateField> it(create);
1491
1293
  CreateField *def;
1492
 
  copy_end= copy;
1493
 
  for (Field **ptr= to->getFields(); *ptr ; ptr++)
 
1294
  copy_end=copy;
 
1295
  for (Field **ptr=to->field ; *ptr ; ptr++)
1494
1296
  {
1495
1297
    def=it++;
1496
1298
    if (def->field)
1505
1307
 
1506
1308
  found_count=delete_count=0;
1507
1309
 
1508
 
  do
1509
 
  {
1510
 
    if (order)
1511
 
    {
1512
 
      if (to->getShare()->hasPrimaryKey() && to->cursor->primary_key_is_clustered())
1513
 
      {
1514
 
        char warn_buff[DRIZZLE_ERRMSG_SIZE];
1515
 
        snprintf(warn_buff, sizeof(warn_buff),
1516
 
                 _("order_st BY ignored because there is a user-defined clustered "
1517
 
                   "index in the table '%-.192s'"),
1518
 
                 from->getMutableShare()->getTableName());
1519
 
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
1520
 
                     warn_buff);
1521
 
      }
 
1310
  if (order)
 
1311
  {
 
1312
    if (to->s->primary_key != MAX_KEY && to->file->primary_key_is_clustered())
 
1313
    {
 
1314
      char warn_buff[DRIZZLE_ERRMSG_SIZE];
 
1315
      snprintf(warn_buff, sizeof(warn_buff),
 
1316
               _("order_st BY ignored because there is a user-defined clustered "
 
1317
                 "index in the table '%-.192s'"),
 
1318
               from->s->table_name.str);
 
1319
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
 
1320
                   warn_buff);
 
1321
    }
 
1322
    else
 
1323
    {
 
1324
      from->sort.io_cache= new IO_CACHE;
 
1325
      memset(from->sort.io_cache, 0, sizeof(IO_CACHE));
 
1326
 
 
1327
      memset(&tables, 0, sizeof(tables));
 
1328
      tables.table= from;
 
1329
      tables.alias= tables.table_name= from->s->table_name.str;
 
1330
      tables.db= from->s->db.str;
 
1331
      error= 1;
 
1332
 
 
1333
      if (session->lex->select_lex.setup_ref_array(session, order_num) ||
 
1334
          setup_order(session, session->lex->select_lex.ref_pointer_array,
 
1335
                      &tables, fields, all_fields, order) ||
 
1336
          !(sortorder= make_unireg_sortorder(order, &length, NULL)) ||
 
1337
          (from->sort.found_records= filesort(session, from, sortorder, length,
 
1338
                                              (SQL_SELECT *) 0, HA_POS_ERROR,
 
1339
                                              1, &examined_rows)) ==
 
1340
          HA_POS_ERROR)
 
1341
        goto err;
 
1342
    }
 
1343
  };
 
1344
 
 
1345
  /* Tell handler that we have values for all columns in the to table */
 
1346
  to->use_all_columns();
 
1347
  init_read_record(&info, session, from, (SQL_SELECT *) 0, 1,1);
 
1348
  if (ignore)
 
1349
    to->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
 
1350
  session->row_count= 0;
 
1351
  to->restoreRecordAsDefault();        // Create empty record
 
1352
  while (!(error=info.read_record(&info)))
 
1353
  {
 
1354
    if (session->killed)
 
1355
    {
 
1356
      session->send_kill_message();
 
1357
      error= 1;
 
1358
      break;
 
1359
    }
 
1360
    session->row_count++;
 
1361
    /* Return error if source table isn't empty. */
 
1362
    if (error_if_not_empty)
 
1363
    {
 
1364
      error= 1;
 
1365
      break;
 
1366
    }
 
1367
    if (to->next_number_field)
 
1368
    {
 
1369
      if (auto_increment_field_copied)
 
1370
        to->auto_increment_field_not_null= true;
1522
1371
      else
1523
 
      {
1524
 
        FileSort filesort(*session);
1525
 
        from->sort.io_cache= new internal::IO_CACHE;
1526
 
 
1527
 
        tables.table= from;
1528
 
        tables.setTableName(from->getMutableShare()->getTableName());
1529
 
        tables.alias= tables.getTableName();
1530
 
        tables.setSchemaName(const_cast<char *>(from->getMutableShare()->getSchemaName()));
1531
 
        error= 1;
1532
 
 
1533
 
        if (session->getLex()->select_lex.setup_ref_array(session, order_num) ||
1534
 
            setup_order(session, session->getLex()->select_lex.ref_pointer_array,
1535
 
                        &tables, fields, all_fields, order) ||
1536
 
            !(sortorder= make_unireg_sortorder(order, &length, NULL)) ||
1537
 
            (from->sort.found_records= filesort.run(from, sortorder, length,
1538
 
                                                    (optimizer::SqlSelect *) 0, HA_POS_ERROR,
1539
 
                                                    1, examined_rows)) == HA_POS_ERROR)
1540
 
        {
1541
 
          break;
1542
 
        }
1543
 
      }
1544
 
    }
1545
 
 
1546
 
    /* Tell handler that we have values for all columns in the to table */
1547
 
    to->use_all_columns();
1548
 
 
1549
 
    error= info.init_read_record(session, from, (optimizer::SqlSelect *) 0, 1, true);
 
1372
        to->next_number_field->reset();
 
1373
    }
 
1374
 
 
1375
    for (CopyField *copy_ptr=copy ; copy_ptr != copy_end ; copy_ptr++)
 
1376
    {
 
1377
      copy_ptr->do_copy(copy_ptr);
 
1378
    }
 
1379
    prev_insert_id= to->file->next_insert_id;
 
1380
    error=to->file->ha_write_row(to->record[0]);
 
1381
    to->auto_increment_field_not_null= false;
1550
1382
    if (error)
1551
1383
    {
1552
 
      to->print_error(errno, MYF(0));
1553
 
 
1554
 
      break;
1555
 
    }
1556
 
 
1557
 
    if (ignore)
1558
 
    {
1559
 
      to->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1560
 
    }
1561
 
 
1562
 
    session->row_count= 0;
1563
 
    to->restoreRecordAsDefault();        // Create empty record
1564
 
    while (not (error=info.read_record(&info)))
1565
 
    {
1566
 
      if (session->getKilled())
1567
 
      {
1568
 
        session->send_kill_message();
1569
 
        error= 1;
1570
 
        break;
1571
 
      }
1572
 
      session->row_count++;
1573
 
      /* Return error if source table isn't empty. */
1574
 
      if (error_if_not_empty)
1575
 
      {
1576
 
        error= 1;
1577
 
        break;
1578
 
      }
1579
 
      if (to->next_number_field)
1580
 
      {
1581
 
        if (auto_increment_field_copied)
1582
 
          to->auto_increment_field_not_null= true;
1583
 
        else
1584
 
          to->next_number_field->reset();
1585
 
      }
1586
 
 
1587
 
      for (CopyField *copy_ptr= copy; copy_ptr != copy_end ; copy_ptr++)
1588
 
      {
1589
 
        if (not copy->to_field->hasDefault() and copy->from_null_ptr and  *copy->from_null_ptr & copy->from_bit)
1590
 
        {
1591
 
          copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
1592
 
                                      ER_WARN_DATA_TRUNCATED, 1);
1593
 
          copy->to_field->reset();
1594
 
          error= 1;
1595
 
          break;
1596
 
        }
1597
 
 
1598
 
        copy_ptr->do_copy(copy_ptr);
1599
 
      }
1600
 
 
1601
 
      if (error)
1602
 
      {
1603
 
        break;
1604
 
      }
1605
 
 
1606
 
      prev_insert_id= to->cursor->next_insert_id;
1607
 
      error= to->cursor->insertRecord(to->record[0]);
1608
 
      to->auto_increment_field_not_null= false;
1609
 
 
1610
 
      if (error)
1611
 
      { 
1612
 
        if (!ignore || to->cursor->is_fatal_error(error, HA_CHECK_DUP))
1613
 
        { 
1614
 
          to->print_error(error, MYF(0));
1615
 
          break;
1616
 
        }
1617
 
        to->cursor->restore_auto_increment(prev_insert_id);
1618
 
        delete_count++;
1619
 
      }
1620
 
      else
1621
 
      {
1622
 
        found_count++;
1623
 
      }
1624
 
    }
1625
 
 
1626
 
    info.end_read_record();
1627
 
    from->free_io_cache();
1628
 
    delete [] copy;                             // This is never 0
1629
 
 
1630
 
    if (to->cursor->ha_end_bulk_insert() && error <= 0)
1631
 
    {
1632
 
      to->print_error(errno, MYF(0));
1633
 
      error= 1;
1634
 
    }
1635
 
    to->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1636
 
 
1637
 
    /*
1638
 
      Ensure that the new table is saved properly to disk so that we
1639
 
      can do a rename
1640
 
    */
1641
 
    if (transaction_services.autocommitOrRollback(*session, false))
1642
 
      error= 1;
1643
 
 
1644
 
    if (not session->endActiveTransaction())
1645
 
      error= 1;
1646
 
 
1647
 
  } while (0);
1648
 
 
1649
 
  session->setAbortOnWarning(false);
 
1384
      if (!ignore ||
 
1385
          to->file->is_fatal_error(error, HA_CHECK_DUP))
 
1386
      {
 
1387
         if (!to->file->is_fatal_error(error, HA_CHECK_DUP))
 
1388
         {
 
1389
           uint32_t key_nr= to->file->get_dup_key(error);
 
1390
           if ((int) key_nr >= 0)
 
1391
           {
 
1392
             const char *err_msg= ER(ER_DUP_ENTRY_WITH_KEY_NAME);
 
1393
             if (key_nr == 0 &&
 
1394
                 (to->key_info[0].key_part[0].field->flags &
 
1395
                  AUTO_INCREMENT_FLAG))
 
1396
               err_msg= ER(ER_DUP_ENTRY_AUTOINCREMENT_CASE);
 
1397
             to->file->print_keydup_error(key_nr, err_msg);
 
1398
             break;
 
1399
           }
 
1400
         }
 
1401
 
 
1402
        to->file->print_error(error,MYF(0));
 
1403
        break;
 
1404
      }
 
1405
      to->file->restore_auto_increment(prev_insert_id);
 
1406
      delete_count++;
 
1407
    }
 
1408
    else
 
1409
      found_count++;
 
1410
  }
 
1411
  end_read_record(&info);
 
1412
  from->free_io_cache();
 
1413
  delete [] copy;                               // This is never 0
 
1414
 
 
1415
  if (to->file->ha_end_bulk_insert() && error <= 0)
 
1416
  {
 
1417
    to->file->print_error(my_errno,MYF(0));
 
1418
    error=1;
 
1419
  }
 
1420
  to->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
 
1421
 
 
1422
  if (ha_enable_transaction(session, true))
 
1423
  {
 
1424
    error= 1;
 
1425
    goto err;
 
1426
  }
 
1427
 
 
1428
  /*
 
1429
    Ensure that the new table is saved properly to disk so that we
 
1430
    can do a rename
 
1431
  */
 
1432
  if (ha_autocommit_or_rollback(session, 0))
 
1433
    error=1;
 
1434
  if (! session->endActiveTransaction())
 
1435
    error=1;
 
1436
 
 
1437
 err:
 
1438
  session->abort_on_warning= 0;
1650
1439
  from->free_io_cache();
1651
1440
  *copied= found_count;
1652
1441
  *deleted=delete_count;
1653
 
  to->cursor->ha_release_auto_increment();
1654
 
 
1655
 
  if (to->cursor->ha_external_lock(session, F_UNLCK))
1656
 
  {
 
1442
  to->file->ha_release_auto_increment();
 
1443
  if (to->file->ha_external_lock(session,F_UNLCK))
1657
1444
    error=1;
1658
 
  }
1659
 
 
1660
1445
  return(error > 0 ? -1 : 0);
1661
1446
}
1662
1447
 
1663
 
static Table *open_alter_table(Session *session, Table *table, identifier::Table &identifier)
1664
 
{
1665
 
  Table *new_table;
1666
 
 
1667
 
  /* Open the table so we need to copy the data to it. */
1668
 
  if (table->getShare()->getType())
1669
 
  {
1670
 
    TableList tbl;
1671
 
    tbl.setSchemaName(const_cast<char *>(identifier.getSchemaName().c_str()));
1672
 
    tbl.alias= const_cast<char *>(identifier.getTableName().c_str());
1673
 
    tbl.setTableName(const_cast<char *>(identifier.getTableName().c_str()));
1674
 
 
1675
 
    /* Table is in session->temporary_tables */
1676
 
    new_table= session->openTable(&tbl, (bool*) 0, DRIZZLE_LOCK_IGNORE_FLUSH);
1677
 
  }
 
1448
static int
 
1449
create_temporary_table(Session *session,
 
1450
                       Table *table,
 
1451
                       char *new_db,
 
1452
                       char *tmp_name,
 
1453
                       HA_CREATE_INFO *create_info,
 
1454
                       message::Table *create_proto,
 
1455
                       AlterInfo *alter_info)
 
1456
{
 
1457
  int error;
 
1458
  plugin::StorageEngine *old_db_type, *new_db_type;
 
1459
  old_db_type= table->s->db_type();
 
1460
  new_db_type= create_info->db_type;
 
1461
  /*
 
1462
    Create a table with a temporary name.
 
1463
    We don't log the statement, it will be logged later.
 
1464
  */
 
1465
  create_proto->set_name(tmp_name);
 
1466
  create_proto->set_type(message::Table::TEMPORARY);
 
1467
 
 
1468
  message::Table::StorageEngine *protoengine;
 
1469
  protoengine= create_proto->mutable_engine();
 
1470
  protoengine->set_name(new_db_type->getName());
 
1471
 
 
1472
  error= mysql_create_table(session, new_db, tmp_name,
 
1473
                            create_info, create_proto, alter_info, 1, 0);
 
1474
 
 
1475
  return(error);
 
1476
}
 
1477
 
 
1478
/** @TODO This will soon die. */
 
1479
bool mysql_create_like_schema_frm(Session* session,
 
1480
                                  TableList* schema_table,
 
1481
                                  HA_CREATE_INFO *create_info,
 
1482
                                  message::Table* table_proto)
 
1483
{
 
1484
  HA_CREATE_INFO local_create_info;
 
1485
  AlterInfo alter_info;
 
1486
  bool tmp_table= (create_info->options & HA_LEX_CREATE_TMP_TABLE);
 
1487
  uint32_t keys= schema_table->table->s->keys;
 
1488
  uint32_t db_options= 0;
 
1489
 
 
1490
  memset(&local_create_info, 0, sizeof(local_create_info));
 
1491
  local_create_info.db_type= schema_table->table->s->db_type();
 
1492
  local_create_info.row_type= schema_table->table->s->row_type;
 
1493
  local_create_info.default_table_charset=default_charset_info;
 
1494
  alter_info.flags.set(ALTER_CHANGE_COLUMN);
 
1495
  alter_info.flags.set(ALTER_RECREATE);
 
1496
  schema_table->table->use_all_columns();
 
1497
  if (mysql_prepare_alter_table(session, schema_table->table,
 
1498
                                &local_create_info, table_proto, &alter_info))
 
1499
    return true;
 
1500
 
 
1501
  /* I_S tables are created with MAX_ROWS for some efficiency drive.
 
1502
     When CREATE LIKE, we don't want to keep it coming across */
 
1503
  message::Table::TableOptions *table_options;
 
1504
  table_options= table_proto->mutable_options();
 
1505
  table_options->clear_max_rows();
 
1506
 
 
1507
  if (mysql_prepare_create_table(session, &local_create_info, &alter_info,
 
1508
                                 tmp_table, &db_options,
 
1509
                                 schema_table->table->file,
 
1510
                                 &schema_table->table->s->key_info, &keys, 0))
 
1511
    return true;
 
1512
 
 
1513
  table_proto->set_name("system_stupid_i_s_fix_nonsense");
 
1514
  if(tmp_table)
 
1515
    table_proto->set_type(message::Table::TEMPORARY);
1678
1516
  else
 
1517
    table_proto->set_type(message::Table::STANDARD);
 
1518
 
1679
1519
  {
1680
 
    /* Open our intermediate table */
1681
 
    new_table= session->open_temporary_table(identifier, false);
 
1520
    message::Table::StorageEngine *protoengine;
 
1521
    protoengine= table_proto->mutable_engine();
 
1522
 
 
1523
    plugin::StorageEngine *engine= local_create_info.db_type;
 
1524
 
 
1525
    protoengine->set_name(engine->getName());
1682
1526
  }
1683
1527
 
1684
 
  return new_table;
 
1528
  if (fill_table_proto(table_proto, "system_stupid_i_s_fix_nonsense",
 
1529
                       alter_info.create_list, &local_create_info,
 
1530
                       keys, schema_table->table->s->key_info))
 
1531
    return true;
 
1532
 
 
1533
  return false;
1685
1534
}
1686
 
 
1687
 
} /* namespace drizzled */