~posulliv/drizzle/optimizer-style-cleanup

« back to all changes in this revision

Viewing changes to drizzled/statement/alter_table.cc

  • Committer: Padraig O'Sullivan
  • Date: 2010-03-15 14:05:26 UTC
  • mfrom: (1237.9.99 staging)
  • Revision ID: osullivan.padraig@gmail.com-20100315140526-opbgwdwn6tfecdkq
MergeĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
#include "drizzled/data_home.h"
36
36
#include "drizzled/sql_table.h"
37
37
#include "drizzled/table_proto.h"
38
 
#include "drizzled/plugin/info_schema_table.h"
39
38
#include "drizzled/optimizer/range.h"
40
39
#include "drizzled/time_functions.h"
41
40
#include "drizzled/records.h"
69
68
                                      AlterInfo *alter_info);
70
69
 
71
70
static int create_temporary_table(Session *session,
72
 
                                  Table *table,
73
71
                                  TableIdentifier &identifier,
74
72
                                  HA_CREATE_INFO *create_info,
75
 
                                  message::Table *create_proto,
 
73
                                  message::Table &create_proto,
76
74
                                  AlterInfo *alter_info);
77
75
 
78
76
static Table *open_alter_table(Session *session, Table *table, char *db, char *table_name);
117
115
                        select_lex->db, 
118
116
                        session->lex->name.str,
119
117
                        &create_info,
120
 
                        &create_table_proto,
 
118
                        create_table_proto,
121
119
                        first_table,
122
120
                        &alter_info,
123
121
                        select_lex->order_list.elements,
357
355
      */
358
356
      if (alter_info->build_method == HA_BUILD_ONLINE)
359
357
      {
360
 
        my_error(ER_NOT_SUPPORTED_YET, MYF(0), session->query);
 
358
        my_error(ER_NOT_SUPPORTED_YET, MYF(0), session->query.c_str());
361
359
        goto err;
362
360
      }
363
361
      alter_info->build_method= HA_BUILD_OFFLINE;
575
573
    error=1;
576
574
  if (error)
577
575
    goto err;
578
 
  write_bin_log(session, session->query, session->query_length);
 
576
  write_bin_log(session, session->query.c_str());
579
577
 
580
578
err:
581
579
  (void) transaction_services.ha_autocommit_or_rollback(session, error);
679
677
                 char *new_db,
680
678
                 char *new_name,
681
679
                 HA_CREATE_INFO *create_info,
682
 
                 message::Table *create_proto,
 
680
                 message::Table &create_proto,
683
681
                 TableList *table_list,
684
682
                 AlterInfo *alter_info,
685
683
                 uint32_t order_num,
693
691
  int error= 0;
694
692
  char tmp_name[80];
695
693
  char old_name[32];
696
 
  char new_name_buff[FN_REFLEN];
697
 
  char new_alias_buff[FN_REFLEN];
698
694
  char *table_name;
699
695
  char *db;
700
696
  const char *new_alias;
705
701
  plugin::StorageEngine *save_old_db_type;
706
702
  bitset<32> tmp;
707
703
 
708
 
  new_name_buff[0]= '\0';
709
 
 
710
 
  /**
711
 
   * @todo this is a result of retaining the behavior that was here before. This should be removed
712
 
   * and the correct error handling should be done in doDropTable for the I_S engine.
713
 
   */
714
 
  plugin::InfoSchemaTable *sch_table= plugin::InfoSchemaTable::getTable(table_list->table_name);
715
 
  if (sch_table)
716
 
  {
717
 
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.c_str());
718
 
    return true;
719
 
  }
720
 
 
721
704
  session->set_proc_info("init");
722
705
 
723
706
  /*
736
719
    return mysql_discard_or_import_tablespace(session, table_list, alter_info->tablespace_op);
737
720
  }
738
721
 
739
 
  ostringstream oss;
740
 
  oss << drizzle_data_home << "/" << db << "/" << table_name;
741
 
 
742
 
  (void) internal::unpack_filename(new_name_buff, oss.str().c_str());
743
 
 
744
722
  /*
745
723
    If this is just a rename of a view, short cut to the
746
724
    following scenario: 1) lock LOCK_open 2) do a RENAME
755
733
    This code is wrong and will be removed, please do not copy.
756
734
  */
