1// Code generated by sqlc. DO NOT EDIT.
2// versions:
3// sqlc v1.30.0
4// source: sessions.sql
5
6package sqlc
7
8import (
9 "context"
10 "database/sql"
11 "time"
12)
13
14const DeleteExitedSessionsBefore = `-- name: DeleteExitedSessionsBefore :execrows
15DELETE FROM sessions
16WHERE type = 'browser'
17 AND ended_at IS NOT NULL
18 AND ended_at < ?
19`
20
21// Deletes exited browser sessions older than the given cutoff time.
22func (q *Queries) DeleteExitedSessionsBefore(ctx context.Context, endedAt sql.NullTime) (int64, error) {
23 result, err := q.db.ExecContext(ctx, DeleteExitedSessionsBefore, endedAt)
24 if err != nil {
25 return 0, err
26 }
27 return result.RowsAffected()
28}
29
30const DeleteOldestExitedSessions = `-- name: DeleteOldestExitedSessions :execrows
31DELETE FROM sessions
32WHERE id IN (
33 SELECT id FROM sessions
34 WHERE type = 'browser' AND ended_at IS NOT NULL
35 ORDER BY ended_at DESC
36 LIMIT -1 OFFSET ?
37)
38`
39
40// Deletes exited browser sessions beyond the keep limit, keeping the most recent ones.
41func (q *Queries) DeleteOldestExitedSessions(ctx context.Context, offset int64) (int64, error) {
42 result, err := q.db.ExecContext(ctx, DeleteOldestExitedSessions, offset)
43 if err != nil {
44 return 0, err
45 }
46 return result.RowsAffected()
47}
48
49const DeleteSession = `-- name: DeleteSession :exec
50DELETE FROM sessions WHERE id = ?
51`
52
53func (q *Queries) DeleteSession(ctx context.Context, id string) error {
54 _, err := q.db.ExecContext(ctx, DeleteSession, id)
55 return err
56}
57
58const GetActiveBrowserSession = `-- name: GetActiveBrowserSession :one
59SELECT id, type, started_at, ended_at FROM sessions
60WHERE type = 'browser' AND ended_at IS NULL
61ORDER BY started_at DESC
62LIMIT 1
63`
64
65func (q *Queries) GetActiveBrowserSession(ctx context.Context) (Session, error) {
66 row := q.db.QueryRowContext(ctx, GetActiveBrowserSession)
67 var i Session
68 err := row.Scan(
69 &i.ID,
70 &i.Type,
71 &i.StartedAt,
72 &i.EndedAt,
73 )
74 return i, err
75}
76
77const GetRecentSessions = `-- name: GetRecentSessions :many
78SELECT id, type, started_at, ended_at FROM sessions
79ORDER BY started_at DESC
80LIMIT ?
81`
82
83func (q *Queries) GetRecentSessions(ctx context.Context, limit int64) ([]Session, error) {
84 rows, err := q.db.QueryContext(ctx, GetRecentSessions, limit)
85 if err != nil {
86 return nil, err
87 }
88 defer rows.Close()
89 items := []Session{}
90 for rows.Next() {
91 var i Session
92 if err := rows.Scan(
93 &i.ID,
94 &i.Type,
95 &i.StartedAt,
96 &i.EndedAt,
97 ); err != nil {
98 return nil, err
99 }
100 items = append(items, i)
101 }
102 if err := rows.Close(); err != nil {
103 return nil, err
104 }
105 if err := rows.Err(); err != nil {
106 return nil, err
107 }
108 return items, nil
109}
110
111const GetSessionByID = `-- name: GetSessionByID :one
112SELECT id, type, started_at, ended_at FROM sessions WHERE id = ? LIMIT 1
113`
114
115func (q *Queries) GetSessionByID(ctx context.Context, id string) (Session, error) {
116 row := q.db.QueryRowContext(ctx, GetSessionByID, id)
117 var i Session
118 err := row.Scan(
119 &i.ID,
120 &i.Type,
121 &i.StartedAt,
122 &i.EndedAt,
123 )
124 return i, err
125}
126
127const InsertSession = `-- name: InsertSession :exec
128INSERT INTO sessions (id, type, started_at, ended_at)
129VALUES (?, ?, ?, ?)
130`
131
132type InsertSessionParams struct {
133 ID string `json:"id"`
134 Type string `json:"type"`
135 StartedAt time.Time `json:"started_at"`
136 EndedAt sql.NullTime `json:"ended_at"`
137}
138
139func (q *Queries) InsertSession(ctx context.Context, arg InsertSessionParams) error {
140 _, err := q.db.ExecContext(ctx, InsertSession,
141 arg.ID,
142 arg.Type,
143 arg.StartedAt,
144 arg.EndedAt,
145 )
146 return err
147}
148
149const MarkSessionEnded = `-- name: MarkSessionEnded :exec
150UPDATE sessions
151SET ended_at = ?
152WHERE id = ?
153`
154
155type MarkSessionEndedParams struct {
156 EndedAt sql.NullTime `json:"ended_at"`
157 ID string `json:"id"`
158}
159
160func (q *Queries) MarkSessionEnded(ctx context.Context, arg MarkSessionEndedParams) error {
161 _, err := q.db.ExecContext(ctx, MarkSessionEnded, arg.EndedAt, arg.ID)
162 return err
163}