Commit f1e1c133 authored by Aleksey Midenkov's avatar Aleksey Midenkov

MDEV-28933 Moved RENAME_CONSTRAINT_IDS to include/sql_funcs.h

All InnoDB internal SQL functions should be moved to sql_funcs.h
parent a228ec80
...@@ -231,6 +231,7 @@ SET(INNOBASE_SOURCES ...@@ -231,6 +231,7 @@ SET(INNOBASE_SOURCES
include/srv0start.h include/srv0start.h
include/srw_lock.h include/srw_lock.h
include/sux_lock.h include/sux_lock.h
include/sql_funcs.h
include/trx0i_s.h include/trx0i_s.h
include/trx0purge.h include/trx0purge.h
include/trx0rec.h include/trx0rec.h
......
/*****************************************************************************
Copyright (c) 2022, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
*****************************************************************************/
/**
@file include/sql_funcs.h
SQL procedures for InnoDB system tables operation
*/
/**
Rename foreign keys for rename table
@see row_rename_table_for_mysql()
*/
constexpr const char *rename_constraint_ids=
R"===(PROCEDURE RENAME_CONSTRAINT_IDS () IS
gen_constr_prefix CHAR;
new_db_name CHAR;
foreign_id CHAR;
new_foreign_id CHAR;
old_db_name_len INT;
old_t_name_len INT;
new_db_name_len INT;
id_len INT;
offset INT;
found INT;
BEGIN
found := 1;
old_db_name_len := INSTR(:old_table_name, '/') - 1;
new_db_name_len := INSTR(:new_table_name, '/') - 1;
new_db_name := SUBSTR(:new_table_name, 0,
new_db_name_len);
old_t_name_len := LENGTH(:old_table_name);
gen_constr_prefix := CONCAT(:old_table_name_utf8,
'_ibfk_');
WHILE found = 1 LOOP
SELECT ID INTO foreign_id
FROM SYS_FOREIGN
WHERE FOR_NAME = :old_table_name
AND TO_BINARY(FOR_NAME)
= TO_BINARY(:old_table_name)
LOCK IN SHARE MODE;
IF (SQL % NOTFOUND) THEN
found := 0;
ELSE
UPDATE SYS_FOREIGN
SET FOR_NAME = :new_table_name
WHERE ID = foreign_id;
id_len := LENGTH(foreign_id);
IF (INSTR(foreign_id, '/') > 0) THEN
IF (INSTR(foreign_id,
gen_constr_prefix) > 0)
THEN
offset := INSTR(foreign_id, '_ibfk_') - 1;
new_foreign_id :=
CONCAT(:new_table_utf8,
SUBSTR(foreign_id, offset, id_len - offset));
ELSE
new_foreign_id :=
CONCAT(new_db_name,
SUBSTR(foreign_id, old_db_name_len,
id_len - old_db_name_len));
END IF;
UPDATE SYS_FOREIGN
SET ID = new_foreign_id
WHERE ID = foreign_id;
UPDATE SYS_FOREIGN_COLS
SET ID = new_foreign_id
WHERE ID = foreign_id;
END IF;
END IF;
END LOOP;
UPDATE SYS_FOREIGN SET REF_NAME = :new_table_name
WHERE REF_NAME = :old_table_name
AND TO_BINARY(REF_NAME)
= TO_BINARY(:old_table_name);
END;)===";
...@@ -72,6 +72,8 @@ Created 9/17/2000 Heikki Tuuri ...@@ -72,6 +72,8 @@ Created 9/17/2000 Heikki Tuuri
#include "wsrep_mysqld.h" #include "wsrep_mysqld.h"
#endif #endif
#include "sql_funcs.h"
/*******************************************************************//** /*******************************************************************//**
Delays an INSERT, DELETE or UPDATE operation if the purge is lagging. */ Delays an INSERT, DELETE or UPDATE operation if the purge is lagging. */
static static
...@@ -2725,72 +2727,7 @@ row_rename_table_for_mysql( ...@@ -2725,72 +2727,7 @@ row_rename_table_for_mysql(
pars_info_add_str_literal(info, "new_table_utf8", new_table_name); pars_info_add_str_literal(info, "new_table_utf8", new_table_name);
err = que_eval_sql( err = que_eval_sql(info, rename_constraint_ids, trx);
info,
"PROCEDURE RENAME_CONSTRAINT_IDS () IS\n"
"gen_constr_prefix CHAR;\n"
"new_db_name CHAR;\n"
"foreign_id CHAR;\n"
"new_foreign_id CHAR;\n"
"old_db_name_len INT;\n"
"old_t_name_len INT;\n"
"new_db_name_len INT;\n"
"id_len INT;\n"
"offset INT;\n"
"found INT;\n"
"BEGIN\n"
"found := 1;\n"
"old_db_name_len := INSTR(:old_table_name, '/')-1;\n"
"new_db_name_len := INSTR(:new_table_name, '/')-1;\n"
"new_db_name := SUBSTR(:new_table_name, 0,\n"
" new_db_name_len);\n"
"old_t_name_len := LENGTH(:old_table_name);\n"
"gen_constr_prefix := CONCAT(:old_table_name_utf8,\n"
" '_ibfk_');\n"
"WHILE found = 1 LOOP\n"
" SELECT ID INTO foreign_id\n"
" FROM SYS_FOREIGN\n"
" WHERE FOR_NAME = :old_table_name\n"
" AND TO_BINARY(FOR_NAME)\n"
" = TO_BINARY(:old_table_name)\n"
" LOCK IN SHARE MODE;\n"
" IF (SQL % NOTFOUND) THEN\n"
" found := 0;\n"
" ELSE\n"
" UPDATE SYS_FOREIGN\n"
" SET FOR_NAME = :new_table_name\n"
" WHERE ID = foreign_id;\n"
" id_len := LENGTH(foreign_id);\n"
" IF (INSTR(foreign_id, '/') > 0) THEN\n"
" IF (INSTR(foreign_id,\n"
" gen_constr_prefix) > 0)\n"
" THEN\n"
" offset := INSTR(foreign_id, '_ibfk_') - 1;\n"
" new_foreign_id :=\n"
" CONCAT(:new_table_utf8,\n"
" SUBSTR(foreign_id, offset,\n"
" id_len - offset));\n"
" ELSE\n"
" new_foreign_id :=\n"
" CONCAT(new_db_name,\n"
" SUBSTR(foreign_id,\n"
" old_db_name_len,\n"
" id_len - old_db_name_len));\n"
" END IF;\n"
" UPDATE SYS_FOREIGN\n"
" SET ID = new_foreign_id\n"
" WHERE ID = foreign_id;\n"
" UPDATE SYS_FOREIGN_COLS\n"
" SET ID = new_foreign_id\n"
" WHERE ID = foreign_id;\n"
" END IF;\n"
" END IF;\n"
"END LOOP;\n"
"UPDATE SYS_FOREIGN SET REF_NAME = :new_table_name\n"
"WHERE REF_NAME = :old_table_name\n"
" AND TO_BINARY(REF_NAME)\n"
" = TO_BINARY(:old_table_name);\n"
"END;\n", trx);
} else if (n_constraints_to_drop > 0) { } else if (n_constraints_to_drop > 0) {
/* Drop some constraints of tmp tables. */ /* Drop some constraints of tmp tables. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment