Implement dataset deletion

This commit is contained in:
Kiana Sheibani 2023-09-30 00:22:48 -04:00
parent 0e9fddec9e
commit c3a35c5ef6
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
2 changed files with 49 additions and 18 deletions

View file

@ -20,17 +20,13 @@ fn default_datasets_path(config_dir: &Path) -> io::Result<PathBuf> {
Ok(path) Ok(path)
} }
pub fn open_datasets(config_dir: &Path, path: Option<&Path>) -> sqlite::Result<Connection> { pub fn open_datasets(config_dir: &Path) -> sqlite::Result<Connection> {
let path = path.map_or_else( let path = default_datasets_path(config_dir).unwrap();
|| default_datasets_path(config_dir).unwrap(),
|p| p.to_owned(),
);
let query = " let query = "
CREATE TABLE IF NOT EXISTS datasets ( CREATE TABLE IF NOT EXISTS datasets (
name TEXT UNIQUE NOT NULL name TEXT UNIQUE NOT NULL
) STRICT; ) STRICT;";
";
let connection = sqlite::open(path)?; let connection = sqlite::open(path)?;
connection.execute(query)?; connection.execute(query)?;
@ -49,18 +45,26 @@ pub fn list_datasets(connection: &Connection) -> sqlite::Result<Vec<String>> {
.try_collect() .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<()> { pub fn new_dataset(connection: &Connection, dataset: &str) -> sqlite::Result<()> {
let query = format!( let query = format!(
" r#"INSERT INTO datasets VALUES ('{0}');
INSERT INTO datasets VALUES ('{0}');
CREATE TABLE IF NOT EXISTS \"dataset_{0}\" ( CREATE TABLE IF NOT EXISTS "dataset_{0}" (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name TEXT, name TEXT,
prefix TEXT, prefix TEXT,
elo REAL NOT NULL elo REAL NOT NULL
) STRICT; ) STRICT;"#,
",
dataset dataset
); );
@ -103,7 +107,7 @@ pub fn add_players(
teams: &Teams<PlayerData>, teams: &Teams<PlayerData>,
) -> sqlite::Result<()> { ) -> sqlite::Result<()> {
let query = format!( let query = format!(
"INSERT OR IGNORE INTO \"dataset_{}\" VALUES (?, ?, ?, 1500)", r#"INSERT OR IGNORE INTO "dataset_{}" VALUES (?, ?, ?, 1500)"#,
dataset dataset
); );
@ -123,7 +127,7 @@ pub fn get_ratings(
dataset: &str, dataset: &str,
teams: &Teams<PlayerData>, teams: &Teams<PlayerData>,
) -> sqlite::Result<Teams<(PlayerId, f64)>> { ) -> sqlite::Result<Teams<(PlayerId, f64)>> {
let query = format!("SELECT id, elo FROM \"dataset_{}\" WHERE id = ?", dataset); let query = format!(r#"SELECT id, elo FROM "dataset_{}" WHERE id = ?"#, dataset);
teams teams
.iter() .iter()
@ -146,7 +150,7 @@ pub fn update_ratings(
elos: Teams<(PlayerId, f64)>, elos: Teams<(PlayerId, f64)>,
) -> sqlite::Result<()> { ) -> sqlite::Result<()> {
let query = format!( let query = format!(
"UPDATE \"dataset_{}\" SET elo = :elo WHERE id = :id", r#"UPDATE "dataset_{}" SET elo = :elo WHERE id = :id"#,
dataset dataset
); );
elos.into_iter().try_for_each(|team| { elos.into_iter().try_for_each(|team| {

View file

@ -17,10 +17,16 @@ use datasets::*;
#[command(name = "StartGGElo")] #[command(name = "StartGGElo")]
#[command(author = "Kiana Sheibani <kiana.a.sheibani@gmail.com>")] #[command(author = "Kiana Sheibani <kiana.a.sheibani@gmail.com>")]
#[command(version = "0.1.0")] #[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 { struct Cli {
#[command(subcommand)] #[command(subcommand)]
subcommand: Subcommands, subcommand: Subcommands,
#[arg(short = 'A', long = "auth", value_name = "TOKEN", global = true)]
auth_token: Option<String>,
#[arg(short, long = "config", value_name = "DIR", global = true)]
config_dir: Option<PathBuf>,
} }
#[derive(Subcommand)] #[derive(Subcommand)]
@ -35,6 +41,7 @@ enum Subcommands {
enum DatasetSC { enum DatasetSC {
List, List,
New { name: Option<String> }, New { name: Option<String> },
Delete { name: Option<String> },
} }
fn main() { fn main() {
@ -47,13 +54,16 @@ fn main() {
Subcommands::Dataset { Subcommands::Dataset {
subcommand: DatasetSC::New { name }, subcommand: DatasetSC::New { name },
} => dataset_new(name), } => dataset_new(name),
Subcommands::Dataset {
subcommand: DatasetSC::Delete { name },
} => dataset_delete(name),
} }
} }
fn dataset_list() { fn dataset_list() {
let config_dir = dirs::config_dir().unwrap(); 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(); let datasets = list_datasets(&connection).unwrap();
println!("{:?}", datasets); println!("{:?}", datasets);
@ -72,6 +82,23 @@ fn dataset_new(name: Option<String>) {
line.trim().to_owned() line.trim().to_owned()
}); });
let connection = open_datasets(&config_dir, None).unwrap(); let connection = open_datasets(&config_dir).unwrap();
new_dataset(&connection, &name).unwrap(); new_dataset(&connection, &name).unwrap();
} }
fn dataset_delete(name: Option<String>) {
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();
}