Skip to content

Commit

Permalink
Simplify and rename _resolve_prefetch
Browse files Browse the repository at this point in the history
No functional changes, just logic and arglist cleanup - a couple of them were
no longer used: $order was forgotten in d4d8e97, and the alias-prefix went
unused somewhere around that time as well
  • Loading branch information
ribasushi committed Jun 23, 2015
1 parent b05e745 commit 47dba3e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
6 changes: 3 additions & 3 deletions lib/DBIx/Class/ResultSet.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3725,7 +3725,7 @@ sub _resolved_attrs {
# this is a separate structure (we don't look in {from} directly)
# as the resolver needs to shift things off the lists to work
# properly (identical-prefetches on different branches)
my $join_map = {};
my $joined_node_aliases_map = {};
if (ref $attrs->{from} eq 'ARRAY') {

my $start_depth = $attrs->{seen_join}{-relation_chain_depth} || 0;
Expand All @@ -3737,14 +3737,14 @@ sub _resolved_attrs {

my @jpath = map { keys %$_ } @{$j->[0]{-join_path}};

my $p = $join_map;
my $p = $joined_node_aliases_map;
$p = $p->{$_} ||= {} for @jpath[ ($start_depth/2) .. $#jpath]; #only even depths are actual jpath boundaries
push @{$p->{-join_aliases} }, $j->[0]{-alias};
}
}

( push @{$attrs->{select}}, $_->[0] ) and ( push @{$attrs->{as}}, $_->[1] )
for $source->_resolve_prefetch( $prefetch, $alias, $join_map );
for $source->_resolve_selection_from_prefetch( $prefetch, $joined_node_aliases_map );
}


Expand Down
62 changes: 38 additions & 24 deletions lib/DBIx/Class/ResultSource/RowParser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,73 @@ use DBIx::Class::ResultSource::RowParser::Util qw(
assemble_collapsing_parser
);

use DBIx::Class::Carp;

use namespace::clean;

# Accepts one or more relationships for the current source and returns an
# array of column names for each of those relationships. Column names are
# prefixed relative to the current source, in accordance with where they appear
# in the supplied relationships.
sub _resolve_prefetch {
my ($self, $pre, $alias, $alias_map, $order, $pref_path) = @_;
# Accepts a prefetch map (one or more relationships for the current source),
# returns a set of select/as pairs for each of those relationships. Columns
# are fully qualified inflation_slot names
sub _resolve_selection_from_prefetch {
my ($self, $pre, $alias_map, $pref_path) = @_;

# internal recursion marker
$pref_path ||= [];

if (not defined $pre or not length $pre) {
return ();
}
elsif( ref $pre eq 'ARRAY' ) {
return
map { $self->_resolve_prefetch( $_, $alias, $alias_map, $order, [ @$pref_path ] ) }
@$pre;
map { $self->_resolve_selection_from_prefetch( $_, $alias_map, [ @$pref_path ] ) }
@$pre;
}
elsif( ref $pre eq 'HASH' ) {
my @ret =
map {
$self->_resolve_prefetch($_, $alias, $alias_map, $order, [ @$pref_path ] ),
$self->related_source($_)->_resolve_prefetch(
$pre->{$_}, "${alias}.$_", $alias_map, $order, [ @$pref_path, $_] )
$self->_resolve_selection_from_prefetch($_, $alias_map, [ @$pref_path ] ),
$self->related_source($_)->_resolve_selection_from_prefetch(
$pre->{$_}, $alias_map, [ @$pref_path, $_] )
} keys %$pre;
return @ret;
}
elsif( ref $pre ) {
$self->throw_exception(
"don't know how to resolve prefetch reftype ".ref($pre));
}
else {
my $p = $alias_map;
$p = $p->{$_} for (@$pref_path, $pre);
$p = $p->{$_} for @$pref_path, $pre;

$self->throw_exception (
"Unable to resolve prefetch '$pre' - join alias map does not contain an entry for path: "
. join (' -> ', @$pref_path, $pre)
) if (ref $p->{-join_aliases} ne 'ARRAY' or not @{$p->{-join_aliases}} );

my $as = shift @{$p->{-join_aliases}};

my $rel_info = $self->relationship_info( $pre );
$self->throw_exception( $self->source_name . " has no such relationship '$pre'" )
unless $rel_info;
# this shift() is critical - it is what allows prefetch => [ (foo) x 2 ] to work
my $src_alias = shift @{$p->{-join_aliases}};

# ordered [select => as] pairs
map { [
"${src_alias}.$_" => join ( '.',
@$pref_path,
$pre,
$_,
)
] } $self->related_source($pre)->columns;
}
}

my $as_prefix = ($alias =~ /^.*?\.(.+)$/ ? $1.'.' : '');
sub _resolve_prefetch {
carp_unique(
'There is no good reason to call this internal deprecated method - '
. 'please open a ticket detailing your usage, so that a better plan can '
. 'be devised for your case. In either case _resolve_prefetch() is '
. 'deprecated in favor of _resolve_selection_from_prefetch(), which has '
. 'a greatly simplified arglist.'
);

return map { [ "${as}.$_", "${as_prefix}${pre}.$_", ] }
$self->related_source($pre)->columns;
}
$_[0]->_resolve_selection_from_prefetch( $_[1], $_[3] );
}


# Takes an arrayref of {as} dbic column aliases and the collapse and select
# attributes from the same $rs (the selector requirement is a temporary
# workaround... I hope), and returns a coderef capable of:
Expand Down

0 comments on commit 47dba3e

Please sign in to comment.