@@ -19,6 +19,8 @@ import (
1919 "code.gitea.io/gitea/models/unit"
2020 user_model "code.gitea.io/gitea/models/user"
2121 "code.gitea.io/gitea/modules/log"
22+
23+ "xorm.io/builder"
2224)
2325
2426// Init initialize model
@@ -27,7 +29,7 @@ func Init(ctx context.Context) error {
2729}
2830
2931type repoChecker struct {
30- querySQL func (ctx context.Context ) ([]map [ string ][] byte , error )
32+ querySQL func (ctx context.Context ) ([]int64 , error )
3133 correctSQL func (ctx context.Context , id int64 ) error
3234 desc string
3335}
@@ -38,8 +40,7 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) {
3840 log .Error ("Select %s: %v" , checker .desc , err )
3941 return
4042 }
41- for _ , result := range results {
42- id , _ := strconv .ParseInt (string (result ["id" ]), 10 , 64 )
43+ for _ , id := range results {
4344 select {
4445 case <- ctx .Done ():
4546 log .Warn ("CheckRepoStats: Cancelled before checking %s for with id=%d" , checker .desc , id )
@@ -54,21 +55,23 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) {
5455 }
5556}
5657
57- func StatsCorrectSQL (ctx context.Context , sql string , id int64 ) error {
58- _ , err := db .GetEngine (ctx ).Exec (sql , id , id )
58+ func StatsCorrectSQL (ctx context.Context , sql any , ids ... any ) error {
59+ args := []any {sql }
60+ args = append (args , ids ... )
61+ _ , err := db .GetEngine (ctx ).Exec (args ... )
5962 return err
6063}
6164
6265func repoStatsCorrectNumWatches (ctx context.Context , id int64 ) error {
63- return StatsCorrectSQL (ctx , "UPDATE `repository` SET num_watches=(SELECT COUNT(*) FROM `watch` WHERE repo_id=? AND mode<>2) WHERE id=?" , id )
66+ return StatsCorrectSQL (ctx , "UPDATE `repository` SET num_watches=(SELECT COUNT(*) FROM `watch` WHERE repo_id=? AND mode<>2) WHERE id=?" , id , id )
6467}
6568
6669func repoStatsCorrectNumStars (ctx context.Context , id int64 ) error {
67- return StatsCorrectSQL (ctx , "UPDATE `repository` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE repo_id=?) WHERE id=?" , id )
70+ return StatsCorrectSQL (ctx , "UPDATE `repository` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE repo_id=?) WHERE id=?" , id , id )
6871}
6972
7073func labelStatsCorrectNumIssues (ctx context.Context , id int64 ) error {
71- return StatsCorrectSQL (ctx , "UPDATE `label` SET num_issues=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=?) WHERE id=?" , id )
74+ return StatsCorrectSQL (ctx , "UPDATE `label` SET num_issues=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=?) WHERE id=?" , id , id )
7275}
7376
7477func labelStatsCorrectNumIssuesRepo (ctx context.Context , id int64 ) error {
@@ -105,11 +108,11 @@ func milestoneStatsCorrectNumIssuesRepo(ctx context.Context, id int64) error {
105108}
106109
107110func userStatsCorrectNumRepos (ctx context.Context , id int64 ) error {
108- return StatsCorrectSQL (ctx , "UPDATE `user` SET num_repos=(SELECT COUNT(*) FROM `repository` WHERE owner_id=?) WHERE id=?" , id )
111+ return StatsCorrectSQL (ctx , "UPDATE `user` SET num_repos=(SELECT COUNT(*) FROM `repository` WHERE owner_id=?) WHERE id=?" , id , id )
109112}
110113
111114func repoStatsCorrectIssueNumComments (ctx context.Context , id int64 ) error {
112- return StatsCorrectSQL (ctx , "UPDATE `issue` SET num_comments=(SELECT COUNT(*) FROM `comment` WHERE issue_id=? AND type=0) WHERE id=?" , id )
115+ return StatsCorrectSQL (ctx , issues_model . UpdateIssueNumCommentsBuilder ( id ) )
113116}
114117
115118func repoStatsCorrectNumIssues (ctx context.Context , id int64 ) error {
@@ -128,9 +131,12 @@ func repoStatsCorrectNumClosedPulls(ctx context.Context, id int64) error {
128131 return repo_model .UpdateRepoIssueNumbers (ctx , id , true , true )
129132}
130133
131- func statsQuery (args ... any ) func (context.Context ) ([]map [string ][]byte , error ) {
132- return func (ctx context.Context ) ([]map [string ][]byte , error ) {
133- return db .GetEngine (ctx ).Query (args ... )
134+ // statsQuery returns a function that queries the database for a list of IDs
135+ // sql could be a string or a *builder.Builder
136+ func statsQuery (sql any , args ... any ) func (context.Context ) ([]int64 , error ) {
137+ return func (ctx context.Context ) ([]int64 , error ) {
138+ var ids []int64
139+ return ids , db .GetEngine (ctx ).SQL (sql , args ... ).Find (& ids )
134140 }
135141}
136142
@@ -201,7 +207,16 @@ func CheckRepoStats(ctx context.Context) error {
201207 },
202208 // Issue.NumComments
203209 {
204- statsQuery ("SELECT `issue`.id FROM `issue` WHERE `issue`.num_comments!=(SELECT COUNT(*) FROM `comment` WHERE issue_id=`issue`.id AND type=0)" ),
210+ statsQuery (builder .Select ("`issue`.id" ).From ("`issue`" ).Where (
211+ builder.Neq {
212+ "`issue`.num_comments" : builder .Select ("COUNT(*)" ).From ("`comment`" ).Where (
213+ builder .Expr ("issue_id = `issue`.id" ).And (
214+ builder .In ("type" , issues_model .ConversationCountedCommentType ()),
215+ ),
216+ ),
217+ },
218+ ),
219+ ),
205220 repoStatsCorrectIssueNumComments ,
206221 "issue count 'num_comments'" ,
207222 },
0 commit comments