~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/alter_table.cc

Merge Andrew - Enable boost filesystem requirement and fix linking requirements for it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
 
45
45
#include "drizzled/transaction_services.h"
46
46
 
47
 
#include "drizzled/filesort.h"
48
 
 
49
 
#include "drizzled/message.h"
50
 
 
51
47
using namespace std;
52
48
 
53
49
namespace drizzled
60
56
                                    List<CreateField> &create,
61
57
                                    bool ignore,
62
58
                                    uint32_t order_num,
63
 
                                    Order *order,
 
59
                                    order_st *order,
64
60
                                    ha_rows *copied,
65
61
                                    ha_rows *deleted,
66
62
                                    enum enum_enable_or_disable keys_onoff,
107
103
  assert(select_lex->db);
108
104
 
109
105
  /* Chicken/Egg... we need to search for the table, to know if the table exists, so we can build a full identifier from it */
110
 
  message::table::shared_ptr original_table_message;
 
106
  message::Table original_table_message;
111
107
  {
112
 
    TableIdentifier identifier(first_table->getSchemaName(), first_table->getTableName());
 
108
    TableIdentifier identifier(first_table->db, first_table->table_name);
113
109
    if (plugin::StorageEngine::getTableDefinition(*session, identifier, original_table_message) != EEXIST)
114
110
    {
115
 
      std::string path;
116
 
      identifier.getSQLPath(path);
117
 
      my_error(ER_BAD_TABLE_ERROR, MYF(0), path.c_str());
 
111
      my_error(ER_BAD_TABLE_ERROR, MYF(0), identifier.getSQLPath().c_str());
118
112
      return true;
119
113
    }
120
114
 
121
115
    if (not  create_info.db_type)
122
116
    {
123
117
      create_info.db_type= 
124
 
        plugin::StorageEngine::findByName(*session, original_table_message->engine().name());
 
118
        plugin::StorageEngine::findByName(*session, original_table_message.engine().name());
125
119
 
126
120
      if (not create_info.db_type)
127
121
      {
128
 
        std::string path;
129
 
        identifier.getSQLPath(path);
130
 
        my_error(ER_BAD_TABLE_ERROR, MYF(0), path.c_str());
 
122
        my_error(ER_BAD_TABLE_ERROR, MYF(0), identifier.getSQLPath().c_str());
131
123
        return true;
132
124
      }
133
125
    }
134
126
  }
135
127
 
136
128
  if (not validateCreateTableOption())
 
129
  {
137
130
    return true;
 
131
  }
138
132
 
139
133
  /* ALTER TABLE ends previous transaction */
140
134
  if (not session->endActiveTransaction())
 
135
  {
141
136
    return true;
 
137
  }
142
138
 
143
 
  if (not (need_start_waiting= not session->wait_if_global_read_lock(0, 1)))
 
139
  if (not (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
 
140
  {
144
141
    return true;
 
142
  }
145
143
 
146
144
  bool res;
147
 
  if (original_table_message->type() == message::Table::STANDARD )
 
145
  if (original_table_message.type() == message::Table::STANDARD )
148
146
  {
149
 
    TableIdentifier identifier(first_table->getSchemaName(), first_table->getTableName());
150
 
    TableIdentifier new_identifier(select_lex->db ? select_lex->db : first_table->getSchemaName(),
151
 
                                   session->lex->name.str ? session->lex->name.str : first_table->getTableName());
 
147
    TableIdentifier identifier(first_table->db, first_table->table_name);
 
148
    TableIdentifier new_identifier(select_lex->db ? select_lex->db : first_table->db,
 
149
                                   session->lex->name.str ? session->lex->name.str : first_table->table_name);
152
150
 
153
151
    res= alter_table(session, 
154
152
                     identifier,
155
153
                     new_identifier,
156
154
                     &create_info,
157
 
                     *original_table_message,
 
155
                     original_table_message,
158
156
                     create_table_message,
159
157
                     first_table,
160
158
                     &alter_info,
161
159
                     select_lex->order_list.elements,
162
 
                     (Order *) select_lex->order_list.first,
 
160
                     (order_st *) select_lex->order_list.first,
163
161
                     session->lex->ignore);
164
162
  }
165
163
  else
166
164
  {
167
 
    TableIdentifier catch22(first_table->getSchemaName(), first_table->getTableName());
168
 
    Table *table= session->find_temporary_table(catch22);
 
165
    Table *table= session->find_temporary_table(first_table);
169
166
    assert(table);
170
167
    {
171
 
      TableIdentifier identifier(first_table->getSchemaName(), first_table->getTableName(), table->getMutableShare()->getPath());
172
 
      TableIdentifier new_identifier(select_lex->db ? select_lex->db : first_table->getSchemaName(),
173
 
                                     session->lex->name.str ? session->lex->name.str : first_table->getTableName(),
 
168
      TableIdentifier identifier(first_table->db, first_table->table_name, table->getMutableShare()->getPath());
 
169
      TableIdentifier new_identifier(select_lex->db ? select_lex->db : first_table->db,
 
170
                                     session->lex->name.str ? session->lex->name.str : first_table->table_name,
174
171
                                     table->getMutableShare()->getPath());
175
172
 
176
173
      res= alter_table(session, 
177
174
                       identifier,
178
175
                       new_identifier,
179
176
                       &create_info,
180
 
                       *original_table_message,
 
177
                       original_table_message,
181
178
                       create_table_message,
182
179
                       first_table,
183
180
                       &alter_info,
184
181
                       select_lex->order_list.elements,
185
 
                       (Order *) select_lex->order_list.first,
 
182
                       (order_st *) select_lex->order_list.first,
186
183
                       session->lex->ignore);
187
184
    }
188
185
  }
191
188
     Release the protection against the global read lock and wake
192
189
     everyone, who might want to set a global read lock.
193
190
   */
194
 
  session->startWaitingGlobalReadLock();
195
 
 
 
191
  start_waiting_global_read_lock(session);
196
192
  return res;
197
193
}
198
194
 
347
343
        if (def->sql_type == DRIZZLE_TYPE_BLOB)
348
344
        {
349
345
          my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0), def->change);
350
 
          return true;
 
346
                goto err;
351
347
        }
352
348
        if ((def->def= alter->def))
353
349
        {
355
351
          def->flags&= ~NO_DEFAULT_VALUE_FLAG;
356
352
        }
357
353
        else
358
 
        {
359
354
          def->flags|= NO_DEFAULT_VALUE_FLAG;
360
 
        }
361
355
        alter_it.remove();
362
356
      }
363
357
    }
368
362
    if (def->change && ! def->field)
369
363
    {
370
364
      my_error(ER_BAD_FIELD_ERROR, MYF(0), def->change, table->getMutableShare()->getTableName());
371
 
      return true;
 
365
      goto err;
372
366
    }
373
367
    /*
374
 
      If we have been given a field which has no default value, and is not null then we need to bail.
 
368
      Check that the DATE/DATETIME not null field we are going to add is
 
369
      either has a default value or the '0000-00-00' is allowed by the
 
370
      set sql mode.
 
371
      If the '0000-00-00' value isn't allowed then raise the error_if_not_empty
 
372
      flag to allow ALTER Table only if the table to be altered is empty.
375
373
    */
376
 
    if (not (~def->flags & (NO_DEFAULT_VALUE_FLAG | NOT_NULL_FLAG)) and not def->change)
 
374
    if ((def->sql_type == DRIZZLE_TYPE_DATE ||
 
375
         def->sql_type == DRIZZLE_TYPE_DATETIME) &&
 
376
        ! alter_info->datetime_field &&
 
377
        ! (~def->flags & (NO_DEFAULT_VALUE_FLAG | NOT_NULL_FLAG)))
377
378
    {
 
379
      alter_info->datetime_field= def;
378
380
      alter_info->error_if_not_empty= true;
379
381
    }
380
382
    if (! def->after)
381
 
    {
382
383
      new_create_list.push_back(def);
383
 
    }
384
384
    else if (def->after == first_keyword)
385
 
    {
386
385
      new_create_list.push_front(def);
387
 
    }
388
386
    else
