Files
OwnCord/Server/db/dbgen/sessions.sql.go
T
Claude e46bd0e015 refactor(server/db): delegate users, sessions, profile to dbgen (D2)
Convert the auth_queries.go user + session reads/writes and
profile_queries.go to the sqlc-generated layer, adding shared
userFromGen/sessionFromGen mappers (db/mappers.go) for the
int64/*string -> int/bool/string domain-model narrowing.

Delegated: GetUserByID, GetUserByUsername, UpdateUserStatus,
UpdateUserTOTPSecret, ResetAllUserStatuses, BanUser, UnbanUser,
ListMembers, CreateSession (EvictOldestSessions + InsertSession),
GetSessionByTokenHash, GetSessionWithBanStatus, DeleteSession,
DeleteOtherSessions, DeleteExpiredSessions, TouchSession,
UpdateUserProfile, UpdateUserPassword, ListUserSessions,
DeleteSessionByID (query changed to :execresult so the RowsAffected
ErrNotFound check is preserved). Behavior and public signatures unchanged.

Verified: go build ./...; go test ./db ./service ./auth; sqlc-verify.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UA17KPvqGBX3XbXYnMf1rA
2026-07-19 15:34:27 +00:00

208 lines
5.2 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: sessions.sql
package dbgen
import (
"context"
"database/sql"
)
const deleteExpiredSessions = `-- name: DeleteExpiredSessions :exec
DELETE FROM sessions WHERE strftime('%s', expires_at) < strftime('%s', 'now')
`
func (q *Queries) DeleteExpiredSessions(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, deleteExpiredSessions)
return err
}
const deleteOtherSessions = `-- name: DeleteOtherSessions :execresult
DELETE FROM sessions WHERE user_id = ? AND id != ?
`
type DeleteOtherSessionsParams struct {
UserID int64 `json:"userId"`
ID int64 `json:"id"`
}
func (q *Queries) DeleteOtherSessions(ctx context.Context, arg DeleteOtherSessionsParams) (sql.Result, error) {
return q.db.ExecContext(ctx, deleteOtherSessions, arg.UserID, arg.ID)
}
const deleteSessionByID = `-- name: DeleteSessionByID :execresult
DELETE FROM sessions WHERE id = ? AND user_id = ?
`
type DeleteSessionByIDParams struct {
ID int64 `json:"id"`
UserID int64 `json:"userId"`
}
func (q *Queries) DeleteSessionByID(ctx context.Context, arg DeleteSessionByIDParams) (sql.Result, error) {
return q.db.ExecContext(ctx, deleteSessionByID, arg.ID, arg.UserID)
}
const deleteSessionByToken = `-- name: DeleteSessionByToken :exec
DELETE FROM sessions WHERE token = ?
`
func (q *Queries) DeleteSessionByToken(ctx context.Context, token string) error {
_, err := q.db.ExecContext(ctx, deleteSessionByToken, token)
return err
}
const evictOldestSessions = `-- name: EvictOldestSessions :exec
DELETE FROM sessions WHERE id IN (
SELECT s2.id FROM sessions AS s2 WHERE s2.user_id = ?
ORDER BY s2.created_at DESC
LIMIT -1 OFFSET ?
)
`
type EvictOldestSessionsParams struct {
UserID int64 `json:"userId"`
Offset int64 `json:"offset"`
}
func (q *Queries) EvictOldestSessions(ctx context.Context, arg EvictOldestSessionsParams) error {
_, err := q.db.ExecContext(ctx, evictOldestSessions, arg.UserID, arg.Offset)
return err
}
const getSessionByTokenHash = `-- name: GetSessionByTokenHash :one
SELECT id, user_id, token, device, ip_address, created_at, last_used, expires_at
FROM sessions WHERE token = ?
`
func (q *Queries) GetSessionByTokenHash(ctx context.Context, token string) (Session, error) {
row := q.db.QueryRowContext(ctx, getSessionByTokenHash, token)
var i Session
err := row.Scan(
&i.ID,
&i.UserID,
&i.Token,
&i.Device,
&i.IpAddress,
&i.CreatedAt,
&i.LastUsed,
&i.ExpiresAt,
)
return i, err
}
const getSessionWithBanStatus = `-- name: GetSessionWithBanStatus :one
SELECT s.id, s.user_id, s.token, s.device, s.ip_address,
s.created_at, s.last_used, s.expires_at,
u.banned, u.ban_reason, u.ban_expires
FROM sessions s
JOIN users u ON s.user_id = u.id
WHERE s.token = ?
`
type GetSessionWithBanStatusRow struct {
ID int64 `json:"id"`
UserID int64 `json:"userId"`
Token string `json:"token"`
Device *string `json:"device"`
IpAddress *string `json:"ipAddress"`
CreatedAt string `json:"createdAt"`
LastUsed string `json:"lastUsed"`
ExpiresAt string `json:"expiresAt"`
Banned int64 `json:"banned"`
BanReason *string `json:"banReason"`
BanExpires *string `json:"banExpires"`
}
func (q *Queries) GetSessionWithBanStatus(ctx context.Context, token string) (GetSessionWithBanStatusRow, error) {
row := q.db.QueryRowContext(ctx, getSessionWithBanStatus, token)
var i GetSessionWithBanStatusRow
err := row.Scan(
&i.ID,
&i.UserID,
&i.Token,
&i.Device,
&i.IpAddress,
&i.CreatedAt,
&i.LastUsed,
&i.ExpiresAt,
&i.Banned,
&i.BanReason,
&i.BanExpires,
)
return i, err
}
const insertSession = `-- name: InsertSession :execresult
INSERT INTO sessions (user_id, token, device, ip_address, expires_at)
VALUES (?, ?, ?, ?, ?)
`
type InsertSessionParams struct {
UserID int64 `json:"userId"`
Token string `json:"token"`
Device *string `json:"device"`
IpAddress *string `json:"ipAddress"`
ExpiresAt string `json:"expiresAt"`
}
func (q *Queries) InsertSession(ctx context.Context, arg InsertSessionParams) (sql.Result, error) {
return q.db.ExecContext(ctx, insertSession,
arg.UserID,
arg.Token,
arg.Device,
arg.IpAddress,
arg.ExpiresAt,
)
}
const listUserSessions = `-- name: ListUserSessions :many
SELECT id, user_id, token, device, ip_address, created_at, last_used, expires_at
FROM sessions
WHERE user_id = ?
ORDER BY created_at DESC
`
func (q *Queries) ListUserSessions(ctx context.Context, userID int64) ([]Session, error) {
rows, err := q.db.QueryContext(ctx, listUserSessions, userID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []Session{}
for rows.Next() {
var i Session
if err := rows.Scan(
&i.ID,
&i.UserID,
&i.Token,
&i.Device,
&i.IpAddress,
&i.CreatedAt,
&i.LastUsed,
&i.ExpiresAt,
); 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 touchSession = `-- name: TouchSession :exec
UPDATE sessions SET last_used = datetime('now') WHERE token = ?
`
func (q *Queries) TouchSession(ctx context.Context, token string) error {
_, err := q.db.ExecContext(ctx, touchSession, token)
return err
}