From cd645e90e607de4deb792eecf5c6519b6004fa1a Mon Sep 17 00:00:00 2001 From: zhangming03 Date: Thu, 29 Jun 2023 00:02:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BB=A3=E7=A0=81=E8=BF=87?= =?UTF-8?q?=E9=95=BF=E7=9A=84=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- store/postgresql/admin.go | 3 ++- .../postgresql/config_file_release_history.go | 3 ++- store/postgresql/config_file_tag.go | 3 ++- store/postgresql/instance.go | 9 +++++--- store/postgresql/namespace.go | 12 +++++++---- store/postgresql/routing_config.go | 6 ++++-- store/postgresql/service.go | 9 +++++--- store/postgresql/sql.go | 21 ++++++++++++------- store/postgresql/strategy.go | 12 +++++++---- store/postgresql/user.go | 6 ++++-- 10 files changed, 56 insertions(+), 28 deletions(-) diff --git a/store/postgresql/admin.go b/store/postgresql/admin.go index 99938d5..fb9f417 100644 --- a/store/postgresql/admin.go +++ b/store/postgresql/admin.go @@ -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) } diff --git a/store/postgresql/config_file_release_history.go b/store/postgresql/config_file_release_history.go index 7ed68ea..d43e130 100644 --- a/store/postgresql/config_file_release_history.go +++ b/store/postgresql/config_file_release_history.go @@ -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+"%") diff --git a/store/postgresql/config_file_tag.go b/store/postgresql/config_file_tag.go index 2bf1225..fff1660 100644 --- a/store/postgresql/config_file_tag.go +++ b/store/postgresql/config_file_tag.go @@ -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 diff --git a/store/postgresql/instance.go b/store/postgresql/instance.go index 6e75f75..9cd0515 100644 --- a/store/postgresql/instance.go +++ b/store/postgresql/instance.go @@ -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 @@ -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) diff --git a/store/postgresql/namespace.go b/store/postgresql/namespace.go index bd240e8..5ed8a63 100644 --- a/store/postgresql/namespace.go +++ b/store/postgresql/namespace.go @@ -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) } @@ -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"} @@ -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) diff --git a/store/postgresql/routing_config.go b/store/postgresql/routing_config.go index 4d7abcf..e072597 100644 --- a/store/postgresql/routing_config.go +++ b/store/postgresql/routing_config.go @@ -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) } @@ -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 { diff --git a/store/postgresql/service.go b/store/postgresql/service.go index 5f98f29..b7a3b81 100644 --- a/store/postgresql/service.go +++ b/store/postgresql/service.go @@ -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 @@ -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) diff --git a/store/postgresql/sql.go b/store/postgresql/sql.go index 5e9063c..78ed287 100644 --- a/store/postgresql/sql.go +++ b/store/postgresql/sql.go @@ -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 @@ -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 + "%" @@ -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) @@ -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{} @@ -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) @@ -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 } diff --git a/store/postgresql/strategy.go b/store/postgresql/strategy.go index a2e69c1..e8e33be 100644 --- a/store/postgresql/strategy.go +++ b/store/postgresql/strategy.go @@ -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 } @@ -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 { @@ -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 { diff --git a/store/postgresql/user.go b/store/postgresql/user.go index 5ee59a9..9fc5c8b 100644 --- a/store/postgresql/user.go +++ b/store/postgresql/user.go @@ -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 { @@ -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))