[Bast-commits] r7957 - in DBIx-Class/0.08/trunk: . lib/DBIx/Class t/count

ribasushi at dev.catalyst.perl.org ribasushi at dev.catalyst.perl.org
Thu Nov 26 11:52:06 GMT 2009


Author: ribasushi
Date: 2009-11-26 11:52:05 +0000 (Thu, 26 Nov 2009)
New Revision: 7957

Added:
   DBIx-Class/0.08/trunk/t/count/search_related.t
Modified:
   DBIx-Class/0.08/trunk/Changes
   DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm
Log:
Another count() quirk down

Modified: DBIx-Class/0.08/trunk/Changes
===================================================================
--- DBIx-Class/0.08/trunk/Changes	2009-11-26 11:11:21 UTC (rev 7956)
+++ DBIx-Class/0.08/trunk/Changes	2009-11-26 11:52:05 UTC (rev 7957)
@@ -7,6 +7,7 @@
           dispatcher, and a sybase-specific ::DBI::Sybase::ASE
         - Make sure populate() inherits the resultset conditions just
           like create() does
+        - Fix count/objects from search_related on limited resultset
 
 0.08114 2009-11-14 17:45:00 (UTC)
         - Preliminary support for MSSQL via DBD::ADO

Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm	2009-11-26 11:11:21 UTC (rev 7956)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm	2009-11-26 11:52:05 UTC (rev 7957)
@@ -2619,27 +2619,45 @@
 # The increments happen twice per join. An even number means a
 # relationship specified via a search_related, whereas an odd
 # number indicates a join/prefetch added via attributes
+#
+# Also this code will wrap the current resultset (the one we
+# chain to) in a subselect IFF it contains limiting attributes
 sub _chain_relationship {
   my ($self, $rel) = @_;
   my $source = $self->result_source;
   my $attrs = { %{$self->{attrs}||{}} };
 
-  my $from = [ @{
-      $attrs->{from}
-        ||
-      [{
-        -source_handle => $source->handle,
-        -alias => $attrs->{alias},
-        $attrs->{alias} => $source->from,
-      }]
-  }];
+  my $from;
+  my @force_subq_attrs = qw/offset rows/;
 
+  if (
+    ($attrs->{from} && ref $attrs->{from} ne 'ARRAY')
+      ||
+    $self->_has_resolved_attr (@force_subq_attrs)
+  ) {
+    $from = [{
+      -source_handle => $source->handle,
+      -alias => $attrs->{alias},
+      $attrs->{alias} => $self->as_query,
+    }];
+    delete @{$attrs}{@force_subq_attrs};
+  }
+  elsif ($attrs->{from}) {  #shallow copy suffices
+    $from = [ @{$attrs->{from}} ];
+  }
+  else {
+    $from = [{
+      -source_handle => $source->handle,
+      -alias => $attrs->{alias},
+      $attrs->{alias} => $source->from,
+    }];
+  }
+
   my $seen = { %{$attrs->{seen_join} || {} } };
   my $jpath = ($attrs->{seen_join} && keys %{$attrs->{seen_join}})
     ? $from->[-1][0]{-join_path}
     : [];
 
-
   # we need to take the prefetch the attrs into account before we
   # ->_resolve_join as otherwise they get lost - captainL
   my $merged = $self->_merge_attr( $attrs->{join}, $attrs->{prefetch} );

Added: DBIx-Class/0.08/trunk/t/count/search_related.t
===================================================================
--- DBIx-Class/0.08/trunk/t/count/search_related.t	                        (rev 0)
+++ DBIx-Class/0.08/trunk/t/count/search_related.t	2009-11-26 11:52:05 UTC (rev 7957)
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+
+use Test::More;
+
+use lib qw(t/lib);
+
+use DBICTest;
+
+my $schema = DBICTest->init_schema();
+my $cd_rs = $schema->resultset('CD')->search ({}, { rows => 1, order_by => 'cdid' });
+
+my $track_count = $cd_rs->first->tracks->count;
+
+cmp_ok ($track_count, '>', 1, 'First CD has several tracks');
+
+is ($cd_rs->search_related ('tracks')->count, $track_count, 'related->count returns correct number');
+is (scalar ($cd_rs->search_related ('tracks')->all), $track_count, 'related->all returns correct number of objects');
+
+done_testing;




More information about the Bast-commits mailing list