@@ -6,7 +6,6 @@ package models
66
77import (
88 "context"
9- "fmt"
109 "strconv"
1110
1211 _ "image/jpeg" // Needed for jpeg support
@@ -17,6 +16,7 @@ import (
1716 "code.gitea.io/gitea/models/unit"
1817 user_model "code.gitea.io/gitea/models/user"
1918 "code.gitea.io/gitea/modules/log"
19+ "xorm.io/builder"
2020)
2121
2222// Init initialize model
@@ -25,7 +25,7 @@ func Init(ctx context.Context) error {
2525}
2626
2727type repoChecker struct {
28- querySQL func (ctx context.Context ) ([]map [ string ][] byte , error )
28+ querySQL func (ctx context.Context ) ([]int64 , error )
2929 correctSQL func (ctx context.Context , id int64 ) error
3030 desc string
3131}
@@ -36,8 +36,7 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) {
3636 log .Error ("Select %s: %v" , checker .desc , err )
3737 return
3838 }
39- for _ , result := range results {
40- id , _ := strconv .ParseInt (string (result ["id" ]), 10 , 64 )
39+ for _ , id := range results {
4140 select {
4241 case <- ctx .Done ():
4342 log .Warn ("CheckRepoStats: Cancelled before checking %s for with id=%d" , checker .desc , id )
@@ -52,8 +51,10 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) {
5251 }
5352}
5453
55- func StatsCorrectSQL (ctx context.Context , sql string , id int64 ) error {
56- _ , err := db .GetEngine (ctx ).Exec (sql , id , id )
54+ func StatsCorrectSQL (ctx context.Context , sql any , ids ... any ) error {
55+ args := []any {sql }
56+ args = append (args , ids ... )
57+ _ , err := db .GetEngine (ctx ).Exec (args ... )
5758 return err
5859}
5960
@@ -107,8 +108,14 @@ func userStatsCorrectNumRepos(ctx context.Context, id int64) error {
107108}
108109
109110func repoStatsCorrectIssueNumComments (ctx context.Context , id int64 ) error {
110- return StatsCorrectSQL (ctx , fmt .Sprintf ("UPDATE `issue` SET num_comments=(SELECT COUNT(*) FROM `comment` WHERE issue_id=? AND (type=%d or type=%d)) WHERE id=?" ,
111- issues_model .ConversationCountedCommentType ()... ), id )
111+ return StatsCorrectSQL (ctx ,
112+ builder .Update (builder.Eq {
113+ "num_comments" : builder .Select ("COUNT(*)" ).From ("`comment`" ).Where (
114+ builder.Eq {"issue_id" : id }.And (
115+ builder .In ("type" , issues_model .ConversationCountedCommentType ()),
116+ )),
117+ }).From ("`issue`" ).Where (builder.Eq {"id" : id }),
118+ )
112119}
113120
114121func repoStatsCorrectNumIssues (ctx context.Context , id int64 ) error {
@@ -127,9 +134,12 @@ func repoStatsCorrectNumClosedPulls(ctx context.Context, id int64) error {
127134 return repo_model .UpdateRepoIssueNumbers (ctx , id , true , true )
128135}
129136
130- func statsQuery (sql string , args ... any ) func (context.Context ) ([]map [string ][]byte , error ) {
131- return func (ctx context.Context ) ([]map [string ][]byte , error ) {
132- return db .GetEngine (ctx ).Query (append ([]any {sql }, args ... )... )
137+ // statsQuery returns a function that queries the database for a list of IDs
138+ // sql could be a string or a *builder.Builder
139+ func statsQuery (sql any , args ... any ) func (context.Context ) ([]int64 , error ) {
140+ return func (ctx context.Context ) ([]int64 , error ) {
141+ var ids []int64
142+ return ids , db .GetEngine (ctx ).SQL (sql , args ... ).Find (& ids )
133143 }
134144}
135145
@@ -200,8 +210,16 @@ func CheckRepoStats(ctx context.Context) error {
200210 },
201211 // Issue.NumComments
202212 {
203- statsQuery ("SELECT `issue`.id FROM `issue` WHERE `issue`.num_comments!=(SELECT COUNT(*) FROM `comment` WHERE issue_id=`issue`.id AND (type=? OR type=?))" ,
204- issues_model .ConversationCountedCommentType ()... ),
213+ statsQuery (builder .Select ("`issue`.id" ).From ("`issue`" ).Where (
214+ builder.Neq {
215+ "`issue`.num_comments" : builder .Select ("COUNT(*)" ).From ("`comment`" ).Where (
216+ builder .Expr ("issue_id = `issue`.id" ).And (
217+ builder .In ("type" , issues_model .ConversationCountedCommentType ()),
218+ ),
219+ ),
220+ },
221+ ),
222+ ),
205223 repoStatsCorrectIssueNumComments ,
206224 "issue count 'num_comments'" ,
207225 },
0 commit comments