389
387
    {
390
388
      CreateField *find;
397
395
      if (! find)
398
396
      {
399
397
        my_error(ER_BAD_FIELD_ERROR, MYF(0), def->after, table->getMutableShare()->getTableName());
400
 
        return true;
 
398
        goto err;
401
399
      }
402
400
      find_it.after(def); /* Put element after this */
403
401
      /*
412
410
      */
413
411
      if (alter_info->build_method == HA_BUILD_ONLINE)
414
412
      {
415
 
        my_error(ER_NOT_SUPPORTED_YET, MYF(0), session->getQueryString()->c_str());
416
 
        return true;
 
413
        my_error(ER_NOT_SUPPORTED_YET, MYF(0), session->query.c_str());
 
414
        goto err;
417
415
      }
418
416
      alter_info->build_method= HA_BUILD_OFFLINE;
419
417
    }
424
422
             MYF(0),
425
423
             alter_info->alter_list.head()->name,
426
424
             table->getMutableShare()->getTableName());
427
 
    return true;
 
425
    goto err;
428
426
  }
429
427
  if (! new_create_list.elements)
430
428
  {
431
429
    my_message(ER_CANT_REMOVE_ALL_FIELDS,
432
430
               ER(ER_CANT_REMOVE_ALL_FIELDS),
433
431
               MYF(0));
434
 
    return true;
 
432
    goto err;
435
433
  }
436
434
 
437
435
  /*
568
566
      if (key->type == Key::FOREIGN_KEY)
569
567
      {
570
568
        if (((Foreign_key *)key)->validate(new_create_list))
571
 
        {
572
 
          return true;
573
 
        }
 
569
          goto err;
574
570
 
575
571
        Foreign_key *fkey= (Foreign_key*)key;
576
572
        add_foreign_key_to_table_message(&table_message,
591
587
        my_error(ER_WRONG_NAME_FOR_INDEX,
592
588
                 MYF(0),
593
589
                 key->name.str);
594
 
        return true;
 
590
        goto err;
595
591
      }
596
592
    }
597
593
  }
618
614
    my_error(ER_CANT_DROP_FIELD_OR_KEY,
619
615
             MYF(0),
620
616
             alter_info->drop_list.head()->name);
621
 
    return true;
 
617
    goto err;
622
618
  }
623
619
  if (alter_info->alter_list.elements)
624
620
  {
625
621
    my_error(ER_CANT_DROP_FIELD_OR_KEY,
626
622
             MYF(0),
627
623
             alter_info->alter_list.head()->name);
628
 
    return true;
 
624
    goto err;
629
625
  }
630
626
 
631
627
  if (not table_message.options().has_comment()
638
634
  }
639
635
 
640
636
  table_message.set_creation_timestamp(table->getShare()->getTableProto()->creation_timestamp());
641
 
  table_message.set_version(table->getShare()->getTableProto()->version());
642
 
  table_message.set_uuid(table->getShare()->getTableProto()->uuid());
 
637
 
 
638
  table_message.set_update_timestamp(time(NULL));
643
639
 
644
640
  rc= false;
645
641
  alter_info->create_list.swap(new_create_list);
646
642
  alter_info->key_list.swap(new_key_list);
 
643
err:
647
644
 
648
645
  size_t num_engine_options= table_message.engine().options_size();
649
646
  size_t original_num_engine_options= original_proto.engine().options_size();
668
665
    }
669
666
  }
670
667
 
671
 
  drizzled::message::update(table_message);
672
 
 
673
 
  return false;
 
668
  return rc;
674
669
}
675
670
 
676
671
/* table_list should contain just one table */
712
707
 
713
708
  /* The ALTER Table is always in its own transaction */
714
709
  error= transaction_services.autocommitOrRollback(session, false);
715
 
  if (not session->endActiveTransaction())
 
710
  if (! session->endActiveTransaction())
716
711
    error=1;
717
 
 
718
712
  if (error)
719
713
    goto err;
720
 
 
721
 
  write_bin_log(session, *session->getQueryString());
 
714
  write_bin_log(session, session->query.c_str());
722
715
 
723
716
err:
724
717
  (void) transaction_services.autocommitOrRollback(session, error);
791
784
 
792
785
      if (session.find_temporary_table(new_table_identifier))
793
786
      {
794
 
        std::string path;
795
 
        new_table_identifier.getSQLPath(path);
796
 
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
 
787
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_table_identifier.getSQLPath().c_str());
797
788
        return false;
798
789
      }
799
790
    }
806
797
 
807
798
      if (not name_lock)
