~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/alter_table.cc

Remove PLUGIN and MODULES.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include <drizzled/server_includes.h>
22
 
#include <drizzled/show.h>
23
 
#include <drizzled/lock.h>
24
 
#include <drizzled/session.h>
25
 
#include <drizzled/statement/alter_table.h>
 
21
#include "config.h"
 
22
 
 
23
#include <fcntl.h>
 
24
 
 
25
#include <sstream>
 
26
 
 
27
#include "drizzled/show.h"
 
28
#include "drizzled/lock.h"
 
29
#include "drizzled/session.h"
 
30
#include "drizzled/statement/alter_table.h"
 
31
#include "drizzled/global_charset_info.h"
 
32
 
26
33
 
27
34
#include "drizzled/gettext.h"
28
35
#include "drizzled/data_home.h"
29
36
#include "drizzled/sql_table.h"
30
37
#include "drizzled/table_proto.h"
31
38
#include "drizzled/plugin/info_schema_table.h"
 
39
#include "drizzled/optimizer/range.h"
 
40
#include "drizzled/time_functions.h"
 
41
#include "drizzled/records.h"
 
42
#include "drizzled/pthread_globals.h"
 
43
#include "drizzled/internal/my_sys.h"
 
44
#include "drizzled/internal/iocache.h"
 
45
 
 
46
extern pid_t current_pid;
32
47
 
33
48
using namespace std;
34
49
 
53
68
 
54
69
static int create_temporary_table(Session *session,
55
70
                                  Table *table,
56
 
                                  char *new_db,
57
 
                                  char *tmp_name,
 
71
                                  TableIdentifier &identifier,
58
72
                                  HA_CREATE_INFO *create_info,
59
73
                                  message::Table *create_proto,
60
74
                                  AlterInfo *alter_info);
974
988
  alter_info->build_method= HA_BUILD_OFFLINE;
975
989
 
976
990
  snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%"PRIx64, TMP_FILE_PREFIX, (unsigned long) current_pid, session->thread_id);
977
 
 
 
991
 
978
992
  /* Safety fix for innodb */
979
993
  my_casedn_str(files_charset_info, tmp_name);
980
994
 
981
995
  /* Create a temporary table with the new format */
982
 
  error= create_temporary_table(session, table, new_db, tmp_name, create_info, create_proto, alter_info);
983
 
  if (error != 0)
984
 
    goto err;
 
996
  {
 
997
    /**
 
998
      @note we make an internal temporary table unless the table is a temporary table. In last
 
999
      case we just use it as is. Neither of these tables require locks in order to  be
 
1000
      filled.
 
1001
    */
 
1002
    TableIdentifier new_table_temp(new_db,
 
1003
                                   tmp_name,
 
1004
                                   create_proto->type() != message::Table::TEMPORARY ? INTERNAL_TMP_TABLE :
 
1005
                                   TEMP_TABLE);
 
1006
 
 
1007
    error= create_temporary_table(session, table, new_table_temp, create_info, create_proto, alter_info);
 
1008
 
 
1009
    if (error != 0)
 
1010
      goto err;
 
1011
  }
985
1012
 
986
1013
  /* Open the table so we need to copy the data to it. */
987
1014
  new_table= open_alter_table(session, table, new_db, tmp_name);
1000
1027
  /* We don't want update TIMESTAMP fields during ALTER Table. */
1001
1028
  new_table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
1002
1029
  new_table->next_number_field= new_table->found_next_number_field;
1003
 
  error= copy_data_between_tables(table, 
 
1030
  error= copy_data_between_tables(table,
1004
1031
                                  new_table,
1005
 
                                  alter_info->create_list, 
 
1032
                                  alter_info->create_list,
1006
1033
                                  ignore,
1007
 
                                  order_num, 
1008
 
                                  order, 
1009
 
                                  &copied, 
 
1034
                                  order_num,
 
1035
                                  order,
 
1036
                                  &copied,
1010
1037
                                  &deleted,
1011
1038
                                  alter_info->keys_onoff,
1012
1039
                                  alter_info->error_if_not_empty);
1033
1060
    /* Should pass the 'new_name' as we store table name in the cache */
1034
1061
    if (new_table->rename_temporary_table(new_db, new_name))
1035
1062
      goto err1;
1036
 
    
 
1063
 
1037
1064
    goto end_temporary;
1038
1065
  }
1039
1066
 
1048
1075
  }
1049
1076
 
1050
1077
  pthread_mutex_lock(&LOCK_open); /* ALTER TABLE */
1051
 
  
 
1078
 
1052
1079
  if (error)
1053
1080
  {
1054
 
    quick_rm_table(*session, new_db, tmp_name, true);
 
1081
    TableIdentifier identifier(new_db, tmp_name, INTERNAL_TMP_TABLE);
 
1082
    quick_rm_table(*session, identifier);
1055
1083
    pthread_mutex_unlock(&LOCK_open);
1056
1084
    goto err;
1057
1085
  }
