Skip to content

Commit

Permalink
删除代码过长的行
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangming03 committed Jun 28, 2023
1 parent 84a5875 commit cd645e9
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 28 deletions.
3 changes: 2 additions & 1 deletion store/postgresql/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,8 @@ func (m *adminStore) BatchCleanDeletedInstances(timeout time.Duration, batchSize

var rows int64
err := m.master.processWithTransaction("batchCleanDeletedInstances", func(tx *BaseTx) error {
stmt, err := tx.Prepare("delete from instance where id in (select id from instance where flag = 1 and mtime <= $1 limit $2)")
stmt, err := tx.Prepare("delete from instance where id in (select id from instance where " +
"flag = 1 and mtime <= $1 limit $2)")
if err != nil {
return store.Error(err)
}
Expand Down
3 changes: 2 additions & 1 deletion store/postgresql/config_file_release_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func (rh *configFileReleaseHistoryStore) QueryConfigFileReleaseHistories(namespa
}

countSql += fmt.Sprintf("group like $%d and file_name like $%d", idx, idx+1)
querySql += fmt.Sprintf("group like $%d and file_name like $%d order by id desc limit $%d offset $%d", idx+2, idx+3, idx+4, idx+5)
querySql += fmt.Sprintf("group like $%d and file_name like $%d order by id desc limit $%d offset $%d",
idx+2, idx+3, idx+4, idx+5)
queryParams = append(queryParams, "%"+group+"%")
queryParams = append(queryParams, "%"+fileName+"%")

Expand Down
3 changes: 2 additions & 1 deletion store/postgresql/config_file_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func (t *configFileTagStore) QueryConfigFileByTag(namespace, group, fileName str
var idx = 1
group = "%" + group + "%"
fileName = "%" + fileName + "%"
querySql := t.baseSelectSql() + fmt.Sprintf(" where namespace = $%d and \"group\" like $%d and file_name like $%d ", idx, idx+1, idx+2)
querySql := t.baseSelectSql() + fmt.Sprintf(" where namespace = $%d and \"group\" like $%d and file_name like $%d ",
idx, idx+1, idx+2)
idx += 3

var tagWhereSql []string
Expand Down
9 changes: 6 additions & 3 deletions store/postgresql/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,9 +1129,11 @@ func addInstanceMeta(tx *BaseTx, id string, meta map[string]string) error {
for key, value := range meta {
cnt++
if cnt == len(meta) {
str += fmt.Sprintf("($%d, $%d, $%d, '%v', '%v')", indexSort, indexSort+1, indexSort+2, GetCurrentTimeFormat(), GetCurrentTimeFormat())
str += fmt.Sprintf("($%d, $%d, $%d, '%v', '%v')", indexSort, indexSort+1,
indexSort+2, GetCurrentTimeFormat(), GetCurrentTimeFormat())
} else {
str += fmt.Sprintf("($%d, $%d, $%d, '%v', '%v'), ", indexSort, indexSort+1, indexSort+2, GetCurrentTimeFormat(), GetCurrentTimeFormat())
str += fmt.Sprintf("($%d, $%d, $%d, '%v', '%v'), ", indexSort, indexSort+1,
indexSort+2, GetCurrentTimeFormat(), GetCurrentTimeFormat())
}
indexSort += 3

Expand Down Expand Up @@ -1191,7 +1193,8 @@ func batchAddInstanceMeta(tx *BaseTx, instances []*model.Instance) error {
if !first {
str += ","
}
str += fmt.Sprintf("($%d, $%d, $%d, '%v', '%v')", index, index+1, index+2, GetCurrentTimeFormat(), GetCurrentTimeFormat())
str += fmt.Sprintf("($%d, $%d, $%d, '%v', '%v')", index, index+1, index+2,
GetCurrentTimeFormat(), GetCurrentTimeFormat())
index += 3
first = false
args = append(args, entry.ID(), key, value)
Expand Down
12 changes: 8 additions & 4 deletions store/postgresql/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ func (ns *namespaceStore) AddNamespace(namespace *model.Namespace) error {
return err
}

str := fmt.Sprintf("insert into namespace(name, comment, token, owner, ctime, mtime) values($1, $2, $3, $4, $5, $6)")
str := fmt.Sprintf("insert into namespace(name, comment, token, owner, ctime, mtime) " +
"values($1, $2, $3, $4, $5, $6)")
stmt, err := tx.Prepare(str)
if err != nil {
log.Errorf("[Store][database] insert prepare[%v] commit tx err: %s", namespace, err.Error())
return store.Error(err)
}

if _, err := stmt.Exec(namespace.Name, namespace.Comment, namespace.Token, namespace.Owner, GetCurrentTimeFormat(), GetCurrentTimeFormat()); err != nil {
if _, err := stmt.Exec(namespace.Name, namespace.Comment, namespace.Token, namespace.Owner,
GetCurrentTimeFormat(), GetCurrentTimeFormat()); err != nil {
return store.Error(err)
}

Expand Down Expand Up @@ -135,7 +137,8 @@ func (ns *namespaceStore) GetNamespace(name string) (*model.Namespace, error) {
}

// GetNamespaces 根据过滤条件查询命名空间及数目
func (ns *namespaceStore) GetNamespaces(filter map[string][]string, offset, limit int) ([]*model.Namespace, uint32, error) {
func (ns *namespaceStore) GetNamespaces(filter map[string][]string, offset,
limit int) ([]*model.Namespace, uint32, error) {
// 只查询有效数据
filter["flag"] = []string{"0"}

Expand Down Expand Up @@ -185,7 +188,8 @@ func (ns *namespaceStore) getNamespacesCount(filter map[string][]string) (uint32
}

// getNamespaces 根据相关条件查询对应命名空间
func (ns *namespaceStore) getNamespaces(filter map[string][]string, offset, limit int) ([]*model.Namespace, error) {
func (ns *namespaceStore) getNamespaces(filter map[string][]string, offset,
limit int) ([]*model.Namespace, error) {
str := genNamespaceSelectSQL()
order := &Order{"mtime", "desc"}
str, args := genNamespaceWhereSQLAndArgs(str, filter, order, offset, limit)
Expand Down
6 changes: 4 additions & 2 deletions store/postgresql/routing_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func (rs *routingConfigStore) CreateRoutingConfig(conf *model.RoutingConfig) err
if err != nil {
return store.Error(err)
}
if _, err = stmt.Exec(conf.ID, conf.InBounds, conf.OutBounds, conf.Revision, GetCurrentTimeFormat(), GetCurrentTimeFormat()); err != nil {
if _, err = stmt.Exec(conf.ID, conf.InBounds, conf.OutBounds,
conf.Revision, GetCurrentTimeFormat(), GetCurrentTimeFormat()); err != nil {
log.Errorf("[Store][database] create routing(%+v) err: %s", conf, err.Error())
return store.Error(err)
}
Expand Down Expand Up @@ -158,7 +159,8 @@ func (rs *routingConfigStore) DeleteRoutingConfigTx(tx store.Tx, serviceID strin
}

// GetRoutingConfigsForCache 缓存增量拉取
func (rs *routingConfigStore) GetRoutingConfigsForCache(mtime time.Time, firstUpdate bool) ([]*model.RoutingConfig, error) {
func (rs *routingConfigStore) GetRoutingConfigsForCache(mtime time.Time,
firstUpdate bool) ([]*model.RoutingConfig, error) {
str := `select id, in_bounds, out_bounds, revision,flag, ctime, mtime
from routing_config where mtime > $1`
if firstUpdate {
Expand Down
9 changes: 6 additions & 3 deletions store/postgresql/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,9 +995,11 @@ func addServiceMeta(tx *BaseTx, id string, meta map[string]string) error {
for key, value := range meta {
cnt++
if cnt == len(meta) {
str += fmt.Sprintf("($%d, $%d, $%d, '%v', '%v')", index, index+1, index+2, GetCurrentTimeFormat(), GetCurrentTimeFormat())
str += fmt.Sprintf("($%d, $%d, $%d, '%v', '%v')", index, index+1, index+2,
GetCurrentTimeFormat(), GetCurrentTimeFormat())
} else {
str += fmt.Sprintf("($%d, $%d, $%d, '%v', '%v'),", index, index+1, index+2, GetCurrentTimeFormat(), GetCurrentTimeFormat())
str += fmt.Sprintf("($%d, $%d, $%d, '%v', '%v'),", index, index+1, index+2,
GetCurrentTimeFormat(), GetCurrentTimeFormat())
}
index += 3

Expand Down Expand Up @@ -1171,7 +1173,8 @@ func filterInstance(filters *store.InstanceArgs, indexSort int) (string, []inter
// 生成子查询语句
// 多个metadata,取交集(and)
func filterMetadata(metas map[string]string, indexSort int) (string, []interface{}, int) {
str := fmt.Sprintf("(select id from service_metadata where $%d = $%d and $%d = $%d)", indexSort+1, indexSort+2, indexSort+3, indexSort+4)
str := fmt.Sprintf("(select id from service_metadata where $%d = $%d and $%d = $%d)",
indexSort+1, indexSort+2, indexSort+3, indexSort+4)
args := make([]interface{}, 0, 2)
for key, value := range metas {
args = append(args, key)
Expand Down
21 changes: 14 additions & 7 deletions store/postgresql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func boolToInt(v bool) int {
return 0
}

func genNamespaceWhereSQLAndArgs(str string, filter map[string][]string, order *Order, offset, limit int) (string, []interface{}) {
func genNamespaceWhereSQLAndArgs(str string, filter map[string][]string, order *Order,
offset, limit int) (string, []interface{}) {
num := 0
var sqlIndex = 1

Expand Down Expand Up @@ -136,9 +137,11 @@ func genServiceFilterSQL(filter map[string]string, indexSort int) (string, []int
firstIndex = false

if key == OwnerAttribute {
str += fmt.Sprintf(" (service.name, service.namespace) in (select service,namespace from owner_service_map where owner=$%d)", indexSort)
str += fmt.Sprintf(" (service.name, service.namespace) in (select service,"+
"namespace from owner_service_map where owner=$%d)", indexSort)
} else if key == "alias."+OwnerAttribute {
str += fmt.Sprintf(" (alias.name, alias.namespace) in (select service,namespace from owner_service_map where owner=$%d)", indexSort)
str += fmt.Sprintf(" (alias.name, alias.namespace) in (select service,namespace "+
"from owner_service_map where owner=$%d)", indexSort)
} else if key == "business" {
str += fmt.Sprintf(" %s like $%d", key, indexSort)
value = "%" + value + "%"
Expand Down Expand Up @@ -173,7 +176,8 @@ func genOrderAndPage(order *Order, page *Page, indexSort int) (string, []interfa
}

// genServiceAliasWhereSQLAndArgs 生成service alias查询数据的where语句和对应参数
func genServiceAliasWhereSQLAndArgs(str string, filter map[string]string, order *Order, offset uint32, limit uint32, indexSort int) (
func genServiceAliasWhereSQLAndArgs(str string, filter map[string]string, order *Order,
offset uint32, limit uint32, indexSort int) (
string, []interface{}) {
baseStr := str
filterStr, filterArgs, indexSort1 := genServiceFilterSQL(filter, indexSort)
Expand All @@ -188,7 +192,8 @@ func genServiceAliasWhereSQLAndArgs(str string, filter map[string]string, order
}

// genWhereSQLAndArgs 生成service和instance查询数据的where语句和对应参数
func genWhereSQLAndArgs(str string, filter, metaFilter map[string]string, order *Order, offset uint32, limit uint32) (string, []interface{}) {
func genWhereSQLAndArgs(str string, filter, metaFilter map[string]string, order *Order,
offset uint32, limit uint32) (string, []interface{}) {
baseStr := str
var (
args []interface{}
Expand Down Expand Up @@ -275,7 +280,8 @@ func genFilterSQL(filter map[string]string, index int) (string, []interface{}, i
}

func genInstanceMetadataArgs(metaFilter map[string]string, index int) (string, []interface{}, int) {
str := fmt.Sprintf(`instance.id in (select id from instance_metadata where $%d = $%d and $%d = $%d)`, index, index+1, index+2, index+3)
str := fmt.Sprintf(`instance.id in (select id from instance_metadata where $%d = $%d and $%d = $%d)`,
index, index+1, index+2, index+3)
args := make([]interface{}, 0, 2)
for k, v := range metaFilter {
args = append(args, k)
Expand All @@ -285,7 +291,8 @@ func genInstanceMetadataArgs(metaFilter map[string]string, index int) (string, [
}

// genRuleFilterSQL 根据规则的filter生成where相关的语句
func genRuleFilterSQL(tableName string, filter map[string]string, index int) (string, []interface{}, int) {
func genRuleFilterSQL(tableName string, filter map[string]string,
index int) (string, []interface{}, int) {
if len(filter) == 0 {
return "", nil, index
}
Expand Down
12 changes: 8 additions & 4 deletions store/postgresql/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ func (s *strategyStore) updateStrategy(strategy *model.ModifyStrategyDetail) err
if err != nil {
return err
}
if _, err = stmt.Exec([]interface{}{strategy.Action, strategy.Comment, GetCurrentTimeFormat(), strategy.ID}...); err != nil {
if _, err = stmt.Exec([]interface{}{strategy.Action, strategy.Comment,
GetCurrentTimeFormat(), strategy.ID}...); err != nil {
log.Error("[Store][Strategy] update strategy main info", zap.Error(err))
return err
}
Expand Down Expand Up @@ -589,8 +590,10 @@ func (s *strategyStore) queryStrategies(
countSql += " " + k + fmt.Sprintf(" like $%d ", idx)
args = append(args, "%"+v+"%")
} else if k == "ag.owner" {
querySql += fmt.Sprintf(" (ag.owner = $%d OR (ap.principal_id = $%d AND ap.principal_role = 1 )) ", idx, idx+1)
countSql += fmt.Sprintf(" (ag.owner = $%d OR (ap.principal_id = $%d AND ap.principal_role = 1 )) ", idx, idx+1)
querySql += fmt.Sprintf(" (ag.owner = $%d OR (ap.principal_id = $%d "+
"AND ap.principal_role = 1 )) ", idx, idx+1)
countSql += fmt.Sprintf(" (ag.owner = $%d OR (ap.principal_id = $%d "+
"AND ap.principal_role = 1 )) ", idx, idx+1)
idx += 1
args = append(args, v, v)
} else {
Expand Down Expand Up @@ -755,7 +758,8 @@ func (s *strategyStore) GetStrategyResources(principalId string,
return resArr, nil
}

func (s *strategyStore) getStrategyPrincipals(queryHander QueryHandler, id string) ([]model.Principal, error) {
func (s *strategyStore) getStrategyPrincipals(queryHander QueryHandler,
id string) ([]model.Principal, error) {

rows, err := queryHander("SELECT principal_id, principal_role FROM auth_principal WHERE strategy_id = $1", id)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions store/postgresql/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ func (u *userStore) updateUser(user *model.User) error {
tokenEnable = 0
}

modifySql := "UPDATE user SET password = $1, token = $2, comment = $3, token_enable = $4, mobile = $5, email = $6, " +
modifySql := "UPDATE user SET password = $1, token = $2, comment = $3, token_enable = $4, " +
"mobile = $5, email = $6, " +
" mtime = $7 WHERE id = $8 AND flag = 0"
stmt, err := tx.Prepare(modifySql)
if err != nil {
Expand Down Expand Up @@ -544,7 +545,8 @@ func (u *userStore) GetUsersForCache(mtime time.Time, firstUpdate bool) ([]*mode
}

// collectUsers General query user list
func (u *userStore) collectUsers(handler QueryHandler, querySql string, args []interface{}) ([]*model.User, error) {
func (u *userStore) collectUsers(handler QueryHandler, querySql string,
args []interface{}) ([]*model.User, error) {
rows, err := u.master.Query(querySql, args...)
if err != nil {
log.Error("[Store][User] list user ", zap.String("query sql", querySql), zap.Any("args", args))
Expand Down

0 comments on commit cd645e9

Please sign in to comment.