scan.go 942 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package db
  2. import (
  3. "fmt"
  4. "context"
  5. "github.com/felamaslen/go-music-player/pkg/read"
  6. )
  7. func InsertMusicIntoDatabase(songs chan *read.Song) {
  8. for {
  9. select {
  10. case song, more := <- songs:
  11. if !more {
  12. return
  13. }
  14. duration := "NULL"
  15. if song.DurationOk {
  16. duration = fmt.Sprintf("%d", song.Duration)
  17. }
  18. conn := GetConnection()
  19. _, err := conn.Query(
  20. context.Background(),
  21. "insert into songs (title, artist, album, duration, base_path, relative_path) values ($1, $2, $3, $4, $5, $6)",
  22. song.Title,
  23. song.Artist,
  24. song.Album,
  25. duration,
  26. song.BasePath,
  27. song.RelativePath,
  28. )
  29. if err == nil {
  30. fmt.Printf("Inserted record successfully: %s, %s, %s, %s\n", song.RelativePath, song.Artist, song.Album, song.Title)
  31. } else {
  32. fmt.Printf("Error inserting record: %s\n", err)
  33. }
  34. }
  35. }
  36. }