salix/db/changes/10081-agency/00-zone_getFromGeo.sql

59 lines
1.3 KiB
MySQL
Raw Normal View History

2019-09-26 08:08:37 +00:00
DROP PROCEDURE IF EXISTS `vn`.`zone_getFromGeo`;
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`zone_getFromGeo`(vGeoFk INT)
BEGIN
/**
2019-09-26 10:47:16 +00:00
* Returns all zones which have the passed geo included.
2019-09-26 08:08:37 +00:00
*
2019-09-26 10:47:16 +00:00
* @param vGeoFk The geo id
* @return tmp.zone(id) The list of zones
2019-09-26 08:08:37 +00:00
*/
DECLARE vChildFk INT DEFAULT vGeoFk;
DECLARE vParentFk INT;
DECLARE vLevel INT DEFAULT 1;
DROP TEMPORARY TABLE IF EXISTS tNodes;
CREATE TEMPORARY TABLE tNodes
(PRIMARY KEY (id))
ENGINE = MEMORY
SELECT vGeoFk id, vLevel `level`;
myLoop: LOOP
SELECT parentFk INTO vParentFk
FROM zoneGeo
WHERE id = vChildFk;
SET vChildFk = vParentFk;
SET vLevel = vLevel + 1;
INSERT IGNORE INTO tNodes
SELECT vChildFk, vLevel
FROM DUAL
WHERE vChildFk IS NOT NULL;
IF ROW_COUNT() = 0 THEN
LEAVE myLoop;
END IF;
END LOOP;
DROP TEMPORARY TABLE IF EXISTS tmp.zone;
CREATE TEMPORARY TABLE tmp.zone
(INDEX (id))
2019-09-26 10:47:16 +00:00
ENGINE = MEMORY
2019-09-26 08:08:37 +00:00
SELECT id FROM (
SELECT zoneFk id, isIncluded
FROM (
SELECT i.zoneFk, i.isIncluded
FROM tNodes n
JOIN zoneIncluded i ON i.geoFk = n.id
ORDER BY zoneFk, n.`level`
) t
GROUP BY id HAVING isIncluded
) t;
DROP TEMPORARY TABLE tNodes;
END$$
DELIMITER ;