mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
250 lines
5.5 KiB
Go
250 lines
5.5 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: schedule.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const createSchedule = `-- name: CreateSchedule :one
|
|
INSERT INTO schedule (name, description, pattern, max_calls, enabled, command, user_id)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
RETURNING id, name, description, pattern, max_calls, current_calls, created_at, updated_at, enabled, command, user_id
|
|
`
|
|
|
|
type CreateScheduleParams struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Pattern string `json:"pattern"`
|
|
MaxCalls pgtype.Int4 `json:"max_calls"`
|
|
Enabled bool `json:"enabled"`
|
|
Command string `json:"command"`
|
|
UserID pgtype.UUID `json:"user_id"`
|
|
}
|
|
|
|
func (q *Queries) CreateSchedule(ctx context.Context, arg CreateScheduleParams) (Schedule, error) {
|
|
row := q.db.QueryRow(ctx, createSchedule,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.Pattern,
|
|
arg.MaxCalls,
|
|
arg.Enabled,
|
|
arg.Command,
|
|
arg.UserID,
|
|
)
|
|
var i Schedule
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Pattern,
|
|
&i.MaxCalls,
|
|
&i.CurrentCalls,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Enabled,
|
|
&i.Command,
|
|
&i.UserID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deleteSchedule = `-- name: DeleteSchedule :exec
|
|
DELETE FROM schedule
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteSchedule(ctx context.Context, id pgtype.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteSchedule, id)
|
|
return err
|
|
}
|
|
|
|
const getScheduleByID = `-- name: GetScheduleByID :one
|
|
SELECT id, name, description, pattern, max_calls, current_calls, created_at, updated_at, enabled, command, user_id
|
|
FROM schedule
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) GetScheduleByID(ctx context.Context, id pgtype.UUID) (Schedule, error) {
|
|
row := q.db.QueryRow(ctx, getScheduleByID, id)
|
|
var i Schedule
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Pattern,
|
|
&i.MaxCalls,
|
|
&i.CurrentCalls,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Enabled,
|
|
&i.Command,
|
|
&i.UserID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const incrementScheduleCalls = `-- name: IncrementScheduleCalls :one
|
|
UPDATE schedule
|
|
SET current_calls = current_calls + 1,
|
|
enabled = CASE
|
|
WHEN max_calls IS NOT NULL AND current_calls + 1 >= max_calls THEN false
|
|
ELSE enabled
|
|
END,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
RETURNING id, name, description, pattern, max_calls, current_calls, created_at, updated_at, enabled, command, user_id
|
|
`
|
|
|
|
func (q *Queries) IncrementScheduleCalls(ctx context.Context, id pgtype.UUID) (Schedule, error) {
|
|
row := q.db.QueryRow(ctx, incrementScheduleCalls, id)
|
|
var i Schedule
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Pattern,
|
|
&i.MaxCalls,
|
|
&i.CurrentCalls,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Enabled,
|
|
&i.Command,
|
|
&i.UserID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const listEnabledSchedules = `-- name: ListEnabledSchedules :many
|
|
SELECT id, name, description, pattern, max_calls, current_calls, created_at, updated_at, enabled, command, user_id
|
|
FROM schedule
|
|
WHERE enabled = true
|
|
ORDER BY created_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListEnabledSchedules(ctx context.Context) ([]Schedule, error) {
|
|
rows, err := q.db.Query(ctx, listEnabledSchedules)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Schedule
|
|
for rows.Next() {
|
|
var i Schedule
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Pattern,
|
|
&i.MaxCalls,
|
|
&i.CurrentCalls,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Enabled,
|
|
&i.Command,
|
|
&i.UserID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listSchedulesByUser = `-- name: ListSchedulesByUser :many
|
|
SELECT id, name, description, pattern, max_calls, current_calls, created_at, updated_at, enabled, command, user_id
|
|
FROM schedule
|
|
WHERE user_id = $1
|
|
ORDER BY created_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListSchedulesByUser(ctx context.Context, userID pgtype.UUID) ([]Schedule, error) {
|
|
rows, err := q.db.Query(ctx, listSchedulesByUser, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Schedule
|
|
for rows.Next() {
|
|
var i Schedule
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Pattern,
|
|
&i.MaxCalls,
|
|
&i.CurrentCalls,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Enabled,
|
|
&i.Command,
|
|
&i.UserID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const updateSchedule = `-- name: UpdateSchedule :one
|
|
UPDATE schedule
|
|
SET name = $2,
|
|
description = $3,
|
|
pattern = $4,
|
|
max_calls = $5,
|
|
enabled = $6,
|
|
command = $7,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
RETURNING id, name, description, pattern, max_calls, current_calls, created_at, updated_at, enabled, command, user_id
|
|
`
|
|
|
|
type UpdateScheduleParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Pattern string `json:"pattern"`
|
|
MaxCalls pgtype.Int4 `json:"max_calls"`
|
|
Enabled bool `json:"enabled"`
|
|
Command string `json:"command"`
|
|
}
|
|
|
|
func (q *Queries) UpdateSchedule(ctx context.Context, arg UpdateScheduleParams) (Schedule, error) {
|
|
row := q.db.QueryRow(ctx, updateSchedule,
|
|
arg.ID,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.Pattern,
|
|
arg.MaxCalls,
|
|
arg.Enabled,
|
|
arg.Command,
|
|
)
|
|
var i Schedule
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.Pattern,
|
|
&i.MaxCalls,
|
|
&i.CurrentCalls,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Enabled,
|
|
&i.Command,
|
|
&i.UserID,
|
|
)
|
|
return i, err
|
|
}
|