10 my $schema = DBICTest->init_schema();
13 my $debugcb = sub { $queries++; };
14 my $orig_debug = $schema->storage->debug;
17 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
23 order_by => 'me.trackid',
28 my $use_prefetch = $no_prefetch->search(
35 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
37 scalar ($use_prefetch->all),
38 scalar ($no_prefetch->all),
39 "Amount of returned rows is right"
42 }, 'search_related prefetch with order_by works');
45 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
56 my $use_prefetch = $no_prefetch->search(
64 scalar ($use_prefetch->all),
65 scalar ($no_prefetch->all),
66 "Amount of returned rows is right"
68 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
70 }, 'search_related prefetch with condition referencing unqualified column of a joined table works');
72 # make sure chains off prefetched results still work
74 my $cd = $schema->resultset('CD')->search({}, { prefetch => 'cd_to_producer' })->find(1);
77 $schema->storage->debugcb ($debugcb);
78 $schema->storage->debug (1);
80 is( $cd->cd_to_producer->count, 3 ,'Count of prefetched m2m links via accessor' );
81 is( scalar $cd->cd_to_producer->all, 3, 'Amount of prefetched m2m link objects via accessor' );
82 is( $cd->search_related('cd_to_producer')->count, 3, 'Count of prefetched m2m links via search_related' );
83 is( scalar $cd->search_related('cd_to_producer')->all, 3, 'Amount of prefetched m2m links via search_related' );
85 is($queries, 0, 'No queries ran so far');
87 is( scalar $cd->cd_to_producer->search_related('producer')->all, 3,
88 'Amount of objects via search_related off prefetched linker' );
89 is( $cd->cd_to_producer->search_related('producer')->count, 3,
90 'Count via search_related off prefetched linker' );
91 is( scalar $cd->search_related('cd_to_producer')->search_related('producer')->all, 3,
92 'Amount of objects via chained search_related off prefetched linker' );
93 is( $cd->search_related('cd_to_producer')->search_related('producer')->count, 3,
94 'Count via chained search_related off prefetched linker' );
95 is( scalar $cd->producers->all, 3,
96 'Amount of objects via m2m accessor' );
97 is( $cd->producers->count, 3,
98 'Count via m2m accessor' );
102 is( $cd->cd_to_producer->count, 3 ,'Review count of prefetched m2m links via accessor' );
103 is( scalar $cd->cd_to_producer->all, 3, 'Review amount of prefetched m2m link objects via accessor' );
104 is( $cd->search_related('cd_to_producer')->count, 3, 'Review count of prefetched m2m links via search_related' );
105 is( scalar $cd->search_related('cd_to_producer')->all, 3, 'Rreview amount of prefetched m2m links via search_related' );
107 is($queries, 0, 'Still no queries on prefetched linker');
108 $schema->storage->debugcb (undef);
109 $schema->storage->debug ($orig_debug);
112 # tests with distinct => 1
114 my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
115 ->search_related('artwork_to_artist')->search_related('artist',
117 { prefetch => 'cds' },
119 is($rs->all, 0, 'prefetch without WHERE (objects)');
120 is($rs->count, 0, 'prefetch without WHERE (count)');
122 $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
123 ->search_related('artwork_to_artist')->search_related('artist',
124 { 'cds.title' => 'foo' },
125 { prefetch => 'cds' },
127 is($rs->all, 0, 'prefetch with WHERE (objects)');
128 is($rs->count, 0, 'prefetch with WHERE (count)');
131 # test where conditions at the root of the related chain
132 my $artist_rs = $schema->resultset("Artist")->search({artistid => 2});
133 my $artist = $artist_rs->next;
134 $artist->create_related ('cds', $_) for (
136 year => 1999, title => 'vague cd', genre => { name => 'vague genre' }
139 year => 1999, title => 'vague cd2', genre => { name => 'vague genre' }
143 $rs = $artist_rs->search_related('cds')->search_related('genre',
144 { 'genre.name' => 'vague genre' },
145 { prefetch => 'cds' },
147 is($rs->all, 1, 'base without distinct (objects)');
148 is($rs->count, 1, 'base without distinct (count)');
149 # artist -> 2 cds -> 2 genres -> 2 cds for each genre = 4
150 is($rs->search_related('cds')->all, 4, 'prefetch without distinct (objects)');
151 is($rs->search_related('cds')->count, 4, 'prefetch without distinct (count)');
154 $rs = $artist_rs->search_related('cds', {}, { distinct => 1})->search_related('genre',
155 { 'genre.name' => 'vague genre' },
157 is($rs->all, 2, 'distinct does not propagate over search_related (objects)');
158 is($rs->count, 2, 'distinct does not propagate over search_related (count)');
160 $rs = $rs->search ({}, { distinct => 1} );
161 is($rs->all, 1, 'distinct without prefetch (objects)');
162 is($rs->count, 1, 'distinct without prefetch (count)');
165 $rs = $artist_rs->search_related('cds')->search_related('genre',
166 { 'genre.name' => 'vague genre' },
167 { prefetch => 'cds', distinct => 1 },
169 is($rs->all, 1, 'distinct with prefetch (objects)');
170 is($rs->count, 1, 'distinct with prefetch (count)');
173 $schema->storage->debugcb ($debugcb);
174 $schema->storage->debug (1);
176 # artist -> 2 cds -> 2 genres -> 2 cds for each genre + distinct = 2
177 is($rs->search_related('cds')->all, 2, 'prefetched distinct with prefetch (objects)');
178 is($rs->search_related('cds')->count, 2, 'prefetched distinct with prefetch (count)');
181 local $TODO = "This makes another 2 trips to the database, it can't be right";
182 is ($queries, 0, 'No extra queries fired (prefetch survives search_related)');
185 $schema->storage->debugcb (undef);
186 $schema->storage->debug ($orig_debug);
187 }, 'distinct generally works with prefetch on deep search_related chains');
189 # pathological "user knows what they're doing" case
190 # lifted from production somewhere
192 $schema->resultset('CD')
193 ->search({ cdid => [1,2] })
194 ->search_related('tracks', { position => [3,1] })
197 my $rs = $schema->resultset('CD')->search_related('tracks', {}, {
198 group_by => 'me.title',
199 columns => { title => 'me.title', max_trk => \ 'MAX(tracks.position)' },
205 { title => "Caterwaulin' Blues", max_trk => 3 },
206 { title => "Come Be Depressed With Us", max_trk => 3 },
207 { title => "Forkful of bees", max_trk => 1 },
208 { title => "Generic Manufactured Singles", max_trk => 3 },
209 { title => "Spoonful of bees", max_trk => 1 },