Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ID or Where to instead directly use Get when load object from database (#11925) #11934

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use ID or Where to instead directly use Get when load object from dat…
…abase (#11925)

* Use ID or Where to instead directly use Get when load object from database

* Apply suggestions from code review

Co-authored-by: 6543 <[email protected]>

Co-authored-by: 6543 <[email protected]>
Co-authored-by: zeripath <[email protected]>
  • Loading branch information
3 people committed Jun 17, 2020
commit 5aecf77dfe5d45b4e1b42d9e6fd2bf53e4295aea
9 changes: 4 additions & 5 deletions models/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,8 @@ func GetAttachmentByID(id int64) (*Attachment, error) {
}

func getAttachmentByID(e Engine, id int64) (*Attachment, error) {
attach := &Attachment{ID: id}

if has, err := e.Get(attach); err != nil {
attach := &Attachment{}
if has, err := e.ID(id).Get(attach); err != nil {
return nil, err
} else if !has {
return nil, ErrAttachmentNotExist{ID: id, UUID: ""}
Expand All @@ -147,8 +146,8 @@ func getAttachmentByID(e Engine, id int64) (*Attachment, error) {
}

func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
attach := &Attachment{UUID: uuid}
has, err := e.Get(attach)
attach := &Attachment{}
has, err := e.Where("uuid=?", uuid).Get(attach)
if err != nil {
return nil, err
} else if !has {
Expand Down
10 changes: 5 additions & 5 deletions models/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ func getProtectedBranchBy(e Engine, repoID int64, branchName string) (*Protected

// GetProtectedBranchByID getting protected branch by ID
func GetProtectedBranchByID(id int64) (*ProtectedBranch, error) {
rel := &ProtectedBranch{ID: id}
has, err := x.Get(rel)
rel := &ProtectedBranch{}
has, err := x.ID(id).Get(rel)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -509,9 +509,9 @@ func (repo *Repository) GetDeletedBranches() ([]*DeletedBranch, error) {
}

// GetDeletedBranchByID get a deleted branch by its ID
func (repo *Repository) GetDeletedBranchByID(ID int64) (*DeletedBranch, error) {
deletedBranch := &DeletedBranch{ID: ID}
has, err := x.Get(deletedBranch)
func (repo *Repository) GetDeletedBranchByID(id int64) (*DeletedBranch, error) {
deletedBranch := &DeletedBranch{}
has, err := x.ID(id).Get(deletedBranch)
if err != nil {
return nil, err
}
Expand Down
6 changes: 2 additions & 4 deletions models/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,8 @@ func getLabelByID(e Engine, labelID int64) (*Label, error) {
return nil, ErrLabelNotExist{labelID}
}

l := &Label{
ID: labelID,
}
has, err := e.Get(l)
l := &Label{}
has, err := e.ID(labelID).Get(l)
if err != nil {
return nil, err
} else if !has {
Expand Down
2 changes: 1 addition & 1 deletion models/login_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (source *LoginSource) SSPI() *SSPIConfig {
// CreateLoginSource inserts a LoginSource in the DB if not already
// existing with the given name.
func CreateLoginSource(source *LoginSource) error {
has, err := x.Get(&LoginSource{Name: source.Name})
has, err := x.Where("name=?", source.Name).Exist(new(LoginSource))
if err != nil {
return err
} else if has {
Expand Down
4 changes: 2 additions & 2 deletions models/twofactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ func UpdateTwoFactor(t *TwoFactor) error {
// GetTwoFactorByUID returns the two-factor authentication token associated with
// the user, if any.
func GetTwoFactorByUID(uid int64) (*TwoFactor, error) {
twofa := &TwoFactor{UID: uid}
has, err := x.Get(twofa)
twofa := &TwoFactor{}
has, err := x.Where("uid=?", uid).Get(twofa)
if err != nil {
return nil, err
} else if !has {
Expand Down
4 changes: 2 additions & 2 deletions models/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func NewUpload(name string, buf []byte, file multipart.File) (_ *Upload, err err

// GetUploadByUUID returns the Upload by UUID
func GetUploadByUUID(uuid string) (*Upload, error) {
upload := &Upload{UUID: uuid}
has, err := x.Get(upload)
upload := &Upload{}
has, err := x.Where("uuid=?", uuid).Get(upload)
if err != nil {
return nil, err
} else if !has {
Expand Down
4 changes: 2 additions & 2 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1558,8 +1558,8 @@ func GetUserByEmailContext(ctx DBContext, email string) (*User, error) {
// Finally, if email address is the protected email address:
if strings.HasSuffix(email, fmt.Sprintf("@%s", setting.Service.NoReplyAddress)) {
username := strings.TrimSuffix(email, fmt.Sprintf("@%s", setting.Service.NoReplyAddress))
user := &User{LowerName: username}
has, err := ctx.e.Get(user)
user := &User{}
has, err := ctx.e.Where("lower_name=?", username).Get(user)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions models/user_mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func GetEmailAddresses(uid int64) ([]*EmailAddress, error) {
// GetEmailAddressByID gets a user's email address by ID
func GetEmailAddressByID(uid, id int64) (*EmailAddress, error) {
// User ID is required for security reasons
email := &EmailAddress{ID: id, UID: uid}
if has, err := x.Get(email); err != nil {
email := &EmailAddress{UID: uid}
if has, err := x.ID(id).Get(email); err != nil {
return nil, err
} else if !has {
return nil, nil
Expand Down Expand Up @@ -126,7 +126,7 @@ func isEmailUsed(e Engine, email string) (bool, error) {
return true, nil
}

return e.Get(&EmailAddress{Email: email})
return e.Where("email=?", email).Get(&EmailAddress{})
}

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

user := &User{ID: email.UID}
has, err = x.Get(user)
user := &User{}
has, err = x.ID(email.UID).Get(user)
if err != nil {
return err
} else if !has {
Expand Down
4 changes: 2 additions & 2 deletions models/user_openid.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func GetUserByOpenID(uri string) (*User, error) {
log.Trace("Normalized OpenID URI: " + uri)

// Otherwise, check in openid table
oid := &UserOpenID{URI: uri}
has, err := x.Get(oid)
oid := &UserOpenID{}
has, err := x.Where("uri=?", uri).Get(oid)
if err != nil {
return nil, err
}
Expand Down