7 use DBIC::SqlMakerTest;
9 use DBIx::Class::SQLMaker::LimitDialects;
11 my ($ROWS, $OFFSET) = (
12 DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
13 DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype,
16 my $schema = DBICTest->init_schema();
18 my $artist_rs = $schema->resultset('Artist');
20 my $filtered_cd_rs = $artist_rs->search_related('cds_unordered',
25 order_by => [ { -desc => 'genre.name' }, 'tracks.title DESC', { -asc => "me.name" }, { -desc => 'cds_unordered.title' } ], # me. is the artist, *NOT* the cd
31 artist => 1, cdid => 1, genreid => 1, single_track => undef, title => "Spoonful of bees", year => 1999, tracks => [
32 { cd => 1, last_updated_at => undef, last_updated_on => undef, position => 1, title => "The Bees Knees", trackid => 16 },
33 { cd => 1, last_updated_at => undef, last_updated_on => undef, position => 3, title => "Beehind You", trackid => 18 },
34 { cd => 1, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Apiary", trackid => 17 },
38 artist => 1, cdid => 3, genreid => undef, single_track => undef, title => "Caterwaulin' Blues", year => 1997, tracks => [
39 { cd => 3, last_updated_at => undef, last_updated_on => undef, position => 1, title => "Yowlin", trackid => 7 },
40 { cd => 3, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Howlin", trackid => 8 },
41 { cd => 3, last_updated_at => undef, last_updated_on => undef, position => 3, title => "Fowlin", trackid => 9 },
45 artist => 3, cdid => 5, genreid => undef, single_track => undef, title => "Come Be Depressed With Us", year => 1998, tracks => [
46 { cd => 5, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Under The Weather", trackid => 14 },
47 { cd => 5, last_updated_at => undef, last_updated_on => undef, position => 3, title => "Suicidal", trackid => 15 },
48 { cd => 5, last_updated_at => undef, last_updated_on => undef, position => 1, title => "Sad", trackid => 13 },
52 artist => 1, cdid => 2, genreid => undef, single_track => undef, title => "Forkful of bees", year => 2001, tracks => [
53 { cd => 2, last_updated_at => undef, last_updated_on => undef, position => 1, title => "Stung with Success", trackid => 4 },
54 { cd => 2, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Stripy", trackid => 5 },
55 { cd => 2, last_updated_at => undef, last_updated_on => undef, position => 3, title => "Sticky Honey", trackid => 6 },
59 artist => 2, cdid => 4, genreid => undef, single_track => undef, title => "Generic Manufactured Singles", year => 2001, tracks => [
60 { cd => 4, last_updated_at => undef, last_updated_on => undef, position => 3, title => "No More Ideas", trackid => 12 },
61 { cd => 4, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Boring Song", trackid => 11 },
62 { cd => 4, last_updated_at => undef, last_updated_on => undef, position => 1, title => "Boring Name", trackid => 10},
68 $filtered_cd_rs->all_hri,
70 'Expected ordered unlimited contents',
80 my ($limit, $offset) = @$_;
82 my $rs = $filtered_cd_rs->search({}, { $limit ? (rows => $limit) : (), offset => $offset });
84 my $used_limit = $limit || DBIx::Class::SQLMaker->__max_int;
85 my $offset_str = $offset ? 'OFFSET ?' : '';
90 SELECT cds_unordered.cdid, cds_unordered.artist, cds_unordered.title, cds_unordered.year, cds_unordered.genreid, cds_unordered.single_track,
91 tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at
94 SELECT cds_unordered.cdid, cds_unordered.artist, cds_unordered.title, cds_unordered.year, cds_unordered.genreid, cds_unordered.single_track
97 ON cds_unordered.artist = me.artistid
99 ON genre.genreid = cds_unordered.genreid
100 LEFT JOIN track tracks
101 ON tracks.cd = cds_unordered.cdid
102 WHERE ( me.rank = ? )
103 GROUP BY cds_unordered.cdid, cds_unordered.artist, cds_unordered.title, cds_unordered.year, cds_unordered.genreid, cds_unordered.single_track
104 ORDER BY MAX(genre.name) DESC, MAX(tracks.title) DESC, MIN(me.name), cds_unordered.title DESC
108 ON cds_unordered.artist = me.artistid
109 LEFT JOIN genre genre
110 ON genre.genreid = cds_unordered.genreid
111 LEFT JOIN track tracks
112 ON tracks.cd = cds_unordered.cdid
113 WHERE ( me.rank = ? )
114 ORDER BY genre.name DESC, tracks.title DESC, me.name ASC, cds_unordered.title DESC
117 [ { sqlt_datatype => 'integer', dbic_colname => 'me.rank' } => 13 ],
118 [ $ROWS => $used_limit ],
119 $offset ? [ $OFFSET => $offset ] : (),
120 [ { sqlt_datatype => 'integer', dbic_colname => 'me.rank' } => 13 ],
122 "correct SQL on prefetch over search_related ordered by external joins with limit '$limit', offset '$offset'",
127 [ @{$hri_contents}[$offset .. List::Util::min( $used_limit+$offset-1, $#$hri_contents)] ],
128 "Correct slice of the resultset returned with limit '$limit', offset '$offset'",