| 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 | import string |
|---|
| 12 | |
|---|
| 13 | from CodeReviewStruct import * |
|---|
| 14 | from ReviewCommentStruct import * |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | class dbBackend(object): |
|---|
| 18 | db = None |
|---|
| 19 | |
|---|
| 20 | def __init__(self, tdb): |
|---|
| 21 | self.db = tdb |
|---|
| 22 | |
|---|
| 23 | #Creates a set of SQL ORs from a string of keywords |
|---|
| 24 | def createORLoop(self, keyword, colName): |
|---|
| 25 | array = keyword.split() |
|---|
| 26 | newStr = "" |
|---|
| 27 | for str in array: |
|---|
| 28 | if len(newStr) != 0: |
|---|
| 29 | newStr = newStr + "OR " |
|---|
| 30 | newStr = newStr + colName + " LIKE '%s%s%s' " % ('%', str, '%') |
|---|
| 31 | return newStr |
|---|
| 32 | |
|---|
| 33 | #Returns an array of code reviews which have a namwe like any of the |
|---|
| 34 | #names given in the 'name' string |
|---|
| 35 | def searchCodeReviewsByName(self, name): |
|---|
| 36 | queryPart = self.createORLoop(name, "Name") |
|---|
| 37 | if len(queryPart) == 0: |
|---|
| 38 | query = "SELECT review_id, owner, status, created, name, notes, closed FROM peerreview" |
|---|
| 39 | else: |
|---|
| 40 | query = "SELECT review_id, owner, status, created, name, notes, closed FROM peerreview WHERE %s" % (queryPart) |
|---|
| 41 | return self.execCodeReviewQuery(query, True) |
|---|
| 42 | |
|---|
| 43 | #Returns an array of code reviews that match the values in the given |
|---|
| 44 | #code review structure. The 'name' part is treated as a keyword list |
|---|
| 45 | def searchCodeReviews(self, crStruct): |
|---|
| 46 | query = "SELECT review_id, owner, status, created, name, notes, closed FROM peerreview WHERE " |
|---|
| 47 | queryPart = self.createORLoop(crStruct.Name, "name") |
|---|
| 48 | if len(queryPart) != 0: |
|---|
| 49 | query = query + "(%s) AND " % (queryPart) |
|---|
| 50 | query = query + "owner LIKE '%s%s%s' AND status LIKE '%s%s%s' AND created >= '%s'" % \ |
|---|
| 51 | ('%', crStruct.Author, '%', '%', crStruct.Status, '%', crStruct.DateCreate) |
|---|
| 52 | return self.execCodeReviewQuery(query, False) |
|---|
| 53 | |
|---|
| 54 | #Returns the requested comment |
|---|
| 55 | def getCommentByID(self, id): |
|---|
| 56 | query = "SELECT comment_id, file_id, parent_id, line_num, author, comment, attachment_path, created " \ |
|---|
| 57 | "FROM peerreviewcomment WHERE comment_id = '%s'" % (id) |
|---|
| 58 | return self.execReviewCommentQuery(query, True) |
|---|
| 59 | |
|---|
| 60 | #Returns all the comments for the given file on the given line |
|---|
| 61 | def getCommentsByFileIDAndLine(self, id, line): |
|---|
| 62 | query = "SELECT comment_id, file_id, parent_id, line_num, author, comment, attachment_path, created " \ |
|---|
| 63 | "FROM peerreviewcomment WHERE file_id = '%s' AND line_num = '%s' ORDER BY created" % (id, line) |
|---|
| 64 | return self.execReviewCommentQuery(query, False) |
|---|
| 65 | |
|---|
| 66 | #A generic method for executing queries that return CodeReview structures |
|---|
| 67 | #query: the query to execute |
|---|
| 68 | #single: true if this query will always return only one result, false otherwise |
|---|
| 69 | def execCodeReviewQuery(self, query, single): |
|---|
| 70 | cursor = self.db.cursor() |
|---|
| 71 | cursor.execute(query) |
|---|
| 72 | if single: |
|---|
| 73 | row = cursor.fetchone() |
|---|
| 74 | if not row: |
|---|
| 75 | return None |
|---|
| 76 | return CodeReviewStruct(row) |
|---|
| 77 | |
|---|
| 78 | rows = cursor.fetchall() |
|---|
| 79 | if not rows: |
|---|
| 80 | return [] |
|---|
| 81 | |
|---|
| 82 | codeReviews = [] |
|---|
| 83 | for row in rows: |
|---|
| 84 | codeReviews.append(CodeReviewStruct(row)) |
|---|
| 85 | return codeReviews |
|---|
| 86 | |
|---|
| 87 | #A generic method for executing queries that return Comment structures |
|---|
| 88 | #query: the query to execute |
|---|
| 89 | #single: true if this query will always return only one result, false otherwise |
|---|
| 90 | def execReviewCommentQuery(self, query, single): |
|---|
| 91 | cursor = self.db.cursor() |
|---|
| 92 | cursor.execute(query) |
|---|
| 93 | if single: |
|---|
| 94 | row = cursor.fetchone() |
|---|
| 95 | if not row: |
|---|
| 96 | return None |
|---|
| 97 | return ReviewCommentStruct(row) |
|---|
| 98 | |
|---|
| 99 | rows = cursor.fetchall() |
|---|
| 100 | if not rows: |
|---|
| 101 | return {} |
|---|
| 102 | |
|---|
| 103 | comments = {} |
|---|
| 104 | for row in rows: |
|---|
| 105 | comment = ReviewCommentStruct(row) |
|---|
| 106 | if comment.IDComment != "-1": |
|---|
| 107 | comments[comment.IDComment] = comment |
|---|
| 108 | |
|---|
| 109 | for key in comments.keys(): |
|---|
| 110 | comment = comments[key] |
|---|
| 111 | if comment.IDParent != "-1" and comment.IDParent in comments and comment.IDParent != comment.IDComment: |
|---|
| 112 | comments[comment.IDParent].Children[comment.IDComment] = comment |
|---|
| 113 | |
|---|
| 114 | return comments |
|---|