8 my $schema = DBICTest->init_schema();
10 my $orig_debug = $schema->storage->debug;
15 eval "use DBD::SQLite";
17 ? ( skip_all => 'needs DBD::SQLite for testing' )
21 # figure out if we've got a version of sqlite that is older than 3.2.6, in
22 # which case COUNT(DISTINCT()) doesn't work
23 my $is_broken_sqlite = 0;
24 my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
25 split /\./, $schema->storage->dbh->get_info(18);
26 if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
27 ( ($sqlite_major_ver < 3) ||
28 ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
29 ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
30 $is_broken_sqlite = 1;
33 # test the abstract join => SQL generator
34 my $sa = new DBIC::SQL::Abstract;
37 { child => 'person' },
38 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
39 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
41 my $match = 'person child JOIN person father ON ( father.person_id = '
42 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
43 . '= child.mother_id )'
45 is( $sa->_recurse_from(@j), $match, 'join 1 ok' );
48 { mother => 'person' },
49 [ [ { child => 'person' },
50 [ { father => 'person' },
51 { 'father.person_id' => 'child.father_id' }
54 { 'mother.person_id' => 'child.mother_id' }
57 $match = 'person mother JOIN (person child JOIN person father ON ('
58 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
61 is( $sa->_recurse_from(@j2), $match, 'join 2 ok' );
64 { child => 'person' },
65 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
66 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
68 $match = 'person child INNER JOIN person father ON ( father.person_id = '
69 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
70 . '= child.mother_id )'
73 is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok');
76 { mother => 'person' },
77 [ [ { child => 'person', -join_type => 'left' },
78 [ { father => 'person', -join_type => 'right' },
79 { 'father.person_id' => 'child.father_id' }
82 { 'mother.person_id' => 'child.mother_id' }
85 $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
86 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
89 is( $sa->_recurse_from(@j4), $match, 'join 4 (nested joins + join types) ok');
91 my $rs = $schema->resultset("CD")->search(
92 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
93 { from => [ { 'me' => 'cd' },
95 { artist => 'artist' },
96 { 'me.artist' => 'artist.artistid' }
100 cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
102 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
104 $rs = $schema->resultset("CD")->search(
105 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
106 { join => 'artist' });
108 cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
110 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
112 $rs = $schema->resultset("CD")->search(
113 { 'artist.name' => 'We Are Goth',
114 'liner_notes.notes' => 'Kill Yourself!' },
115 { join => [ qw/artist liner_notes/ ] });
117 cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
119 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
121 # when using join attribute, make sure slice()ing all objects has same count as all()
122 $rs = $schema->resultset("CD")->search(
124 { join => [qw/artist/], order_by => 'artist.name' }
126 cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
128 $rs = $schema->resultset("Artist")->search(
129 { 'liner_notes.notes' => 'Kill Yourself!' },
130 { join => { 'cds' => 'liner_notes' } });
132 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
134 is($rs->first->name, 'We Are Goth', 'Correct record returned');
136 $rs = $schema->resultset("CD")->search(
137 { 'artist.name' => 'Caterwauler McCrae' },
138 { prefetch => [ qw/artist liner_notes/ ],
139 order_by => 'me.cdid' });
141 cmp_ok($rs + 0, '==', 3, 'Correct number of records returned');
144 $schema->storage->debugcb(sub { $queries++ });
147 $schema->storage->debug(1);
151 is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
153 ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
155 is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
157 is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
159 is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
161 is($queries, 1, 'prefetch ran only 1 select statement');
163 $schema->storage->debug($orig_debug);
164 $schema->storage->debugobj->callback(undef);
166 # test for partial prefetch via columns attr
167 my $cd = $schema->resultset('CD')->find(1,
169 columns => [qw/title artist.name/],
170 join => { 'artist' => {} }
173 ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
175 # start test for nested prefetch SELECT count
177 $schema->storage->debug(1);
179 $rs = $schema->resultset('Tag')->search(
182 prefetch => { cd => 'artist' }
186 my $tag = $rs->first;
188 is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
190 is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
193 #$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
194 is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
198 $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
200 is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
202 is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
204 $schema->storage->debug($orig_debug);
205 $schema->storage->debugobj->callback(undef);
207 $rs = $schema->resultset('Tag')->search(
210 join => { cd => 'artist' },
211 prefetch => { cd => 'artist' }
215 cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
217 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
218 { order_by => 'artistid DESC', join => 'cds' });
220 is($artist->name, 'Random Boy Band', "Join search by object ok");
222 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
223 { join => 'liner_notes' });
225 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
227 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
229 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
230 { join => { 'cds' => 'tags' } });
232 cmp_ok( @artists, '==', 2, "two-join search ok" );
234 $rs = $schema->resultset("CD")->search(
236 { group_by => [qw/ title me.cdid /] }
240 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
241 if $is_broken_sqlite;
242 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
245 cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
247 $rs = $schema->resultset("CD")->search(
249 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
253 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
254 if $is_broken_sqlite;
255 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
258 $rs = $schema->resultset("Artist")->search(
260 { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } }
263 cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
265 $rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }});
267 cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
269 $rs = $schema->resultset("Artist")->search(
270 { 'cds.title' => 'Spoonful of bees',
271 'cds_2.title' => 'Forkful of bees' },
272 { join => [ 'cds', 'cds' ] });
275 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
276 if $is_broken_sqlite;
277 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
280 is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
283 $schema->storage->debug(1);
286 $schema->resultset('TreeLike')->find(4,
287 { join => { parent => { parent => 'parent' } },
288 prefetch => { parent => { parent => 'parent' } } });
290 is($tree_like->name, 'quux', 'Bottom of tree ok');
291 $tree_like = $tree_like->parent;
292 is($tree_like->name, 'baz', 'First level up ok');
293 $tree_like = $tree_like->parent;
294 is($tree_like->name, 'bar', 'Second level up ok');
295 $tree_like = $tree_like->parent;
296 is($tree_like->name, 'foo', 'Third level up ok');
298 $schema->storage->debug($orig_debug);
299 $schema->storage->debugobj->callback(undef);
301 cmp_ok($queries, '==', 1, 'Only one query run');
303 $tree_like = $schema->resultset('TreeLike')->search({'me.id' => 1});
304 $tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first;
305 is($tree_like->name, 'quux', 'Tree search_related ok');
307 $tree_like = $schema->resultset('TreeLike')->search({ 'children.id' => 2 });
308 $tree_like = $tree_like->search_related('children', undef, { prefetch => { children => 'children' } })->first;
309 is($tree_like->children->first->children->first->name, 'quux', 'Tree search_related with prefetch ok');
311 $tree_like = $schema->resultset('TreeLike')->search(
312 { 'children.id' => 2, 'children_2.id' => 5 },
313 { join => [qw/children children/] }
314 )->search_related('children', { 'children_3.id' => 3 }, { prefetch => 'children' }
315 )->first->children->first;
316 is($tree_like->name, 'baz', 'Tree with multiple has_many joins ok');
318 # test that collapsed joins don't get a _2 appended to the alias
321 $schema->storage->debugcb(sub { $sql = $_[1] });
322 $schema->storage->debug(1);
325 my $row = $schema->resultset('Artist')->search_related('cds', undef, {
327 prefetch => 'tracks',
328 })->search_related('tracks')->first;
331 like( $sql, qr/^SELECT tracks\.trackid/, "collapsed join didn't add _2 to alias" );
333 $schema->storage->debug($orig_debug);
334 $schema->storage->debugobj->callback(undef);