scan.go 851 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. conn := GetConnection()
  9. defer conn.Close(context.Background())
  10. for {
  11. select {
  12. case song, more := <- songs:
  13. if !more {
  14. return
  15. }
  16. var duration string = fmt.Sprintf("%d", song.Duration)
  17. if !song.DurationOk {
  18. duration = "NULL"
  19. }
  20. _, err := conn.Query(
  21. context.Background(),
  22. "insert into songs (title, artist, album, duration, base_path, relative_path) values ($1, $2, $3, $4, $5, $6)",
  23. song.Title,
  24. song.Artist,
  25. song.Album,
  26. duration,
  27. song.BasePath,
  28. song.RelativePath,
  29. )
  30. if err != nil {
  31. fmt.Printf("Error inserting record: %s\n", err)
  32. }
  33. }
  34. }
  35. }