-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdao.py
320 lines (265 loc) · 12.4 KB
/
dao.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
from model import User, Problem, Submit, Run
import model
import logging
class UsersDAO:
columns = 'origin, user_id'
def __init__(self, connector):
self.connector = connector
@staticmethod
def load(row):
result = User(row['user_id'], row['origin'])
return result
def deep_load(self, row):
return self.load(row)
def define(self, origin, user_id):
ref = self.lookup(origin, user_id)
if ref is None:
ref = self.create(origin, user_id)
return ref
def lookup(self, origin, user_id):
cursor = self.connector.get_cursor()
cursor.execute('SELECT id FROM Users WHERE origin = ? AND user_id = ?', [origin, user_id])
response = cursor.fetchone()
return response['id'] if response else None
def create(self, origin, user_id):
cursor = self.connector.get_cursor()
cursor.execute('INSERT INTO Users (id, origin, user_id) VALUES (NULL, ?, ?)', [origin, user_id])
return cursor.lastrowid
def update(self, ref, update_def):
cursor = self.connector.get_cursor()
cursor.execute('SELECT {} FROM Users WHERE id = ?'.format(self.columns), [ref])
old = self.load(cursor.fetchone())
new_def = {'origin': old.origin, 'user_id': old.user_id}
for key, value in update_def.items():
new_def[key] = value
new_def['id'] = ref
cursor.execute('UPDATE Users SET origin = :origin, user_id = :user_id WHERE id = :id', new_def)
class ContestsDAO:
columns = 'contest_id, origin, name, scoring'
def __init__(self, connector):
self.connector = connector
@staticmethod
def load(row):
result = model.Contest(row['contest_id'], row['origin'], row['name'], row['scoring'])
return result
def deep_load(self, row):
return self.load(row)
def define(self, origin, scoring, contest_id):
ref = self.lookup(origin, scoring, contest_id)
if ref is None:
ref = self.create(origin, scoring, contest_id)
return ref
def lookup(self, origin, scoring, contest_id):
cursor = self.connector.get_cursor()
cursor.execute('SELECT id FROM Contests WHERE origin = ? AND scoring = ? AND contest_id = ?',
[origin, scoring, contest_id])
response = cursor.fetchone()
return response['id'] if response else None
def create(self, origin, scoring, contest_id):
cursor = self.connector.get_cursor()
cursor.execute('INSERT INTO Contests (id, origin, scoring, contest_id) VALUES (NULL, ?, ?, ?)',
[origin, scoring, contest_id])
return cursor.lastrowid
def update(self, ref, update_def):
cursor = self.connector.get_cursor()
cursor.execute('SELECT {} FROM Contests WHERE id = ?'.format(ContestsDAO.columns), [ref])
old = self.load(cursor.fetchone())
new_def = {'origin': old.origin, 'name': old.name, 'scoring': old.scoring, 'contest_id': old.contest_id}
for key, value in update_def.items():
new_def[key] = value
new_def['id'] = ref
cursor.execute('UPDATE Contests SET origin = :origin, name = :name, scoring = :scoring, '
'contest_id = :contest_id WHERE id = :id', new_def)
class CasesDAO:
columns = "io_hash"
def __init__(self, connector):
self.connector = connector
@staticmethod
def load(row):
result_hash = row['io_hash']
return result_hash
def deep_load(self, row):
return self.load(row)
def define(self, problem_ref, case_id):
ref = self.lookup(problem_ref, case_id)
if ref is None:
ref = self.create(problem_ref, case_id)
return ref
def lookup(self, problem_ref, case_id):
cursor = self.connector.get_cursor()
cursor.execute('SELECT id FROM Cases WHERE problem_ref = ? AND case_id = ?', [problem_ref, case_id])
response = cursor.fetchone()
return response['id'] if response else None
def create(self, problem_ref, case_id):
cursor = self.connector.get_cursor()
cursor.execute('INSERT INTO Cases (id, problem_ref, case_id) VALUES (NULL, ?, ?)', [problem_ref, case_id])
return cursor.lastrowid
def update(self, ref, update_def):
cursor = self.connector.get_cursor()
cursor.execute('SELECT {} FROM Cases WHERE id = ?'.format(self.columns), ref)
old = self.load(cursor.fetchone())
new_def = {'io_hash': old}
for key, value in update_def.items():
new_def[key] = value
new_def['id'] = ref
cursor.execute('UPDATE Cases SET io_hash = :io_hash WHERE id = :id', new_def)
class ProblemsDAO:
columns = 'Problems.id, contest_ref, problem_id, Problems.name, polygon_id'
def __init__(self, connector):
self.connector = connector
@staticmethod
def load(row):
result = Problem(('', row['problem_id']), row['polygon_id'], row['name'], [])
result.contest_ref = row['contest_ref']
result.db_id = row['id'] # kostil
return result
def deep_load(self, row, contest_id=None):
result = self.load(row)
cursor = self.connector.get_cursor()
if contest_id is not None:
result.problem_id = (contest_id, row['problem_id'])
else:
cursor.execute('SELECT contest_id FROM Contests WHERE id = ?', [row['contest_ref']])
result.problem_id = (cursor.fetchone()['contest_id'], row['problem_id'])
cursor.execute('SELECT {} FROM Cases WHERE problem_ref = ?'.format(CasesDAO.columns), [row['id']])
cases_row = cursor.fetchone()
while cases_row:
hash = CasesDAO.load(cases_row)
result.cases.append(hash)
cases_row = cursor.fetchone()
return result
def define(self, contest_ref, problem_id):
ref = self.lookup(contest_ref, problem_id)
if ref is None:
ref = self.create(contest_ref, problem_id)
return ref
def lookup(self, contest_ref, problem_id):
cursor = self.connector.get_cursor()
cursor.execute('SELECT id FROM Problems WHERE contest_ref = ? AND problem_id = ?',
[contest_ref, problem_id])
response = cursor.fetchone()
return response['id'] if response else None
def create(self, contest_ref, problem_id):
cursor = self.connector.get_cursor()
cursor.execute('INSERT INTO Problems (id, contest_ref, problem_id) VALUES (NULL, ?, ?)',
[contest_ref, problem_id])
return cursor.lastrowid
def update(self, ref, update_def):
cursor = self.connector.get_cursor()
cursor.execute('SELECT {} FROM Problems WHERE id = ?'.format(self.columns), [ref])
old = self.load(cursor.fetchone())
new_def = {'contest_ref': old.contest_ref, 'problem_id': old.problem_id[1], 'name': old.name, 'polygon_id': old.polygon_id}
for key, value in update_def.items():
new_def[key] = value
new_def['id'] = ref
cursor.execute('UPDATE Problems SET contest_ref = :contest_ref, name = :name, problem_id = :problem_id, polygon_id = :polygon_id, '
'WHERE id = :id', new_def)
class SubmitsDAO:
columns = 'Submits.id, submit_id, lang_id, problem_ref, user_ref, outcome, timestamp'
def __init__(self, connector):
self.connector = connector
@staticmethod
def load(row):
submit = Submit(row['submit_id'], ('', ''), '', row['lang_id'], [], row['outcome'], '', row['timestamp'])
submit.problem_ref, submit.user_ref = row['problem_ref'], row['user_ref']
return submit
def deep_load(self, row, problem_id=None, scoring=None):
submit = self.load(row)
cursor = self.connector.get_cursor()
cursor.execute('SELECT {} FROM Runs WHERE submit_ref = ?'.format(RunsDAO.columns), [row['id']])
runs_dao = RunsDAO(self.connector)
submit.runs = runs_dao.load_all(cursor.fetchall(), row['problem_ref'])
submit.count_results()
if problem_id is not None:
submit.problem_id = problem_id
else:
logging.warning('SubmitsDAO: problem_id is not filled')
if scoring is not None:
submit.scoring = scoring
else:
logging.warning('SubmitsDAO: scoring is not filled')
return submit
def define(self, submit_id, problem_ref):
ref = self.lookup(submit_id, problem_ref)
if ref is None:
ref = self.create(submit_id, problem_ref)
return ref
def lookup(self, submit_id, problem_ref):
cursor = self.connector.get_cursor()
cursor.execute('SELECT id FROM Submits WHERE submit_id = ? AND problem_ref = ?',
[submit_id, problem_ref])
response = cursor.fetchone()
return response['id'] if response else None
def create(self, submit_id, problem_ref):
cursor = self.connector.get_cursor()
cursor.execute('INSERT INTO Submits (id, submit_id, problem_ref) VALUES (NULL, ?, ?)',
[submit_id, problem_ref])
return cursor.lastrowid
def update(self, ref, update_def):
cursor = self.connector.get_cursor()
cursor.execute('SELECT {} FROM Submits WHERE id = ?'.format(self.columns), [ref])
old = self.load(cursor.fetchone())
new_def = {'submit_id': old.submit_id, 'lang_id': old.lang_id, 'problem_ref': old.problem_ref,
'user_ref': old.user_ref, 'outcome': old.outcome, 'timestamp': old.timestamp}
for key, value in update_def.items():
new_def[key] = value
new_def['id'] = ref
cursor.execute('UPDATE Submits SET submit_id = :submit_id, lang_id = :lang_id, problem_ref = :problem_ref, '
'user_ref = :user_ref, outcome = :outcome, timestamp = :timestamp WHERE id = :id', new_def)
class RunsDAO:
columns = 'realtime, time, outcome, submit_ref, case_ref'
case_cache = {}
def __init__(self, sqlite_connector):
self.connector = sqlite_connector
@staticmethod
def load(row):
run = Run('', '', '', row['realtime'], row['time'], row['outcome'])
run.submit_ref, run.case_ref = row['submit_ref'], row['case_ref']
return run
def deep_load(self, row):
run = self.load(row)
cursor = self.connector.get_cursor()
cursor.execute('SELECT case_id FROM Cases WHERE id = ?', [row['case_ref']])
run.case_id = cursor.fetchone()['case_id']
return run
def load_all(self, rows, problem_ref):
cursor = self.connector.get_cursor()
runs = []
if problem_ref in self.case_cache:
cases = self.case_cache[problem_ref]
else:
cursor.execute('SELECT id,case_id FROM Cases WHERE problem_ref=?', (problem_ref,))
cases = dict(cursor.fetchall())
self.case_cache[problem_ref] = cases
for row in rows:
run = self.load(row)
run.case_id = cases[row['case_ref']]
runs.append(run)
return runs
def define(self, submit_ref, case_ref):
ref = self.lookup(submit_ref, case_ref)
if ref is None:
ref = self.create(submit_ref, case_ref)
return ref
def lookup(self, submit_ref, case_ref):
cursor = self.connector.get_cursor()
cursor.execute('SELECT id FROM Runs WHERE submit_ref = ? AND case_ref = ?',
[submit_ref, case_ref])
response = cursor.fetchone()
return response['id'] if response else None
def create(self, submit_ref, case_ref):
cursor = self.connector.get_cursor()
cursor.execute('INSERT INTO Runs (id, submit_ref, case_ref) VALUES (NULL, ?, ?)',
[submit_ref, case_ref])
return cursor.lastrowid
def update(self, ref, update_def):
cursor = self.connector.get_cursor()
cursor.execute('SELECT {} FROM Runs WHERE id = ?'.format(self.columns), [ref])
old = self.load(cursor.fetchone())
new_def = {'submit_ref': old.submit_ref, 'case_ref': old.case_ref, 'realtime': old.real_time,
'time': old.time, 'outcome': old.outcome}
for key, value in update_def.items():
new_def[key] = value
new_def['id'] = ref
cursor.execute('UPDATE Runs SET submit_ref = :submit_ref, case_ref = :case_ref, realtime = :realtime, '
'time = :time, outcome = :outcome WHERE id = :id', new_def)