Do not return solution during contests
[gruntmaster-data.git] / db.sql
CommitLineData
4ed3f8e7 1CREATE TABLE users (
2fec2d56 2 id TEXT PRIMARY KEY,
9bb39921 3 passphrase TEXT, -- NOT NULL,
2fec2d56 4 admin BOOLEAN NOT NULL DEFAULT FALSE,
4ed3f8e7
MG
5 name TEXT, -- NOT NULL,
6 email TEXT, -- NOT NULL,
9a987597 7 phone TEXT, -- NOT NULL,
4ed3f8e7
MG
8 town TEXT, -- NOT NULL,
9 university TEXT, -- NOT NULL,
9bb39921 10 level TEXT, -- NOT NULL,
73243865 11 country TEXT,
8c7ef664
MG
12 lastjob BIGINT,
13 since BIGINT DEFAULT CAST(EXTRACT(epoch from now()) AS bigint)
4ed3f8e7
MG
14);
15
16CREATE TABLE contests (
b596b2a9
MG
17 id TEXT PRIMARY KEY,
18 name TEXT NOT NULL,
19 editorial TEXT,
20 description TEXT,
21 start INT NOT NULL,
22 stop INT NOT NULL,
23 owner TEXT NOT NULL REFERENCES users ON DELETE CASCADE,
24 CONSTRAINT positive_duration CHECK (stop > start)
4ed3f8e7
MG
25);
26
adb42d4d
MG
27CREATE TABLE contest_status (
28 contest TEXT NOT NULL REFERENCES contests ON DELETE CASCADE,
29 owner TEXT NOT NULL REFERENCES users ON DELETE CASCADE,
30 score INT NOT NULL,
31 rank INT NOT NULL,
32
33 PRIMARY KEY (owner, contest)
34);
35
4ed3f8e7
MG
36CREATE TABLE problems (
37 id TEXT PRIMARY KEY,
20fa6cfc 38 author TEXT,
6a03bf0d 39 writer TEXT,
9bb39921
MG
40 generator TEXT NOT NULL,
41 judge TEXT NOT NULL,
42 level TEXT NOT NULL,
43 name TEXT NOT NULL,
4ed3f8e7 44 olimit INT,
9bb39921
MG
45 owner TEXT NOT NULL REFERENCES users ON DELETE CASCADE,
46 private BOOLEAN NOT NULL DEFAULT FALSE,
47 runner TEXT NOT NULL,
1980106a 48 solution TEXT ,
9bb39921
MG
49 statement TEXT NOT NULL,
50 testcnt INT NOT NULL,
f7c095d3 51 precnt INT,
b2725d9d 52 tests TEXT,
9bb39921 53 timeout REAL NOT NULL,
aaa9eb7d 54 value INT NOT NULL,
85d3f015
MG
55 genformat TEXT,
56 gensource TEXT,
4ed3f8e7
MG
57 verformat TEXT,
58 versource TEXT
59);
60
61CREATE TABLE contest_problems (
9bb39921
MG
62 contest TEXT REFERENCES contests ON DELETE CASCADE,
63 problem TEXT NOT NULL REFERENCES problems ON DELETE CASCADE,
4ed3f8e7
MG
64 PRIMARY KEY (contest, problem)
65);
66
67CREATE TABLE jobs (
68 id SERIAL PRIMARY KEY,
9bb39921 69 contest TEXT REFERENCES contests ON DELETE CASCADE,
4ed3f8e7
MG
70 daemon TEXT,
71 date BIGINT NOT NULL,
72 errors TEXT,
73 extension TEXT NOT NULL,
74 format TEXT NOT NULL,
75 private BOOLEAN NOT NULL DEFAULT FALSE,
9bb39921 76 problem TEXT NOT NULL REFERENCES problems ON DELETE CASCADE,
987ee14e 77 reference INT,
4ed3f8e7
MG
78 result INT,
79 result_text TEXT,
20fa6cfc 80 results TEXT,
4ed3f8e7 81 source TEXT NOT NULL,
9bb39921 82 owner TEXT NOT NULL REFERENCES users ON DELETE CASCADE
4ed3f8e7
MG
83);
84
adb42d4d
MG
85CREATE TABLE problem_status (
86 problem TEXT NOT NULL REFERENCES problems ON DELETE CASCADE,
87 owner TEXT NOT NULL REFERENCES users ON DELETE CASCADE,
88 job SERIAL NOT NULL REFERENCES jobs ON DELETE CASCADE,
89 solved BOOLEAN NOT NULL DEFAULT FALSE,
90
91 PRIMARY KEY (owner, problem)
92);
93
4ed3f8e7 94CREATE TABLE opens (
9bb39921
MG
95 contest TEXT NOT NULL REFERENCES contests ON DELETE CASCADE,
96 problem TEXT NOT NULL REFERENCES problems ON DELETE CASCADE,
97 owner TEXT NOT NULL REFERENCES users ON DELETE CASCADE,
4ed3f8e7
MG
98 time BIGINT NOT NULL,
99 PRIMARY KEY (contest, problem, owner)
100);
be2f7678 101
6dda1f23
MG
102CREATE TABLE limits (
103 problem TEXT NOT NULL REFERENCES problems ON DELETE CASCADE,
104 format TEXT NOT NULL,
105 timeout REAL NOT NULL,
106 PRIMARY KEY (problem, format)
107);
108
be2f7678
MG
109CREATE TABLE table_comments (
110 table_name TEXT NOT NULL PRIMARY KEY,
111 comment_text TEXT NOT NULL
112);
113
114CREATE TABLE column_comments (
115 table_name TEXT NOT NULL,
116 column_name TEXT NOT NULL,
117 comment_text TEXT NOT NULL,
118 PRIMARY KEY (table_name, column_name)
119);
120
121INSERT INTO table_comments VALUES ('users', 'List of users');
122INSERT INTO table_comments VALUES ('contests', 'List of contests');
123INSERT INTO table_comments VALUES ('contest_status', 'List of (contest, user, result)');
124INSERT INTO table_comments VALUES ('problems', 'List of problems');
125INSERT INTO table_comments VALUES ('contest_problems', 'Many-to-many bridge between contests and problems');
126INSERT INTO table_comments VALUES ('jobs', 'List of jobs');
127INSERT INTO table_comments VALUES ('problem_status', 'List of (problem, user, result)');
128INSERT INTO table_comments VALUES ('opens', 'List of (contest, problem, user, time when user opened problem)');
6dda1f23 129INSERT INTO table_comments VALUES ('limits', 'Time limit overrides for certain problem/format pairs');
be2f7678
MG
130
131INSERT INTO column_comments VALUES ('users', 'passphrase', 'RFC2307-encoded passphrase');
132INSERT INTO column_comments VALUES ('users', 'name', 'Full name of user');
133INSERT INTO column_comments VALUES ('users', 'level', 'Highschool, Undergraduate, Master, Doctorate or Other');
134INSERT INTO column_comments VALUES ('users', 'lastjob', 'Unix time when this user last submitted a job');
8c7ef664 135INSERT INTO column_comments VALUES ('users', 'since', 'Unix time when this user was created');
be2f7678
MG
136
137INSERT INTO column_comments VALUES ('contests', 'start', 'Unix time when contest starts');
138INSERT INTO column_comments VALUES ('contests', 'stop', 'Unix time when contest ends');
b596b2a9
MG
139INSERT INTO column_comments VALUES ('contests', 'editorial', 'HTML fragment placed before the editorial');
140INSERT INTO column_comments VALUES ('contests', 'description', 'HTML fragment placed on contest page');
be2f7678
MG
141
142INSERT INTO column_comments VALUES ('problems', 'author', 'Full name(s) of problem author(s)/problemsetter(s)/tester(s)/etc');
143INSERT INTO column_comments VALUES ('problems', 'writer', 'Full name(s) of statement writer(s) (DEPRECATED)');
144INSERT INTO column_comments VALUES ('problems', 'generator', 'Generator class, without the leading Gruntmaster::Daemon::Generator::');
145INSERT INTO column_comments VALUES ('problems', 'runner', 'Runner class, without the leading Gruntmaster::Daemon::Runner::');
146INSERT INTO column_comments VALUES ('problems', 'judge', 'Judge class, without the leading Gruntmaster::Daemon::Judge::');
147INSERT INTO column_comments VALUES ('problems', 'level', 'Problem level, one of beginner, easy, medium, hard');
148INSERT INTO column_comments VALUES ('problems', 'olimit', 'Output limit (in bytes)');
149INSERT INTO column_comments VALUES ('problems', 'timeout', 'Time limit (in seconds)');
150INSERT INTO column_comments VALUES ('problems', 'solution', 'Solution (HTML)');
151INSERT INTO column_comments VALUES ('problems', 'statement', 'Statement (HTML)');
152INSERT INTO column_comments VALUES ('problems', 'testcnt', 'Number of tests');
f7c095d3 153INSERT INTO column_comments VALUES ('problems', 'precnt', 'Number of pretests. NULL indicates full feedback.');
be2f7678
MG
154INSERT INTO column_comments VALUES ('problems', 'tests', 'JSON array of test values for ::Runner::File');
155INSERT INTO column_comments VALUES ('problems', 'value', 'Problem value when used in a contest.');
156INSERT INTO column_comments VALUES ('problems', 'genformat', 'Format (programming language) of the generator if using the Run generator');
157INSERT INTO column_comments VALUES ('problems', 'gensource', 'Source code of generator if using the Run generator');
158INSERT INTO column_comments VALUES ('problems', 'verformat', 'Format (programming language) of the verifier if using the Verifier runner');
159INSERT INTO column_comments VALUES ('problems', 'versource', 'Source code of verifier if using the Verifier runner');
160
161INSERT INTO column_comments VALUES ('jobs', 'daemon', 'hostname:PID of daemon that last executed this job. NULL if never executed');
162INSERT INTO column_comments VALUES ('jobs', 'date', 'Unix time when job was submitted');
163INSERT INTO column_comments VALUES ('jobs', 'errors', 'Compiler errors');
164INSERT INTO column_comments VALUES ('jobs', 'extension', 'File extension of submitted program, without a leading dot');
165INSERT INTO column_comments VALUES ('jobs', 'format', 'Format (programming language) of submitted program');
987ee14e 166INSERT INTO column_comments VALUES ('jobs', 'reference', 'If not null, this is a reference solution that should get this result. For example, set reference=0 on jobs that should be accepted, reference=3 on jobs that should get TLE, etc');
be2f7678
MG
167INSERT INTO column_comments VALUES ('jobs', 'result', 'Job result (integer constant from Gruntmaster::Daemon::Constants)');
168INSERT INTO column_comments VALUES ('jobs', 'result_text', 'Job result (human-readable text)');
169INSERT INTO column_comments VALUES ('jobs', 'results', 'Per-test results (JSON array of hashes with keys id (test number, counting from 1), result (integer constant from Gruntmaster::Daemon::Constants), result_text (human-readable text), time (execution time in decimal seconds))');
170
171INSERT INTO column_comments VALUES ('problem_status', 'solved', 'True if the result is Accepted, False otherwise');
52172a1a
MG
172
173CREATE OR REPLACE VIEW user_data AS (SELECT
174 id,admin,name,town,university,country,level,lastjob
175 FROM users
176);
177
178CREATE OR REPLACE VIEW user_solved_problems AS (SELECT
179 us.id,
180 COALESCE(array_agg(ps.problem) FILTER (WHERE ps.problem IS NOT NULL), ARRAY[]::text[]) AS solved
181 FROM users us
182 LEFT JOIN (SELECT * FROM problem_status WHERE solved = TRUE) ps ON ps.owner=id
183 GROUP BY us.id);
184
185CREATE OR REPLACE VIEW user_attempted_problems AS (SELECT
186 us.id,
187 COALESCE(array_agg(ps.problem) FILTER (WHERE ps.problem IS NOT NULL), ARRAY[]::text[]) AS attempted
188 FROM users us
189 LEFT JOIN (SELECT * FROM problem_status WHERE solved = FALSE) ps ON ps.owner=id
190 GROUP BY us.id);
191
192CREATE OR REPLACE VIEW user_contests AS (SELECT
193 us.id,
194 COALESCE(array_agg(cs.contest) FILTER (WHERE cs.contest IS NOT NULL), ARRAY[]::text[]) AS contests
195 FROM users us
196 LEFT JOIN contest_status cs ON cs.owner=id
197 GROUP BY us.id);
198
199CREATE OR REPLACE VIEW user_list AS (SELECT
200 dt.*,
201 COALESCE(array_length(solved, 1), 0) AS solved,
202 COALESCE(array_length(attempted, 1), 0) AS attempted,
203 COALESCE(array_length(contests, 1), 0) AS contests
204 FROM user_data dt
205 JOIN user_contests ct USING (id)
206 JOIN user_solved_problems sp USING (id)
207 JOIN user_attempted_problems ap USING (id)
208 ORDER BY solved DESC, attempted DESC, id);
209
210CREATE OR REPLACE VIEW contest_entry AS (SELECT
211 id,name,description,editorial,start,stop,owner,
212 (EXTRACT(epoch from NOW()) >= start) AS started,
213 (EXTRACT(epoch from NOW()) >= stop) AS finished
214 FROM contests
215 ORDER BY start DESC);
216
217CREATE OR REPLACE VIEW job_entry AS (SELECT
218 id,contest,date,errors,extension,format,private,problem,result,result_text,results,owner,
219 LENGTH(source) AS size
220 FROM jobs
221 ORDER BY id DESC);
222
223-- CREATE OR REPLACE FUCNTION source_private(jobid INT) RETURNS BOOLEAN AS $$
224-- BEGIN
225-- private BOOLEAN;
226-- problem TEXT;
227-- contest TEXT;
228
229-- SELECT private, problem, contest INTO STRICT private, problem, contest FROM jobs WHERE id = jobid;
230-- IF private THEN
231-- RETURN TRUE;
232-- END IF
233
234-- IF
235-- END;
236-- $$
This page took 0.031071 seconds and 4 git commands to generate.