Skip to content

Commit 52cc3fd

Browse files
authored
Merge pull request #136 from 0xbaadf00d/feature/rewrite-xorm-queries
Rewrite raw queries
2 parents 4247304 + b12f2a5 commit 52cc3fd

21 files changed

+474
-227
lines changed

models/access.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,18 @@ func (u *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
124124
// GetAccessibleRepositories finds repositories which the user has access but does not own.
125125
// If limit is smaller than 1 means returns all found results.
126126
func (user *User) GetAccessibleRepositories(limit int) (repos []*Repository, _ error) {
127-
sess := x.Where("owner_id !=? ", user.ID).Desc("updated_unix")
127+
sess := x.
128+
Where("owner_id !=? ", user.ID).
129+
Desc("updated_unix")
128130
if limit > 0 {
129131
sess.Limit(limit)
130132
repos = make([]*Repository, 0, limit)
131133
} else {
132134
repos = make([]*Repository, 0, 10)
133135
}
134-
return repos, sess.Join("INNER", "access", "access.user_id = ? AND access.repo_id = repository.id", user.ID).Find(&repos)
136+
return repos, sess.
137+
Join("INNER", "access", "access.user_id = ? AND access.repo_id = repository.id", user.ID).
138+
Find(&repos)
135139
}
136140

137141
func maxAccessMode(modes ...AccessMode) AccessMode {

models/action.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ import (
2727
type ActionType int
2828

2929
const (
30-
ActionCreateRepo ActionType = iota + 1 // 1
31-
ActionRenameRepo // 2
32-
ActionStarRepo // 3
33-
ActionWatchRepo // 4
34-
ActionCommitRepo // 5
35-
ActionCreateIssue // 6
30+
ActionCreateRepo ActionType = iota + 1 // 1
31+
ActionRenameRepo // 2
32+
ActionStarRepo // 3
33+
ActionWatchRepo // 4
34+
ActionCommitRepo // 5
35+
ActionCreateIssue // 6
3636
ActionCreatePullRequest // 7
37-
ActionTransferRepo // 8
38-
ActionPushTag // 9
39-
ActionCommentIssue // 10
37+
ActionTransferRepo // 8
38+
ActionPushTag // 9
39+
ActionCommentIssue // 10
4040
ActionMergePullRequest // 11
41-
ActionCloseIssue // 12
42-
ActionReopenIssue // 13
41+
ActionCloseIssue // 12
42+
ActionReopenIssue // 13
4343
ActionClosePullRequest // 14
4444
ActionReopenPullRequest // 15
4545
)
@@ -591,9 +591,14 @@ func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error
591591
// actorID can be -1 when isProfile is true or to skip the permission check.
592592
func GetFeeds(ctxUser *User, actorID, offset int64, isProfile bool) ([]*Action, error) {
593593
actions := make([]*Action, 0, 20)
594-
sess := x.Limit(20, int(offset)).Desc("id").Where("user_id = ?", ctxUser.ID)
594+
sess := x.
595+
Limit(20, int(offset)).
596+
Desc("id").
597+
Where("user_id = ?", ctxUser.ID)
595598
if isProfile {
596-
sess.And("is_private = ?", false).And("act_user_id = ?", ctxUser.ID)
599+
sess.
600+
And("is_private = ?", false).
601+
And("act_user_id = ?", ctxUser.ID)
597602
} else if actorID != -1 && ctxUser.IsOrganization() {
598603
// FIXME: only need to get IDs here, not all fields of repository.
599604
repos, _, err := ctxUser.GetUserRepositories(actorID, 1, ctxUser.NumRepos)

models/admin.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ func CountNotices() int64 {
103103
// Notices returns number of notices in given page.
104104
func Notices(page, pageSize int) ([]*Notice, error) {
105105
notices := make([]*Notice, 0, pageSize)
106-
return notices, x.Limit(pageSize, (page-1)*pageSize).Desc("id").Find(&notices)
106+
return notices, x.
107+
Limit(pageSize, (page-1)*pageSize).
108+
Desc("id").
109+
Find(&notices)
107110
}
108111

109112
// DeleteNotice deletes a system notice by given ID.
@@ -127,6 +130,8 @@ func DeleteNoticesByIDs(ids []int64) error {
127130
if len(ids) == 0 {
128131
return nil
129132
}
130-
_, err := x.Where("id IN (" + strings.Join(base.Int64sToStrings(ids), ",") + ")").Delete(new(Notice))
133+
_, err := x.
134+
Where("id IN (" + strings.Join(base.Int64sToStrings(ids), ",") + ")").
135+
Delete(new(Notice))
131136
return err
132137
}

models/issue.go

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -820,13 +820,17 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
820820
sess := x.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum)
821821

822822
if opts.RepoID > 0 {
823-
sess.Where("issue.repo_id=?", opts.RepoID).And("issue.is_closed=?", opts.IsClosed)
823+
sess.
824+
Where("issue.repo_id=?", opts.RepoID).
825+
And("issue.is_closed=?", opts.IsClosed)
824826
} else if opts.RepoIDs != nil {
825827
// In case repository IDs are provided but actually no repository has issue.
826828
if len(opts.RepoIDs) == 0 {
827829
return make([]*Issue, 0), nil
828830
}
829-
sess.In("issue.repo_id", base.Int64sToStrings(opts.RepoIDs)).And("issue.is_closed=?", opts.IsClosed)
831+
sess.
832+
In("issue.repo_id", base.Int64sToStrings(opts.RepoIDs)).
833+
And("issue.is_closed=?", opts.IsClosed)
830834
} else {
831835
sess.Where("issue.is_closed=?", opts.IsClosed)
832836
}
@@ -863,12 +867,16 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
863867
if len(opts.Labels) > 0 && opts.Labels != "0" {
864868
labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
865869
if len(labelIDs) > 0 {
866-
sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").In("issue_label.label_id", labelIDs)
870+
sess.
871+
Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
872+
In("issue_label.label_id", labelIDs)
867873
}
868874
}
869875

870876
if opts.IsMention {
871-
sess.Join("INNER", "issue_user", "issue.id = issue_user.issue_id").And("issue_user.is_mentioned = ?", true)
877+
sess.
878+
Join("INNER", "issue_user", "issue.id = issue_user.issue_id").
879+
And("issue_user.is_mentioned = ?", true)
872880

873881
if opts.UserID > 0 {
874882
sess.And("issue_user.uid = ?", opts.UserID)
@@ -991,15 +999,21 @@ func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*Issue
991999
}
9921000

9931001
ius := make([]*IssueUser, 0, 10)
994-
sess := x.Limit(20, (page-1)*20).Where("is_closed=?", isClosed).In("repo_id", rids)
1002+
sess := x.
1003+
Limit(20, (page-1)*20).
1004+
Where("is_closed=?", isClosed).
1005+
In("repo_id", rids)
9951006
err := sess.Find(&ius)
9961007
return ius, err
9971008
}
9981009

9991010
// GetIssueUserPairsByMode returns issue-user pairs by given repository and user.
10001011
func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) {
10011012
ius := make([]*IssueUser, 0, 10)
1002-
sess := x.Limit(20, (page-1)*20).Where("uid=?", uid).And("is_closed=?", isClosed)
1013+
sess := x.
1014+
Limit(20, (page-1)*20).
1015+
Where("uid=?", uid).
1016+
And("is_closed=?", isClosed)
10031017
if rid > 0 {
10041018
sess.And("repo_id=?", rid)
10051019
}
@@ -1101,12 +1115,16 @@ func GetIssueStats(opts *IssueStatsOptions) *IssueStats {
11011115
stats := &IssueStats{}
11021116

11031117
countSession := func(opts *IssueStatsOptions) *xorm.Session {
1104-
sess := x.Where("issue.repo_id = ?", opts.RepoID).And("is_pull = ?", opts.IsPull)
1118+
sess := x.
1119+
Where("issue.repo_id = ?", opts.RepoID).
1120+
And("is_pull = ?", opts.IsPull)
11051121

11061122
if len(opts.Labels) > 0 && opts.Labels != "0" {
11071123
labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
11081124
if len(labelIDs) > 0 {
1109-
sess.Join("INNER", "issue_label", "issue.id = issue_id").In("label_id", labelIDs)
1125+
sess.
1126+
Join("INNER", "issue_label", "issue.id = issue_id").
1127+
In("label_id", labelIDs)
11101128
}
11111129
}
11121130

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

11651183
countSession := func(isClosed, isPull bool, repoID int64, repoIDs []int64) *xorm.Session {
1166-
sess := x.Where("issue.is_closed = ?", isClosed).And("issue.is_pull = ?", isPull)
1184+
sess := x.
1185+
Where("issue.is_closed = ?", isClosed).
1186+
And("issue.is_pull = ?", isPull)
11671187

11681188
if repoID > 0 || len(repoIDs) == 0 {
11691189
sess.And("repo_id = ?", repoID)
@@ -1203,7 +1223,8 @@ func GetUserIssueStats(repoID, uid int64, repoIDs []int64, filterMode int, isPul
12031223
// GetRepoIssueStats returns number of open and closed repository issues by given filter mode.
12041224
func GetRepoIssueStats(repoID, uid int64, filterMode int, isPull bool) (numOpen int64, numClosed int64) {
12051225
countSession := func(isClosed, isPull bool, repoID int64) *xorm.Session {
1206-
sess := x.Where("issue.repo_id = ?", isClosed).
1226+
sess := x.
1227+
Where("issue.repo_id = ?", isClosed).
12071228
And("is_pull = ?", isPull).
12081229
And("repo_id = ?", repoID)
12091230

@@ -1463,7 +1484,9 @@ func UpdateMilestone(m *Milestone) error {
14631484
}
14641485

14651486
func countRepoMilestones(e Engine, repoID int64) int64 {
1466-
count, _ := e.Where("repo_id=?", repoID).Count(new(Milestone))
1487+
count, _ := e.
1488+
Where("repo_id=?", repoID).
1489+
Count(new(Milestone))
14671490
return count
14681491
}
14691492

@@ -1473,7 +1496,9 @@ func CountRepoMilestones(repoID int64) int64 {
14731496
}
14741497

14751498
func countRepoClosedMilestones(e Engine, repoID int64) int64 {
1476-
closed, _ := e.Where("repo_id=? AND is_closed=?", repoID, true).Count(new(Milestone))
1499+
closed, _ := e.
1500+
Where("repo_id=? AND is_closed=?", repoID, true).
1501+
Count(new(Milestone))
14771502
return closed
14781503
}
14791504

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

14851510
// MilestoneStats returns number of open and closed milestones of given repository.
14861511
func MilestoneStats(repoID int64) (open int64, closed int64) {
1487-
open, _ = x.Where("repo_id=? AND is_closed=?", repoID, false).Count(new(Milestone))
1512+
open, _ = x.
1513+
Where("repo_id=? AND is_closed=?", repoID, false).
1514+
Count(new(Milestone))
14881515
return open, CountRepoClosedMilestones(repoID)
14891516
}
14901517

models/issue_comment.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,9 @@ func GetCommentByID(id int64) (*Comment, error) {
356356

357357
func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, error) {
358358
comments := make([]*Comment, 0, 10)
359-
sess := e.Where("issue_id = ?", issueID).Asc("created_unix")
359+
sess := e.
360+
Where("issue_id = ?", issueID).
361+
Asc("created_unix")
360362
if since > 0 {
361363
sess.And("updated_unix >= ?", since)
362364
}

models/issue_label.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,20 @@ func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) {
138138
// it silently ignores label IDs that are not belong to the repository.
139139
func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
140140
labels := make([]*Label, 0, len(labelIDs))
141-
return labels, x.Where("repo_id = ?", repoID).In("id", base.Int64sToStrings(labelIDs)).Asc("name").Find(&labels)
141+
return labels, x.
142+
Where("repo_id = ?", repoID).
143+
In("id", base.Int64sToStrings(labelIDs)).
144+
Asc("name").
145+
Find(&labels)
142146
}
143147

144148
// GetLabelsByRepoID returns all labels that belong to given repository by ID.
145149
func GetLabelsByRepoID(repoID int64) ([]*Label, error) {
146150
labels := make([]*Label, 0, 10)
147-
return labels, x.Where("repo_id = ?", repoID).Asc("name").Find(&labels)
151+
return labels, x.
152+
Where("repo_id = ?", repoID).
153+
Asc("name").
154+
Find(&labels)
148155
}
149156

150157
func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
@@ -161,7 +168,11 @@ func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
161168
}
162169

163170
labels := make([]*Label, 0, len(labelIDs))
164-
return labels, e.Where("id > 0").In("id", base.Int64sToStrings(labelIDs)).Asc("name").Find(&labels)
171+
return labels, e.
172+
Where("id > 0").
173+
In("id", base.Int64sToStrings(labelIDs)).
174+
Asc("name").
175+
Find(&labels)
165176
}
166177

167178
// GetLabelsByIssueID returns all labels that belong to given issue by ID.
@@ -197,7 +208,9 @@ func DeleteLabel(repoID, labelID int64) error {
197208

198209
if _, err = sess.Id(labelID).Delete(new(Label)); err != nil {
199210
return err
200-
} else if _, err = sess.Where("label_id = ?", labelID).Delete(new(IssueLabel)); err != nil {
211+
} else if _, err = sess.
212+
Where("label_id = ?", labelID).
213+
Delete(new(IssueLabel)); err != nil {
201214
return err
202215
}
203216

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

294307
func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
295308
issueLabels := make([]*IssueLabel, 0, 10)
296-
return issueLabels, e.Where("issue_id=?", issueID).Asc("label_id").Find(&issueLabels)
309+
return issueLabels, e.
310+
Where("issue_id=?", issueID).
311+
Asc("label_id").
312+
Find(&issueLabels)
297313
}
298314

299315
// GetIssueLabels returns all issue-label relations of given issue by ID.

models/login_source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var LoginNames = map[LoginType]string{
4646
var SecurityProtocolNames = map[ldap.SecurityProtocol]string{
4747
ldap.SecurityProtocolUnencrypted: "Unencrypted",
4848
ldap.SecurityProtocolLDAPS: "LDAPS",
49-
ldap.SecurityProtocolStartTLS: "StartTLS",
49+
ldap.SecurityProtocolStartTLS: "StartTLS",
5050
}
5151

5252
// Ensure structs implemented interface.

models/mail.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
const (
23-
MailAuthActivate base.TplName = "auth/activate"
23+
MailAuthActivate base.TplName = "auth/activate"
2424
MailAuthActivateEmail base.TplName = "auth/activate_email"
2525
MailAuthResetPassword base.TplName = "auth/reset_passwd"
2626
MailAuthRegisterNotify base.TplName = "auth/register_notify"

0 commit comments

Comments
 (0)