Commit | Line | Data |
0a3441ee |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | |
6 | use lib qw(t/lib); |
a5a7bb73 |
7 | use DBICTest ':DiffSQL'; |
fcb7fcbb |
8 | use DBIx::Class::SQLMaker::LimitDialects; |
9 | |
10 | my ($ROWS, $OFFSET) = ( |
11 | DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype, |
12 | DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype, |
13 | ); |
0a3441ee |
14 | |
e6977bbb |
15 | my $schema = DBICTest->init_schema(quote_names => 1); |
0a3441ee |
16 | |
17 | my $artist_rs = $schema->resultset('Artist'); |
0a3441ee |
18 | |
19 | my $filtered_cd_rs = $artist_rs->search_related('cds_unordered', |
1e4f9fb3 |
20 | { "me.rank" => 13 }, |
0a3441ee |
21 | { |
1e4f9fb3 |
22 | prefetch => 'tracks', |
23 | join => 'genre', |
e6977bbb |
24 | order_by => [ { -desc => 'genre.name' }, { -desc => \ 'tracks.title' }, { -asc => "me.name" }, { -desc => [qw(year cds_unordered.title)] } ], # me. is the artist, *NOT* the cd |
0a3441ee |
25 | }, |
26 | ); |
27 | |
1e4f9fb3 |
28 | my $hri_contents = [ |
29 | { |
30 | artist => 1, cdid => 1, genreid => 1, single_track => undef, title => "Spoonful of bees", year => 1999, tracks => [ |
31 | { cd => 1, last_updated_at => undef, last_updated_on => undef, position => 1, title => "The Bees Knees", trackid => 16 }, |
32 | { cd => 1, last_updated_at => undef, last_updated_on => undef, position => 3, title => "Beehind You", trackid => 18 }, |
33 | { cd => 1, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Apiary", trackid => 17 }, |
34 | ], |
35 | }, |
36 | { |
37 | artist => 1, cdid => 3, genreid => undef, single_track => undef, title => "Caterwaulin' Blues", year => 1997, tracks => [ |
38 | { cd => 3, last_updated_at => undef, last_updated_on => undef, position => 1, title => "Yowlin", trackid => 7 }, |
39 | { cd => 3, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Howlin", trackid => 8 }, |
40 | { cd => 3, last_updated_at => undef, last_updated_on => undef, position => 3, title => "Fowlin", trackid => 9 }, |
41 | ], |
42 | }, |
43 | { |
44 | artist => 3, cdid => 5, genreid => undef, single_track => undef, title => "Come Be Depressed With Us", year => 1998, tracks => [ |
45 | { cd => 5, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Under The Weather", trackid => 14 }, |
46 | { cd => 5, last_updated_at => undef, last_updated_on => undef, position => 3, title => "Suicidal", trackid => 15 }, |
47 | { cd => 5, last_updated_at => undef, last_updated_on => undef, position => 1, title => "Sad", trackid => 13 }, |
48 | ], |
49 | }, |
50 | { |
51 | artist => 1, cdid => 2, genreid => undef, single_track => undef, title => "Forkful of bees", year => 2001, tracks => [ |
52 | { cd => 2, last_updated_at => undef, last_updated_on => undef, position => 1, title => "Stung with Success", trackid => 4 }, |
53 | { cd => 2, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Stripy", trackid => 5 }, |
54 | { cd => 2, last_updated_at => undef, last_updated_on => undef, position => 3, title => "Sticky Honey", trackid => 6 }, |
55 | ], |
56 | }, |
57 | { |
58 | artist => 2, cdid => 4, genreid => undef, single_track => undef, title => "Generic Manufactured Singles", year => 2001, tracks => [ |
59 | { cd => 4, last_updated_at => undef, last_updated_on => undef, position => 3, title => "No More Ideas", trackid => 12 }, |
60 | { cd => 4, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Boring Song", trackid => 11 }, |
61 | { cd => 4, last_updated_at => undef, last_updated_on => undef, position => 1, title => "Boring Name", trackid => 10}, |
62 | ], |
63 | }, |
64 | ]; |
0a3441ee |
65 | |
1e4f9fb3 |
66 | is_deeply( |
13fa2937 |
67 | $filtered_cd_rs->all_hri, |
1e4f9fb3 |
68 | $hri_contents, |
69 | 'Expected ordered unlimited contents', |
0a3441ee |
70 | ); |
71 | |
1e4f9fb3 |
72 | for ( |
73 | [ 0, 1 ], |
74 | [ 2, 0 ], |
75 | [ 20, 2 ], |
76 | [ 1, 3 ], |
77 | [ 2, 4 ], |
78 | ) { |
79 | my ($limit, $offset) = @$_; |
80 | |
81 | my $rs = $filtered_cd_rs->search({}, { $limit ? (rows => $limit) : (), offset => $offset }); |
82 | |
83 | my $used_limit = $limit || DBIx::Class::SQLMaker->__max_int; |
84 | my $offset_str = $offset ? 'OFFSET ?' : ''; |
85 | |
86 | is_same_sql_bind( |
87 | $rs->as_query, |
e6977bbb |
88 | qq{( |
89 | SELECT "cds_unordered"."cdid", "cds_unordered"."artist", "cds_unordered"."title", "cds_unordered"."year", "cds_unordered"."genreid", "cds_unordered"."single_track", |
90 | "tracks"."trackid", "tracks"."cd", "tracks"."position", "tracks"."title", "tracks"."last_updated_on", "tracks"."last_updated_at" |
91 | FROM "artist" "me" |
1e4f9fb3 |
92 | JOIN ( |
e6977bbb |
93 | SELECT "cds_unordered"."cdid", "cds_unordered"."artist", "cds_unordered"."title", "cds_unordered"."year", "cds_unordered"."genreid", "cds_unordered"."single_track" |
94 | FROM "artist" "me" |
95 | JOIN cd "cds_unordered" |
96 | ON "cds_unordered"."artist" = "me"."artistid" |
97 | LEFT JOIN "genre" "genre" |
98 | ON "genre"."genreid" = "cds_unordered"."genreid" |
99 | LEFT JOIN "track" "tracks" |
100 | ON "tracks"."cd" = "cds_unordered"."cdid" |
101 | WHERE "me"."rank" = ? |
eb58c082 |
102 | GROUP BY "cds_unordered"."cdid", "cds_unordered"."artist", "cds_unordered"."title", "cds_unordered"."year", "cds_unordered"."genreid", "cds_unordered"."single_track", "me"."name" |
e6977bbb |
103 | ORDER BY MAX("genre"."name") DESC, |
104 | MAX( tracks.title ) DESC, |
eb58c082 |
105 | "me"."name" ASC, |
e6977bbb |
106 | "year" DESC, |
107 | "cds_unordered"."title" DESC |
1e4f9fb3 |
108 | LIMIT ? |
109 | $offset_str |
e6977bbb |
110 | ) "cds_unordered" |
111 | ON "cds_unordered"."artist" = "me"."artistid" |
112 | LEFT JOIN "genre" "genre" |
113 | ON "genre"."genreid" = "cds_unordered"."genreid" |
114 | LEFT JOIN "track" "tracks" |
115 | ON "tracks"."cd" = "cds_unordered"."cdid" |
116 | WHERE "me"."rank" = ? |
117 | ORDER BY "genre"."name" DESC, |
118 | tracks.title DESC, |
119 | "me"."name" ASC, |
120 | "year" DESC, |
121 | "cds_unordered"."title" DESC |
122 | )}, |
1e4f9fb3 |
123 | [ |
124 | [ { sqlt_datatype => 'integer', dbic_colname => 'me.rank' } => 13 ], |
125 | [ $ROWS => $used_limit ], |
126 | $offset ? [ $OFFSET => $offset ] : (), |
127 | [ { sqlt_datatype => 'integer', dbic_colname => 'me.rank' } => 13 ], |
128 | ], |
129 | "correct SQL on prefetch over search_related ordered by external joins with limit '$limit', offset '$offset'", |
130 | ); |
131 | |
132 | is_deeply( |
133 | $rs->all_hri, |
134 | [ @{$hri_contents}[$offset .. List::Util::min( $used_limit+$offset-1, $#$hri_contents)] ], |
135 | "Correct slice of the resultset returned with limit '$limit', offset '$offset'", |
136 | ); |
137 | } |
138 | |
0a3441ee |
139 | done_testing; |