Added make_tmp_table_name() to simplify creating temporary table names

This commit is contained in:
Monty 2025-01-05 14:50:45 +02:00
parent efc5d3f84d
commit c234a312d7
6 changed files with 40 additions and 32 deletions

View File

@ -28,6 +28,7 @@
#include "sql_select.h" #include "sql_select.h"
#include "sql_update.h" // class Sql_cmd_update #include "sql_update.h" // class Sql_cmd_update
#include "sql_delete.h" // class Sql_cmd_delete #include "sql_delete.h" // class Sql_cmd_delete
#include "sql_table.h" // make_tmp_table_name
#include "filesort.h" #include "filesort.h"
#include "opt_subselect.h" #include "opt_subselect.h"
#include "sql_test.h" #include "sql_test.h"
@ -4780,8 +4781,8 @@ SJ_TMP_TABLE::create_sj_weedout_tmp_table(THD *thd)
else else
{ {
/* if we run out of slots or we are not using tempool */ /* if we run out of slots or we are not using tempool */
sprintf(path,"%s-subquery-%lx-%lx-%x", tmp_file_prefix,current_pid, LEX_STRING tmp= { path, sizeof(path) };
(ulong) thd->thread_id, thd->tmp_table++); make_tmp_table_name(thd, &tmp, "subquery");
} }
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME); fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);

View File

@ -454,15 +454,8 @@ Alter_table_ctx::Alter_table_ctx(THD *thd, TABLE_LIST *table_list,
} }
tmp_name.str= tmp_name_buff; tmp_name.str= tmp_name_buff;
tmp_name.length= my_snprintf(tmp_name_buff, sizeof(tmp_name_buff), tmp_name.length= sizeof(tmp_name_buff);
"%s-alter-%lx-%llx", make_tmp_table_name(thd, (LEX_STRING*) &tmp_name, "alter");
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);
}
if (table_list->table->s->tmp_table == NO_TMP_TABLE) if (table_list->table->s->tmp_table == NO_TMP_TABLE)
{ {

View File

@ -654,14 +654,10 @@ bool Sql_cmd_alter_table_exchange_partition::
swap_table_list->db.str, swap_table_list->db.str,
swap_table_list->table_name.str, swap_table_list->table_name.str,
"", 0); "", 0);
/* create a unique temp name */
my_snprintf(temp_name, sizeof(temp_name), "%s-exchange-%lx-%llx", LEX_STRING tmp= { temp_name, sizeof(temp_name) };
tmp_file_prefix, current_pid, thd->thread_id); make_tmp_table_name(thd, &tmp, "exchange");
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);
}
build_table_filename(temp_file_name, sizeof(temp_file_name), build_table_filename(temp_file_name, sizeof(temp_file_name),
table_list->next_local->db.str, table_list->next_local->db.str,
temp_name, "", FN_IS_TMP); temp_name, "", FN_IS_TMP);

View File

@ -21807,8 +21807,8 @@ TABLE *Create_tmp_table::start(THD *thd,
else else
{ {
/* if we run out of slots or we are not using tempool */ /* 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, LEX_STRING tmp= {path, sizeof(path) };
current_pid, thd->thread_id, thd->tmp_table++); make_tmp_table_name(thd, &tmp, param->tmp_name);
} }
/* /*

View File

@ -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 Create path to a temporary table mysql_tmpdir/#sql-temptable-1234-12-1
(i.e. to its .FRM file but without an extension). (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) uint build_tmptable_filename(THD* thd, char *buff, size_t bufflen)
{ {
LEX_STRING name;
DBUG_ENTER("build_tmptable_filename"); DBUG_ENTER("build_tmptable_filename");
char *p= strnmov(buff, mysql_tmpdir, bufflen); char *p= strnmov(buff, mysql_tmpdir, bufflen-2);
my_snprintf(p, bufflen - (p - buff), "/%s-temptable-%lx-%llx-%x", *p++= '/';
tmp_file_prefix, current_pid,
thd->thread_id, thd->tmp_table++);
if (lower_case_table_names) name= {p, bufflen - (p - buff) };
{
/* Convert all except tmpdir to lower case */ make_tmp_table_name(thd, &name, "temptable");
my_casedn_str_latin1(p);
}
size_t length= unpack_filename(buff, buff); size_t length= unpack_filename(buff, buff);
DBUG_PRINT("exit", ("buff: '%s'", buff)); DBUG_PRINT("exit", ("buff: '%s'", buff));
@ -707,9 +723,10 @@ uint build_table_shadow_filename(char *buff, size_t bufflen,
bool backup) bool backup)
{ {
char tmp_name[FN_REFLEN]; 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", 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, return build_table_filename(buff, bufflen, lpt->alter_info->db.str, tmp_name,
"", FN_IS_TMP); "", FN_IS_TMP);
} }

View File

@ -95,6 +95,7 @@ void build_lower_case_table_filename(char *buff, size_t bufflen,
const LEX_CSTRING *table, const LEX_CSTRING *table,
uint flags); uint flags);
uint build_tmptable_filename(THD* thd, char *buff, size_t bufflen); 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, bool add_keyword_to_query(THD *thd, String *result, const LEX_CSTRING *keyword,
const LEX_CSTRING *add); const LEX_CSTRING *add);