func (album *APIAlbum) Save(db *sqlpg.DB) error { var queries = dna.StringArray{} awards, credits, discos, releases, songs, mainAlbum := album.Convert() queries.Push(getInsertStmt(mainAlbum, dna.Sprintf("WHERE NOT EXISTS (SELECT 1 FROM %v WHERE id=%v)", getTableName(mainAlbum), album.Id))) for _, award := range awards { // dna.Log(award) queries.Push(getInsertStmt(&award, dna.Sprintf("WHERE NOT EXISTS (SELECT 1 FROM %v WHERE id=%v AND albumid=%v AND chart=$binhdna$%v$binhdna$ AND prize=$binhdna$%v$binhdna$)", getTableName(&award), award.Id, award.Albumid, award.Chart, award.Prize))) } for _, credit := range credits { queries.Push(getInsertStmt(&credit, dna.Sprintf("WHERE NOT EXISTS (SELECT 1 FROM %v WHERE id=%v and albumid=%v)", getTableName(&credit), credit.Id, credit.Albumid))) } for _, disco := range discos { queries.Push(getInsertStmt(&disco, dna.Sprintf("WHERE NOT EXISTS (SELECT 1 FROM %v WHERE id=%v and artistid=%v)", getTableName(&disco), disco.Id, disco.Artistid)).Replace("amdiscographys", "amdiscographies")) } for _, release := range releases { queries.Push(getInsertStmt(&release, dna.Sprintf("WHERE NOT EXISTS (SELECT 1 FROM %v WHERE id=%v and albumid=%v)", getTableName(&release), release.Id, release.Albumid))) } for _, song := range songs { queries.Push(getInsertStmt(&song, dna.Sprintf("WHERE NOT EXISTS (SELECT 1 FROM %v WHERE id=%v and albumid=%v)", getTableName(&song), song.Id, song.Albumid))) } // dna.Log(queries.Join("\n")) // return db.Update(mainAlbum, "id", "coverart") return sqlpg.ExecQueriesInTransaction(db, &queries) // return db.InsertIgnore(album) }
func (sf *APISongFreaksTrack) Save(db *sqlpg.DB) error { var queries = dna.StringArray{} album, artist, song, videos := sf.Convert() if artist != nil { queries.Push(getInsertStmt(artist, dna.Sprintf("WHERE NOT EXISTS (SELECT 1 FROM %v WHERE id=%v)", getTableName(artist), artist.Id))) } if album != nil { queries.Push(getInsertStmt(album, dna.Sprintf("WHERE NOT EXISTS (SELECT 1 FROM %v WHERE id=%v)", getTableName(album), album.Id))) } if song != nil { queries.Push(getInsertStmt(song, dna.Sprintf("WHERE NOT EXISTS (SELECT 1 FROM %v WHERE id=%v)", getTableName(song), song.Id))) } for _, video := range videos { queries.Push(getInsertStmt(video, dna.Sprintf("WHERE NOT EXISTS (SELECT 1 FROM %v WHERE songid=%v AND youtube_id='%v')", getTableName(video), video.Songid, video.YoutubeId))) } // dna.Log(queries.Join("\n")) // dna.Log(queries.Join("\n")) // return nil return sqlpg.ExecQueriesInTransaction(db, &queries) }