diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/MangaRestorer.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/MangaRestorer.kt index 213fc4bf7..71e84d265 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/MangaRestorer.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/restore/restorers/MangaRestorer.kt @@ -330,13 +330,13 @@ class MangaRestorer( null } else { // New history entry - item.copy(chapterId = chapter._id) + item.copy(chapterId = chapter.id) } } // Update history entry item.copy( - id = dbHistory._id, + id = dbHistory.id, chapterId = dbHistory.chapter_id, readAt = max(item.readAt?.time ?: 0L, dbHistory.last_read?.time ?: 0L) .takeIf { it > 0L } diff --git a/data/src/main/sqldelight/tachiyomi/data/categories.sq b/data/src/main/sqldelight/tachiyomi/data/categories.sq index a25e6ca3b..d042fec81 100644 --- a/data/src/main/sqldelight/tachiyomi/data/categories.sq +++ b/data/src/main/sqldelight/tachiyomi/data/categories.sq @@ -1,17 +1,18 @@ CREATE TABLE categories( - _id INTEGER NOT NULL PRIMARY KEY, + id INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL, - sort INTEGER NOT NULL, - flags INTEGER NOT NULL + `order` INTEGER NOT NULL, + flags INTEGER NOT NULL, + UNIQUE(`order`) ON CONFLICT ABORT ); -- Insert system category -INSERT OR IGNORE INTO categories(_id, name, sort, flags) VALUES (0, "", -1, 0); +INSERT OR IGNORE INTO categories(id, name, `order`, flags) VALUES (0, "", -1, 0); -- Disallow deletion of default category CREATE TRIGGER IF NOT EXISTS system_category_delete_trigger BEFORE DELETE ON categories BEGIN SELECT CASE - WHEN old._id <= 0 THEN + WHEN old.id <= 0 THEN RAISE(ABORT, "System category can't be deleted") END; END; @@ -19,47 +20,46 @@ END; getCategory: SELECT * FROM categories -WHERE _id = :id -LIMIT 1; +WHERE id = :id; getCategories: SELECT -_id AS id, +id, name, -sort AS `order`, +`order`, flags FROM categories -ORDER BY sort; +ORDER BY `order`; getCategoriesByMangaId: SELECT -C._id AS id, +C.id, C.name, -C.sort AS `order`, +C.`order`, C.flags FROM categories C JOIN mangas_categories MC -ON C._id = MC.category_id +ON C.id = MC.category_id WHERE MC.manga_id = :mangaId; insert: -INSERT INTO categories(name, sort, flags) +INSERT INTO categories(name, `order`, flags) VALUES (:name, :order, :flags); delete: DELETE FROM categories -WHERE _id = :categoryId; +WHERE id = :categoryId; update: UPDATE categories SET name = coalesce(:name, name), - sort = coalesce(:order, sort), + `order` = coalesce(:order, `order`), flags = coalesce(:flags, flags) -WHERE _id = :categoryId; +WHERE id = :categoryId; updateAllFlags: UPDATE categories SET flags = coalesce(?, flags); selectLastInsertedRowId: -SELECT last_insert_rowid(); \ No newline at end of file +SELECT last_insert_rowid(); diff --git a/data/src/main/sqldelight/tachiyomi/data/chapters.sq b/data/src/main/sqldelight/tachiyomi/data/chapters.sq index c69de987d..aa8809194 100644 --- a/data/src/main/sqldelight/tachiyomi/data/chapters.sq +++ b/data/src/main/sqldelight/tachiyomi/data/chapters.sq @@ -1,7 +1,7 @@ import kotlin.Boolean; CREATE TABLE chapters( - _id INTEGER NOT NULL PRIMARY KEY, + id INTEGER NOT NULL PRIMARY KEY, manga_id INTEGER NOT NULL, url TEXT NOT NULL, name TEXT NOT NULL, @@ -16,20 +16,20 @@ CREATE TABLE chapters( last_modified_at INTEGER NOT NULL DEFAULT 0, version INTEGER NOT NULL DEFAULT 0, is_syncing INTEGER NOT NULL DEFAULT 0, - FOREIGN KEY(manga_id) REFERENCES mangas (_id) + UNIQUE(manga_id, url) ON CONFLICT IGNORE, + FOREIGN KEY(manga_id) REFERENCES mangas(id) ON DELETE CASCADE ); CREATE INDEX chapters_manga_id_index ON chapters(manga_id); -CREATE INDEX chapters_unread_by_manga_index ON chapters(manga_id, read) WHERE read = 0; CREATE TRIGGER update_last_modified_at_chapters AFTER UPDATE ON chapters FOR EACH ROW BEGIN - UPDATE chapters - SET last_modified_at = strftime('%s', 'now') - WHERE _id = new._id; + UPDATE chapters + SET last_modified_at = strftime('%s', 'now') + WHERE id = new.id; END; CREATE TRIGGER update_chapter_and_manga_version AFTER UPDATE ON chapters @@ -41,17 +41,17 @@ WHEN new.is_syncing = 0 AND ( BEGIN -- Update the chapter version UPDATE chapters SET version = version + 1 - WHERE _id = new._id; + WHERE id = new.id; -- Update the manga version UPDATE mangas SET version = version + 1 - WHERE _id = new.manga_id AND (SELECT is_syncing FROM mangas WHERE _id = new.manga_id) = 0; + WHERE id = new.manga_id AND (SELECT is_syncing FROM mangas WHERE id = new.manga_id) = 0; END; getChapterById: SELECT * FROM chapters -WHERE _id = :id; +WHERE id = :id; getChaptersByMangaId: SELECT C.* @@ -89,7 +89,7 @@ AND manga_id = :mangaId; removeChaptersWithIds: DELETE FROM chapters -WHERE _id IN :chapterIds; +WHERE id IN :chapterIds; resetIsSyncing: UPDATE chapters @@ -115,7 +115,7 @@ SET manga_id = coalesce(:mangaId, manga_id), date_upload = coalesce(:dateUpload, date_upload), version = coalesce(:version, version), is_syncing = coalesce(:isSyncing, is_syncing) -WHERE _id = :chapterId; +WHERE id = :chapterId; selectLastInsertedRowId: -SELECT last_insert_rowid(); \ No newline at end of file +SELECT last_insert_rowid(); diff --git a/data/src/main/sqldelight/tachiyomi/data/excluded_scanlators.sq b/data/src/main/sqldelight/tachiyomi/data/excluded_scanlators.sq index 2af2f4199..8ad2ec4ba 100644 --- a/data/src/main/sqldelight/tachiyomi/data/excluded_scanlators.sq +++ b/data/src/main/sqldelight/tachiyomi/data/excluded_scanlators.sq @@ -1,7 +1,8 @@ CREATE TABLE excluded_scanlators( manga_id INTEGER NOT NULL, scanlator TEXT NOT NULL, - FOREIGN KEY(manga_id) REFERENCES mangas (_id) + UNIQUE(manga_id, scanlator) ON CONFLICT IGNORE, + FOREIGN KEY(manga_id) REFERENCES mangas(id) ON DELETE CASCADE ); diff --git a/data/src/main/sqldelight/tachiyomi/data/history.sq b/data/src/main/sqldelight/tachiyomi/data/history.sq index 6ddb06ca7..32677e8ac 100644 --- a/data/src/main/sqldelight/tachiyomi/data/history.sq +++ b/data/src/main/sqldelight/tachiyomi/data/history.sq @@ -1,54 +1,55 @@ import java.util.Date; CREATE TABLE history( - _id INTEGER NOT NULL PRIMARY KEY, + id INTEGER NOT NULL PRIMARY KEY, chapter_id INTEGER NOT NULL UNIQUE, last_read INTEGER AS Date, time_read INTEGER NOT NULL, - FOREIGN KEY(chapter_id) REFERENCES chapters (_id) + UNIQUE(chapter_id) ON CONFLICT IGNORE, + FOREIGN KEY(chapter_id) REFERENCES chapters (id) ON DELETE CASCADE ); -CREATE INDEX history_history_chapter_id_index ON history(chapter_id); +CREATE INDEX history_chapter_id_index ON history(chapter_id); getHistoryByMangaId: SELECT -H._id, +H.id, H.chapter_id, H.last_read, H.time_read FROM history H JOIN chapters C -ON H.chapter_id = C._id -WHERE C.manga_id = :mangaId AND C._id = H.chapter_id; +ON H.chapter_id = C.id +WHERE C.manga_id = :mangaId AND C.id = H.chapter_id; getHistoryByChapterUrl: SELECT -H._id, +H.id, H.chapter_id, H.last_read, H.time_read FROM history H JOIN chapters C -ON H.chapter_id = C._id -WHERE C.url = :chapterUrl AND C._id = H.chapter_id; +ON H.chapter_id = C.id +WHERE C.url = :chapterUrl AND C.id = H.chapter_id; resetHistoryById: UPDATE history SET last_read = 0 -WHERE _id = :historyId; +WHERE id = :historyId; resetHistoryByMangaId: UPDATE history SET last_read = 0 -WHERE _id IN ( - SELECT H._id +WHERE id IN ( + SELECT H.id FROM mangas M INNER JOIN chapters C - ON M._id = C.manga_id + ON M.id = C.manga_id INNER JOIN history H - ON C._id = H.chapter_id - WHERE M._id = :mangaId + ON C.id = H.chapter_id + WHERE M.id = :mangaId ); removeAllHistory: diff --git a/data/src/main/sqldelight/tachiyomi/data/manga_sync.sq b/data/src/main/sqldelight/tachiyomi/data/manga_sync.sq index bf8d0cba3..af25ff6a5 100644 --- a/data/src/main/sqldelight/tachiyomi/data/manga_sync.sq +++ b/data/src/main/sqldelight/tachiyomi/data/manga_sync.sq @@ -1,5 +1,5 @@ CREATE TABLE manga_sync( - _id INTEGER NOT NULL PRIMARY KEY, + id INTEGER NOT NULL PRIMARY KEY, manga_id INTEGER NOT NULL, sync_id INTEGER NOT NULL, remote_id INTEGER NOT NULL, @@ -12,11 +12,13 @@ CREATE TABLE manga_sync( remote_url TEXT NOT NULL, start_date INTEGER NOT NULL, finish_date INTEGER NOT NULL, - UNIQUE (manga_id, sync_id) ON CONFLICT REPLACE, - FOREIGN KEY(manga_id) REFERENCES mangas (_id) + UNIQUE(manga_id, sync_id) ON CONFLICT IGNORE, + FOREIGN KEY(manga_id) REFERENCES mangas(id) ON DELETE CASCADE ); +CREATE INDEX manga_sync_manga_id_index ON manga_sync(manga_id); + delete: DELETE FROM manga_sync WHERE manga_id = :mangaId AND sync_id = :syncId; @@ -28,7 +30,7 @@ FROM manga_sync; getTrackById: SELECT * FROM manga_sync -WHERE _id = :id; +WHERE id = :id; getTracksByMangaId: SELECT * @@ -54,4 +56,4 @@ SET remote_url = coalesce(:trackingUrl, remote_url), start_date = coalesce(:startDate, start_date), finish_date = coalesce(:finishDate, finish_date) -WHERE _id = :id; +WHERE id = :id; diff --git a/data/src/main/sqldelight/tachiyomi/data/mangas.sq b/data/src/main/sqldelight/tachiyomi/data/mangas.sq index 3e21e5bc0..57baa3ca7 100644 --- a/data/src/main/sqldelight/tachiyomi/data/mangas.sq +++ b/data/src/main/sqldelight/tachiyomi/data/mangas.sq @@ -4,7 +4,7 @@ import kotlin.Boolean; import kotlin.String; CREATE TABLE mangas( - _id INTEGER NOT NULL PRIMARY KEY, + id INTEGER NOT NULL PRIMARY KEY, source INTEGER NOT NULL, url TEXT NOT NULL, artist TEXT, @@ -27,7 +27,8 @@ CREATE TABLE mangas( last_modified_at INTEGER NOT NULL DEFAULT 0, favorite_modified_at INTEGER, version INTEGER NOT NULL DEFAULT 0, - is_syncing INTEGER NOT NULL DEFAULT 0 + is_syncing INTEGER NOT NULL DEFAULT 0, + UNIQUE(source, url) ON CONFLICT IGNORE ); CREATE INDEX library_favorite_index ON mangas(favorite) WHERE favorite = 1; @@ -38,7 +39,7 @@ AFTER UPDATE OF favorite ON mangas BEGIN UPDATE mangas SET favorite_modified_at = strftime('%s', 'now') - WHERE _id = new._id; + WHERE id = new.id; END; CREATE TRIGGER update_last_modified_at_mangas @@ -47,13 +48,13 @@ FOR EACH ROW BEGIN UPDATE mangas SET last_modified_at = strftime('%s', 'now') - WHERE _id = new._id; + WHERE id = new.id; END; CREATE TRIGGER update_manga_version AFTER UPDATE ON mangas BEGIN UPDATE mangas SET version = version + 1 - WHERE _id = new._id AND new.is_syncing = 0 AND ( + WHERE id = new.id AND new.is_syncing = 0 AND ( new.url != old.url OR new.description != old.description OR new.favorite != old.favorite @@ -63,15 +64,13 @@ END; getMangaById: SELECT * FROM mangas -WHERE _id = :id; +WHERE id = :id; --- TODO: this should ideally never really have more than 1 result getMangaByUrlAndSource: SELECT * FROM mangas WHERE url = :url -AND source = :source -LIMIT 1; +AND source = :source; getFavorites: SELECT * @@ -110,7 +109,7 @@ SELECT * FROM mangas WHERE favorite = 1 AND LOWER(title) = :title -AND _id != :id; +AND id != :id; getUpcomingManga: SELECT * @@ -167,7 +166,7 @@ UPDATE mangas SET calculate_interval = coalesce(:calculateInterval, calculate_interval), version = coalesce(:version, version), is_syncing = coalesce(:isSyncing, is_syncing) -WHERE _id = :mangaId; +WHERE id = :mangaId; selectLastInsertedRowId: SELECT last_insert_rowid(); diff --git a/data/src/main/sqldelight/tachiyomi/data/mangas_categories.sq b/data/src/main/sqldelight/tachiyomi/data/mangas_categories.sq index 3d8c815c9..ac7184715 100644 --- a/data/src/main/sqldelight/tachiyomi/data/mangas_categories.sq +++ b/data/src/main/sqldelight/tachiyomi/data/mangas_categories.sq @@ -1,18 +1,21 @@ CREATE TABLE mangas_categories( - _id INTEGER NOT NULL PRIMARY KEY, manga_id INTEGER NOT NULL, category_id INTEGER NOT NULL, - FOREIGN KEY(category_id) REFERENCES categories (_id) + UNIQUE(manga_id, category_id) ON CONFLICT IGNORE, + FOREIGN KEY(category_id) REFERENCES categories(id) ON DELETE CASCADE, - FOREIGN KEY(manga_id) REFERENCES mangas (_id) + FOREIGN KEY(manga_id) REFERENCES mangas(id) ON DELETE CASCADE ); +CREATE INDEX mangas_categories_manga_id_index ON mangas_categories(manga_id); +CREATE INDEX mangas_categories_category_id_index ON mangas_categories(category_id); + CREATE TRIGGER insert_manga_category_update_version AFTER INSERT ON mangas_categories BEGIN UPDATE mangas SET version = version + 1 - WHERE _id = new.manga_id AND (SELECT is_syncing FROM mangas WHERE _id = new.manga_id) = 0; + WHERE id = new.manga_id AND (SELECT is_syncing FROM mangas WHERE id = new.manga_id) = 0; END; insert: @@ -21,4 +24,4 @@ VALUES (:mangaId, :categoryId); deleteMangaCategoryByMangaId: DELETE FROM mangas_categories -WHERE manga_id = :mangaId; \ No newline at end of file +WHERE manga_id = :mangaId; diff --git a/data/src/main/sqldelight/tachiyomi/data/sources.sq b/data/src/main/sqldelight/tachiyomi/data/sources.sq index aac60bb78..9a8396f28 100644 --- a/data/src/main/sqldelight/tachiyomi/data/sources.sq +++ b/data/src/main/sqldelight/tachiyomi/data/sources.sq @@ -1,5 +1,5 @@ CREATE TABLE sources( - _id INTEGER NOT NULL PRIMARY KEY, + id INTEGER NOT NULL PRIMARY KEY, lang TEXT NOT NULL, name TEXT NOT NULL ); @@ -11,14 +11,14 @@ FROM sources; findOne: SELECT * FROM sources -WHERE _id = :id; +WHERE id = :id; upsert: -INSERT INTO sources(_id, lang, name) +INSERT INTO sources(id, lang, name) VALUES (:id, :lang, :name) -ON CONFLICT(_id) +ON CONFLICT(id) DO UPDATE SET lang = :lang, name = :name -WHERE _id = :id; \ No newline at end of file +WHERE id = :id; diff --git a/data/src/main/sqldelight/tachiyomi/migrations/4.sqm b/data/src/main/sqldelight/tachiyomi/migrations/4.sqm new file mode 100644 index 000000000..4983ad041 --- /dev/null +++ b/data/src/main/sqldelight/tachiyomi/migrations/4.sqm @@ -0,0 +1,252 @@ +import eu.kanade.tachiyomi.source.model.UpdateStrategy; +import java.util.Date; +import kotlin.Boolean; +import kotlin.String; +import kotlin.collections.List; + +DROP INDEX IF EXISTS chapters_manga_id_index; +DROP INDEX IF EXISTS chapters_unread_by_manga_index; +DROP INDEX IF EXISTS history_history_chapter_id_index; +DROP INDEX IF EXISTS library_favorite_index; +DROP INDEX IF EXISTS mangas_url_index; + +DROP VIEW IF EXISTS historyView; +DROP VIEW IF EXISTS libraryView; +DROP VIEW IF EXISTS updatesView; + +ALTER TABLE mangas RENAME TO manga_temp; +CREATE TABLE mangas( + id INTEGER NOT NULL PRIMARY KEY, + source INTEGER NOT NULL, + url TEXT NOT NULL, + artist TEXT, + author TEXT, + description TEXT, + genre TEXT AS List, + title TEXT NOT NULL, + status INTEGER NOT NULL, + thumbnail_url TEXT, + favorite INTEGER AS Boolean NOT NULL, + last_update INTEGER, + next_update INTEGER, + initialized INTEGER AS Boolean NOT NULL, + viewer INTEGER NOT NULL, + chapter_flags INTEGER NOT NULL, + cover_last_modified INTEGER NOT NULL, + date_added INTEGER NOT NULL, + update_strategy INTEGER AS UpdateStrategy NOT NULL DEFAULT 0, + calculate_interval INTEGER DEFAULT 0 NOT NULL, + last_modified_at INTEGER NOT NULL DEFAULT 0, + favorite_modified_at INTEGER, + version INTEGER NOT NULL DEFAULT 0, + is_syncing INTEGER NOT NULL DEFAULT 0, + UNIQUE(source, url) ON CONFLICT IGNORE +); +INSERT INTO mangas +SELECT + _id, source, url, artist, author, description, genre, title, status, thumbnail_url, favorite, + last_update, next_update, initialized, viewer, chapter_flags, cover_last_modified, date_added, + update_strategy, calculate_interval, last_modified_at, favorite_modified_at, version, is_syncing +FROM manga_temp; + +ALTER TABLE categories RENAME TO categories_temp; +CREATE TABLE categories( + id INTEGER NOT NULL PRIMARY KEY, + name TEXT NOT NULL, + `order` INTEGER NOT NULL, + flags INTEGER NOT NULL, + UNIQUE(`order`) ON CONFLICT ABORT +); +INSERT INTO categories +SELECT _id, name, (RANK() OVER (ORDER BY sort)) - 2, flags +FROM categories_temp; + +ALTER TABLE chapters RENAME TO chapters_temp; +CREATE TABLE chapters( + id INTEGER NOT NULL PRIMARY KEY, + manga_id INTEGER NOT NULL, + url TEXT NOT NULL, + name TEXT NOT NULL, + scanlator TEXT, + read INTEGER AS Boolean NOT NULL, + bookmark INTEGER AS Boolean NOT NULL, + last_page_read INTEGER NOT NULL, + chapter_number REAL NOT NULL, + source_order INTEGER NOT NULL, + date_fetch INTEGER NOT NULL, + date_upload INTEGER NOT NULL, + last_modified_at INTEGER NOT NULL DEFAULT 0, + version INTEGER NOT NULL DEFAULT 0, + is_syncing INTEGER NOT NULL DEFAULT 0, + UNIQUE(manga_id, url) ON CONFLICT IGNORE, + FOREIGN KEY(manga_id) REFERENCES mangas(id) + ON DELETE CASCADE +); +INSERT INTO chapters +SELECT + _id, manga_id, url, name, scanlator, read, bookmark, last_page_read, chapter_number, source_order, + date_fetch, date_upload, last_modified_at, version, is_syncing +FROM chapters_temp; + +ALTER TABLE excluded_scanlators RENAME TO excluded_scanlators_temp; +CREATE TABLE excluded_scanlators( + manga_id INTEGER NOT NULL, + scanlator TEXT NOT NULL, + UNIQUE(manga_id, scanlator) ON CONFLICT IGNORE, + FOREIGN KEY(manga_id) REFERENCES mangas(id) + ON DELETE CASCADE +); +INSERT INTO excluded_scanlators +SELECT manga_id, scanlator +FROM excluded_scanlators_temp; + +ALTER TABLE history RENAME TO history_temp; +CREATE TABLE history( + id INTEGER NOT NULL PRIMARY KEY, + chapter_id INTEGER NOT NULL UNIQUE, + last_read INTEGER AS Date, + time_read INTEGER NOT NULL, + FOREIGN KEY(chapter_id) REFERENCES chapters (id) + ON DELETE CASCADE +); +INSERT INTO history +SELECT history_id, history_chapter_id, history_last_read, history_time_read +FROM history_temp; + +ALTER TABLE mangas_categories RENAME TO mangas_categories_temp; +CREATE TABLE mangas_categories( + manga_id INTEGER NOT NULL, + category_id INTEGER NOT NULL, + UNIQUE(manga_id, category_id) ON CONFLICT IGNORE, + FOREIGN KEY(category_id) REFERENCES categories(id) + ON DELETE CASCADE, + FOREIGN KEY(manga_id) REFERENCES mangas(id) + ON DELETE CASCADE +); +INSERT INTO mangas_categories +SELECT manga_id, category_id +FROM mangas_categories_temp; + +ALTER TABLE manga_sync RENAME TO manga_sync_temp; +CREATE TABLE manga_sync( + id INTEGER NOT NULL PRIMARY KEY, + manga_id INTEGER NOT NULL, + sync_id INTEGER NOT NULL, + remote_id INTEGER NOT NULL, + library_id INTEGER, + title TEXT NOT NULL, + last_chapter_read REAL NOT NULL, + total_chapters INTEGER NOT NULL, + status INTEGER NOT NULL, + score REAL NOT NULL, + remote_url TEXT NOT NULL, + start_date INTEGER NOT NULL, + finish_date INTEGER NOT NULL, + UNIQUE(manga_id, sync_id) ON CONFLICT IGNORE, + FOREIGN KEY(manga_id) REFERENCES mangas(id) + ON DELETE CASCADE +); +INSERT INTO manga_sync +SELECT + _id, manga_id, sync_id, remote_id, library_id, title, last_chapter_read, total_chapters, status, + score, remote_url, start_date, finish_date +FROM manga_sync_temp; + +CREATE INDEX chapters_manga_id_index ON chapters(manga_id); +CREATE INDEX excluded_scanlators_manga_id_index ON excluded_scanlators(manga_id); +CREATE INDEX history_chapter_id_index ON history(chapter_id); +CREATE INDEX manga_sync_manga_id_index ON manga_sync(manga_id); +CREATE INDEX library_favorite_index ON mangas(favorite) WHERE favorite = 1; +CREATE INDEX mangas_url_index ON mangas(url); +CREATE INDEX mangas_categories_manga_id_index ON mangas_categories(manga_id); +CREATE INDEX mangas_categories_category_id_index ON mangas_categories(category_id); + +CREATE VIEW historyView AS +SELECT + history.id AS id, + mangas.id AS mangaId, + chapters.id AS chapterId, + mangas.title, + mangas.thumbnail_url AS thumbnailUrl, + mangas.source, + mangas.favorite, + mangas.cover_last_modified, + chapters.chapter_number AS chapterNumber, + history.last_read AS readAt, + history.time_read AS readDuration, + max_last_read.last_read AS maxReadAt, + max_last_read.chapter_id AS maxReadAtChapterId +FROM mangas +JOIN chapters +ON mangas.id = chapters.manga_id +JOIN history +ON chapters.id = history.chapter_id +JOIN ( + SELECT chapters.manga_id,chapters.id AS chapter_id, MAX(history.last_read) AS last_read + FROM chapters JOIN history + ON chapters.id = history.chapter_id + GROUP BY chapters.manga_id +) AS max_last_read; + +CREATE VIEW libraryView AS +SELECT + M.*, + coalesce(C.total, 0) AS totalCount, + 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(C.bookmarkCount, 0) AS bookmarkCount, + coalesce(MC.category_id, 0) AS category +FROM mangas M +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, + sum(chapters.bookmark) AS bookmarkCount + FROM chapters + LEFT JOIN excluded_scanlators + ON chapters.manga_id = excluded_scanlators.manga_id + AND chapters.scanlator = excluded_scanlators.scanlator + LEFT JOIN history + ON chapters.id = history.chapter_id + WHERE excluded_scanlators.scanlator IS NULL + GROUP BY chapters.manga_id +) AS C +ON M.id = C.manga_id +LEFT JOIN mangas_categories AS MC +ON MC.manga_id = M.id +WHERE M.favorite = 1; + +CREATE VIEW updatesView AS +SELECT + mangas.id AS mangaId, + mangas.title AS mangaTitle, + chapters.id AS chapterId, + chapters.name AS chapterName, + chapters.scanlator, + chapters.read, + chapters.bookmark, + chapters.last_page_read, + mangas.source, + mangas.favorite, + mangas.thumbnail_url AS thumbnailUrl, + mangas.cover_last_modified AS coverLastModified, + chapters.date_upload AS dateUpload, + chapters.date_fetch AS datefetch +FROM mangas JOIN chapters +ON mangas.id = chapters.manga_id +WHERE favorite = 1 +AND date_fetch > date_added +ORDER BY date_fetch DESC; + +DROP TABLE IF EXISTS manga_temp; +DROP TABLE IF EXISTS categories_temp; +DROP TABLE IF EXISTS chapters_temp; +DROP TABLE IF EXISTS history_temp; +DROP TABLE IF EXISTS mangas_categories_temp; +DROP TABLE IF EXISTS manga_sync_temp; diff --git a/data/src/main/sqldelight/tachiyomi/view/historyView.sq b/data/src/main/sqldelight/tachiyomi/view/historyView.sq index bdaeb5259..f79fe5c47 100644 --- a/data/src/main/sqldelight/tachiyomi/view/historyView.sq +++ b/data/src/main/sqldelight/tachiyomi/view/historyView.sq @@ -1,8 +1,8 @@ CREATE VIEW historyView AS SELECT - history._id AS id, - mangas._id AS mangaId, - chapters._id AS chapterId, + history.id AS id, + mangas.id AS mangaId, + chapters.id AS chapterId, mangas.title, mangas.thumbnail_url AS thumbnailUrl, mangas.source, @@ -15,16 +15,15 @@ SELECT max_last_read.chapter_id AS maxReadAtChapterId FROM mangas JOIN chapters -ON mangas._id = chapters.manga_id +ON mangas.id = chapters.manga_id JOIN history -ON chapters._id = history.chapter_id +ON chapters.id = history.chapter_id JOIN ( - SELECT chapters.manga_id,chapters._id AS chapter_id, MAX(history.last_read) AS last_read + SELECT chapters.manga_id,chapters.id AS chapter_id, MAX(history.last_read) AS last_read FROM chapters JOIN history - ON chapters._id = history.chapter_id + ON chapters.id = history.chapter_id GROUP BY chapters.manga_id -) AS max_last_read -ON chapters.manga_id = max_last_read.manga_id; +) AS max_last_read; history: SELECT diff --git a/data/src/main/sqldelight/tachiyomi/view/libraryView.sq b/data/src/main/sqldelight/tachiyomi/view/libraryView.sq index 0a5d28543..f216faffc 100644 --- a/data/src/main/sqldelight/tachiyomi/view/libraryView.sq +++ b/data/src/main/sqldelight/tachiyomi/view/libraryView.sq @@ -23,13 +23,13 @@ LEFT JOIN( ON chapters.manga_id = excluded_scanlators.manga_id AND chapters.scanlator = excluded_scanlators.scanlator LEFT JOIN history - ON chapters._id = history.chapter_id + ON chapters.id = history.chapter_id WHERE excluded_scanlators.scanlator IS NULL GROUP BY chapters.manga_id ) AS C -ON M._id = C.manga_id +ON M.id = C.manga_id LEFT JOIN mangas_categories AS MC -ON MC.manga_id = M._id +ON MC.manga_id = M.id WHERE M.favorite = 1; library: diff --git a/data/src/main/sqldelight/tachiyomi/view/updatesView.sq b/data/src/main/sqldelight/tachiyomi/view/updatesView.sq index 5fdd3ea75..94ad5fad6 100644 --- a/data/src/main/sqldelight/tachiyomi/view/updatesView.sq +++ b/data/src/main/sqldelight/tachiyomi/view/updatesView.sq @@ -1,8 +1,8 @@ CREATE VIEW updatesView AS SELECT - mangas._id AS mangaId, + mangas.id AS mangaId, mangas.title AS mangaTitle, - chapters._id AS chapterId, + chapters.id AS chapterId, chapters.name AS chapterName, chapters.scanlator, chapters.read, @@ -15,7 +15,7 @@ SELECT chapters.date_upload AS dateUpload, chapters.date_fetch AS datefetch FROM mangas JOIN chapters -ON mangas._id = chapters.manga_id +ON mangas.id = chapters.manga_id WHERE favorite = 1 AND date_fetch > date_added ORDER BY date_fetch DESC;