7 use DBIC::SqlMakerTest;
9 my $schema = DBICTest->init_schema();
11 my $orig_debug = $schema->storage->debug;
13 # test the abstract join => SQL generator
14 my $sa = new DBIx::Class::SQLMaker;
17 { child => 'person' },
18 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
19 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
21 my $match = 'person child JOIN person father ON ( father.person_id = '
22 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
23 . '= child.mother_id )'
26 $sa->_recurse_from(@j),
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 = '
46 $sa->_recurse_from(@j2),
53 { child => 'person' },
54 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
55 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
57 $match = 'person child INNER JOIN person father ON ( father.person_id = '
58 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
59 . '= child.mother_id )'
63 $sa->_recurse_from(@j3),
65 'join 3 (inner join) ok'
69 { mother => 'person' },
70 [ [ { child => 'person', -join_type => 'left' },
71 [ { father => 'person', -join_type => 'right' },
72 { 'father.person_id' => 'child.father_id' }
75 { 'mother.person_id' => 'child.mother_id' }
78 $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
79 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
83 $sa->_recurse_from(@j4),
85 'join 4 (nested joins + join types) ok'
89 { child => 'person' },
90 [ { father => 'person' }, { 'father.person_id' => \'!= child.father_id' }, ],
91 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
93 $match = 'person child JOIN person father ON ( father.person_id != '
94 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
95 . '= child.mother_id )'
98 $sa->_recurse_from(@j5),
100 'join 5 (SCALAR reference for ON statement) ok'
103 my $rs = $schema->resultset("CD")->search(
104 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
105 { from => [ { 'me' => 'cd' },
107 { artist => 'artist' },
108 { 'me.artist' => 'artist.artistid' }
112 is( $rs + 0, 1, "Single record in resultset");
114 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
116 $rs = $schema->resultset("CD")->search(
117 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
118 { join => 'artist' });
120 is( $rs + 0, 1, "Single record in resultset");
122 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
124 $rs = $schema->resultset("CD")->search(
125 { 'artist.name' => 'We Are Goth',
126 'liner_notes.notes' => 'Kill Yourself!' },
127 { join => [ qw/artist liner_notes/ ] });
129 is( $rs + 0, 1, "Single record in resultset");
131 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
133 # when using join attribute, make sure slice()ing all objects has same count as all()
134 $rs = $schema->resultset("CD")->search(
136 { join => [qw/artist/], order_by => 'artist.name' }
138 is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
140 ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
141 'Slicing beyond end of rs returns a zero count');
143 $rs = $schema->resultset("Artist")->search(
144 { 'liner_notes.notes' => 'Kill Yourself!' },
145 { join => { 'cds' => 'liner_notes' } });
147 is( $rs->count, 1, "Single record in resultset");
149 is($rs->first->name, 'We Are Goth', 'Correct record returned');
153 $schema->populate('Artist', [
154 [ qw/artistid name/ ],
155 [ 4, 'Another Boy Band' ],
157 $schema->populate('CD', [
158 [ qw/cdid artist title year/ ],
159 [ 6, 2, "Greatest Hits", 2001 ],
160 [ 7, 4, "Greatest Hits", 2005 ],
161 [ 8, 4, "BoyBandBlues", 2008 ],
163 $schema->populate('TwoKeys', [
171 my $cd_count = sub { $schema->resultset("CD")->count };
172 my $tk_count = sub { $schema->resultset("TwoKeys")->count };
174 is($cd_count->(), 8, '8 rows in table cd');
175 is($tk_count->(), 7, '7 rows in table twokeys');
177 my $artist1_rs = $schema->resultset("CD")->search(
178 { 'artist.name' => 'Caterwauler McCrae' },
179 { join => [qw/artist/]}
182 my $artist2_rs = $schema->resultset("CD")->search(
183 { 'artist.name' => 'Random Boy Band' },
184 { join => [qw/artist/]}
187 is( $artist1_rs->count, 3, '3 Caterwauler McCrae CDs' );
188 ok( $artist1_rs->delete, 'Successfully deleted 3 CDs' );
189 is( $artist1_rs->count, 0, '0 Caterwauler McCrae CDs' );
190 is( $artist2_rs->count, 2, '3 Random Boy Band CDs' );
191 ok( $artist2_rs->update( { 'artist' => 1 } ) );
192 is( $artist2_rs->count, 0, '0 Random Boy Band CDs' );
193 is( $artist1_rs->count, 2, '2 Caterwauler McCrae CDs' );
195 # test update on multi-column-pk
196 my $tk1_rs = $schema->resultset("TwoKeys")->search(
198 'artist.name' => { like => '%Boy Band' },
199 'cd.title' => 'Greatest Hits',
201 { join => [qw/artist cd/] }
204 my $tk2_rs = $schema->resultset("TwoKeys")->search(
205 { 'artist.name' => 'Caterwauler McCrae' },
206 { join => [qw/artist/]}
209 is( $tk2_rs->count, 2, 'TwoKeys count == 2' );
210 is( $tk1_rs->count, 2, 'TwoKeys count == 2' );
211 ok( $tk1_rs->update( { artist => 1 } ) );
212 is( $tk1_rs->count, 0, 'TwoKeys count == 0' );
213 is( $tk2_rs->count, 4, '2 Caterwauler McCrae CDs' );
214 ok( $tk2_rs->delete, 'Successfully deleted 4 CDs' );
215 is($cd_count->(), 5, '5 rows in table cd');
216 is($tk_count->(), 3, '3 rows in table twokeys');