mirror of
https://github.com/go-gitea/gitea.git
synced 2025-03-12 04:58:21 +03:00
Use builder
This commit is contained in:
parent
17d8a24801
commit
c3d5d26c49
@ -208,8 +208,8 @@ func (t CommentType) CountedAsConversation() bool {
|
|||||||
|
|
||||||
// ConversationCountedCommentType returns the comment types that are counted as a conversation
|
// ConversationCountedCommentType returns the comment types that are counted as a conversation
|
||||||
// The returned types are []any rather than []CommentType to allow for easy use as xorm args
|
// The returned types are []any rather than []CommentType to allow for easy use as xorm args
|
||||||
func ConversationCountedCommentType() []any {
|
func ConversationCountedCommentType() []CommentType {
|
||||||
return []any{CommentTypeComment, CommentTypeReview}
|
return []CommentType{CommentTypeComment, CommentTypeReview}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoleInRepo presents the user's participation in the repo
|
// RoleInRepo presents the user's participation in the repo
|
||||||
@ -1318,7 +1318,7 @@ func (c *Comment) HasOriginalAuthor() bool {
|
|||||||
func UpdateIssueNumComments(ctx context.Context, issueID int64) error {
|
func UpdateIssueNumComments(ctx context.Context, issueID int64) error {
|
||||||
countCommentsBuilder := builder.Select("count(*)").From("comment").Where(builder.Eq{
|
countCommentsBuilder := builder.Select("count(*)").From("comment").Where(builder.Eq{
|
||||||
"issue_id": issueID,
|
"issue_id": issueID,
|
||||||
}.And(builder.In("type", ConversationCountedCommentType()...)))
|
}.And(builder.In("type", ConversationCountedCommentType())))
|
||||||
|
|
||||||
_, err := db.GetEngine(ctx).
|
_, err := db.GetEngine(ctx).
|
||||||
SetExpr("num_comments", countCommentsBuilder).
|
SetExpr("num_comments", countCommentsBuilder).
|
||||||
|
@ -6,7 +6,6 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
_ "image/jpeg" // Needed for jpeg support
|
_ "image/jpeg" // Needed for jpeg support
|
||||||
@ -17,6 +16,7 @@ import (
|
|||||||
"code.gitea.io/gitea/models/unit"
|
"code.gitea.io/gitea/models/unit"
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Init initialize model
|
// Init initialize model
|
||||||
@ -25,7 +25,7 @@ func Init(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type repoChecker struct {
|
type repoChecker struct {
|
||||||
querySQL func(ctx context.Context) ([]map[string][]byte, error)
|
querySQL func(ctx context.Context) ([]int64, error)
|
||||||
correctSQL func(ctx context.Context, id int64) error
|
correctSQL func(ctx context.Context, id int64) error
|
||||||
desc string
|
desc string
|
||||||
}
|
}
|
||||||
@ -36,8 +36,7 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) {
|
|||||||
log.Error("Select %s: %v", checker.desc, err)
|
log.Error("Select %s: %v", checker.desc, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, result := range results {
|
for _, id := range results {
|
||||||
id, _ := strconv.ParseInt(string(result["id"]), 10, 64)
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
log.Warn("CheckRepoStats: Cancelled before checking %s for with id=%d", checker.desc, id)
|
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) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func StatsCorrectSQL(ctx context.Context, sql string, id int64) error {
|
func StatsCorrectSQL(ctx context.Context, sql any, ids ...any) error {
|
||||||
_, err := db.GetEngine(ctx).Exec(sql, id, id)
|
args := []any{sql}
|
||||||
|
args = append(args, ids...)
|
||||||
|
_, err := db.GetEngine(ctx).Exec(args...)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,8 +108,14 @@ func userStatsCorrectNumRepos(ctx context.Context, id int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func repoStatsCorrectIssueNumComments(ctx context.Context, id int64) error {
|
func repoStatsCorrectIssueNumComments(ctx context.Context, id int64) error {
|
||||||
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=?",
|
return StatsCorrectSQL(ctx,
|
||||||
issues_model.ConversationCountedCommentType()...), id)
|
builder.Update(builder.Eq{
|
||||||
|
"num_comments": builder.Select("COUNT(*)").From("`comment`").Where(
|
||||||
|
builder.Eq{"issue_id": id}.And(
|
||||||
|
builder.In("type", issues_model.ConversationCountedCommentType()),
|
||||||
|
)),
|
||||||
|
}).From("`issue`").Where(builder.Eq{"id": id}),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func repoStatsCorrectNumIssues(ctx context.Context, id int64) error {
|
func repoStatsCorrectNumIssues(ctx context.Context, id int64) error {
|
||||||
@ -127,9 +134,12 @@ func repoStatsCorrectNumClosedPulls(ctx context.Context, id int64) error {
|
|||||||
return repo_model.UpdateRepoIssueNumbers(ctx, id, true, true)
|
return repo_model.UpdateRepoIssueNumbers(ctx, id, true, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func statsQuery(sql string, args ...any) func(context.Context) ([]map[string][]byte, error) {
|
// statsQuery returns a function that queries the database for a list of IDs
|
||||||
return func(ctx context.Context) ([]map[string][]byte, error) {
|
// sql could be a string or a *builder.Builder
|
||||||
return db.GetEngine(ctx).Query(append([]any{sql}, args...)...)
|
func statsQuery(sql any, args ...any) func(context.Context) ([]int64, error) {
|
||||||
|
return func(ctx context.Context) ([]int64, error) {
|
||||||
|
var ids []int64
|
||||||
|
return ids, db.GetEngine(ctx).SQL(sql, args...).Find(&ids)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,8 +210,16 @@ func CheckRepoStats(ctx context.Context) error {
|
|||||||
},
|
},
|
||||||
// Issue.NumComments
|
// Issue.NumComments
|
||||||
{
|
{
|
||||||
statsQuery("SELECT `issue`.id FROM `issue` WHERE `issue`.num_comments!=(SELECT COUNT(*) FROM `comment` WHERE issue_id=`issue`.id AND (type=? OR type=?))",
|
statsQuery(builder.Select("`issue`.id").From("`issue`").Where(
|
||||||
issues_model.ConversationCountedCommentType()...),
|
builder.Neq{
|
||||||
|
"`issue`.num_comments": builder.Select("COUNT(*)").From("`comment`").Where(
|
||||||
|
builder.Expr("issue_id = `issue`.id").And(
|
||||||
|
builder.In("type", issues_model.ConversationCountedCommentType()),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
repoStatsCorrectIssueNumComments,
|
repoStatsCorrectIssueNumComments,
|
||||||
"issue count 'num_comments'",
|
"issue count 'num_comments'",
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user