From 3f812e5906b3aba57f0efa03d5aadf942bd66ce3 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 28 Mar 2022 21:33:55 +0200 Subject: [PATCH 1/3] remove error who is none --- models/issue.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/issue.go b/models/issue.go index 79771ce15c24..31cbe2b16fd5 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1543,7 +1543,7 @@ func CountIssues(opts *IssuesOptions) (int64, error) { return 0, fmt.Errorf("Find: %v", err) } if len(countsSlice) < 1 { - return 0, fmt.Errorf("there is less than one result sql record") + return 0, nil } return countsSlice[0].Count, nil } From 522fdeea7fa3c4834e45b090c434a989d823c17e Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 29 Mar 2022 19:30:35 +0200 Subject: [PATCH 2/3] Update models/issue.go --- models/issue.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/issue.go b/models/issue.go index 31cbe2b16fd5..f3a152df2800 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1542,7 +1542,7 @@ func CountIssues(opts *IssuesOptions) (int64, error) { if err := sess.Find(&countsSlice); err != nil { return 0, fmt.Errorf("Find: %v", err) } - if len(countsSlice) < 1 { + if len(countsSlice) == 0 { return 0, nil } return countsSlice[0].Count, nil From 18aeedf8a784401659279525d5223f889f312cd5 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 30 Mar 2022 13:37:39 +0800 Subject: [PATCH 3/3] use setupSessionNoLimit instead of setupSessionWithLimit when no pagination --- models/issue.go | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/models/issue.go b/models/issue.go index 70cc1a8dfda5..5fb7e3eb6306 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1271,7 +1271,7 @@ func sortIssuesSession(sess *xorm.Session, sortType string, priorityRepoID int64 } } -func (opts *IssuesOptions) setupSession(sess *xorm.Session) { +func (opts *IssuesOptions) setupSessionWithLimit(sess *xorm.Session) { if opts.Page >= 0 && opts.PageSize > 0 { var start int if opts.Page == 0 { @@ -1281,7 +1281,10 @@ func (opts *IssuesOptions) setupSession(sess *xorm.Session) { } sess.Limit(opts.PageSize, start) } + opts.setupSessionNoLimit(sess) +} +func (opts *IssuesOptions) setupSessionNoLimit(sess *xorm.Session) { if len(opts.IssueIDs) > 0 { sess.In("issue.id", opts.IssueIDs) } @@ -1447,7 +1450,7 @@ func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) { sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id") - opts.setupSession(sess) + opts.setupSessionNoLimit(sess) countsSlice := make([]*struct { RepoID int64 @@ -1457,7 +1460,7 @@ func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) { Select("issue.repo_id AS repo_id, COUNT(*) AS count"). Table("issue"). Find(&countsSlice); err != nil { - return nil, err + return nil, fmt.Errorf("unable to CountIssuesByRepo: %w", err) } countMap := make(map[int64]int64, len(countsSlice)) @@ -1474,14 +1477,14 @@ func GetRepoIDsForIssuesOptions(opts *IssuesOptions, user *user_model.User) ([]i sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id") - opts.setupSession(sess) + opts.setupSessionNoLimit(sess) accessCond := accessibleRepositoryCondition(user) if err := sess.Where(accessCond). Distinct("issue.repo_id"). Table("issue"). Find(&repoIDs); err != nil { - return nil, err + return nil, fmt.Errorf("unable to GetRepoIDsForIssuesOptions: %w", err) } return repoIDs, nil @@ -1492,17 +1495,16 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) { e := db.GetEngine(db.DefaultContext) sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id") - opts.setupSession(sess) + opts.setupSessionWithLimit(sess) sortIssuesSession(sess, opts.SortType, opts.PriorityRepoID) issues := make([]*Issue, 0, opts.ListOptions.PageSize) if err := sess.Find(&issues); err != nil { - return nil, fmt.Errorf("Find: %v", err) + return nil, fmt.Errorf("unable to query Issues: %w", err) } - sess.Close() if err := IssueList(issues).LoadAttributes(); err != nil { - return nil, fmt.Errorf("LoadAttributes: %v", err) + return nil, fmt.Errorf("unable to LoadAttributes for Issues: %w", err) } return issues, nil @@ -1513,18 +1515,17 @@ func CountIssues(opts *IssuesOptions) (int64, error) { e := db.GetEngine(db.DefaultContext) countsSlice := make([]*struct { - RepoID int64 - Count int64 + Count int64 }, 0, 1) sess := e.Select("COUNT(issue.id) AS count").Table("issue") sess.Join("INNER", "repository", "`issue`.repo_id = `repository`.id") - opts.setupSession(sess) + opts.setupSessionNoLimit(sess) if err := sess.Find(&countsSlice); err != nil { - return 0, fmt.Errorf("Find: %v", err) + return 0, fmt.Errorf("unable to CountIssues: %w", err) } - if len(countsSlice) == 0 { - return 0, nil + if len(countsSlice) != 1 { + return 0, fmt.Errorf("unable to get one row result when CountIssues, row count=%d", len(countsSlice)) } return countsSlice[0].Count, nil }