-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb2.sql
100 lines (83 loc) · 2.69 KB
/
db2.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
-- INSERT INTO attempts (userid, taskid) VALUES ((SELECT ID FROM users), 1);
-- CREATE DATABASE docker;
--
-- create user docker with encrypted password 'docker';
--
-- grant all privileges on database docker to docker;
DROP TABLE IF EXISTS users CASCADE;
CREATE TABLE IF NOT EXISTS users
(
ID BIGSERIAL NOT NULL PRIMARY KEY,
userName TEXT UNIQUE,
fullName TEXT,
studentID TEXT
);
DROP TABLE IF EXISTS tasks CASCADE;
CREATE TABLE IF NOT EXISTS tasks
(
ID BIGSERIAL NOT NULL PRIMARY KEY,
taskName TEXT UNIQUE,
fullName TEXT,
maxTime BIGINT,
maxMemory BIGINT
);
DROP TABLE tasks CASCADE;
DROP TABLE IF EXISTS attempts CASCADE;
CREATE TABLE IF NOT EXISTS attempts
(
ID BIGSERIAL NOT NULL PRIMARY KEY,
userID BIGINT NOT NULL,
taskID BIGINT NOT NULL,
time BIGINT DEFAULT 0,
memory BIGINT DEFAULT 0,
sourceCode TEXT NOT NULL ,
uploadDate TIMESTAMP WITH TIME ZONE NOT NULL,
status INT,
FOREIGN KEY (userID) REFERENCES users (ID) ON DELETE CASCADE,
FOREIGN KEY (taskID) REFERENCES tasks (ID) ON DELETE CASCADE
);
DROP TABLE IF EXISTS borrowings CASCADE;
CREATE TABLE IF NOT EXISTS borrowings
(
attemptID BIGINT NOT NULL,
copiedFrom BIGINT NOT NULL,
plagiarismPercent INT,
FOREIGN KEY (attemptID) REFERENCES attempts (ID) ON DELETE CASCADE,
FOREIGN KEY (copiedFrom) REFERENCES attempts (ID) ON DELETE CASCADE
);
DROP TABLE IF EXISTS hashes CASCADE;
CREATE TABLE IF NOT EXISTS hashes
(
attemptID BIGINT NOT NULL PRIMARY KEY,
hash BIGINT,
FOREIGN KEY (attemptID) REFERENCES attempts (ID) ON DELETE CASCADE
);
DROP TABLE IF EXISTS hashes2 CASCADE;
CREATE TABLE IF NOT EXISTS hashes2
(
attemptID BIGINT NOT NULL PRIMARY KEY,
hash BIGINT[],
FOREIGN KEY (attemptID) REFERENCES attempts (ID) ON DELETE CASCADE
);
INSERT INTO hashes2 VALUES (11, '{1, 2, 3}'::BIGINT[]);
DROP VIEW IF EXISTS results;
CREATE VIEW results
AS
SELECT a.ID as ID,
u.userName as userName,
t.taskName as taskName,
a.uploadDate as uploadDate,
a.status as status,
b.plagiarismPercent as percent,
u2.userName as copiedFrom,
t2.taskName as copiedTask,
a2.uploadDate as copiedDate,
a.sourcecode as sourceCode,
a2.sourceCode as copiedCode
FROM users u
JOIN attempts a on u.ID = a.userID
JOIN tasks t ON a.taskID = t.ID
JOIN borrowings b on a.ID = b.attemptID
JOIN attempts a2 on b.copiedFrom = a2.ID
JOIN users u2 ON a2.userID = u2.ID
JOIN tasks t2 ON a2.taskID = t2.ID;