fix: refs #6421 worker_getHierarchy infinite loop

This commit is contained in:
Guillermo Bonet 2024-09-18 13:43:00 +02:00
parent 4dd70ccc69
commit e3c92c3644
1 changed files with 7 additions and 4 deletions

View File

@ -1,5 +1,7 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`worker_getHierarchy`(vUserFk INT) CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`worker_getHierarchy`(
vUserFk INT
)
BEGIN BEGIN
/** /**
* Retorna una tabla temporal con los trabajadores que tiene * Retorna una tabla temporal con los trabajadores que tiene
@ -12,13 +14,14 @@ BEGIN
(PRIMARY KEY (workerFk)) (PRIMARY KEY (workerFk))
ENGINE = MEMORY ENGINE = MEMORY
WITH RECURSIVE workerHierarchy AS ( WITH RECURSIVE workerHierarchy AS (
SELECT id workerFk, bossFk, 0 depth SELECT id workerFk, bossFk, 0 `depth`, CAST(id AS CHAR(255)) `path`
FROM vn.worker FROM vn.worker
WHERE id = vUserFk WHERE id = vUserFk
UNION ALL UNION ALL
SELECT w.id, w.bossFk, wh.depth + 1 SELECT w.id, w.bossFk, wh.`depth` + 1, CONCAT(wh.`path`, ',', w.id)
FROM vn.worker w FROM vn.worker w
JOIN workerHierarchy wh ON w.bossFk = wh.workerFk JOIN workerHierarchy wh ON w.bossFk = wh.workerFk
WHERE NOT FIND_IN_SET(w.id, wh.`path`)
) )
SELECT * SELECT *
FROM workerHierarchy FROM workerHierarchy