Move Rolestore.GetByNames query to raw query (#14403)
Given that this query is part of the top 5 most used queries we want to move it to use raw queries instead of gorp so we can get rid of the reflection overhead
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
5cb16c9fc7
Коммит
278c295869
@@ -191,28 +191,44 @@ func (s *SqlRoleStore) GetByName(name string) (*model.Role, *model.AppError) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SqlRoleStore) GetByNames(names []string) ([]*model.Role, *model.AppError) {
|
func (s *SqlRoleStore) GetByNames(names []string) ([]*model.Role, *model.AppError) {
|
||||||
var dbRoles []*Role
|
|
||||||
|
|
||||||
if len(names) == 0 {
|
if len(names) == 0 {
|
||||||
return []*model.Role{}, nil
|
return []*model.Role{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var searchPlaceholders []string
|
failure := func(e error) ([]*model.Role, *model.AppError) {
|
||||||
var parameters = map[string]interface{}{}
|
return nil, model.NewAppError("SqlRoleStore.GetByNames", "store.sql_role.get_by_names.app_error", nil, e.Error(), http.StatusInternalServerError)
|
||||||
for i, value := range names {
|
|
||||||
searchPlaceholders = append(searchPlaceholders, fmt.Sprintf(":Name%d", i))
|
|
||||||
parameters[fmt.Sprintf("Name%d", i)] = value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
searchTerm := "Name IN (" + strings.Join(searchPlaceholders, ", ") + ")"
|
query := s.getQueryBuilder().
|
||||||
|
Select("Id, Name, DisplayName, Description, CreateAt, UpdateAt, DeleteAt, Permissions, SchemeManaged, BuiltIn").
|
||||||
|
From("Roles").
|
||||||
|
Where(sq.Eq{"Name": names})
|
||||||
|
queryString, args, err := query.ToSql()
|
||||||
|
if err != nil {
|
||||||
|
return failure(err)
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := s.GetReplica().Select(&dbRoles, "SELECT * from Roles WHERE "+searchTerm, parameters); err != nil {
|
rows, err := s.GetReplica().Db.Query(queryString, args...)
|
||||||
return nil, model.NewAppError("SqlRoleStore.GetByNames", "store.sql_role.get_by_names.app_error", nil, err.Error(), http.StatusInternalServerError)
|
if err != nil {
|
||||||
|
return failure(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var roles []*model.Role
|
var roles []*model.Role
|
||||||
for _, dbRole := range dbRoles {
|
defer rows.Close()
|
||||||
roles = append(roles, dbRole.ToModel())
|
for rows.Next() {
|
||||||
|
var role Role
|
||||||
|
err = rows.Scan(
|
||||||
|
&role.Id, &role.Name, &role.DisplayName, &role.Description,
|
||||||
|
&role.CreateAt, &role.UpdateAt, &role.DeleteAt, &role.Permissions,
|
||||||
|
&role.SchemeManaged, &role.BuiltIn)
|
||||||
|
if err != nil {
|
||||||
|
return failure(err)
|
||||||
|
}
|
||||||
|
roles = append(roles, role.ToModel())
|
||||||
|
}
|
||||||
|
err = rows.Err()
|
||||||
|
if err != nil {
|
||||||
|
return failure(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return roles, nil
|
return roles, nil
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user