DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`zoneGeo_doCalc`()
proc: BEGIN
/**
 * Recalculates the zones tree.
 */
    DECLARE vIsChanged BOOL;

	DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
    BEGIN
		DO RELEASE_LOCK('vn.zoneGeo_doCalc');
        RESIGNAL;
	END;

	IF !GET_LOCK('vn.zoneGeo_doCalc', 0) THEN
		LEAVE proc;
	END IF;

	SELECT isChanged INTO vIsChanged
		FROM zoneGeoRecalc;
        
	IF vIsChanged THEN
		UPDATE zoneGeoRecalc SET isChanged = FALSE;
        CALL vn.zoneGeo_calcTree;
	END IF;

	DO RELEASE_LOCK('vn.zoneGeo_doCalc');
END$$
DELIMITER ;
