|
| 1 | +WITH filtered_default_versions as ( |
| 2 | + -- Get all `default_versions` that are depending on the crate $1 |
| 3 | + SELECT default_versions.* |
| 4 | + FROM default_versions |
| 5 | + WHERE version_id IN ( |
| 6 | + SELECT dependencies.version_id |
| 7 | + FROM dependencies |
| 8 | + WHERE dependencies.crate_id = $1 |
| 9 | + ) AND NOT EXISTS ( |
| 10 | + -- Filter out yanked crates |
| 11 | + -- (if the default version is yanked, then the whole crate is yanked) |
| 12 | + SELECT 1 |
| 13 | + FROM versions |
| 14 | + WHERE id = version_id and yanked |
| 15 | + ) |
| 16 | +) |
1 | 17 | SELECT |
2 | | - dependencies.*, crate_downloads, crate_name, total |
3 | | -FROM ( |
4 | | - -- Apply pagination to the crates |
5 | | - SELECT *, COUNT(*) OVER () as total FROM ( |
6 | | - SELECT |
7 | | - crate_downloads.downloads AS crate_downloads, |
8 | | - crates.name AS crate_name, |
9 | | - versions.id AS version_id |
10 | | - FROM |
11 | | - -- We only want the crates whose *max* version is dependent, so we join on a |
12 | | - -- subselect that includes the versions with their ordinal position |
13 | | - ( |
14 | | - SELECT DISTINCT ON (crate_id) |
15 | | - crate_id, semver_no_prerelease, id |
16 | | - FROM versions |
17 | | - WHERE NOT yanked |
18 | | - ORDER BY |
19 | | - crate_id, |
20 | | - semver_no_prerelease DESC NULLS LAST, |
21 | | - id DESC |
22 | | - ) versions |
23 | | - INNER JOIN crates |
24 | | - ON crates.id = versions.crate_id |
25 | | - INNER JOIN crate_downloads |
26 | | - ON crate_downloads.crate_id = crates.id |
27 | | - WHERE versions.id IN (SELECT version_id FROM dependencies WHERE crate_id = $1) |
28 | | - ) c |
29 | | - ORDER BY |
30 | | - crate_downloads DESC, |
31 | | - crate_name ASC |
32 | | -) crates |
| 18 | + dependencies.*, |
| 19 | + crate_downloads.downloads as crate_downloads, |
| 20 | + crates.name as crate_name, |
| 21 | + (SELECT COUNT(*) from filtered_default_versions) as total |
| 22 | +FROM filtered_default_versions |
| 23 | +INNER JOIN crates |
| 24 | + ON crates.id = filtered_default_versions.crate_id |
| 25 | +INNER JOIN crate_downloads using (crate_id) |
33 | 26 | -- Multiple dependencies can exist, we only want first one |
34 | 27 | CROSS JOIN LATERAL ( |
35 | 28 | SELECT dependencies.* |
36 | 29 | FROM dependencies |
37 | | - WHERE dependencies.crate_id = $1 AND dependencies.version_id = crates.version_id |
| 30 | + WHERE dependencies.crate_id = $1 AND dependencies.version_id = filtered_default_versions.version_id |
38 | 31 | ORDER BY id ASC |
39 | 32 | LIMIT 1 |
40 | 33 | ) dependencies |
| 34 | +ORDER BY |
| 35 | + crate_downloads DESC, |
| 36 | + crate_name ASC |
41 | 37 | OFFSET $2 |
42 | 38 | LIMIT $3 |
0 commit comments