|
| 1 | +DROP VIEW IF EXISTS scanlators_view; |
| 2 | +CREATE VIEW scanlators_view AS |
| 3 | +SELECT S.* FROM ( |
| 4 | + WITH RECURSIVE split(seq, _id, name, str) AS ( |
| 5 | + SELECT 0, mangas._id, NULL, mangas.filtered_scanlators||' [.] ' FROM mangas WHERE mangas._id |
| 6 | + UNION ALL SELECT |
| 7 | + seq+1, |
| 8 | + _id, |
| 9 | + substr(str, 0, instr(str, ' [.] ')), |
| 10 | + substr(str, instr(str, ' [.] ')+5) |
| 11 | + FROM split WHERE str != '' |
| 12 | + ) |
| 13 | + SELECT _id AS manga_id, name FROM split WHERE name != '' ORDER BY split.seq ASC |
| 14 | +) AS S; |
| 15 | + |
| 16 | +DROP VIEW IF EXISTS library_view; |
| 17 | +CREATE VIEW library_view AS |
| 18 | +SELECT |
| 19 | + M.*, |
| 20 | + coalesce(C.total, 0) AS total, |
| 21 | + coalesce(C.read_count, 0) AS has_read, |
| 22 | + coalesce(C.bookmark_count, 0) AS bookmark_count, |
| 23 | + coalesce(MC.category_id, 0) AS category, |
| 24 | + coalesce(C.latestUpload, 0) AS latestUpload, |
| 25 | + coalesce(C.lastRead, 0) AS lastRead, |
| 26 | + coalesce(C.lastFetch, 0) AS lastFetch |
| 27 | +FROM mangas AS M |
| 28 | +LEFT JOIN ( |
| 29 | + SELECT |
| 30 | + chapters.manga_id, |
| 31 | + count(*) AS total, |
| 32 | + sum(read) AS read_count, |
| 33 | + sum(bookmark) AS bookmark_count, |
| 34 | + coalesce(max(chapters.date_upload), 0) AS latestUpload, |
| 35 | + coalesce(max(history.history_last_read), 0) AS lastRead, |
| 36 | + coalesce(max(chapters.date_fetch), 0) AS lastFetch |
| 37 | + FROM chapters |
| 38 | + LEFT JOIN scanlators_view AS filtered_scanlators |
| 39 | + ON chapters.manga_id = filtered_scanlators.manga_id |
| 40 | + AND chapters.scanlator = filtered_scanlators.name |
| 41 | + LEFT JOIN history |
| 42 | + ON chapters._id = history.history_chapter_id |
| 43 | + WHERE filtered_scanlators.name IS NULL |
| 44 | + GROUP BY chapters.manga_id |
| 45 | +) AS C |
| 46 | +ON M._id = C.manga_id |
| 47 | +LEFT JOIN (SELECT * FROM mangas_categories) AS MC |
| 48 | +ON MC.manga_id = M._id |
| 49 | +WHERE M.favorite = 1 |
| 50 | +ORDER BY M.title; |
0 commit comments