-- name: SelectChangeByID :one
SELECT * FROM data_change WHERE id = $1 LIMIT 1;

-- name: SelectChanges :many
SELECT * FROM data_change
WHERE (sqlc.arg(filter_keys)::bool = false OR keys && (sqlc.arg(keys)::text[]))
  AND (sqlc.arg(filter_earliest_date)::bool = false OR date >= sqlc.arg(earliest_date)::timestamp)
  AND (sqlc.arg(filter_latest_date)::bool = false OR date <= sqlc.arg(latest_date)::timestamp)
  AND (sqlc.arg(filter_author)::bool = false OR author = sqlc.arg(author)::text)
LIMIT sqlc.arg(limit_size)::int;

-- name: InsertChange :exec
INSERT INTO data_change (id, model, op, author, listed, date, keys, objects)
VALUES (
    sqlc.arg(id)::text, sqlc.arg(model)::text, sqlc.arg(op)::text, sqlc.arg(author)::text,
    sqlc.arg(listed)::boolean, sqlc.arg(date)::timestamp, sqlc.arg(keys)::text[],
    sqlc.arg(objects)::jsonb
);

-- name: DeleteChange :exec
DELETE FROM data_change WHERE id = $1;