~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/alter_table.cc

  • Committer: tdavies
  • Date: 2010-10-15 05:42:42 UTC
  • mto: (1849.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1850.
  • Revision ID: tdavies@molly-20101015054242-pwurc94i9m0w1guh
File:drizzled/internal/my_static.h; Converted struct 'st_irem' to c++ class and renamed it 'irem'. Also added constructor, and made the changed name consistant in my_static.cc

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