files.sql 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. -- name: GetFile :one
  2. SELECT *
  3. FROM files
  4. WHERE id = ? LIMIT 1;
  5. -- name: GetFileByPathAndSession :one
  6. SELECT *
  7. FROM files
  8. WHERE path = ? AND session_id = ?
  9. ORDER BY created_at DESC
  10. LIMIT 1;
  11. -- name: ListFilesBySession :many
  12. SELECT *
  13. FROM files
  14. WHERE session_id = ?
  15. ORDER BY created_at ASC;
  16. -- name: ListFilesByPath :many
  17. SELECT *
  18. FROM files
  19. WHERE path = ?
  20. ORDER BY created_at DESC;
  21. -- name: CreateFile :one
  22. INSERT INTO files (
  23. id,
  24. session_id,
  25. path,
  26. content,
  27. version
  28. ) VALUES (
  29. ?, ?, ?, ?, ?
  30. )
  31. RETURNING *;
  32. -- name: UpdateFile :one
  33. UPDATE files
  34. SET
  35. content = ?,
  36. version = ?,
  37. updated_at = strftime('%Y-%m-%dT%H:%M:%f000Z', 'now')
  38. WHERE id = ?
  39. RETURNING *;
  40. -- name: DeleteFile :exec
  41. DELETE FROM files
  42. WHERE id = ?;
  43. -- name: DeleteSessionFiles :exec
  44. DELETE FROM files
  45. WHERE session_id = ?;
  46. -- name: ListLatestSessionFiles :many
  47. SELECT f.*
  48. FROM files f
  49. INNER JOIN (
  50. SELECT path, MAX(created_at) as max_created_at
  51. FROM files
  52. GROUP BY path
  53. ) latest ON f.path = latest.path AND f.created_at = latest.max_created_at
  54. WHERE f.session_id = ?
  55. ORDER BY f.path;
  56. -- name: ListNewFiles :many
  57. SELECT *
  58. FROM files
  59. WHERE is_new = 1
  60. ORDER BY created_at DESC;