Added the start of the db create
Need to do insert (obv), recall, search by name (get the whole name column), and then display nicely. Also renamed / used more correctly the "data" folder. I should use cache for the downloaded .json file from scryfall though
This commit is contained in:
@@ -1,34 +1,75 @@
|
||||
use dir_spec::Dir;
|
||||
use sqlite;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
const CACHE_FOLDER: &str = "scryfall_cache";
|
||||
const DATA_FOLDER: &str = "scryfall";
|
||||
const SQLITE_FILENAME: &str = "scryfall_db.sqlite3";
|
||||
|
||||
// NOTE: this should be idempotent
|
||||
pub fn create_cache_folder() {
|
||||
let cache_folder = Dir::cache_home();
|
||||
fn get_local_data_folder() -> PathBuf {
|
||||
let cache_folder = Dir::data_home();
|
||||
match cache_folder {
|
||||
None => {
|
||||
panic!("Can't find a cache folder - really don't know what the problem is sorry");
|
||||
}
|
||||
Some(mut f) => {
|
||||
f.push(CACHE_FOLDER);
|
||||
let ret = fs::create_dir(&f);
|
||||
match ret {
|
||||
Ok(_) => (),
|
||||
Err(_e) => {
|
||||
let err_string = format!(
|
||||
"Couldn't create folder within your cache folder: {}",
|
||||
f.display()
|
||||
);
|
||||
panic!("{}", err_string);
|
||||
}
|
||||
}
|
||||
f.push(DATA_FOLDER);
|
||||
f
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_local_data_sqlite_file() -> PathBuf {
|
||||
let mut folder = get_local_data_folder();
|
||||
folder.push(SQLITE_FILENAME);
|
||||
folder
|
||||
}
|
||||
|
||||
// NOTE: this should be idempotent - creating a dir always is... right?
|
||||
pub fn create_cache_folder() {
|
||||
let f = get_local_data_folder();
|
||||
let ret = fs::create_dir(&f);
|
||||
match ret {
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
if e.raw_os_error().unwrap() == 17 {
|
||||
// This is folder already exists - which is fine for us
|
||||
// TODO probably should use e.kind() for better readability
|
||||
return;
|
||||
}
|
||||
panic!(
|
||||
"Couldn't create folder within your cache folder: {}. Error is {}",
|
||||
f.display(),
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn create_db_sql() -> String {
|
||||
"
|
||||
CREATE TABLE cards (
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
type_line TEXT,
|
||||
oracle_text TEXT,
|
||||
power_toughness TEXT,
|
||||
loyalty INTEGER,
|
||||
legalities TEXT,
|
||||
mana_cost TEXT,
|
||||
scryfall_uri TEXT NOT NULL UNIQUE
|
||||
);"
|
||||
.to_string()
|
||||
}
|
||||
|
||||
// Will delete your current db
|
||||
pub fn init_db() -> bool {
|
||||
create_cache_folder();
|
||||
let sqlite_file = get_local_data_sqlite_file();
|
||||
println!("sqlite file location: {}", sqlite_file.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 init_query = create_db_sql();
|
||||
connection.execute(init_query).unwrap();
|
||||
true
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use clap::Parser;
|
||||
use scryfall_deser::init_db;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
@@ -19,4 +20,6 @@ fn main() {
|
||||
}
|
||||
let search_string = card_name.join(" ");
|
||||
dbg!(search_string);
|
||||
|
||||
init_db();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user