diff --git a/src/database.rs b/src/database.rs index 3a073d9..7b44e34 100644 --- a/src/database.rs +++ b/src/database.rs @@ -124,14 +124,28 @@ pub fn delete_dataset(connection: &Connection, dataset: &str) -> sqlite::Result< let query = format!( r#"DELETE FROM datasets WHERE name = '{0}'; DROP TABLE "{0}_players"; - DROP TABLE "{0}_network"; - DROP VIEW "{0}_view";"#, + DROP TABLE "{0}_network";"#, dataset ); connection.execute(query) } +pub fn rename_dataset(connection: &Connection, old: &str, new: &str) -> sqlite::Result<()> { + let query = format!( + r#"UPDATE datasets SET name = '{1}' WHERE name = '{0}'; +ALTER TABLE "{0}_players" RENAME TO "{1}_players"; +ALTER TABLE "{0}_network" RENAME TO "{1}_network"; +DROP INDEX "{0}_network_A"; +CREATE INDEX "{1}_network_A" ON "{1}_network" (player_A); +DROP INDEX "{0}_network_B"; +CREATE INDEX "{1}_network_B" ON "{1}_network" (player_B);"#, + old, new + ); + + connection.execute(query) +} + pub fn new_dataset( connection: &Connection, dataset: &str, @@ -172,18 +186,8 @@ CREATE TABLE "{0}_network" ( FOREIGN KEY(player_B) REFERENCES "{0}_players" ON DELETE CASCADE ) STRICT; -CREATE INDEX "{0}_network_A" - ON "{0}_network" (player_A); -CREATE INDEX "{0}_network_B" - ON "{0}_network" (player_B); - -CREATE VIEW "{0}_view" - (player_A_id, player_B_id, player_A_name, player_B_name, advantage, - sets_A, sets_count_A, sets_B, sets_count_B, sets, sets_count) AS - SELECT players_A.id, players_B.id, players_A.name, players_B.name, advantage, - sets_A, sets_count_A, sets_B, sets_count_B, network.sets, network.sets_count FROM "{0}_network" network - INNER JOIN players players_A ON player_A = players_A.id - INNER JOIN players players_B ON player_B = players_B.id;"#, +CREATE INDEX "{0}_network_A" ON "{0}_network" (player_A); +CREATE INDEX "{0}_network_B" ON "{0}_network" (player_B);"#, dataset ); diff --git a/src/main.rs b/src/main.rs index c1e73b5..8174e1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -#![feature(binary_heap_as_slice)] #![feature(iterator_try_collect)] #![feature(extend_one)] @@ -93,6 +92,11 @@ enum DatasetSC { New { name: Option }, #[command(about = "Delete a dataset")] Delete { name: Option }, + #[command(about = "Rename a dataset")] + Rename { + old: Option, + new: Option, + }, } #[derive(Subcommand)] @@ -115,6 +119,9 @@ fn main() { Subcommands::Dataset { subcommand: DatasetSC::Delete { name }, } => dataset_delete(name), + Subcommands::Dataset { + subcommand: DatasetSC::Rename { old, new }, + } => dataset_rename(old, new), Subcommands::Player { subcommand: PlayerSC::Info { player }, @@ -451,6 +458,23 @@ fn dataset_delete(name: Option) { delete_dataset(&connection, &name).unwrap_or_else(|_| error("That dataset does not exist!", 1)); } +fn dataset_rename(old: Option, new: Option) { + let config_dir = dirs::config_dir().expect("Could not determine config directory"); + + let old = old.unwrap_or_else(|| { + print!("Dataset to rename: "); + read_string() + }); + let new = new.unwrap_or_else(|| { + print!("Rename to: "); + read_string() + }); + + let connection = + open_datasets(&config_dir).unwrap_or_else(|_| error("Could not open datasets file", 2)); + rename_dataset(&connection, &old, &new).unwrap(); +} + // Players fn player_info(dataset: Option, player: String) {