757
735
 
758
 
  if (!(table= session->openTableLock(table_list, TL_WRITE_ALLOW_READ)))
 
736
  if (not (table= session->openTableLock(table_list, TL_WRITE_ALLOW_READ)))
759
737
    return true;
760
738
  
761
739
  table->use_all_columns();
763
741
  /* Check that we are not trying to rename to an existing table */
764
742
  if (new_name)
765
743
  {
766
 
    strcpy(new_name_buff, new_name);
 
744
    char new_alias_buff[FN_REFLEN];
 
745
    char lower_case_table_name[FN_REFLEN];
 
746
 
 
747
    strcpy(lower_case_table_name, new_name);
767
748
    strcpy(new_alias_buff, new_name);
768
749
    new_alias= new_alias_buff;
769
750
 
770
 
    my_casedn_str(files_charset_info, new_name_buff);
 
751
    my_casedn_str(files_charset_info, lower_case_table_name);
771
752
    new_alias= new_name; // Create lower case table name
772
753
    my_casedn_str(files_charset_info, new_name);
773
754
 
774
755
    if (new_db == db &&
775
 
        ! my_strcasecmp(table_alias_charset, new_name_buff, table_name))
 
756
        not my_strcasecmp(table_alias_charset, lower_case_table_name, table_name))
776
757
    {
777
758
      /*
778
759
        Source and destination table names are equal: make later check
782
763
    }
783
764
    else
784
765
    {
785
 
      if (table->s->tmp_table != NO_TMP_TABLE)
 
766
      if (table->s->tmp_table != STANDARD_TABLE)
786
767
      {
787
 
        if (session->find_temporary_table(new_db, new_name_buff))
 
768
        if (session->find_temporary_table(new_db, lower_case_table_name))
788
769
        {
789
 
          my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name_buff);
 
770
          my_error(ER_TABLE_EXISTS_ERROR, MYF(0), lower_case_table_name);
790
771
          return true;
791
772
        }
792
773
      }
795
776
        if (session->lock_table_name_if_not_cached(new_db, new_name, &name_lock))
796
777
          return true;
797
778
 
798
 
        if (! name_lock)
 
779
        if (not name_lock)
799
780
        {
800
781
          my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
801
782
          return true;
802
783
        }
803
784
 
804
 
        build_table_filename(new_name_buff, sizeof(new_name_buff), new_db, new_name_buff, false);
 
785
        TableIdentifier identifier(new_db, lower_case_table_name);
805
786
 
806
 
        if (plugin::StorageEngine::getTableDefinition(*session, new_name_buff, new_db, new_name_buff, false) == EEXIST)
 
787
        if (plugin::StorageEngine::doesTableExist(*session, identifier))
807
788
        {
808
789
          /* Table will be closed by Session::executeCommand() */
809
790
          my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
819
800
  }
820
801
 
821
802
  old_db_type= table->s->db_type();
822
 
  if (! create_info->db_type)
 
803
  if (not create_info->db_type)
823
804
  {
824
805
    create_info->db_type= old_db_type;
825
806
  }
826
807
 
827
 
  if (table->s->tmp_table != NO_TMP_TABLE)
 
808
  if (table->s->tmp_table != STANDARD_TABLE)
828
809
  {
829
 
    create_proto->set_type(message::Table::TEMPORARY);
 
810
    create_proto.set_type(message::Table::TEMPORARY);
830
811
  }
831
812
  else
832
813
  {
833
 
    create_proto->set_type(message::Table::STANDARD);
 
814
    create_proto.set_type(message::Table::STANDARD);
834
815
  }
835
816
 
836
817
  new_db_type= create_info->db_type;
837
818
 
 
819
  /**
 
820
    @todo Have a check on the table definition for FK in the future 
 
821
    to remove the need for the cursor. (aka can_switch_engines())
 
822
  */
838
823
  if (new_db_type != old_db_type &&
839
 
      !table->cursor->can_switch_engines())
 
824
      not table->cursor->can_switch_engines())
