queries.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package repository
  2. const querySelectSongById = `
  3. select
  4. id
  5. ,track_number
  6. ,title
  7. ,artist
  8. ,album
  9. ,duration
  10. ,modified_date
  11. ,base_path
  12. ,relative_path
  13. from songs
  14. where id = ANY($1)
  15. `
  16. const querySelectArtistsOrdered = `
  17. select distinct artist
  18. from songs
  19. order by artist
  20. limit $1
  21. offset $2
  22. `
  23. const queryCountArtists = `
  24. select count(*) as count from (
  25. select distinct artist from songs
  26. ) distinct_artists
  27. `
  28. const querySelectAlbumsByArtist = `
  29. select distinct album
  30. from songs
  31. where artist = $1
  32. order by album
  33. `
  34. const querySelectSongsByArtist = `
  35. select
  36. id
  37. ,track_number
  38. ,title
  39. ,artist
  40. ,album
  41. ,duration
  42. from songs
  43. where artist = $1
  44. order by album, track_number, title, id
  45. `
  46. const queryInsertSongs = `
  47. insert into songs (
  48. track_number
  49. ,title
  50. ,artist
  51. ,album
  52. ,duration
  53. ,modified_date
  54. ,base_path
  55. ,relative_path
  56. )
  57. select * from unnest(
  58. $1::integer[]
  59. ,$2::varchar[]
  60. ,$3::varchar[]
  61. ,$4::varchar[]
  62. ,$5::integer[]
  63. ,$6::bigint[]
  64. ,$7::varchar[]
  65. ,$8::varchar[]
  66. )
  67. on conflict (base_path, relative_path) do update
  68. set
  69. track_number = excluded.track_number
  70. ,title = excluded.title
  71. ,artist = excluded.artist
  72. ,album = excluded.album
  73. ,duration = excluded.duration
  74. ,modified_date = excluded.modified_date
  75. `
  76. const queryDeleteSongByPath = `
  77. delete from songs
  78. where base_path = $1 and relative_path = $2
  79. `
  80. const querySelectNextSong = `
  81. select
  82. s1.id
  83. ,s1.track_number
  84. ,s1.title
  85. ,s1.artist
  86. ,s1.album
  87. ,s1.duration
  88. from (
  89. select * from songs where id = $1
  90. ) s0
  91. left join songs s1 on (
  92. s1.artist > s0.artist
  93. or (s1.artist = s0.artist
  94. and s1.album > s0.album
  95. )
  96. or (s1.artist = s0.artist
  97. and s1.album = s0.album
  98. and s1.track_number > s0.track_number
  99. )
  100. or (s1.artist = s0.artist
  101. and s1.album = s0.album
  102. and s1.track_number = s0.track_number
  103. and s1.title > s0.title
  104. )
  105. or (s1.artist = s0.artist
  106. and s1.album = s0.album
  107. and s1.track_number = s0.track_number
  108. and s1.title = s0.title
  109. and s1.id > s0.id
  110. )
  111. )
  112. order by
  113. s1.artist
  114. ,s1.album
  115. ,s1.track_number
  116. ,s1.title
  117. ,s1.id
  118. limit 1
  119. `
  120. const querySelectPrevSong = `
  121. select
  122. s1.id
  123. ,s1.track_number
  124. ,s1.title
  125. ,s1.artist
  126. ,s1.album
  127. ,s1.duration
  128. from (
  129. select * from songs where id = $1
  130. ) s0
  131. left join songs s1 on (
  132. s1.artist < s0.artist
  133. or (s1.artist = s0.artist
  134. and s1.album < s0.album
  135. )
  136. or (s1.artist = s0.artist
  137. and s1.album = s0.album
  138. and s1.track_number < s0.track_number
  139. )
  140. or (s1.artist = s0.artist
  141. and s1.album = s0.album
  142. and s1.track_number = s0.track_number
  143. and s1.title < s0.title
  144. )
  145. or (s1.artist = s0.artist
  146. and s1.album = s0.album
  147. and s1.track_number = s0.track_number
  148. and s1.title = s0.title
  149. and s1.id < s0.id
  150. )
  151. )
  152. order by
  153. s1.artist desc
  154. ,s1.album desc
  155. ,s1.track_number desc
  156. ,s1.title desc
  157. ,s1.id desc
  158. limit 1
  159. `
  160. const queryInsertScanError = `
  161. insert into scan_errors (created_at, base_path, relative_path, error)
  162. values ($1, $2, $3, $4)
  163. `
  164. const querySelectNewOrUpdatedFiles = `
  165. with all_files as (
  166. select * from unnest($1::varchar[], $2::bigint[])
  167. as t(relative_path, modified_date)
  168. )
  169. select r.relative_path, r.modified_date
  170. from all_files r
  171. left join songs on
  172. songs.base_path = $3
  173. and songs.relative_path = r.relative_path
  174. and songs.modified_date = r.modified_date
  175. left join scan_errors e on
  176. e.base_path = $3
  177. and e.relative_path = r.relative_path
  178. where songs.id is null
  179. and e.id is null
  180. `