DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`balanceNestTree_delete`(
    vSelf INT
)
BEGIN
/**
 * Elimina un nodo dentro de la estructura de árbol de vn.balanceNestTree.
 *
 * @param vSelf Identificador del nodo
 */
	DECLARE vTable VARCHAR(45) DEFAULT util.quoteIdentifier('balanceNestTree');
	DECLARE vRight INT;
	DECLARE vLeft INT;
	DECLARE vWidth INT;

	CREATE OR REPLACE TEMPORARY TABLE tAux
		SELECT 0 rgt, 0 lft, 0 wdt;

	EXECUTE IMMEDIATE CONCAT(
		'UPDATE tAux a 
				JOIN ', vTable, ' t 
			SET a.rgt = t.rgt, 
				a.lft = t.lft,
				a.wdt = t.rgt - t.lft + 1 
			WHERE t.id = ?')
		USING vSelf;

	SELECT rgt, lft, wdt 
		INTO vRight, vLeft, vWidth 
		FROM tAux;

	EXECUTE IMMEDIATE CONCAT(
		'DELETE FROM ', vTable, 
			' WHERE lft BETWEEN ? AND ?')
		USING vLeft, vRight;

	EXECUTE IMMEDIATE CONCAT(
		'UPDATE ', vTable, 
			' SET rgt = rgt - ? 
			WHERE rgt > ? 
			ORDER BY rgt')
		USING vWidth,vRight;

	EXECUTE IMMEDIATE CONCAT(
		'UPDATE ', vTable, 
			' SET lft = lft - ?
			WHERE lft > ? 
			ORDER BY lft')
		USING vWidth, vRight;

	DROP TEMPORARY TABLE tAux;
END$$
DELIMITER ;
