@@ -16,6 +16,8 @@ import (
1616 "code.gitea.io/gitea/models/unit"
1717 user_model "code.gitea.io/gitea/models/user"
1818 "code.gitea.io/gitea/modules/log"
19+
20+ "xorm.io/builder"
1921)
2022
2123// Init initialize model
@@ -24,7 +26,7 @@ func Init(ctx context.Context) error {
2426}
2527
2628type repoChecker struct {
27- querySQL func (ctx context.Context ) ([]map [ string ][] byte , error )
29+ querySQL func (ctx context.Context ) ([]int64 , error )
2830 correctSQL func (ctx context.Context , id int64 ) error
2931 desc string
3032}
@@ -35,8 +37,7 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) {
3537 log .Error ("Select %s: %v" , checker .desc , err )
3638 return
3739 }
38- for _ , result := range results {
39- id , _ := strconv .ParseInt (string (result ["id" ]), 10 , 64 )
40+ for _ , id := range results {
4041 select {
4142 case <- ctx .Done ():
4243 log .Warn ("CheckRepoStats: Cancelled before checking %s for with id=%d" , checker .desc , id )
@@ -51,21 +52,23 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) {
5152 }
5253}
5354
54- func StatsCorrectSQL (ctx context.Context , sql string , id int64 ) error {
55- _ , err := db .GetEngine (ctx ).Exec (sql , id , id )
55+ func StatsCorrectSQL (ctx context.Context , sql any , ids ... any ) error {
56+ args := []any {sql }
57+ args = append (args , ids ... )
58+ _ , err := db .GetEngine (ctx ).Exec (args ... )
5659 return err
5760}
5861
5962func repoStatsCorrectNumWatches (ctx context.Context , id int64 ) error {
60- return StatsCorrectSQL (ctx , "UPDATE `repository` SET num_watches=(SELECT COUNT(*) FROM `watch` WHERE repo_id=? AND mode<>2) WHERE id=?" , id )
63+ return StatsCorrectSQL (ctx , "UPDATE `repository` SET num_watches=(SELECT COUNT(*) FROM `watch` WHERE repo_id=? AND mode<>2) WHERE id=?" , id , id )
6164}
6265
6366func repoStatsCorrectNumStars (ctx context.Context , id int64 ) error {
64- return StatsCorrectSQL (ctx , "UPDATE `repository` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE repo_id=?) WHERE id=?" , id )
67+ return StatsCorrectSQL (ctx , "UPDATE `repository` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE repo_id=?) WHERE id=?" , id , id )
6568}
6669
6770func labelStatsCorrectNumIssues (ctx context.Context , id int64 ) error {
68- return StatsCorrectSQL (ctx , "UPDATE `label` SET num_issues=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=?) WHERE id=?" , id )
71+ return StatsCorrectSQL (ctx , "UPDATE `label` SET num_issues=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=?) WHERE id=?" , id , id )
6972}
7073
7174func labelStatsCorrectNumIssuesRepo (ctx context.Context , id int64 ) error {
@@ -102,11 +105,11 @@ func milestoneStatsCorrectNumIssuesRepo(ctx context.Context, id int64) error {
102105}
103106
104107func userStatsCorrectNumRepos (ctx context.Context , id int64 ) error {
105- return StatsCorrectSQL (ctx , "UPDATE `user` SET num_repos=(SELECT COUNT(*) FROM `repository` WHERE owner_id=?) WHERE id=?" , id )
108+ return StatsCorrectSQL (ctx , "UPDATE `user` SET num_repos=(SELECT COUNT(*) FROM `repository` WHERE owner_id=?) WHERE id=?" , id , id )
106109}
107110
108111func repoStatsCorrectIssueNumComments (ctx context.Context , id int64 ) error {
109- return StatsCorrectSQL (ctx , "UPDATE `issue` SET num_comments=(SELECT COUNT(*) FROM `comment` WHERE issue_id=? AND type=0) WHERE id=?" , id )
112+ return StatsCorrectSQL (ctx , issues_model . UpdateIssueNumCommentsBuilder ( id ) )
110113}
111114
112115func repoStatsCorrectNumIssues (ctx context.Context , id int64 ) error {
@@ -125,9 +128,12 @@ func repoStatsCorrectNumClosedPulls(ctx context.Context, id int64) error {
125128 return repo_model .UpdateRepoIssueNumbers (ctx , id , true , true )
126129}
127130
128- func statsQuery (args ... any ) func (context.Context ) ([]map [string ][]byte , error ) {
129- return func (ctx context.Context ) ([]map [string ][]byte , error ) {
130- return db .GetEngine (ctx ).Query (args ... )
131+ // statsQuery returns a function that queries the database for a list of IDs
132+ // sql could be a string or a *builder.Builder
133+ func statsQuery (sql any , args ... any ) func (context.Context ) ([]int64 , error ) {
134+ return func (ctx context.Context ) ([]int64 , error ) {
135+ var ids []int64
136+ return ids , db .GetEngine (ctx ).SQL (sql , args ... ).Find (& ids )
131137 }
132138}
133139
@@ -198,7 +204,16 @@ func CheckRepoStats(ctx context.Context) error {
198204 },
199205 // Issue.NumComments
200206 {
201- statsQuery ("SELECT `issue`.id FROM `issue` WHERE `issue`.num_comments!=(SELECT COUNT(*) FROM `comment` WHERE issue_id=`issue`.id AND type=0)" ),
207+ statsQuery (builder .Select ("`issue`.id" ).From ("`issue`" ).Where (
208+ builder.Neq {
209+ "`issue`.num_comments" : builder .Select ("COUNT(*)" ).From ("`comment`" ).Where (
210+ builder .Expr ("issue_id = `issue`.id" ).And (
211+ builder .In ("type" , issues_model .ConversationCountedCommentType ()),
212+ ),
213+ ),
214+ },
215+ ),
216+ ),
202217 repoStatsCorrectIssueNumComments ,
203218 "issue count 'num_comments'" ,
204219 },
0 commit comments