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 REPLACE,
    FOREIGN KEY(manga_id) REFERENCES mangas (_id)
    ON DELETE CASCADE
);

getAllByMangaId:
SELECT *
FROM manga_sync
WHERE manga_id = :mangaId;

deleteForManga:
DELETE FROM manga_sync
WHERE manga_id = :mangaId AND sync_id = :syncId;

insert:
INSERT INTO manga_sync(manga_id, sync_id, remote_id, library_id, title, last_chapter_read, total_chapters, status, score, remote_url, start_date, finish_date)
VALUES(:mangaId, :syncId, :remoteId, :libraryId, :title, :lastChapterRead, :totalChapters, :status, :score, :remoteUrl, :startDate, :finishDate);

update:
UPDATE manga_sync
SET
    manga_id = coalesce(:mangaId, manga_id),
    sync_id = coalesce(:syncId, sync_id),
    remote_id = coalesce(:remoteId, remote_id),
    library_id = coalesce(:libraryId, library_id),
    title = coalesce(:title, title),
    last_chapter_read = coalesce(:lastChapterRead, last_chapter_read),
    total_chapters = coalesce(:totalChapters, total_chapters),
    status = coalesce(:status, status),
    score = coalesce(:score, score),
    remote_url = coalesce(:remoteUrl, remote_url),
    start_date = coalesce(:startDate, start_date),
    finish_date = coalesce(:finishDate, finish_date)
WHERE _id = :id;
