7 use DBIC::SqlMakerTest;
9 my $schema = DBICTest->init_schema();
11 my $orig_debug = $schema->storage->debug;
16 eval "use DBD::SQLite";
18 ? ( skip_all => 'needs DBD::SQLite for testing' )
22 # test the abstract join => SQL generator
23 my $sa = new DBIx::Class::SQLAHacks;
26 { child => 'person' },
27 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
28 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
30 my $match = 'person child JOIN person father ON ( father.person_id = '
31 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
32 . '= child.mother_id )'
35 $sa->_recurse_from(@j),
41 { mother => 'person' },
42 [ [ { child => 'person' },
43 [ { father => 'person' },
44 { 'father.person_id' => 'child.father_id' }
47 { 'mother.person_id' => 'child.mother_id' }
50 $match = 'person mother JOIN (person child JOIN person father ON ('
51 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
55 $sa->_recurse_from(@j2),
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 )'
72 $sa->_recurse_from(@j3),
74 'join 3 (inner join) ok'
78 { mother => 'person' },
79 [ [ { child => 'person', -join_type => 'left' },
80 [ { father => 'person', -join_type => 'right' },
81 { 'father.person_id' => 'child.father_id' }
84 { 'mother.person_id' => 'child.mother_id' }
87 $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
88 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
92 $sa->_recurse_from(@j4),
94 'join 4 (nested joins + join types) ok'
98 { child => 'person' },
99 [ { father => 'person' }, { 'father.person_id' => \'!= child.father_id' }, ],
100 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
102 $match = 'person child JOIN person father ON ( father.person_id != '
103 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
104 . '= child.mother_id )'
107 $sa->_recurse_from(@j5),
109 'join 5 (SCALAR reference for ON statement) ok'
113 { child => 'person' },
114 [ { father => 'person' }, { 'father.person_id' => { '!=', '42' } }, ],
115 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
117 $match = qr/HASH reference arguments are not supported in JOINS/;
118 eval { $sa->_recurse_from(@j6) };
119 like( $@, $match, 'join 6 (HASH reference for ON statement dies) ok' );
121 my $rs = $schema->resultset("CD")->search(
122 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
123 { from => [ { 'me' => 'cd' },
125 { artist => 'artist' },
126 { 'me.artist' => 'artist.artistid' }
130 is( $rs + 0, 1, "Single record in resultset");
132 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
134 $rs = $schema->resultset("CD")->search(
135 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
136 { join => 'artist' });
138 is( $rs + 0, 1, "Single record in resultset");
140 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
142 $rs = $schema->resultset("CD")->search(
143 { 'artist.name' => 'We Are Goth',
144 'liner_notes.notes' => 'Kill Yourself!' },
145 { join => [ qw/artist liner_notes/ ] });
147 is( $rs + 0, 1, "Single record in resultset");
149 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
151 # when using join attribute, make sure slice()ing all objects has same count as all()
152 $rs = $schema->resultset("CD")->search(
154 { join => [qw/artist/], order_by => 'artist.name' }
156 is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
158 ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
159 'Slicing beyond end of rs returns a zero count');
161 $rs = $schema->resultset("Artist")->search(
162 { 'liner_notes.notes' => 'Kill Yourself!' },
163 { join => { 'cds' => 'liner_notes' } });
165 is( $rs->count, 1, "Single record in resultset");
167 is($rs->first->name, 'We Are Goth', 'Correct record returned');
171 $schema->populate('Artist', [
172 [ qw/artistid name/ ],
173 [ 4, 'Another Boy Band' ],
175 $schema->populate('CD', [
176 [ qw/cdid artist title year/ ],
177 [ 6, 2, "Greatest Hits", 2001 ],
178 [ 7, 4, "Greatest Hits", 2005 ],
179 [ 8, 4, "BoyBandBlues", 2008 ],
181 $schema->populate('TwoKeys', [
190 return $schema->resultset("CD")->count;
193 return $schema->resultset("TwoKeys")->count;
196 is(cd_count(), 8, '8 rows in table cd');
197 is(tk_count(), 7, '7 rows in table twokeys');
200 return $schema->resultset("CD")->search(
201 { 'artist.name' => 'Caterwauler McCrae' },
202 { join => [qw/artist/]}
206 return $schema->resultset("CD")->search(
207 { 'artist.name' => 'Random Boy Band' },
208 { join => [qw/artist/]}
212 is( artist1()->count, 3, '3 Caterwauler McCrae CDs' );
213 ok( artist1()->delete, 'Successfully deleted 3 CDs' );
214 is( artist1()->count, 0, '0 Caterwauler McCrae CDs' );
215 is( artist2()->count, 2, '3 Random Boy Band CDs' );
216 ok( artist2()->update( { 'artist' => 1 } ) );
217 is( artist2()->count, 0, '0 Random Boy Band CDs' );
218 is( artist1()->count, 2, '2 Caterwauler McCrae CDs' );
220 # test update on multi-column-pk
222 return $schema->resultset("TwoKeys")->search(
224 'artist.name' => { like => '%Boy Band' },
225 'cd.title' => 'Greatest Hits',
227 { join => [qw/artist cd/] }
231 return $schema->resultset("TwoKeys")->search(
232 { 'artist.name' => 'Caterwauler McCrae' },
233 { join => [qw/artist/]}
236 is( tk2()->count, 2, 'TwoKeys count == 2' );
237 is( tk1()->count, 2, 'TwoKeys count == 2' );
238 ok( tk1()->update( { artist => 1 } ) );
239 is( tk1()->count, 0, 'TwoKeys count == 0' );
240 is( tk2()->count, 4, '2 Caterwauler McCrae CDs' );
241 ok( tk2()->delete, 'Successfully deleted 4 CDs' );
242 is(cd_count(), 5, '5 rows in table cd');
243 is(tk_count(), 3, '3 rows in table twokeys');