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 = ? LIMIT 1;
  9. -- name: ListFilesBySession :many
  10. SELECT *
  11. FROM files
  12. WHERE session_id = ?
  13. ORDER BY created_at ASC;
  14. -- name: ListFilesByPath :many
  15. SELECT *
  16. FROM files
  17. WHERE path = ?
  18. ORDER BY created_at DESC;
  19. -- name: CreateFile :one
  20. INSERT INTO files (
  21. id,
  22. session_id,
  23. path,
  24. content,
  25. version,
  26. created_at,
  27. updated_at
  28. ) VALUES (
  29. ?, ?, ?, ?, ?, strftime('%s', 'now'), strftime('%s', 'now')
  30. )
  31. RETURNING *;
  32. -- name: UpdateFile :one
  33. UPDATE files
  34. SET
  35. content = ?,
  36. version = ?,
  37. updated_at = strftime('%s', '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;