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

View File

@ -1,15 +1,45 @@
package database package imports
/* import (
func populateColorTable(db *sql.DB) { "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 (?, ?);` query := `INSERT INTO Color (Name, Abbreviation) VALUES (?, ?);`
insert, err := db.Prepare(query) insert, err := db.Prepare(query)
defer insert.Close() defer insert.Close()
if err != nil { 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{ colors := map[string]string{
"White": "W", "White": "W",
"Blue": "U", "Blue": "U",
@ -17,13 +47,18 @@ func populateColorTable(db *sql.DB) {
"Red": "R", "Red": "R",
"Green": "G", "Green": "G",
} }
for name, abbrev := range colors { for name, abbrev := range colors {
// TODO: Don't import a color if it's already in the database if checkColorExists(db, abbrev) {
result, err := insert.Exec(name, abbrev) log.Println("[Color] Skipping " + name + ", already in database")
rowsAffected, err := result.RowsAffected() continue
if err != nil || rowsAffected != 1 { }
log.Fatal(err)
err := insertColor(db, name, abbrev)
if err != nil {
return err
} }
} }
return nil
} }
*/

View File

@ -1,4 +1,4 @@
package database package imports
import ( import (
"database/sql" "database/sql"
@ -36,8 +36,9 @@ func insertSet(db *sql.DB, set scryfall.Set) error {
return nil 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) 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)
if err != nil { rowsAffected, err := result.RowsAffected()
if err != nil || rowsAffected != 1 {
return err return err
} }

View File

@ -0,0 +1 @@
package imports