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);
41 is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
43 ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
45 is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
47 is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
49 is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
51 is($queries, 1, 'prefetch ran only 1 select statement');
53 $schema->storage->debug($orig_debug);
54 $schema->storage->debugobj->callback(undef);
56 # test for partial prefetch via columns attr
57 my $cd = $schema->resultset('CD')->find(1,
59 columns => [qw/title artist artist.name/],
60 join => { 'artist' => {} }
63 ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
65 # start test for nested prefetch SELECT count
67 $schema->storage->debugcb(sub { $queries++ });
68 $schema->storage->debug(1);
70 $rs = $schema->resultset('Tag')->search(
73 prefetch => { cd => 'artist' }
79 is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
81 is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
84 #$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
85 is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
89 is($tag->search_related('cd')->search_related('artist')->first->name,
91 'chained belongs_to->belongs_to search_related ok');
93 is($queries, 0, 'chained search_related after belontgs_to->belongs_to prefetch ran no queries');
97 $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
99 is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
101 is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
105 $schema->storage->debugcb(sub { $queries++; });
107 $cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' } });
109 is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok');
111 is($queries, 1, 'many_to_many accessor with nested prefetch ran exactly 1 query');
115 my $producers = $cd->search_related('cd_to_producer')->search_related('producer');
117 is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok');
119 is($queries, 0, 'chained search_related after many_to_many prefetch ran no queries');
121 $schema->storage->debug($orig_debug);
122 $schema->storage->debugobj->callback(undef);
124 $rs = $schema->resultset('Tag')->search(
127 join => { cd => 'artist' },
128 prefetch => { cd => 'artist' }
132 cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
134 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
135 { order_by => 'artistid DESC', join => 'cds' });
137 is($artist->name, 'Random Boy Band', "Join search by object ok");
139 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
140 { join => 'liner_notes' });
142 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
144 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
146 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
147 { join => { 'cds' => 'tags' } });
149 cmp_ok( @artists, '==', 2, "two-join search ok" );
151 $rs = $schema->resultset("CD")->search(
153 { group_by => [qw/ title me.cdid /] }
157 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
158 if $is_broken_sqlite;
159 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
162 cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
164 $rs = $schema->resultset("CD")->search(
166 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
170 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
171 if $is_broken_sqlite;
172 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
175 $rs = $schema->resultset("Artist")->search(
177 { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } }
180 cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
182 $rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }});
184 cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
186 $rs = $schema->resultset("Artist")->search(
187 { 'cds.title' => 'Spoonful of bees',
188 'cds_2.title' => 'Forkful of bees' },
189 { join => [ 'cds', 'cds' ] });
192 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
193 if $is_broken_sqlite;
194 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
197 is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
199 $cd = $schema->resultset('Artist')->first->create_related('cds',
201 title => 'Unproduced Single',
205 my $left_join = $schema->resultset('CD')->search(
206 { 'me.cdid' => $cd->cdid },
207 { prefetch => { cd_to_producer => 'producer' } }
210 cmp_ok($left_join, '==', 1, 'prefetch with no join record present');
213 $schema->storage->debugcb(sub { $queries++ });
214 $schema->storage->debug(1);
217 $schema->resultset('TreeLike')->find(5,
218 { join => { parent => { parent => 'parent' } },
219 prefetch => { parent => { parent => 'parent' } } });
221 is($tree_like->name, 'quux', 'Bottom of tree ok');
222 $tree_like = $tree_like->parent;
223 is($tree_like->name, 'baz', 'First level up ok');
224 $tree_like = $tree_like->parent;
225 is($tree_like->name, 'bar', 'Second level up ok');
226 $tree_like = $tree_like->parent;
227 is($tree_like->name, 'foo', 'Third level up ok');
229 $schema->storage->debug($orig_debug);
230 $schema->storage->debugobj->callback(undef);
232 cmp_ok($queries, '==', 1, 'Only one query run');
234 $tree_like = $schema->resultset('TreeLike')->search({'me.id' => 2});
235 $tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first;
236 is($tree_like->name, 'quux', 'Tree search_related ok');
238 $tree_like = $schema->resultset('TreeLike')->search_related('children',
239 { 'children.id' => 3, 'children_2.id' => 4 },
240 { prefetch => { children => 'children' } }
242 is(eval { $tree_like->children->first->children->first->name }, 'quux',
243 'Tree search_related with prefetch ok');
245 $tree_like = eval { $schema->resultset('TreeLike')->search(
246 { 'children.id' => 3, 'children_2.id' => 6 },
247 { join => [qw/children children/] }
248 )->search_related('children', { 'children_4.id' => 7 }, { prefetch => 'children' }
249 )->first->children->first; };
250 is(eval { $tree_like->name }, 'fong', 'Tree with multiple has_many joins ok');
252 # test that collapsed joins don't get a _2 appended to the alias
255 $schema->storage->debugcb(sub { $sql = $_[1] });
256 $schema->storage->debug(1);
259 my $row = $schema->resultset('Artist')->search_related('cds', undef, {
261 prefetch => 'tracks',
262 })->search_related('tracks')->first;
265 like( $sql, qr/^SELECT tracks_2\.trackid/, "join not collapsed for search_related" );
267 $schema->storage->debug($orig_debug);
268 $schema->storage->debugobj->callback(undef);
270 $rs = $schema->resultset('Artist');
271 $rs->create({ artistid => 4, name => 'Unknown singer-songwriter' });
272 $rs->create({ artistid => 5, name => 'Emo 4ever' });
273 @artists = $rs->search(undef, { prefetch => 'cds', order_by => 'artistid' });
274 is(scalar @artists, 5, 'has_many prefetch with adjacent empty rows ok');
278 # Tests for multilevel has_many prefetch
280 # artist resultsets - with and without prefetch
281 my $art_rs = $schema->resultset('Artist');
282 my $art_rs_pr = $art_rs->search(
285 join => [ { cds => ['tracks'] } ],
286 prefetch => [ { cds => ['tracks'] } ],
287 cache => 1 # last test needs this
291 # This test does the same operation twice - once on a
292 # set of items fetched from the db with no prefetch of has_many rels
293 # The second prefetches 2 levels of has_many
294 # We check things are the same by comparing the name or title
295 # we build everything into a hash structure and compare the one
296 # from each rs to see what differs
298 sub make_hash_struc {
302 foreach my $art ( $rs->all ) {
303 foreach my $cd ( $art->cds ) {
304 foreach my $track ( $cd->tracks ) {
305 $struc->{ $art->name }{ $cd->title }{ $track->title }++;
313 $schema->storage->debugcb(sub { $queries++ });
314 $schema->storage->debug(1);
316 my $prefetch_result = make_hash_struc($art_rs_pr);
318 is($queries, 1, 'nested prefetch across has_many->has_many ran exactly 1 query');
320 my $nonpre_result = make_hash_struc($art_rs);
322 is_deeply( $prefetch_result, $nonpre_result,
323 'Compare 2 level prefetch result to non-prefetch result' );
327 is($art_rs_pr->search_related('cds')->search_related('tracks')->first->title,
329 'chained has_many->has_many search_related ok'
332 is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries');