@@ -19,6 +19,8 @@ import (
19
19
"code.gitea.io/gitea/models/unit"
20
20
user_model "code.gitea.io/gitea/models/user"
21
21
"code.gitea.io/gitea/modules/log"
22
+
23
+ "xorm.io/builder"
22
24
)
23
25
24
26
// Init initialize model
@@ -27,7 +29,7 @@ func Init(ctx context.Context) error {
27
29
}
28
30
29
31
type repoChecker struct {
30
- querySQL func (ctx context.Context ) ([]map [ string ][] byte , error )
32
+ querySQL func (ctx context.Context ) ([]int64 , error )
31
33
correctSQL func (ctx context.Context , id int64 ) error
32
34
desc string
33
35
}
@@ -38,8 +40,7 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) {
38
40
log .Error ("Select %s: %v" , checker .desc , err )
39
41
return
40
42
}
41
- for _ , result := range results {
42
- id , _ := strconv .ParseInt (string (result ["id" ]), 10 , 64 )
43
+ for _ , id := range results {
43
44
select {
44
45
case <- ctx .Done ():
45
46
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) {
54
55
}
55
56
}
56
57
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 ... )
59
62
return err
60
63
}
61
64
62
65
func 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 )
64
67
}
65
68
66
69
func 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 )
68
71
}
69
72
70
73
func 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 )
72
75
}
73
76
74
77
func labelStatsCorrectNumIssuesRepo (ctx context.Context , id int64 ) error {
@@ -105,11 +108,11 @@ func milestoneStatsCorrectNumIssuesRepo(ctx context.Context, id int64) error {
105
108
}
106
109
107
110
func 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 )
109
112
}
110
113
111
114
func 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 ) )
113
116
}
114
117
115
118
func repoStatsCorrectNumIssues (ctx context.Context , id int64 ) error {
@@ -128,9 +131,12 @@ func repoStatsCorrectNumClosedPulls(ctx context.Context, id int64) error {
128
131
return repo_model .UpdateRepoIssueNumbers (ctx , id , true , true )
129
132
}
130
133
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 )
134
140
}
135
141
}
136
142
@@ -201,7 +207,16 @@ func CheckRepoStats(ctx context.Context) error {
201
207
},
202
208
// Issue.NumComments
203
209
{
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
+ ),
205
220
repoStatsCorrectIssueNumComments ,
206
221
"issue count 'num_comments'" ,
207
222
},
0 commit comments