1 package DBIx::Class::ResultSource::RowParser;
7 use List::Util 'first';
12 use base 'DBIx::Class';
14 # Accepts one or more relationships for the current source and returns an
15 # array of column names for each of those relationships. Column names are
16 # prefixed relative to the current source, in accordance with where they appear
17 # in the supplied relationships.
18 sub _resolve_prefetch {
19 my ($self, $pre, $alias, $alias_map, $order, $pref_path) = @_;
22 if (not defined $pre or not length $pre) {
25 elsif( ref $pre eq 'ARRAY' ) {
27 map { $self->_resolve_prefetch( $_, $alias, $alias_map, $order, [ @$pref_path ] ) }
30 elsif( ref $pre eq 'HASH' ) {
33 $self->_resolve_prefetch($_, $alias, $alias_map, $order, [ @$pref_path ] ),
34 $self->related_source($_)->_resolve_prefetch(
35 $pre->{$_}, "${alias}.$_", $alias_map, $order, [ @$pref_path, $_] )
40 $self->throw_exception(
41 "don't know how to resolve prefetch reftype ".ref($pre));
45 $p = $p->{$_} for (@$pref_path, $pre);
47 $self->throw_exception (
48 "Unable to resolve prefetch '$pre' - join alias map does not contain an entry for path: "
49 . join (' -> ', @$pref_path, $pre)
50 ) if (ref $p->{-join_aliases} ne 'ARRAY' or not @{$p->{-join_aliases}} );
52 my $as = shift @{$p->{-join_aliases}};
54 my $rel_info = $self->relationship_info( $pre );
55 $self->throw_exception( $self->source_name . " has no such relationship '$pre'" )
58 my $as_prefix = ($alias =~ /^.*?\.(.+)$/ ? $1.'.' : '');
59 my $rel_source = $self->related_source($pre);
61 if ($rel_info->{attrs}{accessor} && $rel_info->{attrs}{accessor} eq 'multi') {
62 $self->throw_exception(
63 "Can't prefetch has_many ${pre} (join cond too complex)")
64 unless ref($rel_info->{cond}) eq 'HASH';
65 my $dots = @{[$as_prefix =~ m/\./g]} + 1; # +1 to match the ".${as_prefix}"
67 #my @col = map { (/^self\.(.+)$/ ? ("${as_prefix}.$1") : ()); }
68 # values %{$rel_info->{cond}};
69 my @key = map { (/^foreign\.(.+)$/ ? ($1) : ()); }
70 keys %{$rel_info->{cond}};
72 push @$order, map { "${as}.$_" } @key;
74 if (my $rel_order = $rel_info->{attrs}{order_by}) {
75 # this is kludgy and incomplete, I am well aware
76 # but the parent method is going away entirely anyway
78 my $sql_maker = $self->storage->sql_maker;
79 my ($orig_ql, $orig_qr) = $sql_maker->_quote_chars;
80 my $sep = $sql_maker->name_sep;
82 # install our own quoter, so we can catch unqualified stuff
83 local $sql_maker->{quote_char} = ["\x00", "\xFF"];
85 my $quoted_prefix = "\x00${as}\xFF";
87 for my $chunk ( $sql_maker->_order_by_chunks ($rel_order) ) {
89 ($chunk, @bind) = @$chunk if ref $chunk;
91 $chunk = "${quoted_prefix}${sep}${chunk}"
92 unless $chunk =~ /\Q$sep/;
94 $chunk =~ s/\x00/$orig_ql/g;
95 $chunk =~ s/\xFF/$orig_qr/g;
96 push @$order, \[$chunk, @bind];
101 return map { [ "${as}.$_", "${as_prefix}${pre}.$_", ] }
102 $rel_source->columns;
106 # Takes a selection list and generates a collapse-map representing
107 # row-object fold-points. Every relationship is assigned a set of unique,
108 # non-nullable columns (which may *not even be* from the same resultset)
109 # and the collapser will use this information to correctly distinguish
110 # data of individual to-be-row-objects.
111 sub _resolve_collapse {
112 my ($self, $as, $as_fq_idx, $rel_chain, $parent_info, $node_idx_ref) = @_;
114 # for comprehensible error messages put ourselves at the head of the relationship chain
115 $rel_chain ||= [ $self->source_name ];
117 # record top-level fully-qualified column index
118 $as_fq_idx ||= { %$as };
120 my ($my_cols, $rel_cols);
122 if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
123 $rel_cols->{$1}{$2} = 1;
126 $my_cols->{$_} = {}; # important for ||= below
131 # run through relationships, collect metadata, inject non-left fk-bridges from
132 # *INNER-JOINED* children (if any)
133 for my $rel (keys %$rel_cols) {
134 my $rel_src = __get_related_source($self, $rel, $rel_cols->{$rel});
136 my $inf = $self->relationship_info ($rel);
138 $relinfo->{$rel}{is_single} = $inf->{attrs}{accessor} && $inf->{attrs}{accessor} ne 'multi';
139 $relinfo->{$rel}{is_inner} = ( $inf->{attrs}{join_type} || '' ) !~ /^left/i;
140 $relinfo->{$rel}{rsrc} = $rel_src;
142 my $cond = $inf->{cond};
149 ! first { $_ !~ /^foreign\./ } (keys %$cond)
151 ! first { $_ !~ /^self\./ } (values %$cond)
153 for my $f (keys %$cond) {
155 $_ =~ s/^ (?: foreign | self ) \.//x for ($f, $s);
156 $relinfo->{$rel}{fk_map}{$s} = $f;
158 # need to know source from *our* pov, hnce $rel.
159 $my_cols->{$s} ||= { via_fk => "$rel.$f" } if (
160 defined $rel_cols->{$rel}{$f} # in fact selected
162 (! $node_idx_ref or $relinfo->{$rel}{is_inner}) # either top-level or an inner join
168 # if the parent is already defined, assume all of its related FKs are selected
169 # (even if they in fact are NOT in the select list). Keep a record of what we
170 # assumed, and if any such phantom-column becomes part of our own collapser,
171 # throw everything assumed-from-parent away and replace with the collapser of
172 # the parent (whatever it may be)
173 my $assumed_from_parent;
174 unless ($parent_info->{underdefined}) {
175 $assumed_from_parent->{columns} = { map
176 # only add to the list if we do not already select said columns
177 { ! exists $my_cols->{$_} ? ( $_ => 1 ) : () }
178 values %{$parent_info->{rel_condition} || {}}
181 $my_cols->{$_} = { via_collapse => $parent_info->{collapse_on} }
182 for keys %{$assumed_from_parent->{columns}};
185 # get colinfo for everything
187 my $ci = $self->columns_info;
188 $my_cols->{$_}{colinfo} = $ci->{$_} for keys %$my_cols;
193 # try to resolve based on our columns (plus already inserted FK bridges)
197 my $uset = $self->_unique_column_set ($my_cols)
199 # see if the resulting collapser relies on any implied columns,
200 # and fix stuff up if this is the case
202 my $parent_collapser_used = defined delete @{$uset}{keys %{$assumed_from_parent->{columns}}};
203 $collapse_map->{-node_id} = __unique_numlist(
204 $parent_collapser_used ? @{$parent_info->{collapse_on}} : (),
208 @{$rel_chain}[1 .. $#$rel_chain],
209 ( $my_cols->{$_}{via_fk} || $_ ),
219 # Stil don't know how to collapse - keep descending down 1:1 chains - if
220 # a related non-LEFT 1:1 is resolvable - its condition will collapse us
222 unless ($collapse_map->{-node_id}) {
225 for my $rel (keys %$relinfo) {
226 next unless ($relinfo->{$rel}{is_single} && $relinfo->{$rel}{is_inner});
228 if ( my $rel_collapse = $relinfo->{$rel}{rsrc}->_resolve_collapse (
231 [ @$rel_chain, $rel ],
232 { underdefined => 1 }
234 push @candidates, $rel_collapse->{-node_id};
238 # get the set with least amount of columns
239 # FIXME - maybe need to implement a data type order as well (i.e. prefer several ints
240 # to a single varchar)
242 ($collapse_map->{-node_id}) = sort { scalar @$a <=> scalar @$b } (@candidates);
246 # Still dont know how to collapse - see if the parent passed us anything
247 # (i.e. reuse collapser over 1:1)
248 unless ($collapse_map->{-node_id}) {
249 $collapse_map->{-node_id} = $parent_info->{collapse_on}
250 if $parent_info->{collapser_reusable};
253 # stop descending into children if we were called by a parent for first-pass
254 # and don't despair if nothing was found (there may be other parallel branches
256 if ($parent_info->{underdefined}) {
257 return $collapse_map->{-node_id} ? $collapse_map : undef
259 # nothing down the chain resolved - can't calculate a collapse-map
260 elsif (! $collapse_map->{-node_id}) {
261 $self->throw_exception ( sprintf
262 "Unable to calculate a definitive collapse column set for %s%s: fetch more unique non-nullable columns",
265 ? sprintf (' (last member of the %s chain)', join ' -> ', @$rel_chain )
271 # If we got that far - we are collapsable - GREAT! Now go down all children
272 # a second time, and fill in the rest
274 $collapse_map->{-is_optional} = 1 if $parent_info->{is_optional};
275 $collapse_map->{-node_index} = ${ $node_idx_ref ||= \do { my $x = 1 } }++; # this is *deliberately* not 0-based
277 my (@id_sets, $multis_in_chain);
278 for my $rel (sort keys %$relinfo) {
280 $collapse_map->{$rel} = $relinfo->{$rel}{rsrc}->_resolve_collapse (
281 { map { $_ => 1 } ( keys %{$rel_cols->{$rel}} ) },
285 [ @$rel_chain, $rel],
288 collapse_on => [ @{$collapse_map->{-node_id}} ],
290 rel_condition => $relinfo->{$rel}{fk_map},
292 is_optional => $collapse_map->{-is_optional},
294 # if this is a 1:1 our own collapser can be used as a collapse-map
295 # (regardless of left or not)
296 collapser_reusable => $relinfo->{$rel}{is_single},
302 $collapse_map->{$rel}{-is_single} = 1 if $relinfo->{$rel}{is_single};
303 $collapse_map->{$rel}{-is_optional} ||= 1 unless $relinfo->{$rel}{is_inner};
304 push @id_sets, @{ $collapse_map->{$rel}{-branch_id} };
307 $collapse_map->{-branch_id} = __unique_numlist( @id_sets, @{$collapse_map->{-node_id}} );
309 return $collapse_map;
312 sub _unique_column_set {
313 my ($self, $cols) = @_;
315 my %unique = $self->unique_constraints;
317 # always prefer the PK first, and then shortest constraints first
319 for my $set (delete $unique{primary}, sort { @$a <=> @$b } (values %unique) ) {
320 next unless $set && @$set;
323 next USET unless ($cols->{$_} && $cols->{$_}{colinfo} && !$cols->{$_}{colinfo}{is_nullable} );
326 return { map { $_ => 1 } @$set };
332 # Takes an arrayref of {as} dbic column aliases and the collapse and select
333 # attributes from the same $rs (the slector requirement is a temporary
334 # workaround), and returns a coderef capable of:
335 # my $me_pref_clps = $coderef->([$rs->cursor->next])
336 # Where the $me_pref_clps arrayref is the future argument to
337 # ::ResultSet::_collapse_result.
339 # $me_pref_clps->[0] is always returned (even if as an empty hash with no
340 # rowdata), however branches of related data in $me_pref_clps->[1] may be
341 # pruned short of what was originally requested based on {as}, depending
344 # * If collapse is requested, a definitive collapse map is calculated for
345 # every relationship "fold-point", consisting of a set of values (which
346 # may not even be contained in the future 'me' of said relationship
347 # (for example a cd.artist_id defines the related inner-joined artist)).
348 # Thus a definedness check is carried on all collapse-condition values
349 # and if at least one is undef it is assumed that we are dealing with a
350 # NULLed right-side of a left-join, so we don't return a related data
351 # container at all, which implies no related objects
353 # * If we are not collapsing, there is no constraint on having a selector
354 # uniquely identifying all possible objects, and the user might have very
355 # well requested a column that just *happens* to be all NULLs. What we do
356 # in this case is fallback to the old behavior (which is a potential FIXME)
357 # by always returning a data container, but only filling it with columns
358 # IFF at least one of them is defined. This way we do not get an object
359 # with a bunch of has_column_loaded to undef, but at the same time do not
360 # further relationships based off this "null" object (e.g. in case the user
361 # deliberately skipped link-table values). I am pretty sure there are some
362 # tests that codify this behavior, need to find the exact testname.
364 # For an example of this coderef in action (and to see its guts) look at
365 # t/prefetch/_internals.t
367 # This is a huge performance win, as we call the same code for
368 # every row returned from the db, thus avoiding repeated method
369 # lookups when traversing relationships
371 # Also since the coderef is completely stateless (the returned structure is
372 # always fresh on every new invocation) this is a very good opportunity for
373 # memoization if further speed improvements are needed
375 # The way we construct this coderef is somewhat fugly, although I am not
376 # sure if the string eval is *that* bad of an idea. The alternative is to
377 # have a *very* large number of anon coderefs calling each other in a twisty
378 # maze, whereas the current result is a nice, smooth, single-pass function.
379 # In any case - the output of this thing is meticulously micro-tested, so
380 # any sort of rewrite should be relatively easy
383 my ($self, $args) = @_;
385 my $inflate_index = { map
386 { $args->{inflate_map}[$_] => $_ }
387 ( 0 .. $#{$args->{inflate_map}} )
391 if ($args->{collapse}) {
392 # FIXME - deal with unorderedness
393 # unordered => $unordered
395 my $collapse_map = $self->_resolve_collapse (
397 # only consider real columns (not functions) during collapse resolution
398 # this check shouldn't really be here, as fucktards are not supposed to
399 # alias random crap to existing column names anyway, but still - just in
401 # FIXME !!!! - this does not yet deal with unbalanced selectors correctly
402 # (it is now trivial as the attrs specify where things go out of sync)
404 { ref $args->{selection}[$inflate_index->{$_}] ? () : ( $_ => $inflate_index->{$_} ) }
409 my $unrolled_top_branch_id_indexes = join (', ', @{$collapse_map->{-branch_id}});
411 my ($sequenced_top_branch_id, $sequenced_top_node_id) = map
412 { join ('', map { "{'\xFF__IDVALPOS__${_}__\xFF'}" } @$_ ) }
413 $collapse_map->{-branch_id}, $collapse_map->{-node_id}
416 my $rolled_out_assemblers = __visit_infmap_collapse (
417 $inflate_index, $collapse_map
420 $unrolled_top_branch_id_indexes,
421 $sequenced_top_branch_id,
422 $sequenced_top_node_id,
423 $rolled_out_assemblers,
426 $parser_src = sprintf (<<'EOS', @sprintf_args);
428 ### BEGIN STRING EVAL
429 my ($rows_pos, $result_pos, $cur_row, @cur_row_id_values, $is_new_res, @collapse_idx) = (0,0);
431 # this loop is a bit arcane - the rationale is that the passed in
432 # $_[0] will either have only one row (->next) or will have all
433 # rows already pulled in (->all and/or unordered). Given that the
434 # result can be rather large - we reuse the same already allocated
435 # array, since the collapsed prefetch is smaller by definition.
436 # At the end we cut the leftovers away and move on.
438 ( ( $rows_pos >= 0 and $_[0][$rows_pos++] ) or do { $rows_pos = -1; 0 } )
440 ($_[1] and $_[1]->())
444 # optimize this away when we know we have no undefs in the collapse map
445 $cur_row_id_values[$_] = defined $cur_row->[$_] ? $cur_row->[$_] : "\xFF\xFFN\xFFU\xFFL\xFFL\xFF\xFF"
446 for (%1$s); # the top branch_id includes all id values
448 # check top branch for doubling via a has_many non-selecting join or something
449 # 0 is reserved for this (node indexes start from 1)
450 next if $collapse_idx[0]%2$s++;
452 $is_new_res = ! $collapse_idx[1]%3$s;
455 # fire on ordered only
456 # if ($is_new_res = ! $collapse_idx[1]{$cur_row_id_values[2]}) {
461 $_[0][$result_pos++] = $collapse_idx[1]%3$s
465 splice @{$_[0]}, $result_pos; # truncate the passed in array for cases of collapsing ->all()
469 # change the quoted placeholders to unquoted alias-references
470 $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$cur_row->[$1]"/gex;
471 $parser_src =~ s/ \' \xFF__IDVALPOS__(\d+)__\xFF \' /"\$cur_row_id_values[$1]"/gex;
475 $parser_src = sprintf('$_ = %s for @{$_[0]}', __visit_infmap_simple(
476 $inflate_index, { rsrc => $self }), # need the $rsrc to determine left-ness
479 # change the quoted placeholders to unquoted alias-references
480 # !!! note - different var than the one above
481 $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$_->[$1]"/gex;
487 sub __visit_infmap_simple {
488 my ($val_idx, $args) = @_;
492 for (keys %$val_idx) {
493 if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
494 $rel_cols->{$1}{$2} = $val_idx->{$_};
497 $my_cols->{$_} = $val_idx->{$_};
501 for my $rel (sort keys %$rel_cols) {
503 my $rel_rsrc = __get_related_source($args->{rsrc}, $rel, $rel_cols->{$rel});
505 #my $optional = $args->{is_optional};
506 #$optional ||= ($args->{rsrc}->relationship_info($rel)->{attrs}{join_type} || '') =~ /^left/i;
508 push @relperl, join ' => ', perlstring($rel), __visit_infmap_simple($rel_cols->{$rel}, {
510 #is_optional => $optional,
514 # FIXME SUBOPTIMAL - disabled to satisfy t/resultset/inflate_result_api.t
515 #if ($optional and my @branch_null_checks = map
516 # { "(! defined '\xFF__VALPOS__${_}__\xFF')" }
517 # sort { $a <=> $b } values %{$rel_cols->{$rel}}
519 # $relperl[-1] = sprintf ( '(%s) ? ( %s => [] ) : ( %s )',
520 # join (' && ', @branch_null_checks ),
527 my $me_struct = keys %$my_cols
528 ? __visit_dump({ map { $_ => "\xFF__VALPOS__$my_cols->{$_}__\xFF" } (keys %$my_cols) })
532 return sprintf '[%s]', join (',',
534 @relperl ? sprintf ('{ %s }', join (',', @relperl)) : (),
538 sub __visit_infmap_collapse {
540 my ($val_idx, $collapse_map, $parent_info) = @_;
544 for (keys %$val_idx) {
545 if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
546 $rel_cols->{$1}{$2} = $val_idx->{$_};
549 $my_cols->{$_} = $val_idx->{$_};
553 my $sequenced_node_id = join ('', map
554 { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
555 @{$collapse_map->{-node_id}}
558 my $me_struct = keys %$my_cols
559 ? __visit_dump([{ map { $_ => "\xFF__VALPOS__$my_cols->{$_}__\xFF" } (keys %$my_cols) }])
562 my $node_idx_ref = sprintf '$collapse_idx[%d]%s', $collapse_map->{-node_index}, $sequenced_node_id;
564 my $parent_idx_ref = sprintf( '$collapse_idx[%d]%s[1]{%s}',
565 @{$parent_info}{qw/node_idx sequenced_node_id/},
566 perlstring($parent_info->{relname}),
570 if ($collapse_map->{-node_index} == 1) {
571 push @src, sprintf( '%s ||= %s;',
576 elsif ($collapse_map->{-is_single}) {
577 push @src, sprintf ( '%s = %s ||= %s;',
584 push @src, sprintf('push @{%s}, %s = %s if !%s;',
592 #my $known_defined = { %{ $parent_info->{known_defined} || {} } };
593 #$known_defined->{$_}++ for @{$collapse_map->{-node_id}};
595 for my $rel (sort keys %$rel_cols) {
597 push @src, sprintf( '%s[1]{%s} ||= [];', $node_idx_ref, perlstring($rel) );
599 push @src, __visit_infmap_collapse($rel_cols->{$rel}, $collapse_map->{$rel}, {
600 node_idx => $collapse_map->{-node_index},
601 sequenced_node_id => $sequenced_node_id,
603 #known_defined => $known_defined,
606 # FIXME SUBOPTIMAL - disabled to satisfy t/resultset/inflate_result_api.t
607 #if ($collapse_map->{$rel}{-is_optional} and my @null_checks = map
608 # { "(! defined '\xFF__VALPOS__${_}__\xFF')" }
609 # sort { $a <=> $b } grep
610 # { ! $known_defined->{$_} }
611 # @{$collapse_map->{$rel}{-node_id}}
613 # $src[-1] = sprintf( '(%s) or %s',
614 # join (' || ', @null_checks ),
623 # adding a dep on MoreUtils *just* for this is retarded
624 sub __unique_numlist {
625 [ sort { $a <=> $b } keys %{ {map { $_ => 1 } @_ }} ]
628 # This error must be thrown from two distinct codepaths, joining them is
629 # rather hard. Go for this hack instead.
630 sub __get_related_source {
631 my ($rsrc, $rel, $relcols) = @_;
633 $rsrc->related_source ($rel)
635 $rsrc->throw_exception(sprintf(
636 "Can't inflate prefetch into non-existent relationship '%s' from '%s', "
637 . "check the inflation specification (columns/as) ending in '...%s.%s'.",
641 (sort { length($a) <=> length ($b) } keys %$relcols)[0],
645 # keep our own DD object around so we don't have to fitz with quoting
648 # we actually will be producing functional perl code here,
649 # thus no second-guessing of what these globals might have
650 # been set to. DO NOT CHANGE!
651 ($dumper_obj ||= do {
652 require Data::Dumper;
653 Data::Dumper->new([])
663 ->Indent (0) # faster but harder to read, perhaps leave at 1 ?
664 })->Values ([$_[0]])->Dump;