8 my $schema = DBICTest::init_schema();
13 eval "use DBD::SQLite";
15 ? ( skip_all => 'needs DBD::SQLite for testing' )
19 # figure out if we've got a version of sqlite that is older than 3.2.6, in
20 # which case COUNT(DISTINCT()) doesn't work
21 my $is_broken_sqlite = 0;
22 my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
23 split /\./, $schema->storage->dbh->get_info(18);
24 if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
25 ( ($sqlite_major_ver < 3) ||
26 ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
27 ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
28 $is_broken_sqlite = 1;
31 # test the abstract join => SQL generator
32 my $sa = new DBIC::SQL::Abstract;
35 { child => 'person' },
36 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
37 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
39 my $match = 'person child JOIN person father ON ( father.person_id = '
40 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
41 . '= child.mother_id )'
43 is( $sa->_recurse_from(@j), $match, 'join 1 ok' );
46 { mother => 'person' },
47 [ [ { child => 'person' },
48 [ { father => 'person' },
49 { 'father.person_id' => 'child.father_id' }
52 { 'mother.person_id' => 'child.mother_id' }
55 $match = 'person mother JOIN (person child JOIN person father ON ('
56 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
59 is( $sa->_recurse_from(@j2), $match, 'join 2 ok' );
62 { child => 'person' },
63 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
64 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
66 $match = 'person child INNER JOIN person father ON ( father.person_id = '
67 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
68 . '= child.mother_id )'
71 is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok');
73 my $rs = $schema->resultset("CD")->search(
74 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
75 { from => [ { 'me' => 'cd' },
77 { artist => 'artist' },
78 { 'me.artist' => 'artist.artistid' }
82 cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
84 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
86 $rs = $schema->resultset("CD")->search(
87 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
88 { join => 'artist' });
90 cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
92 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
94 $rs = $schema->resultset("CD")->search(
95 { 'artist.name' => 'We Are Goth',
96 'liner_notes.notes' => 'Kill Yourself!' },
97 { join => [ qw/artist liner_notes/ ] });
99 cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
101 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
103 # when using join attribute, make sure slice()ing all objects has same count as all()
104 $rs = $schema->resultset("CD")->search(
106 { join => [qw/artist/], order_by => 'artist.name' }
108 cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
110 eval { $rs->search(undef, { rows => 0, offset => 3 })->all; };
112 ok($@, "rows => 0 errors: $@");
114 $rs = $schema->resultset("Artist")->search(
115 { 'liner_notes.notes' => 'Kill Yourself!' },
116 { join => { 'cds' => 'liner_notes' } });
118 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
120 is($rs->first->name, 'We Are Goth', 'Correct record returned');
122 $rs = $schema->resultset("CD")->search(
123 { 'artist.name' => 'Caterwauler McCrae' },
124 { prefetch => [ qw/artist liner_notes/ ],
125 order_by => 'me.cdid' });
127 cmp_ok($rs + 0, '==', 3, 'Correct number of records returned');
130 $schema->storage->debugcb(sub { $queries++ });
133 $schema->storage->debug(1);
137 is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
139 ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
141 is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
143 is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
145 is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
147 is($queries, 1, 'prefetch ran only 1 select statement');
149 $schema->storage->debug(0);
151 # test for partial prefetch via columns attr
152 my $cd = $schema->resultset('CD')->find(1,
154 columns => [qw/title artist.name/],
155 join => { 'artist' => {} }
158 ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
160 # start test for nested prefetch SELECT count
162 $schema->storage->debug(1);
164 $rs = $schema->resultset('Tag')->search(
167 prefetch => { cd => 'artist' }
171 my $tag = $rs->first;
173 is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
175 is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
178 #$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
179 is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
183 $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
185 is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
187 is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
189 $schema->storage->debug(0);
191 $rs = $schema->resultset('Tag')->search(
194 join => { cd => 'artist' },
195 prefetch => { cd => 'artist' }
199 cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
201 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
202 { order_by => 'artistid DESC', join => 'cds' });
204 is($artist->name, 'Random Boy Band', "Join search by object ok");
206 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
207 { join => 'liner_notes' });
209 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
211 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
213 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
214 { join => { 'cds' => 'tags' } });
216 cmp_ok( @artists, '==', 2, "two-join search ok" );
218 $rs = $schema->resultset("CD")->search(
220 { group_by => [qw/ title me.cdid /] }
224 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
225 if $is_broken_sqlite;
226 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
229 cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
231 $rs = $schema->resultset("CD")->search(
233 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
237 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
238 if $is_broken_sqlite;
239 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
242 $rs = $schema->resultset("Artist")->search(
244 { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } }
247 cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
249 $rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }});
251 cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
253 $rs = $schema->resultset("Artist")->search(
254 { 'cds.title' => 'Spoonful of bees',
255 'cds_2.title' => 'Forkful of bees' },
256 { join => [ 'cds', 'cds' ] });
259 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
260 if $is_broken_sqlite;
261 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
264 is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
267 $schema->storage->debug(1);
270 $schema->resultset('TreeLike')->find(4,
271 { join => { parent => { parent => 'parent' } },
272 prefetch => { parent => { parent => 'parent' } } });
274 is($tree_like->name, 'quux', 'Bottom of tree ok');
275 $tree_like = $tree_like->parent;
276 is($tree_like->name, 'baz', 'First level up ok');
277 $tree_like = $tree_like->parent;
278 is($tree_like->name, 'bar', 'Second level up ok');
279 $tree_like = $tree_like->parent;
280 is($tree_like->name, 'foo', 'Third level up ok');
282 $schema->storage->debug(0);
284 cmp_ok($queries, '==', 1, 'Only one query run');
286 # has_many resulting in an additional select if no records available despite prefetch
287 my $track = $schema->resultset("Artist")->create( {
289 name => 'Artist without CDs',
293 $schema->storage->debug(1);
295 my $artist_without_cds = $schema->resultset("Artist")->find(4, {
297 prefetch => [qw/ cds /],
299 my @no_cds = $artist_without_cds->cds;
301 is($queries, 1, 'prefetch ran only 1 sql statement');
303 $schema->storage->debug(0);