diff options
author | btrepp <beautrepp@gmail.com> | 2017-01-14 22:32:36 +0800 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-01-14 22:32:36 +0800 |
commit | 302fa4298008cf2e5d52d442b16c8e598551200b (patch) | |
tree | 1fc9ac3713620567c93a8b05d7cc1848bf9c2d14 | |
parent | 88f45ce38ccc93dccb5bbffea9ff6328d7612275 (diff) | |
download | gitea-302fa4298008cf2e5d52d442b16c8e598551200b.tar.gz gitea-302fa4298008cf2e5d52d442b16c8e598551200b.zip |
Removes reliance on server specific SQL (#393)
Breaks the retrieval of repositories into two queries
This fetches the paged ids in one go, then the
actual repository information in a second query
Some databases do not support SELECT with *
when group by is used.
-rw-r--r-- | models/org.go | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/models/org.go b/models/org.go index a293f58216..36d868d5ce 100644 --- a/models/org.go +++ b/models/org.go @@ -598,15 +598,29 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos if page <= 0 { page = 1 } + repos := make([]*Repository, 0, pageSize) - if err := x.Select("`repository`.*"). + if err := x. + Select("`repository`.id"). Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id"). Where(cond). - GroupBy("`repository`.id"). + GroupBy("`repository`.id,`repository`.updated_unix"). OrderBy("updated_unix DESC"). Limit(pageSize, (page-1)*pageSize). Find(&repos); err != nil { + return nil, 0, fmt.Errorf("get repository ids: %v", err) + } + + repoIDs := make([]int64,pageSize) + for i := range repos { + repoIDs[i] = repos[i].ID + } + + if err := x. + Select("`repository`.*"). + Where(builder.In("`repository`.id",repoIDs)). + Find(&repos); err!=nil { return nil, 0, fmt.Errorf("get repositories: %v", err) } |