Yimaru-BackEnd/gen/db/scheduled_notification.sql.go

331 lines
8.9 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: scheduled_notification.sql
package dbgen
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const CancelScheduledNotification = `-- name: CancelScheduledNotification :one
UPDATE scheduled_notifications
SET
status = 'cancelled',
cancelled_at = CURRENT_TIMESTAMP,
updated_at = CURRENT_TIMESTAMP
WHERE id = $1
AND status IN ('pending', 'processing')
RETURNING id, channel, title, message, html, scheduled_at, status, target_user_ids, target_role, target_raw, attempt_count, last_error, processing_started_at, sent_at, cancelled_at, created_by, created_at, updated_at
`
func (q *Queries) CancelScheduledNotification(ctx context.Context, id int64) (ScheduledNotification, error) {
row := q.db.QueryRow(ctx, CancelScheduledNotification, id)
var i ScheduledNotification
err := row.Scan(
&i.ID,
&i.Channel,
&i.Title,
&i.Message,
&i.Html,
&i.ScheduledAt,
&i.Status,
&i.TargetUserIds,
&i.TargetRole,
&i.TargetRaw,
&i.AttemptCount,
&i.LastError,
&i.ProcessingStartedAt,
&i.SentAt,
&i.CancelledAt,
&i.CreatedBy,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const ClaimDueScheduledNotifications = `-- name: ClaimDueScheduledNotifications :many
UPDATE scheduled_notifications sn
SET
status = 'processing',
processing_started_at = CURRENT_TIMESTAMP,
updated_at = CURRENT_TIMESTAMP
WHERE sn.id IN (
SELECT id
FROM scheduled_notifications
WHERE status = 'pending'
AND scheduled_at <= CURRENT_TIMESTAMP
ORDER BY scheduled_at ASC
FOR UPDATE SKIP LOCKED
LIMIT $1
)
RETURNING id, channel, title, message, html, scheduled_at, status, target_user_ids, target_role, target_raw, attempt_count, last_error, processing_started_at, sent_at, cancelled_at, created_by, created_at, updated_at
`
func (q *Queries) ClaimDueScheduledNotifications(ctx context.Context, limit int32) ([]ScheduledNotification, error) {
rows, err := q.db.Query(ctx, ClaimDueScheduledNotifications, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ScheduledNotification
for rows.Next() {
var i ScheduledNotification
if err := rows.Scan(
&i.ID,
&i.Channel,
&i.Title,
&i.Message,
&i.Html,
&i.ScheduledAt,
&i.Status,
&i.TargetUserIds,
&i.TargetRole,
&i.TargetRaw,
&i.AttemptCount,
&i.LastError,
&i.ProcessingStartedAt,
&i.SentAt,
&i.CancelledAt,
&i.CreatedBy,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const CountScheduledNotifications = `-- name: CountScheduledNotifications :one
SELECT COUNT(*)
FROM scheduled_notifications
WHERE
($1::text IS NULL OR status = $1)
AND ($2::text IS NULL OR channel = $2)
AND ($3::timestamptz IS NULL OR scheduled_at >= $3)
AND ($4::timestamptz IS NULL OR scheduled_at <= $4)
`
type CountScheduledNotificationsParams struct {
FilterStatus pgtype.Text `json:"filter_status"`
FilterChannel pgtype.Text `json:"filter_channel"`
FilterAfter pgtype.Timestamptz `json:"filter_after"`
FilterBefore pgtype.Timestamptz `json:"filter_before"`
}
func (q *Queries) CountScheduledNotifications(ctx context.Context, arg CountScheduledNotificationsParams) (int64, error) {
row := q.db.QueryRow(ctx, CountScheduledNotifications,
arg.FilterStatus,
arg.FilterChannel,
arg.FilterAfter,
arg.FilterBefore,
)
var count int64
err := row.Scan(&count)
return count, err
}
const CreateScheduledNotification = `-- name: CreateScheduledNotification :one
INSERT INTO scheduled_notifications (
channel, title, message, html,
scheduled_at,
target_user_ids, target_role, target_raw,
created_by
) VALUES (
$1, $2, $3, $4,
$5,
$6, $7, $8,
$9
)
RETURNING id, channel, title, message, html, scheduled_at, status, target_user_ids, target_role, target_raw, attempt_count, last_error, processing_started_at, sent_at, cancelled_at, created_by, created_at, updated_at
`
type CreateScheduledNotificationParams struct {
Channel string `json:"channel"`
Title pgtype.Text `json:"title"`
Message string `json:"message"`
Html pgtype.Text `json:"html"`
ScheduledAt pgtype.Timestamptz `json:"scheduled_at"`
TargetUserIds []int64 `json:"target_user_ids"`
TargetRole pgtype.Text `json:"target_role"`
TargetRaw []byte `json:"target_raw"`
CreatedBy int64 `json:"created_by"`
}
func (q *Queries) CreateScheduledNotification(ctx context.Context, arg CreateScheduledNotificationParams) (ScheduledNotification, error) {
row := q.db.QueryRow(ctx, CreateScheduledNotification,
arg.Channel,
arg.Title,
arg.Message,
arg.Html,
arg.ScheduledAt,
arg.TargetUserIds,
arg.TargetRole,
arg.TargetRaw,
arg.CreatedBy,
)
var i ScheduledNotification
err := row.Scan(
&i.ID,
&i.Channel,
&i.Title,
&i.Message,
&i.Html,
&i.ScheduledAt,
&i.Status,
&i.TargetUserIds,
&i.TargetRole,
&i.TargetRaw,
&i.AttemptCount,
&i.LastError,
&i.ProcessingStartedAt,
&i.SentAt,
&i.CancelledAt,
&i.CreatedBy,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const GetScheduledNotification = `-- name: GetScheduledNotification :one
SELECT id, channel, title, message, html, scheduled_at, status, target_user_ids, target_role, target_raw, attempt_count, last_error, processing_started_at, sent_at, cancelled_at, created_by, created_at, updated_at FROM scheduled_notifications
WHERE id = $1
`
func (q *Queries) GetScheduledNotification(ctx context.Context, id int64) (ScheduledNotification, error) {
row := q.db.QueryRow(ctx, GetScheduledNotification, id)
var i ScheduledNotification
err := row.Scan(
&i.ID,
&i.Channel,
&i.Title,
&i.Message,
&i.Html,
&i.ScheduledAt,
&i.Status,
&i.TargetUserIds,
&i.TargetRole,
&i.TargetRaw,
&i.AttemptCount,
&i.LastError,
&i.ProcessingStartedAt,
&i.SentAt,
&i.CancelledAt,
&i.CreatedBy,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const ListScheduledNotifications = `-- name: ListScheduledNotifications :many
SELECT id, channel, title, message, html, scheduled_at, status, target_user_ids, target_role, target_raw, attempt_count, last_error, processing_started_at, sent_at, cancelled_at, created_by, created_at, updated_at
FROM scheduled_notifications
WHERE
($1::text IS NULL OR status = $1)
AND ($2::text IS NULL OR channel = $2)
AND ($3::timestamptz IS NULL OR scheduled_at >= $3)
AND ($4::timestamptz IS NULL OR scheduled_at <= $4)
ORDER BY scheduled_at DESC
LIMIT $6 OFFSET $5
`
type ListScheduledNotificationsParams struct {
FilterStatus pgtype.Text `json:"filter_status"`
FilterChannel pgtype.Text `json:"filter_channel"`
FilterAfter pgtype.Timestamptz `json:"filter_after"`
FilterBefore pgtype.Timestamptz `json:"filter_before"`
PageOffset int32 `json:"page_offset"`
PageLimit int32 `json:"page_limit"`
}
func (q *Queries) ListScheduledNotifications(ctx context.Context, arg ListScheduledNotificationsParams) ([]ScheduledNotification, error) {
rows, err := q.db.Query(ctx, ListScheduledNotifications,
arg.FilterStatus,
arg.FilterChannel,
arg.FilterAfter,
arg.FilterBefore,
arg.PageOffset,
arg.PageLimit,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ScheduledNotification
for rows.Next() {
var i ScheduledNotification
if err := rows.Scan(
&i.ID,
&i.Channel,
&i.Title,
&i.Message,
&i.Html,
&i.ScheduledAt,
&i.Status,
&i.TargetUserIds,
&i.TargetRole,
&i.TargetRaw,
&i.AttemptCount,
&i.LastError,
&i.ProcessingStartedAt,
&i.SentAt,
&i.CancelledAt,
&i.CreatedBy,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const MarkScheduledNotificationFailed = `-- name: MarkScheduledNotificationFailed :exec
UPDATE scheduled_notifications
SET
status = 'failed',
last_error = $2,
attempt_count = attempt_count + 1,
updated_at = CURRENT_TIMESTAMP
WHERE id = $1
`
type MarkScheduledNotificationFailedParams struct {
ID int64 `json:"id"`
LastError pgtype.Text `json:"last_error"`
}
func (q *Queries) MarkScheduledNotificationFailed(ctx context.Context, arg MarkScheduledNotificationFailedParams) error {
_, err := q.db.Exec(ctx, MarkScheduledNotificationFailed, arg.ID, arg.LastError)
return err
}
const MarkScheduledNotificationSent = `-- name: MarkScheduledNotificationSent :exec
UPDATE scheduled_notifications
SET
status = 'sent',
sent_at = CURRENT_TIMESTAMP,
updated_at = CURRENT_TIMESTAMP
WHERE id = $1
`
func (q *Queries) MarkScheduledNotificationSent(ctx context.Context, id int64) error {
_, err := q.db.Exec(ctx, MarkScheduledNotificationSent, id)
return err
}