MM-39634: Migrated gorp to sqlx (#18951)
* MM-39634: Migrated gorp to sqlx * MM-39634: Removed redundant limit on get since only one value is returned * MM-39485: Fixed returned errors + goimports Co-authored-by: Alex <alex@rumandcode.io> Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
@@ -6,11 +6,10 @@ package sqlstore
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
|
|
||||||
"github.com/mattermost/mattermost-server/v6/einterfaces"
|
"github.com/mattermost/mattermost-server/v6/einterfaces"
|
||||||
"github.com/mattermost/mattermost-server/v6/model"
|
"github.com/mattermost/mattermost-server/v6/model"
|
||||||
"github.com/mattermost/mattermost-server/v6/store"
|
"github.com/mattermost/mattermost-server/v6/store"
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SqlTermsOfServiceStore struct {
|
type SqlTermsOfServiceStore struct {
|
||||||
@@ -44,8 +43,13 @@ func (s SqlTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*mod
|
|||||||
if err := termsOfService.IsValid(); err != nil {
|
if err := termsOfService.IsValid(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
query := `INSERT INTO TermsOfService
|
||||||
|
(Id, CreateAt, UserId, Text)
|
||||||
|
VALUES
|
||||||
|
(:Id, :CreateAt, :UserId, :Text)
|
||||||
|
`
|
||||||
|
|
||||||
if err := s.GetMaster().Insert(termsOfService); err != nil {
|
if _, err := s.GetMasterX().NamedExec(query, termsOfService); err != nil {
|
||||||
return nil, errors.Wrapf(err, "could not save a new TermsOfService")
|
return nil, errors.Wrapf(err, "could not save a new TermsOfService")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +57,7 @@ func (s SqlTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*mod
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s SqlTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, error) {
|
func (s SqlTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, error) {
|
||||||
var termsOfService *model.TermsOfService
|
var termsOfService model.TermsOfService
|
||||||
|
|
||||||
query := s.getQueryBuilder().
|
query := s.getQueryBuilder().
|
||||||
Select("*").
|
Select("*").
|
||||||
@@ -66,23 +70,34 @@ func (s SqlTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfSe
|
|||||||
return nil, errors.Wrap(err, "could not build sql query to get latest TOS")
|
return nil, errors.Wrap(err, "could not build sql query to get latest TOS")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.GetReplica().SelectOne(&termsOfService, queryString, args...); err != nil {
|
if err := s.GetReplicaX().Get(&termsOfService, queryString, args...); err != nil {
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return nil, store.NewErrNotFound("TermsOfService", "CreateAt=latest")
|
return nil, store.NewErrNotFound("TermsOfService", "CreateAt=latest")
|
||||||
}
|
}
|
||||||
return nil, errors.Wrap(err, "could not find latest TermsOfService")
|
return nil, errors.Wrap(err, "could not find latest TermsOfService")
|
||||||
}
|
}
|
||||||
|
|
||||||
return termsOfService, nil
|
return &termsOfService, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SqlTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, error) {
|
func (s SqlTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, error) {
|
||||||
obj, err := s.GetReplica().Get(model.TermsOfService{}, id)
|
var termsOfService model.TermsOfService
|
||||||
|
queryString, _, err := s.getQueryBuilder().
|
||||||
|
Select("*").
|
||||||
|
From("TermsOfService").
|
||||||
|
Where("id = ?").
|
||||||
|
ToSql()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "terms_of_service_to_sql")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.GetReplicaX().Get(&termsOfService, queryString, id)
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, store.NewErrNotFound("TermsOfService", "id")
|
||||||
|
}
|
||||||
return nil, errors.Wrapf(err, "could not find TermsOfService with id=%s", id)
|
return nil, errors.Wrapf(err, "could not find TermsOfService with id=%s", id)
|
||||||
}
|
}
|
||||||
if obj == nil {
|
return &termsOfService, nil
|
||||||
return nil, store.NewErrNotFound("TermsOfService", id)
|
|
||||||
}
|
|
||||||
return obj.(*model.TermsOfService), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user