Skip to content

Commit 1037065

Browse files
lunnytechknowlogick
authored andcommitted
This commit will reduce join star, repo_topic, topic tables on repo search, so that fix extra columns problem on mssql (#5136)
* This commit will reduce join star, repo_topic, topic tables on repo search, so that fix extra columns problem on mssql * fix tests
1 parent e5daa26 commit 1037065

File tree

2 files changed

+17
-37
lines changed

2 files changed

+17
-37
lines changed

models/repo_list.go

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,9 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
173173
cond = cond.And(builder.Eq{"is_private": false})
174174
}
175175

176-
var starred bool
177176
if opts.OwnerID > 0 {
178177
if opts.Starred {
179-
starred = true
180-
cond = builder.Eq{"star.uid": opts.OwnerID}
178+
cond = cond.And(builder.In("id", builder.Select("repo_id").From("star").Where(builder.Eq{"uid": opts.OwnerID})))
181179
} else {
182180
var accessCond = builder.NewCond()
183181
if opts.Collaborate != util.OptionalBoolTrue {
@@ -204,15 +202,23 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
204202
}
205203

206204
if opts.Keyword != "" {
207-
var keywordCond = builder.NewCond()
208205
// separate keyword
206+
var subQueryCond = builder.NewCond()
209207
for _, v := range strings.Split(opts.Keyword, ",") {
210-
if opts.TopicOnly {
211-
keywordCond = keywordCond.Or(builder.Like{"topic.name", strings.ToLower(v)})
212-
} else {
213-
keywordCond = keywordCond.Or(builder.Like{"lower_name", strings.ToLower(v)})
214-
keywordCond = keywordCond.Or(builder.Like{"topic.name", strings.ToLower(v)})
208+
subQueryCond = subQueryCond.Or(builder.Like{"topic.name", strings.ToLower(v)})
209+
}
210+
subQuery := builder.Select("repo_topic.repo_id").From("repo_topic").
211+
Join("INNER", "topic", "topic.id = repo_topic.topic_id").
212+
Where(subQueryCond).
213+
GroupBy("repo_topic.repo_id")
214+
215+
var keywordCond = builder.In("id", subQuery)
216+
if !opts.TopicOnly {
217+
var likes = builder.NewCond()
218+
for _, v := range strings.Split(opts.Keyword, ",") {
219+
likes = likes.Or(builder.Like{"lower_name", strings.ToLower(v)})
215220
}
221+
keywordCond = keywordCond.Or(likes)
216222
}
217223
cond = cond.And(keywordCond)
218224
}
@@ -232,15 +238,6 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
232238
sess := x.NewSession()
233239
defer sess.Close()
234240

235-
if starred {
236-
sess.Join("INNER", "star", "star.repo_id = repository.id")
237-
}
238-
239-
if opts.Keyword != "" {
240-
sess.Join("LEFT", "repo_topic", "repo_topic.repo_id = repository.id")
241-
sess.Join("LEFT", "topic", "repo_topic.topic_id = topic.id")
242-
}
243-
244241
count, err := sess.
245242
Where(cond).
246243
Count(new(Repository))
@@ -249,27 +246,10 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
249246
return nil, 0, fmt.Errorf("Count: %v", err)
250247
}
251248

252-
// Set again after reset by Count()
253-
if starred {
254-
sess.Join("INNER", "star", "star.repo_id = repository.id")
255-
}
256-
257-
if opts.Keyword != "" {
258-
sess.Join("LEFT", "repo_topic", "repo_topic.repo_id = repository.id")
259-
sess.Join("LEFT", "topic", "repo_topic.topic_id = topic.id")
260-
}
261-
262-
if opts.Keyword != "" {
263-
sess.Select("repository.*")
264-
sess.GroupBy("repository.id")
265-
sess.OrderBy("repository." + opts.OrderBy.String())
266-
} else {
267-
sess.OrderBy(opts.OrderBy.String())
268-
}
269-
270249
repos := make(RepositoryList, 0, opts.PageSize)
271250
if err = sess.
272251
Where(cond).
252+
OrderBy(opts.OrderBy.String()).
273253
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
274254
Find(&repos); err != nil {
275255
return nil, 0, fmt.Errorf("Repo: %v", err)

models/repo_list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func TestSearchRepositoryByTopicName(t *testing.T) {
239239
count: 1},
240240
{name: "AllPublic/OnlySearchMultipleKeywordPublicRepositoriesFromTopic",
241241
opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql,golang", TopicOnly: true},
242-
count: 3},
242+
count: 2},
243243
}
244244

245245
for _, testCase := range testCases {

0 commit comments

Comments
 (0)