slight optimization for GetUserRepositories (#498)

This commit is contained in:
Lunny Xiao 2016-12-29 02:53:33 -06:00 committed by GitHub
parent 22e1bd31c6
commit 799d0c2030
1 changed files with 11 additions and 8 deletions

View File

@ -536,24 +536,28 @@ func (org *User) GetUserTeams(userID int64) ([]*Team, error) {
// that the user with the given userID has access to, // that the user with the given userID has access to,
// and total number of records based on given condition. // and total number of records based on given condition.
func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repository, int64, error) { func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repository, int64, error) {
var cond builder.Cond = builder.Eq{
"`repository`.owner_id": org.ID,
"`repository`.is_private": false,
}
teamIDs, err := org.GetUserTeamIDs(userID) teamIDs, err := org.GetUserTeamIDs(userID)
if err != nil { if err != nil {
return nil, 0, fmt.Errorf("GetUserTeamIDs: %v", err) return nil, 0, fmt.Errorf("GetUserTeamIDs: %v", err)
} }
if len(teamIDs) == 0 {
// user has no team but "IN ()" is invalid SQL if len(teamIDs) > 0 {
teamIDs = []int64{-1} // there is no repo with id=-1 cond = cond.Or(builder.In("team_repo.team_id", teamIDs))
} }
if page <= 0 { if page <= 0 {
page = 1 page = 1
} }
repos := make([]*Repository, 0, pageSize) repos := make([]*Repository, 0, pageSize)
if err := x. if err := x.
Select("`repository`.*").
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id"). Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
Where("(`repository`.owner_id=? AND `repository`.is_private=?)", org.ID, false). Where(cond).
Or(builder.In("team_repo.team_id", teamIDs)).
GroupBy("`repository`.id"). GroupBy("`repository`.id").
OrderBy("updated_unix DESC"). OrderBy("updated_unix DESC").
Limit(pageSize, (page-1)*pageSize). Limit(pageSize, (page-1)*pageSize).
@ -563,8 +567,7 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos
repoCount, err := x. repoCount, err := x.
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id"). Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
Where("(`repository`.owner_id=? AND `repository`.is_private=?)", org.ID, false). Where(cond).
Or(builder.In("team_repo.team_id", teamIDs)).
GroupBy("`repository`.id"). GroupBy("`repository`.id").
Count(&Repository{}) Count(&Repository{})
if err != nil { if err != nil {