808
799
      {
809
 
        std::string path;
810
 
        new_table_identifier.getSQLPath(path);
811
 
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
 
800
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_table_identifier.getSQLPath().c_str());
812
801
        return false;
813
802
      }
814
803
 
815
804
      if (plugin::StorageEngine::doesTableExist(session, new_table_identifier))
816
805
      {
817
 
        std::string path;
818
 
        new_table_identifier.getSQLPath(path);
819
 
 
820
806
        /* Table will be closed by Session::executeCommand() */
821
 
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
 
807
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_table_identifier.getSQLPath().c_str());
822
808
 
823
 
        table::Cache::singleton().mutex().lock(); /* ALTER TABLe */
 
809
        LOCK_open.lock(); /* ALTER TABLe */
824
810
        session.unlink_open_table(name_lock);
825
 
        table::Cache::singleton().mutex().unlock();
 
811
        LOCK_open.unlock();
826
812
 
827
813
        return false;
828
814
      }
884
870
                                 TableList *table_list,
885
871
                                 AlterInfo *alter_info,
886
872
                                 uint32_t order_num,
887
 
                                 Order *order,
 
873
                                 order_st *order,
888
874
                                 bool ignore)
889
875
{
890
876
  int error= 0;
893
879
  ha_rows copied= 0;
894
880
  ha_rows deleted= 0;
895
881
 
896
 
  if (not original_table_identifier.isValid())
897
 
    return true;
898
 
 
899
 
  if (not new_table_identifier.isValid())
900
 
    return true;
901
 
 
902
882
  session->set_proc_info("init");
903
883
 
904
884
  table->use_all_columns();
934
914
  if (original_engine->check_flag(HTON_BIT_ALTER_NOT_SUPPORTED) ||
935
915
      new_engine->check_flag(HTON_BIT_ALTER_NOT_SUPPORTED))
936
916
  {
937
 
    std::string path;
938
 
    new_table_identifier.getSQLPath(path);
939
 
    my_error(ER_ILLEGAL_HA, MYF(0), path.c_str());
 
917
    my_error(ER_ILLEGAL_HA, MYF(0), new_table_identifier.getSQLPath().c_str());
940
918
 
941
919
    return true;
942
920
  }
970
948
          while the fact that the table is still open gives us protection
971
949
          from concurrent DDL statements.
972
950
        */
973
 
        table::Cache::singleton().mutex().lock(); /* DDL wait for/blocker */
 
951
        LOCK_open.lock(); /* DDL wait for/blocker */
974
952
        wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
975
 
        table::Cache::singleton().mutex().unlock();
 
953
        LOCK_open.unlock();
976
954
        error= table->cursor->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
977
955
        /* COND_refresh will be signaled in close_thread_tables() */
978
956
        break;
979
957
      case DISABLE:
980
 
        table::Cache::singleton().mutex().lock(); /* DDL wait for/blocker */
 
958
        LOCK_open.lock(); /* DDL wait for/blocker */
981
959
        wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
982
 
        table::Cache::singleton().mutex().unlock();
 
960
        LOCK_open.unlock();
983
961
        error=table->cursor->ha_disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
984
962
        /* COND_refresh will be signaled in close_thread_tables() */
985
963
        break;
997
975
                            table->getAlias());
998
976
      }
999
977
 
1000
 
      table::Cache::singleton().mutex().lock(); /* Lock to remove all instances of table from table cache before ALTER */
 
978
      LOCK_open.lock(); /* Lock to remove all instances of table from table cache before ALTER */
1001
979
      /*
1002
980
        Unlike to the above case close_cached_table() below will remove ALL
1003
981
        instances of Table from table cache (it will also remove table lock
1004
982
        held by this thread). So to make actual table renaming and writing
1005
983
        to binlog atomic we have to put them into the same critical section
1006
 
        protected by table::Cache::singleton().mutex() mutex. This also removes gap for races between
 
984
        protected by LOCK_open mutex. This also removes gap for races between
1007
985
        access() and mysql_rename_table() calls.
1008
986
      */
1009
987
 
1025
1003
        */
1026
1004
        if (plugin::StorageEngine::doesTableExist(*session, new_table_identifier))
1027
1005
        {
1028
 
          std::string path;
1029
 
          new_table_identifier.getSQLPath(path);
1030
 
          my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
 
1006
          my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_table_identifier.getSQLPath().c_str());
