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

@@ -1,7 +1,8 @@
use sqlite;
use rusqlite;
use std::fs;
use std::path::PathBuf;
use super::deser::ScryfallCard;
use super::utils::get_local_cache_folder;
use super::utils::{create_cache_folder, get_local_data_folder, SQLITE_FILENAME};
@@ -19,13 +20,44 @@ CREATE TABLE cards (
oracle_text TEXT,
power_toughness TEXT,
loyalty INTEGER,
legalities TEXT,
mana_cost TEXT,
scryfall_uri TEXT NOT NULL UNIQUE
);"
.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
pub fn init_db() -> bool {
create_cache_folder();
@@ -35,8 +67,8 @@ pub fn init_db() -> bool {
println!("cache folder: {}", get_local_cache_folder().display());
let _res = fs::remove_file(&sqlite_file);
// 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();
connection.execute(init_query).unwrap();
connection.execute(&init_query, ()).unwrap();
true
}