From 6c844313bb4ff9e2a4e92c9fc462a2e63d7137f9 Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Mon, 23 Sep 2019 15:55:26 +0800 Subject: [PATCH] new heuristic rule ARG.013 for full-width quote --- advisor/heuristic.go | 20 +++++++++++++++++++- advisor/heuristic_test.go | 39 +++++++++++++++++++++++++++++++++++++++ advisor/rules.go | 8 ++++++++ doc/heuristic.md | 10 ++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/advisor/heuristic.go b/advisor/heuristic.go index 68321e0..f667bf6 100644 --- a/advisor/heuristic.go +++ b/advisor/heuristic.go @@ -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() diff --git a/advisor/heuristic_test.go b/advisor/heuristic_test.go index 41f135c..115cfcf 100644 --- a/advisor/heuristic_test.go +++ b/advisor/heuristic_test.go @@ -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()) diff --git a/advisor/rules.go b/advisor/rules.go index 8614cb6..ad12c9a 100644 --- a/advisor/rules.go +++ b/advisor/rules.go @@ -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", diff --git a/doc/heuristic.md b/doc/heuristic.md index 91ad11d..7c02c08 100644 --- a/doc/heuristic.md +++ b/doc/heuristic.md @@ -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 -- GitLab