[MM-24944] Refactor command store to use squirrel (#14552)
Automatic Merge
Этот коммит содержится в:
@@ -6,17 +6,24 @@ package sqlstore
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
sq "github.com/Masterminds/squirrel"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/store"
|
||||
)
|
||||
|
||||
type SqlCommandStore struct {
|
||||
SqlStore
|
||||
|
||||
commandsQuery sq.SelectBuilder
|
||||
}
|
||||
|
||||
func newSqlCommandStore(sqlStore SqlStore) store.CommandStore {
|
||||
s := &SqlCommandStore{sqlStore}
|
||||
s := &SqlCommandStore{SqlStore: sqlStore}
|
||||
|
||||
s.commandsQuery = s.getQueryBuilder().
|
||||
Select("*").
|
||||
From("Commands")
|
||||
for _, db := range sqlStore.GetAllConns() {
|
||||
tableo := db.AddTableWithName(model.Command{}, "Commands").SetKeys(false, "Id")
|
||||
tableo.ColMap("Id").SetMaxSize(26)
|
||||
@@ -64,7 +71,12 @@ func (s SqlCommandStore) Save(command *model.Command) (*model.Command, *model.Ap
|
||||
func (s SqlCommandStore) Get(id string) (*model.Command, *model.AppError) {
|
||||
var command model.Command
|
||||
|
||||
if err := s.GetReplica().SelectOne(&command, "SELECT * FROM Commands WHERE Id = :Id AND DeleteAt = 0", map[string]interface{}{"Id": id}); err != nil {
|
||||
sql, args, err := s.commandsQuery.
|
||||
Where(sq.Eq{"Id": id, "DeleteAt": 0}).ToSql()
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("SqlCommandStore.Get", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
if err = s.GetReplica().SelectOne(&command, sql, args...); err != nil {
|
||||
return nil, model.NewAppError("SqlCommandStore.Get", "store.sql_command.save.get.app_error", nil, "id="+id+", err="+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
@@ -73,8 +85,12 @@ func (s SqlCommandStore) Get(id string) (*model.Command, *model.AppError) {
|
||||
|
||||
func (s SqlCommandStore) GetByTeam(teamId string) ([]*model.Command, *model.AppError) {
|
||||
var commands []*model.Command
|
||||
|
||||
if _, err := s.GetReplica().Select(&commands, "SELECT * FROM Commands WHERE TeamId = :TeamId AND DeleteAt = 0", map[string]interface{}{"TeamId": teamId}); err != nil {
|
||||
sql, args, err := s.commandsQuery.
|
||||
Where(sq.Eq{"TeamId": teamId, "DeleteAt": 0}).ToSql()
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("SqlCommandStore.GetByTeam", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
if _, err := s.GetReplica().Select(&commands, sql, args...); err != nil {
|
||||
return nil, model.NewAppError("SqlCommandStore.GetByTeam", "store.sql_command.save.get_team.app_error", nil, "teamId="+teamId+", err="+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
@@ -83,15 +99,20 @@ func (s SqlCommandStore) GetByTeam(teamId string) ([]*model.Command, *model.AppE
|
||||
|
||||
func (s SqlCommandStore) GetByTrigger(teamId string, trigger string) (*model.Command, *model.AppError) {
|
||||
var command model.Command
|
||||
|
||||
var query string
|
||||
var triggerStr string
|
||||
if s.DriverName() == "mysql" {
|
||||
query = "SELECT * FROM Commands WHERE TeamId = :TeamId AND `Trigger` = :Trigger AND DeleteAt = 0"
|
||||
triggerStr = "`Trigger`"
|
||||
} else {
|
||||
query = "SELECT * FROM Commands WHERE TeamId = :TeamId AND \"trigger\" = :Trigger AND DeleteAt = 0"
|
||||
triggerStr = "\"trigger\""
|
||||
}
|
||||
|
||||
if err := s.GetReplica().SelectOne(&command, query, map[string]interface{}{"TeamId": teamId, "Trigger": trigger}); err != nil {
|
||||
sql, args, err := s.commandsQuery.
|
||||
Where(sq.Eq{"TeamId": teamId, "DeleteAt": 0, triggerStr: trigger}).ToSql()
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("SqlCommandStore.GetByTrigger", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if err := s.GetReplica().SelectOne(&command, sql, args...); err != nil {
|
||||
return nil, model.NewAppError("SqlCommandStore.GetByTrigger", "store.sql_command.get_by_trigger.app_error", nil, "teamId="+teamId+", trigger="+trigger+", err="+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
@@ -99,7 +120,15 @@ func (s SqlCommandStore) GetByTrigger(teamId string, trigger string) (*model.Com
|
||||
}
|
||||
|
||||
func (s SqlCommandStore) Delete(commandId string, time int64) *model.AppError {
|
||||
_, err := s.GetMaster().Exec("Update Commands SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :Id", map[string]interface{}{"DeleteAt": time, "UpdateAt": time, "Id": commandId})
|
||||
sql, args, err := s.getQueryBuilder().
|
||||
Update("Commands").
|
||||
SetMap(sq.Eq{"DeleteAt": time, "UpdateAt": time}).
|
||||
Where(sq.Eq{"Id": commandId}).ToSql()
|
||||
if err != nil {
|
||||
return model.NewAppError("SqlCommandStore.Delete", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
_, err = s.GetMaster().Exec(sql, args...)
|
||||
if err != nil {
|
||||
return model.NewAppError("SqlCommandStore.Delete", "store.sql_command.save.delete.app_error", nil, "id="+commandId+", err="+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
@@ -108,7 +137,13 @@ func (s SqlCommandStore) Delete(commandId string, time int64) *model.AppError {
|
||||
}
|
||||
|
||||
func (s SqlCommandStore) PermanentDeleteByTeam(teamId string) *model.AppError {
|
||||
_, err := s.GetMaster().Exec("DELETE FROM Commands WHERE TeamId = :TeamId", map[string]interface{}{"TeamId": teamId})
|
||||
sql, args, err := s.getQueryBuilder().
|
||||
Delete("Commands").
|
||||
Where(sq.Eq{"TeamId": teamId}).ToSql()
|
||||
if err != nil {
|
||||
return model.NewAppError("SqlCommandStore.DeleteByTeam", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
_, err = s.GetMaster().Exec(sql, args...)
|
||||
if err != nil {
|
||||
return model.NewAppError("SqlCommandStore.DeleteByTeam", "store.sql_command.save.delete_perm.app_error", nil, "id="+teamId+", err="+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
@@ -116,7 +151,13 @@ func (s SqlCommandStore) PermanentDeleteByTeam(teamId string) *model.AppError {
|
||||
}
|
||||
|
||||
func (s SqlCommandStore) PermanentDeleteByUser(userId string) *model.AppError {
|
||||
_, err := s.GetMaster().Exec("DELETE FROM Commands WHERE CreatorId = :UserId", map[string]interface{}{"UserId": userId})
|
||||
sql, args, err := s.getQueryBuilder().
|
||||
Delete("Commands").
|
||||
Where(sq.Eq{"CreatorId": userId}).ToSql()
|
||||
if err != nil {
|
||||
return model.NewAppError("SqlCommandStore.DeleteByUser", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
_, err = s.GetMaster().Exec(sql, args...)
|
||||
if err != nil {
|
||||
return model.NewAppError("SqlCommandStore.DeleteByUser", "store.sql_command.save.delete_perm.app_error", nil, "id="+userId+", err="+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
@@ -139,19 +180,21 @@ func (s SqlCommandStore) Update(cmd *model.Command) (*model.Command, *model.AppE
|
||||
}
|
||||
|
||||
func (s SqlCommandStore) AnalyticsCommandCount(teamId string) (int64, *model.AppError) {
|
||||
query :=
|
||||
`SELECT
|
||||
COUNT(*)
|
||||
FROM
|
||||
Commands
|
||||
WHERE
|
||||
DeleteAt = 0`
|
||||
query := s.getQueryBuilder().
|
||||
Select("COUNT(*)").
|
||||
From("Commands").
|
||||
Where(sq.Eq{"DeleteAt": 0})
|
||||
|
||||
if len(teamId) > 0 {
|
||||
query += " AND TeamId = :TeamId"
|
||||
query = query.Where(sq.Eq{"TeamId": teamId})
|
||||
}
|
||||
|
||||
c, err := s.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId})
|
||||
sql, args, err := query.ToSql()
|
||||
if err != nil {
|
||||
return 0, model.NewAppError("SqlCommandStore.AnalyticsCommandCount", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
c, err := s.GetReplica().SelectInt(sql, args...)
|
||||
if err != nil {
|
||||
return 0, model.NewAppError("SqlCommandStore.AnalyticsCommandCount", "store.sql_command.analytics_command_count.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user