-- name: CreateSysConfig :exec
INSERT INTO sys_config (
    key,
    value
) VALUES (
  $1, $2
);

-- name: GetSysConfig :one
SELECT *
FROM sys_config
WHERE id = $1
LIMIT 1;

-- name: GetSysConfigByKey :one
SELECT *
FROM sys_config
WHERE key = $1
LIMIT 1;

-- name: UpdateSysConfigByKey :exec
UPDATE sys_config
SET value = $2
WHERE key = $1;

-- name: ExistsSysConfigByKey :one
SELECT EXISTS (
    SELECT 1 FROM sys_config WHERE key = $1
);

-- name: CountSysConfigCondition :one
SELECT COUNT(*) FROM sys_config
WHERE (@key::text = '' OR key ILIKE '%' || @key || '%');

-- name: ListSysConfigCondition :many
SELECT *
FROM sys_config
WHERE (@key::text = '' OR key ILIKE '%' || @key || '%')
ORDER BY created_at DESC
OFFSET @skip
LIMIT @size;