-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.rb
77 lines (68 loc) · 2.04 KB
/
db.rb
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
require 'sqlite3'
module DB
class Connector
def initialize
@db = SQLite3::Database.new 'cpc.db'
end
def create_tables
@db.execute '
CREATE TABLE user (
id INTEGER PRIMARY KEY,
alias TEXT,
UNIQUE (alias)
);'
@db.execute '
CREATE TABLE contest (
id INTEGER PRIMARY KEY,
name TEXT,
alias TEXT,
start INTEGER,
end INTEGER,
UNIQUE(name)
);'
@db.execute '
CREATE TABLE problem (
id INTEGER PRIMARY KEY,
contest INTEGER,
name TEXT,
points INTEGER,
FOREIGN KEY(contest) REFERENCES contest(id),
UNIQUE(name)
);'
@db.execute '
CREATE TABLE submission (
id INTEGER PRIMARY KEY,
user INTEGER,
problem INTEGER,
time INTEGER,
status STRING,
executionTime INTEGER,
errorId INTEGER,
score INTEGER,
FOREIGN KEY(user) REFERENCES user(id),
FOREIGN KEY(problem) REFERENCES problem(id)
);'
end
def all_contests
sql = 'SELECT name FROM contest;'
cons = @db.execute sql
cons.flatten
end
def active_contests
sql = '
SELECT name FROM contest
WHERE start < strftime(\'%s\',\'now\')
AND end > strftime(\'%s\',\'now\');'
cons = @db.execute sql
cons.flatten
end
def close
@db.close
end
end
def self.connect(&block)
db = DB::Connector.new
block.call db
db.close
end
end