-
Notifications
You must be signed in to change notification settings - Fork 539
Expand file tree
/
Copy pathjoin_manager.rb
More file actions
302 lines (255 loc) · 11.8 KB
/
join_manager.rb
File metadata and controls
302 lines (255 loc) · 11.8 KB
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
module JSONAPI
module ActiveRelation
# Stores relationship paths starting from the resource_klass, consolidating duplicate paths from
# relationships, filters and sorts. When joins are made the table aliases are tracked in join_details
class JoinManager
attr_reader :resource_klass,
:source_relationship,
:resource_join_tree,
:join_details
def initialize(resource_klass:,
source_relationship: nil,
relationships: nil,
filters: nil,
sort_criteria: nil)
@resource_klass = resource_klass
@join_details = nil
@collected_aliases = Set.new
@resource_join_tree = {
root: {
join_type: :root,
resource_klasses: {
resource_klass => {
relationships: {}
}
}
}
}
add_source_relationship(source_relationship)
add_sort_criteria(sort_criteria)
add_filters(filters)
add_relationships(relationships)
end
def join(records, options)
fail "can't be joined again" if @join_details
@join_details = {}
perform_joins(records, options)
end
# source details will only be on a relationship if the source_relationship is set
# this method gets the join details whether they are on a relationship or are just pseudo details for the base
# resource. Specify the resource type for polymorphic relationships
#
def source_join_details(type=nil)
if source_relationship
related_resource_klass = type ? resource_klass.resource_klass_for(type) : source_relationship.resource_klass
segment = PathSegment::Relationship.new(relationship: source_relationship, resource_klass: related_resource_klass)
details = @join_details[segment]
else
if type
details = @join_details["##{type}"]
else
details = @join_details['']
end
end
details
end
def join_details_by_polymorphic_relationship(relationship, type)
segment = PathSegment::Relationship.new(relationship: relationship, resource_klass: resource_klass.resource_klass_for(type))
@join_details[segment]
end
def join_details_by_relationship(relationship)
segment = PathSegment::Relationship.new(relationship: relationship, resource_klass: relationship.resource_klass)
@join_details[segment]
end
def self.get_join_arel_node(records, options = {})
init_join_sources = records.arel.join_sources
init_join_sources_length = init_join_sources.length
records = yield(records, options)
join_sources = records.arel.join_sources
if join_sources.length > init_join_sources_length
last_join = (join_sources - init_join_sources).last
else
# :nocov:
warn "get_join_arel_node: No join added"
last_join = nil
# :nocov:
end
return records, last_join
end
def self.alias_from_arel_node(node)
case node.left
when Arel::Table
node.left.name
when Arel::Nodes::TableAlias
node.left.right
when Arel::Nodes::StringJoin
# :nocov:
warn "alias_from_arel_node: Unsupported join type - use custom filtering and sorting"
nil
# :nocov:
end
end
private
def flatten_join_tree_by_depth(join_array = [], node = @resource_join_tree, level = 0)
join_array[level] = [] unless join_array[level]
node.each do |relationship, relationship_details|
relationship_details[:resource_klasses].each do |related_resource_klass, resource_details|
join_array[level] << { relationship: relationship,
relationship_details: relationship_details,
related_resource_klass: related_resource_klass}
flatten_join_tree_by_depth(join_array, resource_details[:relationships], level+1)
end
end
join_array
end
def add_join_details(join_key, details, check_for_duplicate_alias = true)
fail "details already set" if @join_details.has_key?(join_key)
@join_details[join_key] = details
# Joins are being tracked as they are added to the built up relation. If the same table is added to a
# relation more than once subsequent versions will be assigned an alias. Depending on the order the joins
# are made the computed aliases may change. The order this library performs the joins was chosen
# to prevent this. However if the relation is reordered it should result in reusing on of the earlier
# aliases (in this case a plain table name). The following check will catch this an raise an exception.
# An exception is appropriate because not using the correct alias could leak data due to filters and
# applied permissions being performed on the wrong data.
if check_for_duplicate_alias && @collected_aliases.include?(details[:alias])
fail "alias '#{details[:alias]}' has already been added. Possible relation reordering"
end
@collected_aliases << details[:alias]
end
def perform_joins(records, options)
join_array = flatten_join_tree_by_depth
join_array.each do |level_joins|
level_joins.each do |join_details|
relationship = join_details[:relationship]
relationship_details = join_details[:relationship_details]
related_resource_klass = join_details[:related_resource_klass]
join_type = relationship_details[:join_type]
if relationship == :root
unless source_relationship
add_join_details('', {alias: resource_klass._table_name, join_type: :root})
end
next
end
records, join_node = self.class.get_join_arel_node(records, options) {|records, options|
related_resource_klass.join_relationship(
records: records,
resource_type: related_resource_klass._type,
join_type: join_type,
relationship: relationship,
options: options)
}
unless join_node
warn "join node for #{related_resource_klass._type} not found"
next
end
details = {alias: self.class.alias_from_arel_node(join_node), join_type: join_type}
if relationship == source_relationship
if relationship.polymorphic? && relationship.belongs_to?
add_join_details("##{related_resource_klass._type}", details)
else
add_join_details('', details)
end
end
# We're adding the source alias with two keys. We only want the check for duplicate aliases once.
# See the note in `add_join_details`.
check_for_duplicate_alias = !(relationship == source_relationship)
add_join_details(PathSegment::Relationship.new(relationship: relationship, resource_klass: related_resource_klass), details, check_for_duplicate_alias)
end
end
records
end
def add_join(path, default_type = :inner, default_polymorphic_join_type = :left)
if source_relationship
if source_relationship.polymorphic?
# Polymorphic paths will come it with the resource_type as the first segment (for example `#documents.comments`)
# We just need to prepend the relationship portion the
sourced_path = "#{source_relationship.name}#{path}"
else
sourced_path = "#{source_relationship.name}.#{path}"
end
else
sourced_path = path
end
join_manager, _field = parse_path_to_tree(sourced_path, resource_klass, default_type, default_polymorphic_join_type)
@resource_join_tree[:root].deep_merge!(join_manager) { |key, val, other_val|
if key == :join_type
if val == other_val
val
else
:inner
end
end
}
end
def process_path_to_tree(path_segments, resource_klass, default_join_type, default_polymorphic_join_type)
node = {
resource_klasses: {
resource_klass => {
relationships: {}
}
}
}
segment = path_segments.shift
if segment.is_a?(PathSegment::Relationship)
node[:resource_klasses][resource_klass][:relationships][segment.relationship] ||= {}
# join polymorphic as left joins
node[:resource_klasses][resource_klass][:relationships][segment.relationship][:join_type] ||=
segment.relationship.polymorphic? ? default_polymorphic_join_type : default_join_type
segment.relationship.resource_types.each do |related_resource_type|
related_resource_klass = resource_klass.resource_klass_for(related_resource_type)
# If the resource type was specified in the path segment we want to only process the next segments for
# that resource type, otherwise process for all
process_all_types = !segment.path_specified_resource_klass?
if process_all_types || related_resource_klass == segment.resource_klass
# Prefer using `segment.resource_klass` insead of `related_resource_klass` to avoid inheritance issue
related_resource_tree = process_path_to_tree(path_segments.dup, segment.resource_klass, default_join_type, default_polymorphic_join_type)
node[:resource_klasses][resource_klass][:relationships][segment.relationship].deep_merge!(related_resource_tree)
end
end
end
node
end
def parse_path_to_tree(path_string, resource_klass, default_join_type = :inner, default_polymorphic_join_type = :left)
path = JSONAPI::Path.new(resource_klass: resource_klass, path_string: path_string)
field = path.segments[-1]
return process_path_to_tree(path.segments, resource_klass, default_join_type, default_polymorphic_join_type), field
end
def add_source_relationship(source_relationship)
@source_relationship = source_relationship
if @source_relationship
resource_klasses = {}
source_relationship.resource_types.each do |related_resource_type|
related_resource_klass = resource_klass.resource_klass_for(related_resource_type)
resource_klasses[related_resource_klass] = {relationships: {}}
end
join_type = source_relationship.polymorphic? ? :left : :inner
@resource_join_tree[:root][:resource_klasses][resource_klass][:relationships][@source_relationship] = {
source: true, resource_klasses: resource_klasses, join_type: join_type
}
end
end
def add_filters(filters)
return if filters.blank?
filters.each_key do |filter|
# Do not add joins for filters with an apply callable. This can be overridden by setting perform_joins to true
next if resource_klass._allowed_filters[filter].try(:[], :apply) &&
!resource_klass._allowed_filters[filter].try(:[], :perform_joins)
add_join(filter, :left)
end
end
def add_sort_criteria(sort_criteria)
return if sort_criteria.blank?
sort_criteria.each do |sort|
add_join(sort[:field], :left)
end
end
def add_relationships(relationships)
return if relationships.blank?
relationships.each do |relationship|
add_join(relationship, :left)
end
end
end
end
end