Module: ApacheAge::Entities::Edge

Extended by:
ActiveSupport::Concern
Included in:
ApacheAge::Edge
Defined in:
lib/apache_age/entities/edge.rb

Instance Method Summary collapse

Instance Method Details

#age_typeObject



36
# File 'lib/apache_age/entities/edge.rb', line 36

def age_type = 'edge'

#create_sqlObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/apache_age/entities/edge.rb', line 50

def create_sql
  self.start_node = start_node.save unless start_node.persisted?
  self.end_node = end_node.save unless end_node.persisted?

  start_node_age_label = ActiveRecord::Base.sanitize_sql(start_node.age_label)
  end_node_age_label = ActiveRecord::Base.sanitize_sql(end_node.age_label)
  sanitized_start_id = ActiveRecord::Base.sanitize_sql(["?", start_node.id])
  sanitized_end_id = ActiveRecord::Base.sanitize_sql(["?", end_node.id])
  # cant use sanitize_sql_like because it escapes the % and _ characters
  # label_name = ActiveRecord::Base.sanitize_sql_like(age_label)

  reject_keys = %i[id start_id end_id start_node end_node]
  sanitized_properties =
    self.to_h.reject { |k, _v| reject_keys.include?(k) }.reject { |_k, v| v.nil? }
      .map { |k, v| "#{k}: #{ActiveRecord::Base.sanitize_sql(["?", v])}" }
      .join(', ')
  <<-SQL
    SELECT *
    FROM cypher('#{age_graph}', $$
        MATCH (from_node:#{start_node_age_label}), (to_node:#{end_node_age_label})
        WHERE id(from_node) = #{sanitized_start_id} AND id(to_node) = #{sanitized_end_id}
        CREATE (from_node)-[edge:#{age_label} {#{sanitized_properties}}]->(to_node)
        RETURN edge
    $$) as (edge agtype);
  SQL
end

#end_classObject



37
# File 'lib/apache_age/entities/edge.rb', line 37

def end_class = end_node.class

#end_node_classObject



39
# File 'lib/apache_age/entities/edge.rb', line 39

def end_node_class = end_node.class

#initialize(**attributes) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/apache_age/entities/edge.rb', line 28

def initialize(**attributes)
  super
  self.end_id ||= end_node.id if end_node
  self.start_id ||= start_node.id if start_node
  self.end_node ||= Entity.find(end_id) if end_id
  self.start_node ||= Entity.find(start_id) if start_id
end

#start_classObject



38
# File 'lib/apache_age/entities/edge.rb', line 38

def start_class = start_node.class

#start_node_classObject



40
# File 'lib/apache_age/entities/edge.rb', line 40

def start_node_class = start_node.class

#update_sqlObject

So far just properties of string type with ” around them



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/apache_age/entities/edge.rb', line 78

def update_sql
  alias_name = age_alias || age_label.downcase
  set_clause =
    age_properties.map do |k, v|
      if v
        sanitized_value = ActiveRecord::Base.sanitize_sql(["?", v])
        "#{alias_name}.#{k} = #{sanitized_value}"
      else
        "#{alias_name}.#{k} = NULL"
      end
    end.join(', ')

  sanitized_id = ActiveRecord::Base.sanitize_sql(["?", id])

  <<-SQL
    SELECT *
    FROM cypher('#{age_graph}', $$
        MATCH ()-[#{alias_name}:#{age_label}]->()
        WHERE id(#{alias_name}) = #{sanitized_id}
        SET #{set_clause}
        RETURN #{alias_name}
    $$) as (#{age_label} agtype);
  SQL
end

#validate_nodesObject

Custom validation method to validate start_node and end_node



45
46
47
48
# File 'lib/apache_age/entities/edge.rb', line 45

def validate_nodes
  errors.add(:start_node, 'invalid') if start_node && !start_node.valid?
  errors.add(:end_node, 'invalid') if end_node && !end_node.valid?
end