files.sql 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. created_at,
  29. updated_at
  30. ) VALUES (
  31. ?, ?, ?, ?, ?, strftime('%s', 'now'), strftime('%s', 'now')
  32. )
  33. RETURNING *;
  34. -- name: UpdateFile :one
  35. UPDATE files
  36. SET
  37. content = ?,
  38. version = ?,
  39. updated_at = strftime('%s', 'now')
  40. WHERE id = ?
  41. RETURNING *;
  42. -- name: DeleteFile :exec
  43. DELETE FROM files
  44. WHERE id = ?;
  45. -- name: DeleteSessionFiles :exec
  46. DELETE FROM files
  47. WHERE session_id = ?;
  48. -- name: ListLatestSessionFiles :many
  49. SELECT f.*
  50. FROM files f
  51. INNER JOIN (
  52. SELECT path, MAX(created_at) as max_created_at
  53. FROM files
  54. GROUP BY path
  55. ) latest ON f.path = latest.path AND f.created_at = latest.max_created_at
  56. WHERE f.session_id = ?
  57. ORDER BY f.path;
  58. -- name: ListNewFiles :many
  59. SELECT *
  60. FROM files
  61. WHERE is_new = 1
  62. ORDER BY created_at DESC;