player.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. }
  60. func GetPrevSongId(db *sqlx.DB, nextSongId int64) (prevSongId int64, err error) {
  61. err = db.QueryRowx(
  62. `
  63. select coalesce(id_prev, 0) as id from (
  64. select
  65. next.track_number, next.title, next.artist, next.album
  66. ,coalesce(prev_artist.id, next.id_prev) as id_prev
  67. from (
  68. select
  69. next.track_number, next.title, next.artist, next.album
  70. ,coalesce(prev_album.id, next.id_prev) as id_prev
  71. from (
  72. select
  73. next.track_number, next.title, next.artist, next.album
  74. ,coalesce(prev_title.id, next.id_prev) as id_prev
  75. from (
  76. select
  77. next.track_number, next.title, next.artist, next.album
  78. ,prev_track_number.id as id_prev
  79. from (
  80. select track_number, title, artist, album from songs
  81. where id = $1
  82. ) next
  83. left join songs prev_track_number on 1=1
  84. and prev_track_number.artist = next.artist
  85. and prev_track_number.album = next.album
  86. and prev_track_number.track_number < next.track_number
  87. order by prev_track_number.track_number desc
  88. limit 1
  89. ) next
  90. left join songs prev_title on 1=1
  91. and next.id_prev is null
  92. and prev_title.artist = next.artist
  93. and prev_title.album = next.album
  94. and prev_title.track_number is null
  95. and prev_title.title < next.title
  96. order by prev_title.title desc
  97. limit 1
  98. ) next
  99. left join songs prev_album on 1=1
  100. and next.id_prev is null
  101. and prev_album.artist = next.artist
  102. and prev_album.album < next.album
  103. order by prev_album.album desc, prev_album.track_number desc, prev_album.title desc
  104. limit 1
  105. ) next
  106. left join songs prev_artist on 1=1
  107. and next.id_prev is null
  108. and prev_artist.artist < next.artist
  109. order by prev_artist.artist desc, prev_artist.album desc, prev_artist.track_number desc, prev_artist.title desc
  110. limit 1
  111. ) result
  112. `,
  113. nextSongId,
  114. ).Scan(&prevSongId)
  115. return
  116. }