@@ -23,6 +23,8 @@ import (
2323 api "code.gitea.io/gitea/modules/structs"
2424 "code.gitea.io/gitea/modules/timeutil"
2525 "code.gitea.io/gitea/modules/util"
26+
27+ "xorm.io/builder"
2628)
2729
2830// ErrUserDoesNotHaveAccessToRepo represets an error where the user doesn't has access to a given repo.
@@ -319,13 +321,7 @@ func (repo *Repository) LoadUnits(ctx context.Context) (err error) {
319321
320322// UnitEnabled if this repository has the given unit enabled
321323func (repo * Repository ) UnitEnabled (tp unit.Type ) (result bool ) {
322- if err := db .WithContext (func (ctx * db.Context ) error {
323- result = repo .UnitEnabledCtx (ctx , tp )
324- return nil
325- }); err != nil {
326- log .Error ("repo.UnitEnabled: %v" , err )
327- }
328- return
324+ return repo .UnitEnabledCtx (db .DefaultContext , tp )
329325}
330326
331327// UnitEnabled if this repository has the given unit enabled
@@ -760,33 +756,28 @@ func CountRepositories(ctx context.Context, opts CountRepositoryOptions) (int64,
760756 return count , nil
761757}
762758
763- // StatsCorrectNumClosed update repository's issue related numbers
764- func StatsCorrectNumClosed (ctx context.Context , id int64 , isPull bool , field string ) error {
765- _ , err := db .Exec (ctx , "UPDATE `repository` SET " + field + "=(SELECT COUNT(*) FROM `issue` WHERE repo_id=? AND is_closed=? AND is_pull=?) WHERE id=?" , id , true , isPull , id )
766- return err
767- }
768-
769- // UpdateRepoIssueNumbers update repository issue numbers
759+ // UpdateRepoIssueNumbers updates one of a repositories amount of (open|closed) (issues|PRs) with the current count
770760func UpdateRepoIssueNumbers (ctx context.Context , repoID int64 , isPull , isClosed bool ) error {
771- e := db .GetEngine (ctx )
761+ field := "num_"
762+ if isClosed {
763+ field += "closed_"
764+ }
772765 if isPull {
773- if _ , err := e .ID (repoID ).Decr ("num_pulls" ).Update (new (Repository )); err != nil {
774- return err
775- }
776- if isClosed {
777- if _ , err := e .ID (repoID ).Decr ("num_closed_pulls" ).Update (new (Repository )); err != nil {
778- return err
779- }
780- }
766+ field += "pulls"
781767 } else {
782- if _ , err := e .ID (repoID ).Decr ("num_issues" ).Update (new (Repository )); err != nil {
783- return err
784- }
785- if isClosed {
786- if _ , err := e .ID (repoID ).Decr ("num_closed_issues" ).Update (new (Repository )); err != nil {
787- return err
788- }
789- }
768+ field += "issues"
790769 }
791- return nil
770+
771+ subQuery := builder .Select ("count(*)" ).
772+ From ("issue" ).Where (builder.Eq {
773+ "repo_id" : repoID ,
774+ "is_pull" : isPull ,
775+ }.And (builder .If (isClosed , builder.Eq {"is_closed" : isClosed })))
776+
777+ // builder.Update(cond) will generate SQL like UPDATE ... SET cond
778+ query := builder .Update (builder.Eq {field : subQuery }).
779+ From ("repository" ).
780+ Where (builder.Eq {"id" : repoID })
781+ _ , err := db .Exec (ctx , query )
782+ return err
792783}
0 commit comments