Move StatusStore.GetByIds away from gorp (#14685)
We're removing the usage of gorp in some of the most used queries in our system. This is one of them
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
e83cc7357c
Коммит
c9cdeba1a7
@@ -6,9 +6,10 @@ package sqlstore
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
sq "github.com/Masterminds/squirrel"
|
||||||
|
|
||||||
"github.com/mattermost/mattermost-server/v5/model"
|
"github.com/mattermost/mattermost-server/v5/model"
|
||||||
"github.com/mattermost/mattermost-server/v5/store"
|
"github.com/mattermost/mattermost-server/v5/store"
|
||||||
)
|
)
|
||||||
@@ -73,22 +74,42 @@ func (s SqlStatusStore) Get(userId string) (*model.Status, *model.AppError) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s SqlStatusStore) GetByIds(userIds []string) ([]*model.Status, *model.AppError) {
|
func (s SqlStatusStore) GetByIds(userIds []string) ([]*model.Status, *model.AppError) {
|
||||||
props := make(map[string]interface{})
|
|
||||||
idQuery := ""
|
|
||||||
|
|
||||||
for index, userId := range userIds {
|
failure := func(err error) *model.AppError {
|
||||||
if len(idQuery) > 0 {
|
return model.NewAppError(
|
||||||
idQuery += ", "
|
"SqlStatusStore.GetByIds",
|
||||||
}
|
"store.sql_status.get.app_error",
|
||||||
|
nil,
|
||||||
props["userId"+strconv.Itoa(index)] = userId
|
err.Error(),
|
||||||
idQuery += ":userId" + strconv.Itoa(index)
|
http.StatusInternalServerError,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
query := s.getQueryBuilder().
|
||||||
|
Select("UserId, Status, Manual, LastActivityAt").
|
||||||
|
From("Status").
|
||||||
|
Where(sq.Eq{"UserId": userIds})
|
||||||
|
queryString, args, err := query.ToSql()
|
||||||
|
if err != nil {
|
||||||
|
return nil, failure(err)
|
||||||
|
}
|
||||||
|
rows, err := s.GetReplica().Db.Query(queryString, args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, failure(err)
|
||||||
|
}
|
||||||
var statuses []*model.Status
|
var statuses []*model.Status
|
||||||
if _, err := s.GetReplica().Select(&statuses, "SELECT * FROM Status WHERE UserId IN ("+idQuery+")", props); err != nil {
|
defer rows.Close()
|
||||||
return nil, model.NewAppError("SqlStatusStore.GetByIds", "store.sql_status.get.app_error", nil, err.Error(), http.StatusInternalServerError)
|
for rows.Next() {
|
||||||
|
var status model.Status
|
||||||
|
if err = rows.Scan(&status.UserId, &status.Status, &status.Manual, &status.LastActivityAt); err != nil {
|
||||||
|
return nil, failure(err)
|
||||||
|
}
|
||||||
|
statuses = append(statuses, &status)
|
||||||
}
|
}
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, failure(err)
|
||||||
|
}
|
||||||
|
|
||||||
return statuses, nil
|
return statuses, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user