-
Notifications
You must be signed in to change notification settings - Fork 7
/
Database.py
389 lines (301 loc) · 15.6 KB
/
Database.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import psycopg2, psycopg2.extras, re
from psycopg2 import DatabaseError, InterfaceError
import Logger
from contextlib import closing
class Database(object):
def __init__(self, host, port, user, db, pwd, bot):
self.bot = bot
self.host = host
self.port = port
self.user = user
self.db = db
self.pwd = pwd
self.conn = None
#self.cursor = None
self.logger = Logger.getLogger("MCPBot.Database", self.bot.lognormal, self.bot.logerrors)
def connect(self):
self.conn = psycopg2.connect(database=self.db, user=self.user, password=self.pwd, host=self.host, port=self.port)
if not self.conn:
self.logger.error("Error while connecting to database")
return None
else:
self.logger.info("Connection to database established")
#self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
#return self.cursor
return self.conn
def disconnect(self):
self.logger.info("Committing all changes and shutting down db connection")
self.conn.commit()
#self.cursor.close()
self.conn.close()
self.logger.info("Done")
def checkdbconn(self):
try:
with self.conn.cursor() as cur:
cur.execute("select 1;")
except (DatabaseError, InterfaceError):
self.conn = psycopg2.connect(database=self.db, user=self.user, password=self.pwd, host=self.host, port=self.port)
self.logger.info("*** Connection to database re-established ***")
def execute(self, request, arguments=None, cursor_type=psycopg2.extras.DictCursor):
self.checkdbconn()
with closing(self.conn.cursor(cursor_factory=cursor_type)) as cursor:
try:
if arguments:
bound_request = cursor.mogrify(request, arguments)
else:
bound_request = request
self.logger.info(bound_request)
cursor.execute(bound_request)
retval = cursor.fetchall()
self.conn.commit()
return retval, None
except Exception as e:
self.conn.rollback()
return None, e
# Getters
def getVersions(self, limit=0, cursor_type=psycopg2.extras.DictCursor):
sqlrequest = "select * from mcp.version_vw order by mcp_version_code desc "
if limit > 0: sqlrequest += "limit " + str(limit)
return self.execute(sqlrequest, cursor_type=cursor_type)
def getVersionPromotions(self, limit=1, cursor_type=psycopg2.extras.DictCursor):
sqlrequest = "select * from mcp.promotion_vw order by promoted_ts desc "
if limit > 0: sqlrequest += "limit " + str(limit)
return self.execute(sqlrequest, cursor_type=cursor_type)
def getAvailableVersions(self, version_type=None, cursor_type=psycopg2.extras.DictCursor):
sqlrequest = 'select * from mcp.versions_available_vw '
if version_type:
sqlrequest += 'where VERSION_TYPE = %s '
sqlrequest += 'order by mc_version_pid desc, VERSION_TYPE, cast (VERSION_CODE as INTEGER) desc '
if version_type:
return self.execute(sqlrequest, (version_type,), cursor_type=cursor_type)
else:
return self.execute(sqlrequest, cursor_type=cursor_type)
def getParam(self, args):
arg1 = args[0]
if arg1[0] == ".": arg1 = arg1[1:]
params = {}
sqlrequest = "SELECT * FROM mcp.method_param_vw "
if len(args) > 1:
versplit = args[1].split('.')
if len(versplit) == 2:
sqlrequest += "where (mcp_version_code like %(version)s or mc_version_code like %(version)s) "
else:
sqlrequest += "where mc_version_code like %(version)s "
params['version'] = args[1]
else: sqlrequest += "WHERE is_current "
splitted = arg1.split('.')
params['param'] = splitted[-1]
_splitted = splitted[-1].split('_')
if arg1.startswith('p_'):
sqlrequest += 'AND srg_name = %(param)s '
elif len(_splitted) == 2 and is_integer(_splitted[0]) and is_integer(_splitted[1]):
sqlrequest += 'AND srg_index = %(param)s '
else:
sqlrequest += 'AND mcp_name = %(param)s '
if len(splitted) >= 2:
if splitted[-2].startswith('func_'):
sqlrequest += 'AND method_srg_name = %(method)s '
else:
sqlrequest += 'AND method_mcp_name = %(method)s '
params['method'] = splitted[-2]
if len(splitted) == 3:
sqlrequest += "AND class_srg_name = %(class)s "
params['class'] = splitted[0]
else: # if the class is not specified, only return the record for the base class entry
sqlrequest += "AND class_srg_name = srg_member_base_class "
return self.execute(sqlrequest, params)
def getMember(self, table, args):
member = args[0]
if member[0] == ".": member = member[1:]
params = {}
sqlrequest = "SELECT * FROM mcp.%s " % (table + "_vw")
if len(args) > 1:
versplit = args[1].split('.')
if len(versplit) == 2:
sqlrequest += "where (mcp_version_code like %(version)s or mc_version_code like %(version)s) "
else:
sqlrequest += "where mc_version_code like %(version)s "
params['version'] = args[1]
else: sqlrequest += "WHERE is_current "
splitted = member.split('.')
if len(splitted) == 1:
sqlrequest += "AND class_srg_name = srg_member_base_class "
params.update({'member':member})
else:
sqlrequest += "AND (class_srg_name = %(class)s OR class_obf_name = %(class)s) "
params.update({'class':splitted[0], 'member':splitted[1]})
if params['member'].startswith('func_') or params['member'].startswith('field_'):
sqlrequest += 'AND srg_name = %(member)s'
elif is_integer(params['member'].lstrip('i')):
sqlrequest += 'AND srg_index = %(member)s'
else:
sqlrequest += 'AND (mcp_name = %(member)s OR obf_name = %(member)s)'
return self.execute(sqlrequest, params)
def getHistory(self, table, args):
sqlrequest1 = """SELECT *, 'Committed' as status FROM mcp.%s """ % (table + "_history_vw")
sqlrequest2 = """SELECT *, 'Staged' as status FROM mcp.%s """ % ('staged_' + table + "_vw")
if args[0].startswith('func_') or args[0].startswith('field_') or args[0].startswith('p_'):
sqlrequest1 += 'where srg_name = %(member)s'
sqlrequest2 += 'where srg_name = %(member)s'
else:
sqlrequest1 += 'where srg_index = %(member)s'
sqlrequest2 += 'where srg_index = %(member)s'
sqlrequest = 'select * from (%s union %s) hist order by srg_name, time_stamp desc' % (sqlrequest1, sqlrequest2)
return self.execute(sqlrequest, {'member': args[0]})
def searchHistory(self, table, args):
sqlrequest1 = "SELECT *, 'Committed' as status FROM mcp.%s " % (table + "_history_vw")
sqlrequest2 = "SELECT *, 'Staged' as status FROM mcp.%s " % ('staged_' + table + "_vw")
sqlrequest1 += 'where (old_mcp_name = %(member)s or new_mcp_name = %(member)s) '
sqlrequest2 += 'where (old_mcp_name = %(member)s or new_mcp_name = %(member)s) '
splitted = args[0].split('.')
if len(splitted) > 1:
clazz = splitted[0]
member = splitted[1]
sqlrequest1 += 'and class_srg_name = %(clazz)s'
sqlrequest2 += 'and class_srg_name = %(clazz)s'
else:
clazz = None
member = args[0]
sqlrequest = 'select * from (%s union %s) hist order by srg_name, time_stamp desc' % (sqlrequest1, sqlrequest2)
return self.execute(sqlrequest, {'clazz': clazz, 'member': member})
def getClass(self, args):
sqlrequest = "SELECT * FROM mcp.class_vw "
if len(args) > 1:
versplit = args[1].split('.')
if len(versplit) == 2:
sqlrequest += "where (mcp_version_code like %(version)s or mc_version_code like %(version)s) "
else:
sqlrequest += "where mc_version_code like %(version)s "
else:
sqlrequest += "WHERE is_current "
sqlrequest += """AND (obf_name = %(clazz)s
OR srg_name = %(clazz)s)"""
if len(args) > 1: return self.execute(sqlrequest, {'clazz':args[0], 'version':args[1]})
else: return self.execute(sqlrequest, {'clazz':args[0]})
def findInTable(self, table, args):
sqlrequest = "SELECT * FROM mcp.%s " % (table + '_vw')
if len(args) > 1:
versplit = args[1].split('.')
if len(versplit) == 2:
sqlrequest += "where (mcp_version_code like %(version)s or mc_version_code like %(version)s) "
else:
sqlrequest += "where mc_version_code like %(version)s "
else:
sqlrequest += "WHERE is_current "
clazz = None
method = None
prefix = args[0][0] if args[0][0] == '^' else ''
suffix = args[0][-1] if args[0][-1] == '$' else ''
splitted = args[0].lstrip('^').rstrip('$').split(r'\.')
if len(splitted) == 1:
splitted = args[0].lstrip('^').rstrip('$').split(r'||')
if len(splitted) == 1:
splitted = args[0].lstrip('^').rstrip('$').split(r'@')
if len(splitted) == 1:
splitted = args[0].lstrip('^').rstrip('$').split(r':')
if table in ['method', 'field']:
if len(splitted) > 1:
clazz = prefix + splitted[0] + suffix
name = prefix + splitted[1] + suffix
else:
name = args[0]
elif table == 'method_param':
if len(splitted) > 2:
clazz = prefix + splitted[0] + suffix
method = prefix + splitted[1] + suffix
name = prefix + splitted[2] + suffix
elif len(splitted) > 1:
method = prefix + splitted[0] + suffix
name = prefix + splitted[1] + suffix
else:
name = args[0]
else:
name = args[0]
if clazz:
sqlrequest += "AND class_srg_name ~* %(class)s "
if method:
if method.lstrip('^').startswith('func_'):
sqlrequest += "AND method_srg_name ~ %(method)s "
else:
sqlrequest += "AND (method_mcp_name ~* %(method)s OR method_srg_index ~ %(method)s) "
if name.lstrip('^').startswith('func_') or name.lstrip('^').startswith('field_') or name.lstrip('^').startswith('p_'):
sqlrequest += "AND srg_name ~ %(name)s "
elif is_integer(args[0]):
sqlrequest += "AND srg_index ~ %(name)s "
else:
arg0split = args[0].lstrip('^').rstrip('$').split('_')
if table == 'method_param' and len(arg0split) == 2 and is_integer(arg0split[0]) and is_integer(arg0split[1]):
sqlrequest += "AND srg_index ~ %(name)s "
else:
if table == 'class':
sqlrequest += 'AND srg_name ~ %(name)s '
else:
sqlrequest += "AND (mcp_name ~* %(name)s OR srg_name ~ %(name)s OR srg_index ~ %(name)s) "
if table != 'class':
sqlrequest += "ORDER BY class_srg_name"
if table == 'method_param':
sqlrequest += ', srg_name'
else:
sqlrequest += ', mcp_name'
else:
sqlrequest += "ORDER BY pkg_name, srg_name"
if len(args) > 1: return self.execute(sqlrequest, {'class': clazz, 'method': method, 'name': name, 'version': args[1]})
else: return self.execute(sqlrequest, {'class': clazz, 'method': method, 'name': name})
def getUnnamed(self, table, args):
pkg, _, class_name = args[0].rpartition('/')
sqlrequest = "SELECT * FROM mcp.%s WHERE is_current " % (table + '_vw')
if pkg == '':
sqlrequest += "AND class_srg_name = %(class_name)s "
qry_params = {'class_name': class_name}
else:
sqlrequest += "AND class_srg_name = %(class_name)s AND class_pkg_name = %(pkg)s "
qry_params = {'class_name': class_name, 'pkg': pkg}
sqlrequest += 'AND mcp_name '
if table == 'method':
sqlrequest += "~ 'func_[0-9]+_[a-zA-Z]+' "
elif table == 'field':
sqlrequest += "~ 'field_[0-9]+_[a-zA-Z]+' "
else:
sqlrequest += "~ 'p_i?[0-9]+_[0-9]+_' "
sqlrequest += 'ORDER BY srg_name'
return self.execute(sqlrequest, qry_params)
# Setters
def setMemberLock(self, member_type, is_lock, command, sender, args):
params = {'member_type': member_type, 'is_lock': is_lock, 'nick': sender.regnick.lower(),
'command': command, 'params': ' '.join(args), 'srg_name': args[0]}
sqlrequest = "select mcp.set_member_lock(%(member_type)s, %(command)s, %(nick)s, %(params)s, %(srg_name)s, %(is_lock)s) as result;"
return self.execute(sqlrequest, params)
def doMemberUndo(self, member_type, is_undo, can_undo_any, command, sender, args):
params = {'member_type': member_type, 'is_undo': is_undo, 'can_undo_any': can_undo_any, 'nick': sender.regnick.lower(),
'command': command, 'params': ' '.join(args), 'srg_name': args[0]}
sqlrequest = "select mcp.undo_staged_member(%(member_type)s, %(command)s, %(nick)s, %(params)s, %(srg_name)s, %(is_undo)s, %(can_undo_any)s) as result;"
return self.execute(sqlrequest, params)
def setMember(self, member_type, is_forced, bypass_lock, command, sender, args):
params = {'member_type': member_type, 'is_forced': is_forced, 'bypass_lock': bypass_lock, 'nick': sender.regnick.lower(),
'command': command, 'params': ' '.join(args), 'srg_name': args[0], 'new_name': args[1]}
if len(args) > 2: params['new_desc'] = ' '.join(args[2:])
else: params['new_desc'] = None
sqlrequest = """select mcp.process_member_change(%(member_type)s, %(command)s, %(nick)s, %(params)s, %(srg_name)s,
%(new_name)s, %(new_desc)s, %(is_forced)s, %(bypass_lock)s) as result;"""
return self.execute(sqlrequest, params)
def removeComment(self, member_type, command, sender, args):
params = {'member_type': member_type, 'nick': sender.regnick.lower(),
'command': command, 'params': ' '.join(args), 'srg_name': args[0]}
sqlrequest = """select mcp.remove_member_comment(%(member_type)s, %(command)s, %(nick)s, %(params)s, %(srg_name)s) as result;"""
return self.execute(sqlrequest, params)
def getMemberChange(self, member_type, staged_pid):
sqlrequest = "select * from mcp.staged_%(member_type)s where staged_%(member_type)s_pid = %%(staged_pid)s" % {'member_type': member_type}
return self.execute(sqlrequest, {'staged_pid': staged_pid})
def doCommit(self, member_type, command, sender, args, srg_name):
sqlrequest = 'select mcp.commit_mappings(%s, %s, %s, %s, %s);'
return self.execute(sqlrequest, (member_type, command, sender.regnick.lower(), ' '.join(args), srg_name))
def addAvailableVersion(self, mc_version, version_type, version_code):
sqlrequest = 'select mcp.add_available_version(%s, %s, %s);'
return self.execute(sqlrequest, (mc_version, version_type, version_code))
def is_integer(s):
try:
int(s)
except ValueError:
return False
else:
return True