Class: NoSE::Connection

Inherits:
Statement show all
Includes:
StatementSupportQuery
Defined in:
lib/nose/statements/connection.rb

Overview

Superclass for connect and disconnect statements

Direct Known Subclasses

Connect, Disconnect

Instance Attribute Summary collapse

Attributes inherited from Statement

#comment, #entity, #eq_fields, #graph, #group, #key_path, #label, #range_field, #text

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Statement

#materialize_view, #read_only?, #requires_delete?, #requires_insert?, #to_color

Constructor Details

#initialize(params, text, group: nil, label: nil) ⇒ Connection

Returns a new instance of Connection.



11
12
13
14
15
16
17
# File 'lib/nose/statements/connection.rb', line 11

def initialize(params, text, group: nil, label: nil)
  super params, text, group: group, label: label
  fail InvalidStatementException, 'Incorrect connection initialization' \
    unless text.split.first == self.class.name.split('::').last.upcase

  populate_conditions params
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



8
9
10
# File 'lib/nose/statements/connection.rb', line 8

def conditions
  @conditions
end

#source_pkObject (readonly)

Returns the value of attribute source_pk.



8
9
10
# File 'lib/nose/statements/connection.rb', line 8

def source_pk
  @source_pk
end

#targetObject (readonly)

Returns the value of attribute target.



8
9
10
# File 'lib/nose/statements/connection.rb', line 8

def target
  @target
end

#target_pkObject (readonly)

Returns the value of attribute target_pk.



8
9
10
# File 'lib/nose/statements/connection.rb', line 8

def target_pk
  @target_pk
end

Class Method Details

.keys_from_tree(tree, params) ⇒ Object



28
29
30
31
32
# File 'lib/nose/statements/connection.rb', line 28

def self.keys_from_tree(tree, params)
  params[:source_pk] = tree[:source_pk]
  params[:target] = params[:entity].foreign_keys[tree[:target].to_s]
  params[:target_pk] = tree[:target_pk]
end

.parse(tree, params, text, group: nil, label: nil) ⇒ Connection

Build a new disconnect from a provided parse tree

Returns:



21
22
23
24
25
# File 'lib/nose/statements/connection.rb', line 21

def self.parse(tree, params, text, group: nil, label: nil)
  keys_from_tree tree, params

  new params, text, group: group, label: label
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



41
42
43
44
45
46
47
# File 'lib/nose/statements/connection.rb', line 41

def ==(other)
  self.class == other.class &&
    @graph == other.graph &&
    @source == other.source &&
    @target == other.target &&
    @conditions == other.conditions
end

#hashObject



50
51
52
# File 'lib/nose/statements/connection.rb', line 50

def hash
  @hash ||= [@graph, @source, @target, @conditions].hash
end

#modifies_index?(index) ⇒ Boolean

A connection modifies an index if the relationship is in the path

Returns:

  • (Boolean)


55
56
57
# File 'lib/nose/statements/connection.rb', line 55

def modifies_index?(index)
  index.path.include?(@target) || index.path.include?(@target.reverse)
end

#support_queries(index) ⇒ Object

Get the support queries for updating an index



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/nose/statements/connection.rb', line 60

def support_queries(index)
  return [] unless modifies_index?(index)

  select = index.all_fields - @conditions.each_value.map(&:field).to_set
  return [] if select.empty?

  index.graph.split(entity).map do |graph|
    support_fields = select.select do |field|
      graph.entities.include? field.parent
    end.to_set
    conditions = @conditions.select do |_, c|
      graph.entities.include? c.field.parent
    end

    split_entity = split_entity graph, index.graph, entity
    build_support_query split_entity, index, graph, support_fields,
                        conditions
  end.compact
end

#unparseString

Produce the SQL text corresponding to this connection

Returns:

  • (String)


36
37
38
39
# File 'lib/nose/statements/connection.rb', line 36

def unparse
  "CONNECT #{source.name}(\"#{source_pk}\") TO " \
    "#{target.name}(\"#{target_pk}\")"
end