闭包表(Closure Table)

设计血缘关系(data-lineage)时,想到要使用的表模型。

表设计 

节点记录表 - node


CREATE TABLE `lineages_node` (
  `name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '节点名称',
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='节点';

节点关系表 - relation

CREATE TABLE `lineages_relation` (
  `ancestor` bigint(20) unsigned DEFAULT NULL COMMENT '祖先节点',
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `descendant` bigint(20) unsigned DEFAULT NULL COMMENT '后代节点',
  `depth` int(10) unsigned DEFAULT NULL COMMENT '相隔层级',
  PRIMARY KEY (`id`),
  KEY `lineages_relation_descendant_IDX` (`descendant`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='数据血缘-关系';

祖先 和 后代 数据表中的 约束 和 外键,还有表中其他字段,看自己业务需求

数据结构获取

获取祖先为id 1的节点的关系

SELECT elr.relation, elr.ancestor, elr.descendant FROM lineages_node eln


INNER JOIN lineages_relation elr ON eln.id = elr.ancestor


INNER JOIN lineages_node n3 ON elr.descendant = n3.id


WHERE eln.id = 1

删除id为4的节点

DELETE FROM NodeInfo WHERE node_id = 4;

DELETE FROM NodeRelation AS n1
 WHERE n1.descendant IN 
 (SELECT a.descendant FROM 
    (SELECT n2.descendant FROM NodeRelation AS n2 WHERE n2.ancestor = 4)
 AS a);

添加节点

CREATE DEFINER = `root`@`localhost` PROCEDURE `AddNode`(`_parent_name` varchar(255),`_node_name` varchar(255))

BEGIN

  DECLARE _ancestor INT(10) UNSIGNED;

  DECLARE _descendant INT(10) UNSIGNED;

  DECLARE _parent INT(10) UNSIGNED;

  IF NOT EXISTS(SELECT node_id From NodeInfo WHERE node_name = _node_name)

  THEN

    INSERT INTO NodeInfo (node_name) VALUES(_node_name);

    SET _descendant = (SELECT node_id FROM NodeInfo WHERE node_name = _node_name);

    INSERT INTO NodeRelation (ancestor,descendant,distance) VALUES(_descendant,_descendant,0);

    

    IF EXISTS (SELECT node_id FROM NodeInfo WHERE node_name = _parent_name)

    THEN

      SET _parent = (SELECT node_id FROM NodeInfo WHERE node_name = _parent_name);

      INSERT INTO NodeRelation (ancestor,descendant,distance) SELECT ancestor,_descendant,distance+1 FROM NodeRelation WHERE descendant = _parent;

    END IF;

  END IF;

END;
<?php

function addNode($nodeEntity, $parentNodeEntity) {

if (LineagesNode::getQuery()->where([['id', '=', $nodeEntity->id]])->exists()) {
            if (!empty($parentNodeEntity)) {
                /** @var LineagesNode $node */
                $node = LineagesNode::getQuery()->where([['id', '=', $nodeEntity->id]])->first();
                $nodeId = $node->id;
                // all descendants update
                $descendants = LineagesRelation::getQuery()->where([['ancestor', '=', $nodeId]])->get();
                // add new ancestor to descendants
                foreach ($descendants as $descendant) {
                    $insertBranches[] = [
                        'ancestor' => $parentNodeEntity->id,
                        'descendant' => $descendant->descendant,
                        'depth' => $descendant->depth + 1,
                    ];
                }
            }
            return true;
        } else {
            // create node and get new node id
            $nodeId = LineagesNode::getQuery()->insertGetId([
                'name' => $nodeEntity->name
            ]);
            // relationship
            $result = LineagesRelation::getQuery()->insert([
                'ancestor' => $nodeId,
                'descendant' => $nodeId,
                'depth' => 0
            ]);
            if (!empty($parentNodeEntity)) {
                // all ancestors from parent node
                $ancestors = LineagesRelation::getQuery()->where([['descendant', '=', $parentNodeEntity->id], ['deleted_at', '=', null]])->get();
                LineagesRelation::getQuery()->insert([
                    'ancestor' => $parentNodeEntity->id,
                    'descendant' => $nodeId,
                    'depth' => 1
                ]);
                LineagesRelation::getQuery()->insert([
                    'ancestor' => $parentNodeEntity->id,
                    'descendant' => $nodeId,
                    'depth' => 1
                ]);
                foreach ($ancestors as $ancestor) {
                    $insertBranches[] = [
                        'ancestor' => $ancestor->id,
                        'descendant' => $nodeId,
                        'depth' => $ancestor->depth + 1
                    ];
                }
            }
            // update
            if (!empty($insertBranches)) {
                return LineagesRelation::getQuery()->insert($insertBranches);
            }
            return $result;
        }
}

相关推荐

  1. 9、python-

    2024-06-17 18:40:05       42 阅读
  2. 关于Golang

    2024-06-17 18:40:05       36 阅读
  3. Rust

    2024-06-17 18:40:05       35 阅读
  4. Python

    2024-06-17 18:40:05       32 阅读
  5. 如何理解

    2024-06-17 18:40:05       22 阅读
  6. Python

    2024-06-17 18:40:05       16 阅读
  7. Python:

    2024-06-17 18:40:05       14 阅读
  8. 用运。

    2024-06-17 18:40:05       19 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-17 18:40:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-17 18:40:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-17 18:40:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-17 18:40:05       18 阅读

热门阅读

  1. 怎样在C语⾔中制作动画?

    2024-06-17 18:40:05       7 阅读
  2. Go 基础丨切片 slice

    2024-06-17 18:40:05       8 阅读
  3. 超级异地组网工具有哪些?

    2024-06-17 18:40:05       8 阅读
  4. vlan、vxlan、vpc学习

    2024-06-17 18:40:05       6 阅读
  5. QY-22 低功耗墒情监测站 厂家直营 无线传输

    2024-06-17 18:40:05       5 阅读
  6. pytest unittest temp path单元测试创建临时文件

    2024-06-17 18:40:05       10 阅读
  7. 上升 Stable Diffusion之最全详解图解

    2024-06-17 18:40:05       8 阅读