salix/db/routines/vn2008/procedures/nest_child_add.sql

49 lines
935 B
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`nest_child_add`(
vTable VARCHAR(45)
,vChild VARCHAR(45)
,vFatherId INT
)
BEGIN
DECLARE vMyLeft INT;
SET vTable = util.quoteIdentifier(vTable);
DROP TEMPORARY TABLE IF EXISTS aux;
CREATE TEMPORARY TABLE aux
SELECT 0 as lft;
EXECUTE IMMEDIATE CONCAT(
'UPDATE aux
SET lft = (SELECT lft
FROM ', vTable,
' WHERE id = ?)')
USING vFatherId;
SELECT lft INTO vMyLeft FROM aux;
DROP TEMPORARY TABLE aux;
EXECUTE IMMEDIATE CONCAT(
'UPDATE ', vTable, '
SET rgt = rgt + 2
WHERE rgt > ?
ORDER BY rgt DESC')
USING vMyLeft;
EXECUTE IMMEDIATE CONCAT(
'UPDATE ', vTable, '
SET lft = lft + 2
WHERE lft > ?
ORDER BY lft DESC')
USING vMyLeft;
EXECUTE IMMEDIATE CONCAT(
'INSERT INTO ', vTable, ' (name, lft, rgt)
VALUES(?, ? + 1, ? + 2)')
USING vChild,
vMyLeft,
vMyLeft;
END$$
DELIMITER ;