From c3a35c5ef6fbccf6da855931f8d70b8899db93a7 Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Sat, 30 Sep 2023 00:22:48 -0400 Subject: [PATCH] Implement dataset deletion --- src/datasets.rs | 34 +++++++++++++++++++--------------- src/main.rs | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/datasets.rs b/src/datasets.rs index e08deb0..4f80e1e 100644 --- a/src/datasets.rs +++ b/src/datasets.rs @@ -20,17 +20,13 @@ fn default_datasets_path(config_dir: &Path) -> io::Result { Ok(path) } -pub fn open_datasets(config_dir: &Path, path: Option<&Path>) -> sqlite::Result { - let path = path.map_or_else( - || default_datasets_path(config_dir).unwrap(), - |p| p.to_owned(), - ); +pub fn open_datasets(config_dir: &Path) -> sqlite::Result { + let path = default_datasets_path(config_dir).unwrap(); let query = " CREATE TABLE IF NOT EXISTS datasets ( name TEXT UNIQUE NOT NULL - ) STRICT; - "; + ) STRICT;"; let connection = sqlite::open(path)?; connection.execute(query)?; @@ -49,18 +45,26 @@ pub fn list_datasets(connection: &Connection) -> sqlite::Result> { .try_collect() } +pub fn delete_dataset(connection: &Connection, dataset: &str) -> sqlite::Result<()> { + let query = format!( + r#"DELETE FROM datasets WHERE name = '{0}'; + DROP TABLE "dataset_{0}";"#, + dataset + ); + + connection.execute(query) +} + pub fn new_dataset(connection: &Connection, dataset: &str) -> sqlite::Result<()> { let query = format!( - " - INSERT INTO datasets VALUES ('{0}'); + r#"INSERT INTO datasets VALUES ('{0}'); - CREATE TABLE IF NOT EXISTS \"dataset_{0}\" ( + CREATE TABLE IF NOT EXISTS "dataset_{0}" ( id INTEGER PRIMARY KEY, name TEXT, prefix TEXT, elo REAL NOT NULL - ) STRICT; - ", + ) STRICT;"#, dataset ); @@ -103,7 +107,7 @@ pub fn add_players( teams: &Teams, ) -> sqlite::Result<()> { let query = format!( - "INSERT OR IGNORE INTO \"dataset_{}\" VALUES (?, ?, ?, 1500)", + r#"INSERT OR IGNORE INTO "dataset_{}" VALUES (?, ?, ?, 1500)"#, dataset ); @@ -123,7 +127,7 @@ pub fn get_ratings( dataset: &str, teams: &Teams, ) -> sqlite::Result> { - let query = format!("SELECT id, elo FROM \"dataset_{}\" WHERE id = ?", dataset); + let query = format!(r#"SELECT id, elo FROM "dataset_{}" WHERE id = ?"#, dataset); teams .iter() @@ -146,7 +150,7 @@ pub fn update_ratings( elos: Teams<(PlayerId, f64)>, ) -> sqlite::Result<()> { let query = format!( - "UPDATE \"dataset_{}\" SET elo = :elo WHERE id = :id", + r#"UPDATE "dataset_{}" SET elo = :elo WHERE id = :id"#, dataset ); elos.into_iter().try_for_each(|team| { diff --git a/src/main.rs b/src/main.rs index 1982f16..15be420 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,10 +17,16 @@ use datasets::*; #[command(name = "StartGGElo")] #[command(author = "Kiana Sheibani ")] #[command(version = "0.1.0")] -#[command(about = "Elo rating calculator for start.gg tournaments", long_about = None)] +#[command(about = "StartGGElo - Elo rating calculator for start.gg tournaments", long_about = None)] struct Cli { #[command(subcommand)] subcommand: Subcommands, + + #[arg(short = 'A', long = "auth", value_name = "TOKEN", global = true)] + auth_token: Option, + + #[arg(short, long = "config", value_name = "DIR", global = true)] + config_dir: Option, } #[derive(Subcommand)] @@ -35,6 +41,7 @@ enum Subcommands { enum DatasetSC { List, New { name: Option }, + Delete { name: Option }, } fn main() { @@ -47,13 +54,16 @@ fn main() { Subcommands::Dataset { subcommand: DatasetSC::New { name }, } => dataset_new(name), + Subcommands::Dataset { + subcommand: DatasetSC::Delete { name }, + } => dataset_delete(name), } } fn dataset_list() { let config_dir = dirs::config_dir().unwrap(); - let connection = open_datasets(&config_dir, None).unwrap(); + let connection = open_datasets(&config_dir).unwrap(); let datasets = list_datasets(&connection).unwrap(); println!("{:?}", datasets); @@ -72,6 +82,23 @@ fn dataset_new(name: Option) { line.trim().to_owned() }); - let connection = open_datasets(&config_dir, None).unwrap(); + let connection = open_datasets(&config_dir).unwrap(); new_dataset(&connection, &name).unwrap(); } + +fn dataset_delete(name: Option) { + let config_dir = dirs::config_dir().unwrap(); + + let name = name.unwrap_or_else(|| { + let mut line = String::new(); + print!("Dataset to delete: "); + io::stdout().flush().expect("Could not access stdout"); + io::stdin() + .read_line(&mut line) + .expect("Could not read from stdin"); + line.trim().to_owned() + }); + + let connection = open_datasets(&config_dir).unwrap(); + delete_dataset(&connection, &name).unwrap(); +}