10 my $schema = DBICTest->init_schema();
12 my $orig_debug = $schema->storage->debug;
19 $schema->storage->debugcb(sub { $queries++; });
20 $schema->storage->debug(1);
22 my $search = { 'artist.name' => 'Caterwauler McCrae' };
23 my $attr = { prefetch => [ qw/artist liner_notes/ ],
24 order_by => 'me.cdid' };
25 my $search_str = Dumper($search);
26 my $attr_str = Dumper($attr);
28 my $rs = $schema->resultset("CD")->search($search, $attr);
31 is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
33 ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
35 is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
37 is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
39 is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
41 is($queries, 1, 'prefetch ran only 1 select statement');
43 $schema->storage->debug($orig_debug);
44 $schema->storage->debugobj->callback(undef);
46 # test for partial prefetch via columns attr
47 my $cd = $schema->resultset('CD')->find(1,
49 columns => [qw/title artist artist.name/],
50 join => { 'artist' => {} }
53 ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
55 # start test for nested prefetch SELECT count
57 $schema->storage->debugcb(sub { $queries++ });
58 $schema->storage->debug(1);
60 $rs = $schema->resultset('Tag')->search(
63 prefetch => { cd => 'artist' }
69 is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
71 is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
74 #$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
75 is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
79 is($tag->search_related('cd')->search_related('artist')->first->name,
81 'chained belongs_to->belongs_to search_related ok');
83 is($queries, 0, 'chained search_related after belontgs_to->belongs_to prefetch ran no queries');
87 $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
89 is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
91 is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
95 $schema->storage->debugcb(sub { $queries++; });
97 $cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' } });
99 is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok');
101 is($queries, 1, 'many_to_many accessor with nested prefetch ran exactly 1 query');
105 my $producers = $cd->search_related('cd_to_producer')->search_related('producer');
107 is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok');
109 is($queries, 0, 'chained search_related after many_to_many prefetch ran no queries');
111 $schema->storage->debug($orig_debug);
112 $schema->storage->debugobj->callback(undef);
114 $rs = $schema->resultset('Tag')->search(
117 join => { cd => 'artist' },
118 prefetch => { cd => 'artist' }
122 cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
124 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
125 { order_by => 'artistid DESC', join => 'cds' });
127 is($artist->name, 'Random Boy Band', "Join search by object ok");
129 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
130 { join => 'liner_notes' });
132 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
134 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
136 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
137 { join => { 'cds' => 'tags' } });
139 cmp_ok( @artists, '==', 2, "two-join search ok" );
141 $rs = $schema->resultset("CD")->search(
143 { group_by => [qw/ title me.cdid /] }
146 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
148 cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
150 $rs = $schema->resultset("CD")->search(
152 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
155 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
157 $rs = $schema->resultset("Artist")->search(
159 { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } }
162 cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
164 $rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }});
166 cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
168 $rs = $schema->resultset("Artist")->search(
169 { 'cds.title' => 'Spoonful of bees',
170 'cds_2.title' => 'Forkful of bees' },
171 { join => [ 'cds', 'cds' ] });
173 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
175 is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
177 $cd = $schema->resultset('Artist')->first->create_related('cds',
179 title => 'Unproduced Single',
183 my $left_join = $schema->resultset('CD')->search(
184 { 'me.cdid' => $cd->cdid },
185 { prefetch => { cd_to_producer => 'producer' } }
188 cmp_ok($left_join, '==', 1, 'prefetch with no join record present');
191 $schema->storage->debugcb(sub { $queries++ });
192 $schema->storage->debug(1);
195 $schema->resultset('TreeLike')->find(5,
196 { join => { parent => { parent => 'parent' } },
197 prefetch => { parent => { parent => 'parent' } } });
199 is($tree_like->name, 'quux', 'Bottom of tree ok');
200 $tree_like = $tree_like->parent;
201 is($tree_like->name, 'baz', 'First level up ok');
202 $tree_like = $tree_like->parent;
203 is($tree_like->name, 'bar', 'Second level up ok');
204 $tree_like = $tree_like->parent;
205 is($tree_like->name, 'foo', 'Third level up ok');
207 $schema->storage->debug($orig_debug);
208 $schema->storage->debugobj->callback(undef);
210 cmp_ok($queries, '==', 1, 'Only one query run');
212 $tree_like = $schema->resultset('TreeLike')->search({'me.id' => 2});
213 $tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first;
214 is($tree_like->name, 'quux', 'Tree search_related ok');
216 $tree_like = $schema->resultset('TreeLike')->search_related('children',
217 { 'children.id' => 3, 'children_2.id' => 4 },
218 { prefetch => { children => 'children' } }
220 is(eval { $tree_like->children->first->children->first->name }, 'quux',
221 'Tree search_related with prefetch ok');
223 $tree_like = eval { $schema->resultset('TreeLike')->search(
224 { 'children.id' => 3, 'children_2.id' => 6 },
225 { join => [qw/children children children/] }
226 )->search_related('children', { 'children_4.id' => 7 }, { prefetch => 'children' }
227 )->first->children->first; };
228 is(eval { $tree_like->name }, 'fong', 'Tree with multiple has_many joins ok');
230 $rs = $schema->resultset('Artist');
231 $rs->create({ artistid => 4, name => 'Unknown singer-songwriter' });
232 $rs->create({ artistid => 5, name => 'Emo 4ever' });
233 @artists = $rs->search(undef, { prefetch => 'cds', order_by => 'artistid' });
234 is(scalar @artists, 5, 'has_many prefetch with adjacent empty rows ok');
238 # Tests for multilevel has_many prefetch
240 # artist resultsets - with and without prefetch
241 my $art_rs = $schema->resultset('Artist');
242 my $art_rs_pr = $art_rs->search(
245 join => [ { cds => ['tracks'] } ],
246 prefetch => [ { cds => ['tracks'] } ],
247 cache => 1 # last test needs this
251 # This test does the same operation twice - once on a
252 # set of items fetched from the db with no prefetch of has_many rels
253 # The second prefetches 2 levels of has_many
254 # We check things are the same by comparing the name or title
255 # we build everything into a hash structure and compare the one
256 # from each rs to see what differs
258 sub make_hash_struc {
262 foreach my $art ( $rs->all ) {
263 foreach my $cd ( $art->cds ) {
264 foreach my $track ( $cd->tracks ) {
265 $struc->{ $art->name }{ $cd->title }{ $track->title }++;
273 $schema->storage->debugcb(sub { $queries++ });
274 $schema->storage->debug(1);
276 my $prefetch_result = make_hash_struc($art_rs_pr);
278 is($queries, 1, 'nested prefetch across has_many->has_many ran exactly 1 query');
280 my $nonpre_result = make_hash_struc($art_rs);
282 is_deeply( $prefetch_result, $nonpre_result,
283 'Compare 2 level prefetch result to non-prefetch result' );
287 is($art_rs_pr->search_related('cds')->search_related('tracks')->first->title,
289 'chained has_many->has_many search_related ok'
292 is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries');