Tweak library query (#8214)

* Tweak library query

Co-Authored-By: Quang Kieu <kieuq@wit.edu>

* Update app/src/main/sqldelight/migrations/21.sqm

* Update app/src/main/java/eu/kanade/domain/library/model/LibraryManga.kt

* Update app/src/main/sqldelight/view/libraryView.sq

* Update app/src/main/java/eu/kanade/data/manga/MangaMapper.kt

* Update app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt

* Update app/src/main/java/eu/kanade/data/manga/MangaMapper.kt

* Bump version

Co-authored-by: Quang Kieu <kieuq@wit.edu>
This commit is contained in:
AntsyLich
2022-10-17 01:33:12 +06:00
committed by GitHub
parent aea0cadbfb
commit d6cbff2837
8 changed files with 76 additions and 108 deletions

View File

@@ -74,62 +74,6 @@ FROM mangas
WHERE favorite = 0
GROUP BY source;
getLibrary:
SELECT M.*, COALESCE(MC.category_id, 0) AS category
FROM (
SELECT mangas.*, COALESCE(UR.unreadCount, 0) AS unread_count, COALESCE(R.readCount, 0) AS read_count
FROM mangas
LEFT JOIN (
SELECT chapters.manga_id, COUNT(*) AS unreadCount
FROM chapters
WHERE chapters.read = 0
GROUP BY chapters.manga_id
) AS UR
ON mangas._id = UR.manga_id
LEFT JOIN (
SELECT chapters.manga_id, COUNT(*) AS readCount
FROM chapters
WHERE chapters.read = 1
GROUP BY chapters.manga_id
) AS R
ON mangas._id = R.manga_id
WHERE mangas.favorite = 1
GROUP BY mangas._id
ORDER BY mangas.title
) AS M
LEFT JOIN (
SELECT *
FROM mangas_categories
) AS MC
ON M._id = MC.manga_id;
getLastRead:
SELECT M.*, MAX(H.last_read) AS max
FROM mangas M
JOIN chapters C
ON M._id = C.manga_id
JOIN history H
ON C._id = H.chapter_id
WHERE M.favorite = 1
GROUP BY M._id
ORDER BY max ASC;
getLatestByChapterUploadDate:
SELECT M.*, MAX(C.date_upload) AS max
FROM mangas M
JOIN chapters C
ON M._id = C.manga_id
GROUP BY M._id
ORDER BY max ASC;
getLatestByChapterFetchDate:
SELECT M.*, MAX(C.date_fetch) AS max
FROM mangas M
JOIN chapters C
ON M._id = C.manga_id
GROUP BY M._id
ORDER BY max ASC;
deleteMangasNotInLibraryBySourceIds:
DELETE FROM mangas
WHERE favorite = 0 AND source IN :sourceIds;

View File

@@ -0,0 +1,27 @@
CREATE VIEW libraryView AS
SELECT
M.*,
coalesce(C.total - C.readCount, 0) AS unreadCount,
coalesce(C.readCount, 0) AS readCount,
coalesce(C.latestUpload, 0) AS latestUpload,
coalesce(C.fetchedAt, 0) AS chapterFetchedAt,
coalesce(C.lastRead, 0) AS lastRead,
COALESCE(MC.category_id, 0) AS category
FROM mangas M
LEFT JOIN mangas_categories AS MC
ON MC.manga_id = M._id
LEFT JOIN(
SELECT
chapters.manga_id,
count(*) AS total,
sum(read) AS readCount,
max(chapters.date_upload) AS latestUpload,
max(history.last_read) AS lastRead,
max(chapters.date_fetch) AS fetchedAt
FROM chapters
LEFT JOIN history
ON chapters._id = history.chapter_id
GROUP BY chapters.manga_id
) AS C
ON M._id = C.manga_id
WHERE M.favorite = 1;

View File

@@ -0,0 +1,31 @@
CREATE VIEW libraryView AS
SELECT
M.*,
coalesce(C.total - C.readCount, 0) AS unreadCount,
coalesce(C.readCount, 0) AS readCount,
coalesce(C.latestUpload, 0) AS latestUpload,
coalesce(C.fetchedAt, 0) AS chapterFetchedAt,
coalesce(C.lastRead, 0) AS lastRead,
COALESCE(MC.category_id, 0) AS category
FROM mangas M
LEFT JOIN mangas_categories AS MC
ON MC.manga_id = M._id
LEFT JOIN(
SELECT
chapters.manga_id,
count(*) AS total,
sum(read) AS readCount,
coalesce(max(chapters.date_upload), 0) AS latestUpload,
coalesce(max(history.last_read), 0) AS lastRead,
coalesce(max(chapters.date_fetch), 0) AS fetchedAt
FROM chapters
LEFT JOIN history
ON chapters._id = history.chapter_id
GROUP BY chapters.manga_id
) AS C
ON M._id = C.manga_id
WHERE M.favorite = 1;
library:
SELECT *
FROM libraryView;