-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcypher.py
56 lines (46 loc) · 2.11 KB
/
cypher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Library for functions related to cypher queries.
"""
from enum import Enum
from string import Template
def construct_query(src_type='', src_id='', rel_type='', dest_type='',
action='RETURN', action_entity='ID(dest)'):
if src_type:
src_type = ':' + src_type
if rel_type:
rel_type = ':' + rel_type
if dest_type:
dest_type = ':' + dest_type
query = Template('MATCH (src$src_type)-[rel$rel_type]-(dest$dest_type) WHERE ID(src)=$src_id $action $action_entity').substitute(
src_type=src_type, rel_type=rel_type, dest_type=dest_type,
src_id=src_id, action=action, action_entity=action_entity)
return query
def one_hop_from_id(graph, src_id, src_type, rel_type, dest_type,
action_entity):
"""Returns list of matches. Each match is a dictionary with keys being
the action_entity specified."""
query = construct_query(src_type=src_type, src_id=src_id,
rel_type=rel_type, dest_type=dest_type,
action_entity=action_entity)
matches = graph.run(query).data()
return matches
def delete_relationships_from(graph, src_id, src_type=None, rel_type=None,
dest_type=None):
""" Deletes relationships of type rel_type connected to node w/ src_id."""
query = construct_query(src_type=src_type, src_id=src_id,
rel_type=rel_type, dest_type=dest_type,
action='DELETE', action_entity='rel')
graph.run(query)
def delete_node(node, graph):
""" Deletes a node and all its relationships from graph."""
src_type = type(node).__name__
src_id = node.__primaryvalue__
# Delete all relationships.
delete_rels_query = construct_query(src_type=src_type, src_id=src_id,
action='DELETE', action_entity='rel')
# Delete node itself.
delete_node_query = Template(
'MATCH(src: $src_type) WHERE ID(src)=$src_id DELETE src'
).substitute(src_type=src_type, src_id=src_id)
graph.run(delete_rels_query)
graph.run(delete_node_query)