8 eval "use DBD::SQLite";
10 ? ( skip_all => 'needs DBD::SQLite for testing' )
14 my $is_broken_sqlite = version->new($schema->storage->dbh->get_info(18)) lt '3.2.6' &&
15 $schema->storage->dbh->get_info(17) eq 'SQLite';
17 # test the abstract join => SQL generator
18 my $sa = new DBIC::SQL::Abstract;
21 { child => 'person' },
22 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
23 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
25 my $match = 'person child JOIN person father ON ( father.person_id = '
26 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
27 . '= child.mother_id )'
29 is( $sa->_recurse_from(@j), $match, 'join 1 ok' );
32 { mother => 'person' },
33 [ [ { child => 'person' },
34 [ { father => 'person' },
35 { 'father.person_id' => 'child.father_id' }
38 { 'mother.person_id' => 'child.mother_id' }
41 $match = 'person mother JOIN (person child JOIN person father ON ('
42 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
45 is( $sa->_recurse_from(@j2), $match, 'join 2 ok' );
48 { child => 'person' },
49 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
50 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
52 $match = 'person child INNER JOIN person father ON ( father.person_id = '
53 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
54 . '= child.mother_id )'
57 is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok');
59 my $rs = $schema->resultset("CD")->search(
60 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
61 { from => [ { 'me' => 'cd' },
63 { artist => 'artist' },
64 { 'me.artist' => 'artist.artistid' }
68 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
70 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
72 $rs = $schema->resultset("CD")->search(
73 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
74 { join => 'artist' });
76 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
78 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
80 $rs = $schema->resultset("CD")->search(
81 { 'artist.name' => 'We Are Goth',
82 'liner_notes.notes' => 'Kill Yourself!' },
83 { join => [ qw/artist liner_notes/ ] });
85 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
87 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
89 # when using join attribute, make sure slice()ing all objects has same count as all()
90 $rs = $schema->resultset("CD")->search(
92 { join => [qw/artist/], order_by => 'artist.name' }
94 cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
96 $rs = $schema->resultset("Artist")->search(
97 { 'liner_notes.notes' => 'Kill Yourself!' },
98 { join => { 'cds' => 'liner_notes' } });
100 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
102 is($rs->first->name, 'We Are Goth', 'Correct record returned');
104 $rs = $schema->resultset("CD")->search(
105 { 'artist.name' => 'Caterwauler McCrae' },
106 { prefetch => [ qw/artist liner_notes/ ],
107 order_by => 'me.cdid' });
109 cmp_ok($rs->count, '==', 3, 'Correct number of records returned');
111 # start test for prefetch SELECT count
112 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
113 DBI->trace(1, 't/var/dbic.trace');
117 is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
119 ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
121 is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
123 is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
125 is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
128 DBI->trace(0, undef);
130 my $trace = IO::File->new('t/var/dbic.trace', '<')
131 or die "Unable to read trace file";
133 $selects++ if /SELECT/;
136 unlink 't/var/dbic.trace';
137 is($selects, 1, 'prefetch ran only 1 select statement');
139 # test for partial prefetch via cols attr
140 my $cd = $schema->resultset('CD')->find(1,
142 cols => [qw/title artist.name/],
146 ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
148 # start test for nested prefetch SELECT count
149 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
150 DBI->trace(1, 't/var/dbic.trace');
152 $rs = $schema->resultset('Tag')->search(
155 prefetch => { cd => 'artist' }
159 my $tag = $rs->first;
161 is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
163 is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
166 DBI->trace(0, undef);
168 $trace = IO::File->new('t/var/dbic.trace', '<')
169 or die "Unable to read trace file";
171 $selects++ if /SELECT(?!.*WHERE 1=0.*)/;
174 unlink 't/var/dbic.trace';
175 is($selects, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
177 # start test for prefetch on find SELECT count
178 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
179 DBI->trace(1, 't/var/dbic.trace');
181 $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
183 is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
186 DBI->trace(0, undef);
188 $trace = IO::File->new('t/var/dbic.trace', '<')
189 or die "Unable to read trace file";
191 $selects++ if /SELECT(?!.*WHERE 1=0.*)/;
194 unlink 't/var/dbic.trace';
195 is($selects, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
197 $rs = $schema->resultset('Tag')->search(
200 join => { cd => 'artist' },
201 prefetch => { cd => 'artist' }
205 cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
207 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
208 { order_by => 'artistid DESC', join => 'cds' });
210 is($artist->name, 'Random Boy Band', "Join search by object ok");
212 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
213 { join => 'liner_notes' });
215 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
217 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
219 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
220 { join => { 'cds' => 'tags' } });
222 cmp_ok( @artists, '==', 2, "two-join search ok" );
224 $rs = $schema->resultset("CD")->search(
226 { group_by => [qw/ title me.cdid /] }
230 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
231 if $is_broken_sqlite;
232 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
235 cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
237 $rs = $schema->resultset("CD")->search(
239 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
243 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
244 if $is_broken_sqlite;
245 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
248 cmp_ok( scalar $rs->all, '==', 3, "all() returns same count as count() after group_by on related column" );
250 $rs = $schema->resultset("Artist")->search(
251 { 'cds.title' => 'Spoonful of bees',
252 'cds_2.title' => 'Forkful of bees' },
253 { join => [ 'cds', 'cds' ] });
256 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
257 if $is_broken_sqlite;
258 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
261 is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
264 $schema->storage->debugcb(sub { $queries++ });
265 $schema->storage->debug(1);
268 $schema->resultset('TreeLike')->find(4,
269 { join => { parent => { parent => 'parent' } },
270 prefetch => { parent => { parent => 'parent' } } });
272 is($tree_like->name, 'quux', 'Bottom of tree ok');
273 $tree_like = $tree_like->parent;
274 is($tree_like->name, 'baz', 'First level up ok');
275 $tree_like = $tree_like->parent;
276 is($tree_like->name, 'bar', 'Second level up ok');
277 $tree_like = $tree_like->parent;
278 is($tree_like->name, 'foo', 'Third level up ok');
280 $schema->storage->debug(0);
282 cmp_ok($queries, '==', 1, 'Only one query run');