-- name: SetTag :exec
INSERT INTO common_tag (tag, target_kind, target_id)
VALUES (
    sqlc.arg(tag)::TEXT, sqlc.arg(target_kind)::TEXT, sqlc.arg(target_id)::TEXT
)
ON CONFLICT DO NOTHING;

-- name: ClearTag :exec
DELETE FROM common_tag
WHERE tag=sqlc.arg(tag)::TEXT
  AND target_kind=sqlc.arg(target_kind)::TEXT
  AND target_id=sqlc.arg(target_id)::TEXT;

-- name: ClearTagsByTarget :exec
DELETE FROM common_tag
WHERE target_kind=sqlc.arg(target_kind)::TEXT
  AND target_id=sqlc.arg(target_id)::TEXT;

-- name: SelectDistinctTags :many
SELECT DISTINCT tag FROM common_tag;

-- name: SelectDistinctTagsByKind :many
SELECT DISTINCT tag FROM common_tag WHERE tag LIKE '$1::text%';

-- name: SelectTagsByTag :many
SELECT * FROM common_tag WHERE tag = sqlc.arg(tag_name)::text;

-- name: SelectTagsByTags :many
SELECT * FROM common_tag WHERE tag = ANY(sqlc.arg(tag_names)::text[]);

-- name: SelectTagsByTarget :many
SELECT * FROM common_tag WHERE target_kind = $1 AND target_id = $2::text;

-- name: SelectTagsByTargets :many
SELECT * FROM common_tag WHERE target_kind = $1 AND target_id = ANY($2::text[]);
