TheMathemagicians/sevenkeys/database/operations/inserts.go

96 lines
2.4 KiB
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-21 14:17:25 +00:00
// TODO: There's really no need for this to be an "upsert", we can just ignore sets that we already have
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
}
2024-05-21 14:27:05 +00:00
func InsertCard(db *sql.DB, card types.Card) error {
query := `INSERT IGNORE INTO CardPrinting
2024-05-21 14:27:05 +00:00
(Id, Name, SetCode, HasFoil, HasNonFoil, IsReserved, IsRacist, IsPromo, CollectorNumber, Language)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`
insert, err := db.Prepare(query)
defer insert.Close()
2024-05-21 14:27:05 +00:00
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
}
2024-05-27 11:05:52 +00:00
func InsertCardStorageLocation(db *sql.DB, cardPrintingId string, storageBox string, source string) error {
var lastPosition int
getLastPositionQuery := `SELECT Position FROM CardStorageLocation WHERE StorageBox = ? ORDER BY Position DESC LIMIT 1;`
2024-05-27 12:29:00 +00:00
err := db.QueryRow(getLastPositionQuery, storageBox).Scan(&lastPosition)
2024-05-27 11:05:52 +00:00
var nextPosition int
if err == sql.ErrNoRows {
nextPosition = 1
} else {
nextPosition = lastPosition + 1
}
insertQuery := `INSERT INTO CardStorageLocation
(CardPrintingId, StorageBox, Source, Position)
VALUES (?, ?, ?, ?);`
insert, err := db.Prepare(insertQuery)
if err != nil {
return err
}
_, err = insert.Exec(cardPrintingId, storageBox, source, nextPosition)
if err != nil {
return err
}
return nil
}