module ActiveGraph::Node::Query::QueryProxyEagerLoading

Public Instance Methods

association_tree_class() click to toggle source
   # File lib/active_graph/node/query/query_proxy_eager_loading.rb
45 def association_tree_class
46   AssociationTree
47 end
first() click to toggle source
   # File lib/active_graph/node/query/query_proxy_eager_loading.rb
53 def first
54   (query.clause?(:order) ? self : order(order_property)).limit(1).to_a.first
55 end
perform_query() click to toggle source
   # File lib/active_graph/node/query/query_proxy_eager_loading.rb
15 def perform_query
16   @_cache = IdentityMap.new
17   build_query
18     .map do |record, eager_data|
19     record = cache_and_init(record, with_associations_tree)
20     eager_data.zip(with_associations_tree.paths.map(&:last)).each do |eager_records, element|
21       eager_records.first.zip(eager_records.last).each do |eager_record|
22         add_to_cache(*eager_record, element)
23       end
24     end
25     record
26   end
27 end
pluck_vars(node, rel) click to toggle source
Calls superclass method
   # File lib/active_graph/node/query/query_proxy_eager_loading.rb
11 def pluck_vars(node, rel)
12   with_associations_tree.empty? ? super : perform_query
13 end
propagate_context(query_proxy) click to toggle source
Calls superclass method
   # File lib/active_graph/node/query/query_proxy_eager_loading.rb
36 def propagate_context(query_proxy)
37   super
38   query_proxy.instance_variable_set('@with_associations_tree', @with_associations_tree)
39 end
with_associations(*spec) click to toggle source
   # File lib/active_graph/node/query/query_proxy_eager_loading.rb
29 def with_associations(*spec)
30   new_link.tap do |new_query_proxy|
31     new_query_proxy.with_associations_tree = with_associations_tree.clone
32     new_query_proxy.with_associations_tree.add_spec(spec)
33   end
34 end
with_associations_tree() click to toggle source
   # File lib/active_graph/node/query/query_proxy_eager_loading.rb
41 def with_associations_tree
42   @with_associations_tree ||= association_tree_class.new(model)
43 end
with_associations_tree=(tree) click to toggle source
   # File lib/active_graph/node/query/query_proxy_eager_loading.rb
49 def with_associations_tree=(tree)
50   @with_associations_tree = tree
51 end

Private Instance Methods

add_to_cache(rel, node, element) click to toggle source
   # File lib/active_graph/node/query/query_proxy_eager_loading.rb
59 def add_to_cache(rel, node, element)
60   direction = element.association.direction
61   node = cache_and_init(node, element)
62   if rel.is_a?(ActiveGraph::Relationship)
63     rel.instance_variable_set(direction == :in ? '@from_node' : '@to_node', node)
64   end
65   @_cache[direction == :out ? rel.start_node_id : rel.end_node_id]
66     .association_proxy(element.name).add_to_cache(node, rel)
67 end
as_alias(var) click to toggle source
   # File lib/active_graph/node/query/query_proxy_eager_loading.rb
97 def as_alias(var)
98   "#{var} AS #{var}"
99 end
before_pluck(query) click to toggle source
    # File lib/active_graph/node/query/query_proxy_eager_loading.rb
117 def before_pluck(query)
118   query_from_chain(@order_chain, query, identity)
119 end
build_query() click to toggle source
    # File lib/active_graph/node/query/query_proxy_eager_loading.rb
113 def build_query
114   before_pluck(query_from_association_tree).pluck(identity, "[#{with_associations_return_clause}]")
115 end
cache_and_init(node, element) click to toggle source
   # File lib/active_graph/node/query/query_proxy_eager_loading.rb
74 def cache_and_init(node, element)
75   @_cache.add(node).tap { |n| init_associations(n, element) }
76 end
chain() click to toggle source
    # File lib/active_graph/node/query/query_proxy_eager_loading.rb
170 def chain
171   @order_chain = @chain.select { |link| link.clause == :order } unless with_associations_tree.empty?
172   @chain
173 end
escape(s) click to toggle source
    # File lib/active_graph/node/query/query_proxy_eager_loading.rb
101 def escape(s)
102   "`#{s}`"
103 end
init_associations(node, element) click to toggle source
   # File lib/active_graph/node/query/query_proxy_eager_loading.rb
