-- name: CreateCategory :exec
INSERT INTO categories(name, code)
VALUES ($1, $2);

-- name: ListCategories :many
SELECT *
FROM categories
ORDER BY id LIMIT $1
OFFSET $2;

-- name: CountCategories :one
SELECT COUNT(*)
FROM categories;

-- name: ListAllCategories :many
SELECT *
FROM categories;

-- name: GetCategoryById :one
SELECT *
FROM categories
WHERE id = $1;

-- name: UpdateCategory :execrows
UPDATE categories
SET name = coalesce(sqlc.narg('name'), name),
    code = coalesce(sqlc.narg('code'), code)
WHERE id = sqlc.arg('id');

-- name: DeleteCategory :execrows
DELETE
FROM categories
WHERE id = $1;

-- name: CreatePostCategory :exec
INSERT INTO post_category(post_id, category_id)
VALUES ($1, $2);

-- name: DeletePostCategory :exec
DELETE
FROM post_category
WHERE post_id = $1;