190
190
path length on success, 0 on failure
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)
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);
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));
204
203
tablename_to_filename(table_name, tbbuff, sizeof(tbbuff));
260
259
post_tmpdir_str << session->thread_id << session->tmp_table++;
261
260
tmp= post_tmpdir_str.str();
263
if (lower_case_table_names)
264
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
262
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
496
494
goto err_with_placeholders;
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 ?
497
path_length= build_table_filename(path, sizeof(path), db, table->table_name, table->internal_tmp_table);
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.
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)
639
634
char path[FN_REFLEN];
642
build_table_filename(path, sizeof(path), db, table_name, "", flags);
637
build_table_filename(path, sizeof(path), db, table_name, is_tmp);
644
639
error= delete_table_proto_file(path);
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)))
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))
1727
my_error(ER_WRONG_TABLE_NAME, MYF(0), alias);
1720
my_error(ER_WRONG_TABLE_NAME, MYF(0), table_name);
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);
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),
1748
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), alias);
1740
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table_name);
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.
1771
if (get_cached_table_share(db, alias))
1763
if (get_cached_table_share(db, table_name))
1773
1765
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table_name);
1774
1766
goto unlock_and_end;
2031
2023
const char *new_name, uint32_t flags)
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];
2040
2031
file= (base == NULL ? 0 :
2041
2032
get_new_handler((TableShare*) 0, session->mem_root, base));
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);
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.
2053
if (lower_case_table_names == 2 && file &&
2054
!(file->ha_table_flags() & HA_FILE_BASED))
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);
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);
2068
2039
if (!file || !(error=file->ha_rename_table(from_base, to_base)))
2070
2041
if(!(flags & NO_FRM_RENAME)
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;
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.
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))
3770
build_table_filename(path, sizeof(path), db, table_name, "", 0);
3741
build_table_filename(path, sizeof(path), db, table_name, false);
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 */
3802
strcpy(new_name_buff,new_name);
3803
strcpy(new_alias= new_alias_buff, new_name);
3804
if (lower_case_table_names)
3806
if (lower_case_table_names != 2)
3808
my_casedn_str(files_charset_info, new_name_buff);
3809
new_alias= new_name; // Create lower case table name
3811
my_casedn_str(files_charset_info, new_name);
3773
strcpy(new_name_buff, new_name);
3774
strcpy(new_alias_buff, new_name);
3775
new_alias= new_alias_buff;
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);
3813
3781
if (new_db == db &&
3814
3782
!my_strcasecmp(table_alias_charset, new_name_buff, table_name))
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)
3846
3814
/* Table will be closed by Session::executeCommand() */
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))
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);
4029
3996
/* Create a temporary table with the new format */
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;
4044
4013
/* Table is in session->temporary_tables */
4045
4014
new_table= open_table(session, &tbl, (bool*) 0, DRIZZLE_LOCK_IGNORE_FLUSH);
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, "",
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);
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);
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);
4179
4146
/* Try to get everything back. */
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);
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);