Creating database seems alright

Unsure really whether the data is in there properly - but there
is data in there!
This commit is contained in:
2025-08-16 03:41:51 +01:00
parent da121940da
commit ff4c58113f
4 changed files with 44 additions and 6 deletions

View File

@@ -10,9 +10,9 @@ chrono = { version = "0.4.39", features = ["serde"] }
clap = { version = "4.5.42", features = ["derive"] } clap = { version = "4.5.42", features = ["derive"] }
closestmatch = "0.1.2" closestmatch = "0.1.2"
dir_spec = "0.2.0" dir_spec = "0.2.0"
rusqlite = "0.37.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.138" serde_json = "1.0.138"
sqlite = "0.37.0"
tempfile = "3.20.0" tempfile = "3.20.0"
ureq = { version = "3.0.12", features = ["json"] } ureq = { version = "3.0.12", features = ["json"] }
uuid = { version = "1.12.1", features = ["v4", "serde"] } uuid = { version = "1.12.1", features = ["v4", "serde"] }

View File

@@ -1,7 +1,8 @@
use sqlite; use rusqlite;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use super::deser::ScryfallCard;
use super::utils::get_local_cache_folder; use super::utils::get_local_cache_folder;
use super::utils::{create_cache_folder, get_local_data_folder, SQLITE_FILENAME}; use super::utils::{create_cache_folder, get_local_data_folder, SQLITE_FILENAME};
@@ -19,13 +20,44 @@ CREATE TABLE cards (
oracle_text TEXT, oracle_text TEXT,
power_toughness TEXT, power_toughness TEXT,
loyalty INTEGER, loyalty INTEGER,
legalities TEXT,
mana_cost TEXT, mana_cost TEXT,
scryfall_uri TEXT NOT NULL UNIQUE scryfall_uri TEXT NOT NULL UNIQUE
);" );"
.to_string() .to_string()
} }
pub fn update_db_with_file(file: PathBuf) -> bool {
let ac = fs::read_to_string(file).unwrap();
let ac: Vec<ScryfallCard> = serde_json::from_str(&ac).unwrap();
let sqlite_file = get_local_data_sqlite_file();
let mut conn = rusqlite::Connection::open(sqlite_file).unwrap();
let tx = conn.transaction().unwrap();
for card in ac {
let power_toughness = match card.power {
Some(p) => format!("{}/{}", p, card.toughness.unwrap()),
None => "".to_string(),
};
let oracle_text = match card.oracle_text {
Some(ot) => ot,
None => "".to_string(),
};
let loyalty = match card.loyalty {
Some(loy) => loy,
None => "".to_string(),
};
let mana_cost = match card.mana_cost {
Some(mc) => mc,
None => "".to_string(),
};
tx.execute(
"INSERT INTO cards (name, type_line, oracle_text, power_toughness, loyalty, mana_cost, scryfall_uri) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
[card.name, card.type_line, oracle_text, power_toughness, loyalty, mana_cost, card.scryfall_uri],
);
}
tx.commit();
true
}
// Will delete your current db // Will delete your current db
pub fn init_db() -> bool { pub fn init_db() -> bool {
create_cache_folder(); create_cache_folder();
@@ -35,8 +67,8 @@ pub fn init_db() -> bool {
println!("cache folder: {}", get_local_cache_folder().display()); println!("cache folder: {}", get_local_cache_folder().display());
let _res = fs::remove_file(&sqlite_file); let _res = fs::remove_file(&sqlite_file);
// TODO actually check result for whether it was a permissions thing or something // TODO actually check result for whether it was a permissions thing or something
let connection = sqlite::open(sqlite_file).unwrap(); let connection = rusqlite::Connection::open(sqlite_file).unwrap();
let init_query = create_db_sql(); let init_query = create_db_sql();
connection.execute(init_query).unwrap(); connection.execute(&init_query, ()).unwrap();
true true
} }

View File

@@ -5,6 +5,7 @@ mod deser;
pub use crate::deser::ScryfallCard; pub use crate::deser::ScryfallCard;
mod db; mod db;
pub use db::init_db; pub use db::{init_db, update_db_with_file};
mod utils; mod utils;
pub use utils::get_local_cache_folder;

View File

@@ -1,5 +1,7 @@
use clap::Parser; use clap::Parser;
use scryfall_deser::get_local_cache_folder;
use scryfall_deser::init_db; use scryfall_deser::init_db;
use scryfall_deser::update_db_with_file;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
@@ -22,4 +24,7 @@ fn main() {
dbg!(search_string); dbg!(search_string);
init_db(); init_db();
let mut path = get_local_cache_folder();
path.push("oracle-cards-20250814210711.json");
update_db_with_file(path);
} }