Reorganized tables

main
noah metz 2024-01-27 13:35:07 -07:00
parent 61a1f8d8e0
commit e96790b215
1 changed files with 21 additions and 17 deletions

@ -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)).