7 eval "use DBD::SQLite";
9 ? ( skip_all => 'needs DBD::SQLite for testing' )
13 # figure out if we've got a version of sqlite that is older than 3.2.6, in
14 # which case COUNT(DISTINCT()) doesn't work
15 my $is_broken_sqlite = 0;
16 my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
17 split /\./, $schema->storage->dbh->get_info(18);
18 if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
19 ( ($sqlite_major_ver < 3) ||
20 ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
21 ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
22 $is_broken_sqlite = 1;
25 # test the abstract join => SQL generator
26 my $sa = new DBIC::SQL::Abstract;
29 { child => 'person' },
30 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
31 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
33 my $match = 'person child JOIN person father ON ( father.person_id = '
34 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
35 . '= child.mother_id )'
37 is( $sa->_recurse_from(@j), $match, 'join 1 ok' );
40 { mother => 'person' },
41 [ [ { child => 'person' },
42 [ { father => 'person' },
43 { 'father.person_id' => 'child.father_id' }
46 { 'mother.person_id' => 'child.mother_id' }
49 $match = 'person mother JOIN (person child JOIN person father ON ('
50 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
53 is( $sa->_recurse_from(@j2), $match, 'join 2 ok' );
56 { child => 'person' },
57 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
58 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
60 $match = 'person child INNER JOIN person father ON ( father.person_id = '
61 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
62 . '= child.mother_id )'
65 is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok');
67 my $rs = $schema->resultset("CD")->search(
68 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
69 { from => [ { 'me' => 'cd' },
71 { artist => 'artist' },
72 { 'me.artist' => 'artist.artistid' }
76 cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
78 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
80 $rs = $schema->resultset("CD")->search(
81 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
82 { join => 'artist' });
84 cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
86 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
88 $rs = $schema->resultset("CD")->search(
89 { 'artist.name' => 'We Are Goth',
90 'liner_notes.notes' => 'Kill Yourself!' },
91 { join => [ qw/artist liner_notes/ ] });
93 cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
95 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
97 # when using join attribute, make sure slice()ing all objects has same count as all()
98 $rs = $schema->resultset("CD")->search(
100 { join => [qw/artist/], order_by => 'artist.name' }
102 cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
104 $rs = $schema->resultset("Artist")->search(
105 { 'liner_notes.notes' => 'Kill Yourself!' },
106 { join => { 'cds' => 'liner_notes' } });
108 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
110 is($rs->first->name, 'We Are Goth', 'Correct record returned');
112 $rs = $schema->resultset("CD")->search(
113 { 'artist.name' => 'Caterwauler McCrae' },
114 { prefetch => [ qw/artist liner_notes/ ],
115 order_by => 'me.cdid' });
117 cmp_ok($rs + 0, '==', 3, 'Correct number of records returned');
119 # start test for prefetch SELECT count
120 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
121 DBI->trace(1, 't/var/dbic.trace');
125 is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
127 ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
129 is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
131 is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
133 is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
136 DBI->trace(0, undef);
138 my $trace = IO::File->new('t/var/dbic.trace', '<')
139 or die "Unable to read trace file";
141 $selects++ if /SELECT/;
144 unlink 't/var/dbic.trace';
145 is($selects, 1, 'prefetch ran only 1 select statement');
147 # test for partial prefetch via cols attr
148 my $cd = $schema->resultset('CD')->find(1,
150 cols => [qw/title artist.name/],
154 ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
156 # start test for nested prefetch SELECT count
157 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
158 DBI->trace(1, 't/var/dbic.trace');
160 $rs = $schema->resultset('Tag')->search(
163 prefetch => { cd => 'artist' }
167 my $tag = $rs->first;
169 is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
171 is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
174 DBI->trace(0, undef);
176 $trace = IO::File->new('t/var/dbic.trace', '<')
177 or die "Unable to read trace file";
179 $selects++ if /SELECT(?!.*WHERE 1=0.*)/;
182 unlink 't/var/dbic.trace';
183 is($selects, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
185 # start test for prefetch on find SELECT count
186 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
187 DBI->trace(1, 't/var/dbic.trace');
189 $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
191 is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
194 DBI->trace(0, undef);
196 $trace = IO::File->new('t/var/dbic.trace', '<')
197 or die "Unable to read trace file";
199 $selects++ if /SELECT(?!.*WHERE 1=0.*)/;
202 unlink 't/var/dbic.trace';
203 is($selects, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
205 $rs = $schema->resultset('Tag')->search(
208 join => { cd => 'artist' },
209 prefetch => { cd => 'artist' }
213 cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
215 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
216 { order_by => 'artistid DESC', join => 'cds' });
218 is($artist->name, 'Random Boy Band', "Join search by object ok");
220 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
221 { join => 'liner_notes' });
223 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
225 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
227 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
228 { join => { 'cds' => 'tags' } });
230 cmp_ok( @artists, '==', 2, "two-join search ok" );
232 $rs = $schema->resultset("CD")->search(
234 { group_by => [qw/ title me.cdid /] }
238 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
239 if $is_broken_sqlite;
240 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
243 cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
245 $rs = $schema->resultset("CD")->search(
247 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
251 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
252 if $is_broken_sqlite;
253 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
256 cmp_ok( scalar $rs->all, '==', 3, "all() returns same count as count() after group_by on related column" );
258 $rs = $schema->resultset("Artist")->search(
259 { 'cds.title' => 'Spoonful of bees',
260 'cds_2.title' => 'Forkful of bees' },
261 { join => [ 'cds', 'cds' ] });
264 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
265 if $is_broken_sqlite;
266 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
269 is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
272 $schema->storage->debugcb(sub { $queries++ });
273 $schema->storage->debug(1);
276 $schema->resultset('TreeLike')->find(4,
277 { join => { parent => { parent => 'parent' } },
278 prefetch => { parent => { parent => 'parent' } } });
280 is($tree_like->name, 'quux', 'Bottom of tree ok');
281 $tree_like = $tree_like->parent;
282 is($tree_like->name, 'baz', 'First level up ok');
283 $tree_like = $tree_like->parent;
284 is($tree_like->name, 'bar', 'Second level up ok');
285 $tree_like = $tree_like->parent;
286 is($tree_like->name, 'foo', 'Third level up ok');
288 $schema->storage->debug(0);
290 cmp_ok($queries, '==', 1, 'Only one query run');