skeleton/internal/database/sql/queries/email.sql

71 lines
1.4 KiB
MySQL
Raw Normal View History

2020-07-19 14:08:18 +02:00
-- name: CreateEmail :one
INSERT INTO "email" (
2020-07-21 22:50:11 +02:00
"address",
"identity_id",
"is_verified"
2020-07-19 14:08:18 +02:00
) VALUES (
2020-07-21 22:50:11 +02:00
$1, $2, $3
2020-07-19 14:08:18 +02:00
) RETURNING *;
2020-07-21 22:50:11 +02:00
-- name: UpdateEmailVerified :exec
2020-07-19 14:08:18 +02:00
UPDATE "email" SET (
2020-07-21 22:50:11 +02:00
"is_verified"
2020-07-19 14:08:18 +02:00
) = (
2020-07-21 22:50:11 +02:00
$2
) WHERE "address" = $1;
2020-07-19 14:08:18 +02:00
2020-07-21 22:50:11 +02:00
-- name: UpdateEmailPrimary :exec
-- UpdateEmailPrimary sets exactly one primary email for an identity.
-- The mail address has to have been verified.
-- this query basically combines these two queries into one atomic one:
-- UPDATE "email" SET "is_primary" = false WHERE "identity_id" = $1;
-- UPDATE "email" SET "is_primary" = true WHERE "identity_id" = $1 AND "address" = $2 AND "is_verified" = true;
UPDATE "email"
SET
"is_primary" = NOT "is_primary"
WHERE
"identity_id" = $1 AND (
( "address" <> $2 AND "is_primary" = true ) OR
( "address" = $2 AND "is_primary" = false AND "is_verified" = true )
);
2020-07-19 14:08:18 +02:00
-- name: GetEmailByAddress :one
SELECT
2020-07-21 22:50:11 +02:00
*
FROM "email"
WHERE
"address" = $1;
-- name: GetEmailByIdentityID :many
SELECT
*
FROM "email"
WHERE
"identity_id" = $1;
2020-07-19 14:08:18 +02:00
2020-07-21 22:50:11 +02:00
-- name: GetPrimaryEmailByIdentityID :one
2020-07-19 14:08:18 +02:00
SELECT
2020-07-21 22:50:11 +02:00
*
FROM "email"
WHERE
"identity_id" = $1 AND "is_primary";
-- name: CountEmailsByIdentityID :one
SELECT
COUNT(*)
FROM
"email"
WHERE
"identity_id" = $1;
-- name: CountUnverifiedEmailsByIdentityID :one
SELECT
COUNT(*)
FROM
"email"
WHERE
"identity_id" = $1 AND
"is_verified" = FALSE;
-- name: DestroyEmail :exec
DELETE FROM "email" WHERE "address" = $1;