/* Ejecutar en prod * /

/* DROP PROCEDURE IF EXISTS nst.nodeRecalc;

DELIMITER $$
$$
CREATE DEFINER=`root`@`%` PROCEDURE `nst`.`nodeRecalc`(IN `vScheme` VARCHAR(45), IN `vTable` VARCHAR(45))
BEGIN
	CALL util.exec (sql_printf (
		'UPDATE %t.%t d
			JOIN (SELECT 
					node.id,
					COUNT(parent.id) - 1 as depth,
					cast((node.rgt - node.lft - 1) / 2 as DECIMAL) as sons
				FROM 
					%t.%t AS node,
					%t.%t AS parent
				WHERE node.lft BETWEEN parent.lft AND parent.rgt
				GROUP BY node.id
				ORDER BY node.lft) n ON n.id = d.id 
			SET d.`depth` = n.depth, d.sons = n.sons',
		vScheme,
		vTable,
		vScheme,
		vTable,
		vScheme,
		vTable
	));
END$$
DELIMITER ;
 */