I think we are done here
[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
7use Try::Tiny;
8use List::Util 'first';
9use B 'perlstring';
10
11use namespace::clean;
12
13use base 'DBIx::Class';
14
15# Accepts one or more relationships for the current source and returns an
16# array of column names for each of those relationships. Column names are
17# prefixed relative to the current source, in accordance with where they appear
18# in the supplied relationships.
19sub _resolve_prefetch {
20 my ($self, $pre, $alias, $alias_map, $order, $pref_path) = @_;
21 $pref_path ||= [];
22
23 if (not defined $pre or not length $pre) {
24 return ();
25 }
26 elsif( ref $pre eq 'ARRAY' ) {
27 return
28 map { $self->_resolve_prefetch( $_, $alias, $alias_map, $order, [ @$pref_path ] ) }
29 @$pre;
30 }
31 elsif( ref $pre eq 'HASH' ) {
32 my @ret =
33 map {
34 $self->_resolve_prefetch($_, $alias, $alias_map, $order, [ @$pref_path ] ),
35 $self->related_source($_)->_resolve_prefetch(
4e9fc3f3 36 $pre->{$_}, "${alias}.$_", $alias_map, $order, [ @$pref_path, $_] )
76031e14 37 } keys %$pre;
38 return @ret;
39 }
40 elsif( ref $pre ) {
41 $self->throw_exception(
42 "don't know how to resolve prefetch reftype ".ref($pre));
43 }
44 else {
45 my $p = $alias_map;
46 $p = $p->{$_} for (@$pref_path, $pre);
47
48 $self->throw_exception (
49 "Unable to resolve prefetch '$pre' - join alias map does not contain an entry for path: "
50 . join (' -> ', @$pref_path, $pre)
51 ) if (ref $p->{-join_aliases} ne 'ARRAY' or not @{$p->{-join_aliases}} );
52
53 my $as = shift @{$p->{-join_aliases}};
54
55 my $rel_info = $self->relationship_info( $pre );
56 $self->throw_exception( $self->source_name . " has no such relationship '$pre'" )
57 unless $rel_info;
58
59 my $as_prefix = ($alias =~ /^.*?\.(.+)$/ ? $1.'.' : '');
76031e14 60
61 return map { [ "${as}.$_", "${as_prefix}${pre}.$_", ] }
4e9fc3f3 62 $self->related_source($pre)->columns;
76031e14 63 }
64}
65
66# Takes a selection list and generates a collapse-map representing
67# row-object fold-points. Every relationship is assigned a set of unique,
68# non-nullable columns (which may *not even be* from the same resultset)
69# and the collapser will use this information to correctly distinguish
70# data of individual to-be-row-objects.
71sub _resolve_collapse {
72 my ($self, $as, $as_fq_idx, $rel_chain, $parent_info, $node_idx_ref) = @_;
73
74 # for comprehensible error messages put ourselves at the head of the relationship chain
75 $rel_chain ||= [ $self->source_name ];
76
77 # record top-level fully-qualified column index
78 $as_fq_idx ||= { %$as };
79
80 my ($my_cols, $rel_cols);
81 for (keys %$as) {
82 if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
83 $rel_cols->{$1}{$2} = 1;
84 }
85 else {
86 $my_cols->{$_} = {}; # important for ||= below
87 }
88 }
89
90 my $relinfo;
91 # run through relationships, collect metadata, inject non-left fk-bridges from
92 # *INNER-JOINED* children (if any)
93 for my $rel (keys %$rel_cols) {
94 my $rel_src = __get_related_source($self, $rel, $rel_cols->{$rel});
95
96 my $inf = $self->relationship_info ($rel);
97
98 $relinfo->{$rel}{is_single} = $inf->{attrs}{accessor} && $inf->{attrs}{accessor} ne 'multi';
99 $relinfo->{$rel}{is_inner} = ( $inf->{attrs}{join_type} || '' ) !~ /^left/i;
100 $relinfo->{$rel}{rsrc} = $rel_src;
101
102 my $cond = $inf->{cond};
103
104 if (
105 ref $cond eq 'HASH'
106 and
107 keys %$cond
108 and
109 ! first { $_ !~ /^foreign\./ } (keys %$cond)
110 and
111 ! first { $_ !~ /^self\./ } (values %$cond)
112 ) {
113 for my $f (keys %$cond) {
114 my $s = $cond->{$f};
115 $_ =~ s/^ (?: foreign | self ) \.//x for ($f, $s);
116 $relinfo->{$rel}{fk_map}{$s} = $f;
117
4e9fc3f3 118 # need to know source from *our* pov, hence $rel.
76031e14 119 $my_cols->{$s} ||= { via_fk => "$rel.$f" } if (
120 defined $rel_cols->{$rel}{$f} # in fact selected
121 and
4e9fc3f3 122 $relinfo->{$rel}{is_inner}
76031e14 123 );
124 }
125 }
126 }
127
128 # if the parent is already defined, assume all of its related FKs are selected
129 # (even if they in fact are NOT in the select list). Keep a record of what we
130 # assumed, and if any such phantom-column becomes part of our own collapser,
131 # throw everything assumed-from-parent away and replace with the collapser of
132 # the parent (whatever it may be)
133 my $assumed_from_parent;
134 unless ($parent_info->{underdefined}) {
135 $assumed_from_parent->{columns} = { map
136 # only add to the list if we do not already select said columns
137 { ! exists $my_cols->{$_} ? ( $_ => 1 ) : () }
138 values %{$parent_info->{rel_condition} || {}}
139 };
140
141 $my_cols->{$_} = { via_collapse => $parent_info->{collapse_on} }
142 for keys %{$assumed_from_parent->{columns}};
143 }
144
145 # get colinfo for everything
146 if ($my_cols) {
147 my $ci = $self->columns_info;
148 $my_cols->{$_}{colinfo} = $ci->{$_} for keys %$my_cols;
149 }
150
151 my $collapse_map;
152
153 # try to resolve based on our columns (plus already inserted FK bridges)
154 if (
155 $my_cols
156 and
4e9fc3f3 157 my $idset = $self->_identifying_column_set ({map { $_ => $my_cols->{$_}{colinfo} } keys %$my_cols})
76031e14 158 ) {
159 # see if the resulting collapser relies on any implied columns,
160 # and fix stuff up if this is the case
4e9fc3f3 161 my @reduced_set = grep { ! $assumed_from_parent->{columns}{$_} } @$idset;
76031e14 162
76031e14 163 $collapse_map->{-node_id} = __unique_numlist(
4e9fc3f3 164 (@reduced_set != @$idset) ? @{$parent_info->{collapse_on}} : (),
76031e14 165 (map
166 {
167 my $fqc = join ('.',
168 @{$rel_chain}[1 .. $#$rel_chain],
169 ( $my_cols->{$_}{via_fk} || $_ ),
170 );
171
172 $as_fq_idx->{$fqc};
173 }
4e9fc3f3 174 @reduced_set
76031e14 175 ),
176 );
177 }
178
179 # Stil don't know how to collapse - keep descending down 1:1 chains - if
180 # a related non-LEFT 1:1 is resolvable - its condition will collapse us
181 # too
182 unless ($collapse_map->{-node_id}) {
183 my @candidates;
184
185 for my $rel (keys %$relinfo) {
186 next unless ($relinfo->{$rel}{is_single} && $relinfo->{$rel}{is_inner});
187
188 if ( my $rel_collapse = $relinfo->{$rel}{rsrc}->_resolve_collapse (
189 $rel_cols->{$rel},
190 $as_fq_idx,
191 [ @$rel_chain, $rel ],
192 { underdefined => 1 }
193 )) {
194 push @candidates, $rel_collapse->{-node_id};
195 }
196 }
197
198 # get the set with least amount of columns
199 # FIXME - maybe need to implement a data type order as well (i.e. prefer several ints
200 # to a single varchar)
201 if (@candidates) {
202 ($collapse_map->{-node_id}) = sort { scalar @$a <=> scalar @$b } (@candidates);
203 }
204 }
205
206 # Still dont know how to collapse - see if the parent passed us anything
207 # (i.e. reuse collapser over 1:1)
208 unless ($collapse_map->{-node_id}) {
209 $collapse_map->{-node_id} = $parent_info->{collapse_on}
210 if $parent_info->{collapser_reusable};
211 }
212
213 # stop descending into children if we were called by a parent for first-pass
214 # and don't despair if nothing was found (there may be other parallel branches
215 # to dive into)
216 if ($parent_info->{underdefined}) {
217 return $collapse_map->{-node_id} ? $collapse_map : undef
218 }
219 # nothing down the chain resolved - can't calculate a collapse-map
220 elsif (! $collapse_map->{-node_id}) {
221 $self->throw_exception ( sprintf
222 "Unable to calculate a definitive collapse column set for %s%s: fetch more unique non-nullable columns",
223 $self->source_name,
224 @$rel_chain > 1
225 ? sprintf (' (last member of the %s chain)', join ' -> ', @$rel_chain )
226 : ''
227 ,
228 );
229 }
230
231 # If we got that far - we are collapsable - GREAT! Now go down all children
232 # a second time, and fill in the rest
233
234 $collapse_map->{-is_optional} = 1 if $parent_info->{is_optional};
235 $collapse_map->{-node_index} = ${ $node_idx_ref ||= \do { my $x = 1 } }++; # this is *deliberately* not 0-based
236
237 my (@id_sets, $multis_in_chain);
238 for my $rel (sort keys %$relinfo) {
239
240 $collapse_map->{$rel} = $relinfo->{$rel}{rsrc}->_resolve_collapse (
241 { map { $_ => 1 } ( keys %{$rel_cols->{$rel}} ) },
242
243 $as_fq_idx,
244
245 [ @$rel_chain, $rel],
246
247 {
248 collapse_on => [ @{$collapse_map->{-node_id}} ],
249
250 rel_condition => $relinfo->{$rel}{fk_map},
251
252 is_optional => $collapse_map->{-is_optional},
253
254 # if this is a 1:1 our own collapser can be used as a collapse-map
255 # (regardless of left or not)
256 collapser_reusable => $relinfo->{$rel}{is_single},
257 },
258
259 $node_idx_ref,
260 );
261
262 $collapse_map->{$rel}{-is_single} = 1 if $relinfo->{$rel}{is_single};
263 $collapse_map->{$rel}{-is_optional} ||= 1 unless $relinfo->{$rel}{is_inner};
264 push @id_sets, @{ $collapse_map->{$rel}{-branch_id} };
265 }
266
267 $collapse_map->{-branch_id} = __unique_numlist( @id_sets, @{$collapse_map->{-node_id}} );
268
269 return $collapse_map;
270}
271
76031e14 272# Takes an arrayref of {as} dbic column aliases and the collapse and select
273# attributes from the same $rs (the slector requirement is a temporary
274# workaround), and returns a coderef capable of:
275# my $me_pref_clps = $coderef->([$rs->cursor->next])
276# Where the $me_pref_clps arrayref is the future argument to
277# ::ResultSet::_collapse_result.
278#
279# $me_pref_clps->[0] is always returned (even if as an empty hash with no
280# rowdata), however branches of related data in $me_pref_clps->[1] may be
281# pruned short of what was originally requested based on {as}, depending
282# on:
283#
284# * If collapse is requested, a definitive collapse map is calculated for
285# every relationship "fold-point", consisting of a set of values (which
286# may not even be contained in the future 'me' of said relationship
287# (for example a cd.artist_id defines the related inner-joined artist)).
288# Thus a definedness check is carried on all collapse-condition values
289# and if at least one is undef it is assumed that we are dealing with a
290# NULLed right-side of a left-join, so we don't return a related data
291# container at all, which implies no related objects
292#
293# * If we are not collapsing, there is no constraint on having a selector
294# uniquely identifying all possible objects, and the user might have very
295# well requested a column that just *happens* to be all NULLs. What we do
296# in this case is fallback to the old behavior (which is a potential FIXME)
297# by always returning a data container, but only filling it with columns
298# IFF at least one of them is defined. This way we do not get an object
299# with a bunch of has_column_loaded to undef, but at the same time do not
300# further relationships based off this "null" object (e.g. in case the user
301# deliberately skipped link-table values). I am pretty sure there are some
302# tests that codify this behavior, need to find the exact testname.
303#
304# For an example of this coderef in action (and to see its guts) look at
305# t/prefetch/_internals.t
306#
307# This is a huge performance win, as we call the same code for
308# every row returned from the db, thus avoiding repeated method
309# lookups when traversing relationships
310#
311# Also since the coderef is completely stateless (the returned structure is
312# always fresh on every new invocation) this is a very good opportunity for
313# memoization if further speed improvements are needed
314#
315# The way we construct this coderef is somewhat fugly, although I am not
316# sure if the string eval is *that* bad of an idea. The alternative is to
317# have a *very* large number of anon coderefs calling each other in a twisty
318# maze, whereas the current result is a nice, smooth, single-pass function.
319# In any case - the output of this thing is meticulously micro-tested, so
320# any sort of rewrite should be relatively easy
321#
322sub _mk_row_parser {
323 my ($self, $args) = @_;
324
325 my $inflate_index = { map
326 { $args->{inflate_map}[$_] => $_ }
327 ( 0 .. $#{$args->{inflate_map}} )
328 };
329
330 my ($parser_src);
331 if ($args->{collapse}) {
76031e14 332
333 my $collapse_map = $self->_resolve_collapse (
334 # FIXME
335 # only consider real columns (not functions) during collapse resolution
336 # this check shouldn't really be here, as fucktards are not supposed to
337 # alias random crap to existing column names anyway, but still - just in
338 # case
339 # FIXME !!!! - this does not yet deal with unbalanced selectors correctly
340 # (it is now trivial as the attrs specify where things go out of sync)
341 { map
342 { ref $args->{selection}[$inflate_index->{$_}] ? () : ( $_ => $inflate_index->{$_} ) }
343 keys %$inflate_index
344 }
345 );
346
4e9fc3f3 347 my $top_branch_idx_list = join (', ', @{$collapse_map->{-branch_id}});
76031e14 348
4e9fc3f3 349 my $top_node_id_path = join ('', map
350 { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
351 @{$collapse_map->{-node_id}}
352 );
76031e14 353
4e9fc3f3 354 my $rel_assemblers = __visit_infmap_collapse (
76031e14 355 $inflate_index, $collapse_map
356 );
76031e14 357
4e9fc3f3 358 $parser_src = sprintf (<<'EOS', $top_branch_idx_list, $top_node_id_path, $rel_assemblers);
76031e14 359### BEGIN STRING EVAL
4e9fc3f3 360
361 my ($rows_pos, $result_pos, $cur_row, @cur_row_ids, @collapse_idx, $is_new_res) = (0,0);
76031e14 362
363 # this loop is a bit arcane - the rationale is that the passed in
364 # $_[0] will either have only one row (->next) or will have all
365 # rows already pulled in (->all and/or unordered). Given that the
366 # result can be rather large - we reuse the same already allocated
367 # array, since the collapsed prefetch is smaller by definition.
368 # At the end we cut the leftovers away and move on.
369 while ($cur_row =
4e9fc3f3 370 ( ( $rows_pos >= 0 and $_[0][$rows_pos++] ) or do { $rows_pos = -1; undef } )
76031e14 371 ||
372 ($_[1] and $_[1]->())
373 ) {
374
4e9fc3f3 375 $cur_row_ids[$_] = defined $cur_row->[$_] ? $cur_row->[$_] : "\xFF\xFFN\xFFU\xFFL\xFFL\xFF\xFF"
76031e14 376 for (%1$s); # the top branch_id includes all id values
377
4e9fc3f3 378 $is_new_res = ! $collapse_idx[1]%2$s and (
379 $_[1] and $result_pos and (unshift @{$_[2]}, $cur_row) and last
380 );
76031e14 381
4e9fc3f3 382 %3$s
76031e14 383
4e9fc3f3 384 $_[0][$result_pos++] = $collapse_idx[1]%2$s
76031e14 385 if $is_new_res;
386 }
387
388 splice @{$_[0]}, $result_pos; # truncate the passed in array for cases of collapsing ->all()
389### END STRING EVAL
390EOS
391
392 # change the quoted placeholders to unquoted alias-references
393 $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$cur_row->[$1]"/gex;
4e9fc3f3 394 $parser_src =~ s/ \' \xFF__IDVALPOS__(\d+)__\xFF \' /"\$cur_row_ids[$1]"/gex;
76031e14 395 }
396
397 else {
398 $parser_src = sprintf('$_ = %s for @{$_[0]}', __visit_infmap_simple(
399 $inflate_index, { rsrc => $self }), # need the $rsrc to determine left-ness
400 );
401
402 # change the quoted placeholders to unquoted alias-references
403 # !!! note - different var than the one above
404 $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$_->[$1]"/gex;
405 }
406
407 $parser_src;
408}
409
410sub __visit_infmap_simple {
411 my ($val_idx, $args) = @_;
412
413 my $my_cols = {};
414 my $rel_cols;
415 for (keys %$val_idx) {
416 if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
417 $rel_cols->{$1}{$2} = $val_idx->{$_};
418 }
419 else {
420 $my_cols->{$_} = $val_idx->{$_};
421 }
422 }
423 my @relperl;
424 for my $rel (sort keys %$rel_cols) {
425
426 my $rel_rsrc = __get_related_source($args->{rsrc}, $rel, $rel_cols->{$rel});
427
428 #my $optional = $args->{is_optional};
429 #$optional ||= ($args->{rsrc}->relationship_info($rel)->{attrs}{join_type} || '') =~ /^left/i;
430
431 push @relperl, join ' => ', perlstring($rel), __visit_infmap_simple($rel_cols->{$rel}, {
432 non_top => 1,
433 #is_optional => $optional,
434 rsrc => $rel_rsrc,
435 });
436
437 # FIXME SUBOPTIMAL - disabled to satisfy t/resultset/inflate_result_api.t
438 #if ($optional and my @branch_null_checks = map
439 # { "(! defined '\xFF__VALPOS__${_}__\xFF')" }
440 # sort { $a <=> $b } values %{$rel_cols->{$rel}}
441 #) {
442 # $relperl[-1] = sprintf ( '(%s) ? ( %s => [] ) : ( %s )',
443 # join (' && ', @branch_null_checks ),
444 # perlstring($rel),
445 # $relperl[-1],
446 # );
447 #}
448 }
449
450 my $me_struct = keys %$my_cols
451 ? __visit_dump({ map { $_ => "\xFF__VALPOS__$my_cols->{$_}__\xFF" } (keys %$my_cols) })
452 : 'undef'
453 ;
454
455 return sprintf '[%s]', join (',',
456 $me_struct,
457 @relperl ? sprintf ('{ %s }', join (',', @relperl)) : (),
458 );
459}
460
461sub __visit_infmap_collapse {
462
463 my ($val_idx, $collapse_map, $parent_info) = @_;
464
465 my $my_cols = {};
466 my $rel_cols;
467 for (keys %$val_idx) {
468 if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
469 $rel_cols->{$1}{$2} = $val_idx->{$_};
470 }
471 else {
472 $my_cols->{$_} = $val_idx->{$_};
473 }
474 }
475
476 my $sequenced_node_id = join ('', map
477 { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
478 @{$collapse_map->{-node_id}}
479 );
480
481 my $me_struct = keys %$my_cols
482 ? __visit_dump([{ map { $_ => "\xFF__VALPOS__$my_cols->{$_}__\xFF" } (keys %$my_cols) }])
4e9fc3f3 483 : undef
76031e14 484 ;
485 my $node_idx_ref = sprintf '$collapse_idx[%d]%s', $collapse_map->{-node_index}, $sequenced_node_id;
486
487 my $parent_idx_ref = sprintf( '$collapse_idx[%d]%s[1]{%s}',
488 @{$parent_info}{qw/node_idx sequenced_node_id/},
489 perlstring($parent_info->{relname}),
490 ) if $parent_info;
491
492 my @src;
493 if ($collapse_map->{-node_index} == 1) {
494 push @src, sprintf( '%s ||= %s;',
495 $node_idx_ref,
496 $me_struct,
4e9fc3f3 497 ) if $me_struct;
76031e14 498 }
499 elsif ($collapse_map->{-is_single}) {
4e9fc3f3 500 push @src, sprintf ( '%s ||= %s%s;',
76031e14 501 $parent_idx_ref,
502 $node_idx_ref,
4e9fc3f3 503 $me_struct ? " ||= $me_struct" : '',
76031e14 504 );
505 }
506 else {
4e9fc3f3 507 push @src, sprintf('push @{%s}, %s%s unless %s;',
76031e14 508 $parent_idx_ref,
509 $node_idx_ref,
4e9fc3f3 510 $me_struct ? " ||= $me_struct" : '',
76031e14 511 $node_idx_ref,
512 );
513 }
514
515 #my $known_defined = { %{ $parent_info->{known_defined} || {} } };
516 #$known_defined->{$_}++ for @{$collapse_map->{-node_id}};
517
518 for my $rel (sort keys %$rel_cols) {
519
4e9fc3f3 520 push @src, sprintf( '%s[1]{%s} ||= [];', $node_idx_ref, perlstring($rel) )
521 unless $collapse_map->{$rel}{-is_single};
76031e14 522
523 push @src, __visit_infmap_collapse($rel_cols->{$rel}, $collapse_map->{$rel}, {
524 node_idx => $collapse_map->{-node_index},
525 sequenced_node_id => $sequenced_node_id,
526 relname => $rel,
527 #known_defined => $known_defined,
528 });
529
530 # FIXME SUBOPTIMAL - disabled to satisfy t/resultset/inflate_result_api.t
531 #if ($collapse_map->{$rel}{-is_optional} and my @null_checks = map
4e9fc3f3 532 # { "(! defined '\xFF__IDVALPOS__${_}__\xFF')" }
76031e14 533 # sort { $a <=> $b } grep
534 # { ! $known_defined->{$_} }
535 # @{$collapse_map->{$rel}{-node_id}}
536 #) {
537 # $src[-1] = sprintf( '(%s) or %s',
538 # join (' || ', @null_checks ),
539 # $src[-1],
540 # );
541 #}
542 }
543
544 join "\n", @src;
545}
546
547# adding a dep on MoreUtils *just* for this is retarded
548sub __unique_numlist {
549 [ sort { $a <=> $b } keys %{ {map { $_ => 1 } @_ }} ]
550}
551
552# This error must be thrown from two distinct codepaths, joining them is
553# rather hard. Go for this hack instead.
554sub __get_related_source {
555 my ($rsrc, $rel, $relcols) = @_;
556 try {
557 $rsrc->related_source ($rel)
558 } catch {
559 $rsrc->throw_exception(sprintf(
560 "Can't inflate prefetch into non-existent relationship '%s' from '%s', "
561 . "check the inflation specification (columns/as) ending in '...%s.%s'.",
562 $rel,
563 $rsrc->source_name,
564 $rel,
565 (sort { length($a) <=> length ($b) } keys %$relcols)[0],
566 ))};
567}
568
569# keep our own DD object around so we don't have to fitz with quoting
570my $dumper_obj;
571sub __visit_dump {
572 # we actually will be producing functional perl code here,
573 # thus no second-guessing of what these globals might have
574 # been set to. DO NOT CHANGE!
575 ($dumper_obj ||= do {
576 require Data::Dumper;
577 Data::Dumper->new([])
578 ->Useperl (1)
579 ->Purity (1)
580 ->Pad ('')
581 ->Useqq (0)
582 ->Terse (1)
583 ->Quotekeys (1)
584 ->Deepcopy (0)
585 ->Deparse (0)
586 ->Maxdepth (0)
587 ->Indent (0) # faster but harder to read, perhaps leave at 1 ?
588 })->Values ([$_[0]])->Dump;
589}
590
5911;