opencode_offline/internal/db/sql/files.sql

70 lines
1.2 KiB
MySQL
Raw Normal View History

2025-04-13 17:29:20 +08:00
-- name: GetFile :one
SELECT *
FROM files
WHERE id = ? LIMIT 1;
-- name: GetFileByPathAndSession :one
SELECT *
FROM files
WHERE path = ? AND session_id = ?
ORDER BY created_at DESC
LIMIT 1;
2025-04-13 17:29:20 +08:00
-- name: ListFilesBySession :many
SELECT *
FROM files
WHERE session_id = ?
ORDER BY created_at ASC;
-- name: ListFilesByPath :many
SELECT *
FROM files
WHERE path = ?
ORDER BY created_at DESC;
-- name: CreateFile :one
INSERT INTO files (
id,
session_id,
path,
content,
2025-05-14 02:08:43 +08:00
version
2025-04-13 17:29:20 +08:00
) VALUES (
2025-05-14 02:08:43 +08:00
?, ?, ?, ?, ?
2025-04-13 17:29:20 +08:00
)
RETURNING *;
-- name: UpdateFile :one
UPDATE files
SET
content = ?,
version = ?,
2025-05-14 02:08:43 +08:00
updated_at = strftime('%Y-%m-%dT%H:%M:%f000Z', 'now')
2025-04-13 17:29:20 +08:00
WHERE id = ?
RETURNING *;
-- name: DeleteFile :exec
DELETE FROM files
WHERE id = ?;
-- name: DeleteSessionFiles :exec
DELETE FROM files
WHERE session_id = ?;
-- name: ListLatestSessionFiles :many
SELECT f.*
FROM files f
INNER JOIN (
SELECT path, MAX(created_at) as max_created_at
FROM files
GROUP BY path
) latest ON f.path = latest.path AND f.created_at = latest.max_created_at
WHERE f.session_id = ?
ORDER BY f.path;
-- name: ListNewFiles :many
SELECT *
FROM files
WHERE is_new = 1
ORDER BY created_at DESC;