8 use DBIC::SqlMakerTest;
10 my $schema = DBICTest->init_schema();
12 my $orig_debug = $schema->storage->debug;
17 eval "use DBD::SQLite";
19 ? ( skip_all => 'needs DBD::SQLite for testing' )
23 # figure out if we've got a version of sqlite that is older than 3.2.6, in
24 # which case COUNT(DISTINCT()) doesn't work
25 my $is_broken_sqlite = 0;
26 my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
27 split /\./, $schema->storage->dbh->get_info(18);
28 if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
29 ( ($sqlite_major_ver < 3) ||
30 ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
31 ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
32 $is_broken_sqlite = 1;
35 # test the abstract join => SQL generator
36 my $sa = new DBIC::SQL::Abstract;
39 { child => 'person' },
40 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
41 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
43 my $match = 'person child JOIN person father ON ( father.person_id = '
44 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
45 . '= child.mother_id )'
48 $sa->_recurse_from(@j), [],
54 { mother => 'person' },
55 [ [ { child => 'person' },
56 [ { father => 'person' },
57 { 'father.person_id' => 'child.father_id' }
60 { 'mother.person_id' => 'child.mother_id' }
63 $match = 'person mother JOIN (person child JOIN person father ON ('
64 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
68 $sa->_recurse_from(@j2), [],
75 { child => 'person' },
76 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
77 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
79 $match = 'person child INNER JOIN person father ON ( father.person_id = '
80 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
81 . '= child.mother_id )'
85 $sa->_recurse_from(@j3), [],
87 'join 3 (inner join) ok'
91 { mother => 'person' },
92 [ [ { child => 'person', -join_type => 'left' },
93 [ { father => 'person', -join_type => 'right' },
94 { 'father.person_id' => 'child.father_id' }
97 { 'mother.person_id' => 'child.mother_id' }
100 $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
101 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
102 . 'child.mother_id )'
105 $sa->_recurse_from(@j4), [],
107 'join 4 (nested joins + join types) ok'
111 { child => 'person' },
112 [ { father => 'person' }, { 'father.person_id' => \'!= child.father_id' }, ],
113 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
115 $match = 'person child JOIN person father ON ( father.person_id != '
116 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
117 . '= child.mother_id )'
120 $sa->_recurse_from(@j5), [],
122 'join 5 (SCALAR reference for ON statement) ok'
126 { child => 'person' },
127 [ { father => 'person' }, { 'father.person_id' => { '!=', '42' } }, ],
128 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
130 $match = qr/^HASH reference arguments are not supported in JOINS - try using "\.\.\." instead/;
131 eval { $sa->_recurse_from(@j6) };
132 like( $@, $match, 'join 6 (HASH reference for ON statement dies) ok' );
134 my $rs = $schema->resultset("CD")->search(
135 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
136 { from => [ { 'me' => 'cd' },
138 { artist => 'artist' },
139 { 'me.artist' => 'artist.artistid' }
143 is( $rs + 0, 1, "Single record in resultset");
145 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
147 $rs = $schema->resultset("CD")->search(
148 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
149 { join => 'artist' });
151 is( $rs + 0, 1, "Single record in resultset");
153 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
155 $rs = $schema->resultset("CD")->search(
156 { 'artist.name' => 'We Are Goth',
157 'liner_notes.notes' => 'Kill Yourself!' },
158 { join => [ qw/artist liner_notes/ ] });
160 is( $rs + 0, 1, "Single record in resultset");
162 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
164 # when using join attribute, make sure slice()ing all objects has same count as all()
165 $rs = $schema->resultset("CD")->search(
167 { join => [qw/artist/], order_by => 'artist.name' }
169 is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
171 ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
172 'Slicing beyond end of rs returns a zero count');
174 $rs = $schema->resultset("Artist")->search(
175 { 'liner_notes.notes' => 'Kill Yourself!' },
176 { join => { 'cds' => 'liner_notes' } });
178 is( $rs->count, 1, "Single record in resultset");
180 is($rs->first->name, 'We Are Goth', 'Correct record returned');
184 $schema->populate('Artist', [
185 [ qw/artistid name/ ],
186 [ 4, 'Another Boy Band' ],
188 $schema->populate('CD', [
189 [ qw/cdid artist title year/ ],
190 [ 6, 2, "Greatest Hits", 2001 ],
191 [ 7, 4, "Greatest Hits", 2005 ],
192 [ 8, 4, "BoyBandBlues", 2008 ],
194 $schema->populate('TwoKeys', [
203 return $schema->resultset("CD")->count;
206 return $schema->resultset("TwoKeys")->count;
209 is(cd_count(), 8, '8 rows in table cd');
210 is(tk_count(), 7, '7 rows in table twokeys');
213 return $schema->resultset("CD")->search(
214 { 'artist.name' => 'Caterwauler McCrae' },
215 { join => [qw/artist/]}
219 return $schema->resultset("CD")->search(
220 { 'artist.name' => 'Random Boy Band' },
221 { join => [qw/artist/]}
225 is( artist1()->count, 3, '3 Caterwauler McCrae CDs' );
226 ok( artist1()->delete, 'Successfully deleted 3 CDs' );
227 is( artist1()->count, 0, '0 Caterwauler McCrae CDs' );
228 is( artist2()->count, 2, '3 Random Boy Band CDs' );
229 ok( artist2()->update( { 'artist' => 1 } ) );
230 is( artist2()->count, 0, '0 Random Boy Band CDs' );
231 is( artist1()->count, 2, '2 Caterwauler McCrae CDs' );
233 # test update on multi-column-pk
235 return $schema->resultset("TwoKeys")->search(
237 'artist.name' => { like => '%Boy Band' },
238 'cd.title' => 'Greatest Hits',
240 { join => [qw/artist cd/] }
244 return $schema->resultset("TwoKeys")->search(
245 { 'artist.name' => 'Caterwauler McCrae' },
246 { join => [qw/artist/]}
249 is( tk2()->count, 2, 'TwoKeys count == 2' );
250 is( tk1()->count, 2, 'TwoKeys count == 2' );
251 ok( tk1()->update( { artist => 1 } ) );
252 is( tk1()->count, 0, 'TwoKeys count == 0' );
253 is( tk2()->count, 4, '2 Caterwauler McCrae CDs' );
254 ok( tk2()->delete, 'Successfully deleted 4 CDs' );
255 is(cd_count(), 5, '5 rows in table cd');
256 is(tk_count(), 3, '3 rows in table twokeys');