Import colors to database

This commit is contained in:
The Magician 2024-04-23 21:45:55 +01:00
parent 4a6579a0a2
commit 424bca2d4d
4 changed files with 54 additions and 18 deletions

View File

@ -4,6 +4,7 @@ import (
"database/sql"
"log"
"sevenkeys/database"
"sevenkeys/database/imports"
"sevenkeys/scryfall"
)
@ -13,7 +14,7 @@ func importSets(db *sql.DB) {
log.Fatal(err)
}
err = database.InsertSets(db, setList)
err = imports.InsertSets(db, setList)
if err != nil {
log.Fatal(err)
}
@ -21,10 +22,8 @@ func importSets(db *sql.DB) {
func main() {
db := database.GetDatabaseFromConfig("config.json")
importColors(db)
importSets(db)
// Import keywords
// Import formats
//imports.InsertColors(db)
//importSets(db)
// Import artists
// Import gamepieces and printings
}

View File

@ -1,15 +1,45 @@
package database
package imports
/*
func populateColorTable(db *sql.DB) {
import (
"database/sql"
"log"
)
func checkColorExists(db *sql.DB, abbrev string) bool {
query := "SELECT Id FROM Color WHERE Abbreviation = ?;"
var colorId int
row := db.QueryRow(query, abbrev)
err := row.Scan(&colorId)
if err == sql.ErrNoRows {
return false
} else if err != nil {
log.Fatal(err)
}
return true
}
func insertColor(db *sql.DB, color string, abbrev string) error {
query := `INSERT INTO Color (Name, Abbreviation) VALUES (?, ?);`
insert, err := db.Prepare(query)
defer insert.Close()
if err != nil {
log.Fatal(err)
return err
}
result, err := insert.Exec(color, abbrev)
rowsAffected, err := result.RowsAffected()
if err != nil || rowsAffected != 1 {
return err
}
return nil
}
func InsertColors(db *sql.DB) error {
colors := map[string]string{
"White": "W",
"Blue": "U",
@ -17,13 +47,18 @@ func populateColorTable(db *sql.DB) {
"Red": "R",
"Green": "G",
}
for name, abbrev := range colors {
// TODO: Don't import a color if it's already in the database
result, err := insert.Exec(name, abbrev)
rowsAffected, err := result.RowsAffected()
if err != nil || rowsAffected != 1 {
log.Fatal(err)
if checkColorExists(db, abbrev) {
log.Println("[Color] Skipping " + name + ", already in database")
continue
}
err := insertColor(db, name, abbrev)
if err != nil {
return err
}
}
return nil
}
*/

View File

@ -1,4 +1,4 @@
package database
package imports
import (
"database/sql"
@ -36,8 +36,9 @@ func insertSet(db *sql.DB, set scryfall.Set) error {
return nil
}
_, err = insert.Exec(set.Code, set.Name, set.SetType, set.ReleasedAt, set.BlockCode, set.Block, set.ParentSetCode, set.CardCount, set.PrintedSize, set.Digital, set.FoilOnly, set.NonfoilOnly, set.IconSvgUri)
if err != nil {
result, err := insert.Exec(set.Code, set.Name, set.SetType, set.ReleasedAt, set.BlockCode, set.Block, set.ParentSetCode, set.CardCount, set.PrintedSize, set.Digital, set.FoilOnly, set.NonfoilOnly, set.IconSvgUri)
rowsAffected, err := result.RowsAffected()
if err != nil || rowsAffected != 1 {
return err
}

View File

@ -0,0 +1 @@
package imports