← hierarchy-data-closure-table
Add a sp of delete node
+30 −5 · 3 files changed
-
sql/p_prefix_nodes_delete_nodes_after_update.sql+22 −0 -
sql/p_prefix_nodes_move_old_paths_after_update.sql+1 −4 -
sql/prefix_nodes_move_old_paths_after_update.sql+7 −1
@@ -0,0 +1,22 @@ +DELIMITER $$ + +USE `_category`$$ + +DROP PROCEDURE IF EXISTS `p_prefix_nodes_delete_nodes_after_update`$$ + +CREATE DEFINER = `root` @`localhost` PROCEDURE `p_prefix_nodes_delete_nodes_after_update` ( + `node_id` INT UNSIGNED, + `deleted` INT UNSIGNED +) COMMENT 'Delete a node and its descendant nodes(update is_deleted = 1)' +BEGIN + UPDATE + `prefix_nodes` AS d + JOIN `prefix_nodes_paths` AS p + ON d.`id` = p.`descendant_id` + JOIN `prefix_nodes_paths` AS crumbs + ON crumbs.`descendant_id` = p.`descendant_id` SET d.`is_deleted` = deleted + WHERE p.`ancestor_id` = node_id; +END $$ + +DELIMITER ; + @@ -4,7 +4,7 @@ USE `_category`$$ DROP PROCEDURE IF EXISTS `p_prefix_nodes_move_old_paths_after_update`$$ -CREATE DEFINER=`root`@`localhost` PROCEDURE `p_prefix_nodes_move_old_paths_after_update`( +CREATE DEFINER = `root` @`localhost` PROCEDURE `p_prefix_nodes_move_old_paths_after_update` ( `node_old_parent_id` INT UNSIGNED, `node_new_parent_id` INT UNSIGNED ) COMMENT 'Update paths when parent_id column changed' @@ -15,8 +15,6 @@ BEGIN -- If parent_id has chanaged -- 1. Delete the paths between moved node and old ancestors -- 2. Add the paths between moved node and new ancestors - IF `node_old_parent_id` != `node_new_parent_id` - THEN DELETE a FROM @@ -43,7 +41,6 @@ BEGIN JOIN `prefix_nodes_paths` AS subtree WHERE subtree.`ancestor_id` = `node_old_parent_id` AND supertree.`descendant_id` = `node_new_parent_id` ; - END IF ; END$$ DELIMITER ; @@ -8,7 +8,13 @@ CREATE /*!50017 DEFINER = 'root'@'localhost' */ TRIGGER `prefix_nodes_move_old_paths_after_update` AFTER UPDATE ON `prefix_nodes` FOR EACH ROW BEGIN - CALL p_prefix_nodes_move_old_paths_after_update(OLD.`parent_id`, NEW.`parent_id`); + IF OLD.`parent_id` != NEW.`parent_id` THEN + CALL p_prefix_nodes_move_old_paths_after_update(OLD.`parent_id`, NEW.`parent_id`); + END IF; + + IF OLD.`is_deleted` != NEW.`is_deleted` THEN + CALL p_prefix_nodes_delete_nodes_after_update(NEW.`parent_id`); + END IF; END; $$