69 def init_associations(node, element)
70   element.each_key { |key| node.association_proxy(key).init_cache }
71   node.association_proxy(element.name).init_cache if element.rel_length == ''
72 end
optional_match(base_query, path) click to toggle source
    # File lib/active_graph/node/query/query_proxy_eager_loading.rb
150 def optional_match(base_query, path)
151   start_path = "#{escape("#{path_name(path)}_path")}=(#{identity})"
152   base_query.optional_match(
153     "#{start_path}#{path.each_with_index.map do |element, index|
154       relationship_part(element.association, path_name(path[0..index]), element.rel_length)
155     end.join}"
156   )
157 end
optional_match_with_where(base_query, path, _) click to toggle source
    # File lib/active_graph/node/query/query_proxy_eager_loading.rb
142 def optional_match_with_where(base_query, path, _)
143   path
144     .each_with_index.map { |_, index| path[0..index] }
145     .inject(optional_match(base_query, path)) do |query, path_prefix|
146     query.where(path_prefix.last.association.target_where_clause(escape(path_name(path_prefix))))
147   end
148 end
path_name(path) click to toggle source
    # File lib/active_graph/node/query/query_proxy_eager_loading.rb
105 def path_name(path)
106   path.map(&:name).join('.')
107 end
path_names() click to toggle source
    # File lib/active_graph/node/query/query_proxy_eager_loading.rb
109 def path_names
110   with_associations_tree.paths.map { |path| path_name(path) }
111 end
query_from_association_tree() click to toggle source
    # File lib/active_graph/node/query/query_proxy_eager_loading.rb
121 def query_from_association_tree
122   previous_with_vars = []
123   with_associations_tree.paths.inject(query_as(identity).with(ensure_distinct(identity))) do |query, path|
124     with_association_query_part(query, path, previous_with_vars).tap do
125       previous_with_vars << var_fix(path_name(path), :collection)
126     end
127   end
128 end
relationship_collection(path) click to toggle source
    # File lib/active_graph/node/query/query_proxy_eager_loading.rb
138 def relationship_collection(path)
139   path.last.rel_length ? "collect(last(relationships(#{escape("#{path_name(path)}_path")})))" : "collect(#{escape("#{path_name(path)}_rel")})"
140 end
relationship_part(association, path_name, rel_length) click to toggle source
    # File lib/active_graph/node/query/query_proxy_eager_loading.rb
159 def relationship_part(association, path_name, rel_length)
160   if rel_length
161     rel_name = nil
162     length = {max: rel_length}
163   else
164     rel_name = escape("#{path_name}_rel")
165     length = nil
166   end
167   "#{association.arrow_cypher(rel_name, {}, false, false, length)}(#{escape(path_name)})"
168 end
var(*parts) { |escape(compact.join('_'))| ... } click to toggle source
   # File lib/active_graph/node/query/query_proxy_eager_loading.rb
82 def var(*parts)
83   yield(escape(parts.compact.join('_')))
84 end
var_fix(*var) click to toggle source

In neo4j version 2.1.8 this fails due to a bug: MATCH (`n`) WITH `n` RETURN `n` but this MATCH (`n`) WITH n RETURN `n` and this MATCH (`n`) WITH `n` AS `n` RETURN `n` does not

   # File lib/active_graph/node/query/query_proxy_eager_loading.rb
93 def var_fix(*var)
94   var(*var, &method(:as_alias))
95 end
with_association_query_part(base_query, path, previous_with_vars) click to toggle source
    # File lib/active_graph/node/query/query_proxy_eager_loading.rb
130 def with_association_query_part(base_query, path, previous_with_vars)
131   optional_match_with_where(base_query, path, previous_with_vars)
132     .with(identity,
133           "[#{relationship_collection(path)}, collect(#{escape path_name(path)})] "\
134           "AS #{escape("#{path_name(path)}_collection")}",
135           *previous_with_vars)
136 end
with_associations_return_clause() click to toggle source
   # File lib/active_graph/node/query/query_proxy_eager_loading.rb
78 def with_associations_return_clause
79   path_names.map { |n| var(n, :collection, &:itself) }.join(',')
80 end