-- name: SelectChapter :one
SELECT * FROM story_chapter WHERE id=$1::TEXT LIMIT 1;

-- name: SelectChapters :many
SELECT * FROM story_chapter WHERE (sqlx.arg(story_id)::TEXT == '' OR story_id=sqlc.arg(story_id)::TEXT) ORDER BY created_date LIMIT sqlc.arg(limit_size)::INT;

-- name: InsertChapter :exec
INSERT INTO story_chapter (id, story_id, title, author, source, created_date, fictional_date, edited_date, comment_mode, comments_locked)
VALUES (
    sqlc.arg(id)::TEXT, sqlc.arg(story_id)::TEXT, sqlc.arg(title)::TEXT, sqlc.arg(author)::TEXT, sqlc.arg(source)::TEXT,
    sqlc.arg(created_date)::TIMESTAMP, sqlc.arg(fictional_date)::TIMESTAMP, sqlc.arg(edited_date)::TIMESTAMP,
    sqlc.arg(comment_mode)::TEXT, sqlc.arg(comments_locked)::BOOLEAN
);

-- name: UpdateChapterStoryID :exec
UPDATE story_chapter
SET story_id=sqlc.arg(story_id)::TEXT
WHERE id=sqlc.arg(id);

-- name: UpdateChapter :exec
UPDATE story_chapter
SET title=sqlc.arg(title),
    source=sqlc.arg(source),
    fictional_date=sqlc.arg(fictional_date),
    comment_mode=sqlc.arg(comment_mode),
    comments_locked=sqlc.arg(comments_locked)
WHERE id=sqlc.arg(id);

-- name: DeleteChapter :exec
DELETE FROM story_chapter WHERE id=$1;

-- name: DeleteChaptersByStoryID :exec
DELETE FROM story_chapter WHERE story_id=$1;
