~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/alter_table.cc

  • Committer: Brian Aker
  • Date: 2009-11-26 18:50:02 UTC
  • mfrom: (1226.1.4 push)
  • Revision ID: brian@gaz-20091126185002-se908a2ceq9ub2rn
Mege of TableIdentifier gran patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
                                    ha_rows *deleted,
44
44
                                    enum enum_enable_or_disable keys_onoff,
45
45
                                    bool error_if_not_empty);
 
46
 
46
47
static bool mysql_prepare_alter_table(Session *session,
47
48
                                      Table *table,
48
49
                                      HA_CREATE_INFO *create_info,
49
50
                                      message::Table *table_proto,
50
51
                                      AlterInfo *alter_info);
 
52
 
51
53
static int create_temporary_table(Session *session,
52
54
                                  Table *table,
53
55
                                  char *new_db,
56
58
                                  message::Table *create_proto,
57
59
                                  AlterInfo *alter_info);
58
60
 
 
61
static Table *open_alter_table(Session *session, Table *table, char *db, char *table_name);
 
62
 
59
63
bool statement::AlterTable::execute()
60
64
{
61
65
  TableList *first_table= (TableList *) session->lex->select_lex.table_list.first;
676
680
  char *table_name;
677
681
  char *db;
678
682
  const char *new_alias;
679
 
  char path[FN_REFLEN];
680
683
  ha_rows copied= 0;
681
684
  ha_rows deleted= 0;
682
685
  plugin::StorageEngine *old_db_type;
710
713
    return mysql_discard_or_import_tablespace(session, table_list, alter_info->tablespace_op);
711
714
  }
712
715
 
713
 
  build_table_filename(path, sizeof(path), db, table_name, false);
714
 
 
715
716
  ostringstream oss;
716
717
  oss << drizzle_data_home << "/" << db << "/" << table_name;
717
718
 
977
978
    goto err;
978
979
 
979
980
  /* Open the table so we need to copy the data to it. */
980
 
  if (table->s->tmp_table)
981
 
  {
982
 
    TableList tbl;
983
 
    tbl.db= new_db;
984
 
    tbl.alias= tmp_name;
985
 
    tbl.table_name= tmp_name;
986
 
 
987
 
    /* Table is in session->temporary_tables */
988
 
    new_table= session->openTable(&tbl, (bool*) 0, DRIZZLE_LOCK_IGNORE_FLUSH);
989
 
  }
990
 
  else
991
 
  {
992
 
    char tmp_path[FN_REFLEN];
993
 
    /* table is a normal table: Create temporary table in same directory */
994
 
    build_table_filename(tmp_path, sizeof(tmp_path), new_db, tmp_name, true);
995
 
    /* Open our intermediate table */
996
 
    new_table= session->open_temporary_table(tmp_path, new_db, tmp_name, false);
997
 
  }
 
981
  new_table= open_alter_table(session, table, new_db, tmp_name);
998
982
 
999
983
  if (new_table == NULL)
1000
984
    goto err1;
1102
1086
    However, in case of ALTER Table RENAME there might be no intermediate
1103
1087
    table. This is when the old and new tables are compatible, according to
1104
1088
    compare_table(). Then, we need one additional call to
1105
 
    mysql_rename_table() with flag NO_FRM_RENAME, which does nothing else but
1106
 
    actual rename in the SE and the FRM is not touched. Note that, if the
1107
 
    table is renamed and the SE is also changed, then an intermediate table
1108
 
    is created and the additional call will not take place.
1109
1089
  */
1110
1090
  if (mysql_rename_table(old_db_type, db, table_name, db, old_name, FN_TO_IS_TMP))
1111
1091
  {
1424
1404
{
1425
1405
  int error;
1426
1406
  plugin::StorageEngine *old_db_type, *new_db_type;
 
1407
  TableIdentifier identifier(new_db,
 
1408
                             tmp_name,
 
1409
                             create_proto->type() != message::Table::TEMPORARY ? INTERNAL_TMP_TABLE :
 
1410
                             create_info->db_type->check_flag(HTON_BIT_DOES_TRANSACTIONS) ? TRANSACTIONAL_TMP_TABLE :
 
1411
                             NON_TRANSACTIONAL_TMP_TABLE );
 
1412
 
1427
1413
  old_db_type= table->s->db_type();
1428
1414
  new_db_type= create_info->db_type;
 
1415
 
1429
1416
  /*
1430
1417
    Create a table with a temporary name.
1431
1418
    We don't log the statement, it will be logged later.
1436
1423
  protoengine= create_proto->mutable_engine();
1437
1424
  protoengine->set_name(new_db_type->getName());
1438
1425
 
1439
 
  error= mysql_create_table(session, new_db, tmp_name,
 
1426
  error= mysql_create_table(session,
 
1427
                            identifier,
1440
1428
                            create_info, create_proto, alter_info, true, 0, false);
1441
1429
 
1442
1430
  return error;
1496
1484
  return false;
1497
1485
}
1498
1486
 
 
1487
 
 
1488
static Table *open_alter_table(Session *session, Table *table, char *db, char *table_name)
 
1489
{
 
1490
  Table *new_table;
 
1491
 
 
1492
  /* Open the table so we need to copy the data to it. */
 
1493
  if (table->s->tmp_table)
 
1494
  {
 
1495
    TableList tbl;
 
1496
    tbl.db= db;
 
1497
    tbl.alias= table_name;
 
1498
    tbl.table_name= table_name;
 
1499
 
 
1500
    /* Table is in session->temporary_tables */
 
1501
    new_table= session->openTable(&tbl, (bool*) 0, DRIZZLE_LOCK_IGNORE_FLUSH);
 
1502
  }
 
1503
  else
 
1504
  {
 
1505
    TableIdentifier new_identifier(db, table_name, INTERNAL_TMP_TABLE);
 
1506
 
 
1507
    /* Open our intermediate table */
 
1508
    new_table= session->open_temporary_table(new_identifier, false);
 
1509
  }
 
1510
 
 
1511
  return new_table;
 
1512
}
 
1513
 
1499
1514
} /* namespace drizzled */