~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_table.cc

  • Committer: Brian Aker
  • Date: 2009-05-29 22:44:39 UTC
  • mfrom: (1039.1.14 merge)
  • Revision ID: brian@gaz-20090529224439-0vdgsd8b8c002yi1
Merge Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
1629
1629
 
1630
1630
 
1631
1631
/*
 
1632
  Ignore the name of this function... it locks :(
 
1633
 
1632
1634
  Create a table
1633
1635
 
1634
1636
  SYNOPSIS
1651
1653
    that concurrent operations won't intervene. mysql_create_table()
1652
1654
    is a wrapper that can be used for this.
1653
1655
 
1654
 
    no_log is needed for the case of CREATE ... SELECT,
1655
 
    as the logging will be done later in sql_insert.cc
1656
 
    select_field_count is also used for CREATE ... SELECT,
1657
 
    and must be zero for standard create of table.
1658
 
 
1659
1656
  RETURN VALUES
1660
1657
    false OK
1661
1658
    true  error
1667
1664
                                drizzled::message::Table *table_proto,
1668
1665
                                Alter_info *alter_info,
1669
1666
                                bool internal_tmp_table,
1670
 
                                uint32_t select_field_count,
1671
 
                                bool lock_open_lock)
 
1667
                                uint32_t select_field_count)
1672
1668
{
1673
1669
  char          path[FN_REFLEN];
1674
1670
  uint32_t          path_length;
1681
1677
  {
1682
1678
    my_message(ER_TABLE_MUST_HAVE_COLUMNS, ER(ER_TABLE_MUST_HAVE_COLUMNS),
1683
1679
               MYF(0));
1684
 
    return(true);
 
1680
    return true;
1685
1681
  }
1686
1682
  assert(strcmp(table_name,table_proto->name().c_str())==0);
1687
1683
  if (check_engine(session, table_name, create_info))
1688
 
    return(true);
 
1684
    return true;
1689
1685
  db_options= create_info->table_options;
1690
1686
  if (create_info->row_type == ROW_TYPE_DYNAMIC)
1691
1687
    db_options|=HA_OPTION_PACK_RECORD;
1693
1689
                              create_info->db_type)))
1694
1690
  {
1695
1691
    my_error(ER_OUTOFMEMORY, MYF(0), sizeof(handler));
1696
 
    return(true);
 
1692
    return true;
1697
1693
  }
1698
1694
 
1699
1695
  set_table_default_charset(create_info, (char*) db);
1718
1714
    if (strchr(table_name, FN_DEVCHAR))
1719
1715
    {
1720
1716
      my_error(ER_WRONG_TABLE_NAME, MYF(0), table_name);
1721
 
      return(true);
 
1717
      return true;
1722
1718
    }
1723
1719
#endif
1724
1720
    path_length= build_table_filename(path, sizeof(path), db, table_name, internal_tmp_table);
1741
1737
    goto err;
1742
1738
  }
1743
1739
 
1744
 
  if (lock_open_lock)
1745
 
    pthread_mutex_lock(&LOCK_open);
 
1740
  pthread_mutex_lock(&LOCK_open);
1746
1741
  if (!internal_tmp_table && !(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1747
1742
  {
1748
1743
    if (table_proto_exists(path)==EEXIST)
1749
1744
    {
1750
1745
      if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
1751
 
        goto warn;
1752
 
      my_error(ER_TABLE_EXISTS_ERROR,MYF(0),table_name);
 
1746
      {
 
1747
        error= false;
 
1748
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
1749
                            ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
 
1750
                            table_name);
 
1751
        create_info->table_existed= 1;          // Mark that table existed
 
1752
      }
 
1753
      else 
 
1754
        my_error(ER_TABLE_EXISTS_ERROR,MYF(0),table_name);
 
1755
 
1753
1756
      goto unlock_and_end;
1754
1757
    }
1755
1758
    /*
1787
1790
        /* Normal case, no table exists. we can go and create it */
1788
1791
        break;
1789
1792
      case HA_ERR_TABLE_EXIST:
1790
 
 
1791
1793
        if (create_if_not_exists)
1792
 
          goto warn;
 
1794
        {
 
1795
          error= false;
 
1796
          push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
1797
                              ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
 
1798
                              table_name);
 
1799
          create_info->table_existed= 1;                // Mark that table existed
 
1800
          goto unlock_and_end;
 
1801
        }
1793
1802
        my_error(ER_TABLE_EXISTS_ERROR,MYF(0),table_name);
1794
1803
        goto unlock_and_end;
1795
1804
      default:
1854
1863
    write_bin_log(session, true, session->query, session->query_length);
1855
1864
  error= false;
1856
1865
unlock_and_end:
1857
 
  if (lock_open_lock)
1858
 
    pthread_mutex_unlock(&LOCK_open);
 
1866
  pthread_mutex_unlock(&LOCK_open);
