forked from cryptodotis/code-peer-review
-
Notifications
You must be signed in to change notification settings - Fork 0
/
databaseQueries.py
106 lines (79 loc) · 3.07 KB
/
databaseQueries.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
#!/usr/bin/python
import MySQLdb
from common import *
from config import Config
from database import DB
from repo import Repo
from commit import Commit
from keywordsfilter import *
class DBQ:
@staticmethod
def findByKeywords(keywords):
conn = DB.getConn()
c = conn.cursor()
getcommitsSQL = "SELECT c.*, r.* " + \
"FROM " + DB.commit._table + " c " + \
"INNER JOIN " + DB.repo._table + " r " + \
" ON r.id = c.repoid "
whereClause = " 1=1 "
components = []
if keywords:
keywordsTree = KeywordsParser(keywords)
getcommitsSQL += "LEFT OUTER JOIN " + DB.commitkeyword._table + " ck " + \
" ON c.id = ck.commitid "
whereClause, components = keywordsTree.getWhereClause("ck.keyword", "r.tagname", "r.maturity")
getcommitsSQL += "WHERE " + whereClause
getcommitsSQL += "ORDER BY c.date DESC "
c.execute(getcommitsSQL, components)
commitrows = c.fetchall()
commitfiles = []
if commitrows:
allcommitids = ",".join([str(int(commit[0])) for commit in commitrows])
#This is poor practice, but we assured ourselves the value is composed only of ints first
c.execute("SELECT * from " + DB.commitfile._table + " WHERE commitid IN (" + allcommitids + ")")
commitfiles = c.fetchall()
commits = []
for i in commitrows:
r = Repo()
r.loadFromValues(i[DB.commit._numColumns + 0], i[DB.commit._numColumns + 1], i[DB.commit._numColumns + 2],
i[DB.commit._numColumns + 3], i[DB.commit._numColumns + 4], i[DB.commit._numColumns + 5])
files = [file[DB.commitfile.file] for file in commitfiles
if file[DB.commitfile.commitid] == i[DB.commit.id]]
c = Commit()
c.loadFromDatabase(r, i, files)
commits.append(c)
return commits
@staticmethod
def findByIDs(project, uniqueid):
conn = DB.getConn()
c = conn.cursor()
getcommitsSQL = "SELECT c.*, r.* " + \
"FROM " + DB.commit._table + " c " + \
"INNER JOIN " + DB.repo._table + " r " + \
" ON r.id = c.repoid "
whereClause = " 1=1 "
components = []
if project and uniqueid:
whereClause += "AND r.tagname = %s AND c.uniqueid = %s "
components = [project, uniqueid]
getcommitsSQL += "WHERE " + whereClause
getcommitsSQL += "ORDER BY c.date DESC "
DB.execute(c, getcommitsSQL, components)
commitrows = c.fetchall()
commitfiles = []
if commitrows:
allcommitids = ",".join([str(int(commit[0])) for commit in commitrows])
#This is poor practice, but we assured ourselves the value is composed only of ints first
DB.execute(c, "SELECT * from " + DB.commitfile._table + " WHERE commitid IN (" + allcommitids + ")")
commitfiles = c.fetchall()
commits = []
for i in commitrows:
r = Repo()
r.loadFromValues(i[DB.commit._numColumns + 0], i[DB.commit._numColumns + 1], i[DB.commit._numColumns + 2],
i[DB.commit._numColumns + 3], i[DB.commit._numColumns + 4], i[DB.commit._numColumns + 5])
files = [file[DB.commitfile.file] for file in commitfiles
if file[DB.commitfile.commitid] == i[DB.commit.id]]
c = Commit()
c.loadFromDatabase(r, i, files)
commits.append(c)
return commits