67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package operations
|
|
|
|
import (
|
|
"database/sql"
|
|
"sevenkeys/scryfall/types"
|
|
"time"
|
|
)
|
|
|
|
func InsertOrUpdateCacheTimestampByType(db *sql.DB, cacheType string, stamp time.Time) error {
|
|
query := `INSERT INTO CacheTimestamp (CacheType, Stamp)
|
|
VALUES (?, ?)
|
|
ON DUPLICATE KEY
|
|
UPDATE Stamp = ?;`
|
|
|
|
insertOrUpdate, err := db.Prepare(query)
|
|
defer insertOrUpdate.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = insertOrUpdate.Exec(cacheType, stamp, stamp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// TODO: There's really no need for this to be an "upsert", we can just ignore sets that we already have
|
|
func InsertOrUpdateSet(db *sql.DB, set types.Set) error {
|
|
query := `INSERT INTO ExpansionSet (SetCode, Name, CardCount, IconSvgUri)
|
|
VALUES (?, ?, ?, ?)
|
|
ON DUPLICATE KEY
|
|
Update Name = ?, CardCount = ?, IconSvgUri = ?;`
|
|
|
|
insertOrUpdate, err := db.Prepare(query)
|
|
defer insertOrUpdate.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = insertOrUpdate.Exec(set.Code, set.Name, set.CardCount, set.IconSvgUri, set.Name, set.CardCount, set.IconSvgUri)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func InsertCard(db *sql.DB, card types.Card) error {
|
|
query := `INSERT INTO CardPrinting
|
|
(Id, Name, SetCode, HasFoil, HasNonFoil, IsReserved, IsRacist, IsPromo, CollectorNumber, Language)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`
|
|
|
|
insert, err := db.Prepare(query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = insert.Exec(card.Id, card.Name, card.Set, card.Foil, card.NonFoil, card.Reserved, card.ContentWarning, card.Promo, card.CollectorNumber, card.Language)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|