Rewrite XORM queries

This commit is contained in:
Thibault Meyer 2016-11-10 16:16:32 +01:00
parent c040f2fbb1
commit a4454f5d0f
No known key found for this signature in database
GPG key ID: BE39A108C4DDA755
22 changed files with 480 additions and 233 deletions

View file

@ -124,14 +124,18 @@ func (u *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
// GetAccessibleRepositories finds repositories which the user has access but does not own. // GetAccessibleRepositories finds repositories which the user has access but does not own.
// If limit is smaller than 1 means returns all found results. // If limit is smaller than 1 means returns all found results.
func (user *User) GetAccessibleRepositories(limit int) (repos []*Repository, _ error) { func (user *User) GetAccessibleRepositories(limit int) (repos []*Repository, _ error) {
sess := x.Where("owner_id !=? ", user.ID).Desc("updated_unix") sess := x.
Where("owner_id !=? ", user.ID).
Desc("updated_unix")
if limit > 0 { if limit > 0 {
sess.Limit(limit) sess.Limit(limit)
repos = make([]*Repository, 0, limit) repos = make([]*Repository, 0, limit)
} else { } else {
repos = make([]*Repository, 0, 10) repos = make([]*Repository, 0, 10)
} }
return repos, sess.Join("INNER", "access", "access.user_id = ? AND access.repo_id = repository.id", user.ID).Find(&repos) return repos, sess.
Join("INNER", "access", "access.user_id = ? AND access.repo_id = repository.id", user.ID).
Find(&repos)
} }
func maxAccessMode(modes ...AccessMode) AccessMode { func maxAccessMode(modes ...AccessMode) AccessMode {

View file

@ -591,9 +591,14 @@ func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error
// actorID can be -1 when isProfile is true or to skip the permission check. // actorID can be -1 when isProfile is true or to skip the permission check.
func GetFeeds(ctxUser *User, actorID, offset int64, isProfile bool) ([]*Action, error) { func GetFeeds(ctxUser *User, actorID, offset int64, isProfile bool) ([]*Action, error) {
actions := make([]*Action, 0, 20) actions := make([]*Action, 0, 20)
sess := x.Limit(20, int(offset)).Desc("id").Where("user_id = ?", ctxUser.ID) sess := x.
Limit(20, int(offset)).
Desc("id").
Where("user_id = ?", ctxUser.ID)
if isProfile { if isProfile {
sess.And("is_private = ?", false).And("act_user_id = ?", ctxUser.ID) sess.
And("is_private = ?", false).
And("act_user_id = ?", ctxUser.ID)
} else if actorID != -1 && ctxUser.IsOrganization() { } else if actorID != -1 && ctxUser.IsOrganization() {
// FIXME: only need to get IDs here, not all fields of repository. // FIXME: only need to get IDs here, not all fields of repository.
repos, _, err := ctxUser.GetUserRepositories(actorID, 1, ctxUser.NumRepos) repos, _, err := ctxUser.GetUserRepositories(actorID, 1, ctxUser.NumRepos)

View file

@ -103,7 +103,10 @@ func CountNotices() int64 {
// Notices returns number of notices in given page. // Notices returns number of notices in given page.
func Notices(page, pageSize int) ([]*Notice, error) { func Notices(page, pageSize int) ([]*Notice, error) {
notices := make([]*Notice, 0, pageSize) notices := make([]*Notice, 0, pageSize)
return notices, x.Limit(pageSize, (page-1)*pageSize).Desc("id").Find(&notices) return notices, x.
Limit(pageSize, (page-1)*pageSize).
Desc("id").
Find(&notices)
} }
// DeleteNotice deletes a system notice by given ID. // DeleteNotice deletes a system notice by given ID.
@ -127,6 +130,8 @@ func DeleteNoticesByIDs(ids []int64) error {
if len(ids) == 0 { if len(ids) == 0 {
return nil return nil
} }
_, err := x.Where("id IN (" + strings.Join(base.Int64sToStrings(ids), ",") + ")").Delete(new(Notice)) _, err := x.
Where("id IN (" + strings.Join(base.Int64sToStrings(ids), ",") + ")").
Delete(new(Notice))
return err return err
} }

View file

@ -17,12 +17,12 @@ import (
"strings" "strings"
"github.com/Unknwon/com" "github.com/Unknwon/com"
"github.com/go-gitea/git"
"github.com/go-gitea/gitea/modules/base" "github.com/go-gitea/gitea/modules/base"
"github.com/go-gitea/gitea/modules/log" "github.com/go-gitea/gitea/modules/log"
"github.com/go-gitea/gitea/modules/process" "github.com/go-gitea/gitea/modules/process"
"github.com/go-gitea/gitea/modules/setting" "github.com/go-gitea/gitea/modules/setting"
"github.com/go-gitea/gitea/modules/template/highlight" "github.com/go-gitea/gitea/modules/template/highlight"
"github.com/go-gitea/git"
"github.com/sergi/go-diff/diffmatchpatch" "github.com/sergi/go-diff/diffmatchpatch"
"golang.org/x/net/html/charset" "golang.org/x/net/html/charset"
"golang.org/x/text/transform" "golang.org/x/text/transform"

View file

@ -15,8 +15,8 @@ import (
"time" "time"
"github.com/Unknwon/com" "github.com/Unknwon/com"
"github.com/go-xorm/xorm"
api "github.com/go-gitea/go-sdk/gitea" api "github.com/go-gitea/go-sdk/gitea"
"github.com/go-xorm/xorm"
gouuid "github.com/satori/go.uuid" gouuid "github.com/satori/go.uuid"
"github.com/go-gitea/gitea/modules/base" "github.com/go-gitea/gitea/modules/base"
@ -820,13 +820,17 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
sess := x.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum) sess := x.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum)
if opts.RepoID > 0 { if opts.RepoID > 0 {
sess.Where("issue.repo_id=?", opts.RepoID).And("issue.is_closed=?", opts.IsClosed) sess.
Where("issue.repo_id=?", opts.RepoID).
And("issue.is_closed=?", opts.IsClosed)
} else if opts.RepoIDs != nil { } else if opts.RepoIDs != nil {
// In case repository IDs are provided but actually no repository has issue. // In case repository IDs are provided but actually no repository has issue.
if len(opts.RepoIDs) == 0 { if len(opts.RepoIDs) == 0 {
return make([]*Issue, 0), nil return make([]*Issue, 0), nil
} }
sess.In("issue.repo_id", base.Int64sToStrings(opts.RepoIDs)).And("issue.is_closed=?", opts.IsClosed) sess.
In("issue.repo_id", base.Int64sToStrings(opts.RepoIDs)).
And("issue.is_closed=?", opts.IsClosed)
} else { } else {
sess.Where("issue.is_closed=?", opts.IsClosed) sess.Where("issue.is_closed=?", opts.IsClosed)
} }
@ -863,12 +867,16 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
if len(opts.Labels) > 0 && opts.Labels != "0" { if len(opts.Labels) > 0 && opts.Labels != "0" {
labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ",")) labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
if len(labelIDs) > 0 { if len(labelIDs) > 0 {
sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").In("issue_label.label_id", labelIDs) sess.
Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
In("issue_label.label_id", labelIDs)
} }
} }
if opts.IsMention { if opts.IsMention {
sess.Join("INNER", "issue_user", "issue.id = issue_user.issue_id").And("issue_user.is_mentioned = ?", true) sess.
Join("INNER", "issue_user", "issue.id = issue_user.issue_id").
And("issue_user.is_mentioned = ?", true)
if opts.UserID > 0 { if opts.UserID > 0 {
sess.And("issue_user.uid = ?", opts.UserID) sess.And("issue_user.uid = ?", opts.UserID)
@ -991,7 +999,10 @@ func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*Issue
} }
ius := make([]*IssueUser, 0, 10) ius := make([]*IssueUser, 0, 10)
sess := x.Limit(20, (page-1)*20).Where("is_closed=?", isClosed).In("repo_id", rids) sess := x.
Limit(20, (page-1)*20).
Where("is_closed=?", isClosed).
In("repo_id", rids)
err := sess.Find(&ius) err := sess.Find(&ius)
return ius, err return ius, err
} }
@ -999,7 +1010,10 @@ func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*Issue
// GetIssueUserPairsByMode returns issue-user pairs by given repository and user. // GetIssueUserPairsByMode returns issue-user pairs by given repository and user.
func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) { func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) {
ius := make([]*IssueUser, 0, 10) ius := make([]*IssueUser, 0, 10)
sess := x.Limit(20, (page-1)*20).Where("uid=?", uid).And("is_closed=?", isClosed) sess := x.
Limit(20, (page-1)*20).
Where("uid=?", uid).
And("is_closed=?", isClosed)
if rid > 0 { if rid > 0 {
sess.And("repo_id=?", rid) sess.And("repo_id=?", rid)
} }
@ -1101,12 +1115,16 @@ func GetIssueStats(opts *IssueStatsOptions) *IssueStats {
stats := &IssueStats{} stats := &IssueStats{}
countSession := func(opts *IssueStatsOptions) *xorm.Session { countSession := func(opts *IssueStatsOptions) *xorm.Session {
sess := x.Where("issue.repo_id = ?", opts.RepoID).And("is_pull = ?", opts.IsPull) sess := x.
Where("issue.repo_id = ?", opts.RepoID).
And("is_pull = ?", opts.IsPull)
if len(opts.Labels) > 0 && opts.Labels != "0" { if len(opts.Labels) > 0 && opts.Labels != "0" {
labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ",")) labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
if len(labelIDs) > 0 { if len(labelIDs) > 0 {
sess.Join("INNER", "issue_label", "issue.id = issue_id").In("label_id", labelIDs) sess.
Join("INNER", "issue_label", "issue.id = issue_id").
In("label_id", labelIDs)
} }
} }
@ -1163,7 +1181,9 @@ func GetUserIssueStats(repoID, uid int64, repoIDs []int64, filterMode int, isPul
stats := &IssueStats{} stats := &IssueStats{}
countSession := func(isClosed, isPull bool, repoID int64, repoIDs []int64) *xorm.Session { countSession := func(isClosed, isPull bool, repoID int64, repoIDs []int64) *xorm.Session {
sess := x.Where("issue.is_closed = ?", isClosed).And("issue.is_pull = ?", isPull) sess := x.
Where("issue.is_closed = ?", isClosed).
And("issue.is_pull = ?", isPull)
if repoID > 0 || len(repoIDs) == 0 { if repoID > 0 || len(repoIDs) == 0 {
sess.And("repo_id = ?", repoID) sess.And("repo_id = ?", repoID)
@ -1203,7 +1223,8 @@ func GetUserIssueStats(repoID, uid int64, repoIDs []int64, filterMode int, isPul
// GetRepoIssueStats returns number of open and closed repository issues by given filter mode. // GetRepoIssueStats returns number of open and closed repository issues by given filter mode.
func GetRepoIssueStats(repoID, uid int64, filterMode int, isPull bool) (numOpen int64, numClosed int64) { func GetRepoIssueStats(repoID, uid int64, filterMode int, isPull bool) (numOpen int64, numClosed int64) {
countSession := func(isClosed, isPull bool, repoID int64) *xorm.Session { countSession := func(isClosed, isPull bool, repoID int64) *xorm.Session {
sess := x.Where("issue.repo_id = ?", isClosed). sess := x.
Where("issue.repo_id = ?", isClosed).
And("is_pull = ?", isPull). And("is_pull = ?", isPull).
And("repo_id = ?", repoID) And("repo_id = ?", repoID)
@ -1463,7 +1484,9 @@ func UpdateMilestone(m *Milestone) error {
} }
func countRepoMilestones(e Engine, repoID int64) int64 { func countRepoMilestones(e Engine, repoID int64) int64 {
count, _ := e.Where("repo_id=?", repoID).Count(new(Milestone)) count, _ := e.
Where("repo_id=?", repoID).
Count(new(Milestone))
return count return count
} }
@ -1473,7 +1496,9 @@ func CountRepoMilestones(repoID int64) int64 {
} }
func countRepoClosedMilestones(e Engine, repoID int64) int64 { func countRepoClosedMilestones(e Engine, repoID int64) int64 {
closed, _ := e.Where("repo_id=? AND is_closed=?", repoID, true).Count(new(Milestone)) closed, _ := e.
Where("repo_id=? AND is_closed=?", repoID, true).
Count(new(Milestone))
return closed return closed
} }
@ -1484,7 +1509,9 @@ func CountRepoClosedMilestones(repoID int64) int64 {
// MilestoneStats returns number of open and closed milestones of given repository. // MilestoneStats returns number of open and closed milestones of given repository.
func MilestoneStats(repoID int64) (open int64, closed int64) { func MilestoneStats(repoID int64) (open int64, closed int64) {
open, _ = x.Where("repo_id=? AND is_closed=?", repoID, false).Count(new(Milestone)) open, _ = x.
Where("repo_id=? AND is_closed=?", repoID, false).
Count(new(Milestone))
return open, CountRepoClosedMilestones(repoID) return open, CountRepoClosedMilestones(repoID)
} }

View file

@ -356,7 +356,9 @@ func GetCommentByID(id int64) (*Comment, error) {
func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, error) { func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, error) {
comments := make([]*Comment, 0, 10) comments := make([]*Comment, 0, 10)
sess := e.Where("issue_id = ?", issueID).Asc("created_unix") sess := e.
Where("issue_id = ?", issueID).
Asc("created_unix")
if since > 0 { if since > 0 {
sess.And("updated_unix >= ?", since) sess.And("updated_unix >= ?", since)
} }

View file

@ -138,13 +138,20 @@ func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) {
// it silently ignores label IDs that are not belong to the repository. // it silently ignores label IDs that are not belong to the repository.
func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) { func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
labels := make([]*Label, 0, len(labelIDs)) labels := make([]*Label, 0, len(labelIDs))
return labels, x.Where("repo_id = ?", repoID).In("id", base.Int64sToStrings(labelIDs)).Asc("name").Find(&labels) return labels, x.
Where("repo_id = ?", repoID).
In("id", base.Int64sToStrings(labelIDs)).
Asc("name").
Find(&labels)
} }
// GetLabelsByRepoID returns all labels that belong to given repository by ID. // GetLabelsByRepoID returns all labels that belong to given repository by ID.
func GetLabelsByRepoID(repoID int64) ([]*Label, error) { func GetLabelsByRepoID(repoID int64) ([]*Label, error) {
labels := make([]*Label, 0, 10) labels := make([]*Label, 0, 10)
return labels, x.Where("repo_id = ?", repoID).Asc("name").Find(&labels) return labels, x.
Where("repo_id = ?", repoID).
Asc("name").
Find(&labels)
} }
func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) { func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
@ -161,7 +168,11 @@ func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
} }
labels := make([]*Label, 0, len(labelIDs)) labels := make([]*Label, 0, len(labelIDs))
return labels, e.Where("id > 0").In("id", base.Int64sToStrings(labelIDs)).Asc("name").Find(&labels) return labels, e.
Where("id > 0").
In("id", base.Int64sToStrings(labelIDs)).
Asc("name").
Find(&labels)
} }
// GetLabelsByIssueID returns all labels that belong to given issue by ID. // GetLabelsByIssueID returns all labels that belong to given issue by ID.
@ -197,7 +208,9 @@ func DeleteLabel(repoID, labelID int64) error {
if _, err = sess.Id(labelID).Delete(new(Label)); err != nil { if _, err = sess.Id(labelID).Delete(new(Label)); err != nil {
return err return err
} else if _, err = sess.Where("label_id = ?", labelID).Delete(new(IssueLabel)); err != nil { } else if _, err = sess.
Where("label_id = ?", labelID).
Delete(new(IssueLabel)); err != nil {
return err return err
} }
@ -293,7 +306,10 @@ func NewIssueLabels(issue *Issue, labels []*Label) (err error) {
func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) { func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
issueLabels := make([]*IssueLabel, 0, 10) issueLabels := make([]*IssueLabel, 0, 10)
return issueLabels, e.Where("issue_id=?", issueID).Asc("label_id").Find(&issueLabels) return issueLabels, e.
Where("issue_id=?", issueID).
Asc("label_id").
Find(&issueLabels)
} }
// GetIssueLabels returns all issue-label relations of given issue by ID. // GetIssueLabels returns all issue-label relations of given issue by ID.

View file

@ -13,7 +13,6 @@ import (
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
"github.com/go-gitea/gitea/modules/base" "github.com/go-gitea/gitea/modules/base"
"github.com/go-gitea/gitea/modules/log"
) )
var ( var (
@ -50,7 +49,10 @@ func (org *User) GetOwnerTeam() (*Team, error) {
} }
func (org *User) getTeams(e Engine) error { func (org *User) getTeams(e Engine) error {
return e.Where("org_id=?", org.ID).OrderBy("CASE WHEN name LIKE '" + OWNER_TEAM + "' THEN '' ELSE name END").Find(&org.Teams) return e.
Where("org_id=?", org.ID).
OrderBy("CASE WHEN name LIKE '" + OWNER_TEAM + "' THEN '' ELSE name END").
Find(&org.Teams)
} }
// GetTeams returns all teams that belong to organization. // GetTeams returns all teams that belong to organization.
@ -181,14 +183,20 @@ func GetOrgByName(name string) (*User, error) {
// CountOrganizations returns number of organizations. // CountOrganizations returns number of organizations.
func CountOrganizations() int64 { func CountOrganizations() int64 {
count, _ := x.Where("type=1").Count(new(User)) count, _ := x.
Where("type=1").
Count(new(User))
return count return count
} }
// Organizations returns number of organizations in given page. // Organizations returns number of organizations in given page.
func Organizations(page, pageSize int) ([]*User, error) { func Organizations(page, pageSize int) ([]*User, error) {
orgs := make([]*User, 0, pageSize) orgs := make([]*User, 0, pageSize)
return orgs, x.Limit(pageSize, (page-1)*pageSize).Where("type=1").Asc("name").Find(&orgs) return orgs, x.
Limit(pageSize, (page-1)*pageSize).
Where("type=1").
Asc("name").
Find(&orgs)
} }
// DeleteOrganization completely and permanently deletes everything of organization. // DeleteOrganization completely and permanently deletes everything of organization.
@ -237,19 +245,30 @@ type OrgUser struct {
// IsOrganizationOwner returns true if given user is in the owner team. // IsOrganizationOwner returns true if given user is in the owner team.
func IsOrganizationOwner(orgId, uid int64) bool { func IsOrganizationOwner(orgId, uid int64) bool {
has, _ := x.Where("is_owner=?", true).And("uid=?", uid).And("org_id=?", orgId).Get(new(OrgUser)) has, _ := x.
Where("is_owner=?", true).
And("uid=?", uid).
And("org_id=?", orgId).
Get(new(OrgUser))
return has return has
} }
// IsOrganizationMember returns true if given user is member of organization. // IsOrganizationMember returns true if given user is member of organization.
func IsOrganizationMember(orgId, uid int64) bool { func IsOrganizationMember(orgId, uid int64) bool {
has, _ := x.Where("uid=?", uid).And("org_id=?", orgId).Get(new(OrgUser)) has, _ := x.
Where("uid=?", uid).
And("org_id=?", orgId).
Get(new(OrgUser))
return has return has
} }
// IsPublicMembership returns true if given user public his/her membership. // IsPublicMembership returns true if given user public his/her membership.
func IsPublicMembership(orgId, uid int64) bool { func IsPublicMembership(orgId, uid int64) bool {
has, _ := x.Where("uid=?", uid).And("org_id=?", orgId).And("is_public=?", true).Get(new(OrgUser)) has, _ := x.
Where("uid=?", uid).
And("org_id=?", orgId).
And("is_public=?", true).
Get(new(OrgUser))
return has return has
} }
@ -319,14 +338,19 @@ func GetOrgUsersByUserID(uid int64, all bool) ([]*OrgUser, error) {
// GetOrgUsersByOrgID returns all organization-user relations by organization ID. // GetOrgUsersByOrgID returns all organization-user relations by organization ID.
func GetOrgUsersByOrgID(orgID int64) ([]*OrgUser, error) { func GetOrgUsersByOrgID(orgID int64) ([]*OrgUser, error) {
ous := make([]*OrgUser, 0, 10) ous := make([]*OrgUser, 0, 10)
err := x.Where("org_id=?", orgID).Find(&ous) err := x.
Where("org_id=?", orgID).
Find(&ous)
return ous, err return ous, err
} }
// ChangeOrgUserStatus changes public or private membership status. // ChangeOrgUserStatus changes public or private membership status.
func ChangeOrgUserStatus(orgID, uid int64, public bool) error { func ChangeOrgUserStatus(orgID, uid int64, public bool) error {
ou := new(OrgUser) ou := new(OrgUser)
has, err := x.Where("uid=?", uid).And("org_id=?", orgID).Get(ou) has, err := x.
Where("uid=?", uid).
And("org_id=?", orgID).
Get(ou)
if err != nil { if err != nil {
return err return err
} else if !has { } else if !has {
@ -370,7 +394,10 @@ func AddOrgUser(orgID, uid int64) error {
func RemoveOrgUser(orgID, userID int64) error { func RemoveOrgUser(orgID, userID int64) error {
ou := new(OrgUser) ou := new(OrgUser)
has, err := x.Where("uid=?", userID).And("org_id=?", orgID).Get(ou) has, err := x.
Where("uid=?", userID).
And("org_id=?", orgID).
Get(ou)
if err != nil { if err != nil {
return fmt.Errorf("get org-user: %v", err) return fmt.Errorf("get org-user: %v", err)
} else if !has { } else if !has {
@ -425,7 +452,10 @@ func RemoveOrgUser(orgID, userID int64) error {
} }
if len(repoIDs) > 0 { if len(repoIDs) > 0 {
if _, err = sess.Where("user_id = ?", user.ID).In("repo_id", repoIDs).Delete(new(Access)); err != nil { if _, err = sess.
Where("user_id = ?", user.ID).
In("repo_id", repoIDs).
Delete(new(Access)); err != nil {
return err return err
} }
} }
@ -506,32 +536,29 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos
page = 1 page = 1
} }
repos := make([]*Repository, 0, pageSize) repos := make([]*Repository, 0, pageSize)
// FIXME: use XORM chain operations instead of raw SQL. if err := x.
if err = x.SQL(fmt.Sprintf(`SELECT repository.* FROM repository Select("`repository`.*").
INNER JOIN team_repo Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
ON team_repo.repo_id = repository.id Where("(`repository`.owner_id=? AND `repository`.is_private=?)", org.ID, false).
WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s) Or("team_repo.team_id IN (?)", strings.Join(base.Int64sToStrings(teamIDs), ",")).
GROUP BY repository.id GroupBy("`repository`.id").
ORDER BY updated_unix DESC OrderBy("updated_unix DESC").
LIMIT %d OFFSET %d`, Limit(pageSize, (page-1)*pageSize).
strings.Join(base.Int64sToStrings(teamIDs), ","), pageSize, (page-1)*pageSize), Find(&repos); err != nil {
org.ID, false).Find(&repos); err != nil {
return nil, 0, fmt.Errorf("get repositories: %v", err) return nil, 0, fmt.Errorf("get repositories: %v", err)
} }
results, err := x.Query(fmt.Sprintf(`SELECT repository.id FROM repository repoCount, err := x.
INNER JOIN team_repo Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
ON team_repo.repo_id = repository.id Where("(`repository`.owner_id=? AND `repository`.is_private=?)", org.ID, false).
WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s) Or("team_repo.team_id IN (?)", strings.Join(base.Int64sToStrings(teamIDs), ",")).
GROUP BY repository.id GroupBy("`repository`.id").
ORDER BY updated_unix DESC`, Count(&Repository{})
strings.Join(base.Int64sToStrings(teamIDs), ",")),
org.ID, false)
if err != nil { if err != nil {
log.Error(4, "count user repositories in organization: %v", err) return nil, 0, fmt.Errorf("count user repositories in organization: %v", err)
} }
return repos, int64(len(results)), nil return repos, repoCount, nil
} }
// GetUserRepositories returns mirror repositories of the organization // GetUserRepositories returns mirror repositories of the organization
@ -546,15 +573,12 @@ func (org *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error)
} }
repos := make([]*Repository, 0, 10) repos := make([]*Repository, 0, 10)
if err = x.SQL(fmt.Sprintf(`SELECT repository.* FROM repository return repos, x.
INNER JOIN team_repo Select("`repository`.*").
ON team_repo.repo_id = repository.id AND repository.is_mirror = ? Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id AND `repository`.is_mirror=?", true).
WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s) Where("(`repository`.owner_id=? AND `repository`.is_private=?)", org.ID, false).
GROUP BY repository.id Or("team_repo.team_id IN (?)", strings.Join(base.Int64sToStrings(teamIDs), ",")).
ORDER BY updated_unix DESC`, GroupBy("`repository`.id").
strings.Join(base.Int64sToStrings(teamIDs), ",")), OrderBy("updated_unix DESC").
true, org.ID, false).Find(&repos); err != nil { Find(&repos)
return nil, fmt.Errorf("get repositories: %v", err)
}
return repos, nil
} }

View file

@ -38,7 +38,9 @@ func (t *Team) IsMember(uid int64) bool {
func (t *Team) getRepositories(e Engine) (err error) { func (t *Team) getRepositories(e Engine) (err error) {
teamRepos := make([]*TeamRepo, 0, t.NumRepos) teamRepos := make([]*TeamRepo, 0, t.NumRepos)
if err = x.Where("team_id=?", t.ID).Find(&teamRepos); err != nil { if err = x.
Where("team_id=?", t.ID).
Find(&teamRepos); err != nil {
return fmt.Errorf("get team-repos: %v", err) return fmt.Errorf("get team-repos: %v", err)
} }
@ -225,7 +227,10 @@ func NewTeam(t *Team) (err error) {
} }
t.LowerName = strings.ToLower(t.Name) t.LowerName = strings.ToLower(t.Name)
has, err = x.Where("org_id=?", t.OrgID).And("lower_name=?", t.LowerName).Get(new(Team)) has, err = x.
Where("org_id=?", t.OrgID).
And("lower_name=?", t.LowerName).
Get(new(Team))
if err != nil { if err != nil {
return err return err
} else if has { } else if has {
@ -303,7 +308,11 @@ func UpdateTeam(t *Team, authChanged bool) (err error) {
} }
t.LowerName = strings.ToLower(t.Name) t.LowerName = strings.ToLower(t.Name)
has, err := x.Where("org_id=?", t.OrgID).And("lower_name=?", t.LowerName).And("id!=?", t.ID).Get(new(Team)) has, err := x.
Where("org_id=?", t.OrgID).
And("lower_name=?", t.LowerName).
And("id!=?", t.ID).
Get(new(Team))
if err != nil { if err != nil {
return err return err
} else if has { } else if has {
@ -357,7 +366,10 @@ func DeleteTeam(t *Team) error {
} }
// Delete team-user. // Delete team-user.
if _, err = sess.Where("org_id=?", org.ID).Where("team_id=?", t.ID).Delete(new(TeamUser)); err != nil { if _, err = sess.
Where("org_id=?", org.ID).
Where("team_id=?", t.ID).
Delete(new(TeamUser)); err != nil {
return err return err
} }
@ -389,7 +401,11 @@ type TeamUser struct {
} }
func isTeamMember(e Engine, orgID, teamID, uid int64) bool { func isTeamMember(e Engine, orgID, teamID, uid int64) bool {
has, _ := e.Where("org_id=?", orgID).And("team_id=?", teamID).And("uid=?", uid).Get(new(TeamUser)) has, _ := e.
Where("org_id=?", orgID).
And("team_id=?", teamID).
And("uid=?", uid).
Get(new(TeamUser))
return has return has
} }
@ -400,7 +416,9 @@ func IsTeamMember(orgID, teamID, uid int64) bool {
func getTeamMembers(e Engine, teamID int64) (_ []*User, err error) { func getTeamMembers(e Engine, teamID int64) (_ []*User, err error) {
teamUsers := make([]*TeamUser, 0, 10) teamUsers := make([]*TeamUser, 0, 10)
if err = e.Where("team_id=?", teamID).Find(&teamUsers); err != nil { if err = e.
Where("team_id=?", teamID).
Find(&teamUsers); err != nil {
return nil, fmt.Errorf("get team-users: %v", err) return nil, fmt.Errorf("get team-users: %v", err)
} }
members := make([]*User, 0, len(teamUsers)) members := make([]*User, 0, len(teamUsers))
@ -421,7 +439,10 @@ func GetTeamMembers(teamID int64) ([]*User, error) {
func getUserTeams(e Engine, orgId, uid int64) ([]*Team, error) { func getUserTeams(e Engine, orgId, uid int64) ([]*Team, error) {
tus := make([]*TeamUser, 0, 5) tus := make([]*TeamUser, 0, 5)
if err := e.Where("uid=?", uid).And("org_id=?", orgId).Find(&tus); err != nil { if err := e.
Where("uid=?", uid).
And("org_id=?", orgId).
Find(&tus); err != nil {
return nil, err return nil, err
} }
@ -492,7 +513,10 @@ func AddTeamMember(orgID, teamID, uid int64) error {
// We make sure it exists before. // We make sure it exists before.
ou := new(OrgUser) ou := new(OrgUser)
if _, err = sess.Where("uid = ?", uid).And("org_id = ?", orgID).Get(ou); err != nil { if _, err = sess.
Where("uid = ?", uid).
And("org_id = ?", orgID).
Get(ou); err != nil {
return err return err
} }
ou.NumTeams++ ou.NumTeams++
@ -541,7 +565,10 @@ func removeTeamMember(e Engine, orgID, teamID, uid int64) error {
} }
if _, err := e.Delete(tu); err != nil { if _, err := e.Delete(tu); err != nil {
return err return err
} else if _, err = e.Id(t.ID).AllCols().Update(t); err != nil { } else if _, err = e.
Id(t.ID).
AllCols().
Update(t); err != nil {
return err return err
} }
@ -554,7 +581,10 @@ func removeTeamMember(e Engine, orgID, teamID, uid int64) error {
// This must exist. // This must exist.
ou := new(OrgUser) ou := new(OrgUser)
_, err = e.Where("uid = ?", uid).And("org_id = ?", org.ID).Get(ou) _, err = e.
Where("uid = ?", uid).
And("org_id = ?", org.ID).
Get(ou)
if err != nil { if err != nil {
return err return err
} }
@ -562,7 +592,10 @@ func removeTeamMember(e Engine, orgID, teamID, uid int64) error {
if t.IsOwnerTeam() { if t.IsOwnerTeam() {
ou.IsOwner = false ou.IsOwner = false
} }
if _, err = e.Id(ou.ID).AllCols().Update(ou); err != nil { if _, err = e.
Id(ou.ID).
AllCols().
Update(ou); err != nil {
return err return err
} }
return nil return nil
@ -597,7 +630,11 @@ type TeamRepo struct {
} }
func hasTeamRepo(e Engine, orgID, teamID, repoID int64) bool { func hasTeamRepo(e Engine, orgID, teamID, repoID int64) bool {
has, _ := e.Where("org_id=?", orgID).And("team_id=?", teamID).And("repo_id=?", repoID).Get(new(TeamRepo)) has, _ := e.
Where("org_id=?", orgID).
And("team_id=?", teamID).
And("repo_id=?", repoID).
Get(new(TeamRepo))
return has return has
} }

View file

@ -12,13 +12,13 @@ import (
"time" "time"
"github.com/Unknwon/com" "github.com/Unknwon/com"
"github.com/go-gitea/git"
"github.com/go-gitea/gitea/modules/log" "github.com/go-gitea/gitea/modules/log"
"github.com/go-gitea/gitea/modules/process" "github.com/go-gitea/gitea/modules/process"
"github.com/go-gitea/gitea/modules/setting" "github.com/go-gitea/gitea/modules/setting"
"github.com/go-gitea/gitea/modules/sync" "github.com/go-gitea/gitea/modules/sync"
"github.com/go-xorm/xorm"
"github.com/go-gitea/git"
api "github.com/go-gitea/go-sdk/gitea" api "github.com/go-gitea/go-sdk/gitea"
"github.com/go-xorm/xorm"
) )
var PullRequestQueue = sync.NewUniqueQueue(setting.Repository.PullRequestQueueLength) var PullRequestQueue = sync.NewUniqueQueue(setting.Repository.PullRequestQueueLength)
@ -462,9 +462,11 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
// by given head/base and repo/branch. // by given head/base and repo/branch.
func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch string) (*PullRequest, error) { func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch string) (*PullRequest, error) {
pr := new(PullRequest) pr := new(PullRequest)
has, err := x.Where("head_repo_id=? AND head_branch=? AND base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?", has, err := x.
Where("head_repo_id=? AND head_branch=? AND base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
headRepoID, headBranch, baseRepoID, baseBranch, false, false). headRepoID, headBranch, baseRepoID, baseBranch, false, false).
Join("INNER", "issue", "issue.id=pull_request.issue_id").Get(pr) Join("INNER", "issue", "issue.id=pull_request.issue_id").
Get(pr)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
@ -478,18 +480,22 @@ func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch
// by given head information (repo and branch). // by given head information (repo and branch).
func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequest, error) { func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequest, error) {
prs := make([]*PullRequest, 0, 2) prs := make([]*PullRequest, 0, 2)
return prs, x.Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ?", return prs, x.
Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ?",
repoID, branch, false, false). repoID, branch, false, false).
Join("INNER", "issue", "issue.id = pull_request.issue_id").Find(&prs) Join("INNER", "issue", "issue.id = pull_request.issue_id").
Find(&prs)
} }
// GetUnmergedPullRequestsByBaseInfo returnss all pull requests that are open and has not been merged // GetUnmergedPullRequestsByBaseInfo returnss all pull requests that are open and has not been merged
// by given base information (repo and branch). // by given base information (repo and branch).
func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) { func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
prs := make([]*PullRequest, 0, 2) prs := make([]*PullRequest, 0, 2)
return prs, x.Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?", return prs, x.
Where("base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
repoID, branch, false, false). repoID, branch, false, false).
Join("INNER", "issue", "issue.id=pull_request.issue_id").Find(&prs) Join("INNER", "issue", "issue.id=pull_request.issue_id").
Find(&prs)
} }
func getPullRequestByID(e Engine, id int64) (*PullRequest, error) { func getPullRequestByID(e Engine, id int64) (*PullRequest, error) {
@ -638,7 +644,10 @@ func (prs PullRequestList) loadAttributes(e Engine) error {
issueIDs = append(issueIDs, prs[i].IssueID) issueIDs = append(issueIDs, prs[i].IssueID)
} }
issues := make([]*Issue, 0, len(issueIDs)) issues := make([]*Issue, 0, len(issueIDs))
if err := e.Where("id > 0").In("id", issueIDs).Find(&issues); err != nil { if err := e.
Where("id > 0").
In("id", issueIDs).
Find(&issues); err != nil {
return fmt.Errorf("find issues: %v", err) return fmt.Errorf("find issues: %v", err)
} }
@ -725,7 +734,10 @@ func ChangeUsernameInPullRequests(oldUserName, newUserName string) error {
pr := PullRequest{ pr := PullRequest{
HeadUserName: strings.ToLower(newUserName), HeadUserName: strings.ToLower(newUserName),
} }
_, err := x.Cols("head_user_name").Where("head_user_name = ?", strings.ToLower(oldUserName)).Update(pr) _, err := x.
Cols("head_user_name").
Where("head_user_name = ?", strings.ToLower(oldUserName)).
Update(pr)
return err return err
} }

View file

@ -127,7 +127,9 @@ func GetRelease(repoID int64, tagName string) (*Release, error) {
// GetReleaseByID returns release with given ID. // GetReleaseByID returns release with given ID.
func GetReleaseByID(id int64) (*Release, error) { func GetReleaseByID(id int64) (*Release, error) {
rel := new(Release) rel := new(Release)
has, err := x.Id(id).Get(rel) has, err := x.
Id(id).
Get(rel)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
@ -142,7 +144,10 @@ func GetReleasesByRepoID(repoID int64, page, pageSize int) (rels []*Release, err
if page <= 0 { if page <= 0 {
page = 1 page = 1
} }
err = x.Desc("created_unix").Limit(pageSize, (page-1)*pageSize).Find(&rels, Release{RepoID: repoID}) err = x.
Desc("created_unix").
Limit(pageSize, (page-1)*pageSize).
Find(&rels, Release{RepoID: repoID})
return rels, err return rels, err
} }

View file

@ -21,15 +21,15 @@ import (
"github.com/Unknwon/cae/zip" "github.com/Unknwon/cae/zip"
"github.com/Unknwon/com" "github.com/Unknwon/com"
"github.com/go-gitea/git"
"github.com/go-gitea/gitea/modules/bindata" "github.com/go-gitea/gitea/modules/bindata"
"github.com/go-gitea/gitea/modules/log" "github.com/go-gitea/gitea/modules/log"
"github.com/go-gitea/gitea/modules/markdown" "github.com/go-gitea/gitea/modules/markdown"
"github.com/go-gitea/gitea/modules/process" "github.com/go-gitea/gitea/modules/process"
"github.com/go-gitea/gitea/modules/setting" "github.com/go-gitea/gitea/modules/setting"
"github.com/go-gitea/gitea/modules/sync" "github.com/go-gitea/gitea/modules/sync"
"github.com/go-xorm/xorm"
"github.com/go-gitea/git"
api "github.com/go-gitea/go-sdk/gitea" api "github.com/go-gitea/go-sdk/gitea"
"github.com/go-xorm/xorm"
version "github.com/mcuadros/go-version" version "github.com/mcuadros/go-version"
ini "gopkg.in/ini.v1" ini "gopkg.in/ini.v1"
) )
@ -334,7 +334,9 @@ func (repo *Repository) getAssignees(e Engine) (_ []*User, err error) {
} }
accesses := make([]*Access, 0, 10) accesses := make([]*Access, 0, 10)
if err = e.Where("repo_id = ? AND mode >= ?", repo.ID, AccessModeWrite).Find(&accesses); err != nil { if err = e.
Where("repo_id = ? AND mode >= ?", repo.ID, AccessModeWrite).
Find(&accesses); err != nil {
return nil, err return nil, err
} }
@ -1209,7 +1211,9 @@ func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error)
func getRepositoriesByForkID(e Engine, forkID int64) ([]*Repository, error) { func getRepositoriesByForkID(e Engine, forkID int64) ([]*Repository, error) {
repos := make([]*Repository, 0, 10) repos := make([]*Repository, 0, 10)
return repos, e.Where("fork_id=?", forkID).Find(&repos) return repos, e.
Where("fork_id=?", forkID).
Find(&repos)
} }
// GetRepositoriesByForkID returns all repositories with given fork ID. // GetRepositoriesByForkID returns all repositories with given fork ID.
@ -1341,7 +1345,9 @@ func DeleteRepository(uid, repoID int64) error {
// Delete comments and attachments. // Delete comments and attachments.
issues := make([]*Issue, 0, 25) issues := make([]*Issue, 0, 25)
attachmentPaths := make([]string, 0, len(issues)) attachmentPaths := make([]string, 0, len(issues))
if err = sess.Where("repo_id=?", repoID).Find(&issues); err != nil { if err = sess.
Where("repo_id=?", repoID).
Find(&issues); err != nil {
return err return err
} }
for i := range issues { for i := range issues {
@ -1350,7 +1356,9 @@ func DeleteRepository(uid, repoID int64) error {
} }
attachments := make([]*Attachment, 0, 5) attachments := make([]*Attachment, 0, 5)
if err = sess.Where("issue_id=?", issues[i].ID).Find(&attachments); err != nil { if err = sess.
Where("issue_id=?", issues[i].ID).
Find(&attachments); err != nil {
return err return err
} }
for j := range attachments { for j := range attachments {
@ -1450,7 +1458,9 @@ func GetRepositoryByID(id int64) (*Repository, error) {
// GetUserRepositories returns a list of repositories of given user. // GetUserRepositories returns a list of repositories of given user.
func GetUserRepositories(userID int64, private bool, page, pageSize int) ([]*Repository, error) { func GetUserRepositories(userID int64, private bool, page, pageSize int) ([]*Repository, error) {
sess := x.Where("owner_id = ?", userID).Desc("updated_unix") sess := x.
Where("owner_id = ?", userID).
Desc("updated_unix")
if !private { if !private {
sess.And("is_private=?", false) sess.And("is_private=?", false)
} }
@ -1467,13 +1477,20 @@ func GetUserRepositories(userID int64, private bool, page, pageSize int) ([]*Rep
// GetUserRepositories returns a list of mirror repositories of given user. // GetUserRepositories returns a list of mirror repositories of given user.
func GetUserMirrorRepositories(userID int64) ([]*Repository, error) { func GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
repos := make([]*Repository, 0, 10) repos := make([]*Repository, 0, 10)
return repos, x.Where("owner_id = ?", userID).And("is_mirror = ?", true).Find(&repos) return repos, x.
Where("owner_id = ?", userID).
And("is_mirror = ?", true).
Find(&repos)
} }
// GetRecentUpdatedRepositories returns the list of repositories that are recently updated. // GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
func GetRecentUpdatedRepositories(page, pageSize int) (repos []*Repository, err error) { func GetRecentUpdatedRepositories(page, pageSize int) (repos []*Repository, err error) {
return repos, x.Limit(pageSize, (page-1)*pageSize). return repos, x.
Where("is_private=?", false).Limit(pageSize).Desc("updated_unix").Find(&repos) Limit(pageSize, (page-1)*pageSize).
Where("is_private=?", false).
Limit(pageSize).
Desc("updated_unix").
Find(&repos)
} }
func getRepositoryCount(e Engine, u *User) (int64, error) { func getRepositoryCount(e Engine, u *User) (int64, error) {
@ -1532,7 +1549,9 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int
// DeleteRepositoryArchives deletes all repositories' archives. // DeleteRepositoryArchives deletes all repositories' archives.
func DeleteRepositoryArchives() error { func DeleteRepositoryArchives() error {
return x.Where("id > 0").Iterate(new(Repository), return x.
Where("id > 0").
Iterate(new(Repository),
func(idx int, bean interface{}) error { func(idx int, bean interface{}) error {
repo := bean.(*Repository) repo := bean.(*Repository)
return os.RemoveAll(filepath.Join(repo.RepoPath(), "archives")) return os.RemoveAll(filepath.Join(repo.RepoPath(), "archives"))
@ -1541,7 +1560,9 @@ func DeleteRepositoryArchives() error {
func gatherMissingRepoRecords() ([]*Repository, error) { func gatherMissingRepoRecords() ([]*Repository, error) {
repos := make([]*Repository, 0, 10) repos := make([]*Repository, 0, 10)
if err := x.Where("id > 0").Iterate(new(Repository), if err := x.
Where("id > 0").
Iterate(new(Repository),
func(idx int, bean interface{}) error { func(idx int, bean interface{}) error {
repo := bean.(*Repository) repo := bean.(*Repository)
if !com.IsDir(repo.RepoPath()) { if !com.IsDir(repo.RepoPath()) {
@ -1602,7 +1623,9 @@ func ReinitMissingRepositories() error {
// RewriteRepositoryUpdateHook rewrites all repositories' update hook. // RewriteRepositoryUpdateHook rewrites all repositories' update hook.
func RewriteRepositoryUpdateHook() error { func RewriteRepositoryUpdateHook() error {
return x.Where("id > 0").Iterate(new(Repository), return x.
Where("id > 0").
Iterate(new(Repository),
func(idx int, bean interface{}) error { func(idx int, bean interface{}) error {
repo := bean.(*Repository) repo := bean.(*Repository)
return createUpdateHook(repo.RepoPath()) return createUpdateHook(repo.RepoPath())
@ -1628,7 +1651,9 @@ func GitFsck() {
log.Trace("Doing: GitFsck") log.Trace("Doing: GitFsck")
if err := x.Where("id>0").Iterate(new(Repository), if err := x.
Where("id>0").
Iterate(new(Repository),
func(idx int, bean interface{}) error { func(idx int, bean interface{}) error {
repo := bean.(*Repository) repo := bean.(*Repository)
repoPath := repo.RepoPath() repoPath := repo.RepoPath()
@ -1647,7 +1672,9 @@ func GitFsck() {
func GitGcRepos() error { func GitGcRepos() error {
args := append([]string{"gc"}, setting.Git.GCArgs...) args := append([]string{"gc"}, setting.Git.GCArgs...)
return x.Where("id > 0").Iterate(new(Repository), return x.
Where("id > 0").
Iterate(new(Repository),
func(idx int, bean interface{}) error { func(idx int, bean interface{}) error {
repo := bean.(*Repository) repo := bean.(*Repository)
if err := repo.GetOwner(); err != nil { if err := repo.GetOwner(); err != nil {
@ -1796,7 +1823,10 @@ func (repos RepositoryList) loadAttributes(e Engine) error {
userIDs = append(userIDs, userID) userIDs = append(userIDs, userID)
} }
users := make([]*User, 0, len(userIDs)) users := make([]*User, 0, len(userIDs))
if err := e.Where("id > 0").In("id", userIDs).Find(&users); err != nil { if err := e.
Where("id > 0").
In("id", userIDs).
Find(&users); err != nil {
return fmt.Errorf("find users: %v", err) return fmt.Errorf("find users: %v", err)
} }
for i := range users { for i := range users {
@ -1829,7 +1859,10 @@ func (repos MirrorRepositoryList) loadAttributes(e Engine) error {
repoIDs = append(repoIDs, repos[i].ID) repoIDs = append(repoIDs, repos[i].ID)
} }
mirrors := make([]*Mirror, 0, len(repoIDs)) mirrors := make([]*Mirror, 0, len(repoIDs))
if err := e.Where("id > 0").In("repo_id", repoIDs).Find(&mirrors); err != nil { if err := e.
Where("id > 0").
In("repo_id", repoIDs).
Find(&mirrors); err != nil {
return fmt.Errorf("find mirrors: %v", err) return fmt.Errorf("find mirrors: %v", err)
} }
@ -1910,7 +1943,9 @@ func GetWatchers(repoID int64) ([]*Watch, error) {
// Repository.GetWatchers returns range of users watching given repository. // Repository.GetWatchers returns range of users watching given repository.
func (repo *Repository) GetWatchers(page int) ([]*User, error) { func (repo *Repository) GetWatchers(page int) ([]*User, error) {
users := make([]*User, 0, ItemsPerPage) users := make([]*User, 0, ItemsPerPage)
sess := x.Limit(ItemsPerPage, (page-1)*ItemsPerPage).Where("watch.repo_id=?", repo.ID) sess := x.
Limit(ItemsPerPage, (page-1)*ItemsPerPage).
Where("watch.repo_id=?", repo.ID)
if setting.UsePostgreSQL { if setting.UsePostgreSQL {
sess = sess.Join("LEFT", "watch", `"user".id=watch.user_id`) sess = sess.Join("LEFT", "watch", `"user".id=watch.user_id`)
} else { } else {
@ -1998,7 +2033,9 @@ func IsStaring(userID, repoID int64) bool {
func (repo *Repository) GetStargazers(page int) ([]*User, error) { func (repo *Repository) GetStargazers(page int) ([]*User, error) {
users := make([]*User, 0, ItemsPerPage) users := make([]*User, 0, ItemsPerPage)
sess := x.Limit(ItemsPerPage, (page-1)*ItemsPerPage).Where("star.repo_id=?", repo.ID) sess := x.
Limit(ItemsPerPage, (page-1)*ItemsPerPage).
Where("star.repo_id=?", repo.ID)
if setting.UsePostgreSQL { if setting.UsePostgreSQL {
sess = sess.Join("LEFT", "star", `"user".id=star.uid`) sess = sess.Join("LEFT", "star", `"user".id=star.uid`)
} else { } else {
@ -2017,7 +2054,9 @@ func (repo *Repository) GetStargazers(page int) ([]*User, error) {
// HasForkedRepo checks if given user has already forked a repository with given ID. // HasForkedRepo checks if given user has already forked a repository with given ID.
func HasForkedRepo(ownerID, repoID int64) (*Repository, bool) { func HasForkedRepo(ownerID, repoID int64) (*Repository, bool) {
repo := new(Repository) repo := new(Repository)
has, _ := x.Where("owner_id=? AND fork_id=?", ownerID, repoID).Get(repo) has, _ := x.
Where("owner_id=? AND fork_id=?", ownerID, repoID).
Get(repo)
return repo, has return repo, has
} }

View file

@ -131,7 +131,10 @@ func (repo *Repository) ChangeCollaborationAccessMode(uid int64, mode AccessMode
return err return err
} }
if _, err = sess.Id(collaboration.ID).AllCols().Update(collaboration); err != nil { if _, err = sess.
Id(collaboration.ID).
AllCols().
Update(collaboration); err != nil {
return fmt.Errorf("update collaboration: %v", err) return fmt.Errorf("update collaboration: %v", err)
} else if _, err = sess.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil { } else if _, err = sess.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil {
return fmt.Errorf("update access table: %v", err) return fmt.Errorf("update access table: %v", err)

View file

@ -387,7 +387,9 @@ func DeleteUploads(uploads ...*Upload) (err error) {
for i := 0; i < len(uploads); i++ { for i := 0; i < len(uploads); i++ {
ids[i] = uploads[i].ID ids[i] = uploads[i].ID
} }
if _, err = sess.In("id", ids).Delete(new(Upload)); err != nil { if _, err = sess.
In("id", ids).
Delete(new(Upload)); err != nil {
return fmt.Errorf("delete uploads: %v", err) return fmt.Errorf("delete uploads: %v", err)
} }

View file

@ -199,7 +199,9 @@ func MirrorUpdate() {
log.Trace("Doing: MirrorUpdate") log.Trace("Doing: MirrorUpdate")
if err := x.Where("next_update_unix<=?", time.Now().Unix()).Iterate(new(Mirror), func(idx int, bean interface{}) error { if err := x.
Where("next_update_unix<=?", time.Now().Unix()).
Iterate(new(Mirror), func(idx int, bean interface{}) error {
m := bean.(*Mirror) m := bean.(*Mirror)
if m.Repo == nil { if m.Repo == nil {
log.Error(4, "Disconnected mirror repository found: %d", m.ID) log.Error(4, "Disconnected mirror repository found: %d", m.ID)

View file

@ -398,7 +398,9 @@ func AddPublicKey(ownerID int64, name, content string) (*PublicKey, error) {
} }
// Key name of same user cannot be duplicated. // Key name of same user cannot be duplicated.
has, err := x.Where("owner_id = ? AND name = ?", ownerID, name).Get(new(PublicKey)) has, err := x.
Where("owner_id = ? AND name = ?", ownerID, name).
Get(new(PublicKey))
if err != nil { if err != nil {
return nil, err return nil, err
} else if has { } else if has {
@ -428,7 +430,9 @@ func AddPublicKey(ownerID int64, name, content string) (*PublicKey, error) {
// GetPublicKeyByID returns public key by given ID. // GetPublicKeyByID returns public key by given ID.
func GetPublicKeyByID(keyID int64) (*PublicKey, error) { func GetPublicKeyByID(keyID int64) (*PublicKey, error) {
key := new(PublicKey) key := new(PublicKey)
has, err := x.Id(keyID).Get(key) has, err := x.
Id(keyID).
Get(key)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
@ -441,7 +445,9 @@ func GetPublicKeyByID(keyID int64) (*PublicKey, error) {
// and returns public key found. // and returns public key found.
func SearchPublicKeyByContent(content string) (*PublicKey, error) { func SearchPublicKeyByContent(content string) (*PublicKey, error) {
key := new(PublicKey) key := new(PublicKey)
has, err := x.Where("content like ?", content+"%").Get(key) has, err := x.
Where("content like ?", content+"%").
Get(key)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
@ -453,7 +459,9 @@ func SearchPublicKeyByContent(content string) (*PublicKey, error) {
// ListPublicKeys returns a list of public keys belongs to given user. // ListPublicKeys returns a list of public keys belongs to given user.
func ListPublicKeys(uid int64) ([]*PublicKey, error) { func ListPublicKeys(uid int64) ([]*PublicKey, error) {
keys := make([]*PublicKey, 0, 5) keys := make([]*PublicKey, 0, 5)
return keys, x.Where("owner_id = ?", uid).Find(&keys) return keys, x.
Where("owner_id = ?", uid).
Find(&keys)
} }
// UpdatePublicKey updates given public key. // UpdatePublicKey updates given public key.
@ -595,14 +603,18 @@ func (k *DeployKey) GetContent() error {
func checkDeployKey(e Engine, keyID, repoID int64, name string) error { func checkDeployKey(e Engine, keyID, repoID int64, name string) error {
// Note: We want error detail, not just true or false here. // Note: We want error detail, not just true or false here.
has, err := e.Where("key_id = ? AND repo_id = ?", keyID, repoID).Get(new(DeployKey)) has, err := e.
Where("key_id = ? AND repo_id = ?", keyID, repoID).
Get(new(DeployKey))
if err != nil { if err != nil {
return err return err
} else if has { } else if has {
return ErrDeployKeyAlreadyExist{keyID, repoID} return ErrDeployKeyAlreadyExist{keyID, repoID}
} }
has, err = e.Where("repo_id = ? AND name = ?", repoID, name).Get(new(DeployKey)) has, err = e.
Where("repo_id = ? AND name = ?", repoID, name).
Get(new(DeployKey))
if err != nil { if err != nil {
return err return err
} else if has { } else if has {
@ -630,7 +642,9 @@ func addDeployKey(e *xorm.Session, keyID, repoID int64, name, fingerprint string
// HasDeployKey returns true if public key is a deploy key of given repository. // HasDeployKey returns true if public key is a deploy key of given repository.
func HasDeployKey(keyID, repoID int64) bool { func HasDeployKey(keyID, repoID int64) bool {
has, _ := x.Where("key_id = ? AND repo_id = ?", keyID, repoID).Get(new(DeployKey)) has, _ := x.
Where("key_id = ? AND repo_id = ?", keyID, repoID).
Get(new(DeployKey))
return has return has
} }
@ -739,7 +753,9 @@ func DeleteDeployKey(doer *User, id int64) error {
} }
// Check if this is the last reference to same key content. // Check if this is the last reference to same key content.
has, err := sess.Where("key_id = ?", key.KeyID).Get(new(DeployKey)) has, err := sess.
Where("key_id = ?", key.KeyID).
Get(new(DeployKey))
if err != nil { if err != nil {
return err return err
} else if !has { } else if !has {
@ -754,5 +770,7 @@ func DeleteDeployKey(doer *User, id int64) error {
// ListDeployKeys returns all deploy keys by given repository ID. // ListDeployKeys returns all deploy keys by given repository ID.
func ListDeployKeys(repoID int64) ([]*DeployKey, error) { func ListDeployKeys(repoID int64) ([]*DeployKey, error) {
keys := make([]*DeployKey, 0, 5) keys := make([]*DeployKey, 0, 5)
return keys, x.Where("repo_id = ?", repoID).Find(&keys) return keys, x.
Where("repo_id = ?", repoID).
Find(&keys)
} }

View file

@ -72,7 +72,10 @@ func GetAccessTokenBySHA(sha string) (*AccessToken, error) {
// ListAccessTokens returns a list of access tokens belongs to given user. // ListAccessTokens returns a list of access tokens belongs to given user.
func ListAccessTokens(uid int64) ([]*AccessToken, error) { func ListAccessTokens(uid int64) ([]*AccessToken, error) {
tokens := make([]*AccessToken, 0, 5) tokens := make([]*AccessToken, 0, 5)
return tokens, x.Where("uid=?", uid).Desc("id").Find(&tokens) return tokens, x.
Where("uid=?", uid).
Desc("id").
Find(&tokens)
} }
// UpdateAccessToken updates information of access token. // UpdateAccessToken updates information of access token.

View file

@ -288,7 +288,9 @@ func (u *User) AvatarLink() string {
// User.GetFollwoers returns range of user's followers. // User.GetFollwoers returns range of user's followers.
func (u *User) GetFollowers(page int) ([]*User, error) { func (u *User) GetFollowers(page int) ([]*User, error) {
users := make([]*User, 0, ItemsPerPage) users := make([]*User, 0, ItemsPerPage)
sess := x.Limit(ItemsPerPage, (page-1)*ItemsPerPage).Where("follow.follow_id=?", u.ID) sess := x.
Limit(ItemsPerPage, (page-1)*ItemsPerPage).
Where("follow.follow_id=?", u.ID)
if setting.UsePostgreSQL { if setting.UsePostgreSQL {
sess = sess.Join("LEFT", "follow", `"user".id=follow.user_id`) sess = sess.Join("LEFT", "follow", `"user".id=follow.user_id`)
} else { } else {
@ -304,7 +306,9 @@ func (u *User) IsFollowing(followID int64) bool {
// GetFollowing returns range of user's following. // GetFollowing returns range of user's following.
func (u *User) GetFollowing(page int) ([]*User, error) { func (u *User) GetFollowing(page int) ([]*User, error) {
users := make([]*User, 0, ItemsPerPage) users := make([]*User, 0, ItemsPerPage)
sess := x.Limit(ItemsPerPage, (page-1)*ItemsPerPage).Where("follow.user_id=?", u.ID) sess := x.
Limit(ItemsPerPage, (page-1)*ItemsPerPage).
Where("follow.user_id=?", u.ID)
if setting.UsePostgreSQL { if setting.UsePostgreSQL {
sess = sess.Join("LEFT", "follow", `"user".id=follow.follow_id`) sess = sess.Join("LEFT", "follow", `"user".id=follow.follow_id`)
} else { } else {
@ -416,7 +420,9 @@ func (u *User) IsPublicMember(orgId int64) bool {
} }
func (u *User) getOrganizationCount(e Engine) (int64, error) { func (u *User) getOrganizationCount(e Engine) (int64, error) {
return e.Where("uid=?", u.ID).Count(new(OrgUser)) return e.
Where("uid=?", u.ID).
Count(new(OrgUser))
} }
// GetOrganizationCount returns count of membership of organization of user. // GetOrganizationCount returns count of membership of organization of user.
@ -479,7 +485,9 @@ func IsUserExist(uid int64, name string) (bool, error) {
if len(name) == 0 { if len(name) == 0 {
return false, nil return false, nil
} }
return x.Where("id!=?", uid).Get(&User{LowerName: strings.ToLower(name)}) return x.
Where("id!=?", uid).
Get(&User{LowerName: strings.ToLower(name)})
} }
// GetUserSalt returns a ramdom user salt token. // GetUserSalt returns a ramdom user salt token.
@ -575,7 +583,9 @@ func CreateUser(u *User) (err error) {
} }
func countUsers(e Engine) int64 { func countUsers(e Engine) int64 {
count, _ := e.Where("type=0").Count(new(User)) count, _ := e.
Where("type=0").
Count(new(User))
return count return count
} }
@ -587,7 +597,11 @@ func CountUsers() int64 {
// Users returns number of users in given page. // Users returns number of users in given page.
func Users(page, pageSize int) ([]*User, error) { func Users(page, pageSize int) ([]*User, error) {
users := make([]*User, 0, pageSize) users := make([]*User, 0, pageSize)
return users, x.Limit(pageSize, (page-1)*pageSize).Where("type=0").Asc("name").Find(&users) return users, x.
Limit(pageSize, (page-1)*pageSize).
Where("type=0").
Asc("name").
Find(&users)
} }
// get user by erify code // get user by erify code
@ -661,7 +675,9 @@ func ChangeUserName(u *User, newUserName string) (err error) {
} }
// Delete all local copies of repository wiki that user owns. // Delete all local copies of repository wiki that user owns.
if err = x.Where("owner_id=?", u.ID).Iterate(new(Repository), func(idx int, bean interface{}) error { if err = x.
Where("owner_id=?", u.ID).
Iterate(new(Repository), func(idx int, bean interface{}) error {
repo := bean.(*Repository) repo := bean.(*Repository)
RemoveAllWithNotice("Delete repository wiki local copy", repo.LocalWikiPath()) RemoveAllWithNotice("Delete repository wiki local copy", repo.LocalWikiPath())
return nil return nil
@ -676,7 +692,11 @@ func updateUser(e Engine, u *User) error {
// Organization does not need email // Organization does not need email
if !u.IsOrganization() { if !u.IsOrganization() {
u.Email = strings.ToLower(u.Email) u.Email = strings.ToLower(u.Email)
has, err := e.Where("id!=?", u.ID).And("type=?", u.Type).And("email=?", u.Email).Get(new(User)) has, err := e.
Where("id!=?", u.ID).
And("type=?", u.Type).
And("email=?", u.Email).
Get(new(User))
if err != nil { if err != nil {
return err return err
} else if has { } else if has {
@ -843,7 +863,9 @@ func DeleteUser(u *User) (err error) {
// DeleteInactivateUsers deletes all inactivate users and email addresses. // DeleteInactivateUsers deletes all inactivate users and email addresses.
func DeleteInactivateUsers() (err error) { func DeleteInactivateUsers() (err error) {
users := make([]*User, 0, 10) users := make([]*User, 0, 10)
if err = x.Where("is_active = ?", false).Find(&users); err != nil { if err = x.
Where("is_active = ?", false).
Find(&users); err != nil {
return fmt.Errorf("get all inactive users: %v", err) return fmt.Errorf("get all inactive users: %v", err)
} }
// FIXME: should only update authorized_keys file once after all deletions. // FIXME: should only update authorized_keys file once after all deletions.
@ -857,7 +879,9 @@ func DeleteInactivateUsers() (err error) {
} }
} }
_, err = x.Where("is_activated = ?", false).Delete(new(EmailAddress)) _, err = x.
Where("is_activated = ?", false).
Delete(new(EmailAddress))
return err return err
} }
@ -868,13 +892,10 @@ func UserPath(userName string) string {
func GetUserByKeyID(keyID int64) (*User, error) { func GetUserByKeyID(keyID int64) (*User, error) {
user := new(User) user := new(User)
has, err := x.SQL("SELECT a.* FROM `user` AS a, public_key AS b WHERE a.id = b.owner_id AND b.id=?", keyID).Get(user) return user, x.
if err != nil { Join("INNER", "public_key", "`public_key`.owner_id = `user`.id").
return nil, err Where("`public_key`.id=?", keyID).
} else if !has { Find(user)
return nil, ErrUserNotKeyOwner
}
return user, nil
} }
func getUserByID(e Engine, id int64) (*User, error) { func getUserByID(e Engine, id int64) (*User, error) {
@ -935,7 +956,10 @@ func GetUserEmailsByNames(names []string) []string {
// GetUsersByIDs returns all resolved users from a list of Ids. // GetUsersByIDs returns all resolved users from a list of Ids.
func GetUsersByIDs(ids []int64) ([]*User, error) { func GetUsersByIDs(ids []int64) ([]*User, error) {
ous := make([]*User, 0, len(ids)) ous := make([]*User, 0, len(ids))
err := x.In("id", ids).Asc("name").Find(&ous) err := x.
In("id", ids).
Asc("name").
Find(&ous)
return ous, err return ous, err
} }
@ -1050,7 +1074,8 @@ func SearchUserByName(opts *SearchUserOptions) (users []*User, _ int64, _ error)
searchQuery := "%" + opts.Keyword + "%" searchQuery := "%" + opts.Keyword + "%"
users = make([]*User, 0, opts.PageSize) users = make([]*User, 0, opts.PageSize)
// Append conditions // Append conditions
sess := x.Where("LOWER(lower_name) LIKE ?", searchQuery). sess := x.
Where("LOWER(lower_name) LIKE ?", searchQuery).
Or("LOWER(full_name) LIKE ?", searchQuery). Or("LOWER(full_name) LIKE ?", searchQuery).
And("type = ?", opts.Type) And("type = ?", opts.Type)
@ -1064,7 +1089,9 @@ func SearchUserByName(opts *SearchUserOptions) (users []*User, _ int64, _ error)
if len(opts.OrderBy) > 0 { if len(opts.OrderBy) > 0 {
sess.OrderBy(opts.OrderBy) sess.OrderBy(opts.OrderBy)
} }
return users, count, sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).Find(&users) return users, count, sess.
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
Find(&users)
} }
// ___________ .__ .__ // ___________ .__ .__

View file

@ -22,7 +22,9 @@ type EmailAddress struct {
// GetEmailAddresses returns all email addresses belongs to given user. // GetEmailAddresses returns all email addresses belongs to given user.
func GetEmailAddresses(uid int64) ([]*EmailAddress, error) { func GetEmailAddresses(uid int64) ([]*EmailAddress, error) {
emails := make([]*EmailAddress, 0, 5) emails := make([]*EmailAddress, 0, 5)
if err := x.Where("uid=?", uid).Find(&emails); err != nil { if err := x.
Where("uid=?", uid).
Find(&emails); err != nil {
return nil, err return nil, err
} }
@ -120,7 +122,10 @@ func (email *EmailAddress) Activate() error {
} }
email.IsActivated = true email.IsActivated = true
if _, err := sess.Id(email.ID).AllCols().Update(email); err != nil { if _, err := sess.
Id(email.ID).
AllCols().
Update(email); err != nil {
return err return err
} else if err = updateUser(sess, user); err != nil { } else if err = updateUser(sess, user); err != nil {
return err return err
@ -133,7 +138,9 @@ func DeleteEmailAddress(email *EmailAddress) (err error) {
if email.ID > 0 { if email.ID > 0 {
_, err = x.Id(email.ID).Delete(new(EmailAddress)) _, err = x.Id(email.ID).Delete(new(EmailAddress))
} else { } else {
_, err = x.Where("email=?", email.Email).Delete(new(EmailAddress)) _, err = x.
Where("email=?", email.Email).
Delete(new(EmailAddress))
} }
return err return err
} }

View file

@ -276,7 +276,10 @@ func GetWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
// GetActiveWebhooksByOrgID returns all active webhooks for an organization. // GetActiveWebhooksByOrgID returns all active webhooks for an organization.
func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) { func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
err = x.Where("org_id=?", orgID).And("is_active=?", true).Find(&ws) err = x.
Where("org_id=?", orgID).
And("is_active=?", true).
Find(&ws)
return ws, err return ws, err
} }
@ -413,7 +416,11 @@ func (t *HookTask) SimpleMarshalJSON(v interface{}) string {
// HookTasks returns a list of hook tasks by given conditions. // HookTasks returns a list of hook tasks by given conditions.
func HookTasks(hookID int64, page int) ([]*HookTask, error) { func HookTasks(hookID int64, page int) ([]*HookTask, error) {
tasks := make([]*HookTask, 0, setting.Webhook.PagingNum) tasks := make([]*HookTask, 0, setting.Webhook.PagingNum)
return tasks, x.Limit(setting.Webhook.PagingNum, (page-1)*setting.Webhook.PagingNum).Where("hook_id=?", hookID).Desc("id").Find(&tasks) return tasks, x.
Limit(setting.Webhook.PagingNum, (page-1)*setting.Webhook.PagingNum).
Where("hook_id=?", hookID).
Desc("id").
Find(&tasks)
} }
// CreateHookTask creates a new hook task, // CreateHookTask creates a new hook task,
@ -580,7 +587,9 @@ func (t *HookTask) deliver() {
// TODO: shoot more hooks at same time. // TODO: shoot more hooks at same time.
func DeliverHooks() { func DeliverHooks() {
tasks := make([]*HookTask, 0, 10) tasks := make([]*HookTask, 0, 10)
x.Where("is_delivered=?", false).Iterate(new(HookTask), x.
Where("is_delivered=?", false).
Iterate(new(HookTask),
func(idx int, bean interface{}) error { func(idx int, bean interface{}) error {
t := bean.(*HookTask) t := bean.(*HookTask)
t.deliver() t.deliver()