From: Luke Saunders Date: Wed, 7 Jun 2006 16:30:49 +0000 (+0000) Subject: prefetch bugfix work-in-progress X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d0609d74480d3526ad3638a8e923f108f3fd4514;p=dbsrgits%2FDBIx-Class-Historic.git prefetch bugfix work-in-progress --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index db246df..df1f51c 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -11,7 +11,7 @@ use Data::Page; use Storable; use Data::Dumper; use Scalar::Util qw/weaken/; - +use Data::Dumper; use DBIx::Class::ResultSetColumn; use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/AccessorGroup/); @@ -741,7 +741,6 @@ sub _resolve { my @asadds = (ref($asadds) eq 'ARRAY' ? @$asadds : ($asadds)); $attrs->{as} = [ @{ $attrs->{as} }, @asadds ]; } - my $collapse = $attrs->{collapse} || {}; if (my $prefetch = delete $attrs->{prefetch}) { my @pre_order; @@ -755,8 +754,11 @@ sub _resolve { push(@{$attrs->{from}}, $source->resolve_join($p, $attrs->{alias})) unless $seen{$p}; } + #print "res pre: " . Dumper($p, $collapse); my @prefetch = $source->resolve_prefetch( $p, $attrs->{alias}, {}, \@pre_order, $collapse); + + #print "prefetch: " . Dumper(\@prefetch); push(@{$attrs->{select}}, map { $_->[0] } @prefetch); push(@{$attrs->{as}}, map { $_->[1] } @prefetch); } @@ -775,7 +777,7 @@ sub _merge_attr { if (exists $a->{$key}) { $a->{$key} = $self->_merge_attr($a->{$key}, $b->{$key}, $is_prefetch); } else { - $a->{$key} = delete $b->{$key}; + $a->{$key} = $b->{$key}; } } return $a; @@ -829,6 +831,7 @@ sub _construct_object { my ($self, @row) = @_; my @as = @{ $self->{_attrs}{as} }; + #print "row in: " . Dumper(\@row); my $info = $self->_collapse_result(\@as, \@row); my $new = $self->result_class->inflate_result($self->result_source, @$info); $new = $self->{_attrs}{record_filter}->($new) @@ -870,8 +873,9 @@ sub _collapse_result { $info->[0] = $const{$key}; } } - my @collapse; + + #print "collapse: " . Dumper($self->{_attrs}->{collapse}); if (defined $prefix) { @collapse = map { m/^\Q${prefix}.\E(.+)$/ ? ($1) : () @@ -888,9 +892,11 @@ sub _collapse_result { } my $c_prefix = (defined($prefix) ? "${prefix}.${c}" : $c); my @co_key = @{$self->{_attrs}->{collapse}{$c_prefix}}; - my %co_check = map { ($_, $target->[0]->{$_}); } @co_key; my $tree = $self->_collapse_result($as, $row, $c_prefix); + my %co_check = map { ($_, $tree->[0]->{$_}); } @co_key; my (@final, @raw); + + #print "les free: " . Dumper($tree->[0], \%co_check, \@co_key); while ( !(grep { !defined($tree->[0]->{$_}) || $co_check{$_} ne $tree->[0]->{$_} @@ -903,6 +909,15 @@ sub _collapse_result { @$target = (@final ? @final : [ {}, {} ]); # single empty result to indicate an empty prefetched has_many } + + # get prefetch tree back to result_source level + # $self could be a related resultset + #if ($self->{attrs}->{_live_join_stack}) { + # foreach (@{$self->{attrs}->{_live_join_stack}}) { + # $info->[1] = $info->[1]->{$_}->[1] if(exists $info->[1]->{$_}); + #} + #} + #print "final info: " . Dumper($info); return $info; } diff --git a/t/90join_torture.t b/t/90join_torture.t index b09e9f8..eee1e21 100644 --- a/t/90join_torture.t +++ b/t/90join_torture.t @@ -58,4 +58,18 @@ is($related_rs->first->trackid, '5', 'search related on search related okay'); my $title = $schema->resultset("Artist")->search_related('twokeys')->search_related('cd')->search({'tracks.position' => '2'}, {join => 'tracks', order_by => 'tracks.trackid'})->next->title; is($title, 'Forkful of bees', 'search relateds with order by okay'); +# my $prod_rs = $schema->resultset("CD")->find(1)->other_producers; +# my $prod_rs2 = $prod_rs->search({ name => 'Matt S Trout' }, { prefetch => 'producer_to_cd', order_by => 'other_producer.name' }); +# my $prod_first = $prod_rs2->first; +# warn $prod_first->name; + +my $prod_map_rs = $schema->resultset("Artist")->find(1)->cds->search_related('cd_to_producer', {}, { join => 'producer', prefetch => 'producer' }); +use Data::Dumper; +warn $prod_map_rs->count; +print Dumper($prod_map_rs->{attrs}); +print Dumper($prod_map_rs->{_attrs}); +$prod_map_rs->first; + + + 1; diff --git a/t/lib/DBICTest/Schema/CD.pm b/t/lib/DBICTest/Schema/CD.pm index 7ba727c..d3ad8e8 100644 --- a/t/lib/DBICTest/Schema/CD.pm +++ b/t/lib/DBICTest/Schema/CD.pm @@ -40,6 +40,7 @@ __PACKAGE__->might_have( { proxy => [ qw/notes/ ] }, ); __PACKAGE__->many_to_many( producers => cd_to_producer => 'producer' ); +__PACKAGE__->many_to_many( other_producers => cd_to_producer => 'other_producer' ); __PACKAGE__->many_to_many( producers_sorted => cd_to_producer => 'producer', { order_by => 'producer.name' }, diff --git a/t/lib/DBICTest/Schema/CD_to_Producer.pm b/t/lib/DBICTest/Schema/CD_to_Producer.pm index 117a590..b3a2b18 100644 --- a/t/lib/DBICTest/Schema/CD_to_Producer.pm +++ b/t/lib/DBICTest/Schema/CD_to_Producer.pm @@ -20,4 +20,9 @@ __PACKAGE__->belongs_to( { 'foreign.producerid' => 'self.producer' } ); +__PACKAGE__->belongs_to( + 'other_producer', 'DBICTest::Schema::Producer', + { 'foreign.producerid' => 'self.producer' } +); + 1; diff --git a/t/lib/DBICTest/Schema/Producer.pm b/t/lib/DBICTest/Schema/Producer.pm index 036f9f2..4deee65 100644 --- a/t/lib/DBICTest/Schema/Producer.pm +++ b/t/lib/DBICTest/Schema/Producer.pm @@ -17,4 +17,9 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key('producerid'); __PACKAGE__->add_unique_constraint(prod_name => [ qw/name/ ]); +__PACKAGE__->has_many( + producer_to_cd => 'DBICTest::Schema::CD_to_Producer' => 'producer' +); + + 1;