mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 21:45:50 +01:00
Replace native SQL which retrieves issue count with type safe DSL.
This commit is contained in:
17
src/main/scala/model/IssueLabels.scala
Normal file
17
src/main/scala/model/IssueLabels.scala
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import scala.slick.driver.H2Driver.simple._
|
||||||
|
|
||||||
|
object IssueLabels extends Table[IssueLabel]("ISSUE_LABEL") with Functions {
|
||||||
|
def userName = column[String]("USER_NAME", O PrimaryKey)
|
||||||
|
def repositoryName = column[String]("REPOSITORY_NAME", O PrimaryKey)
|
||||||
|
def issueId = column[Int]("ISSUE_ID", O PrimaryKey)
|
||||||
|
def labelId = column[Int]("LABEL_ID", O PrimaryKey)
|
||||||
|
def * = userName ~ repositoryName ~ issueId ~ labelId <> (IssueLabel, IssueLabel.unapply _)
|
||||||
|
}
|
||||||
|
|
||||||
|
case class IssueLabel(
|
||||||
|
userName: String,
|
||||||
|
repositoryName: String,
|
||||||
|
issueId: Int,
|
||||||
|
labelId: Int)
|
||||||
@@ -2,7 +2,7 @@ package service
|
|||||||
|
|
||||||
import scala.slick.driver.H2Driver.simple._
|
import scala.slick.driver.H2Driver.simple._
|
||||||
import Database.threadLocalSession
|
import Database.threadLocalSession
|
||||||
import scala.slick.jdbc.{StaticQuery => Q, GetResult}
|
import scala.slick.jdbc.{StaticQuery => Q}
|
||||||
import Q.interpolation
|
import Q.interpolation
|
||||||
|
|
||||||
import model._
|
import model._
|
||||||
@@ -54,30 +54,28 @@ trait IssuesService {
|
|||||||
def countIssueGroupByLabels(owner: String, repository: String, condition: IssueSearchCondition,
|
def countIssueGroupByLabels(owner: String, repository: String, condition: IssueSearchCondition,
|
||||||
filter: String, userName: Option[String]): Map[String, Int] = {
|
filter: String, userName: Option[String]): Map[String, Int] = {
|
||||||
|
|
||||||
case class LabelCount(labelName: String, count: Int)
|
Issues
|
||||||
implicit val getLabelCount = GetResult(r => LabelCount(r.<<, r.<<))
|
.innerJoin(IssueLabels).on { (t1, t2) =>
|
||||||
|
(t1.userName is t2.userName) && (t1.repositoryName is t2.repositoryName) && (t1.issueId is t2.issueId)
|
||||||
Q.query[(String, String, Boolean), LabelCount](
|
}
|
||||||
"""SELECT
|
.innerJoin(Labels).on { case ((t1, t2), t3) =>
|
||||||
T3.LABEL_NAME, COUNT(T1.ISSUE_ID)
|
(t2.userName is t3.userName) && (t2.repositoryName is t3.repositoryName) && (t2.labelId is t3.labelId)
|
||||||
FROM ISSUE T1
|
}
|
||||||
INNER JOIN ISSUE_LABEL T2 ON T1.ISSUE_ID = T2.ISSUE_ID
|
.filter { case ((t1, t2), t3) =>
|
||||||
INNER JOIN LABEL T3 ON T2.LABEL_ID = T3.LABEL_ID
|
(t1.userName is owner.bind) &&
|
||||||
WHERE
|
(t1.repositoryName is repository.bind) &&
|
||||||
T1.USER_NAME = ?
|
(t1.closed is (condition.state == "closed").bind) &&
|
||||||
AND T1.REPOSITORY_NAME = ?
|
(t1.milestoneId is condition.milestoneId.get.bind, condition.milestoneId.isDefined) &&
|
||||||
AND T1.CLOSED = ?
|
(t1.assignedUserName is userName.get.bind, filter == "assigned") &&
|
||||||
""" +
|
(t1.openedUserName is userName.get.bind, filter == "created_by")
|
||||||
condition.milestoneId.map(" AND T1.MILESTONE_ID = " + milestoneId).getOrElse("") +
|
}
|
||||||
(filter match {
|
.groupBy { case ((t1, t2), t3) =>
|
||||||
case "assigned" => " AND T1.ASSIGNED_USER_NAME = '%s'".format(userName.get)
|
t3.labelName
|
||||||
case "created_by" => " AND T1.OPENED_USER_NAME = '%s'".format(userName.get)
|
}
|
||||||
case _ => ""
|
.map { case (labelName, t) =>
|
||||||
}) +
|
labelName ~ t.length
|
||||||
" GROUP BY T3.LABEL_NAME")
|
}
|
||||||
.list(owner, repository, condition.state == "closed")
|
.list.toMap
|
||||||
.map(x => x.labelName -> x.count)
|
|
||||||
.toMap
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user