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
This commit is contained in:
Lunny Xiao 2018-10-31 05:48:37 +08:00 committed by techknowlogick
parent e5daa2698f
commit 10370651fc
2 changed files with 17 additions and 37 deletions

View file

@ -173,11 +173,9 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
cond = cond.And(builder.Eq{"is_private": false}) cond = cond.And(builder.Eq{"is_private": false})
} }
var starred bool
if opts.OwnerID > 0 { if opts.OwnerID > 0 {
if opts.Starred { if opts.Starred {
starred = true cond = cond.And(builder.In("id", builder.Select("repo_id").From("star").Where(builder.Eq{"uid": opts.OwnerID})))
cond = builder.Eq{"star.uid": opts.OwnerID}
} else { } else {
var accessCond = builder.NewCond() var accessCond = builder.NewCond()
if opts.Collaborate != util.OptionalBoolTrue { if opts.Collaborate != util.OptionalBoolTrue {
@ -204,15 +202,23 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
} }
if opts.Keyword != "" { if opts.Keyword != "" {
var keywordCond = builder.NewCond()
// separate keyword // separate keyword
var subQueryCond = builder.NewCond()
for _, v := range strings.Split(opts.Keyword, ",") { for _, v := range strings.Split(opts.Keyword, ",") {
if opts.TopicOnly { subQueryCond = subQueryCond.Or(builder.Like{"topic.name", strings.ToLower(v)})
keywordCond = keywordCond.Or(builder.Like{"topic.name", strings.ToLower(v)})
} else {
keywordCond = keywordCond.Or(builder.Like{"lower_name", strings.ToLower(v)})
keywordCond = keywordCond.Or(builder.Like{"topic.name", strings.ToLower(v)})
} }
subQuery := builder.Select("repo_topic.repo_id").From("repo_topic").
Join("INNER", "topic", "topic.id = repo_topic.topic_id").
Where(subQueryCond).
GroupBy("repo_topic.repo_id")
var keywordCond = builder.In("id", subQuery)
if !opts.TopicOnly {
var likes = builder.NewCond()
for _, v := range strings.Split(opts.Keyword, ",") {
likes = likes.Or(builder.Like{"lower_name", strings.ToLower(v)})
}
keywordCond = keywordCond.Or(likes)
} }
cond = cond.And(keywordCond) cond = cond.And(keywordCond)
} }
@ -232,15 +238,6 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
sess := x.NewSession() sess := x.NewSession()
defer sess.Close() defer sess.Close()
if starred {
sess.Join("INNER", "star", "star.repo_id = repository.id")
}
if opts.Keyword != "" {
sess.Join("LEFT", "repo_topic", "repo_topic.repo_id = repository.id")
sess.Join("LEFT", "topic", "repo_topic.topic_id = topic.id")
}
count, err := sess. count, err := sess.
Where(cond). Where(cond).
Count(new(Repository)) Count(new(Repository))
@ -249,27 +246,10 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
return nil, 0, fmt.Errorf("Count: %v", err) return nil, 0, fmt.Errorf("Count: %v", err)
} }
// Set again after reset by Count()
if starred {
sess.Join("INNER", "star", "star.repo_id = repository.id")
}
if opts.Keyword != "" {
sess.Join("LEFT", "repo_topic", "repo_topic.repo_id = repository.id")
sess.Join("LEFT", "topic", "repo_topic.topic_id = topic.id")
}
if opts.Keyword != "" {
sess.Select("repository.*")
sess.GroupBy("repository.id")
sess.OrderBy("repository." + opts.OrderBy.String())
} else {
sess.OrderBy(opts.OrderBy.String())
}
repos := make(RepositoryList, 0, opts.PageSize) repos := make(RepositoryList, 0, opts.PageSize)
if err = sess. if err = sess.
Where(cond). Where(cond).
OrderBy(opts.OrderBy.String()).
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
Find(&repos); err != nil { Find(&repos); err != nil {
return nil, 0, fmt.Errorf("Repo: %v", err) return nil, 0, fmt.Errorf("Repo: %v", err)

View file

@ -239,7 +239,7 @@ func TestSearchRepositoryByTopicName(t *testing.T) {
count: 1}, count: 1},
{name: "AllPublic/OnlySearchMultipleKeywordPublicRepositoriesFromTopic", {name: "AllPublic/OnlySearchMultipleKeywordPublicRepositoriesFromTopic",
opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql,golang", TopicOnly: true}, opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql,golang", TopicOnly: true},
count: 3}, count: 2},
} }
for _, testCase := range testCases { for _, testCase := range testCases {