1031
1007
          error= -1;
1032
1008
        }
1033
1009
        else
1049
1025
 
1050
1026
      if (error == 0)
1051
1027
      {
1052
 
        TransactionServices &transaction_services= TransactionServices::singleton();
1053
 
        transaction_services.allocateNewTransactionId();
1054
 
        write_bin_log(session, *session->getQueryString());
 
1028
        write_bin_log(session, session->query.c_str());
1055
1029
        session->my_ok();
1056
1030
      }
1057
1031
      else if (error > 0)
1060
1034
        error= -1;
1061
1035
      }
1062
1036
 
1063
 
      table::Cache::singleton().mutex().unlock();
 
1037
      LOCK_open.unlock();
1064
1038
      table_list->table= NULL;
1065
1039
 
1066
1040
      return error;
1105
1079
 
1106
1080
  if (not new_table)
1107
1081
  {
1108
 
    plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
 
1082
    quick_rm_table(*session, new_table_as_temporary);
1109
1083
    return true;
1110
1084
  }
1111
1085
 
1149
1123
    */
1150
1124
    if (alter_info->error_if_not_empty && session->row_count)
1151
1125
    {
1152
 
      my_error(ER_INVALID_ALTER_TABLE_FOR_NOT_NULL, MYF(0));
 
1126
      const char *f_val= 0;
 
1127
      enum enum_drizzle_timestamp_type t_type= DRIZZLE_TIMESTAMP_DATE;
 
1128
 
 
1129
      switch (alter_info->datetime_field->sql_type)
 
1130
      {
 
1131
      case DRIZZLE_TYPE_DATE:
 
1132
        f_val= "0000-00-00";
 
1133
        t_type= DRIZZLE_TIMESTAMP_DATE;
 
1134
        break;
 
1135
      case DRIZZLE_TYPE_DATETIME:
 
1136
        f_val= "0000-00-00 00:00:00";
 
1137
        t_type= DRIZZLE_TIMESTAMP_DATETIME;
 
1138
        break;
 
1139
      default:
 
1140
        /* Shouldn't get here. */
 
1141
        assert(0);
 
1142
      }
 
1143
      bool save_abort_on_warning= session->abort_on_warning;
 
1144
      session->abort_on_warning= true;
 
1145
      make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
1146
                                   f_val, internal::strlength(f_val), t_type,
 
1147
                                   alter_info->datetime_field->field_name);
 
1148
      session->abort_on_warning= save_abort_on_warning;
1153
1149
    }
1154
1150
 
1155
1151
    if (original_table_identifier.isTmp())
1161
1157
      }
1162
1158
      else
1163
1159
      {
1164
 
        plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
 
1160
        quick_rm_table(*session, new_table_as_temporary);
1165
1161
      }
1166
1162
 
1167
1163
      return true;
1177
1173
        new_table->intern_close_table();
1178
1174
        if (new_table->hasShare())
1179
1175
        {
1180
 
          delete new_table->getMutableShare();
 
1176
          delete new_table->s;
 
1177
          new_table->s= NULL;
1181
1178
        }
1182
1179
 
1183
1180
        delete new_table;
1184
1181
      }
1185
1182
 
1186
 
      table::Cache::singleton().mutex().lock(); /* ALTER TABLE */
 
1183
      LOCK_open.lock(); /* ALTER TABLE */
1187
1184
 
1188
 
      plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
1189
 
      table::Cache::singleton().mutex().unlock();
 
1185
      quick_rm_table(*session, new_table_as_temporary);
 
1186
      LOCK_open.unlock();
1190
1187
 
1191
1188
      return true;
1192
1189
    }
1197
1194
    /* Close lock if this is a transactional table */
1198
1195
    if (session->lock)
1199
1196
    {
1200
 
      session->unlockTables(session->lock);
 
1197
      mysql_unlock_tables(session, session->lock);
1201
1198
      session->lock= 0;
1202
1199
    }
1203
1200
 
1227
1224
 
1228
1225
      if (new_table->hasShare())
1229
1226
      {
1230
 
        delete new_table->getMutableShare();
 
1227
        delete new_table->s;
 
1228
        new_table->s= NULL;
1231
1229
      }
1232
1230
 
1233
1231
      delete new_table;
1234
1232
    }
1235
1233
 
