player.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package repository
  2. import "github.com/jmoiron/sqlx"
  3. func GetNextSongId(db *sqlx.DB, prevSongId int64) (nextSongId int64, err error) {
  4. err = db.QueryRowx(
  5. `
  6. select coalesce(id_next, 0) as id from (
  7. select
  8. prev.track_number, prev.title, prev.artist, prev.album
  9. ,coalesce(next_artist.id, prev.id_next) as id_next
  10. from (
  11. select
  12. prev.track_number, prev.title, prev.artist, prev.album
  13. ,coalesce(next_album.id, prev.id_next) as id_next
  14. from (
  15. select
  16. prev.track_number, prev.title, prev.artist, prev.album
  17. ,coalesce(next_title.id, prev.id_next) as id_next
  18. from (
  19. select
  20. prev.track_number, prev.title, prev.artist, prev.album
  21. ,next_track_number.id as id_next
  22. from (
  23. select track_number, title, artist, album from songs
  24. where id = $1
  25. ) prev
  26. left join songs next_track_number on 1=1
  27. and next_track_number.artist = prev.artist
  28. and next_track_number.album = prev.album
  29. and next_track_number.track_number > prev.track_number
  30. order by next_track_number.track_number
  31. limit 1
  32. ) prev
  33. left join songs next_title on 1=1
  34. and prev.id_next is null
  35. and next_title.artist = prev.artist
  36. and next_title.album = prev.album
  37. and next_title.track_number is null
  38. and next_title.title > prev.title
  39. order by next_title.title
  40. limit 1
  41. ) prev
  42. left join songs next_album on 1=1
  43. and prev.id_next is null
  44. and next_album.artist = prev.artist
  45. and next_album.album > prev.album
  46. order by next_album.album, next_album.track_number, next_album.title
  47. limit 1
  48. ) prev
  49. left join songs next_artist on 1=1
  50. and prev.id_next is null
  51. and next_artist.artist > prev.artist
  52. order by next_artist.artist, next_artist.album, next_artist.track_number, next_artist.title
  53. limit 1
  54. ) result
  55. `,
  56. prevSongId,
  57. ).Scan(&nextSongId)
  58. return
  59. }