diff --git a/sql/opt_subselect.cc b/sql/opt_subselect.cc index 5640358d363..81ffe95480c 100644 --- a/sql/opt_subselect.cc +++ b/sql/opt_subselect.cc @@ -28,6 +28,7 @@ #include "sql_select.h" #include "sql_update.h" // class Sql_cmd_update #include "sql_delete.h" // class Sql_cmd_delete +#include "sql_table.h" // make_tmp_table_name #include "filesort.h" #include "opt_subselect.h" #include "sql_test.h" @@ -4780,8 +4781,8 @@ SJ_TMP_TABLE::create_sj_weedout_tmp_table(THD *thd) else { /* if we run out of slots or we are not using tempool */ - sprintf(path,"%s-subquery-%lx-%lx-%x", tmp_file_prefix,current_pid, - (ulong) thd->thread_id, thd->tmp_table++); + LEX_STRING tmp= { path, sizeof(path) }; + make_tmp_table_name(thd, &tmp, "subquery"); } fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME); diff --git a/sql/sql_alter.cc b/sql/sql_alter.cc index 607a8e84013..d435821aa47 100644 --- a/sql/sql_alter.cc +++ b/sql/sql_alter.cc @@ -454,15 +454,8 @@ Alter_table_ctx::Alter_table_ctx(THD *thd, TABLE_LIST *table_list, } tmp_name.str= tmp_name_buff; - tmp_name.length= my_snprintf(tmp_name_buff, sizeof(tmp_name_buff), - "%s-alter-%lx-%llx", - tmp_file_prefix, current_pid, thd->thread_id); - /* Safety fix for InnoDB */ - if (lower_case_table_names) - { - // Ok to latin1, as the file name is in the form '#sql-alter-abc-def' - tmp_name.length= my_casedn_str_latin1(tmp_name_buff); - } + tmp_name.length= sizeof(tmp_name_buff); + make_tmp_table_name(thd, (LEX_STRING*) &tmp_name, "alter"); if (table_list->table->s->tmp_table == NO_TMP_TABLE) { diff --git a/sql/sql_partition_admin.cc b/sql/sql_partition_admin.cc index 9cc29391d76..10c451c365f 100644 --- a/sql/sql_partition_admin.cc +++ b/sql/sql_partition_admin.cc @@ -654,14 +654,10 @@ bool Sql_cmd_alter_table_exchange_partition:: swap_table_list->db.str, swap_table_list->table_name.str, "", 0); - /* create a unique temp name */ - my_snprintf(temp_name, sizeof(temp_name), "%s-exchange-%lx-%llx", - tmp_file_prefix, current_pid, thd->thread_id); - if (lower_case_table_names) - { - // Ok to use latin1 as the file name is in the form '#sql-exchange-abc-def' - my_casedn_str_latin1(temp_name); - } + + LEX_STRING tmp= { temp_name, sizeof(temp_name) }; + make_tmp_table_name(thd, &tmp, "exchange"); + build_table_filename(temp_file_name, sizeof(temp_file_name), table_list->next_local->db.str, temp_name, "", FN_IS_TMP); diff --git a/sql/sql_select.cc b/sql/sql_select.cc index fb00d2f2480..c63a05904c1 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -21807,8 +21807,8 @@ TABLE *Create_tmp_table::start(THD *thd, else { /* if we run out of slots or we are not using tempool */ - sprintf(path, "%s-%s-%lx-%llx-%x", tmp_file_prefix, param->tmp_name, - current_pid, thd->thread_id, thd->tmp_table++); + LEX_STRING tmp= {path, sizeof(path) }; + make_tmp_table_name(thd, &tmp, param->tmp_name); } /* diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 7d0b6572b7f..3880822f269 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -635,6 +635,25 @@ uint build_table_filename(char *buff, size_t bufflen, const char *db, } +/** + Create a temporary table name + + @param to string. Note that to->length should contain buffer length +*/ + +void make_tmp_table_name(THD *thd, LEX_STRING *to, const char *prefix) +{ + to->length= my_snprintf((char*) to->str, to->length, "%s-%s-%lx-%llx-%x", + tmp_file_prefix, prefix, current_pid, + thd->thread_id, thd->tmp_table++); + if (lower_case_table_names) + { + // Ok to use latin1 as the file name is in the form '#sql-exchange-abc-def' + my_casedn_str_latin1(to->str); + } +} + + /** Create path to a temporary table mysql_tmpdir/#sql-temptable-1234-12-1 (i.e. to its .FRM file but without an extension). @@ -652,18 +671,15 @@ uint build_table_filename(char *buff, size_t bufflen, const char *db, uint build_tmptable_filename(THD* thd, char *buff, size_t bufflen) { + LEX_STRING name; DBUG_ENTER("build_tmptable_filename"); - char *p= strnmov(buff, mysql_tmpdir, bufflen); - my_snprintf(p, bufflen - (p - buff), "/%s-temptable-%lx-%llx-%x", - tmp_file_prefix, current_pid, - thd->thread_id, thd->tmp_table++); + char *p= strnmov(buff, mysql_tmpdir, bufflen-2); + *p++= '/'; - if (lower_case_table_names) - { - /* Convert all except tmpdir to lower case */ - my_casedn_str_latin1(p); - } + name= {p, bufflen - (p - buff) }; + + make_tmp_table_name(thd, &name, "temptable"); size_t length= unpack_filename(buff, buff); DBUG_PRINT("exit", ("buff: '%s'", buff)); @@ -707,9 +723,10 @@ uint build_table_shadow_filename(char *buff, size_t bufflen, bool backup) { char tmp_name[FN_REFLEN]; - my_snprintf(tmp_name, sizeof (tmp_name), "%s-%s-%lx-%s", tmp_file_prefix, + + my_snprintf(tmp_name, sizeof(tmp_name), "%s-%s-%lx-%s", tmp_file_prefix, backup ? "backup" : "shadow", - (ulong) current_thd->thread_id, lpt->alter_info->table_name.str); + (ulong) lpt->thd->thread_id, lpt->alter_info->table_name.str); return build_table_filename(buff, bufflen, lpt->alter_info->db.str, tmp_name, "", FN_IS_TMP); } diff --git a/sql/sql_table.h b/sql/sql_table.h index 504e534e94c..85cf9a3d2f3 100644 --- a/sql/sql_table.h +++ b/sql/sql_table.h @@ -95,6 +95,7 @@ void build_lower_case_table_filename(char *buff, size_t bufflen, const LEX_CSTRING *table, uint flags); uint build_tmptable_filename(THD* thd, char *buff, size_t bufflen); +void make_tmp_table_name(THD *thd, LEX_STRING *to, const char *prefix); bool add_keyword_to_query(THD *thd, String *result, const LEX_CSTRING *keyword, const LEX_CSTRING *add);