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

-- name: SelectChanges :many
SELECT * FROM data_change
WHERE (@filter_keys::bool = false OR keys && (@keys::text[]))
  AND (@filter_earliest_date::bool = false OR date >= @earliest_date::timestamp)
  AND (@filter_latest_date::bool = false OR date <= @latest_date::timestamp)
  AND (@filter_author::bool = false OR author = @author::text)
ORDER BY date DESC
LIMIT NULLIF(@limit_size::INT, 0);

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

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