From e96790b21515322ecebc17814bad25eef5741645 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Sat, 27 Jan 2024 13:35:07 -0700 Subject: [PATCH] Reorganized tables --- src/database.erl | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/database.erl b/src/database.erl index 1a801e5..8d7824c 100644 --- a/src/database.erl +++ b/src/database.erl @@ -25,15 +25,19 @@ init([DatabaseFile]) -> end. init_db(Database) -> - ok = sqlite3:create_table(Database, genids, [{genid, text, [not_null]}, - {type, text, [not_null]}, - {size, integer, [not_null]}, - {seed, integer, [not_null]}], - [{primary_key, [genid]}, - {check, "type IN ('division', 'final')"}]), - ok = sqlite3:create_table(Database, teams, [{name, text, [not_null]}, + ok = sqlite3:create_table(Database, genids, [{genid, text, [not_null]}], + [{primary_key, [genid]}]), + ok = sqlite3:create_table(Database, divisions, [{division, integer, [not_null]}, + {prac_size, integer, [not_null]}, + {prac_seed, integer, [not_null]}, + {qual_size, integer, [not_null]}, + {qual_seed, integer, [not_null]}], + [{primary_key, [division]}]), + ok = sqlite3:create_table(Database, teams, [{team, text, [not_null]}, + {division, integer}, {inspection, blob}], - [{primary_key, [name]}]), + [{primary_key, [team]}, + {foreign_key, {[division], divisions, [division], "ON DELETE RESTRICT"}}]), ok = sqlite3:create_table(Database, matches, [{genid, text, [not_null]}, {round, text, [not_null]}, {number, integer, [not_null]}, @@ -44,11 +48,10 @@ init_db(Database) -> [{primary_key, [genid, round, number]}, {check, "round IN ('practice', 'qualification', 'elimination', 'final')"}, {foreign_key, {[genid], genids, [genid], "ON DELETE RESTRICT"}}, - {foreign_key, {[blue_1], teams, [name], "ON DELETE RESTRICT"}}, - {foreign_key, {[blue_2], teams, [name], "ON DELETE RESTRICT"}}, - {foreign_key, {[red_1], teams, [name], "ON DELETE RESTRICT"}}, - {foreign_key, {[red_2], teams, [name], "ON DELETE RESTRICT"}}]), - + {foreign_key, {[blue_1], teams, [team], "ON DELETE RESTRICT"}}, + {foreign_key, {[blue_2], teams, [team], "ON DELETE RESTRICT"}}, + {foreign_key, {[red_1], teams, [team], "ON DELETE RESTRICT"}}, + {foreign_key, {[red_2], teams, [team], "ON DELETE RESTRICT"}}]), ok = sqlite3:create_table(Database, match_scores, [{genid, text, [not_null]}, {round, text, [not_null]}, {number, integer, [not_null]}, @@ -67,21 +70,22 @@ init_db(Database) -> ok = sqlite3:create_table(Database, skills_scores, [{team, text, [not_null]}, {type, text, [not_null]}, {attempt, integer, [not_null]}, - {score, blob, [not_null]}], + {score, blob}], [{primary_key, [team, type, attempt]}, - {foreign_key, {[team], teams, [name], "ON DELETE CASCADE"}}]), + {check, "type IN ('driver', 'autonomous')"}, + {foreign_key, {[team], teams, [team], "ON DELETE CASCADE"}}]), ok. delete_teams(Database, Teams) -> SQL = lists:append(["BEGIN; ", - lists:append([io_lib:format("DELETE FROM teams WHERE name = ~p; ", + lists:append([io_lib:format("DELETE FROM teams WHERE team = ~p; ", [Team]) || Team <- Teams]), "COMMIT;"]), first_error(sqlite3:sql_exec_script(Database, SQL)). add_teams(Database, Teams) -> SQL = lists:append(["BEGIN; ", - lists:append([io_lib:format("INSERT INTO teams(name) VALUES('~s') ON CONFLICT(name) DO NOTHING; ", + lists:append([io_lib:format("INSERT INTO teams(team) VALUES('~s') ON CONFLICT(team) DO NOTHING; ", [Team]) || Team <- Teams]), "COMMIT;"]), first_error(sqlite3:sql_exec_script(Database, SQL)).