10 my $schema = DBICTest->init_schema();
13 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
19 order_by => 'me.trackid',
24 my $use_prefetch = $no_prefetch->search(
31 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
33 scalar ($use_prefetch->all),
34 scalar ($no_prefetch->all),
35 "Amount of returned rows is right"
38 }, 'search_related prefetch with order_by works');
41 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
52 my $use_prefetch = $no_prefetch->search(
60 scalar ($use_prefetch->all),
61 scalar ($no_prefetch->all),
62 "Amount of returned rows is right"
64 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
66 }, 'search_related prefetch with condition referencing unqualified column of a joined table works');
68 # make sure chains off prefetched results still work
70 my $cd = $schema->resultset('CD')->search({}, { prefetch => 'cd_to_producer' })->find(1);
72 $schema->is_executed_querycount( sub {
73 is( $cd->cd_to_producer->count, 3 ,'Count of prefetched m2m links via accessor' );
74 is( scalar $cd->cd_to_producer->all, 3, 'Amount of prefetched m2m link objects via accessor' );
75 is( $cd->search_related('cd_to_producer')->count, 3, 'Count of prefetched m2m links via search_related' );
76 is( scalar $cd->search_related('cd_to_producer')->all, 3, 'Amount of prefetched m2m links via search_related' );
77 }, 0, 'No queries ran so far');
79 is( scalar $cd->cd_to_producer->search_related('producer')->all, 3,
80 'Amount of objects via search_related off prefetched linker' );
81 is( $cd->cd_to_producer->search_related('producer')->count, 3,
82 'Count via search_related off prefetched linker' );
83 is( scalar $cd->search_related('cd_to_producer')->search_related('producer')->all, 3,
84 'Amount of objects via chained search_related off prefetched linker' );
85 is( $cd->search_related('cd_to_producer')->search_related('producer')->count, 3,
86 'Count via chained search_related off prefetched linker' );
87 is( scalar $cd->producers->all, 3,
88 'Amount of objects via m2m accessor' );
89 is( $cd->producers->count, 3,
90 'Count via m2m accessor' );
92 $schema->is_executed_querycount( sub {
93 is( $cd->cd_to_producer->count, 3 ,'Review count of prefetched m2m links via accessor' );
94 is( scalar $cd->cd_to_producer->all, 3, 'Review amount of prefetched m2m link objects via accessor' );
95 is( $cd->search_related('cd_to_producer')->count, 3, 'Review count of prefetched m2m links via search_related' );
96 is( scalar $cd->search_related('cd_to_producer')->all, 3, 'Rreview amount of prefetched m2m links via search_related' );
97 }, 0, 'Still no queries on prefetched linker');
100 # tests with distinct => 1
102 my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
103 ->search_related('artwork_to_artist')->search_related('artist',
105 { prefetch => 'cds' },
107 is($rs->all, 0, 'prefetch without WHERE (objects)');
108 is($rs->count, 0, 'prefetch without WHERE (count)');
110 $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
111 ->search_related('artwork_to_artist')->search_related('artist',
112 { 'cds.title' => 'foo' },
113 { prefetch => 'cds' },
115 is($rs->all, 0, 'prefetch with WHERE (objects)');
116 is($rs->count, 0, 'prefetch with WHERE (count)');
119 # test where conditions at the root of the related chain
120 my $artist_rs = $schema->resultset("Artist")->search({artistid => 2});
121 my $artist = $artist_rs->next;
122 $artist->create_related ('cds', $_) for (
124 year => 1999, title => 'vague cd', genre => { name => 'vague genre' }
127 year => 1999, title => 'vague cd2', genre => { name => 'vague genre' }
131 $rs = $artist_rs->search_related('cds')->search_related('genre',
132 { 'genre.name' => 'vague genre' },
133 { prefetch => 'cds' },
135 is($rs->all, 1, 'base without distinct (objects)');
136 is($rs->count, 1, 'base without distinct (count)');
137 # artist -> 2 cds -> 2 genres -> 2 cds for each genre = 4
138 is($rs->search_related('cds')->all, 4, 'prefetch without distinct (objects)');
139 is($rs->search_related('cds')->count, 4, 'prefetch without distinct (count)');
142 $rs = $artist_rs->search_related('cds', {}, { distinct => 1})->search_related('genre',
143 { 'genre.name' => 'vague genre' },
145 is($rs->all, 2, 'distinct does not propagate over search_related (objects)');
146 is($rs->count, 2, 'distinct does not propagate over search_related (count)');
148 $rs = $rs->search ({}, { distinct => 1} );
149 is($rs->all, 1, 'distinct without prefetch (objects)');
150 is($rs->count, 1, 'distinct without prefetch (count)');
153 $rs = $artist_rs->search_related('cds')->search_related('genre',
154 { 'genre.name' => 'vague genre' },
155 { prefetch => 'cds', distinct => 1 },
157 is($rs->all, 1, 'distinct with prefetch (objects)');
158 is($rs->count, 1, 'distinct with prefetch (count)');
160 local $TODO = "This makes another 2 trips to the database, it can't be right";
161 $schema->is_executed_querycount( sub {
163 # the is() calls are not todoified
166 # artist -> 2 cds -> 2 genres -> 2 cds for each genre + distinct = 2
167 is($rs->search_related('cds')->all, 2, 'prefetched distinct with prefetch (objects)');
168 is($rs->search_related('cds')->count, 2, 'prefetched distinct with prefetch (count)');
170 }, 0, 'No extra queries fired (prefetch survives search_related)');
172 }, 'distinct generally works with prefetch on deep search_related chains');
174 # pathological "user knows what they're doing" case
175 # lifted from production somewhere
177 $schema->resultset('CD')
178 ->search({ cdid => [1,2] })
179 ->search_related('tracks', { position => [3,1] })
182 my $rs = $schema->resultset('CD')->search_related('tracks', {}, {
183 group_by => 'me.title',
184 columns => { title => 'me.title', max_trk => \ 'MAX(tracks.position)' },
188 $rs->search({}, { order_by => 'me.title' })->all_hri,
190 { title => "Caterwaulin' Blues", max_trk => 3 },
191 { title => "Come Be Depressed With Us", max_trk => 3 },
192 { title => "Forkful of bees", max_trk => 1 },
193 { title => "Generic Manufactured Singles", max_trk => 3 },
194 { title => "Spoonful of bees", max_trk => 1 },