-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimpleTM.py
262 lines (237 loc) · 8.56 KB
/
SimpleTM.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
import sqlite3
from sqlite3 import Error
import os
import sys
class SimpleTM:
def __init__(self,sSqliteFileName):
self.__sSqliteFileName=sSqliteFileName
isExist=os.path.isfile(sSqliteFileName)
if(isExist==False):
self.__InitSQLite()
try:
self.__conn=sqlite3.connect(self.__sSqliteFileName)
except Error as e:
raise RuntimeError(e)
def __InitSQLite(self):
try:
self.__conn=sqlite3.connect(self.__sSqliteFileName)
c = self.__conn.cursor()
f = open('init_db.sql', encoding='utf-8')
statements = f.read().split(';')
for s in statements:
#print(s)
c.execute(s)
self.__conn.commit()
self.__conn.close()
except Error as e:
raise RuntimeError(e)
def __GetCursor(self):
c = self.__conn.cursor()
c.execute('PRAGMA foreign_keys = ON')
return c
def Query(self, game, raw):
try:
c = self.__GetCursor()
c.execute('''
SELECT raw_word, trans_word
FROM Translate
WHERE game_id=? AND raw_word=?''',
(game, raw,))
self.__conn.commit()
rows=c.fetchall()
return rows
except Error as e:
raise RuntimeError(e)
def QueryByGame(self,game):
try:
c = self.__GetCursor()
c.execute('''
SELECT raw_word, trans_word
FROM Translate
WHERE game_id=?''',
(game,))
self.__conn.commit()
rows=c.fetchall()
return rows
except Error as e:
raise RuntimeError(e)
def QueryGame(self):
try:
c = self.__GetCursor()
c.execute('''
SELECT * FROM Game
''')
self.__conn.commit()
rows=c.fetchall()
return rows
except Error as e:
raise RuntimeError(e)
def __Insert(self, sql, c, *args):
commit = c is None
if commit:
c = self.__GetCursor()
c.execute(sql, args)
if c.rowcount < 1:
self.__conn.rollback()
return False
else:
if commit:
self.__conn.commit()
return True
def AddTranslation(self, sRawWord, sTranslatedWord, sGameID):
return self.__Insert(
'''
INSERT INTO Translate(game_id,raw_word,trans_word)
VALUES (?,?,?)
''', None,
sGameID, sRawWord, sTranslatedWord
)
def UpdateTranslation(self, sRawWord, sTranslatedWord, game_id):
c = self.__GetCursor()
c.execute('''
UPDATE Translate
SET trans_word=?
WHERE game_id=? AND raw_word=?''',
(sTranslatedWord, game_id, sRawWord)
)
if c.rowcount < 1:
self.__conn.rollback()
return False
else:
self.__conn.commit()
return True
def DeleteTranslation(self, sRawWord, game_id):
c = self.__GetCursor()
c.execute('DELETE FROM Translate WHERE game_id=? AND raw_word=?', (game_id, sRawWord))
self.__conn.commit()
return True
def UpdatePermission(self, user_id, game_id, permission, c=None):
commit = c is None
if commit:
c = self.__GetCursor()
c.execute('SELECT * FROM Permission WHERE user_id=? AND game_id=?', (user_id, game_id))
rows = c.fetchall()
if len(rows) == 0:
c.execute('INSERT INTO Permission VALUES (?,?,?)', (user_id, game_id, permission))
else:
c.execute('''
UPDATE Permission SET permission=?
WHERE user_id=? AND game_id=?''',
(permission, user_id, game_id)
)
if c.rowcount < 1:
self.__conn.rollback()
return False
else:
if commit:
self.__conn.commit()
return True
def getPermission(self, uid, gid):
c = self.__GetCursor()
c.execute('SELECT permission FROM Permission WHERE user_id=? AND game_id=?', (uid, gid))
row = c.fetchone()
if not row:
return 0
return row[0]
def AddUser(self, user, salt, token):
return self.__Insert('INSERT INTO User VALUES (?, ?)', None, user, salt) \
and self.__Insert('INSERT INTO APIToken VALUES (?, ?)', None, user, token)
def DeleteUser(self, user):
c = self.__GetCursor()
c.execute('DELETE FROM User WHERE id=?', (user,))
c.execute('DELETE FROM APIToken WHERE user_id=?', (user,))
self.__conn.commit()
def UpdateToken(self, user, token):
c = self.__GetCursor()
c.execute('UPDATE APIToken SET token=? WHERE user_id=?', (token, user))
if c.rowcount < 1:
self.__conn.rollback()
return False
else:
self.__conn.commit()
return True
def QueryUser(self, user):
c = self.__GetCursor()
c.execute('SELECT * FROM User WHERE id=?', (user,))
return c.fetchone()
def GetUserAPIToken(self, user):
c = self.__GetCursor()
c.execute('SELECT token FROM APIToken WHERE user_id=?', (user,))
return c.fetchall()
def AddGame(self, game_id, game_title):
return self.__Insert('INSERT INTO Game VALUES (?, ?)', None, game_id, game_title)
def AddGameAsUser(self, uid, gid, gtitle):
c = self.__GetCursor()
assert self.__Insert('INSERT INTO Game VALUES (?, ?)', c, gid, gtitle)
assert self.UpdatePermission(uid, gid, 3, c)
self.__conn.commit()
def DeleteGame(self, game_id):
c = self.__GetCursor()
c.execute('DELETE FROM Game WHERE id=?', (game_id,))
self.__conn.commit()
def GetUser(self, username):
c = self.__GetCursor()
c.execute('SELECT * from USER where id=?', (username,))
return c.fetchone()
def GetGamesByUser(self, username):
c = self.__GetCursor()
c.execute('''
SELECT p.user_id, p.game_id, g.title, p.permission from Permission AS p
JOIN User AS u ON p.user_id=u.id
JOIN Game AS g ON p.game_id=g.id
WHERE p.user_id=?
''', (username,))
result = []
rows = c.fetchall()
perm_map = ['无','只读','读写','管理员']
for uid, gid, gtitle, perm in rows:
result.append({
'user_id': uid,
'game_id': gid,
'game_title': gtitle,
"permission": perm_map[perm]
})
return result
def GetUsersByGame(self, game_id):
c = self.__GetCursor()
c.execute('''
SELECT p.user_id, p.permission from Permission AS p
JOIN User AS u ON p.user_id=u.id
JOIN Game AS g ON p.game_id=g.id
WHERE p.game_id=?
''', (game_id,))
result = []
rows = c.fetchall()
perm_map = ['无','只读','读写','管理员']
for uid, perm in rows:
result.append({
'user_id': uid,
"permission": perm_map[perm]
})
return result
def Close(self):
self.__conn.close()
if __name__ == "__main__":
#SimpleTest
db = SimpleTM('SimpleTM.db')
assert db.AddTranslation('Test','测试','imoyaba')
print(db.QueryByGame('imoyaba'))
print(db.Query('imoyaba', 'Test'))
assert db.UpdateTranslation('Test','测试2','imoyaba')
assert db.UpdateTranslation('Test','测试3','imoyaba')
assert db.UpdateTranslation('Test2','测试a','imoyaba')
assert db.UpdateTranslation('Test2','测试b','imoyaba')
print(db.QueryByGame('imoyaba'))
try:
print(db.AddTranslation('Test','测试','imoyaba'))
except Exception as e:
print(e)
try:
print(db.AddTranslation('Test','测试','test_game'))
except Exception as e:
print(e)
assert db.AddUser('test_user', 'test_salt')
assert db.AddGame('test_game', 'test_title')
assert db.AddTranslation('Test','测试','test_game')
print(db.QueryGame())
print(db.QueryByGame('test_game'))