add shallow copy of $attrs in ResultSet->new
[dbsrgits/DBIx-Class.git] / t / 76joins.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
cb3e35d2 7use Data::Dumper;
70350518 8
a47e1233 9my $schema = DBICTest->init_schema();
0567538f 10
4d91ad3f 11my $orig_debug = $schema->storage->debug;
12
0567538f 13use IO::File;
14
15BEGIN {
16 eval "use DBD::SQLite";
17 plan $@
18 ? ( skip_all => 'needs DBD::SQLite for testing' )
cb3e35d2 19 : ( tests => 49 );
0567538f 20}
21
58ff4acf 22# figure out if we've got a version of sqlite that is older than 3.2.6, in
23# which case COUNT(DISTINCT()) doesn't work
24my $is_broken_sqlite = 0;
25my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
26 split /\./, $schema->storage->dbh->get_info(18);
27if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
28 ( ($sqlite_major_ver < 3) ||
29 ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
30 ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
31 $is_broken_sqlite = 1;
32}
485f6e10 33
0567538f 34# test the abstract join => SQL generator
35my $sa = new DBIC::SQL::Abstract;
36
37my @j = (
38 { child => 'person' },
39 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
40 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
41);
42my $match = 'person child JOIN person father ON ( father.person_id = '
43 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
44 . '= child.mother_id )'
45 ;
46is( $sa->_recurse_from(@j), $match, 'join 1 ok' );
47
48my @j2 = (
49 { mother => 'person' },
50 [ [ { child => 'person' },
51 [ { father => 'person' },
52 { 'father.person_id' => 'child.father_id' }
53 ]
54 ],
55 { 'mother.person_id' => 'child.mother_id' }
56 ],
57);
58$match = 'person mother JOIN (person child JOIN person father ON ('
59 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
60 . 'child.mother_id )'
61 ;
62is( $sa->_recurse_from(@j2), $match, 'join 2 ok' );
63
64my @j3 = (
65 { child => 'person' },
66 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
67 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
68);
69$match = 'person child INNER JOIN person father ON ( father.person_id = '
70 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
71 . '= child.mother_id )'
72 ;
73
74is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok');
75
ca7b9fdf 76my @j4 = (
77 { mother => 'person' },
78 [ [ { child => 'person', -join_type => 'left' },
79 [ { father => 'person', -join_type => 'right' },
80 { 'father.person_id' => 'child.father_id' }
81 ]
82 ],
83 { 'mother.person_id' => 'child.mother_id' }
84 ],
85);
86$match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
87 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
88 . 'child.mother_id )'
89 ;
90is( $sa->_recurse_from(@j4), $match, 'join 4 (nested joins + join types) ok');
91
f9db5527 92my $rs = $schema->resultset("CD")->search(
0567538f 93 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
94 { from => [ { 'me' => 'cd' },
95 [
96 { artist => 'artist' },
97 { 'me.artist' => 'artist.artistid' }
98 ] ] }
99 );
100
ebaefbc2 101cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
0567538f 102
103is($rs->first->title, 'Forkful of bees', 'Correct record returned');
104
f9db5527 105$rs = $schema->resultset("CD")->search(
0567538f 106 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
107 { join => 'artist' });
108
ebaefbc2 109cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
0567538f 110
111is($rs->first->title, 'Forkful of bees', 'Correct record returned');
112
f9db5527 113$rs = $schema->resultset("CD")->search(
0567538f 114 { 'artist.name' => 'We Are Goth',
115 'liner_notes.notes' => 'Kill Yourself!' },
116 { join => [ qw/artist liner_notes/ ] });
117
ebaefbc2 118cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
0567538f 119
120is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
121
8fe164b9 122# when using join attribute, make sure slice()ing all objects has same count as all()
f9db5527 123$rs = $schema->resultset("CD")->search(
8fe164b9 124 { 'artist' => 1 },
125 { join => [qw/artist/], order_by => 'artist.name' }
126);
127cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
128
f9db5527 129$rs = $schema->resultset("Artist")->search(
0567538f 130 { 'liner_notes.notes' => 'Kill Yourself!' },
131 { join => { 'cds' => 'liner_notes' } });
132
133cmp_ok( $rs->count, '==', 1, "Single record in resultset");
134
135is($rs->first->name, 'We Are Goth', 'Correct record returned');
136
cb3e35d2 137# bug in 0.07000 caused attr (join/prefetch) to be modifed by search
138# so we check the search & attr arrays are not modified
139my $search = { 'artist.name' => 'Caterwauler McCrae' };
140my $attr = { prefetch => [ qw/artist liner_notes/ ],
141 order_by => 'me.cdid' };
142my $search_str = Dumper($search);
143my $attr_str = Dumper($attr);
144
145$rs = $schema->resultset("CD")->search($search, $attr);
0567538f 146
cb3e35d2 147is(Dumper($search), $search_str, 'Search hash untouched after search()');
148is(Dumper($attr), $attr_str, 'Attribute hash untouched after search()');
ebaefbc2 149cmp_ok($rs + 0, '==', 3, 'Correct number of records returned');
0567538f 150
d52170d4 151my $queries = 0;
152$schema->storage->debugcb(sub { $queries++ });
d52170d4 153$schema->storage->debug(1);
0567538f 154
155my @cd = $rs->all;
156
157is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
158
7cd300ea 159ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
0567538f 160
161is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
162
dd417d06 163is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
164
0567538f 165is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
166
d52170d4 167is($queries, 1, 'prefetch ran only 1 select statement');
168
4d91ad3f 169$schema->storage->debug($orig_debug);
170$schema->storage->debugobj->callback(undef);
0567538f 171
9b465d00 172# test for partial prefetch via columns attr
d20bb28e 173my $cd = $schema->resultset('CD')->find(1,
174 {
9b465d00 175 columns => [qw/title artist.name/],
0e35aa05 176 join => { 'artist' => {} }
d20bb28e 177 }
178);
179ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
180
b3e8ac9b 181# start test for nested prefetch SELECT count
d52170d4 182$queries = 0;
db0e65d2 183$schema->storage->debugcb(sub { $queries++ });
d52170d4 184$schema->storage->debug(1);
b3e8ac9b 185
186$rs = $schema->resultset('Tag')->search(
187 {},
188 {
189 prefetch => { cd => 'artist' }
190 }
191);
192
193my $tag = $rs->first;
194
195is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
196
197is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
198
199# count the SELECTs
d52170d4 200#$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
201is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
b3e8ac9b 202
d52170d4 203$queries = 0;
c5b7d799 204
d20bb28e 205$cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
c5b7d799 206
207is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
208
d52170d4 209is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
210
4d91ad3f 211$schema->storage->debug($orig_debug);
212$schema->storage->debugobj->callback(undef);
c5b7d799 213
b3e8ac9b 214$rs = $schema->resultset('Tag')->search(
215 {},
216 {
217 join => { cd => 'artist' },
218 prefetch => { cd => 'artist' }
219 }
220);
221
222cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
223
f9db5527 224my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
0567538f 225 { order_by => 'artistid DESC', join => 'cds' });
226
227is($artist->name, 'Random Boy Band', "Join search by object ok");
228
f9db5527 229my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
0567538f 230 { join => 'liner_notes' });
231
232cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
233
234is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
235
f9db5527 236my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
0567538f 237 { join => { 'cds' => 'tags' } });
238
239cmp_ok( @artists, '==', 2, "two-join search ok" );
240
15c382be 241$rs = $schema->resultset("CD")->search(
242 {},
243 { group_by => [qw/ title me.cdid /] }
244);
245
485f6e10 246SKIP: {
247 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
248 if $is_broken_sqlite;
249 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
250}
15c382be 251
252cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
253
254$rs = $schema->resultset("CD")->search(
255 {},
256 { join => [qw/ artist /], group_by => [qw/ artist.name /] }
257);
258
485f6e10 259SKIP: {
260 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
261 if $is_broken_sqlite;
262 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
263}
15c382be 264
8839560b 265$rs = $schema->resultset("Artist")->search(
6991aba3 266 {},
01c73d7b 267 { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } }
6991aba3 268);
269
8839560b 270cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
6991aba3 271
01c73d7b 272$rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }});
6991aba3 273
8839560b 274cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
489709af 275
276$rs = $schema->resultset("Artist")->search(
277 { 'cds.title' => 'Spoonful of bees',
278 'cds_2.title' => 'Forkful of bees' },
279 { join => [ 'cds', 'cds' ] });
280
485f6e10 281SKIP: {
282 skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
283 if $is_broken_sqlite;
284 cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
285}
286
489709af 287is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
288
d52170d4 289$queries = 0;
db0e65d2 290$schema->storage->debugcb(sub { $queries++ });
887ce227 291$schema->storage->debug(1);
292
293my $tree_like =
294 $schema->resultset('TreeLike')->find(4,
295 { join => { parent => { parent => 'parent' } },
296 prefetch => { parent => { parent => 'parent' } } });
297
298is($tree_like->name, 'quux', 'Bottom of tree ok');
299$tree_like = $tree_like->parent;
300is($tree_like->name, 'baz', 'First level up ok');
301$tree_like = $tree_like->parent;
302is($tree_like->name, 'bar', 'Second level up ok');
303$tree_like = $tree_like->parent;
304is($tree_like->name, 'foo', 'Third level up ok');
305
4d91ad3f 306$schema->storage->debug($orig_debug);
307$schema->storage->debugobj->callback(undef);
887ce227 308
309cmp_ok($queries, '==', 1, 'Only one query run');
310
10ed6c25 311$tree_like = $schema->resultset('TreeLike')->search({'me.id' => 1});
af4cd3e5 312$tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first;
7e5b5f6e 313is($tree_like->name, 'quux', 'Tree search_related ok');
314
48d2be64 315$tree_like = $schema->resultset('TreeLike')->search_related('children',
316 { 'children.id' => 2, 'children_2.id' => 3 },
317 { prefetch => { children => 'children' } }
318 )->first;
b377c7e1 319is(eval { $tree_like->children->first->children->first->name }, 'quux',
320 'Tree search_related with prefetch ok');
10ed6c25 321
b377c7e1 322$tree_like = eval { $schema->resultset('TreeLike')->search(
10ed6c25 323 { 'children.id' => 2, 'children_2.id' => 5 },
3583d755 324 { join => [qw/children children/] }
9755b3df 325 )->search_related('children', { 'children_4.id' => 6 }, { prefetch => 'children' }
b377c7e1 326 )->first->children->first; };
327is(eval { $tree_like->name }, 'fong', 'Tree with multiple has_many joins ok');
10ed6c25 328
7e5b5f6e 329# test that collapsed joins don't get a _2 appended to the alias
330
331my $sql = '';
332$schema->storage->debugcb(sub { $sql = $_[1] });
333$schema->storage->debug(1);
334
335eval {
336 my $row = $schema->resultset('Artist')->search_related('cds', undef, {
337 join => 'tracks',
338 prefetch => 'tracks',
339 })->search_related('tracks')->first;
340};
341
9755b3df 342like( $sql, qr/^SELECT tracks_2\.trackid/, "join not collapsed for search_related" );
7e5b5f6e 343
4d91ad3f 344$schema->storage->debug($orig_debug);
345$schema->storage->debugobj->callback(undef);