Browse Source

small optimization for get issue labels

Lunny Xiao 3 years ago
parent
commit
d76f34ef51
2 changed files with 5 additions and 17 deletions
  1. 4 17
      models/issue_label.go
  2. 1 0
      models/models.go

+ 4 - 17
models/issue_label.go

@@ -190,23 +190,10 @@ func GetLabelsByRepoID(repoID int64, sortType string) ([]*Label, error) {
190 190
 }
191 191
 
192 192
 func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
193
-	issueLabels, err := getIssueLabels(e, issueID)
194
-	if err != nil {
195
-		return nil, fmt.Errorf("getIssueLabels: %v", err)
196
-	} else if len(issueLabels) == 0 {
197
-		return []*Label{}, nil
198
-	}
199
-
200
-	labelIDs := make([]int64, len(issueLabels))
201
-	for i := range issueLabels {
202
-		labelIDs[i] = issueLabels[i].LabelID
203
-	}
204
-
205
-	labels := make([]*Label, 0, len(labelIDs))
206
-	return labels, e.
207
-		Where("id > 0").
208
-		In("id", labelIDs).
209
-		Asc("name").
193
+	var labels []*Label
194
+	return labels, e.Where("issue_label.issue_id = ?", issueID).
195
+		Join("LEFT", "issue_label", "issue_label.label_id = label.id").
196
+		Asc("label.name").
210 197
 		Find(&labels)
211 198
 }
212 199
 

+ 1 - 0
models/models.go

@@ -42,6 +42,7 @@ type Engine interface {
42 42
 	Insert(...interface{}) (int64, error)
43 43
 	InsertOne(interface{}) (int64, error)
44 44
 	Iterate(interface{}, xorm.IterFunc) error
45
+	Join(joinOperator string, tablename interface{}, condition string, args ...interface{}) *xorm.Session
45 46
 	SQL(interface{}, ...interface{}) *xorm.Session
46 47
 	Where(interface{}, ...interface{}) *xorm.Session
47 48
 }