From: Daniel Ruoso Date: Tue, 25 May 2010 18:50:55 +0000 (+0000) Subject: First attempt to make extended_rels work. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=cf320fd7d52a1c9d2e7d097e8b69c54db1453bec;p=dbsrgits%2FDBIx-Class-Historic.git First attempt to make extended_rels work. --- diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index 9468e95..fecbbb7 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -1493,7 +1493,7 @@ sub _resolve_join { -alias => $as, -relation_chain_depth => $seen->{-relation_chain_depth} || 0, }, - $self->_resolve_condition($rel_info->{cond}, $as, $alias) ]; + $self->_resolve_condition($rel_info->{cond}, $as, $alias, $join) ]; } } @@ -1551,8 +1551,31 @@ sub resolve_condition { our $UNRESOLVABLE_CONDITION = \'1 = 0'; sub _resolve_condition { - my ($self, $cond, $as, $for) = @_; - if (ref $cond eq 'HASH') { + my ($self, $cond, $as, $for, $rel) = @_; + if (ref $cond eq 'CODE') { + + # heuristic for the actual relname + if (! defined $rel) { + if (!ref $as) { + $rel = $as; + } + elsif (!ref $for) { + $rel = $for; + } + } + + if (! defined $rel) { + $self->throw_exception ('Unable to determine relationship name for condition resolution'); + } + + $cond = $cond->( + $for, + ref $for ? 'me' : $as, + $self, + $rel, + ); + + } elsif (ref $cond eq 'HASH') { my %ret; foreach my $k (keys %{$cond}) { my $v = $cond->{$k}; @@ -1596,7 +1619,7 @@ sub _resolve_condition { } elsif (ref $cond eq 'ARRAY') { return [ map { $self->_resolve_condition($_, $as, $for) } @$cond ]; } else { - die("Can't handle condition $cond yet :("); + $self->throw_exception ("Can't handle condition $cond yet :("); } } diff --git a/lib/DBIx/Class/SQLMaker.pm b/lib/DBIx/Class/SQLMaker.pm index cb9dcd8..287dcc8 100644 --- a/lib/DBIx/Class/SQLMaker.pm +++ b/lib/DBIx/Class/SQLMaker.pm @@ -442,8 +442,8 @@ sub _join_condition { for (keys %$cond) { my $v = $cond->{$_}; if (ref $v) { - croak (ref($v) . qq{ reference arguments are not supported in JOINS - try using \"..." instead'}) - if ref($v) ne 'SCALAR'; + #croak (ref($v) . qq{ reference arguments are not supported in JOINS - try using \"..." instead'}) + # if ref($v) ne 'SCALAR'; $j{$_} = $v; } else { diff --git a/t/lib/DBICTest/Schema/Artist.pm b/t/lib/DBICTest/Schema/Artist.pm index 4f2bde5..af6257c 100644 --- a/t/lib/DBICTest/Schema/Artist.pm +++ b/t/lib/DBICTest/Schema/Artist.pm @@ -49,12 +49,12 @@ __PACKAGE__->has_many( __PACKAGE__->has_many( cds_80s => 'DBICTest::Schema::CD', sub { - my ( $self_alias, $rel_alias, $self_rsrc, $rel_name ) = @_; - return { - "${rel_alias}.artist" => \ "${self_alias}.artistid", - "${rel_alias}.year" => { '>', "1979" }, - "${rel_alias}.year" => { '<', "1990" } - }; + my ( $me, $as, $self_rsrc, $rel ) = @_; + return { + "${as}.artist" => (ref $me ? $me->artistid : { '=' => \"${me}.artistid"}), + "${as}.year" => { '>', "1979", + '<', "1990" } + }; } ); diff --git a/t/lib/DBICTest/Schema/Track.pm b/t/lib/DBICTest/Schema/Track.pm index 10fd396..f9cbcc9 100644 --- a/t/lib/DBICTest/Schema/Track.pm +++ b/t/lib/DBICTest/Schema/Track.pm @@ -67,10 +67,10 @@ __PACKAGE__->might_have ( 'next_track', __PACKAGE__, sub { - my ( $self_alias, $rel_alias, $self_rsrc, $rel_name ) = @_; + my ( $me, $as, $self_rsrc, $rel_name ) = @_; return { - "${self_alias}.cd" => \ "${rel_alias}.cd", - "${self_alias}.position" => { '<', \ "${rel_alias}.position" }, + "${as}.cd" => (ref $me ? $me->cd : { '=' => \"${me}.cd" }), + "${as}.position" => { '>', (ref $me ? $me->position : \"${me}.position" )}, }; }, ); diff --git a/t/relationship/custom.t b/t/relationship/custom.t index e356733..6d4e85b 100644 --- a/t/relationship/custom.t +++ b/t/relationship/custom.t @@ -10,6 +10,7 @@ my $schema = DBICTest->init_schema(); my $artist = $schema->resultset("Artist")->create({ name => 'Michael Jackson' }); + foreach my $year (1975..1985) { $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year }); } @@ -38,7 +39,7 @@ my $last_tracks = $schema->resultset('Track')->search ( is_deeply ( [$last_tracks->get_column ('trackid')->all], - \@last_track_ids, + [ grep { $_ } @last_track_ids ], 'last group-entry via self-join works', );