48 lines
1.4 KiB
SQL
48 lines
1.4 KiB
SQL
DELIMITER $$
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`worker_isBoss`(vUserId INT)
|
|
RETURNS int(11)
|
|
DETERMINISTIC
|
|
BEGIN
|
|
/**
|
|
* Comprueba por jerarquía si el trabajador actual es jefe
|
|
* de un trabajador en concreto.
|
|
*
|
|
* @param vSelf Id del trabajador que se desea comprobar.
|
|
* @return Revuelve verdadero si es jefe del empleado por escala jerárquica.
|
|
*/
|
|
DECLARE vWorkerId INT;
|
|
DECLARE vBossId INT;
|
|
|
|
SELECT id INTO vWorkerId
|
|
FROM worker
|
|
WHERE id = vSelf;
|
|
IF vWorkerId IS NULL THEN
|
|
CALL util.throw('USER_NOT_FOUND');
|
|
END IF;
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tCheckedWorker;
|
|
CREATE TEMPORARY TABLE tCheckedWorker
|
|
(PRIMARY KEY (workerFk))
|
|
ENGINE = MEMORY
|
|
SELECT id workerFk FROM worker LIMIT 0;
|
|
LOOP
|
|
SELECT bossFk INTO vBossId
|
|
FROM worker
|
|
WHERE id = vWorkerId;
|
|
IF (SELECT COUNT(*) FROM tCheckedWorker WHERE workerFk = vBossId) THEN
|
|
CALL util.throw('INFINITE_LOOP');
|
|
END IF;
|
|
IF vBossId = vWorkerId THEN
|
|
RETURN FALSE;
|
|
ELSEIF vBossId = account.myUser_getId() THEN
|
|
RETURN TRUE;
|
|
ELSE
|
|
INSERT INTO tCheckedWorker VALUES (vWorkerId);
|
|
SET vWorkerId = vBossId;
|
|
END IF;
|
|
END LOOP;
|
|
DROP TEMPORARY TABLE tCheckedWorker;
|
|
RETURN FALSE;
|
|
END$$
|
|
DELIMITER ;
|