~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/alter_table.cc

  • Committer: Eric Day
  • Date: 2009-10-31 21:53:33 UTC
  • mfrom: (1200 staging)
  • mto: This revision was merged to the branch mainline in revision 1202.
  • Revision ID: eday@oddments.org-20091031215333-j94bjoanwmi68p6f
Merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include "drizzled/sql_table.h"
30
30
#include "drizzled/table_proto.h"
31
31
 
32
 
using namespace drizzled;
 
32
using namespace std;
 
33
 
 
34
namespace drizzled
 
35
{
33
36
 
34
37
static int copy_data_between_tables(Table *from,Table *to,
35
38
                                    List<CreateField> &create,
65
68
  {
66
69
 
67
70
    create_info.db_type= 
68
 
      plugin::StorageEngine::findByName(session, create_table_proto.engine().name());
 
71
      plugin::StorageEngine::findByName(*session, create_table_proto.engine().name());
69
72
 
70
73
    if (create_info.db_type == NULL)
71
74
    {
90
93
    return true;
91
94
  }
92
95
 
93
 
  bool res= mysql_alter_table(session, 
94
 
                              select_lex->db, 
95
 
                              session->lex->name.str,
96
 
                              &create_info,
97
 
                              &create_table_proto,
98
 
                              first_table,
99
 
                              &alter_info,
100
 
                              select_lex->order_list.elements,
101
 
                              (order_st *) select_lex->order_list.first,
102
 
                              session->lex->ignore);
 
96
  bool res= alter_table(session, 
 
97
                        select_lex->db, 
 
98
                        session->lex->name.str,
 
99
                        &create_info,
 
100
                        &create_table_proto,
 
101
                        first_table,
 
102
                        &alter_info,
 
103
                        select_lex->order_list.elements,
 
104
                        (order_st *) select_lex->order_list.first,
 
105
                        session->lex->ignore);
103
106
  /*
104
107
     Release the protection against the global read lock and wake
105
108
     everyone, who might want to set a global read lock.
108
111
  return res;
109
112
}
110
113
 
 
114
 
111
115
/**
112
116
  Prepare column and key definitions for CREATE TABLE in ALTER Table.
113
117
 
184
188
    table->file->info(HA_STATUS_AUTO);
185
189
    create_info->auto_increment_value= table->file->stats.auto_increment_value;
186
190
  }
187
 
  if (! (used_fields & HA_CREATE_USED_KEY_BLOCK_SIZE))
188
 
    create_info->key_block_size= table->s->key_block_size;
 
191
  if (! (used_fields & HA_CREATE_USED_KEY_BLOCK_SIZE)
 
192
      && table->s->hasKeyBlockSize())
 
193
    table_options->set_key_block_size(table->s->getKeyBlockSize());
 
194
 
 
195
  if ((used_fields & HA_CREATE_USED_KEY_BLOCK_SIZE)
 
196
      && table_options->key_block_size() == 0)
 
197
    table_options->clear_key_block_size();
189
198
 
190
199
  table->restoreRecordAsDefault(); /* Empty record for DEFAULT */
191
200
  CreateField *def;
606
615
  Alter table
607
616
 
608
617
  SYNOPSIS
609
 
    mysql_alter_table()
 
618
    alter_table()
610
619
      session              Thread handle
611
620
      new_db           If there is a RENAME clause
612
621
      new_name         If there is a RENAME clause
627
636
    When the ALTER Table statement just does a RENAME or ENABLE|DISABLE KEYS,
628
637
    or both, then this function short cuts its operation by renaming
629
638
    the table and/or enabling/disabling the keys. In this case, the FRM is
630
 
    not changed, directly by mysql_alter_table. However, if there is a
 
639
    not changed, directly by alter_table. However, if there is a
631
640
    RENAME + change of a field, or an index, the short cut is not used.
632
641
    See how `create_list` is used to generate the new FRM regarding the
633
642
    structure of the fields. The same is done for the indices of the table.
643
652
    false  OK
644
653
    true   Error
645
654
*/
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)
 
655
bool alter_table(Session *session,
 
656
                 char *new_db,
 
657
                 char *new_name,
 
658
                 HA_CREATE_INFO *create_info,
 
659
                 message::Table *create_proto,
 
660
                 TableList *table_list,
 
661
                 AlterInfo *alter_info,
 
662
                 uint32_t order_num,
 
663
                 order_st *order,
 
664
                 bool ignore)
656
665
{
657
666
  Table *table;
658
667
  Table *new_table= NULL;
769
778
 
770
779
        build_table_filename(new_name_buff, sizeof(new_name_buff), new_db, new_name_buff, false);
771
780
 
772
 
        if (plugin::StorageEngine::getTableProto(new_name_buff, NULL) == EEXIST)
 
781
        if (plugin::StorageEngine::getTableDefinition(*session, new_name_buff, new_db, new_name_buff, false) == EEXIST)
773
782
        {
774
783
          /* Table will be closed by Session::executeCommand() */
775
784
          my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
896
905
        we don't take this name-lock and where this order really matters.
897
906
        TODO: Investigate if we need this access() check at all.
898
907
      */
899
 
      if (plugin::StorageEngine::getTableProto(new_name, NULL) == EEXIST)
 
908
      if (plugin::StorageEngine::getTableDefinition(*session, new_name, db, table_name, false) == EEXIST)
900
909
      {
901
910
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name);
902
911
        error= -1;
1054
1063
  
1055
1064
  if (error)
1056
1065
  {
1057
 
    quick_rm_table(new_db_type, new_db, tmp_name, true);
 
1066
    quick_rm_table(*session, new_db, tmp_name, true);
1058
1067
    pthread_mutex_unlock(&LOCK_open);
1059
1068
    goto err;
1060
1069
  }
1103
1112
  if (mysql_rename_table(old_db_type, db, table_name, db, old_name, FN_TO_IS_TMP))
1104
1113
  {
1105
1114
    error= 1;
1106
 
    quick_rm_table(new_db_type, new_db, tmp_name, true);
 
1115
    quick_rm_table(*session, new_db, tmp_name, true);
1107
1116
  }
1108
1117
  else
1109
1118
  {
1111
1120
    {
1112
1121
      /* Try to get everything back. */
1113
1122
      error= 1;
1114
 
      quick_rm_table(new_db_type, new_db, new_alias, false);
1115
 
      quick_rm_table(new_db_type, new_db, tmp_name, true);
 
1123
      quick_rm_table(*session, new_db, new_alias, false);
 
1124
      quick_rm_table(*session, new_db, tmp_name, true);
1116
1125
      mysql_rename_table(old_db_type, db, old_name, db, table_name, FN_FROM_IS_TMP);
1117
1126
    }
1118
1127
  }
1123
1132
    goto err_with_placeholders;
1124
1133
  }
1125
1134
 
1126
 
  quick_rm_table(old_db_type, db, old_name, true);
 
1135
  quick_rm_table(*session, db, old_name, true);
1127
1136
 
1128
1137
  pthread_mutex_unlock(&LOCK_open);
1129
1138
 
1182
1191
    session->close_temporary_table(new_table, true, true);
1183
1192
  }
1184
1193
  else
1185
 
    quick_rm_table(new_db_type, new_db, tmp_name, true);
 
1194
    quick_rm_table(*session, new_db, tmp_name, true);
1186
1195
 
1187
1196
err:
1188
1197
  /*
1236
1245
  pthread_mutex_unlock(&LOCK_open);
1237
1246
  return true;
1238
1247
}
1239
 
/* mysql_alter_table */
 
1248
/* alter_table */
1240
1249
 
1241
1250
static int
1242
1251
copy_data_between_tables(Table *from,Table *to,
1468
1477
  protoengine->set_name(new_db_type->getName());
1469
1478
 
1470
1479
  error= mysql_create_table(session, new_db, tmp_name,
1471
 
                            create_info, create_proto, alter_info, 1, 0);
 
1480
                            create_info, create_proto, alter_info, true, 0);
1472
1481
 
1473
1482
  return(error);
1474
1483
}
1475
1484
 
1476
1485
/** @TODO This will soon die. */
1477
 
bool mysql_create_like_schema_frm(Session* session,
1478
 
                                  TableList* schema_table,
1479
 
                                  HA_CREATE_INFO *create_info,
1480
 
                                  message::Table* table_proto)
 
1486
bool create_like_schema_frm(Session* session,
 
1487
                            TableList* schema_table,
 
1488
                            HA_CREATE_INFO *create_info,
 
1489
                            message::Table* table_proto)
1481
1490
{
1482
1491
  HA_CREATE_INFO local_create_info;
1483
1492
  AlterInfo alter_info;
1502
1511
  table_options= table_proto->mutable_options();
1503
1512
  table_options->clear_max_rows();
1504
1513
 
1505
 
  if (mysql_prepare_create_table(session, &local_create_info, &alter_info,
 
1514
  if (mysql_prepare_create_table(session, &local_create_info, table_proto,
 
1515
                                 &alter_info,
1506
1516
                                 tmp_table, &db_options,
1507
1517
                                 schema_table->table->file,
1508
1518
                                 &schema_table->table->s->key_info, &keys, 0))
1530
1540
 
1531
1541
  return false;
1532
1542
}
 
1543
 
 
1544
} /* namespace drizzled */