9 my $schema = DBICTest->init_schema();
10 my $orig_debug = $schema->storage->debug;
13 $schema->storage->debugcb(sub { $queries++; });
14 $schema->storage->debug(1);
16 my $search = { 'artist.name' => 'Caterwauler McCrae' };
17 my $attr = { prefetch => [ qw/artist liner_notes/ ],
18 order_by => 'me.cdid' };
20 my $rs = $schema->resultset("CD")->search($search, $attr);
23 is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
25 ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
27 is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
29 is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
31 is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
33 is($queries, 1, 'prefetch ran only 1 select statement');
35 $schema->storage->debug($orig_debug);
36 $schema->storage->debugobj->callback(undef);
38 # test for partial prefetch via columns attr
39 my $cd = $schema->resultset('CD')->find(1,
41 columns => [qw/title artist artist.name/],
42 join => { 'artist' => {} }
45 ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
47 # start test for nested prefetch SELECT count
49 $schema->storage->debugcb(sub { $queries++ });
50 $schema->storage->debug(1);
52 $rs = $schema->resultset('Tag')->search(
55 prefetch => { cd => 'artist' }
61 is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
63 is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
66 #$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
67 is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
71 is($tag->search_related('cd')->search_related('artist')->first->name,
73 'chained belongs_to->belongs_to search_related ok');
75 is($queries, 0, 'chained search_related after belontgs_to->belongs_to prefetch ran no queries');
79 $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
81 is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
83 is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
87 $schema->storage->debugcb(sub { $queries++; });
89 $cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' }, order_by => 'producer.producerid' });
91 is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok');
93 is($queries, 1, 'many_to_many accessor with nested prefetch ran exactly 1 query');
97 my $producers = $cd->search_related('cd_to_producer')->search_related('producer');
99 is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok');
101 is($queries, 0, 'chained search_related after many_to_many prefetch ran no queries');
103 $schema->storage->debug($orig_debug);
104 $schema->storage->debugobj->callback(undef);
106 $rs = $schema->resultset('Tag')->search(
109 join => { cd => 'artist' },
110 prefetch => { cd => 'artist' }
114 cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
116 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
117 { order_by => 'artistid DESC', join => 'cds' });
119 is($artist->name, 'Random Boy Band', "Join search by object ok");
121 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
122 { join => 'liner_notes' });
124 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
126 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
128 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
129 { join => { 'cds' => 'tags' } });
131 cmp_ok( @artists, '==', 2, "two-join search ok" );
133 $rs = $schema->resultset("CD")->search(
135 { group_by => [qw/ title me.cdid /] }
138 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
140 cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
142 $rs = $schema->resultset("CD")->search(
144 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
147 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
149 $rs = $schema->resultset("Artist")->search({}, {
151 group_by => [qw/ me.name /],
152 having => \[ 'MAX(cds.cdid) < ?', [ \'int' => 5 ] ],
155 cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
157 $rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }});
159 cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
161 $rs = $schema->resultset("Artist")->search(
162 { 'cds.title' => 'Spoonful of bees',
163 'cds_2.title' => 'Forkful of bees' },
164 { join => [ 'cds', 'cds' ] });
166 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
168 is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
170 $cd = $schema->resultset('Artist')->first->create_related('cds',
172 title => 'Unproduced Single',
176 my $left_join = $schema->resultset('CD')->search(
177 { 'me.cdid' => $cd->cdid },
178 { prefetch => { cd_to_producer => 'producer' } }
181 cmp_ok($left_join, '==', 1, 'prefetch with no join record present');
184 $schema->storage->debugcb(sub { $queries++ });
185 $schema->storage->debug(1);
188 $schema->resultset('TreeLike')->find(5,
189 { join => { parent => { parent => 'parent' } },
190 prefetch => { parent => { parent => 'parent' } } });
192 is($tree_like->name, 'quux', 'Bottom of tree ok');
193 $tree_like = $tree_like->parent;
194 is($tree_like->name, 'baz', 'First level up ok');
195 $tree_like = $tree_like->parent;
196 is($tree_like->name, 'bar', 'Second level up ok');
197 $tree_like = $tree_like->parent;
198 is($tree_like->name, 'foo', 'Third level up ok');
200 $schema->storage->debug($orig_debug);
201 $schema->storage->debugobj->callback(undef);
203 cmp_ok($queries, '==', 1, 'Only one query run');
205 $tree_like = $schema->resultset('TreeLike')->search({'me.id' => 2});
206 $tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first;
207 is($tree_like->name, 'quux', 'Tree search_related ok');
209 $tree_like = $schema->resultset('TreeLike')->search_related('children',
210 { 'children.id' => 3, 'children_2.id' => 4 },
211 { prefetch => { children => 'children' } }
213 is(eval { $tree_like->children->first->children->first->name }, 'quux',
214 'Tree search_related with prefetch ok');
216 $tree_like = eval { $schema->resultset('TreeLike')->search(
217 { 'children.id' => 3, 'children_2.id' => 6 },
218 { join => [qw/children children children/] }
219 )->search_related('children', { 'children_4.id' => 7 }, { prefetch => 'children' }
220 )->first->children->first; };
221 is(eval { $tree_like->name }, 'fong', 'Tree with multiple has_many joins ok');
223 $rs = $schema->resultset('Artist');
224 $rs->create({ artistid => 4, name => 'Unknown singer-songwriter' });
225 $rs->create({ artistid => 5, name => 'Emo 4ever' });
226 @artists = $rs->search(undef, { prefetch => 'cds', order_by => 'artistid' });
227 is(scalar @artists, 5, 'has_many prefetch with adjacent empty rows ok');
229 lives_ok { @artists = $rs->search(undef, {
234 } 'join and empty prefetch 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 # all of these ought to work, but do not for some reason
263 # a noop cloning search() pollution?
264 #foreach my $art ( $rs->search({}, { order_by => 'me.artistid' })->all ) {
265 #foreach my $art ( $rs->search({}, {})->all ) {
266 #foreach my $art ( $rs->search()->all ) {
267 foreach my $art ( $rs->all ) {
268 foreach my $cd ( $art->cds ) {
269 foreach my $track ( $cd->tracks ) {
270 $struc->{ $art->name }{ $cd->title }{ $track->title }++;
278 $schema->storage->debugcb(sub { $queries++ });
279 $schema->storage->debug(1);
281 my $prefetch_result = make_hash_struc($art_rs_pr);
283 is($queries, 1, 'nested prefetch across has_many->has_many ran exactly 1 query');
285 my $nonpre_result = make_hash_struc($art_rs);
287 is_deeply( $prefetch_result, $nonpre_result,
288 'Compare 2 level prefetch result to non-prefetch result' );
293 [ sort map { $_->title } $art_rs_pr->search_related('cds')->search_related('tracks')->all ],
294 [ 'Apiary', 'Beehind You', 'Boring Name', 'Boring Song', 'Fowlin', 'Howlin',
295 'No More Ideas', 'Sad', 'Sticky Honey', 'Stripy', 'Stung with Success',
296 'Suicidal', 'The Bees Knees', 'Under The Weather', 'Yowlin' ],
297 'chained has_many->has_many search_related ok'
300 is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries');
302 $schema->storage->debug($orig_debug);
303 $schema->storage->debugobj->callback(undef);