Sh2Sh.md 2.0 KB

竖井贯通关系

前置条件

1. 竖井包含的有元空间(竖井在r_sh_contain_si表中有数据)

处理流程

1. 竖井包含有相同的元空间, 则认为两竖井贯通

函数

-- 竖井贯通关系
create or replace function public.rel_sh2sh(project_id character varying) returns boolean
as
$$
def is_exist(shaft1, shaft2, rel_dict):
    if shaft1 in rel_dict:
        if shaft2 in rel_dict[shaft1]:
            return True
    if shaft2 in rel_dict:
        if shaft1 in rel_dict[shaft2]:
            return True
    return False

try:
    # 将下面对数据库的操作作为一个事务, 出异常则自动rollback
    with plpy.subtransaction():
        delete_plan = plpy.prepare("delete from r_sh_through_sh where project_id = $1 and sign = 2", ["text"])
        delete_plan.execute([project_id])
        sh_plan = plpy.prepare("select s1.shaft_id s1, s2.shaft_id s2 from r_sh_contain_si s1 inner join r_sh_contain_si s2 on s1.ispace_id = s2.ispace_id where s1.project_id = $1 and s2.project_id = $1 and s1.shaft_id != s2.shaft_id", ["text"])
        rel = sh_plan.execute([project_id])
        rel_dict = dict()
        for row in rel:
            shaft1 = row['s1']
            shaft2 = row['s2']
            if shaft1 == None or shaft2 == None:
                continue
            if not is_exist(shaft1, shaft2, rel_dict):
                if shaft1 not in rel_dict:
                    rel_dict[shaft1] = set()
                rel_dict[shaft1].add(shaft2)
        for shaft1, sub_set in rel_dict.items():
            for shaft2 in sub_set:
                plan = plpy.prepare("insert into r_sh_through_sh(shaft_id, shaft_other_id, project_id, sign) values($1, $2, $3, 2)", ["text", "text", "text"])
                plan.execute([shaft1, shaft2, project_id])
except Exception as e:
    plpy.warning(e)
    return False
else:
    return True
$$
LANGUAGE 'plpython3u' VOLATILE;


select public.rel_sh2sh('Pj1102290002')

入参

1. 项目id

例子

select public.rel_sh2sh('Pj1102290002');