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' )
23 # figure out if we've got a version of sqlite that is older than 3.2.6, in
24 # which case COUNT(DISTINCT()) doesn't work
25 my $is_broken_sqlite = 0;
26 my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
27 split /\./, $schema->storage->dbh->get_info(18);
28 if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
29 ( ($sqlite_major_ver < 3) ||
30 ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
31 ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
32 $is_broken_sqlite = 1;
36 $schema->storage->debugcb(sub { $queries++; });
37 $schema->storage->debug(1);
39 my $search = { 'artist.name' => 'Caterwauler McCrae' };
40 my $attr = { prefetch => [ qw/artist liner_notes/ ],
41 order_by => 'me.cdid' };
42 my $search_str = Dumper($search);
43 my $attr_str = Dumper($attr);
45 my $rs = $schema->resultset("CD")->search($search, $attr);
48 is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
50 ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
52 is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
54 is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
56 is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
58 is($queries, 1, 'prefetch ran only 1 select statement');
60 $schema->storage->debug($orig_debug);
61 $schema->storage->debugobj->callback(undef);
63 # test for partial prefetch via columns attr
64 my $cd = $schema->resultset('CD')->find(1,
66 columns => [qw/title artist artist.name/],
67 join => { 'artist' => {} }
70 ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
72 # start test for nested prefetch SELECT count
74 $schema->storage->debugcb(sub { $queries++ });
75 $schema->storage->debug(1);
77 $rs = $schema->resultset('Tag')->search(
80 prefetch => { cd => 'artist' }
86 is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
88 is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
91 #$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
92 is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
96 is($tag->search_related('cd')->search_related('artist')->first->name,
98 'chained belongs_to->belongs_to search_related ok');
100 is($queries, 0, 'chained search_related after belontgs_to->belongs_to prefetch ran no queries');
104 $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
106 is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
108 is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
112 $schema->storage->debugcb(sub { $queries++; });
114 $cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' } });
116 is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok');
118 is($queries, 1, 'many_to_many accessor with nested prefetch ran exactly 1 query');
122 my $producers = $cd->search_related('cd_to_producer')->search_related('producer');
124 is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok');
126 is($queries, 0, 'chained search_related after many_to_many prefetch ran no queries');
128 $schema->storage->debug($orig_debug);
129 $schema->storage->debugobj->callback(undef);
131 $rs = $schema->resultset('Tag')->search(
134 join => { cd => 'artist' },
135 prefetch => { cd => 'artist' }
139 cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
141 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
142 { order_by => 'artistid DESC', join => 'cds' });
144 is($artist->name, 'Random Boy Band', "Join search by object ok");
146 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
147 { join => 'liner_notes' });
149 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
151 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
153 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
154 { join => { 'cds' => 'tags' } });
156 cmp_ok( @artists, '==', 2, "two-join search ok" );
158 $rs = $schema->resultset("CD")->search(
160 { group_by => [qw/ title me.cdid /] }
164 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
165 if $is_broken_sqlite;
166 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
169 cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
171 $rs = $schema->resultset("CD")->search(
173 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
177 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
178 if $is_broken_sqlite;
179 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
182 $rs = $schema->resultset("Artist")->search(
184 { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } }
187 cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
189 $rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }});
191 cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
193 $rs = $schema->resultset("Artist")->search(
194 { 'cds.title' => 'Spoonful of bees',
195 'cds_2.title' => 'Forkful of bees' },
196 { join => [ 'cds', 'cds' ] });
199 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
200 if $is_broken_sqlite;
201 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
204 is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
206 $cd = $schema->resultset('Artist')->first->create_related('cds',
208 title => 'Unproduced Single',
212 my $left_join = $schema->resultset('CD')->search(
213 { 'me.cdid' => $cd->cdid },
214 { prefetch => { cd_to_producer => 'producer' } }
217 cmp_ok($left_join, '==', 1, 'prefetch with no join record present');
220 $schema->storage->debugcb(sub { $queries++ });
221 $schema->storage->debug(1);
224 $schema->resultset('TreeLike')->find(5,
225 { join => { parent => { parent => 'parent' } },
226 prefetch => { parent => { parent => 'parent' } } });
228 is($tree_like->name, 'quux', 'Bottom of tree ok');
229 $tree_like = $tree_like->parent;
230 is($tree_like->name, 'baz', 'First level up ok');
231 $tree_like = $tree_like->parent;
232 is($tree_like->name, 'bar', 'Second level up ok');
233 $tree_like = $tree_like->parent;
234 is($tree_like->name, 'foo', 'Third level up ok');
236 $schema->storage->debug($orig_debug);
237 $schema->storage->debugobj->callback(undef);
239 cmp_ok($queries, '==', 1, 'Only one query run');
241 $tree_like = $schema->resultset('TreeLike')->search({'me.id' => 2});
242 $tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first;
243 is($tree_like->name, 'quux', 'Tree search_related ok');
245 $tree_like = $schema->resultset('TreeLike')->search_related('children',
246 { 'children.id' => 3, 'children_2.id' => 4 },
247 { prefetch => { children => 'children' } }
249 is(eval { $tree_like->children->first->children->first->name }, 'quux',
250 'Tree search_related with prefetch ok');
252 $tree_like = eval { $schema->resultset('TreeLike')->search(
253 { 'children.id' => 3, 'children_2.id' => 6 },
254 { join => [qw/children children/] }
255 )->search_related('children', { 'children_4.id' => 7 }, { prefetch => 'children' }
256 )->first->children->first; };
257 is(eval { $tree_like->name }, 'fong', 'Tree with multiple has_many joins ok');
259 # test that collapsed joins don't get a _2 appended to the alias
262 $schema->storage->debugcb(sub { $sql = $_[1] });
263 $schema->storage->debug(1);
266 my $row = $schema->resultset('Artist')->search_related('cds', undef, {
268 prefetch => 'tracks',
269 })->search_related('tracks')->first;
272 like( $sql, qr/^SELECT tracks_2\.trackid/, "join not collapsed for search_related" );
274 $schema->storage->debug($orig_debug);
275 $schema->storage->debugobj->callback(undef);
277 $rs = $schema->resultset('Artist');
278 $rs->create({ artistid => 4, name => 'Unknown singer-songwriter' });
279 $rs->create({ artistid => 5, name => 'Emo 4ever' });
280 @artists = $rs->search(undef, { prefetch => 'cds', order_by => 'artistid' });
281 is(scalar @artists, 5, 'has_many prefetch with adjacent empty rows ok');
285 # Tests for multilevel has_many prefetch
287 # artist resultsets - with and without prefetch
288 my $art_rs = $schema->resultset('Artist');
289 my $art_rs_pr = $art_rs->search(
292 join => [ { cds => ['tracks'] } ],
293 prefetch => [ { cds => ['tracks'] } ],
294 cache => 1 # last test needs this
298 # This test does the same operation twice - once on a
299 # set of items fetched from the db with no prefetch of has_many rels
300 # The second prefetches 2 levels of has_many
301 # We check things are the same by comparing the name or title
302 # we build everything into a hash structure and compare the one
303 # from each rs to see what differs
305 sub make_hash_struc {
309 foreach my $art ( $rs->all ) {
310 foreach my $cd ( $art->cds ) {
311 foreach my $track ( $cd->tracks ) {
312 $struc->{ $art->name }{ $cd->title }{ $track->title }++;
320 $schema->storage->debugcb(sub { $queries++ });
321 $schema->storage->debug(1);
323 my $prefetch_result = make_hash_struc($art_rs_pr);
325 is($queries, 1, 'nested prefetch across has_many->has_many ran exactly 1 query');
327 my $nonpre_result = make_hash_struc($art_rs);
329 is_deeply( $prefetch_result, $nonpre_result,
330 'Compare 2 level prefetch result to non-prefetch result' );
334 is($art_rs_pr->search_related('cds')->search_related('tracks')->first->title,
336 'chained has_many->has_many search_related ok'
339 is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries');