Skip to content

Commit

Permalink
Rewrite XORM queries
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultmeyer committed Nov 10, 2016
1 parent c040f2f commit a4454f5
Show file tree
Hide file tree
Showing 22 changed files with 480 additions and 233 deletions.
8 changes: 6 additions & 2 deletions models/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,18 @@ func (u *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
// GetAccessibleRepositories finds repositories which the user has access but does not own.
// If limit is smaller than 1 means returns all found results.
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 {
sess.Limit(limit)
repos = make([]*Repository, 0, limit)
} else {
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 {
Expand Down
31 changes: 18 additions & 13 deletions models/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ import (
type ActionType int

const (
ActionCreateRepo ActionType = iota + 1 // 1
ActionRenameRepo // 2
ActionStarRepo // 3
ActionWatchRepo // 4
ActionCommitRepo // 5
ActionCreateIssue // 6
ActionCreateRepo ActionType = iota + 1 // 1
ActionRenameRepo // 2
ActionStarRepo // 3
ActionWatchRepo // 4
ActionCommitRepo // 5
ActionCreateIssue // 6
ActionCreatePullRequest // 7
ActionTransferRepo // 8
ActionPushTag // 9
ActionCommentIssue // 10
ActionTransferRepo // 8
ActionPushTag // 9
ActionCommentIssue // 10
ActionMergePullRequest // 11
ActionCloseIssue // 12
ActionReopenIssue // 13
ActionCloseIssue // 12
ActionReopenIssue // 13
ActionClosePullRequest // 14
ActionReopenPullRequest // 15
)
Expand Down Expand Up @@ -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.
func GetFeeds(ctxUser *User, actorID, offset int64, isProfile bool) ([]*Action, error) {
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 {
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() {
// FIXME: only need to get IDs here, not all fields of repository.
repos, _, err := ctxUser.GetUserRepositories(actorID, 1, ctxUser.NumRepos)
Expand Down
9 changes: 7 additions & 2 deletions models/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ func CountNotices() int64 {
// Notices returns number of notices in given page.
func Notices(page, pageSize int) ([]*Notice, error) {
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.
Expand All @@ -127,6 +130,8 @@ func DeleteNoticesByIDs(ids []int64) error {
if len(ids) == 0 {
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
}
2 changes: 1 addition & 1 deletion models/git_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (
"strings"

"github.com/Unknwon/com"
"github.com/go-gitea/git"
"github.com/go-gitea/gitea/modules/base"
"github.com/go-gitea/gitea/modules/log"
"github.com/go-gitea/gitea/modules/process"
"github.com/go-gitea/gitea/modules/setting"
"github.com/go-gitea/gitea/modules/template/highlight"
"github.com/go-gitea/git"
"github.com/sergi/go-diff/diffmatchpatch"
"golang.org/x/net/html/charset"
"golang.org/x/text/transform"
Expand Down
55 changes: 41 additions & 14 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"time"

"github.com/Unknwon/com"
"github.com/go-xorm/xorm"
api "github.com/go-gitea/go-sdk/gitea"
"github.com/go-xorm/xorm"
gouuid "github.com/satori/go.uuid"

"github.com/go-gitea/gitea/modules/base"
Expand Down Expand Up @@ -820,13 +820,17 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
sess := x.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum)

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 {
// In case repository IDs are provided but actually no repository has issue.
if len(opts.RepoIDs) == 0 {
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 {
sess.Where("issue.is_closed=?", opts.IsClosed)
}
Expand Down Expand Up @@ -863,12 +867,16 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
if len(opts.Labels) > 0 && opts.Labels != "0" {
labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
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 {
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 {
sess.And("issue_user.uid = ?", opts.UserID)
Expand Down Expand Up @@ -991,15 +999,21 @@ func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*Issue
}

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)
return ius, err
}

// GetIssueUserPairsByMode returns issue-user pairs by given repository and user.
func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) {
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 {
sess.And("repo_id=?", rid)
}
Expand Down Expand Up @@ -1101,12 +1115,16 @@ func GetIssueStats(opts *IssueStatsOptions) *IssueStats {
stats := &IssueStats{}

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" {
labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
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)
}
}

Expand Down Expand Up @@ -1163,7 +1181,9 @@ func GetUserIssueStats(repoID, uid int64, repoIDs []int64, filterMode int, isPul
stats := &IssueStats{}

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 {
sess.And("repo_id = ?", repoID)
Expand Down Expand Up @@ -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.
func GetRepoIssueStats(repoID, uid int64, filterMode int, isPull bool) (numOpen int64, numClosed int64) {
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("repo_id = ?", repoID)

Expand Down Expand Up @@ -1463,7 +1484,9 @@ func UpdateMilestone(m *Milestone) error {
}

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
}

Expand All @@ -1473,7 +1496,9 @@ func CountRepoMilestones(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
}

Expand All @@ -1484,7 +1509,9 @@ func CountRepoClosedMilestones(repoID int64) int64 {

// MilestoneStats returns number of open and closed milestones of given repository.
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)
}

Expand Down
4 changes: 3 additions & 1 deletion models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@ func GetCommentByID(id int64) (*Comment, error) {

func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, error) {
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 {
sess.And("updated_unix >= ?", since)
}
Expand Down
26 changes: 21 additions & 5 deletions models/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,20 @@ func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) {
// it silently ignores label IDs that are not belong to the repository.
func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
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.
func GetLabelsByRepoID(repoID int64) ([]*Label, error) {
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) {
Expand All @@ -161,7 +168,11 @@ func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
}

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.
Expand Down Expand Up @@ -197,7 +208,9 @@ func DeleteLabel(repoID, labelID int64) error {

if _, err = sess.Id(labelID).Delete(new(Label)); err != nil {
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
}

Expand Down Expand Up @@ -293,7 +306,10 @@ func NewIssueLabels(issue *Issue, labels []*Label) (err error) {

func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
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.
Expand Down
2 changes: 1 addition & 1 deletion models/login_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var LoginNames = map[LoginType]string{
var SecurityProtocolNames = map[ldap.SecurityProtocol]string{
ldap.SecurityProtocolUnencrypted: "Unencrypted",
ldap.SecurityProtocolLDAPS: "LDAPS",
ldap.SecurityProtocolStartTLS: "StartTLS",
ldap.SecurityProtocolStartTLS: "StartTLS",
}

// Ensure structs implemented interface.
Expand Down
2 changes: 1 addition & 1 deletion models/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

const (
MailAuthActivate base.TplName = "auth/activate"
MailAuthActivate base.TplName = "auth/activate"
MailAuthActivateEmail base.TplName = "auth/activate_email"
MailAuthResetPassword base.TplName = "auth/reset_passwd"
MailAuthRegisterNotify base.TplName = "auth/register_notify"
Expand Down
Loading

0 comments on commit a4454f5

Please sign in to comment.