~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_table.cc

  • Committer: Brian Aker
  • Date: 2009-05-28 00:15:58 UTC
  • mfrom: (1039.1.7 merge)
  • Revision ID: brian@gaz-20090528001558-xychs3xtpg8wz5i8
MErge Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
190
190
    path length on success, 0 on failure
191
191
*/
192
192
 
193
 
uint32_t build_table_filename(char *buff, size_t bufflen, const char *db,
194
 
                          const char *table_name, const char *ext, uint32_t flags)
 
193
size_t build_table_filename(char *buff, size_t bufflen, const char *db, const char *table_name, bool is_tmp)
195
194
{
196
195
  string table_path;
197
196
  char dbbuff[FN_REFLEN];
198
197
  char tbbuff[FN_REFLEN];
199
198
  int rootdir_len= strlen(FN_ROOTDIR);
200
199
 
201
 
  if (flags & FN_IS_TMP) // FN_FROM_IS_TMP | FN_TO_IS_TMP
 
200
  if (is_tmp) // FN_FROM_IS_TMP | FN_TO_IS_TMP
202
201
    strncpy(tbbuff, table_name, sizeof(tbbuff));
203
202
  else
204
203
    tablename_to_filename(table_name, tbbuff, sizeof(tbbuff));
221
220
  table_path.clear();
222
221
#endif
223
222
  table_path.append(tbbuff);
224
 
  table_path.append(ext);
225
223
 
226
224
  if (bufflen < table_path.length())
227
225
    return 0;
228
226
 
229
227
  strcpy(buff, table_path.c_str());
 
228
 
230
229
  return table_path.length();
231
230
}
232
231
 
260
259
  post_tmpdir_str << session->thread_id << session->tmp_table++;
261
260
  tmp= post_tmpdir_str.str();
262
261
 
263
 
  if (lower_case_table_names)
264
 
    transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
 
262
  transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
265
263
 
266
264
  path_str << tmp;
267
265
 
389
387
                         bool drop_temporary, bool dont_log_query)
390
388
{
391
389
  TableList *table;
392
 
  char path[FN_REFLEN], *alias;
 
390
  char path[FN_REFLEN];
393
391
  uint32_t path_length= 0;
394
392
  String wrong_tables;
395
393
  int error= 0;
495
493
        error= -1;
496
494
        goto err_with_placeholders;
497
495
      }
498
 
      alias= (lower_case_table_names == 2) ? table->alias : table->table_name;
499
496
      /* remove .frm file and engine files */
500
 
      path_length= build_table_filename(path, sizeof(path), db, alias, "",
501
 
                                        table->internal_tmp_table ?
502
 
                                        FN_IS_TMP : 0);
 
497
      path_length= build_table_filename(path, sizeof(path), db, table->table_name, table->internal_tmp_table);
503
498
    }
504
499
    if (drop_temporary ||
505
500
        ((table_type == NULL && (table_proto_exists(path)!=EEXIST))))
626
621
      base                      The StorageEngine handle.
627
622
      db                        The database name.
628
623
      table_name                The table name.
629
 
      flags                     flags for build_table_filename().
 
624
      is_tmp                    If the table is temp.
630
625
 
631
626
  RETURN
632
627
    0           OK
633
628
    != 0        Error
634
629
*/
635
630
 
636
 
bool quick_rm_table(StorageEngine *,const char *db,
637
 
                    const char *table_name, uint32_t flags)
 
631
bool quick_rm_table(StorageEngine *, const char *db,
 
632
                    const char *table_name, bool is_tmp)
