@@ -16,6 +16,8 @@ import (
16
16
"code.gitea.io/gitea/models/unit"
17
17
user_model "code.gitea.io/gitea/models/user"
18
18
"code.gitea.io/gitea/modules/log"
19
+
20
+ "xorm.io/builder"
19
21
)
20
22
21
23
// Init initialize model
@@ -24,7 +26,7 @@ func Init(ctx context.Context) error {
24
26
}
25
27
26
28
type repoChecker struct {
27
- querySQL func (ctx context.Context ) ([]map [ string ][] byte , error )
29
+ querySQL func (ctx context.Context ) ([]int64 , error )
28
30
correctSQL func (ctx context.Context , id int64 ) error
29
31
desc string
30
32
}
@@ -35,8 +37,7 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) {
35
37
log .Error ("Select %s: %v" , checker .desc , err )
36
38
return
37
39
}
38
- for _ , result := range results {
39
- id , _ := strconv .ParseInt (string (result ["id" ]), 10 , 64 )
40
+ for _ , id := range results {
40
41
select {
41
42
case <- ctx .Done ():
42
43
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) {
51
52
}
52
53
}
53
54
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 ... )
56
59
return err
57
60
}
58
61
59
62
func 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 )
61
64
}
62
65
63
66
func 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 )
65
68
}
66
69
67
70
func 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 )
69
72
}
70
73
71
74
func labelStatsCorrectNumIssuesRepo (ctx context.Context , id int64 ) error {
@@ -102,11 +105,11 @@ func milestoneStatsCorrectNumIssuesRepo(ctx context.Context, id int64) error {
102
105
}
103
106
104
107
func 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 )
106
109
}
107
110
108
111
func 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 ) )
110
113
}
111
114
112
115
func repoStatsCorrectNumIssues (ctx context.Context , id int64 ) error {
@@ -125,9 +128,12 @@ func repoStatsCorrectNumClosedPulls(ctx context.Context, id int64) error {
125
128
return repo_model .UpdateRepoIssueNumbers (ctx , id , true , true )
126
129
}
127
130
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 )
131
137
}
132
138
}
133
139
@@ -198,7 +204,16 @@ func CheckRepoStats(ctx context.Context) error {
198
204
},
199
205
// Issue.NumComments
200
206
{
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
+ ),
202
217
repoStatsCorrectIssueNumComments ,
203
218
"issue count 'num_comments'" ,
204
219
},
0 commit comments