Skip to content

Commit 284b0e1

Browse files
authored
fix code review on mssql (#5502)
1 parent 8a64e67 commit 284b0e1

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

models/review.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"code.gitea.io/gitea/modules/log"
1111
"code.gitea.io/gitea/modules/util"
12+
"github.com/go-xorm/core"
1213
"github.com/go-xorm/xorm"
1314

1415
"github.com/go-xorm/builder"
@@ -266,13 +267,24 @@ type PullReviewersWithType struct {
266267
// GetReviewersByPullID gets all reviewers for a pull request with the statuses
267268
func GetReviewersByPullID(pullID int64) (issueReviewers []*PullReviewersWithType, err error) {
268269
irs := []*PullReviewersWithType{}
269-
err = x.Select("`user`.*, review.type, max(review.updated_unix) as review_updated_unix").
270-
Table("review").
271-
Join("INNER", "`user`", "review.reviewer_id = `user`.id").
272-
Where("review.issue_id = ? AND (review.type = ? OR review.type = ?)", pullID, ReviewTypeApprove, ReviewTypeReject).
273-
GroupBy("`user`.id, review.type").
274-
OrderBy("review_updated_unix DESC").
275-
Find(&irs)
270+
if x.Dialect().DBType() == core.MSSQL {
271+
err = x.SQL(`SELECT [user].*, review.type, review.review_updated_unix FROM
272+
(SELECT review.id, review.type, review.reviewer_id, max(review.updated_unix) as review_updated_unix
273+
FROM review WHERE review.issue_id=? AND (review.type = ? OR review.type = ?)
274+
GROUP BY review.id, review.type, review.reviewer_id) as review
275+
INNER JOIN [user] ON review.reviewer_id = [user].id ORDER BY review_updated_unix DESC`,
276+
pullID, ReviewTypeApprove, ReviewTypeReject).
277+
Find(&irs)
278+
} else {
279+
err = x.Select("`user`.*, review.type, max(review.updated_unix) as review_updated_unix").
280+
Table("review").
281+
Join("INNER", "`user`", "review.reviewer_id = `user`.id").
282+
Where("review.issue_id = ? AND (review.type = ? OR review.type = ?)",
283+
pullID, ReviewTypeApprove, ReviewTypeReject).
284+
GroupBy("`user`.id, review.type").
285+
OrderBy("review_updated_unix DESC").
286+
Find(&irs)
287+
}
276288

277289
// We need to group our results by user id _and_ review type, otherwise the query fails when using postgresql.
278290
// But becaus we're doing this, we need to manually filter out multiple reviews of different types by the

0 commit comments

Comments
 (0)