queries.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 querySelectNextSong = `
  77. select
  78. s1.id
  79. ,s1.track_number
  80. ,s1.title
  81. ,s1.artist
  82. ,s1.album
  83. ,s1.duration
  84. from (
  85. select * from songs where id = $1
  86. ) s0
  87. left join songs s1 on (
  88. s1.artist > s0.artist
  89. or (s1.artist = s0.artist
  90. and s1.album > s0.album
  91. )
  92. or (s1.artist = s0.artist
  93. and s1.album = s0.album
  94. and s1.track_number > s0.track_number
  95. )
  96. or (s1.artist = s0.artist
  97. and s1.album = s0.album
  98. and s1.track_number = s0.track_number
  99. and s1.title > s0.title
  100. )
  101. or (s1.artist = s0.artist
  102. and s1.album = s0.album
  103. and s1.track_number = s0.track_number
  104. and s1.title = s0.title
  105. and s1.id > s0.id
  106. )
  107. )
  108. order by
  109. s1.artist
  110. ,s1.album
  111. ,s1.track_number
  112. ,s1.title
  113. ,s1.id
  114. limit 1
  115. `
  116. const querySelectPrevSong = `
  117. select
  118. s1.id
  119. ,s1.track_number
  120. ,s1.title
  121. ,s1.artist
  122. ,s1.album
  123. ,s1.duration
  124. from (
  125. select * from songs where id = $1
  126. ) s0
  127. left join songs s1 on (
  128. s1.artist < s0.artist
  129. or (s1.artist = s0.artist
  130. and s1.album < s0.album
  131. )
  132. or (s1.artist = s0.artist
  133. and s1.album = s0.album
  134. and s1.track_number < s0.track_number
  135. )
  136. or (s1.artist = s0.artist
  137. and s1.album = s0.album
  138. and s1.track_number = s0.track_number
  139. and s1.title < s0.title
  140. )
  141. or (s1.artist = s0.artist
  142. and s1.album = s0.album
  143. and s1.track_number = s0.track_number
  144. and s1.title = s0.title
  145. and s1.id < s0.id
  146. )
  147. )
  148. order by
  149. s1.artist desc
  150. ,s1.album desc
  151. ,s1.track_number desc
  152. ,s1.title desc
  153. ,s1.id desc
  154. limit 1
  155. `
  156. const queryInsertScanError = `
  157. insert into scan_errors (created_at, base_path, relative_path, error)
  158. values ($1, $2, $3, $4)
  159. `
  160. const querySelectNewOrUpdatedFiles = `
  161. with all_files as (
  162. select * from unnest($1::varchar[], $2::bigint[])
  163. as t(relative_path, modified_date)
  164. )
  165. select r.relative_path, r.modified_date
  166. from all_files r
  167. left join songs on
  168. songs.base_path = $3
  169. and songs.relative_path = r.relative_path
  170. and songs.modified_date = r.modified_date
  171. left join scan_errors e on
  172. e.base_path = $3
  173. and e.relative_path = r.relative_path
  174. where songs.id is null
  175. and e.id is null
  176. `