1 # Test to ensure we get a consistent result set wether or not we use the
2 # prefetch option in combination rows (LIMIT).
10 use DBIC::SqlMakerTest;
12 my $schema = DBICTest->init_schema();
15 my $no_prefetch = $schema->resultset('Artist')->search(
16 [ # search deliberately contrived
17 { 'artwork.cd_id' => undef },
18 { 'tracks.title' => { '!=' => 'blah-blah-1234568' }}
20 { rows => 3, join => { cds => [qw/artwork tracks/] },
24 my $use_prefetch = $no_prefetch->search(
27 select => ['me.artistid', 'me.name'],
28 as => ['artistid', 'name'],
30 order_by => { -desc => 'name' },
34 is($no_prefetch->count, $use_prefetch->count, '$no_prefetch->count == $use_prefetch->count');
36 scalar ($no_prefetch->all),
37 scalar ($use_prefetch->all),
38 "Amount of returned rows is right"
41 my $artist_many_cds = $schema->resultset('Artist')->search ( {}, {
43 group_by => 'me.artistid',
44 having => \ 'count(cds.cdid) > 1',
48 $no_prefetch = $schema->resultset('Artist')->search(
49 { artistid => $artist_many_cds->id },
53 $use_prefetch = $no_prefetch->search ({}, { prefetch => 'cds' });
55 my $normal_artist = $no_prefetch->single;
56 my $prefetch_artist = $use_prefetch->find({ name => $artist_many_cds->name });
57 my $prefetch2_artist = $use_prefetch->first;
60 $prefetch_artist->cds->count,
61 $normal_artist->cds->count,
62 "Count of child rel with prefetch + rows => 1 is right (find)"
65 $prefetch2_artist->cds->count,
66 $normal_artist->cds->count,
67 "Count of child rel with prefetch + rows => 1 is right (first)"
71 scalar ($prefetch_artist->cds->all),
72 scalar ($normal_artist->cds->all),
73 "Amount of child rel rows with prefetch + rows => 1 is right (find)"
76 scalar ($prefetch2_artist->cds->all),
77 scalar ($normal_artist->cds->all),
78 "Amount of child rel rows with prefetch + rows => 1 is right (first)"
82 sub { $use_prefetch->single },
83 qr/resultsets prefetching has_many/,
84 'single() with multiprefetch is illegal',
89 $use_prefetch->search(
90 {'tracks.title' => { '!=' => 'foo' }},
91 { order_by => \ 'some oddball literal sql', join => { cds => 'tracks' } }
93 }, qr/A required group_by clause could not be constructed automatically/,
96 my $artist = $use_prefetch->search({'cds.title' => $artist_many_cds->cds->first->title })->next;
97 is($artist->cds->count, 1, "count on search limiting prefetched has_many");
99 # try with double limit
100 my $artist2 = $use_prefetch->search({'cds.title' => { '!=' => $artist_many_cds->cds->first->title } })->slice (0,0)->next;
101 is($artist2->cds->count, 2, "count on search limiting prefetched has_many");
103 # make sure 1:1 joins do not force a subquery (no point to exercise the optimizer, if at all available)
104 # get cd's that have any tracks and their artists
105 my $single_prefetch_rs = $schema->resultset ('CD')->search (
106 { 'me.year' => 2010, 'artist.name' => 'foo' },
107 { prefetch => ['tracks', 'artist'], rows => 15 },
110 $single_prefetch_rs->as_query,
113 me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
114 tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at,
115 artist.artistid, artist.name, artist.rank, artist.charfield
118 me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
120 JOIN artist artist ON artist.artistid = me.artist
121 WHERE ( ( artist.name = ? AND me.year = ? ) )
124 LEFT JOIN track tracks
125 ON tracks.cd = me.cdid
127 ON artist.artistid = me.artist
128 WHERE ( ( artist.name = ? AND me.year = ? ) )
132 [ 'artist.name' => 'foo' ],
133 [ 'me.year' => 2010 ],
134 [ 'artist.name' => 'foo' ],
135 [ 'me.year' => 2010 ],
137 'No grouping of non-multiplying resultsets',