summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Midenkov <midenok@gmail.com>2022-02-19 16:34:08 +0300
committerAleksey Midenkov <midenok@gmail.com>2022-02-19 16:46:07 +0300
commit627654d83f99e5615b27c6d022a9b878b007a07f (patch)
tree6f7c87ad4fec6f9eddf0098c08f595143b4f94a1
parentd4f46e3f2ded691aee9f6e63b7b4a81f8731b61a (diff)
downloadmariadb-git-bb-10.8-midenok.tar.gz
TABLE::referenced_by_foreign_table()bb-10.8-midenok
-rw-r--r--sql/sql_truncate.cc29
-rw-r--r--sql/table.h1
2 files changed, 17 insertions, 13 deletions
diff --git a/sql/sql_truncate.cc b/sql/sql_truncate.cc
index c6af72c5979..920f262260c 100644
--- a/sql/sql_truncate.cc
+++ b/sql/sql_truncate.cc
@@ -116,18 +116,18 @@ static const char *fk_info_str(THD *thd, FOREIGN_KEY_INFO *fk_info)
error was emitted.
*/
-static bool
-fk_truncate_illegal_if_parent(THD *thd, TABLE *table)
+bool
+TABLE::referenced_by_foreign_table(THD *thd, FOREIGN_KEY_INFO *&fk_info) const
{
- FOREIGN_KEY_INFO *fk_info;
List<FOREIGN_KEY_INFO> fk_list;
List_iterator_fast<FOREIGN_KEY_INFO> it;
+ fk_info= NULL;
/*
Bail out early if the table is not referenced by a foreign key.
In this case, the table could only be, if at all, a child table.
*/
- if (! table->file->referenced_by_foreign_key())
+ if (! file->referenced_by_foreign_key())
return FALSE;
/*
@@ -136,7 +136,7 @@ fk_truncate_illegal_if_parent(THD *thd, TABLE *table)
of foreign keys referencing this table in order to check the name
of the child (dependent) tables.
*/
- table->file->get_parent_foreign_key_list(thd, &fk_list);
+ file->get_parent_foreign_key_list(thd, &fk_list);
/* Out of memory when building list. */
if (unlikely(thd->is_error()))
@@ -148,22 +148,19 @@ fk_truncate_illegal_if_parent(THD *thd, TABLE *table)
while ((fk_info= it++))
{
if (lex_string_cmp(system_charset_info, fk_info->referenced_db,
- &table->s->db) ||
+ &s->db) ||
lex_string_cmp(system_charset_info, fk_info->referenced_table,
- &table->s->table_name) ||
+ &s->table_name) ||
lex_string_cmp(system_charset_info, fk_info->foreign_db,
- &table->s->db) ||
+ &s->db) ||
lex_string_cmp(system_charset_info, fk_info->foreign_table,
- &table->s->table_name))
+ &s->table_name))
break;
}
/* Table is parent in a non-self-referencing foreign key. */
if (fk_info)
- {
- my_error(ER_TRUNCATE_ILLEGAL_FK, MYF(0), fk_info_str(thd, fk_info));
return TRUE;
- }
return FALSE;
}
@@ -193,6 +190,7 @@ Sql_cmd_truncate_table::handler_truncate(THD *thd, TABLE_LIST *table_ref,
int error= 0;
uint flags= 0;
TABLE *table;
+ FOREIGN_KEY_INFO *fk_info;
DBUG_ENTER("Sql_cmd_truncate_table::handler_truncate");
/*
@@ -233,8 +231,13 @@ Sql_cmd_truncate_table::handler_truncate(THD *thd, TABLE_LIST *table_ref,
/* Whether to truncate regardless of foreign keys. */
if (! (thd->variables.option_bits & OPTION_NO_FOREIGN_KEY_CHECKS))
- if (fk_truncate_illegal_if_parent(thd, table_ref->table))
+ if (table_ref->table->referenced_by_foreign_table(thd, fk_info))
+ {
+ /* Table is parent in a non-self-referencing foreign key. */
+ if (fk_info)
+ my_error(ER_TRUNCATE_ILLEGAL_FK, MYF(0), fk_info_str(thd, fk_info));
DBUG_RETURN(TRUNCATE_FAILED_SKIP_BINLOG);
+ }
table= table_ref->table;
diff --git a/sql/table.h b/sql/table.h
index 6aa75df39c6..fe54447d703 100644
--- a/sql/table.h
+++ b/sql/table.h
@@ -1777,6 +1777,7 @@ public:
void vers_update_fields();
void vers_update_end();
void find_constraint_correlated_indexes();
+ bool referenced_by_foreign_table(THD *thd, FOREIGN_KEY_INFO *&fk_info) const;
/** Number of additional fields used in versioned tables */
#define VERSIONING_FIELDS 2