Ejemplo n.º 1
0
func init() {
	var err error
	conn, err = db.Open()
	if err != nil {
		log.Fatalf("Couldn't open DB connection: %s\n", err)
	}
}
Ejemplo n.º 2
0
func write(c <-chan titleIndex) {
	const stmt = `INSERT INTO titles (id, year, title, updated, rating, play_url, synopsis, box_art) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`
	const batchSize = 10

	defer func() {
		if err := recover(); err != nil {
			log.Printf("Caught panic: %s\n", err)
		}
	}()

	db, err := db.Open()
	if err != nil {
		log.Fatalf("Couldn't open DB connection: %s\n", err)
	}
	defer db.Close()

	st, err := db.Prepare(stmt)
	if err != nil {
		log.Fatalf("Couldn't prepare SQL statement: %s\n", err)
	}
	defer st.Close()

	for {
		t, ok := <-c
		if !ok {
			log.Println("Channel closed. Stopping goroutine.")
			return
		}

		_, err := st.Exec(t.id(), t.Year, t.Title.Regular, t.Updated, t.Rating, t.playURL(), t.synopsis(), t.boxArt())
		if err != nil {
			log.Fatalf("Exec failed: %s\n", err)
		}
	}
}