py2neo代码封装

py2neo代码封装

from typing import Text, Dict
from py2neo import Node, Relationship, Graph, NodeMatcher, RelationshipMatcher

class Neo4jDB(object):
    def __init__(self,
                 host: Text,
                 port: int,
                 username: Text,
                 password: Text):
        self.ip = f"http://{host}/:{str(port)}"
        self.username = username
        self.password = password

    def get_client(self):
        graph = Graph(self.ip, username=self.username, password=self.password)
        return graph

    def find_node_matcher(self,
                  graph:Graph,
                  label:Text,
                  condition:Dict):
        return NodeMatcher(graph=graph).match(label).where(**condition)

    def find_node_one(self,
                      graph:Graph,
                      label: Text,
                      condition: Dict):
        return self.find_node_matcher(graph=graph,
                                      label=label,
                                      condition=condition).first()

    def find_node_all(self,
                      graph: Graph,
                      label: Text,
                      condition: Dict
                      ):
        return self.find_node_matcher(graph=graph,
                                      label=label,
                                      condition=condition).all()

    def find_relation_matcher(self,
                      graph:Graph,
                      node1:Node,
                      node2:Node,
                      r_type:Text,
                      condition:Dict):
        return RelationshipMatcher(graph).match((node1, node2), r_type=r_type).where(**condition)

    def find_relation_one(self,
                          graph: Graph,
                          node1: Node,
                          node2: Node,
                          r_type: Text,
                          condition: Dict
                          ):
        return self.find_relation_matcher(graph=graph,
                                          node1=node1,
                                          node2=node2,
                                          r_type=r_type,
                                          condition=condition).first()

    def find_relation_all(self,
                          graph: Graph,
                          node1: Node,
                          node2: Node,
                          r_type: Text,
                          condition: Dict
                          ):
        return self.find_relation_matcher(graph=graph,
                                          node1=node1,
                                          node2=node2,
                                          r_type=r_type,
                                          condition=condition).all()

    def add_node(self,
                 graph:Graph,
                 label:Text,
                 properties:Dict):

        if "name" not in properties:
            raise ValueError
        condition = {"name": properties["name"]}

        if self.find_node_one(graph=graph, label=label, condition=condition):
            return None
        n = graph.create(Node(label, **properties))
        return n

    def add_relation(self,
                     graph:Graph,
                     label1:Text,
                     label2:Text,
                     properties:Dict,
                     r_type:Text,
                     node1_condition:Dict,
                     node2_condition:Dict):
        node1 = self.find_node_one(graph=graph, label=label1, condition=node1_condition)
        node2 = self.find_node_one(graph=graph, label=label2, condition=node2_condition)
        if node1 is None or node2 is None:
            return None

        ## 判断是否存在关系
        if self.find_relation_one(graph=graph, node1=node1, node2=node2, r_type=r_type, condition={}):
            return None

        n = graph.create(Relationship(node1, r_type, node2, **properties))
        return n

    def update_properties(self,
                          graph:Graph,
                          label:Text,
                          properties:Dict,
                          condition:Dict):

        nodes = self.find_node_all(graph=graph, label=label, condition=condition)
        for n in nodes:
            n.update(**properties)
            graph.push(n)

    def delete_all(self, graph:Graph):
        graph.delete_all()

    def delete_node(self, node:Node):
        graph.delete(node)

    def delete_relation(self, rel:Relationship):
        graph.delete(rel)

    def find_by_id(self, graph:Graph, id:int):
        return NodeMatcher(graph)[id]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发呆的比目鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值