Class: Neo4j::Http::RelationshipClient

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4j/http/relationship_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cypher_client) ⇒ RelationshipClient

Returns a new instance of RelationshipClient.



10
11
12
# File 'lib/neo4j/http/relationship_client.rb', line 10

def initialize(cypher_client)
  @cypher_client = cypher_client
end

Class Method Details

.defaultObject



6
7
8
# File 'lib/neo4j/http/relationship_client.rb', line 6

def self.default
  @default ||= new(CypherClient.default)
end

Instance Method Details

#build_match_selector(name, data) ⇒ Object



69
70
71
72
73
# File 'lib/neo4j/http/relationship_client.rb', line 69

def build_match_selector(name, data)
  selector = +"#{name}:#{data.label}"
  selector << " {#{data.key_name}: $#{name}.#{data.key_name}}" if data.key_name.present?
  selector
end

#delete_relationship(relationship:, from:, to:) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/neo4j/http/relationship_client.rb', line 75

def delete_relationship(relationship:, from:, to:)
  from_selector = build_match_selector(:from, from)
  to_selector = build_match_selector(:to, to)
  relationship_selector = build_match_selector(:relationship, relationship)

  cypher = "    MATCH (\#{from_selector}) - [\#{relationship_selector}] - (\#{to_selector})\n    WITH from, to, relationship\n    DELETE relationship\n    RETURN from, to\n  CYPHER\n\n  results = @cypher_client.execute_cypher(cypher, from: from, to: to)\n  results&.first\nend\n"

#delete_relationship_on_primary_key(relationship:) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/neo4j/http/relationship_client.rb', line 91

def delete_relationship_on_primary_key(relationship:)
  # protection against mass deletion of relationships
  return if relationship.key_name.nil?

  relationship_selector = build_match_selector(:relationship, relationship)

  cypher = "    MATCH () - [\#{relationship_selector}] - ()\n    WITH relationship\n    DELETE relationship\n    RETURN relationship\n  CYPHER\n\n  results = @cypher_client.execute_cypher(cypher, relationship: relationship)\n  results&.first\nend\n"

#find_relationship(from:, relationship:, to:) ⇒ Object



64
65
66
67
# File 'lib/neo4j/http/relationship_client.rb', line 64

def find_relationship(from:, relationship:, to:)
  results = find_relationships(from: from, to: to, relationship: relationship)
  results&.first
end

#find_relationships(from:, relationship:, to:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/neo4j/http/relationship_client.rb', line 46

def find_relationships(from:, relationship:, to:)
  from_match_clause = build_match_selector(:from, from)
  to_match_clause = build_match_selector(:to, to)
  relationship_clause = build_match_selector(:relationship, relationship)
  cypher = "    MATCH (\#{from_match_clause}) - [\#{relationship_clause}] - (\#{to_match_clause})\n    RETURN from, to, relationship\n  CYPHER\n\n  @cypher_client.execute_cypher(\n    cypher,\n    from: from,\n    to: to,\n    relationship: relationship,\n    access_mode: \"READ\"\n  )\nend\n"

#upsert_relationship(relationship:, from:, to:, create_nodes: false) ⇒ Object



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
# File 'lib/neo4j/http/relationship_client.rb', line 14

def upsert_relationship(relationship:, from:, to:, create_nodes: false)
  match_or_merge = create_nodes ? "MERGE" : "MATCH"
  from_selector = build_match_selector(:from, from)
  to_selector = build_match_selector(:to, to)
  relationship_selector = build_match_selector(:relationship, relationship)

  on_match = ""
  if relationship.attributes.present?
    on_match = "      ON CREATE SET relationship += $relationship_attributes\n      ON MATCH SET relationship += $relationship_attributes\n    CYPHER\n  end\n\n  cypher = +<<-CYPHER\n    \#{match_or_merge} (\#{from_selector})\n    \#{match_or_merge} (\#{to_selector})\n    MERGE (from) - [\#{relationship_selector}] - (to)\n    \#{on_match}\n    RETURN from, to, relationship\n  CYPHER\n\n  results = @cypher_client.execute_cypher(\n    cypher,\n    from: from,\n    to: to,\n    relationship: relationship,\n    relationship_attributes: relationship.attributes\n  )\n  results&.first\nend\n"