Explorar o código

chore: moved queries to constants file

Fela Maslen %!s(int64=5) %!d(string=hai) anos
pai
achega
784263542b

+ 2 - 88
gmus-backend/pkg/repository/player.go

@@ -10,50 +10,7 @@ import (
 func GetNextSong(db *sqlx.DB, prevSongId int) (nextSong *read.Song, err error) {
 func GetNextSong(db *sqlx.DB, prevSongId int) (nextSong *read.Song, err error) {
 	nextSong = &read.Song{}
 	nextSong = &read.Song{}
 	err = db.QueryRowx(
 	err = db.QueryRowx(
-		`
-select
-  s1.id
-  ,s1.track_number
-  ,s1.title
-  ,s1.artist
-  ,s1.album
-  ,s1.duration
-
-from (
-  select * from songs where id = $1
-) s0
-
-left join songs s1 on (
-  s1.artist > s0.artist
-  or (s1.artist = s0.artist
-    and s1.album > s0.album
-  )
-  or (s1.artist = s0.artist
-    and s1.album = s0.album
-    and s1.track_number > s0.track_number
-  )
-  or (s1.artist = s0.artist
-    and s1.album = s0.album
-    and s1.track_number = s0.track_number
-    and s1.title > s0.title
-  )
-  or (s1.artist = s0.artist
-    and s1.album = s0.album
-    and s1.track_number = s0.track_number
-    and s1.title = s0.title
-    and s1.id > s0.id
-  )
-)
-
-order by
-  s1.artist
-  ,s1.album
-  ,s1.track_number
-  ,s1.title
-  ,s1.id
-
-limit 1
-    `,
+		querySelectNextSong,
 		prevSongId,
 		prevSongId,
 	).StructScan(nextSong)
 	).StructScan(nextSong)
 	if err != nil && err == sql.ErrNoRows {
 	if err != nil && err == sql.ErrNoRows {
@@ -66,50 +23,7 @@ limit 1
 func GetPrevSong(db *sqlx.DB, nextSongId int) (prevSong *read.Song, err error) {
 func GetPrevSong(db *sqlx.DB, nextSongId int) (prevSong *read.Song, err error) {
 	prevSong = &read.Song{}
 	prevSong = &read.Song{}
 	err = db.QueryRowx(
 	err = db.QueryRowx(
-		`
-select
-  s1.id
-  ,s1.track_number
-  ,s1.title
-  ,s1.artist
-  ,s1.album
-  ,s1.duration
-
-from (
-  select * from songs where id = $1
-) s0
-
-left join songs s1 on (
-  s1.artist < s0.artist
-  or (s1.artist = s0.artist
-    and s1.album < s0.album
-  )
-  or (s1.artist = s0.artist
-    and s1.album = s0.album
-    and s1.track_number < s0.track_number
-  )
-  or (s1.artist = s0.artist
-    and s1.album = s0.album
-    and s1.track_number = s0.track_number
-    and s1.title < s0.title
-  )
-  or (s1.artist = s0.artist
-    and s1.album = s0.album
-    and s1.track_number = s0.track_number
-    and s1.title = s0.title
-    and s1.id < s0.id
-  )
-)
-
-order by
-  s1.artist desc
-  ,s1.album desc
-  ,s1.track_number desc
-  ,s1.title desc
-  ,s1.id desc
-
-limit 1
-    `,
+		querySelectPrevSong,
 		nextSongId,
 		nextSongId,
 	).StructScan(prevSong)
 	).StructScan(prevSong)
 	if err != nil && err == sql.ErrNoRows {
 	if err != nil && err == sql.ErrNoRows {

+ 171 - 0
gmus-backend/pkg/repository/queries.go

@@ -0,0 +1,171 @@
+package repository
+
+const querySelectSongById = `
+select
+  id
+  ,track_number
+  ,title
+  ,artist
+  ,album
+  ,duration
+  ,modified_date
+  ,base_path
+  ,relative_path
+from songs
+where id = ANY($1)
+`
+
+const querySelectArtistsOrdered = `
+select distinct artist
+from songs
+order by artist
+limit $1
+offset $2
+`
+
+const queryCountArtists = `
+select count(*) as count from (
+  select distinct artist from songs
+) distinct_artists
+`
+
+const querySelectAlbumsByArtist = `
+select distinct album
+from songs
+where artist = $1
+order by album
+`
+
+const querySelectSongsByArtist = `
+select
+  id
+  ,track_number
+  ,title
+  ,artist
+  ,album
+  ,duration
+from songs
+where artist = $1
+order by album, track_number, title, id
+`
+
+const queryInsertSongs = `
+insert into songs (
+  track_number
+  ,title
+  ,artist
+  ,album
+  ,duration
+  ,modified_date
+  ,base_path
+  ,relative_path
+)
+select * from unnest(
+  $1::integer[]
+  ,$2::varchar[]
+  ,$3::varchar[]
+  ,$4::varchar[]
+  ,$5::integer[]
+  ,$6::bigint[]
+  ,$7::varchar[]
+  ,$8::varchar[]
+)
+on conflict (base_path, relative_path) do update
+set
+  track_number = excluded.track_number
+  ,title = excluded.title
+  ,artist = excluded.artist
+  ,album = excluded.album
+  ,duration = excluded.duration
+  ,modified_date = excluded.modified_date
+`
+
+const querySelectNextSong = `
+select
+  s1.id
+  ,s1.track_number
+  ,s1.title
+  ,s1.artist
+  ,s1.album
+  ,s1.duration
+
+from (
+  select * from songs where id = $1
+) s0
+
+left join songs s1 on (
+  s1.artist > s0.artist
+  or (s1.artist = s0.artist
+    and s1.album > s0.album
+  )
+  or (s1.artist = s0.artist
+    and s1.album = s0.album
+    and s1.track_number > s0.track_number
+  )
+  or (s1.artist = s0.artist
+    and s1.album = s0.album
+    and s1.track_number = s0.track_number
+    and s1.title > s0.title
+  )
+  or (s1.artist = s0.artist
+    and s1.album = s0.album
+    and s1.track_number = s0.track_number
+    and s1.title = s0.title
+    and s1.id > s0.id
+  )
+)
+
+order by
+  s1.artist
+  ,s1.album
+  ,s1.track_number
+  ,s1.title
+  ,s1.id
+
+limit 1
+`
+
+const querySelectPrevSong = `
+select
+  s1.id
+  ,s1.track_number
+  ,s1.title
+  ,s1.artist
+  ,s1.album
+  ,s1.duration
+
+from (
+  select * from songs where id = $1
+) s0
+
+left join songs s1 on (
+  s1.artist < s0.artist
+  or (s1.artist = s0.artist
+    and s1.album < s0.album
+  )
+  or (s1.artist = s0.artist
+    and s1.album = s0.album
+    and s1.track_number < s0.track_number
+  )
+  or (s1.artist = s0.artist
+    and s1.album = s0.album
+    and s1.track_number = s0.track_number
+    and s1.title < s0.title
+  )
+  or (s1.artist = s0.artist
+    and s1.album = s0.album
+    and s1.track_number = s0.track_number
+    and s1.title = s0.title
+    and s1.id < s0.id
+  )
+)
+
+order by
+  s1.artist desc
+  ,s1.album desc
+  ,s1.track_number desc
+  ,s1.title desc
+  ,s1.id desc
+
+limit 1
+`

+ 24 - 0
gmus-backend/pkg/repository/queries_test.go

@@ -0,0 +1,24 @@
+package repository_test
+
+const testQueryInsertHeyJude = `
+insert into songs (track_number, title, artist, album, duration, modified_date, base_path, relative_path)
+values ($1, $2, $3, $4, $5, $6, $7, $8)
+returning id
+`
+
+const testQueryInsertTrack1 = `
+insert into songs (track_number, title, artist, album, duration, modified_date, base_path, relative_path)
+values ($1, $2, $3, $4, $5, $6, $7, $8)
+returning id
+`
+
+const testQuerySelectAllSongs = `
+select track_number, title, artist, album, duration, base_path, relative_path, modified_date
+from songs
+`
+
+const testQuerySelectSong12 = `
+select track_number, title, artist, album, duration, base_path, relative_path, modified_date
+from songs
+where relative_path in ('song1.ogg', 'song2.ogg')
+`

+ 6 - 74
gmus-backend/pkg/repository/songs.go

@@ -15,33 +15,14 @@ func SelectSong(db *sqlx.DB, ids []int) (songs *[]*read.Song, err error) {
 		idsArray = append(idsArray, int64(id))
 		idsArray = append(idsArray, int64(id))
 	}
 	}
 
 
-	err = db.Select(songs, `
-  select
-    id
-    ,track_number
-    ,title
-    ,artist
-    ,album
-    ,duration
-    ,modified_date
-    ,base_path
-    ,relative_path
-  from songs
-  where id = ANY($1)
-  `, idsArray)
+	err = db.Select(songs, querySelectSongById, idsArray)
 
 
 	return
 	return
 }
 }
 
 
 func SelectPagedArtists(db *sqlx.DB, limit int, offset int) (artists *[]string, err error) {
 func SelectPagedArtists(db *sqlx.DB, limit int, offset int) (artists *[]string, err error) {
 	artists = &[]string{}
 	artists = &[]string{}
-	err = db.Select(artists, `
-  select distinct artist
-  from songs
-  order by artist
-  limit $1
-  offset $2
-  `, limit, offset)
+	err = db.Select(artists, querySelectArtistsOrdered, limit, offset)
 	return
 	return
 }
 }
 
 
@@ -52,11 +33,7 @@ type CountRow struct {
 func SelectArtistCount(db *sqlx.DB) (count int, err error) {
 func SelectArtistCount(db *sqlx.DB) (count int, err error) {
 	var countRow CountRow
 	var countRow CountRow
 
 
-	err = db.QueryRowx(`
-  select count(*) as count from (
-    select distinct artist from songs
-  ) distinct_artists
-  `).StructScan(&countRow)
+	err = db.QueryRowx(queryCountArtists).StructScan(&countRow)
 
 
 	count = countRow.Count
 	count = countRow.Count
 
 
@@ -71,30 +48,14 @@ func SelectAllArtists(db *sqlx.DB) (artists *[]string, err error) {
 
 
 func SelectAlbumsByArtist(db *sqlx.DB, artist string) (albums *[]string, err error) {
 func SelectAlbumsByArtist(db *sqlx.DB, artist string) (albums *[]string, err error) {
 	albums = &[]string{}
 	albums = &[]string{}
-	err = db.Select(albums, `
-  select distinct album
-  from songs
-  where artist = $1
-  order by album
-  `, artist)
+	err = db.Select(albums, querySelectAlbumsByArtist, artist)
 
 
 	return
 	return
 }
 }
 
 
 func SelectSongsByArtist(db *sqlx.DB, artist string) (songs *[]*read.SongExternal, err error) {
 func SelectSongsByArtist(db *sqlx.DB, artist string) (songs *[]*read.SongExternal, err error) {
 	songs = &[]*read.SongExternal{}
 	songs = &[]*read.SongExternal{}
-	err = db.Select(songs, `
-  select
-    id
-    ,track_number
-    ,title
-    ,artist
-    ,album
-    ,duration
-  from songs
-  where artist = $1
-  order by album, track_number, title, id
-  `, artist)
+	err = db.Select(songs, querySelectSongsByArtist, artist)
 
 
 	return
 	return
 }
 }
@@ -125,36 +86,7 @@ func BatchUpsertSongs(db *sqlx.DB, batch *[BATCH_SIZE]*read.Song, batchSize int)
 	}
 	}
 
 
 	_, err := db.Exec(
 	_, err := db.Exec(
-		`
-    insert into songs (
-      track_number
-      ,title
-      ,artist
-      ,album
-      ,duration
-      ,modified_date
-      ,base_path
-      ,relative_path
-    )
-    select * from unnest(
-      $1::integer[]
-      ,$2::varchar[]
-      ,$3::varchar[]
-      ,$4::varchar[]
-      ,$5::integer[]
-      ,$6::bigint[]
-      ,$7::varchar[]
-      ,$8::varchar[]
-    )
-    on conflict (base_path, relative_path) do update
-    set
-      track_number = excluded.track_number
-      ,title = excluded.title
-      ,artist = excluded.artist
-      ,album = excluded.album
-      ,duration = excluded.duration
-      ,modified_date = excluded.modified_date
-    `,
+		queryInsertSongs,
 		trackNumbers,
 		trackNumbers,
 		titles,
 		titles,
 		artists,
 		artists,

+ 4 - 19
gmus-backend/pkg/repository/songs_test.go

@@ -23,11 +23,7 @@ var _ = Describe("songs repository", func() {
 
 
 		BeforeEach(func() {
 		BeforeEach(func() {
 			db.QueryRowx(
 			db.QueryRowx(
-				`
-	insert into songs (track_number, title, artist, album, duration, modified_date, base_path, relative_path)
-	values ($1, $2, $3, $4, $5, $6, $7, $8)
-	returning id
-	`,
+				testQueryInsertHeyJude,
 				7,
 				7,
 				"Hey Jude",
 				"Hey Jude",
 				"The Beatles",
 				"The Beatles",
@@ -39,11 +35,7 @@ var _ = Describe("songs repository", func() {
 			).Scan(&id)
 			).Scan(&id)
 
 
 			db.QueryRowx(
 			db.QueryRowx(
-				`
-	insert into songs (track_number, title, artist, album, duration, modified_date, base_path, relative_path)
-	values ($1, $2, $3, $4, $5, $6, $7, $8)
-	returning id
-	`,
+				testQueryInsertTrack1,
 				13,
 				13,
 				"Track 1",
 				"Track 1",
 				"Untitled Artist",
 				"Untitled Artist",
@@ -151,10 +143,7 @@ var _ = Describe("songs repository", func() {
 
 
 			It("should insert the batch into the database", func() {
 			It("should insert the batch into the database", func() {
 				var result []*read.Song
 				var result []*read.Song
-				db.Select(&result, `
-	select track_number, title, artist, album, duration, base_path, relative_path, modified_date
-	from songs
-	`)
+				db.Select(&result, testQuerySelectAllSongs)
 
 
 				Expect(result).To(HaveLen(2))
 				Expect(result).To(HaveLen(2))
 				Expect(songs[0]).To(BeElementOf(result))
 				Expect(songs[0]).To(BeElementOf(result))
@@ -176,11 +165,7 @@ var _ = Describe("songs repository", func() {
 				repository.BatchUpsertSongs(db, &modifiedBatch, 2)
 				repository.BatchUpsertSongs(db, &modifiedBatch, 2)
 
 
 				result = []*read.Song{}
 				result = []*read.Song{}
-				db.Select(&result, `
-  select track_number, title, artist, album, duration, base_path, relative_path, modified_date
-  from songs
-  where relative_path in ('song1.ogg', 'song2.ogg')
-	`)
+				db.Select(&result, testQuerySelectSong12)
 			})
 			})
 
 
 			It("should not create any additional rows", func() {
 			It("should not create any additional rows", func() {