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 = <<-CYPHER
    MATCH (#{from_selector}) - [#{relationship_selector}] - (#{to_selector})
    WITH from, to, relationship
    DELETE relationship
    RETURN from, to
  CYPHER

  results = @cypher_client.execute_cypher(cypher, from: from, to: to)
  results&.first
end

#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 = <<-CYPHER
    MATCH () - [#{relationship_selector}] - ()
    WITH relationship
    DELETE relationship
    RETURN relationship
  CYPHER

  results = @cypher_client.execute_cypher(cypher, relationship: relationship)
  results&.first
end

#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 = <<-CYPHER
    MATCH (#{from_match_clause}) - [#{relationship_clause}] - (#{to_match_clause})
    RETURN from, to, relationship
  CYPHER

  @cypher_client.execute_cypher(
    cypher,
    from: from,
    to: to,
    relationship: relationship,
    access_mode: "READ"
  )
end

#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 = <<-CYPHER
      ON CREATE SET relationship += $relationship_attributes
      ON MATCH SET relationship += $relationship_attributes
    CYPHER
  end

  cypher = +<<-CYPHER
    #{match_or_merge} (#{from_selector})
    #{match_or_merge} (#{to_selector})
    MERGE (from) - [#{relationship_selector}] - (to)
    #{on_match}
    RETURN from, to, relationship
  CYPHER

  results = @cypher_client.execute_cypher(
    cypher,
    from: from,
    to: to,
    relationship: relationship,
    relationship_attributes: relationship.attributes
  )
  results&.first
end