Commit | Line | Data |
4e9fc3f3 |
1 | package # hide from the pauses |
2 | DBIx::Class::ResultSource::RowParser; |
76031e14 |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
9f98c4b2 |
7 | use base 'DBIx::Class'; |
8 | |
76031e14 |
9 | use Try::Tiny; |
fcf32d04 |
10 | use List::Util qw(first max); |
76031e14 |
11 | use B 'perlstring'; |
12 | |
9f98c4b2 |
13 | use DBIx::Class::ResultSource::RowParser::Util qw( |
14 | assemble_simple_parser |
15 | assemble_collapsing_parser |
16 | ); |
76031e14 |
17 | |
9f98c4b2 |
18 | use namespace::clean; |
76031e14 |
19 | |
20 | # Accepts one or more relationships for the current source and returns an |
21 | # array of column names for each of those relationships. Column names are |
22 | # prefixed relative to the current source, in accordance with where they appear |
23 | # in the supplied relationships. |
24 | sub _resolve_prefetch { |
25 | my ($self, $pre, $alias, $alias_map, $order, $pref_path) = @_; |
26 | $pref_path ||= []; |
27 | |
28 | if (not defined $pre or not length $pre) { |
29 | return (); |
30 | } |
31 | elsif( ref $pre eq 'ARRAY' ) { |
32 | return |
33 | map { $self->_resolve_prefetch( $_, $alias, $alias_map, $order, [ @$pref_path ] ) } |
34 | @$pre; |
35 | } |
36 | elsif( ref $pre eq 'HASH' ) { |
37 | my @ret = |
38 | map { |
39 | $self->_resolve_prefetch($_, $alias, $alias_map, $order, [ @$pref_path ] ), |
40 | $self->related_source($_)->_resolve_prefetch( |
4e9fc3f3 |
41 | $pre->{$_}, "${alias}.$_", $alias_map, $order, [ @$pref_path, $_] ) |
76031e14 |
42 | } keys %$pre; |
43 | return @ret; |
44 | } |
45 | elsif( ref $pre ) { |
46 | $self->throw_exception( |
47 | "don't know how to resolve prefetch reftype ".ref($pre)); |
48 | } |
49 | else { |
50 | my $p = $alias_map; |
51 | $p = $p->{$_} for (@$pref_path, $pre); |
52 | |
53 | $self->throw_exception ( |
54 | "Unable to resolve prefetch '$pre' - join alias map does not contain an entry for path: " |
55 | . join (' -> ', @$pref_path, $pre) |
56 | ) if (ref $p->{-join_aliases} ne 'ARRAY' or not @{$p->{-join_aliases}} ); |
57 | |
58 | my $as = shift @{$p->{-join_aliases}}; |
59 | |
60 | my $rel_info = $self->relationship_info( $pre ); |
61 | $self->throw_exception( $self->source_name . " has no such relationship '$pre'" ) |
62 | unless $rel_info; |
63 | |
64 | my $as_prefix = ($alias =~ /^.*?\.(.+)$/ ? $1.'.' : ''); |
76031e14 |
65 | |
66 | return map { [ "${as}.$_", "${as_prefix}${pre}.$_", ] } |
4e9fc3f3 |
67 | $self->related_source($pre)->columns; |
76031e14 |
68 | } |
69 | } |
70 | |
9f98c4b2 |
71 | # Takes an arrayref of {as} dbic column aliases and the collapse and select |
72 | # attributes from the same $rs (the selector requirement is a temporary |
73 | # workaround... I hope), and returns a coderef capable of: |
74 | # my $me_pref_clps = $coderef->([$rs->cursor->next/all]) |
75 | # Where the $me_pref_clps arrayref is the future argument to inflate_result() |
76 | # |
77 | # For an example of this coderef in action (and to see its guts) look at |
78 | # t/resultset/rowparser_internals.t |
79 | # |
80 | # This is a huge performance win, as we call the same code for every row |
81 | # returned from the db, thus avoiding repeated method lookups when traversing |
82 | # relationships |
83 | # |
84 | # Also since the coderef is completely stateless (the returned structure is |
85 | # always fresh on every new invocation) this is a very good opportunity for |
86 | # memoization if further speed improvements are needed |
87 | # |
88 | # The way we construct this coderef is somewhat fugly, although the result is |
89 | # really worth it. The final coderef does not perform any kind of recursion - |
90 | # the entire nested structure constructor is rolled out into a single scope. |
91 | # |
92 | # In any case - the output of this thing is meticulously micro-tested, so |
93 | # any sort of adjustment/rewrite should be relatively easy (fsvo relatively) |
94 | # |
95 | sub _mk_row_parser { |
96 | my ($self, $args) = @_; |
97 | |
79adc44f |
98 | die "HRI without pruning makes zero sense" |
99 | if ( $args->{hri_style} && ! $args->{prune_null_branches} ); |
100 | |
101 | my %common = ( |
102 | hri_style => $args->{hri_style}, |
103 | prune_null_branches => $args->{prune_null_branches}, |
104 | val_index => { map |
105 | { $args->{inflate_map}[$_] => $_ } |
106 | ( 0 .. $#{$args->{inflate_map}} ) |
107 | }, |
108 | ); |
109 | |
110 | my $src = $args->{collapse} |
111 | ? assemble_collapsing_parser({ |
112 | %common, |
113 | collapse_map => $self->_resolve_collapse ({ |
114 | # FIXME |
115 | # only consider real columns (not functions) during collapse resolution |
116 | # this check shouldn't really be here, as fucktards are not supposed to |
117 | # alias random crap to existing column names anyway, but still - just in |
118 | # case |
119 | # FIXME !!!! - this does not yet deal with unbalanced selectors correctly |
120 | # (it is now trivial as the attrs specify where things go out of sync |
121 | # needs MOAR tests) |
122 | as => { map |
123 | { ref $args->{selection}[$common{val_index}{$_}] ? () : ( $_ => $common{val_index}{$_} ) } |
124 | keys %{$common{val_index}} |
125 | }, |
126 | premultiplied => $args->{premultiplied}, |
127 | }) |
128 | }) |
129 | : assemble_simple_parser( |
130 | \%common |
131 | ) |
132 | ; |
ce556881 |
133 | |
52864fbd |
134 | return $args->{eval} |
bdbd2ae8 |
135 | ? ( eval "sub $src" || die $@ ) |
52864fbd |
136 | : $src |
ce556881 |
137 | ; |
9f98c4b2 |
138 | } |
139 | |
140 | |
2d0b795a |
141 | # Takes an arrayref selection list and generates a collapse-map representing |
76031e14 |
142 | # row-object fold-points. Every relationship is assigned a set of unique, |
143 | # non-nullable columns (which may *not even be* from the same resultset) |
144 | # and the collapser will use this information to correctly distinguish |
2d0b795a |
145 | # data of individual to-be-row-objects. See t/resultset/rowparser_internals.t |
146 | # for extensive RV examples |
76031e14 |
147 | sub _resolve_collapse { |
82f0e0aa |
148 | my ($self, $args, $common_args) = @_; |
76031e14 |
149 | |
150 | # for comprehensible error messages put ourselves at the head of the relationship chain |
82f0e0aa |
151 | $args->{_rel_chain} ||= [ $self->source_name ]; |
76031e14 |
152 | |
9f98c4b2 |
153 | # record top-level fully-qualified column index, signify toplevelness |
154 | unless ($common_args->{_as_fq_idx}) { |
155 | $common_args->{_as_fq_idx} = { %{$args->{as}} }; |
156 | $args->{_is_top_level} = 1; |
82f0e0aa |
157 | }; |
76031e14 |
158 | |
159 | my ($my_cols, $rel_cols); |
82f0e0aa |
160 | for (keys %{$args->{as}}) { |
76031e14 |
161 | if ($_ =~ /^ ([^\.]+) \. (.+) /x) { |
162 | $rel_cols->{$1}{$2} = 1; |
163 | } |
164 | else { |
fcf32d04 |
165 | $my_cols->{$_} = {}; # important for ||='s below |
76031e14 |
166 | } |
167 | } |
168 | |
169 | my $relinfo; |
fcf32d04 |
170 | # run through relationships, collect metadata |
76031e14 |
171 | for my $rel (keys %$rel_cols) { |
76031e14 |
172 | my $inf = $self->relationship_info ($rel); |
173 | |
95e41036 |
174 | $relinfo->{$rel} = { |
175 | is_single => ( $inf->{attrs}{accessor} && $inf->{attrs}{accessor} ne 'multi' ), |
176 | is_inner => ( ( $inf->{attrs}{join_type} || '' ) !~ /^left/i), |
177 | rsrc => $self->related_source($rel), |
178 | }; |
76031e14 |
179 | |
fcf32d04 |
180 | # FIME - need to use _resolve_cond here instead |
76031e14 |
181 | my $cond = $inf->{cond}; |
182 | |
183 | if ( |
184 | ref $cond eq 'HASH' |
185 | and |
186 | keys %$cond |
187 | and |
fcf32d04 |
188 | ! defined first { $_ !~ /^foreign\./ } (keys %$cond) |
76031e14 |
189 | and |
fcf32d04 |
190 | ! defined first { $_ !~ /^self\./ } (values %$cond) |
76031e14 |
191 | ) { |
192 | for my $f (keys %$cond) { |
193 | my $s = $cond->{$f}; |
194 | $_ =~ s/^ (?: foreign | self ) \.//x for ($f, $s); |
195 | $relinfo->{$rel}{fk_map}{$s} = $f; |
76031e14 |
196 | } |
197 | } |
198 | } |
199 | |
fcf32d04 |
200 | # inject non-left fk-bridges from *INNER-JOINED* children (if any) |
201 | for my $rel (grep { $relinfo->{$_}{is_inner} } keys %$relinfo) { |
202 | my $ri = $relinfo->{$rel}; |
203 | for (keys %{$ri->{fk_map}} ) { |
204 | # need to know source from *our* pov, hence $rel.col |
205 | $my_cols->{$_} ||= { via_fk => "$rel.$ri->{fk_map}{$_}" } |
206 | if defined $rel_cols->{$rel}{$ri->{fk_map}{$_}} # in fact selected |
207 | } |
208 | } |
209 | |
a0726a33 |
210 | # if the parent is already defined *AND* we have an inner reverse relationship |
211 | # (i.e. do not exist without it) , assume all of its related FKs are selected |
76031e14 |
212 | # (even if they in fact are NOT in the select list). Keep a record of what we |
213 | # assumed, and if any such phantom-column becomes part of our own collapser, |
214 | # throw everything assumed-from-parent away and replace with the collapser of |
215 | # the parent (whatever it may be) |
216 | my $assumed_from_parent; |
a0726a33 |
217 | if ( ! $args->{_parent_info}{underdefined} and ! $args->{_parent_info}{rev_rel_is_optional} ) { |
fcf32d04 |
218 | for my $col ( values %{$args->{_parent_info}{rel_condition} || {}} ) { |
219 | next if exists $my_cols->{$col}; |
220 | $my_cols->{$col} = { via_collapse => $args->{_parent_info}{collapse_on_idcols} }; |
221 | $assumed_from_parent->{columns}{$col}++; |
222 | } |
76031e14 |
223 | } |
224 | |
225 | # get colinfo for everything |
226 | if ($my_cols) { |
227 | my $ci = $self->columns_info; |
228 | $my_cols->{$_}{colinfo} = $ci->{$_} for keys %$my_cols; |
229 | } |
230 | |
231 | my $collapse_map; |
232 | |
3faac878 |
233 | # first try to reuse the parent's collapser (i.e. reuse collapser over 1:1) |
234 | # (makes for a leaner coderef later) |
9f98c4b2 |
235 | unless ($collapse_map->{-identifying_columns}) { |
236 | $collapse_map->{-identifying_columns} = $args->{_parent_info}{collapse_on_idcols} |
3faac878 |
237 | if $args->{_parent_info}{collapser_reusable}; |
238 | } |
239 | |
3faac878 |
240 | # Still dont know how to collapse - try to resolve based on our columns (plus already inserted FK bridges) |
76031e14 |
241 | if ( |
9f98c4b2 |
242 | ! $collapse_map->{-identifying_columns} |
3faac878 |
243 | and |
76031e14 |
244 | $my_cols |
245 | and |
4e9fc3f3 |
246 | my $idset = $self->_identifying_column_set ({map { $_ => $my_cols->{$_}{colinfo} } keys %$my_cols}) |
76031e14 |
247 | ) { |
248 | # see if the resulting collapser relies on any implied columns, |
249 | # and fix stuff up if this is the case |
4e9fc3f3 |
250 | my @reduced_set = grep { ! $assumed_from_parent->{columns}{$_} } @$idset; |
76031e14 |
251 | |
9f98c4b2 |
252 | $collapse_map->{-identifying_columns} = [ __unique_numlist( |
3faac878 |
253 | @{ $args->{_parent_info}{collapse_on_idcols}||[] }, |
254 | |
76031e14 |
255 | (map |
256 | { |
257 | my $fqc = join ('.', |
82f0e0aa |
258 | @{$args->{_rel_chain}}[1 .. $#{$args->{_rel_chain}}], |
76031e14 |
259 | ( $my_cols->{$_}{via_fk} || $_ ), |
260 | ); |
261 | |
82f0e0aa |
262 | $common_args->{_as_fq_idx}->{$fqc}; |
76031e14 |
263 | } |
4e9fc3f3 |
264 | @reduced_set |
76031e14 |
265 | ), |
3faac878 |
266 | )]; |
76031e14 |
267 | } |
268 | |
269 | # Stil don't know how to collapse - keep descending down 1:1 chains - if |
270 | # a related non-LEFT 1:1 is resolvable - its condition will collapse us |
271 | # too |
9f98c4b2 |
272 | unless ($collapse_map->{-identifying_columns}) { |
76031e14 |
273 | my @candidates; |
274 | |
275 | for my $rel (keys %$relinfo) { |
276 | next unless ($relinfo->{$rel}{is_single} && $relinfo->{$rel}{is_inner}); |
277 | |
82f0e0aa |
278 | if ( my $rel_collapse = $relinfo->{$rel}{rsrc}->_resolve_collapse ({ |
279 | as => $rel_cols->{$rel}, |
280 | _rel_chain => [ @{$args->{_rel_chain}}, $rel ], |
281 | _parent_info => { underdefined => 1 }, |
282 | }, $common_args)) { |
9f98c4b2 |
283 | push @candidates, $rel_collapse->{-identifying_columns}; |
76031e14 |
284 | } |
285 | } |
286 | |
287 | # get the set with least amount of columns |
288 | # FIXME - maybe need to implement a data type order as well (i.e. prefer several ints |
289 | # to a single varchar) |
290 | if (@candidates) { |
9f98c4b2 |
291 | ($collapse_map->{-identifying_columns}) = sort { scalar @$a <=> scalar @$b } (@candidates); |
76031e14 |
292 | } |
293 | } |
294 | |
fcf32d04 |
295 | # Stil don't know how to collapse, and we are the root node. Last ditch |
296 | # effort in case we are *NOT* premultiplied. |
297 | # Run through *each multi* all the way down, left or not, and all |
298 | # *left* singles (a single may become a multi underneath) . When everything |
299 | # gets back see if all the rels link to us definitively. If this is the |
300 | # case we are good - either one of them will define us, or if all are NULLs |
301 | # we know we are "unique" due to the "non-premultiplied" check |
302 | if ( |
9f98c4b2 |
303 | ! $collapse_map->{-identifying_columns} |
fcf32d04 |
304 | and |
305 | ! $args->{premultiplied} |
306 | and |
9f98c4b2 |
307 | $args->{_is_top_level} |
fcf32d04 |
308 | ) { |
309 | my (@collapse_sets, $uncollapsible_chain); |
310 | |
311 | for my $rel (keys %$relinfo) { |
312 | |
313 | # we already looked at these higher up |
314 | next if ($relinfo->{$rel}{is_single} && $relinfo->{$rel}{is_inner}); |
315 | |
316 | if (my $clps = $relinfo->{$rel}{rsrc}->_resolve_collapse ({ |
317 | as => $rel_cols->{$rel}, |
318 | _rel_chain => [ @{$args->{_rel_chain}}, $rel ], |
319 | _parent_info => { underdefined => 1 }, |
320 | }, $common_args) ) { |
321 | |
322 | # for singles use the idcols wholesale (either there or not) |
323 | if ($relinfo->{$rel}{is_single}) { |
9f98c4b2 |
324 | push @collapse_sets, $clps->{-identifying_columns}; |
fcf32d04 |
325 | } |
326 | elsif (! $relinfo->{$rel}{fk_map}) { |
327 | $uncollapsible_chain = 1; |
328 | last; |
329 | } |
330 | else { |
331 | my $defined_cols_parent_side; |
332 | |
333 | for my $fq_col ( grep { /^$rel\.[^\.]+$/ } keys %{$args->{as}} ) { |
334 | my ($col) = $fq_col =~ /([^\.]+)$/; |
335 | |
336 | $defined_cols_parent_side->{$_} = $args->{as}{$fq_col} for grep |
337 | { $relinfo->{$rel}{fk_map}{$_} eq $col } |
338 | keys %{$relinfo->{$rel}{fk_map}} |
339 | ; |
340 | } |
341 | |
342 | if (my $set = $self->_identifying_column_set([ keys %$defined_cols_parent_side ]) ) { |
343 | push @collapse_sets, [ sort map { $defined_cols_parent_side->{$_} } @$set ]; |
344 | } |
345 | else { |
346 | $uncollapsible_chain = 1; |
347 | last; |
348 | } |
349 | } |
350 | } |
351 | else { |
352 | $uncollapsible_chain = 1; |
353 | last; |
354 | } |
355 | } |
356 | |
357 | unless ($uncollapsible_chain) { |
358 | # if we got here - we are good to go, but the construction is tricky |
359 | # since our children will want to include our collapse criteria - we |
360 | # don't give them anything (safe, since they are all collapsible on their own) |
361 | # in addition we record the individual collapse posibilities |
362 | # of all left children node collapsers, and merge them in the rowparser |
363 | # coderef later |
9f98c4b2 |
364 | $collapse_map->{-identifying_columns} = []; |
365 | $collapse_map->{-identifying_columns_variants} = [ sort { |
fcf32d04 |
366 | (scalar @$a) <=> (scalar @$b) or max(@$a) <=> max(@$b) |
367 | } @collapse_sets ]; |
368 | } |
369 | } |
370 | |
76031e14 |
371 | # stop descending into children if we were called by a parent for first-pass |
372 | # and don't despair if nothing was found (there may be other parallel branches |
373 | # to dive into) |
82f0e0aa |
374 | if ($args->{_parent_info}{underdefined}) { |
9f98c4b2 |
375 | return $collapse_map->{-identifying_columns} ? $collapse_map : undef |
76031e14 |
376 | } |
377 | # nothing down the chain resolved - can't calculate a collapse-map |
9f98c4b2 |
378 | elsif (! $collapse_map->{-identifying_columns}) { |
76031e14 |
379 | $self->throw_exception ( sprintf |
380 | "Unable to calculate a definitive collapse column set for %s%s: fetch more unique non-nullable columns", |
381 | $self->source_name, |
82f0e0aa |
382 | @{$args->{_rel_chain}} > 1 |
383 | ? sprintf (' (last member of the %s chain)', join ' -> ', @{$args->{_rel_chain}} ) |
76031e14 |
384 | : '' |
385 | , |
386 | ); |
387 | } |
388 | |
389 | # If we got that far - we are collapsable - GREAT! Now go down all children |
390 | # a second time, and fill in the rest |
391 | |
a0726a33 |
392 | $collapse_map->{-identifying_columns} = [ __unique_numlist( |
393 | @{ $args->{_parent_info}{collapse_on_idcols}||[] }, |
394 | @{ $collapse_map->{-identifying_columns} }, |
395 | )]; |
3faac878 |
396 | |
397 | my @id_sets; |
76031e14 |
398 | for my $rel (sort keys %$relinfo) { |
399 | |
82f0e0aa |
400 | $collapse_map->{$rel} = $relinfo->{$rel}{rsrc}->_resolve_collapse ({ |
401 | as => { map { $_ => 1 } ( keys %{$rel_cols->{$rel}} ) }, |
402 | _rel_chain => [ @{$args->{_rel_chain}}, $rel], |
403 | _parent_info => { |
3faac878 |
404 | # shallow copy |
9f98c4b2 |
405 | collapse_on_idcols => [ @{$collapse_map->{-identifying_columns}} ], |
76031e14 |
406 | |
407 | rel_condition => $relinfo->{$rel}{fk_map}, |
408 | |
a0726a33 |
409 | is_optional => ! $relinfo->{$rel}{is_inner}, |
410 | |
411 | # if there is at least one *inner* reverse relationship which is HASH-based (equality only) |
412 | # we can safely assume that the child can not exist without us |
413 | rev_rel_is_optional => ( first |
414 | { ref $_->{cond} eq 'HASH' and ($_->{attrs}{join_type}||'') !~ /^left/i } |
415 | values %{ $self->reverse_relationship_info($rel) }, |
416 | ) ? 0 : 1, |
76031e14 |
417 | |
418 | # if this is a 1:1 our own collapser can be used as a collapse-map |
419 | # (regardless of left or not) |
3d8caf63 |
420 | collapser_reusable => ( |
421 | $relinfo->{$rel}{is_single} |
422 | && |
423 | $relinfo->{$rel}{is_inner} |
424 | && |
425 | @{$collapse_map->{-identifying_columns}} |
426 | ) ? 1 : 0, |
76031e14 |
427 | }, |
82f0e0aa |
428 | }, $common_args ); |
76031e14 |
429 | |
430 | $collapse_map->{$rel}{-is_single} = 1 if $relinfo->{$rel}{is_single}; |
431 | $collapse_map->{$rel}{-is_optional} ||= 1 unless $relinfo->{$rel}{is_inner}; |
3faac878 |
432 | } |
76031e14 |
433 | |
434 | return $collapse_map; |
435 | } |
436 | |
76031e14 |
437 | # adding a dep on MoreUtils *just* for this is retarded |
438 | sub __unique_numlist { |
3faac878 |
439 | sort { $a <=> $b } keys %{ {map { $_ => 1 } @_ }} |
76031e14 |
440 | } |
441 | |
76031e14 |
442 | 1; |