示例#1
0
    def get_node(self, req: Node) -> Optional[List[Node]]:
        builder = QueryBuilder()
        if req.get_entity() is not None:
            builder.add(f"MATCH (n:{req.get_entity()}")
            if req.get_meta() is not None and len(req.get_meta()) > 0:
                builder.add_meta(req.get_meta())
        else:
            builder.add(f"MATCH (n")

        if req.get_properties() is not None:
            properties = dict(
                (x, y) for x, y in req.get_properties().items() if y != "")
            if len(properties) > 0:
                builder.add_parameters(properties)

        builder.add(")")

        if req.get_relationships() is not None:
            counter: int = 0
            for key, value in req.get_relationships().items():
                if len(value) > 0:
                    builder.add(
                        f"MATCH (n)-[r{counter}:{key}]-(m{counter}) WHERE ID(m{counter}) IN {str(value)}"
                    )
                    counter += 1
        builder.add("RETURN COLLECT(ID(n)) AS ids")

        records = self._exec_query(builder.get())[0]
        result_nodes: List[Node] = []  # nodes from result
        if len(records) > 0:
            for record in records:
                result_node = self.get_node_by_id(int(record))
                result_nodes.append(result_node[0])

        return result_nodes
示例#2
0
    def create_node(self, req: Node) -> Optional[Node]:
        builder = QueryBuilder()
        builder.add("CREATE (a:" + req.get_entity())
        if req.get_meta() is not None and len(req.get_meta()) > 0:
            builder.add_meta(req.get_meta())
        builder.add_parameters(req.get_properties()).add(") RETURN a")

        record: Neo4jNode = self._exec_query(builder.get())[0]
        req.set_id(int(record.id))
        return req