638
633
{
639
634
  char path[FN_REFLEN];
640
635
  bool error= 0;
641
636
 
642
 
  build_table_filename(path, sizeof(path), db, table_name, "", flags);
 
637
  build_table_filename(path, sizeof(path), db, table_name, is_tmp);
643
638
 
644
639
  error= delete_table_proto_file(path);
645
640
 
1677
1672
{
1678
1673
  char          path[FN_REFLEN];
1679
1674
  uint32_t          path_length;
1680
 
  const char    *alias;
1681
1675
  uint          db_options, key_count;
1682
1676
  KEY           *key_info_buffer;
1683
1677
  handler       *file;
1695
1689
  db_options= create_info->table_options;
1696
1690
  if (create_info->row_type == ROW_TYPE_DYNAMIC)
1697
1691
    db_options|=HA_OPTION_PACK_RECORD;
1698
 
  alias= table_case_name(create_info, table_name);
1699
1692
  if (!(file= get_new_handler((TableShare*) 0, session->mem_root,
1700
1693
                              create_info->db_type)))
1701
1694
  {
1722
1715
  {
1723
1716
 #ifdef FN_DEVCHAR
1724
1717
    /* check if the table name contains FN_DEVCHAR when defined */
1725
 
    if (strchr(alias, FN_DEVCHAR))
 
1718
    if (strchr(table_name, FN_DEVCHAR))
1726
1719
    {
1727
 
      my_error(ER_WRONG_TABLE_NAME, MYF(0), alias);
 
1720
      my_error(ER_WRONG_TABLE_NAME, MYF(0), table_name);
1728
1721
      return(true);
1729
1722
    }
1730
1723
#endif
1731
 
    path_length= build_table_filename(path, sizeof(path), db, alias, "",
1732
 
                                      internal_tmp_table ? FN_IS_TMP : 0);
 
1724
    path_length= build_table_filename(path, sizeof(path), db, table_name, internal_tmp_table);
1733
1725
  }
1734
1726
 
1735
1727
  /* Check if table already exists */
1741
1733
      create_info->table_existed= 1;            // Mark that table existed
1742
1734
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1743
1735
                          ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1744
 
                          alias);
 
1736
                          table_name);
1745
1737
      error= 0;
1746
1738
      goto err;
1747
1739
    }
1748
 
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), alias);
 
1740
    my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table_name);
1749
1741
    goto err;
1750
1742
  }
1751
1743
 
1768
1760
      Then she could create the table. This case is pretty obscure and
1769
1761
      therefore we don't introduce a new error message only for it.
1770
1762
    */
1771
 
    if (get_cached_table_share(db, alias))
 
1763
    if (get_cached_table_share(db, table_name))
1772
1764
    {
1773
1765
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table_name);
1774
1766
      goto unlock_and_end;
1874
1866
  error= false;
1875
1867
  push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1876
1868
                      ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1877
 
                      alias);
 
1869
                      table_name);
1878
1870
  create_info->table_existed= 1;                // Mark that table existed
1879
1871
  goto unlock_and_end;
1880
1872
}
2031
2023
                   const char *new_name, uint32_t flags)
