From: Matt S Trout Date: Fri, 25 May 2007 16:00:52 +0000 (+0000) Subject: prefetch/cache fixes for all but find X-Git-Tag: v0.08010~150^2~53 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9809a6df7707e9d5139274e28f4bcfb93184288c;p=dbsrgits%2FDBIx-Class.git prefetch/cache fixes for all but find --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index fcba759..64d71f9 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -1622,18 +1622,32 @@ sub related_resultset { my %attrs = %{$self->{attrs}||{}}; delete $attrs{result_class}; - $self->_source_handle->schema->resultset($rel_obj->{class})->search_rs( - undef, { - %attrs, - join => undef, - prefetch => undef, - select => undef, - as => undef, - alias => $alias, - where => $self->{cond}, - seen_join => $seen, - from => $from, - }); + my $new_cache; + + if (my $cache = $self->get_cache) { + if ($cache->[0] && $cache->[0]->related_resultset($rel)->get_cache) { + $new_cache = [ map { @{$_->related_resultset($rel)->get_cache} } + @$cache ]; + } + } + + my $new = $self->_source_handle + ->schema + ->resultset($rel_obj->{class}) + ->search_rs( + undef, { + %attrs, + join => undef, + prefetch => undef, + select => undef, + as => undef, + alias => $alias, + where => $self->{cond}, + seen_join => $seen, + from => $from, + }); + $new->set_cache($new_cache) if $new_cache; + $new; }; } diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index f051629..2165801 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -448,6 +448,7 @@ sub inflate_result { $fetched = $pre_source->result_class->inflate_result( $pre_source, @{$pre_val}); } + $new->related_resultset($pre)->set_cache([ $fetched ]); my $accessor = $source->relationship_info($pre)->{attrs}{accessor}; $class->throw_exception("No accessor for prefetched $pre") unless defined $accessor; diff --git a/t/76joins.t b/t/76joins.t index 2f473a9..1e43b56 100644 --- a/t/76joins.t +++ b/t/76joins.t @@ -169,7 +169,7 @@ is(Dumper($attr), $attr_str, 'Attribute hash untouched after search()'); cmp_ok($rs + 0, '==', 3, 'Correct number of records returned'); my $queries = 0; -$schema->storage->debugcb(sub { $queries++ }); +$schema->storage->debugcb(sub { $queries++; }); $schema->storage->debug(1); my @cd = $rs->all; @@ -226,11 +226,7 @@ is($tag->search_related('cd')->search_related('artist')->first->name, 'Caterwauler McCrae', 'chained belongs_to->belongs_to search_related ok'); -TODO: { - local $TODO = 'use prefetched values for nested search_related'; - - is($queries, 0, 'chained search_related after belontgs_to->belongs_to prefetch ran no queries'); -} +is($queries, 0, 'chained search_related after belontgs_to->belongs_to prefetch ran no queries'); $queries = 0; @@ -242,6 +238,8 @@ is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding co $queries = 0; +$schema->storage->debugcb(sub { $queries++; warn "Q: @_"; }); + $cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' } }); is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok'); @@ -258,11 +256,7 @@ my $producers = $cd->search_related('cd_to_producer')->search_related('producer' is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok'); -TODO: { - local $TODO = 'use prefetched values for search_related'; - - is($queries, 0, 'chained search_related after many_to_many prefetch ran no queries'); -} +is($queries, 0, 'chained search_related after many_to_many prefetch ran no queries'); $schema->storage->debug($orig_debug); $schema->storage->debugobj->callback(undef); @@ -416,7 +410,8 @@ my $art_rs_pr = $art_rs->search( {}, { join => [ { cds => ['tracks'] } ], - prefetch => [ { cds => ['tracks'] } ] + prefetch => [ { cds => ['tracks'] } ], + cache => 1 # last test needs this } ); @@ -461,8 +456,4 @@ is($art_rs_pr->search_related('cds')->search_related('tracks')->first->title, 'chained has_many->has_many search_related ok' ); -TODO: { - local $TODO ='use prefetched values for chained search_related'; - - is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries'); -} +is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries');