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