6 use DBICTest ':DiffSQL';
8 my $schema = DBICTest->init_schema();
10 # test the abstract join => SQL generator
11 my $sa = DBIx::Class::SQLMaker->new;
14 { child => 'person' },
15 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
16 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
18 my $match = 'person child JOIN person father ON ( father.person_id = '
19 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
20 . '= child.mother_id )'
23 $sa->_recurse_from(@j),
29 { mother => 'person' },
30 [ [ { child => 'person' },
31 [ { father => 'person' },
32 { 'father.person_id' => 'child.father_id' }
35 { 'mother.person_id' => 'child.mother_id' }
38 $match = 'person mother JOIN (person child JOIN person father ON ('
39 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
43 $sa->_recurse_from(@j2),
50 { child => 'person' },
51 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
52 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
54 $match = 'person child INNER JOIN person father ON ( father.person_id = '
55 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
56 . '= child.mother_id )'
60 $sa->_recurse_from(@j3),
62 'join 3 (inner join) ok'
66 { mother => 'person' },
67 [ [ { child => 'person', -join_type => 'left' },
68 [ { father => 'person', -join_type => 'right' },
69 { 'father.person_id' => 'child.father_id' }
72 { 'mother.person_id' => 'child.mother_id' }
75 $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
76 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
80 $sa->_recurse_from(@j4),
82 'join 4 (nested joins + join types) ok'
86 { child => 'person' },
87 [ { father => 'person' }, { 'father.person_id' => \'!= child.father_id' }, ],
88 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
90 $match = 'person child JOIN person father ON ( father.person_id != '
91 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
92 . '= child.mother_id )'
95 $sa->_recurse_from(@j5),
97 'join 5 (SCALAR reference for ON statement) ok'
100 my $rs = $schema->resultset("CD")->search(
101 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
102 { from => [ { 'me' => 'cd' },
104 { artist => 'artist' },
105 { 'me.artist' => 'artist.artistid' }
109 is( $rs + 0, 1, "Single record in resultset");
111 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
113 $rs = $schema->resultset("CD")->search(
114 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
115 { join => 'artist' });
117 is( $rs + 0, 1, "Single record in resultset");
119 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
121 $rs = $schema->resultset("CD")->search(
122 { 'artist.name' => 'We Are Goth',
123 'liner_notes.notes' => 'Kill Yourself!' },
124 { join => [ qw/artist liner_notes/ ] });
126 is( $rs + 0, 1, "Single record in resultset");
128 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
130 # when using join attribute, make sure slice()ing all objects has same count as all()
131 $rs = $schema->resultset("CD")->search(
133 { join => [qw/artist/], order_by => 'artist.name' }
135 is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
137 ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
138 'Slicing beyond end of rs returns a zero count');
140 $rs = $schema->resultset("Artist")->search(
141 { 'liner_notes.notes' => 'Kill Yourself!' },
142 { join => { 'cds' => 'liner_notes' } });
144 is( $rs->count, 1, "Single record in resultset");
146 is($rs->first->name, 'We Are Goth', 'Correct record returned');
150 $schema->populate('Artist', [
151 [ qw/artistid name/ ],
152 [ 4, 'Another Boy Band' ],
154 $schema->populate('CD', [
155 [ qw/cdid artist title year/ ],
156 [ 6, 2, "Greatest Hits", 2001 ],
157 [ 7, 4, "Greatest Hits", 2005 ],
158 [ 8, 4, "BoyBandBlues", 2008 ],
160 $schema->populate('TwoKeys', [
168 my $cd_count = sub { $schema->resultset("CD")->count };
169 my $tk_count = sub { $schema->resultset("TwoKeys")->count };
171 is($cd_count->(), 8, '8 rows in table cd');
172 is($tk_count->(), 7, '7 rows in table twokeys');
174 my $artist1_rs = $schema->resultset("CD")->search(
175 { 'artist.name' => 'Caterwauler McCrae' },
176 { join => [qw/artist/]}
179 my $artist2_rs = $schema->resultset("CD")->search(
180 { 'artist.name' => 'Random Boy Band' },
181 { join => [qw/artist/]}
184 is( $artist1_rs->count, 3, '3 Caterwauler McCrae CDs' );
185 ok( $artist1_rs->delete, 'Successfully deleted 3 CDs' );
186 is( $artist1_rs->count, 0, '0 Caterwauler McCrae CDs' );
187 is( $artist2_rs->count, 2, '3 Random Boy Band CDs' );
188 ok( $artist2_rs->update( { 'artist' => 1 } ) );
189 is( $artist2_rs->count, 0, '0 Random Boy Band CDs' );
190 is( $artist1_rs->count, 2, '2 Caterwauler McCrae CDs' );
192 # test update on multi-column-pk
193 my $tk1_rs = $schema->resultset("TwoKeys")->search(
195 'artist.name' => { like => '%Boy Band' },
196 'cd.title' => 'Greatest Hits',
198 { join => [qw/artist cd/] }
201 my $tk2_rs = $schema->resultset("TwoKeys")->search(
202 { 'artist.name' => 'Caterwauler McCrae' },
203 { join => [qw/artist/]}
206 is( $tk2_rs->count, 2, 'TwoKeys count == 2' );
207 is( $tk1_rs->count, 2, 'TwoKeys count == 2' );
208 ok( $tk1_rs->update( { artist => 1 } ) );
209 is( $tk1_rs->count, 0, 'TwoKeys count == 0' );
210 is( $tk2_rs->count, 4, '2 Caterwauler McCrae CDs' );
211 ok( $tk2_rs->delete, 'Successfully deleted 4 CDs' );
212 is($cd_count->(), 5, '5 rows in table cd');
213 is($tk_count->(), 3, '3 rows in table twokeys');