From 3e658c97b5e6982f0d5f128b274aa4fa9fdc1e52 Mon Sep 17 00:00:00 2001 From: littleniannian Date: Fri, 27 Dec 2024 16:58:39 +0800 Subject: [PATCH] fix: spec new category --- sqle/driver/mysql/plocale/active.en.toml | 1 + sqle/driver/mysql/plocale/active.zh.toml | 1 + sqle/driver/mysql/plocale/message_zh.go | 1 + sqle/model/utils.go | 31 ++++++++++++++++++++---- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/sqle/driver/mysql/plocale/active.en.toml b/sqle/driver/mysql/plocale/active.en.toml index 34ec0de87..57fd6ae80 100644 --- a/sqle/driver/mysql/plocale/active.en.toml +++ b/sqle/driver/mysql/plocale/active.en.toml @@ -678,6 +678,7 @@ PrimaryKeyExistMessage = "Primary key already exists, cannot add it again." PrimaryKeyNotExistMessage = "There is no primary key currently, cannot execute deletion." RuleTypeDDLConvention = "DDL convention" RuleTypeDMLConvention = "DML convention" +RuleTypeDQLConvention = "DQL convention" RuleTypeDistributedConvention = "Distributed convention" RuleTypeExecutePlan = "Execute plan" RuleTypeGlobalConfig = "Global configuration" diff --git a/sqle/driver/mysql/plocale/active.zh.toml b/sqle/driver/mysql/plocale/active.zh.toml index b924ba2fb..aa4d33ac3 100644 --- a/sqle/driver/mysql/plocale/active.zh.toml +++ b/sqle/driver/mysql/plocale/active.zh.toml @@ -678,6 +678,7 @@ PrimaryKeyExistMessage = "已经存在主键,不能再添加" PrimaryKeyNotExistMessage = "当前没有主键,不能执行删除" RuleTypeDDLConvention = "DDL规范" RuleTypeDMLConvention = "DML规范" +RuleTypeDQLConvention = "DQL规范" RuleTypeDistributedConvention = "分布式规范" RuleTypeExecutePlan = "执行计划" RuleTypeGlobalConfig = "全局配置" diff --git a/sqle/driver/mysql/plocale/message_zh.go b/sqle/driver/mysql/plocale/message_zh.go index 8cf8db1b9..3543948e9 100644 --- a/sqle/driver/mysql/plocale/message_zh.go +++ b/sqle/driver/mysql/plocale/message_zh.go @@ -111,6 +111,7 @@ var ( RuleTypeIndexingConvention = &i18n.Message{ID: "RuleTypeIndexingConvention", Other: "索引规范"} RuleTypeDDLConvention = &i18n.Message{ID: "RuleTypeDDLConvention", Other: "DDL规范"} RuleTypeDMLConvention = &i18n.Message{ID: "RuleTypeDMLConvention", Other: "DML规范"} + RuleTypeDQLConvention = &i18n.Message{ID: "RuleTypeDQLConvention", Other: "DQL规范"} RuleTypeUsageSuggestion = &i18n.Message{ID: "RuleTypeUsageSuggestion", Other: "使用建议"} RuleTypeIndexOptimization = &i18n.Message{ID: "RuleTypeIndexOptimization", Other: "索引优化"} RuleTypeIndexInvalidation = &i18n.Message{ID: "RuleTypeIndexInvalidation", Other: "索引失效"} diff --git a/sqle/model/utils.go b/sqle/model/utils.go index 66d1f2a31..27fbe7b86 100644 --- a/sqle/model/utils.go +++ b/sqle/model/utils.go @@ -556,13 +556,29 @@ func (s *Storage) CreateDefaultTemplateIfNotExist(projectId ProjectUID, rules ma return nil } -func mappingToNewCategory(ruleName string, oldCategory string) []string { +func mappingToNewCategory(ruleDesc string, oldCategory string) []string { // 当旧规则是命名规范的映射关系 if oldCategory == plocale.RuleTypeNamingConvention.Other { - if strings.Contains(ruleName, "database") || strings.Contains(ruleName, "object") { + if strings.Contains(ruleDesc, plocale.RuleTagDatabase.Other) || strings.Contains(ruleDesc, "对象") { return []string{plocale.RuleTagDatabase.ID} - } else if strings.Contains(ruleName, "index") || strings.Contains(ruleName, "pk") { + } else if strings.Contains(ruleDesc, plocale.RuleTagIndex.Other) || strings.Contains(ruleDesc, "主键") { return []string{plocale.RuleTagIndex.ID} + } else if strings.Contains(ruleDesc, plocale.RuleTagTable.Other) { + return []string{plocale.RuleTagTable.ID} + } else if strings.Contains(ruleDesc, plocale.RuleTagView.Other) { + return []string{plocale.RuleTagView.ID} + } else if strings.Contains(ruleDesc, plocale.RuleTagColumn.Other) { + return []string{plocale.RuleTagColumn.ID} + } else if strings.Contains(ruleDesc, plocale.RuleTagProcedure.Other) { + return []string{plocale.RuleTagProcedure.ID} + } else if strings.Contains(ruleDesc, plocale.RuleTagFunction.Other) { + return []string{plocale.RuleTagFunction.ID} + } else if strings.Contains(ruleDesc, plocale.RuleTagTrigger.Other) { + return []string{plocale.RuleTagTrigger.ID} + } else if strings.Contains(ruleDesc, plocale.RuleTagEvent.Other) { + return []string{plocale.RuleTagEvent.ID} + } else if strings.Contains(ruleDesc, plocale.RuleTagUser.Other) { + return []string{plocale.RuleTagUser.ID} } else { return []string{ plocale.RuleTagDatabase.ID, plocale.RuleTagTablespace.ID, plocale.RuleTagTable.ID, plocale.RuleTagColumn.ID, plocale.RuleTagIndex.ID, plocale.RuleTagView.ID, plocale.RuleTagProcedure.ID, plocale.RuleTagFunction.ID, plocale.RuleTagTrigger.ID, plocale.RuleTagEvent.ID, plocale.RuleTagUser.ID} @@ -599,6 +615,9 @@ var categoryMapping = map[string]map[string][]string{ plocale.RuleTypeDMLConvention.Other: { plocale.RuleCategorySQL.ID: {plocale.RuleTagDML.ID}, }, + plocale.RuleTypeDQLConvention.Other: { + plocale.RuleCategorySQL.ID: {plocale.RuleTagDML.ID}, + }, plocale.RuleTypeUsageSuggestion.Other: { plocale.RuleCategoryAuditPurpose.ID: {plocale.RuleTagMaintenance.ID}, }, @@ -611,7 +630,9 @@ var categoryMapping = map[string]map[string][]string{ } func (s *Storage) UpdateRuleCategoryRels(rule *Rule) error { - oldCategory := rule.I18nRuleInfo.GetRuleInfoByLangTag(language.Chinese).Category + ruleInfo := rule.I18nRuleInfo.GetRuleInfoByLangTag(language.Chinese) + oldCategory := ruleInfo.Category + ruleDesc := ruleInfo.Desc _, existed, err := s.FirstAuditRuleCategoryRelByRule(rule.Name, rule.DBType) if err != nil { return err @@ -620,7 +641,7 @@ func (s *Storage) UpdateRuleCategoryRels(rule *Rule) error { if existed { return nil } - tags := mappingToNewCategory(rule.Name, oldCategory) + tags := mappingToNewCategory(ruleDesc, oldCategory) // 获取分类表中的分类信息 auditRuleCategories, err := s.GetAuditRuleCategoryByTagIn(tags) if err != nil {