[Bast-commits] r9560 - in DBIx-Class/0.08/branches/extended_rels: lib/DBIx/Class lib/DBIx/Class/Relationship t/lib/DBICTest/Schema t/relationship

ruoso at dev.catalyst.perl.org ruoso at dev.catalyst.perl.org
Wed Jun 2 20:13:24 GMT 2010


Author: ruoso
Date: 2010-06-02 21:13:24 +0100 (Wed, 02 Jun 2010)
New Revision: 9560

Modified:
   DBIx-Class/0.08/branches/extended_rels/lib/DBIx/Class/Relationship/Base.pm
   DBIx-Class/0.08/branches/extended_rels/lib/DBIx/Class/ResultSource.pm
   DBIx-Class/0.08/branches/extended_rels/t/lib/DBICTest/Schema/Artist.pm
   DBIx-Class/0.08/branches/extended_rels/t/relationship/custom.t
Log:
makes search_related on extended rels without the optimized version work. involves hacking the from attribute

Modified: DBIx-Class/0.08/branches/extended_rels/lib/DBIx/Class/Relationship/Base.pm
===================================================================
--- DBIx-Class/0.08/branches/extended_rels/lib/DBIx/Class/Relationship/Base.pm	2010-06-02 18:11:42 UTC (rev 9559)
+++ DBIx-Class/0.08/branches/extended_rels/lib/DBIx/Class/Relationship/Base.pm	2010-06-02 20:13:24 UTC (rev 9560)
@@ -243,7 +243,7 @@
     # resultsource, in that case, the condition might provide an
     # additional condition in order to avoid an unecessary join if
     # that is at all possible.
-    my ($cond, $cond2) = try {
+    my ($cond, $extended_cond) = try {
       $source->_resolve_condition( $rel_info->{cond}, $rel, $self )
     }
     catch {
@@ -268,6 +268,35 @@
         }
       }
     }
+
+    # this is where we're going to check if we have an extended
+    # rel. In that case, we need to: 1) If there's a second
+    # condition, we use that instead.  2) If there is only one
+    # condition, we need to join the current resultsource and have
+    # additional conditions.
+    if (ref $rel_info->{cond} eq 'CODE') {
+      # this is an extended relationship.
+      if ($extended_cond) {
+        $cond = $extended_cond;
+
+      } else {
+
+        # it's a bit hard to find out what to do with other joins
+        $self->throw_exception('Extended relationship '.$rel.' with additional join requires optimized declaration')
+          if exists $attrs->{join} && $attrs->{join};
+
+        # aliases get a bit more complicated, so we won't accept additional queries
+        $self->throw_exception('Extended relationship '.$rel.' with additional query requires optimized declaration')
+          if $query;
+
+        $attrs->{from} =
+          [ { $rel => $self->result_source->from },
+            [ { 'me' => $self->result_source->related_source($rel)->from }, { 1 => 1 } ] ];
+
+        $cond->{"${rel}.${_}"} = $self->get_column($_) for $self->result_source->primary_columns;
+      }
+    }
+
     if (ref $cond eq 'ARRAY') {
       $cond = [ map {
         if (ref $_ eq 'HASH') {
@@ -282,28 +311,8 @@
         }
       } @$cond ];
     } elsif (ref $cond eq 'HASH') {
-
-      # this is where we're going to check if we have an extended
-      # rel. In that case, we need to: 1) If there's a second
-      # condition, we use that instead.  2) If there is only one
-      # condition, we need to join the current resultsource and have
-      # additional conditions.
-      if (ref $rel_info->{cond} eq 'CODE') {
-        # this is an extended relationship.
-        if ($cond2) {
-          $cond = $cond2;
-        } else {
-          if (exists $attrs->{join} && $attrs->{join}) {
-            # it's a bit hard to find out what to do here.
-            $self->throw_exception('Extended relationship '.$rel.' with additional join not supported');
-          } else {
-            $attrs->{join} = $rel;
-          }
-        }
-      } else {
-        foreach my $key (grep { ! /\./ } keys %$cond) {
-          $cond->{"me.$key"} = delete $cond->{$key};
-        }
+      foreach my $key (grep { ! /\./ } keys %$cond) {
+        $cond->{"me.$key"} = delete $cond->{$key};
       }
     }
 

Modified: DBIx-Class/0.08/branches/extended_rels/lib/DBIx/Class/ResultSource.pm
===================================================================
--- DBIx-Class/0.08/branches/extended_rels/lib/DBIx/Class/ResultSource.pm	2010-06-02 18:11:42 UTC (rev 9559)
+++ DBIx-Class/0.08/branches/extended_rels/lib/DBIx/Class/ResultSource.pm	2010-06-02 20:13:24 UTC (rev 9560)
@@ -1373,7 +1373,11 @@
       $self->throw_exception ('Unable to determine relationship name for condition resolution');
     }
 
-    return $cond->($for, ref $for ? 'me' : $as, $self, $rel, ref $for ? $for : undef);
+    return $cond->(ref $for ? $as : $for,
+                   ref $for ? 'me' : $as,
+                   $self,
+                   $rel,
+                   ref $for ? $for : undef);
 
   } elsif (ref $cond eq 'HASH') {
     my %ret;

Modified: DBIx-Class/0.08/branches/extended_rels/t/lib/DBICTest/Schema/Artist.pm
===================================================================
--- DBIx-Class/0.08/branches/extended_rels/t/lib/DBICTest/Schema/Artist.pm	2010-06-02 18:11:42 UTC (rev 9559)
+++ DBIx-Class/0.08/branches/extended_rels/t/lib/DBICTest/Schema/Artist.pm	2010-06-02 20:13:24 UTC (rev 9560)
@@ -60,7 +60,19 @@
   }
 );
 
+__PACKAGE__->has_many(
+    cds_90s => 'DBICTest::Schema::CD',
+    sub {
+      my ( $me_alias, $rel_alias, $me_result_source, $rel_name, $optional_me_object ) = @_;
+      return
+        ({ "${rel_alias}.artist"  => { '=' => \"${me_alias}.artistid"},
+           "${rel_alias}.year"    => { '>', "1989",
+                                       '<', "2000" }
+         });
+  }
+);
 
+
 __PACKAGE__->has_many(
     cds_unordered => 'DBICTest::Schema::CD'
 );

Modified: DBIx-Class/0.08/branches/extended_rels/t/relationship/custom.t
===================================================================
--- DBIx-Class/0.08/branches/extended_rels/t/relationship/custom.t	2010-06-02 18:11:42 UTC (rev 9559)
+++ DBIx-Class/0.08/branches/extended_rels/t/relationship/custom.t	2010-06-02 20:13:24 UTC (rev 9560)
@@ -10,15 +10,21 @@
 
 
 my $artist = $schema->resultset("Artist")->create({ name => 'Michael Jackson' });
-
 foreach my $year (1975..1985) {
   $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
 }
 
+my $artist2 = $schema->resultset("Artist")->create({ name => 'Chico Buarque' }) ;
+foreach my $year (1975..1995) {
+  $artist2->create_related('cds', { year => $year, title => 'Compilation from ' . $year });
+}
+
 my @cds_80s = $artist->cds_80s;
-
 is(@cds_80s, 6, '6 80s cds found');
 
+my @cds_90s = $artist2->cds_90s;
+is(@cds_90s, 6, '6 90s cds found even with non-optimized search');
+
 map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s;
 
 




More information about the Bast-commits mailing list