@@ -868,7 +868,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
868868 }
869869 fallthrough
870870 case CommentTypeComment :
871- if _ , err = db . Exec (ctx , "UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?" , opts .Issue .ID ); err != nil {
871+ if err := UpdateIssueNumComments (ctx , opts .Issue .ID ); err != nil {
872872 return err
873873 }
874874 fallthrough
@@ -1148,8 +1148,8 @@ func DeleteComment(ctx context.Context, comment *Comment) error {
11481148 return err
11491149 }
11501150
1151- if comment .Type == CommentTypeComment {
1152- if _ , err := e . ID ( comment .IssueID ). Decr ( "num_comments" ). Update ( new ( Issue ) ); err != nil {
1151+ if comment .Type == CommentTypeComment || comment . Type == CommentTypeReview {
1152+ if err := UpdateIssueNumComments ( ctx , comment .IssueID ); err != nil {
11531153 return err
11541154 }
11551155 }
@@ -1266,6 +1266,26 @@ func (c *Comment) HasOriginalAuthor() bool {
12661266 return c .OriginalAuthor != "" && c .OriginalAuthorID != 0
12671267}
12681268
1269+ func CountCommentsBuilder (issueID int64 ) * builder.Builder {
1270+ return builder .Select ("count(*)" ).From ("comment" ).Where (builder.Eq {
1271+ "issue_id" : issueID ,
1272+ }.And (builder .Or (
1273+ builder.Eq {"type" : CommentTypeComment },
1274+ builder.Eq {"type" : CommentTypeReview },
1275+ builder.Eq {"type" : CommentTypeCode },
1276+ )).And (builder.Neq {
1277+ "invalidated" : true ,
1278+ }))
1279+ }
1280+
1281+ func UpdateIssueNumComments (ctx context.Context , issueID int64 ) error {
1282+ _ , err := db .GetEngine (ctx ).
1283+ SetExpr ("num_comments" , CountCommentsBuilder (issueID )).
1284+ ID (issueID ).
1285+ Update (new (Issue ))
1286+ return err
1287+ }
1288+
12691289// InsertIssueComments inserts many comments of issues.
12701290func InsertIssueComments (ctx context.Context , comments []* Comment ) error {
12711291 if len (comments ) == 0 {
@@ -1298,8 +1318,7 @@ func InsertIssueComments(ctx context.Context, comments []*Comment) error {
12981318 }
12991319
13001320 for _ , issueID := range issueIDs {
1301- if _ , err := db .Exec (ctx , "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?" ,
1302- issueID , CommentTypeComment , issueID ); err != nil {
1321+ if err := UpdateIssueNumComments (ctx , issueID ); err != nil {
13031322 return err
13041323 }
13051324 }
0 commit comments