chore: Prepare libraryView to replace libraryQuery

This commit is contained in:
Ahmad Ansori Palembani 2024-06-06 21:26:19 +07:00
parent d0baff197c
commit 08225ee6a0
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
2 changed files with 33 additions and 0 deletions

View file

@ -0,0 +1,23 @@
CREATE VIEW library_view AS
SELECT
M.*,
coalesce(MC.category_id, 0) AS category
FROM (
SELECT
MS.*,
coalesce(count(*) - sum(read), 0) AS unread,
sum(read) AS has_read,
sum(bookmark) AS bookmark_count
FROM mangas AS MS
LEFT JOIN chapters AS C
ON C.manga_id = MS._id
LEFT JOIN scanlators_view AS filtered_scanlators
ON C.manga_id = filtered_scanlators._id
AND ifnull(C.scanlator, 'N/A') = ifnull(filtered_scanlators.name, '/<INVALID>/') -- I assume if it's N/A it shouldn't be filtered
WHERE MS.favorite = 1
AND filtered_scanlators.name IS NULL
GROUP BY MS._id
ORDER BY MS.title
) AS M
LEFT JOIN mangas_categories AS MC
ON MC.manga_id = M._id;

View file

@ -0,0 +1,10 @@
CREATE VIEW scanlators_view AS -- Probably should migrate these to a real table
WITH RECURSIVE split(seq, _id, name, str) AS (
SELECT 0, M._id, NULL, replace(ifnull(M.filtered_scanlators, ''), ' & ', ',')||',' FROM mangas AS M
UNION ALL SELECT
seq+1,
_id,
substr(str, 0, instr(str, ',')),
substr(str, instr(str, ',')+1)
FROM split WHERE str != ''
) SELECT _id, name FROM split WHERE split.seq != 0 ORDER BY split.seq ASC;