Fix SQL view not being deleted with dataset

This commit is contained in:
Kiana Sheibani 2023-10-05 22:08:13 -04:00
parent 376a8588b9
commit 38e0937d8f
Signed by: toki
GPG key ID: 6CB106C25E86A9F7

View file

@ -86,7 +86,8 @@ 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
); );
@ -100,38 +101,40 @@ pub fn new_dataset(
) -> sqlite::Result<()> { ) -> sqlite::Result<()> {
let query1 = r#"INSERT INTO datasets VALUES (?, ?, ?, ?, ?)"#; let query1 = r#"INSERT INTO datasets VALUES (?, ?, ?, ?, ?)"#;
let query2 = format!( let query2 = format!(
r#" r#"CREATE TABLE "{0}_players" (
CREATE TABLE "{0}_players" ( id INTEGER PRIMARY KEY,
id INTEGER PRIMARY KEY, name TEXT,
name TEXT, prefix TEXT
prefix TEXT );
);
CREATE TABLE "{0}_network" (
player_A INTEGER NOT NULL,
player_B INTEGER NOT NULL,
advantage REAL NOT NULL,
sets_A INTEGER NOT NULL DEFAULT 0,
sets_B INTEGER NOT NULL DEFAULT 0,
games_A INTEGER NOT NULL DEFAULT 0,
games_B INTEGER NOT NULL DEFAULT 0,
UNIQUE (player_A, player_B), CREATE TABLE "{0}_network" (
CHECK (player_A < player_B), player_A INTEGER NOT NULL,
FOREIGN KEY(player_A) REFERENCES "{0}_players" player_B INTEGER NOT NULL,
ON DELETE CASCADE, advantage REAL NOT NULL,
FOREIGN KEY(player_B) REFERENCES "{0}_players" sets_A INTEGER NOT NULL DEFAULT 0,
ON DELETE CASCADE sets_B INTEGER NOT NULL DEFAULT 0,
) STRICT; games_A INTEGER NOT NULL DEFAULT 0,
CREATE INDEX "{0}_network_A" games_B INTEGER NOT NULL DEFAULT 0,
ON "{0}_network" (player_A);
CREATE INDEX "{0}_network_B"
ON "{0}_network" (player_B);
CREATE VIEW "{0}_view" UNIQUE (player_A, player_B),
(player_A_id, player_B_id, player_A_name, player_B_name, advantage, sets_A, sets_B, games_A, games_B) AS CHECK (player_A < player_B),
SELECT players_A.id, players_B.id, players_A.name, players_B.name, advantage, sets_A, sets_B, games_A, games_B FROM "{0}_network" FOREIGN KEY(player_A) REFERENCES "{0}_players"
INNER JOIN "{0}_players" players_A ON player_A = players_A.id ON DELETE CASCADE,
INNER JOIN "{0}_players" players_B ON player_B = players_B.id;"#, 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_B, sets, games_A, games_B, games) AS
SELECT players_A.id, players_B.id, players_A.name, players_B.name, advantage,
sets_A, sets_B, sets_A + sets_B, games_A, games_B, games_A + games_B FROM "{0}_network"
INNER JOIN "{0}_players" players_A ON player_A = players_A.id
INNER JOIN "{0}_players" players_B ON player_B = players_B.id;"#,
dataset dataset
); );