1236
 
    table::Cache::singleton().mutex().lock(); /* ALTER TABLE */
 
1234
    LOCK_open.lock(); /* ALTER TABLE */
1237
1235
 
1238
1236
    /*
1239
1237
      Data is copied. Now we:
1278
1276
    if (mysql_rename_table(*session, original_engine, original_table_identifier, original_table_to_drop))
1279
1277
    {
1280
1278
      error= 1;
1281
 
      plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
 
1279
      quick_rm_table(*session, new_table_as_temporary);
1282
1280
    }
1283
1281
    else
1284
1282
    {
1287
1285
        /* Try to get everything back. */
1288
1286
        error= 1;
1289
1287
 
1290
 
        plugin::StorageEngine::dropTable(*session, new_table_identifier);
 
1288
        quick_rm_table(*session, new_table_identifier);
1291
1289
 
1292
 
        plugin::StorageEngine::dropTable(*session, new_table_as_temporary);
 
1290
        quick_rm_table(*session, new_table_as_temporary);
1293
1291
 
1294
1292
        mysql_rename_table(*session, original_engine, original_table_to_drop, original_table_identifier);
1295
1293
      }
1296
1294
      else
1297
1295
      {
1298
 
        plugin::StorageEngine::dropTable(*session, original_table_to_drop);
 
1296
        quick_rm_table(*session, original_table_to_drop);
1299
1297
      }
1300
1298
    }
1301
1299
 
1307
1305
        from list of open tables list and table cache.
1308
1306
      */
1309
1307
      session->unlink_open_table(table);
1310
 
      table::Cache::singleton().mutex().unlock();
 
1308
      LOCK_open.unlock();
1311
1309
 
1312
1310
      return true;
1313
1311
    }
1314
1312
 
1315
 
    table::Cache::singleton().mutex().unlock();
 
1313
    LOCK_open.unlock();
1316
1314
 
1317
1315
    session->set_proc_info("end");
1318
1316
 
1319
 
    write_bin_log(session, *session->getQueryString());
 
1317
    write_bin_log(session, session->query.c_str());
1320
1318
    table_list->table= NULL;
1321
1319
  }
1322
1320
 
1349
1347
                 TableList *table_list,
1350
1348
                 AlterInfo *alter_info,
1351
1349
                 uint32_t order_num,
1352
 
                 Order *order,
 
1350
                 order_st *order,
1353
1351
                 bool ignore)
1354
1352
{
1355
1353
  bool error;
1395
1393
 
1396
1394
    if (name_lock)
1397
1395
    {
1398
 
      table::Cache::singleton().mutex().lock(); /* ALTER TABLe */
 
1396
      LOCK_open.lock(); /* ALTER TABLe */
1399
1397
      session->unlink_open_table(name_lock);
1400
 
      table::Cache::singleton().mutex().unlock();
 
1398
      LOCK_open.unlock();
1401
1399
    }
1402
1400
  }
1403
1401
 
1410
1408
                         Table *from, Table *to,
1411
1409
                         List<CreateField> &create,
1412
1410
                         bool ignore,
1413
 
                         uint32_t order_num, Order *order,
 
1411
                         uint32_t order_num, order_st *order,
1414
1412
                         ha_rows *copied,
1415
1413
                         ha_rows *deleted,
1416
1414
                         enum enum_enable_or_disable keys_onoff,
1440
1438
  /* 
1441
1439
   * LP Bug #552420 
1442
1440
   *
1443
 
   * Since open_temporary_table() doesn't invoke lockTables(), we
 
1441
   * Since open_temporary_table() doesn't invoke mysql_lock_tables(), we
1444
1442
   * don't get the usual automatic call to StorageEngine::startStatement(), so
1445
1443
   * we manually call it here...
1446
1444
   */
1447
 
  to->getMutableShare()->getEngine()->startStatement(session);
 
1445
  to->s->getEngine()->startStatement(session);
1448
1446
 
1449
1447
  if (!(copy= new CopyField[to->getShare()->sizeFields()]))
1450
1448
    return -1;
1493
1491
    }
1494
1492
    else
