9 my $schema = DBICTest->init_schema();
10 my $orig_debug = $schema->storage->debug;
15 $schema->storage->debugcb(sub { $queries++; });
16 $schema->storage->debug(1);
18 my $search = { 'artist.name' => 'Caterwauler McCrae' };
19 my $attr = { prefetch => [ qw/artist liner_notes/ ],
20 order_by => 'me.cdid' };
22 my $rs = $schema->resultset("CD")->search($search, $attr);
25 is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
27 ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
29 is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
31 is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
33 is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
35 is($queries, 1, 'prefetch ran only 1 select statement');
37 $schema->storage->debug($orig_debug);
38 $schema->storage->debugobj->callback(undef);
40 # test for partial prefetch via columns attr
41 my $cd = $schema->resultset('CD')->find(1,
43 columns => [qw/title artist artist.name/],
44 join => { 'artist' => {} }
47 ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
49 # start test for nested prefetch SELECT count
51 $schema->storage->debugcb(sub { $queries++ });
52 $schema->storage->debug(1);
54 $rs = $schema->resultset('Tag')->search(
57 prefetch => { cd => 'artist' }
63 is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
65 is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
68 #$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
69 is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
73 is($tag->search_related('cd')->search_related('artist')->first->name,
75 'chained belongs_to->belongs_to search_related ok');
77 is($queries, 0, 'chained search_related after belontgs_to->belongs_to prefetch ran no queries');
81 $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
83 is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
85 is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
89 $schema->storage->debugcb(sub { $queries++; });
91 $cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' } });
93 is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok');
95 is($queries, 1, 'many_to_many accessor with nested prefetch ran exactly 1 query');
99 my $producers = $cd->search_related('cd_to_producer')->search_related('producer');
101 is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok');
103 is($queries, 0, 'chained search_related after many_to_many prefetch ran no queries');
105 $schema->storage->debug($orig_debug);
106 $schema->storage->debugobj->callback(undef);
108 $rs = $schema->resultset('Tag')->search(
111 join => { cd => 'artist' },
112 prefetch => { cd => 'artist' }
116 cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
118 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
119 { order_by => 'artistid DESC', join => 'cds' });
121 is($artist->name, 'Random Boy Band', "Join search by object ok");
123 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
124 { join => 'liner_notes' });
126 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
128 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
130 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
131 { join => { 'cds' => 'tags' } });
133 cmp_ok( @artists, '==', 2, "two-join search ok" );
135 $rs = $schema->resultset("CD")->search(
137 { group_by => [qw/ title me.cdid /] }
140 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
142 cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
144 $rs = $schema->resultset("CD")->search(
146 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
149 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
151 $rs = $schema->resultset("Artist")->search(
153 { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } }
156 cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
158 $rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }});
160 cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
162 $rs = $schema->resultset("Artist")->search(
163 { 'cds.title' => 'Spoonful of bees',
164 'cds_2.title' => 'Forkful of bees' },
165 { join => [ 'cds', 'cds' ] });
167 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
169 is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
171 $cd = $schema->resultset('Artist')->first->create_related('cds',
173 title => 'Unproduced Single',
177 my $left_join = $schema->resultset('CD')->search(
178 { 'me.cdid' => $cd->cdid },
179 { prefetch => { cd_to_producer => 'producer' } }
182 cmp_ok($left_join, '==', 1, 'prefetch with no join record present');
185 $schema->storage->debugcb(sub { $queries++ });
186 $schema->storage->debug(1);
189 $schema->resultset('TreeLike')->find(5,
190 { join => { parent => { parent => 'parent' } },
191 prefetch => { parent => { parent => 'parent' } } });
193 is($tree_like->name, 'quux', 'Bottom of tree ok');
194 $tree_like = $tree_like->parent;
195 is($tree_like->name, 'baz', 'First level up ok');
196 $tree_like = $tree_like->parent;
197 is($tree_like->name, 'bar', 'Second level up ok');
198 $tree_like = $tree_like->parent;
199 is($tree_like->name, 'foo', 'Third level up ok');
201 $schema->storage->debug($orig_debug);
202 $schema->storage->debugobj->callback(undef);
204 cmp_ok($queries, '==', 1, 'Only one query run');
206 $tree_like = $schema->resultset('TreeLike')->search({'me.id' => 2});
207 $tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first;
208 is($tree_like->name, 'quux', 'Tree search_related ok');
210 $tree_like = $schema->resultset('TreeLike')->search_related('children',
211 { 'children.id' => 3, 'children_2.id' => 4 },
212 { prefetch => { children => 'children' } }
214 is(eval { $tree_like->children->first->children->first->name }, 'quux',
215 'Tree search_related with prefetch ok');
217 $tree_like = eval { $schema->resultset('TreeLike')->search(
218 { 'children.id' => 3, 'children_2.id' => 6 },
219 { join => [qw/children children children/] }
220 )->search_related('children', { 'children_4.id' => 7 }, { prefetch => 'children' }
221 )->first->children->first; };
222 is(eval { $tree_like->name }, 'fong', 'Tree with multiple has_many joins ok');
224 $rs = $schema->resultset('Artist');
225 $rs->create({ artistid => 4, name => 'Unknown singer-songwriter' });
226 $rs->create({ artistid => 5, name => 'Emo 4ever' });
227 @artists = $rs->search(undef, { prefetch => 'cds', order_by => 'artistid' });
228 is(scalar @artists, 5, 'has_many prefetch with adjacent empty rows ok');
232 # Tests for multilevel has_many prefetch
234 # artist resultsets - with and without prefetch
235 my $art_rs = $schema->resultset('Artist');
236 my $art_rs_pr = $art_rs->search(
239 join => [ { cds => ['tracks'] } ],
240 prefetch => [ { cds => ['tracks'] } ],
241 cache => 1 # last test needs this
245 # This test does the same operation twice - once on a
246 # set of items fetched from the db with no prefetch of has_many rels
247 # The second prefetches 2 levels of has_many
248 # We check things are the same by comparing the name or title
249 # we build everything into a hash structure and compare the one
250 # from each rs to see what differs
252 sub make_hash_struc {
256 foreach my $art ( $rs->all ) {
257 foreach my $cd ( $art->cds ) {
258 foreach my $track ( $cd->tracks ) {
259 $struc->{ $art->name }{ $cd->title }{ $track->title }++;
267 $schema->storage->debugcb(sub { $queries++ });
268 $schema->storage->debug(1);
270 my $prefetch_result = make_hash_struc($art_rs_pr);
272 is($queries, 1, 'nested prefetch across has_many->has_many ran exactly 1 query');
274 my $nonpre_result = make_hash_struc($art_rs);
276 is_deeply( $prefetch_result, $nonpre_result,
277 'Compare 2 level prefetch result to non-prefetch result' );
281 is($art_rs_pr->search_related('cds')->search_related('tracks')->first->title,
283 'chained has_many->has_many search_related ok'
286 is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries');
288 $schema->storage->debug($orig_debug);
289 $schema->storage->debugobj->callback(undef);