| 1 |
# |
|---|
| 2 |
# Copyright (C) 2005-2006 Team5 |
|---|
| 3 |
# All rights reserved. |
|---|
| 4 |
# |
|---|
| 5 |
# This software is licensed as described in the file COPYING.txt, which |
|---|
| 6 |
# you should have received as part of this distribution. |
|---|
| 7 |
# |
|---|
| 8 |
# Author: Team5 |
|---|
| 9 |
# |
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
from codereview.CodeReviewStruct import * |
|---|
| 13 |
from codereview.ReviewerStruct import * |
|---|
| 14 |
from codereview.ReviewFilesStruct import * |
|---|
| 15 |
from codereview.ReviewCommentStruct import * |
|---|
| 16 |
import string |
|---|
| 17 |
|
|---|
| 18 |
class dbBackend(object): |
|---|
| 19 |
db = None |
|---|
| 20 |
|
|---|
| 21 |
def __init__(self, tdb): |
|---|
| 22 |
self.db = tdb |
|---|
| 23 |
|
|---|
| 24 |
#Creates a set of SQL ORs from a string of keywords |
|---|
| 25 |
def createORLoop(self, keyword, colName): |
|---|
| 26 |
array = keyword.split() |
|---|
| 27 |
newStr = "" |
|---|
| 28 |
for str in array: |
|---|
| 29 |
if len(newStr) != 0: |
|---|
| 30 |
newStr = newStr + "OR " |
|---|
| 31 |
newStr = newStr + colName + " LIKE '%s%s%s' " % ('%', str, '%') |
|---|
| 32 |
return newStr |
|---|
| 33 |
|
|---|
| 34 |
#Returns an array of all the code reviews whose author is the given user |
|---|
| 35 |
def getMyCodeReviews(self, user): |
|---|
| 36 |
query = "SELECT IDReview, Author, Status, DateCreate, Name, Notes FROM CodeReviews WHERE Author = '%s' ORDER BY DateCreate" % (user) |
|---|
| 37 |
return self.execCodeReviewQuery(query, False) |
|---|
| 38 |
|
|---|
| 39 |
#Returns an array of all the code reviews who have the given user assigned to them as a reviewer |
|---|
| 40 |
def getCodeReviews(self, user): |
|---|
| 41 |
query = "SELECT cr.IDReview, cr.Author, cr.Status, cr.DateCreate, cr.Name, cr.Notes FROM CodeReviews cr, Reviewers r WHERE r.IDReview = cr.IDReview AND r.Reviewer = '%s' ORDER BY cr.DateCreate" % (user) |
|---|
| 42 |
return self.execCodeReviewQuery(query, False) |
|---|
| 43 |
|
|---|
| 44 |
#Returns an array of all the code reviews with the given status |
|---|
| 45 |
def getCodeReviewsByStatus(self, status): |
|---|
| 46 |
query = "SELECT IDReview, Author, Status, DateCreate, Name, Notes FROM CodeReviews WHERE Status = '%s' ORDER BY DateCreate" % (status) |
|---|
| 47 |
return self.execCodeReviewQuery(query, False) |
|---|
| 48 |
|
|---|
| 49 |
#Returns the number of votes of type 'type' for the given code review |
|---|
| 50 |
def getVotesByID(self, type, id): |
|---|
| 51 |
query = "SELECT Count(Reviewer) FROM Reviewers WHERE IDReview = '%s' AND Vote = '%s'" % (id, type) |
|---|
| 52 |
cursor = self.db.cursor() |
|---|
| 53 |
cursor.execute(query) |
|---|
| 54 |
row = cursor.fetchone() |
|---|
| 55 |
if not row: |
|---|
| 56 |
return 0 |
|---|
| 57 |
return row[0] |
|---|
| 58 |
|
|---|
| 59 |
#Returns the code review requested by ID |
|---|
| 60 |
def getCodeReviewsByID(self, id): |
|---|
| 61 |
query = "SELECT IDReview, Author, Status, DateCreate, Name, Notes FROM CodeReviews WHERE IDReview = '%s'" % (id) |
|---|
| 62 |
return self.execCodeReviewQuery(query, True) |
|---|
| 63 |
|
|---|
| 64 |
#Returns an array of code reviews which have a name like any of the |
|---|
| 65 |
#names given in the 'name' string |
|---|
| 66 |
def searchCodeReviewsByName(self, name): |
|---|
| 67 |
queryPart = self.createORLoop(name, "Name") |
|---|
| 68 |
if len(queryPart) == 0: |
|---|
| 69 |
query = "SELECT IDReview, Author, Status, DateCreate, Name, Notes FROM CodeReviews" |
|---|
| 70 |
else: |
|---|
| 71 |
query = "SELECT IDReview, Author, Status, DateCreate, Name, Notes FROM CodeReviews WHERE %s" % (queryPart) |
|---|
| 72 |
return self.execCodeReviewQuery(query, True) |
|---|
| 73 |
|
|---|
| 74 |
#Returns an array of code reviews that match the values in the given |
|---|
| 75 |
#code review structure. The 'name' part is treated as a keyword list |
|---|
| 76 |
def searchCodeReviews(self, crStruct): |
|---|
| 77 |
query = "SELECT IDReview, Author, Status, DateCreate, Name, Notes FROM CodeReviews WHERE " |
|---|
| 78 |
queryPart = self.createORLoop(crStruct.Name, "Name") |
|---|
| 79 |
if len(queryPart) != 0: |
|---|
| 80 |
query = query + "(%s) AND " % (queryPart) |
|---|
| 81 |
query = query + "Author LIKE '%s%s%s' AND Status LIKE '%s%s%s' AND DateCreate >= '%s'" % ('%', crStruct.Author, '%', '%', crStruct.Status, '%', crStruct.DateCreate) |
|---|
| 82 |
return self.execCodeReviewQuery(query, False) |
|---|
| 83 |
|
|---|
| 84 |
#Returns an array of all the reviewers for a code review |
|---|
| 85 |
def getReviewers(self, id): |
|---|
| 86 |
query = "SELECT IDReview, Reviewer, Status, Vote FROM Reviewers WHERE IDReview = '%s'" % (id) |
|---|
| 87 |
return self.execReviewerQuery(query, False) |
|---|
| 88 |
|
|---|
| 89 |
#Returns a specific reviewer entry for the given code review and name |
|---|
| 90 |
def getReviewerEntry(self, id, name): |
|---|
| 91 |
query = "SELECT IDReview, Reviewer, Status, Vote FROM Reviewers WHERE IDReview = '%s' AND Reviewer = '%s'" % (id, name) |
|---|
| 92 |
return self.execReviewerQuery(query, True) |
|---|
| 93 |
|
|---|
| 94 |
#Returns an array of the files associated with the given review id |
|---|
| 95 |
def getReviewFiles(self, id): |
|---|
| 96 |
query = "SELECT IDFile, IDReview, Path, LineStart, LineEnd, Version FROM ReviewFiles WHERE IDReview = '%s'" % (id) |
|---|
| 97 |
return self.execReviewFileQuery(query, False) |
|---|
| 98 |
|
|---|
| 99 |
#Returns the requested review file |
|---|
| 100 |
def getReviewFile(self, id): |
|---|
| 101 |
query = "SELECT IDFile, IDReview, Path, LineStart, LineEnd, Version FROM ReviewFiles WHERE IDFile = '%s'" % (id) |
|---|
| 102 |
return self.execReviewFileQuery(query, True) |
|---|
| 103 |
|
|---|
| 104 |
#Returns the requested comment |
|---|
| 105 |
def getCommentByID(self, id): |
|---|
| 106 |
query = "SELECT IDComment, IDFile, IDParent, LineNum, Author, Text, AttachmentPath, DateCreate FROM ReviewComments WHERE IDComment = '%s'" % (id) |
|---|
| 107 |
return self.execReviewCommentQuery(query, True) |
|---|
| 108 |
|
|---|
| 109 |
#Returns an array of comments for the given file |
|---|
| 110 |
def getCommentsByFileID(self, id): |
|---|
| 111 |
query = "SELECT IDComment, IDFile, IDParent, LineNum, Author, Text, AttachmentPath, DateCreate FROM ReviewComments WHERE IDFile = '%s' ORDER BY DateCreate" % (id) |
|---|
| 112 |
return self.execReviewCommentQuery(query, False) |
|---|
| 113 |
|
|---|
| 114 |
#Returns all the comments for the given file on the given line |
|---|
| 115 |
def getCommentsByFileIDAndLine(self, id, line): |
|---|
| 116 |
query = "SELECT IDComment, IDFile, IDParent, LineNum, Author, Text, AttachmentPath, DateCreate FROM ReviewComments WHERE IDFile = '%s' AND LineNum = '%s' ORDER BY DateCreate" % (id, line) |
|---|
| 117 |
return self.execReviewCommentQuery(query, False) |
|---|
| 118 |
|
|---|
| 119 |
#Returns the current "Threshold" for code to be ready for inclusion |
|---|
| 120 |
def getThreshold(self): |
|---|
| 121 |
query = "SELECT value FROM system WHERE name = 'CodeReviewVoteThreshold'" |
|---|
| 122 |
cursor = self.db.cursor() |
|---|
| 123 |
cursor.execute(query) |
|---|
| 124 |
row = cursor.fetchone() |
|---|
| 125 |
if not row: |
|---|
| 126 |
return 0 |
|---|
| 127 |
numVal = 0 |
|---|
| 128 |
try: |
|---|
| 129 |
numVal = string.atoi(row[0]) |
|---|
| 130 |
except: |
|---|
| 131 |
return 0 |
|---|
| 132 |
return numVal |
|---|
| 133 |
|
|---|
| 134 |
#Sets the "Threshold" to the given value |
|---|
| 135 |
def setThreshold(self, val): |
|---|
| 136 |
query = "UPDATE system SET value = '%s' WHERE name = 'CodeReviewVoteThreshold'" % (val) |
|---|
| 137 |
cursor = self.db.cursor() |
|---|
| 138 |
cursor.execute(query) |
|---|
| 139 |
self.db.commit() |
|---|
| 140 |
|
|---|
| 141 |
#Returns a dictionary where the key is the line number and the value is the number of comments on that line |
|---|
| 142 |
#for the given file id. |
|---|
| 143 |
def getCommentDictForFile(self, id): |
|---|
| 144 |
query = "SELECT LineNum, Count(IDComment) FROM ReviewComments WHERE IDFile = '%s' GROUP BY LineNum" % (id) |
|---|
| 145 |
cursor = self.db.cursor() |
|---|
| 146 |
cursor.execute(query) |
|---|
| 147 |
rows = cursor.fetchall() |
|---|
| 148 |
d = {} |
|---|
| 149 |
if not rows: |
|---|
| 150 |
return d |
|---|
| 151 |
for row in rows: |
|---|
| 152 |
d[row[0]] = row[1] |
|---|
| 153 |
return d |
|---|
| 154 |
|
|---|
| 155 |
#Returns all the possible users who can review a code review |
|---|
| 156 |
def getPossibleUsers(self): |
|---|
| 157 |
cursor = self.db.cursor() |
|---|
| 158 |
cursor.execute("SELECT DISTINCT p1.username as username FROM permission p1 left join permission p2 on p1.action = p2.username WHERE p1.action = 'CODE_REVIEW_DEV' OR p2.action = 'CODE_REVIEW_DEV' OR p1.action = 'CODE_REVIEW_MGR' OR p2.action = 'CODE_REVIEW_MGR'") |
|---|
| 159 |
rows = cursor.fetchall() |
|---|
| 160 |
if not rows: |
|---|
| 161 |
return [] |
|---|
| 162 |
|
|---|
| 163 |
users = [] |
|---|
| 164 |
for row in rows: |
|---|
| 165 |
users.append(row[0]) |
|---|
| 166 |
return users |
|---|
| 167 |
|
|---|
| 168 |
#A generic method for executing queries that return CodeReview structures |
|---|
| 169 |
#query: the query to execute |
|---|
| 170 |
#single: true if this query will always return only one result, false otherwise |
|---|
| 171 |
def execCodeReviewQuery(self, query, single): |
|---|
| 172 |
cursor = self.db.cursor() |
|---|
| 173 |
cursor.execute(query) |
|---|
| 174 |
if single: |
|---|
| 175 |
row = cursor.fetchone() |
|---|
| 176 |
if not row: |
|---|
| 177 |
return None |
|---|
| 178 |
return CodeReviewStruct(row) |
|---|
| 179 |
|
|---|
| 180 |
rows = cursor.fetchall() |
|---|
| 181 |
if not rows: |
|---|
| 182 |
return [] |
|---|
| 183 |
|
|---|
| 184 |
codeReviews = [] |
|---|
| 185 |
for row in rows: |
|---|
| 186 |
codeReviews.append(CodeReviewStruct(row)) |
|---|
| 187 |
return codeReviews |
|---|
| 188 |
|
|---|
| 189 |
#A generic method for executing queries that return Reviewer structures |
|---|
| 190 |
#query: the query to execute |
|---|
| 191 |
#single: true if this query will always return only one result, false otherwise |
|---|
| 192 |
def execReviewerQuery(self, query, single): |
|---|
| 193 |
cursor = self.db.cursor() |
|---|
| 194 |
cursor.execute(query) |
|---|
| 195 |
if single: |
|---|
| 196 |
row = cursor.fetchone() |
|---|
| 197 |
if not row: |
|---|
| 198 |
return None |
|---|
| 199 |
return ReviewerStruct(row) |
|---|
| 200 |
|
|---|
| 201 |
rows = cursor.fetchall() |
|---|
| 202 |
if not rows: |
|---|
| 203 |
return [] |
|---|
| 204 |
|
|---|
| 205 |
reviewers = [] |
|---|
| 206 |
for row in rows: |
|---|
| 207 |
reviewers.append(ReviewerStruct(row)) |
|---|
| 208 |
return reviewers |
|---|
| 209 |
|
|---|
| 210 |
#A generic method for executing queries that return Comment structures |
|---|
| 211 |
#query: the query to execute |
|---|
| 212 |
#single: true if this query will always return only one result, false otherwise |
|---|
| 213 |
def execReviewCommentQuery(self, query, single): |
|---|
| 214 |
cursor = self.db.cursor() |
|---|
| 215 |
cursor.execute(query) |
|---|
| 216 |
if single: |
|---|
| 217 |
row = cursor.fetchone() |
|---|
| 218 |
if not row: |
|---|
| 219 |
return None |
|---|
| 220 |
return ReviewCommentStruct(row) |
|---|
| 221 |
|
|---|
| 222 |
rows = cursor.fetchall() |
|---|
| 223 |
if not rows: |
|---|
| 224 |
return {} |
|---|
| 225 |
|
|---|
| 226 |
comments = {} |
|---|
| 227 |
for row in rows: |
|---|
| 228 |
comment = ReviewCommentStruct(row) |
|---|
| 229 |
if comment.IDComment != "-1": |
|---|
| 230 |
comments[comment.IDComment] = comment |
|---|
| 231 |
|
|---|
| 232 |
for key in comments.keys(): |
|---|
| 233 |
comment = comments[key] |
|---|
| 234 |
if comment.IDParent != "-1" and comments.has_key(comment.IDParent) and comment.IDParent != comment.IDComment: |
|---|
| 235 |
comments[comment.IDParent].Children[comment.IDComment] = comment |
|---|
| 236 |
|
|---|
| 237 |
return comments |
|---|
| 238 |
|
|---|
| 239 |
#A generic method for executing queries that return File structures |
|---|
| 240 |
#query: the query to execute |
|---|
| 241 |
#single: true if this query will always return only one result, false otherwise |
|---|
| 242 |
def execReviewFileQuery(self, query, single): |
|---|
| 243 |
cursor = self.db.cursor() |
|---|
| 244 |
cursor.execute(query) |
|---|
| 245 |
if single: |
|---|
| 246 |
row = cursor.fetchone() |
|---|
| 247 |
if not row: |
|---|
| 248 |
return None |
|---|
| 249 |
return ReviewFileStruct(row) |
|---|
| 250 |
|
|---|
| 251 |
rows = cursor.fetchall() |
|---|
| 252 |
if not rows: |
|---|
| 253 |
return [] |
|---|
| 254 |
|
|---|
| 255 |
files = [] |
|---|
| 256 |
for row in rows: |
|---|
| 257 |
files.append(ReviewFileStruct(row)) |
|---|
| 258 |
return files |
|---|