import kotlin.Int;

CREATE TABLE search_metadata (
    manga_id INTEGER NOT NULL PRIMARY KEY,
    uploader TEXT,
    extra TEXT NOT NULL,
    indexed_extra TEXT,
    extra_version INTEGER NOT NULL,
    FOREIGN KEY(manga_id) REFERENCES mangas (_id)
    ON DELETE CASCADE
);

CREATE INDEX search_metadata_uploader_index ON search_metadata(uploader);
CREATE INDEX search_metadata_indexed_extra_index ON search_metadata(indexed_extra);

selectAll:
SELECT * FROM search_metadata;

selectByMangaId:
SELECT * FROM search_metadata WHERE manga_id = ?;

selectByIndexedExtra:
SELECT * FROM search_metadata WHERE indexed_extra = ?;

upsert:
INSERT INTO search_metadata(manga_id, uploader, extra, indexed_extra, extra_version)
VALUES (:mangaId, :uploader, :extra, :indexedExtra, :extraVersion)
ON CONFLICT(manga_id)
DO UPDATE
SET
    uploader = :uploader,
    extra = :extra,
    indexed_extra = :indexedExtra,
    extra_version = :extraVersion
WHERE manga_id = :mangaId;

insert:
INSERT INTO search_metadata (manga_id, uploader, extra, indexed_extra, extra_version)
VALUES (?, ?, ?, ?, ?);

deleteAll:
DELETE FROM search_metadata;