1495
1493
    {
1496
 
      FileSort filesort(*session);
1497
1494
      from->sort.io_cache= new internal::IO_CACHE;
1498
1495
 
1499
1496
      memset(&tables, 0, sizeof(tables));
1500
1497
      tables.table= from;
1501
 
      tables.setTableName(const_cast<char *>(from->getMutableShare()->getTableName()));
1502
 
      tables.alias= const_cast<char *>(tables.getTableName());
1503
 
      tables.setSchemaName(const_cast<char *>(from->getMutableShare()->getSchemaName()));
 
1498
      tables.alias= tables.table_name= const_cast<char *>(from->getMutableShare()->getTableName());
 
1499
      tables.db= const_cast<char *>(from->getMutableShare()->getSchemaName());
1504
1500
      error= 1;
1505
1501
 
1506
1502
      if (session->lex->select_lex.setup_ref_array(session, order_num) ||
1507
1503
          setup_order(session, session->lex->select_lex.ref_pointer_array,
1508
1504
                      &tables, fields, all_fields, order) ||
1509
1505
          !(sortorder= make_unireg_sortorder(order, &length, NULL)) ||
1510
 
          (from->sort.found_records= filesort.run(from, sortorder, length,
1511
 
                                                  (optimizer::SqlSelect *) 0, HA_POS_ERROR,
1512
 
                                                  1, examined_rows)) == HA_POS_ERROR)
 
1506
          (from->sort.found_records= filesort(session, from, sortorder, length,
 
1507
                                              (optimizer::SqlSelect *) 0, HA_POS_ERROR,
 
1508
                                              1, &examined_rows)) ==
 
1509
          HA_POS_ERROR)
1513
1510
      {
1514
1511
        goto err;
1515
1512
      }
1525
1522
  to->restoreRecordAsDefault();        // Create empty record
1526
1523
  while (!(error=info.read_record(&info)))
1527
1524
  {
1528
 
    if (session->getKilled())
 
1525
    if (session->killed)
1529
1526
    {
1530
1527
      session->send_kill_message();
1531
1528
      error= 1;
1546
1543
        to->next_number_field->reset();
1547
1544
    }
1548
1545
 
1549
 
    for (CopyField *copy_ptr= copy; copy_ptr != copy_end ; copy_ptr++)
 
1546
    for (CopyField *copy_ptr=copy ; copy_ptr != copy_end ; copy_ptr++)
1550
1547
    {
1551
 
      if (not copy->to_field->hasDefault() and copy->from_null_ptr and  *copy->from_null_ptr & copy->from_bit)
1552
 
      {
1553
 
        copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
1554
 
                                    ER_WARN_DATA_TRUNCATED, 1);
1555
 
        copy->to_field->reset();
1556
 
        error= 1;
1557
 
        break;
1558
 
      }
1559
 
 
1560
1548
      copy_ptr->do_copy(copy_ptr);
1561
1549
    }
1562
 
 
1563
 
    if (error)
1564
 
    {
1565
 
      break;
1566
 
    }
1567
 
 
1568
1550
    prev_insert_id= to->cursor->next_insert_id;
1569
1551
    error= to->cursor->insertRecord(to->record[0]);
1570
1552
    to->auto_increment_field_not_null= false;
1571
1553
 
1572
1554
    if (error)
1573
1555
    { 
1574
 
      if (!ignore || to->cursor->is_fatal_error(error, HA_CHECK_DUP))
 
1556
      if (!ignore ||
 
1557
          to->cursor->is_fatal_error(error, HA_CHECK_DUP))
1575
1558
      { 
1576
1559
        to->print_error(error, MYF(0));
1577
1560
        break;
1649
1632
  if (table->getShare()->getType())
1650
1633
  {
1651
1634
    TableList tbl;
1652
 
    tbl.setSchemaName(const_cast<char *>(identifier.getSchemaName().c_str()));
 
1635
    tbl.db= const_cast<char *>(identifier.getSchemaName().c_str());
1653
1636
    tbl.alias= const_cast<char *>(identifier.getTableName().c_str());
1654
 
    tbl.setTableName(const_cast<char *>(identifier.getTableName().c_str()));
 
1637
    tbl.table_name= const_cast<char *>(identifier.getTableName().c_str());
1655
1638
 
1656
1639
    /* Table is in session->temporary_tables */
1657
1640
    new_table= session->openTable(&tbl, (bool*) 0, DRIZZLE_LOCK_IGNORE_FLUSH);