Add command to rename dataset

This commit is contained in:
Kiana Sheibani 2023-12-01 19:02:15 -05:00
parent b3ff055fd3
commit 4f240b318d
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
2 changed files with 43 additions and 15 deletions

View file

@ -124,14 +124,28 @@ pub fn delete_dataset(connection: &Connection, dataset: &str) -> sqlite::Result<
let query = format!( let query = format!(
r#"DELETE FROM datasets WHERE name = '{0}'; r#"DELETE FROM datasets WHERE name = '{0}';
DROP TABLE "{0}_players"; DROP TABLE "{0}_players";
DROP TABLE "{0}_network"; DROP TABLE "{0}_network";"#,
DROP VIEW "{0}_view";"#,
dataset dataset
); );
connection.execute(query) 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( pub fn new_dataset(
connection: &Connection, connection: &Connection,
dataset: &str, dataset: &str,
@ -172,18 +186,8 @@ CREATE TABLE "{0}_network" (
FOREIGN KEY(player_B) REFERENCES "{0}_players" FOREIGN KEY(player_B) REFERENCES "{0}_players"
ON DELETE CASCADE ON DELETE CASCADE
) STRICT; ) STRICT;
CREATE INDEX "{0}_network_A" CREATE INDEX "{0}_network_A" ON "{0}_network" (player_A);
ON "{0}_network" (player_A); CREATE INDEX "{0}_network_B" ON "{0}_network" (player_B);"#,
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;"#,
dataset dataset
); );

View file

@ -1,4 +1,3 @@
#![feature(binary_heap_as_slice)]
#![feature(iterator_try_collect)] #![feature(iterator_try_collect)]
#![feature(extend_one)] #![feature(extend_one)]
@ -93,6 +92,11 @@ enum DatasetSC {
New { name: Option<String> }, New { name: Option<String> },
#[command(about = "Delete a dataset")] #[command(about = "Delete a dataset")]
Delete { name: Option<String> }, Delete { name: Option<String> },
#[command(about = "Rename a dataset")]
Rename {
old: Option<String>,
new: Option<String>,
},
} }
#[derive(Subcommand)] #[derive(Subcommand)]
@ -115,6 +119,9 @@ fn main() {
Subcommands::Dataset { Subcommands::Dataset {
subcommand: DatasetSC::Delete { name }, subcommand: DatasetSC::Delete { name },
} => dataset_delete(name), } => dataset_delete(name),
Subcommands::Dataset {
subcommand: DatasetSC::Rename { old, new },
} => dataset_rename(old, new),
Subcommands::Player { Subcommands::Player {
subcommand: PlayerSC::Info { player }, subcommand: PlayerSC::Info { player },
@ -451,6 +458,23 @@ fn dataset_delete(name: Option<String>) {
delete_dataset(&connection, &name).unwrap_or_else(|_| error("That dataset does not exist!", 1)); delete_dataset(&connection, &name).unwrap_or_else(|_| error("That dataset does not exist!", 1));
} }
fn dataset_rename(old: Option<String>, new: Option<String>) {
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 // Players
fn player_info(dataset: Option<String>, player: String) { fn player_info(dataset: Option<String>, player: String) {