提交 12fe63af 编写于 作者: martianzhang's avatar martianzhang

check time format for ARG.003

上级 5f720cbb
......@@ -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()
......
......@@ -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())
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册