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 # test the abstract join => SQL generator
24 my $sa = new DBIx::Class::SQLAHacks;
27 { child => 'person' },
28 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
29 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
31 my $match = 'person child JOIN person father ON ( father.person_id = '
32 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
33 . '= child.mother_id )'
36 $sa->_recurse_from(@j),
42 { mother => 'person' },
43 [ [ { child => 'person' },
44 [ { father => 'person' },
45 { 'father.person_id' => 'child.father_id' }
48 { 'mother.person_id' => 'child.mother_id' }
51 $match = 'person mother JOIN (person child JOIN person father ON ('
52 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
56 $sa->_recurse_from(@j2),
63 { child => 'person' },
64 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
65 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
67 $match = 'person child INNER JOIN person father ON ( father.person_id = '
68 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
69 . '= child.mother_id )'
73 $sa->_recurse_from(@j3),
75 'join 3 (inner join) ok'
79 { mother => 'person' },
80 [ [ { child => 'person', -join_type => 'left' },
81 [ { father => 'person', -join_type => 'right' },
82 { 'father.person_id' => 'child.father_id' }
85 { 'mother.person_id' => 'child.mother_id' }
88 $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
89 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
93 $sa->_recurse_from(@j4),
95 'join 4 (nested joins + join types) ok'
99 { child => 'person' },
100 [ { father => 'person' }, { 'father.person_id' => \'!= child.father_id' }, ],
101 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
103 $match = 'person child JOIN person father ON ( father.person_id != '
104 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
105 . '= child.mother_id )'
108 $sa->_recurse_from(@j5),
110 'join 5 (SCALAR reference for ON statement) ok'
114 { child => 'person' },
115 [ { father => 'person' }, { 'father.person_id' => { '!=', '42' } }, ],
116 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
118 $match = qr/HASH reference arguments are not supported in JOINS/;
119 eval { $sa->_recurse_from(@j6) };
120 like( $@, $match, 'join 6 (HASH reference for ON statement dies) ok' );
122 my $rs = $schema->resultset("CD")->search(
123 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
124 { from => [ { 'me' => 'cd' },
126 { artist => 'artist' },
127 { 'me.artist' => 'artist.artistid' }
131 is( $rs + 0, 1, "Single record in resultset");
133 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
135 $rs = $schema->resultset("CD")->search(
136 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
137 { join => 'artist' });
139 is( $rs + 0, 1, "Single record in resultset");
141 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
143 $rs = $schema->resultset("CD")->search(
144 { 'artist.name' => 'We Are Goth',
145 'liner_notes.notes' => 'Kill Yourself!' },
146 { join => [ qw/artist liner_notes/ ] });
148 is( $rs + 0, 1, "Single record in resultset");
150 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
152 # when using join attribute, make sure slice()ing all objects has same count as all()
153 $rs = $schema->resultset("CD")->search(
155 { join => [qw/artist/], order_by => 'artist.name' }
157 is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
159 ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
160 'Slicing beyond end of rs returns a zero count');
162 $rs = $schema->resultset("Artist")->search(
163 { 'liner_notes.notes' => 'Kill Yourself!' },
164 { join => { 'cds' => 'liner_notes' } });
166 is( $rs->count, 1, "Single record in resultset");
168 is($rs->first->name, 'We Are Goth', 'Correct record returned');
172 $schema->populate('Artist', [
173 [ qw/artistid name/ ],
174 [ 4, 'Another Boy Band' ],
176 $schema->populate('CD', [
177 [ qw/cdid artist title year/ ],
178 [ 6, 2, "Greatest Hits", 2001 ],
179 [ 7, 4, "Greatest Hits", 2005 ],
180 [ 8, 4, "BoyBandBlues", 2008 ],
182 $schema->populate('TwoKeys', [
191 return $schema->resultset("CD")->count;
194 return $schema->resultset("TwoKeys")->count;
197 is(cd_count(), 8, '8 rows in table cd');
198 is(tk_count(), 7, '7 rows in table twokeys');
201 return $schema->resultset("CD")->search(
202 { 'artist.name' => 'Caterwauler McCrae' },
203 { join => [qw/artist/]}
207 return $schema->resultset("CD")->search(
208 { 'artist.name' => 'Random Boy Band' },
209 { join => [qw/artist/]}
213 is( artist1()->count, 3, '3 Caterwauler McCrae CDs' );
214 ok( artist1()->delete, 'Successfully deleted 3 CDs' );
215 is( artist1()->count, 0, '0 Caterwauler McCrae CDs' );
216 is( artist2()->count, 2, '3 Random Boy Band CDs' );
217 ok( artist2()->update( { 'artist' => 1 } ) );
218 is( artist2()->count, 0, '0 Random Boy Band CDs' );
219 is( artist1()->count, 2, '2 Caterwauler McCrae CDs' );
221 # test update on multi-column-pk
223 return $schema->resultset("TwoKeys")->search(
225 'artist.name' => { like => '%Boy Band' },
226 'cd.title' => 'Greatest Hits',
228 { join => [qw/artist cd/] }
232 return $schema->resultset("TwoKeys")->search(
233 { 'artist.name' => 'Caterwauler McCrae' },
234 { join => [qw/artist/]}
237 is( tk2()->count, 2, 'TwoKeys count == 2' );
238 is( tk1()->count, 2, 'TwoKeys count == 2' );
239 ok( tk1()->update( { artist => 1 } ) );
240 is( tk1()->count, 0, 'TwoKeys count == 0' );
241 is( tk2()->count, 4, '2 Caterwauler McCrae CDs' );
242 ok( tk2()->delete, 'Successfully deleted 4 CDs' );
243 is(cd_count(), 5, '5 rows in table cd');
244 is(tk_count(), 3, '3 rows in table twokeys');