Skip to content

Commit 3786369

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) (#5229)
* 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 7946421 commit 3786369

File tree

2 files changed

+22
-36
lines changed

2 files changed

+22
-36
lines changed

models/repo_list.go

Lines changed: 19 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,12 +202,23 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
204202
}
205203

206204
if opts.Keyword != "" {
207-
var keywordCond = builder.NewCond()
208-
if opts.TopicOnly {
209-
keywordCond = keywordCond.Or(builder.Like{"topic.name", strings.ToLower(opts.Keyword)})
210-
} else {
211-
keywordCond = keywordCond.Or(builder.Like{"lower_name", strings.ToLower(opts.Keyword)})
212-
keywordCond = keywordCond.Or(builder.Like{"topic.name", strings.ToLower(opts.Keyword)})
205+
// separate keyword
206+
var subQueryCond = builder.NewCond()
207+
for _, v := range strings.Split(opts.Keyword, ",") {
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)})
220+
}
221+
keywordCond = keywordCond.Or(likes)
213222
}
214223
cond = cond.And(keywordCond)
215224
}
@@ -229,15 +238,6 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
229238
sess := x.NewSession()
230239
defer sess.Close()
231240

232-
if starred {
233-
sess.Join("INNER", "star", "star.repo_id = repository.id")
234-
}
235-
236-
if opts.Keyword != "" {
237-
sess.Join("LEFT", "repo_topic", "repo_topic.repo_id = repository.id")
238-
sess.Join("LEFT", "topic", "repo_topic.topic_id = topic.id")
239-
}
240-
241241
count, err := sess.
242242
Where(cond).
243243
Count(new(Repository))
@@ -246,27 +246,10 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
246246
return nil, 0, fmt.Errorf("Count: %v", err)
247247
}
248248

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

models/repo_list_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ func TestSearchRepositoryByTopicName(t *testing.T) {
237237
{name: "AllPublic/OnlySearchPublicRepositoriesFromTopic",
238238
opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql", TopicOnly: true},
239239
count: 1},
240+
{name: "AllPublic/OnlySearchMultipleKeywordPublicRepositoriesFromTopic",
241+
opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql,golang", TopicOnly: true},
242+
count: 2},
240243
}
241244

242245
for _, testCase := range testCases {

0 commit comments

Comments
 (0)