10 my $schema = DBICTest->init_schema();
12 my $orig_debug = $schema->storage->debug;
17 eval "use DBD::SQLite";
19 ? ( skip_all => 'needs DBD::SQLite for testing' )
24 $schema->storage->debugcb(sub { $queries++; });
25 $schema->storage->debug(1);
27 my $search = { 'artist.name' => 'Caterwauler McCrae' };
28 my $attr = { prefetch => [ qw/artist liner_notes/ ],
29 order_by => 'me.cdid' };
30 my $search_str = Dumper($search);
31 my $attr_str = Dumper($attr);
33 my $rs = $schema->resultset("CD")->search($search, $attr);
36 is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
38 ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
40 is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
42 is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
44 is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
46 is($queries, 1, 'prefetch ran only 1 select statement');
48 $schema->storage->debug($orig_debug);
49 $schema->storage->debugobj->callback(undef);
51 # test for partial prefetch via columns attr
52 my $cd = $schema->resultset('CD')->find(1,
54 columns => [qw/title artist artist.name/],
55 join => { 'artist' => {} }
58 ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
60 # start test for nested prefetch SELECT count
62 $schema->storage->debugcb(sub { $queries++ });
63 $schema->storage->debug(1);
65 $rs = $schema->resultset('Tag')->search(
68 prefetch => { cd => 'artist' }
74 is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
76 is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
79 #$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
80 is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
84 is($tag->search_related('cd')->search_related('artist')->first->name,
86 'chained belongs_to->belongs_to search_related ok');
88 is($queries, 0, 'chained search_related after belontgs_to->belongs_to prefetch ran no queries');
92 $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
94 is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
96 is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
100 $schema->storage->debugcb(sub { $queries++; });
102 $cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' } });
104 is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok');
106 is($queries, 1, 'many_to_many accessor with nested prefetch ran exactly 1 query');
110 my $producers = $cd->search_related('cd_to_producer')->search_related('producer');
112 is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok');
114 is($queries, 0, 'chained search_related after many_to_many prefetch ran no queries');
116 $schema->storage->debug($orig_debug);
117 $schema->storage->debugobj->callback(undef);
119 $rs = $schema->resultset('Tag')->search(
122 join => { cd => 'artist' },
123 prefetch => { cd => 'artist' }
127 cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
129 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
130 { order_by => 'artistid DESC', join => 'cds' });
132 is($artist->name, 'Random Boy Band', "Join search by object ok");
134 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
135 { join => 'liner_notes' });
137 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
139 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
141 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
142 { join => { 'cds' => 'tags' } });
144 cmp_ok( @artists, '==', 2, "two-join search ok" );
146 $rs = $schema->resultset("CD")->search(
148 { group_by => [qw/ title me.cdid /] }
151 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
153 cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
155 $rs = $schema->resultset("CD")->search(
157 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
160 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
162 $rs = $schema->resultset("Artist")->search(
164 { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } }
167 cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
169 $rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }});
171 cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
173 $rs = $schema->resultset("Artist")->search(
174 { 'cds.title' => 'Spoonful of bees',
175 'cds_2.title' => 'Forkful of bees' },
176 { join => [ 'cds', 'cds' ] });
178 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
180 is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
182 $cd = $schema->resultset('Artist')->first->create_related('cds',
184 title => 'Unproduced Single',
188 my $left_join = $schema->resultset('CD')->search(
189 { 'me.cdid' => $cd->cdid },
190 { prefetch => { cd_to_producer => 'producer' } }
193 cmp_ok($left_join, '==', 1, 'prefetch with no join record present');
196 $schema->storage->debugcb(sub { $queries++ });
197 $schema->storage->debug(1);
200 $schema->resultset('TreeLike')->find(5,
201 { join => { parent => { parent => 'parent' } },
202 prefetch => { parent => { parent => 'parent' } } });
204 is($tree_like->name, 'quux', 'Bottom of tree ok');
205 $tree_like = $tree_like->parent;
206 is($tree_like->name, 'baz', 'First level up ok');
207 $tree_like = $tree_like->parent;
208 is($tree_like->name, 'bar', 'Second level up ok');
209 $tree_like = $tree_like->parent;
210 is($tree_like->name, 'foo', 'Third level up ok');
212 $schema->storage->debug($orig_debug);
213 $schema->storage->debugobj->callback(undef);
215 cmp_ok($queries, '==', 1, 'Only one query run');
217 $tree_like = $schema->resultset('TreeLike')->search({'me.id' => 2});
218 $tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first;
219 is($tree_like->name, 'quux', 'Tree search_related ok');
221 $tree_like = $schema->resultset('TreeLike')->search_related('children',
222 { 'children.id' => 3, 'children_2.id' => 4 },
223 { prefetch => { children => 'children' } }
225 is(eval { $tree_like->children->first->children->first->name }, 'quux',
226 'Tree search_related with prefetch ok');
228 $tree_like = eval { $schema->resultset('TreeLike')->search(
229 { 'children.id' => 3, 'children_2.id' => 6 },
230 { join => [qw/children children/] }
231 )->search_related('children', { 'children_4.id' => 7 }, { prefetch => 'children' }
232 )->first->children->first; };
233 is(eval { $tree_like->name }, 'fong', 'Tree with multiple has_many joins ok');
235 # test that collapsed joins don't get a _2 appended to the alias
238 $schema->storage->debugcb(sub { $sql = $_[1] });
239 $schema->storage->debug(1);
242 my $row = $schema->resultset('Artist')->search_related('cds', undef, {
244 prefetch => 'tracks',
245 })->search_related('tracks')->first;
248 like( $sql, qr/^SELECT tracks_2\.trackid/, "join not collapsed for search_related" );
250 $schema->storage->debug($orig_debug);
251 $schema->storage->debugobj->callback(undef);
253 $rs = $schema->resultset('Artist');
254 $rs->create({ artistid => 4, name => 'Unknown singer-songwriter' });
255 $rs->create({ artistid => 5, name => 'Emo 4ever' });
256 @artists = $rs->search(undef, { prefetch => 'cds', order_by => 'artistid' });
257 is(scalar @artists, 5, 'has_many prefetch with adjacent empty rows ok');
261 # Tests for multilevel has_many prefetch
263 # artist resultsets - with and without prefetch
264 my $art_rs = $schema->resultset('Artist');
265 my $art_rs_pr = $art_rs->search(
268 join => [ { cds => ['tracks'] } ],
269 prefetch => [ { cds => ['tracks'] } ],
270 cache => 1 # last test needs this
274 # This test does the same operation twice - once on a
275 # set of items fetched from the db with no prefetch of has_many rels
276 # The second prefetches 2 levels of has_many
277 # We check things are the same by comparing the name or title
278 # we build everything into a hash structure and compare the one
279 # from each rs to see what differs
281 sub make_hash_struc {
285 foreach my $art ( $rs->all ) {
286 foreach my $cd ( $art->cds ) {
287 foreach my $track ( $cd->tracks ) {
288 $struc->{ $art->name }{ $cd->title }{ $track->title }++;
296 $schema->storage->debugcb(sub { $queries++ });
297 $schema->storage->debug(1);
299 my $prefetch_result = make_hash_struc($art_rs_pr);
301 is($queries, 1, 'nested prefetch across has_many->has_many ran exactly 1 query');
303 my $nonpre_result = make_hash_struc($art_rs);
305 is_deeply( $prefetch_result, $nonpre_result,
306 'Compare 2 level prefetch result to non-prefetch result' );
310 is($art_rs_pr->search_related('cds')->search_related('tracks')->first->title,
312 'chained has_many->has_many search_related ok'
315 is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries');