コード例 #1
0
 def insert(self, data):
     self._check_primary(data)
     data = self._before_insert(data)
     insert_instance = Node(self.table_name, **data)
     connection = get_connection()
     connection.create(insert_instance)
     return self.queryone(primary_key=self.primary_key)
コード例 #2
0
    def __query_meta(self, filter=None, order_by=None, limit=None, skip=None):
        '''
        :param filter:
        :param order_by:  match.order_by("_.name", "max(_.a, _.b)")
        :param limit: number
        :param skip: number
        :return:
        '''
        filter = filter or {}
        connection = get_connection()
        node = connection.nodes.match(self.table_name, **filter)
        if order_by:
            node = node.order_by(order_by)
        if skip:
            node = node.skip(skip)
        if limit:
            node = node.limit(limit)

        _result = []
        for _data in node:
            _tm_data = dict(_data)
            _tm_data["xid"] = _data.identity
            _result.append(_tm_data)

        return _result
コード例 #3
0
    def delete_relation(self, filter):
        '''

        :param filter: dict
        :return:
        '''
        connection = get_connection()
        res = connection.relationships.match(**filter).first()
        if res:
            connection.delete(res)
            return 1
        else:
            return 0
コード例 #4
0
    def __queryone_meta(self, filter=None):
        '''
        :param filter:
        :return:
        '''
        filter = filter or {}
        connection = get_connection()
        node = connection.nodes.match(self.table_name, **filter).first()
        _result = {}
        if node:
            _result = dict(node)
            _result["xid"] = node.identity

        return _result
コード例 #5
0
 def __delete_many_meta(self, filter=None):
     '''
     py2neo只能一条一条删除
     :param filter:
     :return:
     '''
     filter = filter or {}
     connection = get_connection()
     nodes = connection.nodes.match(self.table_name, **filter)
     _result = 0
     for _node in nodes:
         connection.delete(_node)
         _result += 1
     return _result
コード例 #6
0
    def __delete_meta(self, filter=None):
        '''

        :param filter:
        :return:
        '''
        filter = filter or {}
        connection = get_connection()
        node = connection.nodes.match(self.table_name, **filter).first()
        _result = 0
        if node:
            connection.delete(node)
            _result = 1
        return _result
コード例 #7
0
    def match_relation(self, re_type=None):
        _result = []
        connection = get_connection()
        relations = connection.match(re_type=re_type)
        for relation in relations:
            retype = re_type or relation.__class__.__name__
            _res = {
                retype: {
                    "start": dict(relation.start_node),
                    "end_node": dict(relation.end_node)
                }
            }
            _result.append(_res)

        return _result
コード例 #8
0
    def match_node_relation(self, primary_key1, primary_key2, re_type=None):
        _result = []
        connection = get_connection()
        key1 = {self.primary_key: primary_key1}
        key2 = {self.primary_key: primary_key2}
        node1 = connection.nodes.match(self.table_name, **key1).first()
        node2 = connection.nodes.match(self.table_name, **key2).first()
        if node1 is None or node2 is None:
            return _result
        relations = connection.match(nodes=(node1, node2), re_type=re_type)
        for relation in relations:
            retype = re_type or relation.__class__.__name__
            _result.append(retype)

        return _result
コード例 #9
0
 def __update_many_meta(self, data, filter=None):
     '''
     py2neo只能一条一条更新, 更新速率可能比较慢
     :param filter:
     :return:
     '''
     filter = filter or {}
     connection = get_connection()
     nodes = connection.nodes.match(self.table_name, **filter)
     _result = []
     for _node in nodes:
         _node.update(**data)
         connection.push(_node)
         _res = dict(_node)
         _res["xid"] = _node.identity
         _result.append(_res)
     return _result
コード例 #10
0
 def create_relation(self, primary_key1, primary_key2, re_type, **kwargs):
     '''
     :param primary_key1:
     :param primary_key2:
     :param re_type:
     :param kwargs: 可设置关系键, 用于查找关系
     :return:
     '''
     connection = get_connection()
     key1 = {self.primary_key: primary_key1}
     key2 = {self.primary_key: primary_key2}
     node1 = connection.nodes.match(self.table_name, **key1).first()
     node2 = connection.nodes.match(self.table_name, **key2).first()
     if node1 is None or node2 is None:
         return 0
     s = Relationship(node1, re_type, node2, **kwargs)
     connection.create(s)
     return 1
コード例 #11
0
    def match_node(self, primary_key1, re_type=None):
        _result = []
        connection = get_connection()
        key1 = {self.primary_key: primary_key1}
        node1 = connection.nodes.match(self.table_name, **key1).first()
        if node1 is None:
            return _result
        relations = connection.match(nodes=(node1, ), re_type=re_type)
        for relation in relations:
            retype = re_type or relation.__class__.__name__
            _res = {
                retype: {
                    "start": dict(relation.start_node),
                    "end_node": dict(relation.end_node)
                }
            }
            _result.append(_res)

        return _result
コード例 #12
0
    def deep_match_relation(self, primary_key1, primary_key2, deep=None):
        _result = {}
        deep = None or 1
        connection = get_connection()
        key1 = {self.primary_key: primary_key1}
        key2 = {self.primary_key: primary_key2}
        node1 = connection.nodes.match(self.table_name, **key1).first()
        node2 = connection.nodes.match(self.table_name, **key2).first()
        if node1 is None or node2 is None:
            return _result

        n1 = node1.identity
        n2 = node2.identity
        cql = "START d=node(%s), e=node(%s) MATCH p = shortestpath((d)-[*..%s]-(e)) RETURN p" % (
            n1, n2, deep)
        results = connection.run(cql)
        for res in results:
            _result[str(res[0])] = self.__fetch_nodes(res.to_subgraph().nodes)

        return _result
コード例 #13
0
 def connector(self):
     if self.connection is None:
         self.connection = get_connection()