1859
1867
 
1860
1868
err:
1861
1869
  session->set_proc_info("After create");
1862
1870
  delete file;
1863
1871
  return(error);
1864
 
 
1865
 
warn:
1866
 
  error= false;
1867
 
  push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1868
 
                      ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1869
 
                      table_name);
1870
 
  create_info->table_existed= 1;                // Mark that table existed
1871
 
  goto unlock_and_end;
1872
1872
}
1873
1873
 
1874
1874
 
1933
1933
                                     table_proto,
1934
1934
                                     alter_info,
1935
1935
                                     internal_tmp_table,
1936
 
                                     select_field_count, true);
 
1936
                                     select_field_count);
1937
1937
 
1938
1938
unlock:
1939
1939
  if (name_lock)
2026
2026
  char from[FN_REFLEN], to[FN_REFLEN];
2027
2027
  char *from_base= from, *to_base= to;
2028
2028
  handler *file;
2029
 
  int error=0;
 
2029
  int error= 0;
2030
2030
 
2031
2031
  file= (base == NULL ? 0 :
2032
2032
         get_new_handler((TableShare*) 0, session->mem_root, base));
2167
2167
                                  &error))))
2168
2168
    {
2169
2169
      pthread_mutex_unlock(&LOCK_open);
 
2170
 
2170
2171
      return(0);                                // Can't open frm file
2171
2172
    }
2172
2173
 
2174
2175
    {
2175
2176
      release_table_share(share, RELEASE_NORMAL);
2176
2177
      pthread_mutex_unlock(&LOCK_open);
 
2178
 
2177
2179
      return(0);                           // Out of memory
2178
2180
    }
2179
2181
    table= &tmp_table;
2940
2942
  else if (err)
2941
2943
  {
2942
2944
    (void) quick_rm_table(create_info->db_type, db,
2943
 
                          table_name, 0); /* purecov: inspected */
 
2945
                          table_name, false); /* purecov: inspected */
2944
2946
    goto err;       /* purecov: inspected */
2945
2947
  }
2946
2948
 
3187
3189
  return(error);
3188
3190
}
3189
3191
 
3190
 
int create_temporary_table(Session *session,
3191
 
                           Table *table,
3192
 
                           char *new_db,
3193
 
                           char *tmp_name,
3194
 
                           HA_CREATE_INFO *create_info,
3195
 
                           Alter_info *alter_info,
3196
 
                           bool db_changed)
 
3192
static int 
 
3193
create_temporary_table(Session *session,
 
3194
                       Table *table,
 
3195
                       char *new_db,
 
3196
                       char *tmp_name,
 
3197
                       HA_CREATE_INFO *create_info,
 
3198
                       Alter_info *alter_info,
 
3199
                       bool db_changed)
3197
3200
{
3198
3201
  int error;
3199
3202
  char index_file[FN_REFLEN], data_file[FN_REFLEN];
3764
3767
  */
3765
3768
 
3766
3769
  if (!(table= open_n_lock_single_table(session, table_list, TL_WRITE_ALLOW_READ)))
3767
 
    return(true);
 
3770
    return true;
3768
3771
  table->use_all_columns();
3769
3772
 
3770
3773
  /* Check that we are not trying to rename to an existing table */
3923
3926
      */
3924
3927
      if (table_proto_exists(new_name)==EEXIST)
3925
3928
      {
3926
 
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name);
3927
 
        error= -1;
 
3929
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name);
 
3930
        error= -1;
3928
3931
      }
3929
3932
      else
3930
3933
      {
3931
 
        *fn_ext(new_name)=0;
3932
 
        if (mysql_rename_table(old_db_type, db, table_name, new_db, new_alias, 0))
3933
 
          error= -1;
 
3934
        *fn_ext(new_name)=0;
 
3935
        if (mysql_rename_table(old_db_type, db, table_name, new_db, new_alias, 0))
 
3936
          error= -1;
3934
3937
        else if (0)
3935
 
      {
 
3938
        {
3936
3939
          mysql_rename_table(old_db_type, new_db, new_alias, db,
3937
3940
                             table_name, 0);
3938
3941
          error= -1;
 
3942
        }
3939
3943
      }
3940
3944
    }
3941
 
  }
3942
3945
 
3943
3946
    if (error == HA_ERR_WRONG_COMMAND)
3944
 
  {
 
3947
    {
3945
3948
      error= 0;
3946
3949
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
3947
 
                          ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
3948
 
                          table->alias);
3949
 
  }
 
3950
                          ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
 
3951
                          table->alias);
 
3952
    }
3950
3953
 
3951
3954
    if (!error)
3952
3955
    {
4019
4022
    /* table is a normal table: Create temporary table in same directory */
4020
4023
    build_table_filename(tmp_path, sizeof(tmp_path), new_db, tmp_name, true);
4021
4024
    /* Open our intermediate table */
4022
 
    new_table=open_temporary_table(session, tmp_path, new_db, tmp_name, 0, OTM_OPEN);
 
4025
    new_table= open_temporary_table(session, tmp_path, new_db, tmp_name, 0, OTM_OPEN);
4023
4026
  }
4024
 
  if (!new_table)
 
4027
 
 
4028
  if (new_table == NULL)
4025
4029
    goto err1;
4026
4030
 
4027
4031
  /* Copy the data if necessary. */
4028
4032
  session->count_cuted_fields= CHECK_FIELD_WARN;        // calc cuted fields
4029
4033
  session->cuted_fields=0L;
4030
4034
  session->set_proc_info("copy to tmp table");
4031
 
  copied=deleted=0;
4032
 
  /*
4033
 
    We do not copy data for MERGE tables. Only the children have data.
4034
 
    MERGE tables have HA_NO_COPY_ON_ALTER set.
4035
 
  */
4036
 
  if (new_table && !(new_table->file->ha_table_flags() & HA_NO_COPY_ON_ALTER))
4037
 
  {
4038
 
    /* We don't want update TIMESTAMP fields during ALTER Table. */
4039
 
    new_table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
4040
 
    new_table->next_number_field=new_table->found_next_number_field;
4041
 
    error= copy_data_between_tables(table, new_table,
4042
 
                                    alter_info->create_list, ignore,
4043
 
                                   order_num, order, &copied, &deleted,
4044
 
                                    alter_info->keys_onoff,
4045
 
                                    alter_info->error_if_not_empty);
4046
 
  }
4047
 
  else
4048
 
  {
4049
 
    pthread_mutex_lock(&LOCK_open);
4050
 
    wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
4051
 
    pthread_mutex_unlock(&LOCK_open);
4052
 
    alter_table_manage_keys(table, table->file->indexes_are_disabled(),
4053
 
                            alter_info->keys_onoff);
4054
 
    error= ha_autocommit_or_rollback(session, 0);
4055
 
    if (! session->endActiveTransaction())
4056
 
      error= 1;
4057
 
  }
 
4035
  copied= deleted= 0;
 
4036
 
 
4037
  assert(new_table);
 
4038
 
 
4039
  /* We don't want update TIMESTAMP fields during ALTER Table. */
 
4040
  new_table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
 
4041
  new_table->next_number_field=new_table->found_next_number_field;
 
4042
  error= copy_data_between_tables(table, new_table,
 
4043
                                  alter_info->create_list, ignore,
 
4044
                                  order_num, order, &copied, &deleted,
 
4045
                                  alter_info->keys_onoff,
 
4046
                                  alter_info->error_if_not_empty);
 
4047
 
4058
4048
  /* We must not ignore bad input! */;
4059
4049
  session->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
4060
4050
 
4089
4079
  pthread_mutex_lock(&LOCK_open);
4090
4080
  if (error)
4091
4081
  {
4092
 
    quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP);
 
4082
    quick_rm_table(new_db_type, new_db, tmp_name, true);
4093
4083
    pthread_mutex_unlock(&LOCK_open);
4094
4084
    goto err;
4095
4085
  }
4138
4128
                         FN_TO_IS_TMP))
4139
4129
  {
4140
4130
    error=1;
4141
 
    quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP);
 
4131
    quick_rm_table(new_db_type, new_db, tmp_name, true);
4142
4132
  }
4143
4133
  else if (mysql_rename_table(new_db_type, new_db, tmp_name, new_db,
4144
4134
                              new_alias, FN_FROM_IS_TMP) || ((new_name != table_name || new_db != db) && 0))
4145
4135
  {
4146
4136
    /* Try to get everything back. */
4147
4137
    error=1;
4148
 
    quick_rm_table(new_db_type, new_db, new_alias, 0);
4149
 
    quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP);
 
4138
    quick_rm_table(new_db_type, new_db, new_alias, false);
 
4139
    quick_rm_table(new_db_type, new_db, tmp_name, true);
4150
4140
    mysql_rename_table(old_db_type, db, old_name, db, table_name,
4151
4141
                       FN_FROM_IS_TMP);
4152
4142
  }
4157
4147
    goto err_with_placeholders;
4158
4148
  }
4159
4149
 
4160
 
  quick_rm_table(old_db_type, db, old_name, FN_IS_TMP);
 
4150
  quick_rm_table(old_db_type, db, old_name, true);
4161
4151
 
4162
4152
  if (session->locked_tables && new_name == table_name && new_db == db)
4163
4153
  {
4239
4229
    close_temporary_table(session, new_table, 1, 1);
4240
4230
  }
4241
4231
  else
4242
 
    quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP);
 
4232
    quick_rm_table(new_db_type, new_db, tmp_name, true);
4243
4233
 
4244
4234
err:
4245
4235
  /*