TheMathemagicians/sevenkeys/database/operations/inserts.go

48 lines
1005 B
Go
Raw Normal View History

2024-04-28 14:13:47 +00:00
package operations
import (
"database/sql"
"sevenkeys/scryfall/types"
2024-05-19 14:01:18 +00:00
"time"
2024-04-28 14:13:47 +00:00
)
2024-05-20 16:17:45 +00:00
func InsertOrUpdateCacheTimestampByType(db *sql.DB, cacheType string, stamp time.Time) error {
query := `INSERT INTO CacheTimestamp (CacheType, Stamp)
2024-05-20 16:17:45 +00:00
VALUES (?, ?)
ON DUPLICATE KEY
UPDATE Stamp = ?;`
2024-04-28 14:13:47 +00:00
2024-05-20 16:17:45 +00:00
insertOrUpdate, err := db.Prepare(query)
defer insertOrUpdate.Close()
2024-04-28 14:13:47 +00:00
if err != nil {
2024-05-20 16:17:45 +00:00
return err
2024-04-28 14:13:47 +00:00
}
2024-05-20 16:17:45 +00:00
_, err = insertOrUpdate.Exec(cacheType, stamp, stamp)
2024-04-28 14:13:47 +00:00
if err != nil {
2024-05-20 16:17:45 +00:00
return err
2024-04-28 14:13:47 +00:00
}
2024-05-20 16:17:45 +00:00
return nil
2024-04-28 14:13:47 +00:00
}
2024-05-19 14:01:18 +00:00
2024-05-20 16:33:40 +00:00
func InsertOrUpdateSet(db *sql.DB, set types.Set) error {
2024-05-20 16:17:45 +00:00
query := `INSERT INTO ExpansionSet (SetCode, Name, CardCount, IconSvgUri)
VALUES (?, ?, ?, ?)
ON DUPLICATE KEY
Update Name = ?, CardCount = ?, IconSvgUri = ?;`
2024-05-19 14:01:18 +00:00
insertOrUpdate, err := db.Prepare(query)
defer insertOrUpdate.Close()
if err != nil {
return err
}
2024-05-20 16:33:40 +00:00
_, err = insertOrUpdate.Exec(set.Code, set.Name, set.CardCount, set.IconSvgUri, set.Name, set.CardCount, set.IconSvgUri)
if err != nil {
return err
2024-05-19 14:01:18 +00:00
}
return nil
}