11 my $schema = DBICTest->init_schema();
12 my $orig_debug = $schema->storage->debug;
17 $schema->storage->debugcb(sub { $queries++; });
18 $schema->storage->debug(1);
20 my $search = { 'artist.name' => 'Caterwauler McCrae' };
21 my $attr = { prefetch => [ qw/artist liner_notes/ ],
22 order_by => 'me.cdid' };
23 my $search_str = Dumper($search);
24 my $attr_str = Dumper($attr);
26 my $rs = $schema->resultset("CD")->search($search, $attr);
29 is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
31 ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
33 is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
35 is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
37 is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
39 is($queries, 1, 'prefetch ran only 1 select statement');
41 $schema->storage->debug($orig_debug);
42 $schema->storage->debugobj->callback(undef);
44 # test for partial prefetch via columns attr
45 my $cd = $schema->resultset('CD')->find(1,
47 columns => [qw/title artist artist.name/],
48 join => { 'artist' => {} }
51 ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
53 # start test for nested prefetch SELECT count
55 $schema->storage->debugcb(sub { $queries++ });
56 $schema->storage->debug(1);
58 $rs = $schema->resultset('Tag')->search(
61 prefetch => { cd => 'artist' }
67 is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
69 is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
72 #$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
73 is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
77 is($tag->search_related('cd')->search_related('artist')->first->name,
79 'chained belongs_to->belongs_to search_related ok');
81 is($queries, 0, 'chained search_related after belontgs_to->belongs_to prefetch ran no queries');
85 $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
87 is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
89 is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
93 $schema->storage->debugcb(sub { $queries++; });
95 $cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' } });
97 is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok');
99 is($queries, 1, 'many_to_many accessor with nested prefetch ran exactly 1 query');
103 my $producers = $cd->search_related('cd_to_producer')->search_related('producer');
105 is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok');
107 is($queries, 0, 'chained search_related after many_to_many prefetch ran no queries');
109 $schema->storage->debug($orig_debug);
110 $schema->storage->debugobj->callback(undef);
112 $rs = $schema->resultset('Tag')->search(
115 join => { cd => 'artist' },
116 prefetch => { cd => 'artist' }
120 cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
122 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
123 { order_by => 'artistid DESC', join => 'cds' });
125 is($artist->name, 'Random Boy Band', "Join search by object ok");
127 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
128 { join => 'liner_notes' });
130 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
132 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
134 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
135 { join => { 'cds' => 'tags' } });
137 cmp_ok( @artists, '==', 2, "two-join search ok" );
139 $rs = $schema->resultset("CD")->search(
141 { group_by => [qw/ title me.cdid /] }
144 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
146 cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
148 $rs = $schema->resultset("CD")->search(
150 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
153 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
155 $rs = $schema->resultset("Artist")->search(
157 { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } }
160 cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
162 $rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }});
164 cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
166 $rs = $schema->resultset("Artist")->search(
167 { 'cds.title' => 'Spoonful of bees',
168 'cds_2.title' => 'Forkful of bees' },
169 { join => [ 'cds', 'cds' ] });
171 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
173 is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
175 $cd = $schema->resultset('Artist')->first->create_related('cds',
177 title => 'Unproduced Single',
181 my $left_join = $schema->resultset('CD')->search(
182 { 'me.cdid' => $cd->cdid },
183 { prefetch => { cd_to_producer => 'producer' } }
186 cmp_ok($left_join, '==', 1, 'prefetch with no join record present');
189 $schema->storage->debugcb(sub { $queries++ });
190 $schema->storage->debug(1);
193 $schema->resultset('TreeLike')->find(5,
194 { join => { parent => { parent => 'parent' } },
195 prefetch => { parent => { parent => 'parent' } } });
197 is($tree_like->name, 'quux', 'Bottom of tree ok');
198 $tree_like = $tree_like->parent;
199 is($tree_like->name, 'baz', 'First level up ok');
200 $tree_like = $tree_like->parent;
201 is($tree_like->name, 'bar', 'Second level up ok');
202 $tree_like = $tree_like->parent;
203 is($tree_like->name, 'foo', 'Third level up ok');
205 $schema->storage->debug($orig_debug);
206 $schema->storage->debugobj->callback(undef);
208 cmp_ok($queries, '==', 1, 'Only one query run');
210 $tree_like = $schema->resultset('TreeLike')->search({'me.id' => 2});
211 $tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first;
212 is($tree_like->name, 'quux', 'Tree search_related ok');
214 $tree_like = $schema->resultset('TreeLike')->search_related('children',
215 { 'children.id' => 3, 'children_2.id' => 4 },
216 { prefetch => { children => 'children' } }
218 is(eval { $tree_like->children->first->children->first->name }, 'quux',
219 'Tree search_related with prefetch ok');
221 $tree_like = eval { $schema->resultset('TreeLike')->search(
222 { 'children.id' => 3, 'children_2.id' => 6 },
223 { join => [qw/children children children/] }
224 )->search_related('children', { 'children_4.id' => 7 }, { prefetch => 'children' }
225 )->first->children->first; };
226 is(eval { $tree_like->name }, 'fong', 'Tree with multiple has_many joins ok');
228 $rs = $schema->resultset('Artist');
229 $rs->create({ artistid => 4, name => 'Unknown singer-songwriter' });
230 $rs->create({ artistid => 5, name => 'Emo 4ever' });
231 @artists = $rs->search(undef, { prefetch => 'cds', order_by => 'artistid' });
232 is(scalar @artists, 5, 'has_many prefetch with adjacent empty rows ok');
236 # Tests for multilevel has_many prefetch
238 # artist resultsets - with and without prefetch
239 my $art_rs = $schema->resultset('Artist');
240 my $art_rs_pr = $art_rs->search(
243 join => [ { cds => ['tracks'] } ],
244 prefetch => [ { cds => ['tracks'] } ],
245 cache => 1 # last test needs this
249 # This test does the same operation twice - once on a
250 # set of items fetched from the db with no prefetch of has_many rels
251 # The second prefetches 2 levels of has_many
252 # We check things are the same by comparing the name or title
253 # we build everything into a hash structure and compare the one
254 # from each rs to see what differs
256 sub make_hash_struc {
260 foreach my $art ( $rs->all ) {
261 foreach my $cd ( $art->cds ) {
262 foreach my $track ( $cd->tracks ) {
263 $struc->{ $art->name }{ $cd->title }{ $track->title }++;
271 $schema->storage->debugcb(sub { $queries++ });
272 $schema->storage->debug(1);
274 my $prefetch_result = make_hash_struc($art_rs_pr);
276 is($queries, 1, 'nested prefetch across has_many->has_many ran exactly 1 query');
278 my $nonpre_result = make_hash_struc($art_rs);
280 is_deeply( $prefetch_result, $nonpre_result,
281 'Compare 2 level prefetch result to non-prefetch result' );
285 is($art_rs_pr->search_related('cds')->search_related('tracks')->first->title,
287 'chained has_many->has_many search_related ok'
290 is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries');
292 $schema->storage->debug($orig_debug);
293 $schema->storage->debugobj->callback(undef);