2032
2024
{
2033
2025
  Session *session= current_session;
2034
 
  char from[FN_REFLEN], to[FN_REFLEN], lc_from[FN_REFLEN], lc_to[FN_REFLEN];
 
2026
  char from[FN_REFLEN], to[FN_REFLEN];
2035
2027
  char *from_base= from, *to_base= to;
2036
 
  char tmp_name[NAME_LEN+1];
2037
2028
  handler *file;
2038
2029
  int error=0;
2039
2030
 
2040
2031
  file= (base == NULL ? 0 :
2041
2032
         get_new_handler((TableShare*) 0, session->mem_root, base));
2042
2033
 
2043
 
  build_table_filename(from, sizeof(from), old_db, old_name, "",
 
2034
  build_table_filename(from, sizeof(from), old_db, old_name,
2044
2035
                       flags & FN_FROM_IS_TMP);
2045
 
  build_table_filename(to, sizeof(to), new_db, new_name, "",
 
2036
  build_table_filename(to, sizeof(to), new_db, new_name,
2046
2037
                       flags & FN_TO_IS_TMP);
2047
2038
 
2048
 
  /*
2049
 
    If lower_case_table_names == 2 (case-preserving but case-insensitive
2050
 
    file system) and the storage is not HA_FILE_BASED, we need to provide
2051
 
    a lowercase file name, but we leave the .frm in mixed case.
2052
 
   */
2053
 
  if (lower_case_table_names == 2 && file &&
2054
 
      !(file->ha_table_flags() & HA_FILE_BASED))
2055
 
  {
2056
 
    strcpy(tmp_name, old_name);
2057
 
    my_casedn_str(files_charset_info, tmp_name);
2058
 
    build_table_filename(lc_from, sizeof(lc_from), old_db, tmp_name, "",
2059
 
                         flags & FN_FROM_IS_TMP);
2060
 
    from_base= lc_from;
2061
 
 
2062
 
    strcpy(tmp_name, new_name);
2063
 
    my_casedn_str(files_charset_info, tmp_name);
2064
 
    build_table_filename(lc_to, sizeof(lc_to), new_db, tmp_name, "",
2065
 
                         flags & FN_TO_IS_TMP);
2066
 
    to_base= lc_to;
2067
 
  }
2068
2039
  if (!file || !(error=file->ha_rename_table(from_base, to_base)))
2069
2040
  {
2070
2041
    if(!(flags & NO_FRM_RENAME)
2903
2874
    if (!name_lock)
2904
2875
      goto table_exists;
2905
2876
    dst_path_length= build_table_filename(dst_path, sizeof(dst_path),
2906
 
                                          db, table_name, "", 0);
 
2877
                                          db, table_name, false);
2907
2878
    if (table_proto_exists(dst_path)==EEXIST)
2908
2879
      goto table_exists;
2909
2880
  }
3733
3704
    true   Error
3734
3705
*/
3735
3706
 
3736
 
bool mysql_alter_table(Session *session,char *new_db, char *new_name,
 
3707
bool mysql_alter_table(Session *session, char *new_db, char *new_name,
3737
3708
                       HA_CREATE_INFO *create_info,
3738
3709
                       TableList *table_list,
3739
3710
                       Alter_info *alter_info,
3743
3714
  string new_name_str;
3744
3715
  int error= 0;
3745
3716
  char tmp_name[80],old_name[32],new_name_buff[FN_REFLEN];
3746
 
  char new_alias_buff[FN_REFLEN], *table_name, *db, *new_alias, *alias;
 
3717
  char new_alias_buff[FN_REFLEN], *table_name, *db;
 
3718
  const char *new_alias;
3747
3719
  char path[FN_REFLEN];
3748
3720
  ha_rows copied= 0,deleted= 0;
3749
3721
  StorageEngine *old_db_type, *new_db_type, *save_old_db_type;
3762
3734
    later just by comparing the pointers, avoiding the need for strcmp.
3763
3735
  */
3764
3736
  session->set_proc_info("init");
3765
 
  table_name=table_list->table_name;
3766
 
  alias= (lower_case_table_names == 2) ? table_list->alias : table_name;
 
3737
  table_name= table_list->table_name;
3767
3738
  db=table_list->db;
3768
3739
  if (!new_db || !my_strcasecmp(table_alias_charset, new_db, db))
3769
3740
    new_db= db;
3770
 
  build_table_filename(path, sizeof(path), db, table_name, "", 0);
 
3741
  build_table_filename(path, sizeof(path), db, table_name, false);
3771
3742
 
3772
3743
  /* DISCARD/IMPORT TABLESPACE is always alone in an ALTER Table */
3773
3744
  if (alter_info->tablespace_op != NO_TABLESPACE_OP)
3799
3770
  /* Check that we are not trying to rename to an existing table */
3800
3771
  if (new_name)
3801
3772
  {
3802
 
    strcpy(new_name_buff,new_name);
3803
 
    strcpy(new_alias= new_alias_buff, new_name);
3804
 
    if (lower_case_table_names)
3805
 
    {
3806
 
      if (lower_case_table_names != 2)
3807
 
      {
3808
 
        my_casedn_str(files_charset_info, new_name_buff);
3809
 
        new_alias= new_name;                    // Create lower case table name
3810
 
      }
3811
 
      my_casedn_str(files_charset_info, new_name);
3812
 
    }
 
3773
    strcpy(new_name_buff, new_name);
 
3774
    strcpy(new_alias_buff, new_name);
 
3775
    new_alias= new_alias_buff;
 
3776
 
 
3777
    my_casedn_str(files_charset_info, new_name_buff);
 
3778
    new_alias= new_name;                        // Create lower case table name
 
3779
    my_casedn_str(files_charset_info, new_name);
 
3780
 
3813
3781
    if (new_db == db &&
3814
3782
        !my_strcasecmp(table_alias_charset, new_name_buff, table_name))
3815
3783
    {
3840
3808
        }
3841
3809
 
3842
3810
        build_table_filename(new_name_buff, sizeof(new_name_buff),
3843
 
                             new_db, new_name_buff, "", 0);
 
3811
                             new_db, new_name_buff, false);
3844
3812
        if (table_proto_exists(new_name_buff)==EEXIST)
3845
3813
        {
3846
3814
          /* Table will be closed by Session::executeCommand() */
3852
3820
  }
3853
3821
  else
3854
3822
  {
3855
 
    new_alias= (lower_case_table_names == 2) ? alias : table_name;
 
3823
    new_alias= table_name;
3856
3824
    new_name= table_name;
3857
3825
  }
3858
3826
 
3961
3929
      else
3962
3930
      {
3963
3931
        *fn_ext(new_name)=0;
3964
 
        if (mysql_rename_table(old_db_type,db,table_name,new_db,new_alias, 0))
 
3932
        if (mysql_rename_table(old_db_type, db, table_name, new_db, new_alias, 0))
3965
3933
          error= -1;
3966
3934
        else if (0)
3967
3935
      {
4022
3990
  snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%"PRIx64, TMP_FILE_PREFIX,
4023
3991
           (unsigned long)current_pid, session->thread_id);
4024
3992
  /* Safety fix for innodb */
4025
 
  if (lower_case_table_names)
4026
 
    my_casedn_str(files_charset_info, tmp_name);
 
3993
  my_casedn_str(files_charset_info, tmp_name);
4027
3994
 
4028
3995
 
4029
3996
  /* Create a temporary table with the new format */
4040
4007
    TableList tbl;
4041
4008
    memset(&tbl, 0, sizeof(tbl));
4042
4009
    tbl.db= new_db;
4043
 
    tbl.table_name= tbl.alias= tmp_name;
 
4010
    tbl.alias= tmp_name;
 
4011
    tbl.table_name= tmp_name;
 
4012
 
4044
4013
    /* Table is in session->temporary_tables */
4045
4014
    new_table= open_table(session, &tbl, (bool*) 0, DRIZZLE_LOCK_IGNORE_FLUSH);
4046
4015
  }
4048
4017
  {
4049
4018
    char tmp_path[FN_REFLEN];
4050
4019
    /* table is a normal table: Create temporary table in same directory */
4051
 
    build_table_filename(tmp_path, sizeof(tmp_path), new_db, tmp_name, "",
4052
 
                         FN_IS_TMP);
 
4020
    build_table_filename(tmp_path, sizeof(tmp_path), new_db, tmp_name, true);
4053
4021
    /* Open our intermediate table */
4054
4022
    new_table=open_temporary_table(session, tmp_path, new_db, tmp_name, 0, OTM_OPEN);
4055
4023
  }
4145
4113
  session->set_proc_info("rename result table");
4146
4114
  snprintf(old_name, sizeof(old_name), "%s2-%lx-%"PRIx64, TMP_FILE_PREFIX,
4147
4115
           (unsigned long)current_pid, session->thread_id);
4148
 
  if (lower_case_table_names)
4149
 
    my_casedn_str(files_charset_info, old_name);
 
4116
  my_casedn_str(files_charset_info, old_name);
4150
4117
 
4151
4118
  wait_while_table_is_used(session, table, HA_EXTRA_PREPARE_FOR_RENAME);
4152
4119
  close_data_files_and_morph_locks(session, db, table_name);
4178
4145
  {
4179
4146
    /* Try to get everything back. */
4180
4147
    error=1;
4181
 
    quick_rm_table(new_db_type,new_db,new_alias, 0);
 
4148
    quick_rm_table(new_db_type, new_db, new_alias, 0);
4182
4149
    quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP);
4183
 
    mysql_rename_table(old_db_type, db, old_name, db, alias,
 
4150
    mysql_rename_table(old_db_type, db, old_name, db, table_name,
4184
4151
                       FN_FROM_IS_TMP);
4185
4152
  }
4186
4153
 
4215
4182
    */
4216
4183
    char table_path[FN_REFLEN];
4217
4184
    Table *t_table;
4218
 
    build_table_filename(table_path, sizeof(table_path), new_db, table_name, "", 0);
 
4185
    build_table_filename(table_path, sizeof(table_path), new_db, table_name, false);
4219
4186
    t_table= open_temporary_table(session, table_path, new_db, tmp_name, false, OTM_OPEN);
4220
4187
    if (t_table)
4221
4188
    {