1096
1124
  if (mysql_rename_table(old_db_type, db, table_name, db, old_name, FN_TO_IS_TMP))
1097
1125
  {
1098
1126
    error= 1;
1099
 
    quick_rm_table(*session, new_db, tmp_name, true);
 
1127
    TableIdentifier identifier(new_db, tmp_name, INTERNAL_TMP_TABLE);
 
1128
    quick_rm_table(*session, identifier);
1100
1129
  }
1101
1130
  else
1102
1131
  {
1104
1133
    {
1105
1134
      /* Try to get everything back. */
1106
1135
      error= 1;
1107
 
      quick_rm_table(*session, new_db, new_alias, false);
1108
 
      quick_rm_table(*session, new_db, tmp_name, true);
 
1136
 
 
1137
      TableIdentifier alias_identifier(new_db, new_alias, NO_TMP_TABLE);
 
1138
      quick_rm_table(*session, alias_identifier);
 
1139
 
 
1140
      TableIdentifier tmp_identifier(new_db, tmp_name, INTERNAL_TMP_TABLE);
 
1141
      quick_rm_table(*session, tmp_identifier);
 
1142
 
1109
1143
      mysql_rename_table(old_db_type, db, old_name, db, table_name, FN_FROM_IS_TMP);
1110
1144
    }
1111
1145
  }
1116
1150
    goto err_with_placeholders;
1117
1151
  }
1118
1152
 
1119
 
  quick_rm_table(*session, db, old_name, true);
 
1153
  {
 
1154
    TableIdentifier old_identifier(db, old_name, INTERNAL_TMP_TABLE);
 
1155
    quick_rm_table(*session, old_identifier);
 
1156
  }
 
1157
  
1120
1158
 
1121
1159
  pthread_mutex_unlock(&LOCK_open);
1122
1160
 
1153
1191
    session->close_temporary_table(new_table);
1154
1192
  }
1155
1193
  else
1156
 
    quick_rm_table(*session, new_db, tmp_name, true);
 
1194
  {
 
1195
    TableIdentifier tmp_identifier(new_db, tmp_name, INTERNAL_TMP_TABLE);
 
1196
    quick_rm_table(*session, tmp_identifier);
 
1197
  }
1157
1198
 
1158
1199
err:
1159
1200
  /*
1304
1345
                      &tables, fields, all_fields, order) ||
1305
1346
          !(sortorder= make_unireg_sortorder(order, &length, NULL)) ||
1306
1347
          (from->sort.found_records= filesort(session, from, sortorder, length,
1307
 
                                              (SQL_SELECT *) 0, HA_POS_ERROR,
 
1348
                                              (optimizer::SqlSelect *) 0, HA_POS_ERROR,
1308
1349
                                              1, &examined_rows)) ==
1309
1350
          HA_POS_ERROR)
1310
1351
        goto err;
1313
1354
 
1314
1355
  /* Tell handler that we have values for all columns in the to table */
1315
1356
  to->use_all_columns();
1316
 
  init_read_record(&info, session, from, (SQL_SELECT *) 0, 1,1);
 
1357
  init_read_record(&info, session, from, (optimizer::SqlSelect *) 0, 1,1);
1317
1358
  if (ignore)
1318
1359
    to->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1319
1360
  session->row_count= 0;
1368
1409
 
1369
1410
  if (to->cursor->ha_end_bulk_insert() && error <= 0)
1370
1411
  {
1371
 
    to->print_error(my_errno, MYF(0));
 
1412
    to->print_error(errno, MYF(0));
1372
1413
    error=1;
1373
1414
  }
1374
1415
  to->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1402
1443
static int
1403
1444
create_temporary_table(Session *session,
1404
1445
                       Table *table,
1405
 
                       char *new_db,
1406
 
                       char *tmp_name,
 
1446
                       TableIdentifier &identifier,
1407
1447
                       HA_CREATE_INFO *create_info,
1408
1448
                       message::Table *create_proto,
1409
1449
                       AlterInfo *alter_info)
1410
1450
{
1411
1451
  int error;
1412
1452
  plugin::StorageEngine *old_db_type, *new_db_type;
1413
 
  TableIdentifier identifier(new_db,
1414
 
                             tmp_name,
1415
 
                             create_proto->type() != message::Table::TEMPORARY ? INTERNAL_TMP_TABLE :
1416
 
                             create_info->db_type->check_flag(HTON_BIT_DOES_TRANSACTIONS) ? TRANSACTIONAL_TMP_TABLE :
1417
 
                             NON_TRANSACTIONAL_TMP_TABLE );
1418
1453
 
1419
1454
  old_db_type= table->s->db_type();
1420
1455
  new_db_type= create_info->db_type;
1423
1458
    Create a table with a temporary name.
1424
1459
    We don't log the statement, it will be logged later.
1425
1460
  */
1426
 
  create_proto->set_name(tmp_name);
 
1461
  create_proto->set_name(identifier.getTableName());
1427
1462
 
1428
1463
  message::Table::StorageEngine *protoengine;
1429
1464
  protoengine= create_proto->mutable_engine();