=cut
sub resolve_join {
- my ($self, $join, $alias) = @_;
+ my ($self, $join, $alias, $seen) = @_;
+ $seen ||= {};
if (ref $join eq 'ARRAY') {
- return map { $self->resolve_join($_, $alias) } @$join;
+ return map { $self->resolve_join($_, $alias, $seen) } @$join;
} elsif (ref $join eq 'HASH') {
- return map { $self->resolve_join($_, $alias),
- $self->related_source($_)->resolve_join($join->{$_}, $_) }
+ return
+ map { $self->resolve_join($_, $alias, $seen),
+ $self->related_source($_)->resolve_join($join->{$_}, $_, $seen) }
keys %$join;
} elsif (ref $join) {
$self->throw_exception("No idea how to resolve join reftype ".ref $join);
} else {
+ my $count = ++$seen->{$join};
+ #use Data::Dumper; warn Dumper($seen);
+ my $as = ($count > 1 ? "${join}_${count}" : $join);
my $rel_info = $self->relationship_info($join);
$self->throw_exception("No such relationship ${join}") unless $rel_info;
my $type = $rel_info->{attrs}{join_type} || '';
- return [ { $join => $self->related_source($join)->from,
+ return [ { $as => $self->related_source($join)->from,
-join_type => $type },
- $self->resolve_condition($rel_info->{cond}, $join, $alias) ];
+ $self->resolve_condition($rel_info->{cond}, $as, $alias) ];
}
}
-=head2 resolve_condition($cond, $rel, $alias|$object)
+=head2 resolve_condition($cond, $as, $alias|$object)
Resolves the passed condition to a concrete query fragment. If given an alias,
returns a join condition; if given an object, inverts that object to produce
=cut
sub resolve_condition {
- my ($self, $cond, $rel, $for) = @_;
+ my ($self, $cond, $as, $for) = @_;
#warn %$cond;
if (ref $cond eq 'HASH') {
my %ret;
$ret{$k} = $for->get_column($v);
#warn %ret;
} else {
- $ret{"${rel}.${k}"} = "${for}.${v}";
+ $ret{"${as}.${k}"} = "${for}.${v}";
}
}
return \%ret;
} elsif (ref $cond eq 'ARRAY') {
- return [ map { $self->resolve_condition($_, $rel, $for) } @$cond ];
+ return [ map { $self->resolve_condition($_, $as, $for) } @$cond ];
} else {
die("Can't handle this yet :(");
}
=cut
sub resolve_prefetch {
- my( $self, $pre, $alias ) = @_;
+ my ($self, $pre, $alias, $seen) = @_;
+ $seen ||= {};
use Data::Dumper;
#$alias ||= $self->name;
#warn $alias, Dumper $pre;
if( ref $pre eq 'ARRAY' ) {
- return map { $self->resolve_prefetch( $_, $alias ) } @$pre;
+ return map { $self->resolve_prefetch( $_, $alias, $seen ) } @$pre;
}
elsif( ref $pre eq 'HASH' ) {
my @ret =
map {
- $self->resolve_prefetch($_, $alias),
- $self->related_source($_)->resolve_prefetch( $pre->{$_}, $_ )
- }
- keys %$pre;
+ $self->resolve_prefetch($_, $alias, $seen),
+ $self->related_source($_)->resolve_prefetch(
+ $pre->{$_}, "${alias}.$_", $seen)
+ } keys %$pre;
#die Dumper \@ret;
return @ret;
}
$self->throw_exception( "don't know how to resolve prefetch reftype " . ref $pre);
}
else {
+ my $count = ++$seen->{$pre};
+ my $as = ($count > 1 ? "${pre}_${count}" : $pre);
my $rel_info = $self->relationship_info( $pre );
$self->throw_exception( $self->name . " has no such relationship '$pre'" ) unless $rel_info;
- my $prefix = $alias && $alias ne 'me' ? "$alias.$pre" : $pre;
- my @ret = map { "$prefix.$_" } $self->related_source($pre)->columns;
+ my $as_prefix = ($alias =~ /^.*?\.(.*)$/ ? $1.'.' : '');
+ return map { [ "${as}.$_", "${as_prefix}${pre}.$_", ] }
+ $self->related_source($pre)->columns;
#warn $alias, Dumper (\@ret);
- return @ret;
+ #return @ret;
}
}
eval "use DBD::SQLite";
plan $@
? ( skip_all => 'needs DBD::SQLite for testing' )
- : ( tests => 33 );
+ : ( tests => 35 );
}
# test the abstract join => SQL generator
cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
cmp_ok( scalar $rs->all, '==', 3, "all() returns same count as count() after group_by on related column" );
-}
+
+$rs = $schema->resultset("Artist")->search(
+ { 'cds.title' => 'Spoonful of bees',
+ 'cds_2.title' => 'Forkful of bees' },
+ { join => [ 'cds', 'cds' ] });
+
+cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
+is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
+
+} # end run_tests
1;