Skip to content

Commit ecad970

Browse files
6543lunny
andauthored
Use ID or Where to instead directly use Get when load object from database (#11925) (#11934)
Backport #11925 Use ID or Where to instead directly use Get when load object from database Co-authored-by: Lunny Xiao <[email protected]>
1 parent 47a5c8e commit ecad970

9 files changed

+25
-28
lines changed

models/attachment.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,8 @@ func GetAttachmentByID(id int64) (*Attachment, error) {
136136
}
137137

138138
func getAttachmentByID(e Engine, id int64) (*Attachment, error) {
139-
attach := &Attachment{ID: id}
140-
141-
if has, err := e.Get(attach); err != nil {
139+
attach := &Attachment{}
140+
if has, err := e.ID(id).Get(attach); err != nil {
142141
return nil, err
143142
} else if !has {
144143
return nil, ErrAttachmentNotExist{ID: id, UUID: ""}
@@ -147,8 +146,8 @@ func getAttachmentByID(e Engine, id int64) (*Attachment, error) {
147146
}
148147

149148
func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
150-
attach := &Attachment{UUID: uuid}
151-
has, err := e.Get(attach)
149+
attach := &Attachment{}
150+
has, err := e.Where("uuid=?", uuid).Get(attach)
152151
if err != nil {
153152
return nil, err
154153
} else if !has {

models/branches.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ func getProtectedBranchBy(e Engine, repoID int64, branchName string) (*Protected
240240

241241
// GetProtectedBranchByID getting protected branch by ID
242242
func GetProtectedBranchByID(id int64) (*ProtectedBranch, error) {
243-
rel := &ProtectedBranch{ID: id}
244-
has, err := x.Get(rel)
243+
rel := &ProtectedBranch{}
244+
has, err := x.ID(id).Get(rel)
245245
if err != nil {
246246
return nil, err
247247
}
@@ -509,9 +509,9 @@ func (repo *Repository) GetDeletedBranches() ([]*DeletedBranch, error) {
509509
}
510510

511511
// GetDeletedBranchByID get a deleted branch by its ID
512-
func (repo *Repository) GetDeletedBranchByID(ID int64) (*DeletedBranch, error) {
513-
deletedBranch := &DeletedBranch{ID: ID}
514-
has, err := x.Get(deletedBranch)
512+
func (repo *Repository) GetDeletedBranchByID(id int64) (*DeletedBranch, error) {
513+
deletedBranch := &DeletedBranch{}
514+
has, err := x.ID(id).Get(deletedBranch)
515515
if err != nil {
516516
return nil, err
517517
}

models/issue_label.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,8 @@ func getLabelByID(e Engine, labelID int64) (*Label, error) {
295295
return nil, ErrLabelNotExist{labelID}
296296
}
297297

298-
l := &Label{
299-
ID: labelID,
300-
}
301-
has, err := e.Get(l)
298+
l := &Label{}
299+
has, err := e.ID(labelID).Get(l)
302300
if err != nil {
303301
return nil, err
304302
} else if !has {

models/login_source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func (source *LoginSource) SSPI() *SSPIConfig {
300300
// CreateLoginSource inserts a LoginSource in the DB if not already
301301
// existing with the given name.
302302
func CreateLoginSource(source *LoginSource) error {
303-
has, err := x.Get(&LoginSource{Name: source.Name})
303+
has, err := x.Where("name=?", source.Name).Exist(new(LoginSource))
304304
if err != nil {
305305
return err
306306
} else if has {

models/twofactor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ func UpdateTwoFactor(t *TwoFactor) error {
142142
// GetTwoFactorByUID returns the two-factor authentication token associated with
143143
// the user, if any.
144144
func GetTwoFactorByUID(uid int64) (*TwoFactor, error) {
145-
twofa := &TwoFactor{UID: uid}
146-
has, err := x.Get(twofa)
145+
twofa := &TwoFactor{}
146+
has, err := x.Where("uid=?", uid).Get(twofa)
147147
if err != nil {
148148
return nil, err
149149
} else if !has {

models/upload.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ func NewUpload(name string, buf []byte, file multipart.File) (_ *Upload, err err
7676

7777
// GetUploadByUUID returns the Upload by UUID
7878
func GetUploadByUUID(uuid string) (*Upload, error) {
79-
upload := &Upload{UUID: uuid}
80-
has, err := x.Get(upload)
79+
upload := &Upload{}
80+
has, err := x.Where("uuid=?", uuid).Get(upload)
8181
if err != nil {
8282
return nil, err
8383
} else if !has {

models/user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,8 +1558,8 @@ func GetUserByEmailContext(ctx DBContext, email string) (*User, error) {
15581558
// Finally, if email address is the protected email address:
15591559
if strings.HasSuffix(email, fmt.Sprintf("@%s", setting.Service.NoReplyAddress)) {
15601560
username := strings.TrimSuffix(email, fmt.Sprintf("@%s", setting.Service.NoReplyAddress))
1561-
user := &User{LowerName: username}
1562-
has, err := ctx.e.Get(user)
1561+
user := &User{}
1562+
has, err := ctx.e.Where("lower_name=?", username).Get(user)
15631563
if err != nil {
15641564
return nil, err
15651565
}

models/user_mail.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ func GetEmailAddresses(uid int64) ([]*EmailAddress, error) {
7171
// GetEmailAddressByID gets a user's email address by ID
7272
func GetEmailAddressByID(uid, id int64) (*EmailAddress, error) {
7373
// User ID is required for security reasons
74-
email := &EmailAddress{ID: id, UID: uid}
75-
if has, err := x.Get(email); err != nil {
74+
email := &EmailAddress{UID: uid}
75+
if has, err := x.ID(id).Get(email); err != nil {
7676
return nil, err
7777
} else if !has {
7878
return nil, nil
@@ -126,7 +126,7 @@ func isEmailUsed(e Engine, email string) (bool, error) {
126126
return true, nil
127127
}
128128

129-
return e.Get(&EmailAddress{Email: email})
129+
return e.Where("email=?", email).Get(&EmailAddress{})
130130
}
131131

132132
// IsEmailUsed returns true if the email has been used.
@@ -251,8 +251,8 @@ func MakeEmailPrimary(email *EmailAddress) error {
251251
return ErrEmailNotActivated
252252
}
253253

254-
user := &User{ID: email.UID}
255-
has, err = x.Get(user)
254+
user := &User{}
255+
has, err = x.ID(email.UID).Get(user)
256256
if err != nil {
257257
return err
258258
} else if !has {

models/user_openid.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ func GetUserByOpenID(uri string) (*User, error) {
111111
log.Trace("Normalized OpenID URI: " + uri)
112112

113113
// Otherwise, check in openid table
114-
oid := &UserOpenID{URI: uri}
115-
has, err := x.Get(oid)
114+
oid := &UserOpenID{}
115+
has, err := x.Where("uri=?", uri).Get(oid)
116116
if err != nil {
117117
return nil, err
118118
}

0 commit comments

Comments
 (0)