提交 6c844313 编写于 作者: martianzhang's avatar martianzhang

new heuristic rule ARG.013 for full-width quote

上级 c08fc159
......@@ -418,7 +418,7 @@ func (q *Query4Audit) RuleOffsetLimit() Rule {
switch v := n.Offset.(type) {
case *sqlparser.SQLVal:
offset, err := strconv.Atoi(string(v.Val))
// 检查一下Offset阈值,太小了给这个建议也没什么用,阈值写死了没加配置
// TODO: 检查一下Offset阈值,太小了给这个建议也没什么用,阈值写死了没加配置
if err == nil && offset > 1000 {
rule = HeuristicRules["CLA.003"]
return false, nil
......@@ -2230,6 +2230,24 @@ func (q *Query4Audit) RuleInsertValues() Rule {
return rule
}
// RuleFullWidthQuote ARG.013
func (q *Query4Audit) RuleFullWidthQuote() Rule {
var rule = q.RuleOK()
for _, node := range q.TiStmt {
switch n := node.(type) {
case *tidb.CreateTableStmt, *tidb.AlterTableStmt:
var sb strings.Builder
ctx := format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)
if err := n.Restore(ctx); err == nil {
if strings.Contains(sb.String(), `“”`) || strings.Contains(sb.String(), `‘’`) {
rule = HeuristicRules["ARG.013"]
}
}
}
}
return rule
}
// RuleUNIONUsage SUB.002
func (q *Query4Audit) RuleUNIONUsage() Rule {
var rule = q.RuleOK()
......
......@@ -1886,6 +1886,7 @@ func TestRuleSpaceWithQuote(t *testing.T) {
`SELECT ' a';`,
`SELECT "a ";`,
`SELECT " a";`,
`create table tb ( a varchar(10) default ' ');`,
},
{
`select ''`,
......@@ -2036,6 +2037,44 @@ func TestRuleInsertValues(t *testing.T) {
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}
// ARG.013
func TestRuleFullWidthQuote(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
sqls := [][]string{
{
`CREATE TABLE tb (a varchar(10) default '“”')`,
`CREATE TABLE tb (a varchar(10) default '‘’')`,
`ALTER TABLE tb ADD COLUMN a VARCHAR(10) DEFAULT "“”"`,
},
{
`CREATE TABLE tb (a varchar(10) default '""')`,
},
}
for _, sql := range sqls[0] {
q, err := NewQuery4Audit(sql)
if err == nil {
rule := q.RuleFullWidthQuote()
if rule.Item != "ARG.013" {
t.Error("Rule not match:", rule.Item, "Expect : ARG.013")
}
} else {
t.Error("sqlparser.Parse Error:", err)
}
}
for _, sql := range sqls[1] {
q, err := NewQuery4Audit(sql)
if err == nil {
rule := q.RuleFullWidthQuote()
if rule.Item != "OK" {
t.Error("Rule not match:", rule.Item, "Expect : OK")
}
} else {
t.Error("sqlparser.Parse Error:", err)
}
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}
// SUB.002
func TestRuleUNIONUsage(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
......
......@@ -272,6 +272,14 @@ func init() {
Case: "INSERT INTO tb (a) VALUES (1), (2)",
Func: (*Query4Audit).RuleInsertValues,
},
"ARG.013": {
Item: "ARG.013",
Severity: "L0",
Summary: "DDL 语句中使用了中文全角引号",
Content: "DDL 语句中使用了中文全角引号“”或‘’,这可能是书写错误,请确认是否符合预期。",
Case: "CREATE TABLE tb (a varchar(10) default '“”'",
Func: (*Query4Audit).RuleFullWidthQuote,
},
"CLA.001": {
Item: "CLA.001",
Severity: "L4",
......
......@@ -192,6 +192,16 @@ select id from t where num not in(1,2,3);
```sql
INSERT INTO tb (a) VALUES (1), (2)
```
## DDL 语句中使用了中文全角引号
* **Item**:ARG.013
* **Severity**:L0
* **Content**:DDL 语句中使用了中文全角引号“”或‘’,这可能是书写错误,请确认是否符合预期。
* **Case**:
```sql
CREATE TABLE tb (a varchar(10) default '“”'
```
## 最外层 SELECT 未指定 WHERE 条件
* **Item**:CLA.001
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册