files.sql 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 version DESC, created_at DESC
  10. LIMIT 1;
  11. -- name: ListFilesBySession :many
  12. SELECT *
  13. FROM files
  14. WHERE session_id = ?
  15. ORDER BY version ASC, created_at ASC;
  16. -- name: ListFilesByPath :many
  17. SELECT *
  18. FROM files
  19. WHERE path = ?
  20. ORDER BY version DESC, 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: DeleteFile :exec
  35. DELETE FROM files
  36. WHERE id = ?;
  37. -- name: DeleteSessionFiles :exec
  38. DELETE FROM files
  39. WHERE session_id = ?;
  40. -- name: ListLatestSessionFiles :many
  41. SELECT f.*
  42. FROM files f
  43. INNER JOIN (
  44. SELECT path, MAX(version) as max_version, MAX(created_at) as max_created_at
  45. FROM files
  46. GROUP BY path
  47. ) latest ON f.path = latest.path AND f.version = latest.max_version AND f.created_at = latest.max_created_at
  48. WHERE f.session_id = ?
  49. ORDER BY f.path;
  50. -- name: ListNewFiles :many
  51. SELECT *
  52. FROM files
  53. WHERE is_new = 1
  54. ORDER BY version DESC, created_at DESC;