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;
11 use DBIx::Class::SQLMaker::LimitDialects;
13 my $ROWS = DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype;
15 my $schema = DBICTest->init_schema();
18 my $no_prefetch = $schema->resultset('Artist')->search(
19 [ # search deliberately contrived
20 { 'artwork.cd_id' => undef },
21 { 'tracks.title' => { '!=' => 'blah-blah-1234568' }}
23 { rows => 3, join => { cds => [qw/artwork tracks/] },
27 my $use_prefetch = $no_prefetch->search(
30 select => ['me.artistid', 'me.name'],
31 as => ['artistid', 'name'],
33 order_by => { -desc => 'name' },
37 # add an extra +select to make sure it does not throw things off
38 # we also expect it to appear in both selectors, as we can not know
39 # for sure which part of the query it applies to (may be order_by,
40 # maybe something else)
42 # we use a reference to the same array in bind vals, because
43 # is_deeply picks up this difference too (not sure if bug or
45 $use_prefetch = $use_prefetch->search({}, {
46 '+columns' => { monkeywrench => \[ 'me.artistid + ?', [ \ 'inTEger' => 1 ] ] },
49 my $bind_int_resolved = sub { [ { sqlt_datatype => 'inTEger' } => 1 ] };
50 my $bind_vc_resolved = sub { [
51 { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'tracks.title' }
52 => 'blah-blah-1234568'
55 $use_prefetch->as_query,
57 SELECT me.artistid + ?,
59 cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track
61 SELECT me.artistid + ?,
65 ON cds.artist = me.artistid
66 LEFT JOIN cd_artwork artwork
67 ON artwork.cd_id = cds.cdid
68 LEFT JOIN track tracks
69 ON tracks.cd = cds.cdid
70 WHERE artwork.cd_id IS NULL
72 GROUP BY me.artistid + ?, me.artistid, me.name
73 ORDER BY name DESC LIMIT ?
76 ON cds.artist = me.artistid
77 LEFT JOIN cd_artwork artwork
78 ON artwork.cd_id = cds.cdid
79 LEFT JOIN track tracks
80 ON tracks.cd = cds.cdid
81 WHERE artwork.cd_id IS NULL
83 GROUP BY me.artistid + ?, me.artistid, me.name, cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track
84 ORDER BY name DESC, cds.artist, cds.year ASC
87 $bind_int_resolved->(), # outer select
88 $bind_int_resolved->(), # inner select
89 $bind_vc_resolved->(), # inner where
90 $bind_int_resolved->(), # inner group_by
92 $bind_vc_resolved->(), # outer where
93 $bind_int_resolved->(), # outer group_by
95 'Expected SQL on complex limited prefetch'
98 is($no_prefetch->count, $use_prefetch->count, '$no_prefetch->count == $use_prefetch->count');
100 scalar ($no_prefetch->all),
101 scalar ($use_prefetch->all),
102 "Amount of returned rows is right"
105 my $artist_many_cds = $schema->resultset('Artist')->search ( {}, {
107 group_by => 'me.artistid',
108 having => \ 'count(cds.cdid) > 1',
112 $no_prefetch = $schema->resultset('Artist')->search(
113 { artistid => $artist_many_cds->id },
117 $use_prefetch = $no_prefetch->search ({}, { prefetch => 'cds' });
119 my $normal_artist = $no_prefetch->single;
120 my $prefetch_artist = $use_prefetch->find({ name => $artist_many_cds->name });
121 my $prefetch2_artist = $use_prefetch->first;
124 $prefetch_artist->cds->count,
125 $normal_artist->cds->count,
126 "Count of child rel with prefetch + rows => 1 is right (find)"
129 $prefetch2_artist->cds->count,
130 $normal_artist->cds->count,
131 "Count of child rel with prefetch + rows => 1 is right (first)"
135 scalar ($prefetch_artist->cds->all),
136 scalar ($normal_artist->cds->all),
137 "Amount of child rel rows with prefetch + rows => 1 is right (find)"
140 scalar ($prefetch2_artist->cds->all),
141 scalar ($normal_artist->cds->all),
142 "Amount of child rel rows with prefetch + rows => 1 is right (first)"
146 sub { $use_prefetch->single },
147 qr/resultsets prefetching has_many/,
148 'single() with multiprefetch is illegal',
153 $use_prefetch->search(
154 {'tracks.title' => { '!=' => 'foo' }},
155 { order_by => \ 'some oddball literal sql', join => { cds => 'tracks' } }
157 }, qr/A required group_by clause could not be constructed automatically/,
160 my $artist = $use_prefetch->search({'cds.title' => $artist_many_cds->cds->first->title })->next;
161 is($artist->cds->count, 1, "count on search limiting prefetched has_many");
163 # try with double limit
164 my $artist2 = $use_prefetch->search({'cds.title' => { '!=' => $artist_many_cds->cds->first->title } })->slice (0,0)->next;
165 is($artist2->cds->count, 2, "count on search limiting prefetched has_many");
167 # make sure 1:1 joins do not force a subquery (no point to exercise the optimizer, if at all available)
168 # get cd's that have any tracks and their artists
169 my $single_prefetch_rs = $schema->resultset ('CD')->search (
170 { 'me.year' => 2010, 'artist.name' => 'foo' },
171 { prefetch => ['tracks', 'artist'], rows => 15 },
174 $single_prefetch_rs->as_query,
177 me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
178 tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at,
179 artist.artistid, artist.name, artist.rank, artist.charfield
182 me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
184 JOIN artist artist ON artist.artistid = me.artist
185 WHERE ( ( artist.name = ? AND me.year = ? ) )
188 LEFT JOIN track tracks
189 ON tracks.cd = me.cdid
191 ON artist.artistid = me.artist
192 WHERE ( ( artist.name = ? AND me.year = ? ) )
196 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'artist.name' } => 'foo' ],
197 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' } => 2010 ],
199 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'artist.name' } => 'foo' ],
200 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' } => 2010 ],
202 'No grouping of non-multiplying resultsets',