From 12fe63af998e07acabd1196032485d81d2e72c64 Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Wed, 13 Jan 2021 18:57:36 +0800 Subject: [PATCH] check time format for ARG.003 --- advisor/heuristic.go | 24 +++++++++++++++++++++++- advisor/heuristic_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/advisor/heuristic.go b/advisor/heuristic.go index 4d27551..700e81c 100644 --- a/advisor/heuristic.go +++ b/advisor/heuristic.go @@ -279,7 +279,7 @@ func (idxAdv *IndexAdvisor) RuleImplicitConversion() Rule { continue } - // 检查排序排序不一致导致的隐式数据转换 + // 检查 collation 排序不一致导致的隐式数据转换 common.Log.Debug("Collation: `%s`.`%s` (%s) VS `%s`.`%s` (%s)", colList[0].Table, colList[0].Name, colList[0].Collation, colList[1].Table, colList[1].Name, colList[1].Collation) @@ -322,6 +322,7 @@ func (idxAdv *IndexAdvisor) RuleImplicitConversion() Rule { isCovered := true if tps, ok := typMap[val.Type]; ok { for _, tp := range tps { + // colList[0].DataType, eg. year(4) if strings.HasPrefix(colList[0].DataType, tp) { isCovered = false } @@ -339,6 +340,18 @@ func (idxAdv *IndexAdvisor) RuleImplicitConversion() Rule { common.Log.Debug("Implicit data type conversion: %s", c) content = append(content, c) + } else { + // 检查时间格式,如:"", "2020-0a-01" + switch strings.Split(colList[0].DataType, "(")[0] { + case "date", "time", "datetime", "timestamp", "year": + if !timeFormatCheck(string(val.Val)) { + c := fmt.Sprintf("%s 表中列 %s 的时间格式错误,%s。", colList[0].Table, colList[0].Name, string(val.Val)) + common.Log.Debug("Implicit data type conversion: %s", c) + content = append(content, c) + } + // TODO: 各种数据类型格式检查 + default: + } } } @@ -355,6 +368,15 @@ func (idxAdv *IndexAdvisor) RuleImplicitConversion() Rule { return rule } +// timeFormatCheck 时间格式检查,格式正确返回 true,格式错误返回 false +func timeFormatCheck(t string) bool { + // 不允许为空,但允许时间前后有空格 + t = strings.TrimSpace(t) + // 仅允许 数字、减号、冒号、空格 + allowChars := regexp.MustCompile(`^[\-0-9: ]+$`) + return allowChars.MatchString(t) +} + // RuleNoWhere CLA.001 & CLA.014 & CLA.015 func (q *Query4Audit) RuleNoWhere() Rule { var rule = q.RuleOK() diff --git a/advisor/heuristic_test.go b/advisor/heuristic_test.go index fdbf10a..b7402dc 100644 --- a/advisor/heuristic_test.go +++ b/advisor/heuristic_test.go @@ -150,6 +150,31 @@ func TestRuleEqualLike(t *testing.T) { common.Log.Debug("Exiting function: %s", common.GetFunctionName()) } +// ARG.003 +// TODO: + +func TestTimeFormatError(t *testing.T) { + rightTimes := []string{ + `2020-01-01`, + } + for _, rt := range rightTimes { + if !timeFormatCheck(rt) { + t.Error("wrong time format") + } + } + + wrongTimes := []string{ + ``, // 空时间 + `2020-01-01 abc`, // 含英文字符 + `2020–02-15 23:59:59`, // 2020 后面的不是减号,是个 连接符 + } + for _, wt := range wrongTimes { + if timeFormatCheck(wt) { + t.Error("wrong time format") + } + } +} + // CLA.001 func TestRuleNoWhere(t *testing.T) { common.Log.Debug("Entering function: %s", common.GetFunctionName()) -- GitLab