9 my $schema = DBICTest->init_schema();
11 my $orig_debug = $schema->storage->debug;
16 eval "use DBD::SQLite";
18 ? ( skip_all => 'needs DBD::SQLite for testing' )
22 # figure out if we've got a version of sqlite that is older than 3.2.6, in
23 # which case COUNT(DISTINCT()) doesn't work
24 my $is_broken_sqlite = 0;
25 my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
26 split /\./, $schema->storage->dbh->get_info(18);
27 if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
28 ( ($sqlite_major_ver < 3) ||
29 ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
30 ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
31 $is_broken_sqlite = 1;
34 # test the abstract join => SQL generator
35 my $sa = new DBIC::SQL::Abstract;
38 { child => 'person' },
39 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
40 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
42 my $match = 'person child JOIN person father ON ( father.person_id = '
43 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
44 . '= child.mother_id )'
46 is( $sa->_recurse_from(@j), $match, 'join 1 ok' );
49 { mother => 'person' },
50 [ [ { child => 'person' },
51 [ { father => 'person' },
52 { 'father.person_id' => 'child.father_id' }
55 { 'mother.person_id' => 'child.mother_id' }
58 $match = 'person mother JOIN (person child JOIN person father ON ('
59 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
62 is( $sa->_recurse_from(@j2), $match, 'join 2 ok' );
65 { child => 'person' },
66 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
67 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
69 $match = 'person child INNER JOIN person father ON ( father.person_id = '
70 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
71 . '= child.mother_id )'
74 is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok');
77 { mother => 'person' },
78 [ [ { child => 'person', -join_type => 'left' },
79 [ { father => 'person', -join_type => 'right' },
80 { 'father.person_id' => 'child.father_id' }
83 { 'mother.person_id' => 'child.mother_id' }
86 $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
87 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
90 is( $sa->_recurse_from(@j4), $match, 'join 4 (nested joins + join types) ok');
93 { child => 'person' },
94 [ { father => 'person' }, { 'father.person_id' => \'!= child.father_id' }, ],
95 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
97 $match = 'person child JOIN person father ON ( father.person_id != '
98 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
99 . '= child.mother_id )'
101 is( $sa->_recurse_from(@j5), $match, 'join 5 (SCALAR reference for ON statement) ok' );
104 { child => 'person' },
105 [ { father => 'person' }, { 'father.person_id' => { '!=', '42' } }, ],
106 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
108 $match = qr/^HASH reference arguments are not supported in JOINS - try using "\.\.\." instead/;
109 eval { $sa->_recurse_from(@j6) };
110 like( $@, $match, 'join 6 (HASH reference for ON statement dies) ok' );
112 my $rs = $schema->resultset("CD")->search(
113 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
114 { from => [ { 'me' => 'cd' },
116 { artist => 'artist' },
117 { 'me.artist' => 'artist.artistid' }
121 cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
123 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
125 $rs = $schema->resultset("CD")->search(
126 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
127 { join => 'artist' });
129 cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
131 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
133 $rs = $schema->resultset("CD")->search(
134 { 'artist.name' => 'We Are Goth',
135 'liner_notes.notes' => 'Kill Yourself!' },
136 { join => [ qw/artist liner_notes/ ] });
138 cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
140 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
142 # when using join attribute, make sure slice()ing all objects has same count as all()
143 $rs = $schema->resultset("CD")->search(
145 { join => [qw/artist/], order_by => 'artist.name' }
147 cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
149 $rs = $schema->resultset("Artist")->search(
150 { 'liner_notes.notes' => 'Kill Yourself!' },
151 { join => { 'cds' => 'liner_notes' } });
153 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
155 is($rs->first->name, 'We Are Goth', 'Correct record returned');
157 # bug in 0.07000 caused attr (join/prefetch) to be modifed by search
158 # so we check the search & attr arrays are not modified
159 my $search = { 'artist.name' => 'Caterwauler McCrae' };
160 my $attr = { prefetch => [ qw/artist liner_notes/ ],
161 order_by => 'me.cdid' };
162 my $search_str = Dumper($search);
163 my $attr_str = Dumper($attr);
165 $rs = $schema->resultset("CD")->search($search, $attr);
167 is(Dumper($search), $search_str, 'Search hash untouched after search()');
168 is(Dumper($attr), $attr_str, 'Attribute hash untouched after search()');
169 cmp_ok($rs + 0, '==', 3, 'Correct number of records returned');
172 $schema->storage->debugcb(sub { $queries++; });
173 $schema->storage->debug(1);
177 is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
179 ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
181 is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
183 is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
185 is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
187 is($queries, 1, 'prefetch ran only 1 select statement');
189 $schema->storage->debug($orig_debug);
190 $schema->storage->debugobj->callback(undef);
192 # test for partial prefetch via columns attr
193 my $cd = $schema->resultset('CD')->find(1,
195 columns => [qw/title artist.name/],
196 join => { 'artist' => {} }
199 ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
201 # start test for nested prefetch SELECT count
203 $schema->storage->debugcb(sub { $queries++ });
204 $schema->storage->debug(1);
206 $rs = $schema->resultset('Tag')->search(
209 prefetch => { cd => 'artist' }
213 my $tag = $rs->first;
215 is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
217 is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
220 #$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
221 is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
225 is($tag->search_related('cd')->search_related('artist')->first->name,
226 'Caterwauler McCrae',
227 'chained belongs_to->belongs_to search_related ok');
229 is($queries, 0, 'chained search_related after belontgs_to->belongs_to prefetch ran no queries');
233 $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
235 is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
237 is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
241 $schema->storage->debugcb(sub { $queries++; });
243 $cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' } });
245 is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok');
248 local $TODO = 'use prefetched values for many_to_many accessor';
250 is($queries, 1, 'many_to_many accessor with nested prefetch ran exactly 1 query');
255 my $producers = $cd->search_related('cd_to_producer')->search_related('producer');
257 is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok');
259 is($queries, 0, 'chained search_related after many_to_many prefetch ran no queries');
261 $schema->storage->debug($orig_debug);
262 $schema->storage->debugobj->callback(undef);
264 $rs = $schema->resultset('Tag')->search(
267 join => { cd => 'artist' },
268 prefetch => { cd => 'artist' }
272 cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
274 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
275 { order_by => 'artistid DESC', join => 'cds' });
277 is($artist->name, 'Random Boy Band', "Join search by object ok");
279 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
280 { join => 'liner_notes' });
282 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
284 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
286 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
287 { join => { 'cds' => 'tags' } });
289 cmp_ok( @artists, '==', 2, "two-join search ok" );
291 $rs = $schema->resultset("CD")->search(
293 { group_by => [qw/ title me.cdid /] }
297 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
298 if $is_broken_sqlite;
299 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
302 cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
304 $rs = $schema->resultset("CD")->search(
306 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
310 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
311 if $is_broken_sqlite;
312 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
315 $rs = $schema->resultset("Artist")->search(
317 { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } }
320 cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
322 $rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }});
324 cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
326 $rs = $schema->resultset("Artist")->search(
327 { 'cds.title' => 'Spoonful of bees',
328 'cds_2.title' => 'Forkful of bees' },
329 { join => [ 'cds', 'cds' ] });
332 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
333 if $is_broken_sqlite;
334 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
337 is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
339 $cd = $schema->resultset('Artist')->first->create_related('cds',
341 title => 'Unproduced Single',
345 my $left_join = $schema->resultset('CD')->search(
346 { 'me.cdid' => $cd->cdid },
347 { prefetch => { cd_to_producer => 'producer' } }
350 cmp_ok($left_join, '==', 1, 'prefetch with no join record present');
353 $schema->storage->debugcb(sub { $queries++ });
354 $schema->storage->debug(1);
357 $schema->resultset('TreeLike')->find(4,
358 { join => { parent => { parent => 'parent' } },
359 prefetch => { parent => { parent => 'parent' } } });
361 is($tree_like->name, 'quux', 'Bottom of tree ok');
362 $tree_like = $tree_like->parent;
363 is($tree_like->name, 'baz', 'First level up ok');
364 $tree_like = $tree_like->parent;
365 is($tree_like->name, 'bar', 'Second level up ok');
366 $tree_like = $tree_like->parent;
367 is($tree_like->name, 'foo', 'Third level up ok');
369 $schema->storage->debug($orig_debug);
370 $schema->storage->debugobj->callback(undef);
372 cmp_ok($queries, '==', 1, 'Only one query run');
374 $tree_like = $schema->resultset('TreeLike')->search({'me.id' => 1});
375 $tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first;
376 is($tree_like->name, 'quux', 'Tree search_related ok');
378 $tree_like = $schema->resultset('TreeLike')->search_related('children',
379 { 'children.id' => 2, 'children_2.id' => 3 },
380 { prefetch => { children => 'children' } }
382 is(eval { $tree_like->children->first->children->first->name }, 'quux',
383 'Tree search_related with prefetch ok');
385 $tree_like = eval { $schema->resultset('TreeLike')->search(
386 { 'children.id' => 2, 'children_2.id' => 5 },
387 { join => [qw/children children/] }
388 )->search_related('children', { 'children_4.id' => 6 }, { prefetch => 'children' }
389 )->first->children->first; };
390 is(eval { $tree_like->name }, 'fong', 'Tree with multiple has_many joins ok');
392 # test that collapsed joins don't get a _2 appended to the alias
395 $schema->storage->debugcb(sub { $sql = $_[1] });
396 $schema->storage->debug(1);
399 my $row = $schema->resultset('Artist')->search_related('cds', undef, {
401 prefetch => 'tracks',
402 })->search_related('tracks')->first;
405 like( $sql, qr/^SELECT tracks_2\.trackid/, "join not collapsed for search_related" );
407 $schema->storage->debug($orig_debug);
408 $schema->storage->debugobj->callback(undef);
410 $rs = $schema->resultset('Artist');
411 $rs->create({ artistid => 4, name => 'Unknown singer-songwriter' });
412 $rs->create({ artistid => 5, name => 'Emo 4ever' });
413 @artists = $rs->search(undef, { prefetch => 'cds', order_by => 'artistid' });
414 is(scalar @artists, 5, 'has_many prefetch with adjacent empty rows ok');
418 # Tests for multilevel has_many prefetch
420 # artist resultsets - with and without prefetch
421 my $art_rs = $schema->resultset('Artist');
422 my $art_rs_pr = $art_rs->search(
425 join => [ { cds => ['tracks'] } ],
426 prefetch => [ { cds => ['tracks'] } ],
427 cache => 1 # last test needs this
431 # This test does the same operation twice - once on a
432 # set of items fetched from the db with no prefetch of has_many rels
433 # The second prefetches 2 levels of has_many
434 # We check things are the same by comparing the name or title
435 # we build everything into a hash structure and compare the one
436 # from each rs to see what differs
438 sub make_hash_struc {
442 foreach my $art ( $rs->all ) {
443 foreach my $cd ( $art->cds ) {
444 foreach my $track ( $cd->tracks ) {
445 $struc->{ $art->name }{ $cd->title }{ $track->title }++;
453 $schema->storage->debugcb(sub { $queries++ });
454 $schema->storage->debug(1);
456 my $prefetch_result = make_hash_struc($art_rs_pr);
458 is($queries, 1, 'nested prefetch across has_many->has_many ran exactly 1 query');
460 my $nonpre_result = make_hash_struc($art_rs);
462 is_deeply( $prefetch_result, $nonpre_result,
463 'Compare 2 level prefetch result to non-prefetch result' );
467 is($art_rs_pr->search_related('cds')->search_related('tracks')->first->title,
469 'chained has_many->has_many search_related ok'
472 is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries');