func (apiSong *APIFullSong) Save(db *sqlpg.DB) error { var queries = dna.StringArray{} var err error // Getting artist queries artists := apiSong.ToArtists() for _, artist := range artists { queries.Push(sqlpg.GetInsertIgnoreStatement(sqlpg.GetTableName(artist), artist, "id", artist.Id, false)) } // Getting album query album := apiSong.ToAlbum() queries.Push(sqlpg.GetInsertIgnoreStatement(sqlpg.GetTableName(album), album, "id", album.Id, false)) // Getting song query song := apiSong.ToSong() queries.Push(sqlpg.GetInsertStatement(sqlpg.GetTableName(song), song, false)) for _, query := range queries { _, err = db.Exec(query.String()) } if err != nil { errQueries := dna.StringArray(queries.Map(func(val dna.String, idx dna.Int) dna.String { return "$$$error$$$" + val + "$$$error$$$" }).([]dna.String)) return errors.New(err.Error() + errQueries.Join("\n").String()) } else { return nil } }
// NewStateHandlerWithRange returns new StateHandler with 2nd pattern. func NewStateHandlerWithRange(itm item.Item, r *Range, config *SiteConfig, db *sqlpg.DB) *StateHandler { tableName := sqlpg.GetTableName(itm) return &StateHandler{ Cid: r.First - 1, SiteConfig: config, Range: r, ExtSlice: nil, Db: db, Pattern: 2, IsOver: false, Item: itm, TableName: tableName, NcCount: 0, } }
// NewStateHandlerWithExtSlice returns new StateHandler with 3rd pattern. func NewStateHandlerWithExtSlice(itm item.Item, extSlice *dna.IntArray, config *SiteConfig, db *sqlpg.DB) *StateHandler { tableName := sqlpg.GetTableName(itm) return &StateHandler{ Cid: -1, // In this case, Cid means index of current element in external slice SiteConfig: config, Range: nil, ExtSlice: extSlice, Db: db, Pattern: 3, IsOver: false, Item: itm, TableName: tableName, NcCount: 0, } }