/
query_proxy.rb
318 lines (258 loc) · 9.78 KB
/
query_proxy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
45
46
47
48
49
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
module Neo4j
module ActiveNode
module Query
class InvalidParameterError < StandardError; end
class QueryProxy
include Enumerable
def initialize(model, association = nil, options = {})
@model = model
@association = association
@context = options.delete(:context)
@options = options
@node_var = options[:node]
@rel_var = options[:rel] || _rel_chain_var
@session = options[:session]
@chain = []
@params = options[:query_proxy] ? options[:query_proxy].instance_variable_get('@params') : {}
end
def each(node = true, rel = nil, &block)
if node && rel
self.pluck((@node_var || :result), @rel_var).each do |obj, rel|
yield obj, rel
end
else
pluck_this = !rel ? (@node_var || :result) : @rel_var
self.pluck(pluck_this).each do |obj|
yield obj
end
end
end
def each_rel(&block)
block_given? ? each(false, true, &block) : to_enum(:each, false, true)
end
def each_with_rel(&block)
block_given? ? each(true, true, &block) : to_enum(:each, true, true)
end
def ==(value)
self.to_a == value
end
METHODS = %w[where order skip limit]
METHODS.each do |method|
module_eval(%Q{
def #{method}(*args)
build_deeper_query_proxy(:#{method}, args)
end}, __FILE__, __LINE__)
end
alias_method :offset, :skip
alias_method :order_by, :order
# For getting variables which have been defined as part of the association chain
def pluck(*args)
self.query.pluck(*args)
end
def params(params)
self.dup.tap do |new_query|
new_query._add_params(params)
end
end
# Like calling #query_as, but for when you don't care about the variable name
def query
query_as(@node_var || :result)
end
# Build a Neo4j::Core::Query object for the QueryProxy
def query_as(var)
var = @node_var if @node_var
query = if @association
chain_var = _association_chain_var
label_string = @model && ":`#{@model.mapped_label_name}`"
(_association_query_start(chain_var) & _query_model_as(var)).match("#{chain_var}#{_association_arrow}(#{var}#{label_string})")
else
_query_model_as(var)
end
# Build a query chain via the chain, return the result
@chain.inject(query.params(@params)) do |query, (method, arg)|
query.send(method, arg.respond_to?(:call) ? arg.call(var) : arg)
end
end
# Cypher string for the QueryProxy's query
def to_cypher
query.to_cypher
end
# To add a relationship for the node for the association on this QueryProxy
def <<(other_node)
create(other_node, {})
self
end
def [](index)
# TODO: Maybe for this and other methods, use array if already loaded, otherwise
# use OFFSET and LIMIT 1?
self.to_a[index]
end
def create(other_nodes, properties)
raise "Can only create associations on associations" unless @association
other_nodes = [other_nodes].flatten
other_nodes.map! do |other_node|
case other_node
when Integer, String
@model.find(other_node)
else
other_node
end
end
raise ArgumentError, "Node must be of the association's class when model is specified" if @model && other_nodes.any? {|other_node| other_node.class != @model }
other_nodes.each do |other_node|
#Neo4j::Transaction.run do
other_node.save if not other_node.persisted?
return false if @association.perform_callback(@options[:start_object], other_node, :before) == false
start_object = @options[:start_object]
_session.query(context: @options[:context])
.start(start: "node(#{start_object.neo_id})", end: "node(#{other_node.neo_id})")
.create("start#{_association_arrow(properties, true)}end").exec
@association.perform_callback(@options[:start_object], other_node, :after)
#end
end
end
#TODO: Get these out of here
def first
self.query_as(:n).limit(1).order('ID(n)').pluck(:n).first
end
def last
self.query_as(:n).limit(1).order('ID(n) DESC').pluck(:n).first
end
# @return [Fixnum] number of nodes of this class
def count(distinct = nil)
raise(InvalidParameterError, ':count accepts `distinct` or nil as a parameter') unless distinct.nil? || distinct == :distinct
q = distinct.nil? ? "n" : "DISTINCT n"
self.query_as(:n).return("count(#{q}) AS count").first.count
end
alias_method :size, :count
alias_method :length, :count
def empty?
!self.exists?
end
alias_method :blank?, :empty?
def include?(other)
raise(InvalidParameterError, ':include? only accepts nodes') unless other.respond_to?(:neo_id)
self.query_as(:n).where("ID(n) = #{other.neo_id}").return("count(n) AS count").first.count > 0
end
def exists?(node_id=nil)
raise(InvalidParameterError, ':exists? only accepts neo_ids') unless node_id.is_a?(Integer) || node_id.nil?
start_q = self.query_as(:n)
end_q = node_id.nil? ? start_q : start_q.where("ID(n) = #{node_id}")
end_q.return("COUNT(n) AS count").first.count > 0
end
# QueryProxy objects act as a representation of a model at the class level so we pass through calls
# This allows us to define class functions for reusable query chaining or for end-of-query aggregation/summarizing
def method_missing(method_name, *args)
if @model && @model.respond_to?(method_name)
call_class_method(method_name, *args)
else
super
end
end
attr_reader :context
protected
# Methods are underscored to prevent conflict with user class methods
attr_reader :node_var
def _add_params(params)
@params = @params.merge(params)
end
def _add_links(links)
@chain += links
end
def _query_model_as(var)
match_arg = if @model
label = @model.respond_to?(:mapped_label_name) ? @model.mapped_label_name : @model
{var => label}
else
var
end
_session.query(context: @context).match(match_arg)
end
def _session
@session || (@model && @model.neo4j_session)
end
def _association_arrow(properties = {}, create = false)
@association && @association.arrow_cypher(@rel_var, properties, create)
end
def _chain_level
if @options[:start_object]
1
elsif query_proxy = @options[:query_proxy]
query_proxy._chain_level + 1
else
1
end
end
def _association_chain_var
if start_object = @options[:start_object]
:"#{start_object.class.name.gsub('::', '_').downcase}#{start_object.neo_id}"
elsif query_proxy = @options[:query_proxy]
query_proxy.node_var || :"node#{_chain_level}"
else
raise "Crazy error" # TODO: Better error
end
end
def _association_query_start(var)
if start_object = @options[:start_object]
start_object.query_as(var)
elsif query_proxy = @options[:query_proxy]
query_proxy.query_as(var)
else
raise "Crazy error" # TODO: Better error
end
end
def _rel_chain_var
:"rel#{_chain_level - 1}"
end
attr_writer :context
private
def call_class_method(method_name, *args)
@model.query_proxy = self
result = @model.send(method_name, *args)
@model.query_proxy = nil
result
end
def build_deeper_query_proxy(method, args)
self.dup.tap do |new_query|
args.each do |arg|
new_query._add_links(links_for_arg(method, arg))
end
end
end
def links_for_arg(method, arg)
method_to_call = "links_for_#{method}_arg"
default = [[method, arg]]
self.send(method_to_call, arg) || default
rescue NoMethodError
default
end
def links_for_where_arg(arg)
node_num = 1
result = []
if arg.is_a?(Hash)
arg.map do |key, value|
if @model && @model.has_association?(key)
neo_id = value.try(:neo_id) || value
raise ArgumentError, "Invalid value for '#{key}' condition" if not neo_id.is_a?(Integer)
n_string = "n#{node_num}"
dir = @model.associations[key].direction
arrow = dir == :out ? '-->' : '<--'
result << [:match, ->(v) { "#{v}#{arrow}(#{n_string})" }]
result << [:where, ->(v) { {"ID(#{n_string})" => neo_id.to_i} }]
node_num += 1
else
result << [:where, ->(v) { {v => {key => value}}}]
end
end
elsif arg.is_a?(String)
result << [:where, arg]
end
result
end
def links_for_order_arg(arg)
[[:order, ->(v) { arg.is_a?(String) ? arg : {v => arg} }]]
end
end
end
end
end