840
825
  {
841
826
    assert(0);
842
827
    my_error(ER_ROW_IS_REFERENCED, MYF(0));
846
831
  if (create_info->row_type == ROW_TYPE_NOT_USED)
847
832
  {
848
833
    message::Table::TableOptions *table_options;
849
 
    table_options= create_proto->mutable_options();
 
834
    table_options= create_proto.mutable_options();
850
835
 
851
836
    create_info->row_type= table->s->row_type;
852
837
    table_options->set_row_type((message::Table_TableOptions_RowType)table->s->row_type);
962
947
 
963
948
    if (error == 0)
964
949
    {
965
 
      write_bin_log(session, session->query, session->query_length);
 
950
      write_bin_log(session, session->query.c_str());
966
951
      session->my_ok();
967
952
    }
968
953
    else if (error > 0)
982
967
  /* We have to do full alter table. */
983
968
  new_db_type= create_info->db_type;
984
969
 
985
 
  if (mysql_prepare_alter_table(session, table, create_info, create_proto,
 
970
  if (mysql_prepare_alter_table(session, table, create_info, &create_proto,
986
971
                                alter_info))
987
972
      goto err;
988
973
 
1004
989
    */
1005
990
    TableIdentifier new_table_temp(new_db,
1006
991
                                   tmp_name,
1007
 
                                   create_proto->type() != message::Table::TEMPORARY ? INTERNAL_TMP_TABLE :
 
992
                                   create_proto.type() != message::Table::TEMPORARY ? INTERNAL_TMP_TABLE :
1008
993
                                   TEMP_TABLE);
1009
994
 
1010
 
    error= create_temporary_table(session, table, new_table_temp, create_info, create_proto, alter_info);
 
995
    error= create_temporary_table(session, new_table_temp, create_info, create_proto, alter_info);
1011
996
 
1012
997
    if (error != 0)
1013
998
      goto err;
1044
1029
  /* We must not ignore bad input! */
1045
1030
  session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
1046
1031
 
1047
 
  if (table->s->tmp_table != NO_TMP_TABLE)
 
1032
  if (table->s->tmp_table != STANDARD_TABLE)
1048
1033
  {
1049
1034
    /* We changed a temporary table */
1050
1035
    if (error)
1137
1122
      /* Try to get everything back. */
1138
1123
      error= 1;
1139
1124
 
1140
 
      TableIdentifier alias_identifier(new_db, new_alias, NO_TMP_TABLE);
 
1125
      TableIdentifier alias_identifier(new_db, new_alias, STANDARD_TABLE);
1141
1126
      quick_rm_table(*session, alias_identifier);
1142
1127
 
1143
1128
      TableIdentifier tmp_identifier(new_db, tmp_name, INTERNAL_TMP_TABLE);
1163
1148
 
1164
1149
  session->set_proc_info("end");
1165
1150
 
1166
 
  write_bin_log(session, session->query, session->query_length);
 
1151
  write_bin_log(session, session->query.c_str());
1167
1152
  table_list->table= NULL;
1168
1153
 
1169
1154
end_temporary:
1437
1422
 
1438
1423
static int
1439
1424
create_temporary_table(Session *session,
1440
 
                       Table *table,
1441
1425
                       TableIdentifier &identifier,
1442
1426
                       HA_CREATE_INFO *create_info,
1443
 
                       message::Table *create_proto,
 
1427
                       message::Table &create_proto,
1444
1428
                       AlterInfo *alter_info)
1445
1429
{
1446
1430
  int error;
1447
 
  plugin::StorageEngine *old_db_type, *new_db_type;
1448
 
 
1449
 
  old_db_type= table->s->db_type();
1450
 
  new_db_type= create_info->db_type;
1451
1431
 
1452
1432
  /*
1453
1433
    Create a table with a temporary name.
1454
1434
    We don't log the statement, it will be logged later.
1455
1435
  */
1456
 
  create_proto->set_name(identifier.getTableName());
 
1436
  create_proto.set_name(identifier.getTableName());
1457
1437
 
1458
1438
  message::Table::StorageEngine *protoengine;
1459
 
  protoengine= create_proto->mutable_engine();
1460
 
  protoengine->set_name(new_db_type->getName());
 
1439
  protoengine= create_proto.mutable_engine();
 
1440
  protoengine->set_name(create_info->db_type->getName());
1461
1441
 
1462
1442
  error= mysql_create_table(session,
1463
1443
                            identifier,