skeleton/internal/database/email.sql.go
2020-07-21 22:50:11 +02:00

197 lines
4.7 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// source: email.sql
package database
import (
"context"
)
const countEmailsByIdentityID = `-- name: CountEmailsByIdentityID :one
SELECT
COUNT(*)
FROM
"email"
WHERE
"identity_id" = $1
`
func (q *Queries) CountEmailsByIdentityID(ctx context.Context, identityID int64) (int64, error) {
row := q.db.QueryRowContext(ctx, countEmailsByIdentityID, identityID)
var count int64
err := row.Scan(&count)
return count, err
}
const countUnverifiedEmailsByIdentityID = `-- name: CountUnverifiedEmailsByIdentityID :one
SELECT
COUNT(*)
FROM
"email"
WHERE
"identity_id" = $1 AND
"is_verified" = FALSE
`
func (q *Queries) CountUnverifiedEmailsByIdentityID(ctx context.Context, identityID int64) (int64, error) {
row := q.db.QueryRowContext(ctx, countUnverifiedEmailsByIdentityID, identityID)
var count int64
err := row.Scan(&count)
return count, err
}
const createEmail = `-- name: CreateEmail :one
INSERT INTO "email" (
"address",
"identity_id",
"is_verified"
) VALUES (
$1, $2, $3
) RETURNING address, identity_id, is_verified, is_primary, created_at
`
type CreateEmailParams struct {
Address string `json:"address"`
IdentityID int64 `json:"identity_id"`
IsVerified bool `json:"is_verified"`
}
func (q *Queries) CreateEmail(ctx context.Context, arg CreateEmailParams) (Email, error) {
row := q.db.QueryRowContext(ctx, createEmail, arg.Address, arg.IdentityID, arg.IsVerified)
var i Email
err := row.Scan(
&i.Address,
&i.IdentityID,
&i.IsVerified,
&i.IsPrimary,
&i.CreatedAt,
)
return i, err
}
const destroyEmail = `-- name: DestroyEmail :exec
DELETE FROM "email" WHERE "address" = $1
`
func (q *Queries) DestroyEmail(ctx context.Context, address string) error {
_, err := q.db.ExecContext(ctx, destroyEmail, address)
return err
}
const getEmailByAddress = `-- name: GetEmailByAddress :one
SELECT
address, identity_id, is_verified, is_primary, created_at
FROM "email"
WHERE
"address" = $1
`
func (q *Queries) GetEmailByAddress(ctx context.Context, address string) (Email, error) {
row := q.db.QueryRowContext(ctx, getEmailByAddress, address)
var i Email
err := row.Scan(
&i.Address,
&i.IdentityID,
&i.IsVerified,
&i.IsPrimary,
&i.CreatedAt,
)
return i, err
}
const getEmailByIdentityID = `-- name: GetEmailByIdentityID :many
SELECT
address, identity_id, is_verified, is_primary, created_at
FROM "email"
WHERE
"identity_id" = $1
`
func (q *Queries) GetEmailByIdentityID(ctx context.Context, identityID int64) ([]Email, error) {
rows, err := q.db.QueryContext(ctx, getEmailByIdentityID, identityID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Email
for rows.Next() {
var i Email
if err := rows.Scan(
&i.Address,
&i.IdentityID,
&i.IsVerified,
&i.IsPrimary,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getPrimaryEmailByIdentityID = `-- name: GetPrimaryEmailByIdentityID :one
SELECT
address, identity_id, is_verified, is_primary, created_at
FROM "email"
WHERE
"identity_id" = $1 AND "is_primary"
`
func (q *Queries) GetPrimaryEmailByIdentityID(ctx context.Context, identityID int64) (Email, error) {
row := q.db.QueryRowContext(ctx, getPrimaryEmailByIdentityID, identityID)
var i Email
err := row.Scan(
&i.Address,
&i.IdentityID,
&i.IsVerified,
&i.IsPrimary,
&i.CreatedAt,
)
return i, err
}
const updateEmailPrimary = `-- name: UpdateEmailPrimary :exec
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 )
)
`
type UpdateEmailPrimaryParams struct {
IdentityID int64 `json:"identity_id"`
Address string `json:"address"`
}
// 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;
func (q *Queries) UpdateEmailPrimary(ctx context.Context, arg UpdateEmailPrimaryParams) error {
_, err := q.db.ExecContext(ctx, updateEmailPrimary, arg.IdentityID, arg.Address)
return err
}
const updateEmailVerified = `-- name: UpdateEmailVerified :exec
UPDATE "email" SET (
"is_verified"
) = (
$2
) WHERE "address" = $1
`
func (q *Queries) UpdateEmailVerified(ctx context.Context, address string) error {
_, err := q.db.ExecContext(ctx, updateEmailVerified, address)
return err
}