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 { |
4a0eed52 |
96 | # $args and $attrs are separated to delineate what is core collapser stuff and |
5b309063 |
97 | # what is dbic $rs specific |
98 | my ($self, $args, $attrs) = @_; |
9f98c4b2 |
99 | |
79adc44f |
100 | die "HRI without pruning makes zero sense" |
101 | if ( $args->{hri_style} && ! $args->{prune_null_branches} ); |
102 | |
103 | my %common = ( |
104 | hri_style => $args->{hri_style}, |
105 | prune_null_branches => $args->{prune_null_branches}, |
106 | val_index => { map |
107 | { $args->{inflate_map}[$_] => $_ } |
108 | ( 0 .. $#{$args->{inflate_map}} ) |
109 | }, |
110 | ); |
111 | |
02a73c96 |
112 | my $check_null_columns; |
113 | |
114 | my $src = (! $args->{collapse} ) ? assemble_simple_parser(\%common) : do { |
115 | my $collapse_map = $self->_resolve_collapse ({ |
116 | # FIXME |
117 | # only consider real columns (not functions) during collapse resolution |
118 | # this check shouldn't really be here, as fucktards are not supposed to |
119 | # alias random crap to existing column names anyway, but still - just in |
120 | # case |
121 | # FIXME !!!! - this does not yet deal with unbalanced selectors correctly |
122 | # (it is now trivial as the attrs specify where things go out of sync |
123 | # needs MOAR tests) |
124 | as => { map |
125 | { ref $attrs->{select}[$common{val_index}{$_}] ? () : ( $_ => $common{val_index}{$_} ) } |
126 | keys %{$common{val_index}} |
127 | }, |
128 | premultiplied => $args->{premultiplied}, |
129 | }); |
130 | |
131 | $check_null_columns = $collapse_map->{-identifying_columns} |
132 | if @{$collapse_map->{-identifying_columns}}; |
133 | |
134 | assemble_collapsing_parser({ |
79adc44f |
135 | %common, |
02a73c96 |
136 | collapse_map => $collapse_map, |
137 | }); |
138 | }; |
139 | |
140 | return ( |
141 | $args->{eval} ? ( eval "sub $src" || die $@ ) : $src, |
142 | $check_null_columns, |
143 | ); |
9f98c4b2 |
144 | } |
145 | |
146 | |
2d0b795a |
147 | # Takes an arrayref selection list and generates a collapse-map representing |
76031e14 |
148 | # row-object fold-points. Every relationship is assigned a set of unique, |
149 | # non-nullable columns (which may *not even be* from the same resultset) |
150 | # and the collapser will use this information to correctly distinguish |
2d0b795a |
151 | # data of individual to-be-row-objects. See t/resultset/rowparser_internals.t |
152 | # for extensive RV examples |
76031e14 |
153 | sub _resolve_collapse { |
82f0e0aa |
154 | my ($self, $args, $common_args) = @_; |
76031e14 |
155 | |
156 | # for comprehensible error messages put ourselves at the head of the relationship chain |
82f0e0aa |
157 | $args->{_rel_chain} ||= [ $self->source_name ]; |
76031e14 |
158 | |
9f98c4b2 |
159 | # record top-level fully-qualified column index, signify toplevelness |
160 | unless ($common_args->{_as_fq_idx}) { |
161 | $common_args->{_as_fq_idx} = { %{$args->{as}} }; |
162 | $args->{_is_top_level} = 1; |
82f0e0aa |
163 | }; |
76031e14 |
164 | |
165 | my ($my_cols, $rel_cols); |
82f0e0aa |
166 | for (keys %{$args->{as}}) { |
76031e14 |
167 | if ($_ =~ /^ ([^\.]+) \. (.+) /x) { |
168 | $rel_cols->{$1}{$2} = 1; |
169 | } |
170 | else { |
fcf32d04 |
171 | $my_cols->{$_} = {}; # important for ||='s below |
76031e14 |
172 | } |
173 | } |
174 | |
175 | my $relinfo; |
fcf32d04 |
176 | # run through relationships, collect metadata |
76031e14 |
177 | for my $rel (keys %$rel_cols) { |
76031e14 |
178 | my $inf = $self->relationship_info ($rel); |
179 | |
95e41036 |
180 | $relinfo->{$rel} = { |
181 | is_single => ( $inf->{attrs}{accessor} && $inf->{attrs}{accessor} ne 'multi' ), |
182 | is_inner => ( ( $inf->{attrs}{join_type} || '' ) !~ /^left/i), |
183 | rsrc => $self->related_source($rel), |
184 | }; |
76031e14 |
185 | |
fcf32d04 |
186 | # FIME - need to use _resolve_cond here instead |
76031e14 |
187 | my $cond = $inf->{cond}; |
188 | |
189 | if ( |
190 | ref $cond eq 'HASH' |
191 | and |
192 | keys %$cond |
193 | and |
fcf32d04 |
194 | ! defined first { $_ !~ /^foreign\./ } (keys %$cond) |
76031e14 |
195 | and |
fcf32d04 |
196 | ! defined first { $_ !~ /^self\./ } (values %$cond) |
76031e14 |
197 | ) { |
198 | for my $f (keys %$cond) { |
199 | my $s = $cond->{$f}; |
200 | $_ =~ s/^ (?: foreign | self ) \.//x for ($f, $s); |
201 | $relinfo->{$rel}{fk_map}{$s} = $f; |
76031e14 |
202 | } |
203 | } |
204 | } |
205 | |
fcf32d04 |
206 | # inject non-left fk-bridges from *INNER-JOINED* children (if any) |
207 | for my $rel (grep { $relinfo->{$_}{is_inner} } keys %$relinfo) { |
208 | my $ri = $relinfo->{$rel}; |
209 | for (keys %{$ri->{fk_map}} ) { |
210 | # need to know source from *our* pov, hence $rel.col |
211 | $my_cols->{$_} ||= { via_fk => "$rel.$ri->{fk_map}{$_}" } |
212 | if defined $rel_cols->{$rel}{$ri->{fk_map}{$_}} # in fact selected |
213 | } |
214 | } |
215 | |
a0726a33 |
216 | # if the parent is already defined *AND* we have an inner reverse relationship |
217 | # (i.e. do not exist without it) , assume all of its related FKs are selected |
76031e14 |
218 | # (even if they in fact are NOT in the select list). Keep a record of what we |
219 | # assumed, and if any such phantom-column becomes part of our own collapser, |
220 | # throw everything assumed-from-parent away and replace with the collapser of |
221 | # the parent (whatever it may be) |
222 | my $assumed_from_parent; |
a0726a33 |
223 | if ( ! $args->{_parent_info}{underdefined} and ! $args->{_parent_info}{rev_rel_is_optional} ) { |
fcf32d04 |
224 | for my $col ( values %{$args->{_parent_info}{rel_condition} || {}} ) { |
225 | next if exists $my_cols->{$col}; |
226 | $my_cols->{$col} = { via_collapse => $args->{_parent_info}{collapse_on_idcols} }; |
227 | $assumed_from_parent->{columns}{$col}++; |
228 | } |
76031e14 |
229 | } |
230 | |
231 | # get colinfo for everything |
232 | if ($my_cols) { |
233 | my $ci = $self->columns_info; |
234 | $my_cols->{$_}{colinfo} = $ci->{$_} for keys %$my_cols; |
235 | } |
236 | |
237 | my $collapse_map; |
238 | |
3faac878 |
239 | # first try to reuse the parent's collapser (i.e. reuse collapser over 1:1) |
240 | # (makes for a leaner coderef later) |
9f98c4b2 |
241 | unless ($collapse_map->{-identifying_columns}) { |
242 | $collapse_map->{-identifying_columns} = $args->{_parent_info}{collapse_on_idcols} |
3faac878 |
243 | if $args->{_parent_info}{collapser_reusable}; |
244 | } |
245 | |
4a0eed52 |
246 | # Still don't know how to collapse - try to resolve based on our columns (plus already inserted FK bridges) |
76031e14 |
247 | if ( |
9f98c4b2 |
248 | ! $collapse_map->{-identifying_columns} |
3faac878 |
249 | and |
76031e14 |
250 | $my_cols |
251 | and |
4e9fc3f3 |
252 | my $idset = $self->_identifying_column_set ({map { $_ => $my_cols->{$_}{colinfo} } keys %$my_cols}) |
76031e14 |
253 | ) { |
254 | # see if the resulting collapser relies on any implied columns, |
255 | # and fix stuff up if this is the case |
4e9fc3f3 |
256 | my @reduced_set = grep { ! $assumed_from_parent->{columns}{$_} } @$idset; |
76031e14 |
257 | |
9f98c4b2 |
258 | $collapse_map->{-identifying_columns} = [ __unique_numlist( |
3faac878 |
259 | @{ $args->{_parent_info}{collapse_on_idcols}||[] }, |
260 | |
76031e14 |
261 | (map |
262 | { |
263 | my $fqc = join ('.', |
82f0e0aa |
264 | @{$args->{_rel_chain}}[1 .. $#{$args->{_rel_chain}}], |
76031e14 |
265 | ( $my_cols->{$_}{via_fk} || $_ ), |
266 | ); |
267 | |
82f0e0aa |
268 | $common_args->{_as_fq_idx}->{$fqc}; |
76031e14 |
269 | } |
4e9fc3f3 |
270 | @reduced_set |
76031e14 |
271 | ), |
3faac878 |
272 | )]; |
76031e14 |
273 | } |
274 | |
275 | # Stil don't know how to collapse - keep descending down 1:1 chains - if |
276 | # a related non-LEFT 1:1 is resolvable - its condition will collapse us |
277 | # too |
9f98c4b2 |
278 | unless ($collapse_map->{-identifying_columns}) { |
76031e14 |
279 | my @candidates; |
280 | |
281 | for my $rel (keys %$relinfo) { |
282 | next unless ($relinfo->{$rel}{is_single} && $relinfo->{$rel}{is_inner}); |
283 | |
82f0e0aa |
284 | if ( my $rel_collapse = $relinfo->{$rel}{rsrc}->_resolve_collapse ({ |
285 | as => $rel_cols->{$rel}, |
286 | _rel_chain => [ @{$args->{_rel_chain}}, $rel ], |
287 | _parent_info => { underdefined => 1 }, |
288 | }, $common_args)) { |
9f98c4b2 |
289 | push @candidates, $rel_collapse->{-identifying_columns}; |
76031e14 |
290 | } |
291 | } |
292 | |
293 | # get the set with least amount of columns |
294 | # FIXME - maybe need to implement a data type order as well (i.e. prefer several ints |
295 | # to a single varchar) |
296 | if (@candidates) { |
9f98c4b2 |
297 | ($collapse_map->{-identifying_columns}) = sort { scalar @$a <=> scalar @$b } (@candidates); |
76031e14 |
298 | } |
299 | } |
300 | |
fcf32d04 |
301 | # Stil don't know how to collapse, and we are the root node. Last ditch |
302 | # effort in case we are *NOT* premultiplied. |
303 | # Run through *each multi* all the way down, left or not, and all |
304 | # *left* singles (a single may become a multi underneath) . When everything |
305 | # gets back see if all the rels link to us definitively. If this is the |
306 | # case we are good - either one of them will define us, or if all are NULLs |
307 | # we know we are "unique" due to the "non-premultiplied" check |
308 | if ( |
9f98c4b2 |
309 | ! $collapse_map->{-identifying_columns} |
fcf32d04 |
310 | and |
311 | ! $args->{premultiplied} |
312 | and |
9f98c4b2 |
313 | $args->{_is_top_level} |
fcf32d04 |
314 | ) { |
315 | my (@collapse_sets, $uncollapsible_chain); |
316 | |
317 | for my $rel (keys %$relinfo) { |
318 | |
319 | # we already looked at these higher up |
320 | next if ($relinfo->{$rel}{is_single} && $relinfo->{$rel}{is_inner}); |
321 | |
322 | if (my $clps = $relinfo->{$rel}{rsrc}->_resolve_collapse ({ |
323 | as => $rel_cols->{$rel}, |
324 | _rel_chain => [ @{$args->{_rel_chain}}, $rel ], |
325 | _parent_info => { underdefined => 1 }, |
326 | }, $common_args) ) { |
327 | |
328 | # for singles use the idcols wholesale (either there or not) |
329 | if ($relinfo->{$rel}{is_single}) { |
9f98c4b2 |
330 | push @collapse_sets, $clps->{-identifying_columns}; |
fcf32d04 |
331 | } |
332 | elsif (! $relinfo->{$rel}{fk_map}) { |
333 | $uncollapsible_chain = 1; |
334 | last; |
335 | } |
336 | else { |
337 | my $defined_cols_parent_side; |
338 | |
339 | for my $fq_col ( grep { /^$rel\.[^\.]+$/ } keys %{$args->{as}} ) { |
340 | my ($col) = $fq_col =~ /([^\.]+)$/; |
341 | |
342 | $defined_cols_parent_side->{$_} = $args->{as}{$fq_col} for grep |
343 | { $relinfo->{$rel}{fk_map}{$_} eq $col } |
344 | keys %{$relinfo->{$rel}{fk_map}} |
345 | ; |
346 | } |
347 | |
348 | if (my $set = $self->_identifying_column_set([ keys %$defined_cols_parent_side ]) ) { |
349 | push @collapse_sets, [ sort map { $defined_cols_parent_side->{$_} } @$set ]; |
350 | } |
351 | else { |
352 | $uncollapsible_chain = 1; |
353 | last; |
354 | } |
355 | } |
356 | } |
357 | else { |
358 | $uncollapsible_chain = 1; |
359 | last; |
360 | } |
361 | } |
362 | |
363 | unless ($uncollapsible_chain) { |
364 | # if we got here - we are good to go, but the construction is tricky |
365 | # since our children will want to include our collapse criteria - we |
366 | # don't give them anything (safe, since they are all collapsible on their own) |
4a0eed52 |
367 | # in addition we record the individual collapse possibilities |
fcf32d04 |
368 | # of all left children node collapsers, and merge them in the rowparser |
369 | # coderef later |
9f98c4b2 |
370 | $collapse_map->{-identifying_columns} = []; |
371 | $collapse_map->{-identifying_columns_variants} = [ sort { |
fcf32d04 |
372 | (scalar @$a) <=> (scalar @$b) or max(@$a) <=> max(@$b) |
373 | } @collapse_sets ]; |
374 | } |
375 | } |
376 | |
76031e14 |
377 | # stop descending into children if we were called by a parent for first-pass |
378 | # and don't despair if nothing was found (there may be other parallel branches |
379 | # to dive into) |
82f0e0aa |
380 | if ($args->{_parent_info}{underdefined}) { |
9f98c4b2 |
381 | return $collapse_map->{-identifying_columns} ? $collapse_map : undef |
76031e14 |
382 | } |
383 | # nothing down the chain resolved - can't calculate a collapse-map |
9f98c4b2 |
384 | elsif (! $collapse_map->{-identifying_columns}) { |
76031e14 |
385 | $self->throw_exception ( sprintf |
386 | "Unable to calculate a definitive collapse column set for %s%s: fetch more unique non-nullable columns", |
387 | $self->source_name, |
82f0e0aa |
388 | @{$args->{_rel_chain}} > 1 |
389 | ? sprintf (' (last member of the %s chain)', join ' -> ', @{$args->{_rel_chain}} ) |
76031e14 |
390 | : '' |
391 | , |
392 | ); |
393 | } |
394 | |
395 | # If we got that far - we are collapsable - GREAT! Now go down all children |
396 | # a second time, and fill in the rest |
397 | |
a0726a33 |
398 | $collapse_map->{-identifying_columns} = [ __unique_numlist( |
399 | @{ $args->{_parent_info}{collapse_on_idcols}||[] }, |
400 | @{ $collapse_map->{-identifying_columns} }, |
401 | )]; |
3faac878 |
402 | |
403 | my @id_sets; |
76031e14 |
404 | for my $rel (sort keys %$relinfo) { |
405 | |
82f0e0aa |
406 | $collapse_map->{$rel} = $relinfo->{$rel}{rsrc}->_resolve_collapse ({ |
407 | as => { map { $_ => 1 } ( keys %{$rel_cols->{$rel}} ) }, |
408 | _rel_chain => [ @{$args->{_rel_chain}}, $rel], |
409 | _parent_info => { |
3faac878 |
410 | # shallow copy |
9f98c4b2 |
411 | collapse_on_idcols => [ @{$collapse_map->{-identifying_columns}} ], |
76031e14 |
412 | |
413 | rel_condition => $relinfo->{$rel}{fk_map}, |
414 | |
a0726a33 |
415 | is_optional => ! $relinfo->{$rel}{is_inner}, |
416 | |
417 | # if there is at least one *inner* reverse relationship which is HASH-based (equality only) |
418 | # we can safely assume that the child can not exist without us |
419 | rev_rel_is_optional => ( first |
420 | { ref $_->{cond} eq 'HASH' and ($_->{attrs}{join_type}||'') !~ /^left/i } |
421 | values %{ $self->reverse_relationship_info($rel) }, |
422 | ) ? 0 : 1, |
76031e14 |
423 | |
424 | # if this is a 1:1 our own collapser can be used as a collapse-map |
425 | # (regardless of left or not) |
3d8caf63 |
426 | collapser_reusable => ( |
427 | $relinfo->{$rel}{is_single} |
428 | && |
429 | $relinfo->{$rel}{is_inner} |
430 | && |
431 | @{$collapse_map->{-identifying_columns}} |
432 | ) ? 1 : 0, |
76031e14 |
433 | }, |
82f0e0aa |
434 | }, $common_args ); |
76031e14 |
435 | |
436 | $collapse_map->{$rel}{-is_single} = 1 if $relinfo->{$rel}{is_single}; |
437 | $collapse_map->{$rel}{-is_optional} ||= 1 unless $relinfo->{$rel}{is_inner}; |
3faac878 |
438 | } |
76031e14 |
439 | |
440 | return $collapse_map; |
441 | } |
442 | |
76031e14 |
443 | # adding a dep on MoreUtils *just* for this is retarded |
444 | sub __unique_numlist { |
3faac878 |
445 | sort { $a <=> $b } keys %{ {map { $_ => 1 } @_ }} |
76031e14 |
446 | } |
447 | |
76031e14 |
448 | 1; |