From d76f34ef519f773ab66f2ba9f91c65a3aaf6537e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 11 Feb 2017 20:01:33 +0800 Subject: [PATCH] small optimization for get issue labels --- models/issue_label.go | 21 ++++----------------- models/models.go | 1 + 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/models/issue_label.go b/models/issue_label.go index 015e7f128d..67e0ac77b5 100644 --- a/models/issue_label.go +++ b/models/issue_label.go @@ -190,23 +190,10 @@ func GetLabelsByRepoID(repoID int64, sortType string) ([]*Label, error) { } func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) { - issueLabels, err := getIssueLabels(e, issueID) - if err != nil { - return nil, fmt.Errorf("getIssueLabels: %v", err) - } else if len(issueLabels) == 0 { - return []*Label{}, nil - } - - labelIDs := make([]int64, len(issueLabels)) - for i := range issueLabels { - labelIDs[i] = issueLabels[i].LabelID - } - - labels := make([]*Label, 0, len(labelIDs)) - return labels, e. - Where("id > 0"). - In("id", labelIDs). - Asc("name"). + var labels []*Label + return labels, e.Where("issue_label.issue_id = ?", issueID). + Join("LEFT", "issue_label", "issue_label.label_id = label.id"). + Asc("label.name"). Find(&labels) } diff --git a/models/models.go b/models/models.go index 6590834037..7f0ef59547 100644 --- a/models/models.go +++ b/models/models.go @@ -42,6 +42,7 @@ type Engine interface { Insert(...interface{}) (int64, error) InsertOne(interface{}) (int64, error) Iterate(interface{}, xorm.IterFunc) error + Join(joinOperator string, tablename interface{}, condition string, args ...interface{}) *xorm.Session SQL(interface{}, ...interface{}) *xorm.Session Where(interface{}, ...interface{}) *xorm.Session }