From b11fa0cd816a03fdc115e99561f4bba0d3693131 Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Sat, 17 Nov 2018 14:44:51 +0800 Subject: [PATCH] fix #104 case insensitive regex @ CLA.009 --- advisor/heuristic.go | 4 ++-- advisor/heuristic_test.go | 38 ++++++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/advisor/heuristic.go b/advisor/heuristic.go index c9f6f83..3da78ab 100644 --- a/advisor/heuristic.go +++ b/advisor/heuristic.go @@ -629,8 +629,8 @@ func (q *Query4Audit) RuleOrderByExpr() Rule { var rule = q.RuleOK() var orderByCols []string var selectCols []string - funcExp := regexp.MustCompile(`[a-z0-9]\(`) - allowExp := regexp.MustCompile("[a-z0-9_,.` ()]") + funcExp := regexp.MustCompile(`(?i)[a-z0-9]\(`) + allowExp := regexp.MustCompile("(?i)[a-z0-9_,.` ()]") err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { switch n := node.(type) { case sqlparser.OrderBy: diff --git a/advisor/heuristic_test.go b/advisor/heuristic_test.go index ef4f850..de89233 100644 --- a/advisor/heuristic_test.go +++ b/advisor/heuristic_test.go @@ -319,19 +319,22 @@ func TestRuleExplicitOrderBy(t *testing.T) { // CLA.009 func TestRuleOrderByExpr(t *testing.T) { common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "SELECT col FROM tbl order by cola - colb;", // order by 列运算 - "SELECT cola - colb col FROM tbl order by col;", // 别名为列运算 - "SELECT cola FROM tbl order by from_unixtime(col);", // order by 函数运算 - "SELECT from_unixtime(col) cola FROM tbl order by cola;", // 别名为函数运算 - - // 反面例子 - // `SELECT tbl.col FROM tbl ORDER BY col`, - // "SELECT sum(col) AS col FROM tbl ORDER BY dt", - // "SELECT tbl.col FROM tb, tbl WHERE tbl.tag_id = tb.id ORDER BY tbl.col", - // "SELECT col FROM tbl order by `timestamp`;", // 列名为关键字 + sqls := [][]string{ + { + "SELECT col FROM tbl order by cola - colb;", // order by 列运算 + "SELECT cola - colb col FROM tbl order by col;", // 别名为列运算 + "SELECT cola FROM tbl order by from_unixtime(col);", // order by 函数运算 + "SELECT from_unixtime(col) cola FROM tbl order by cola;", // 别名为函数运算 + }, + { + `SELECT tbl.col FROM tbl ORDER BY col`, + "SELECT sum(col) AS col FROM tbl ORDER BY dt", + "SELECT tbl.col FROM tb, tbl WHERE tbl.tag_id = tb.id ORDER BY tbl.col", + "SELECT col FROM tbl order by `timestamp`;", // 列名为关键字 + "select col from tb where cl = 1 order by APPLY_TIME", // issue #104 case sensitive + }, } - for _, sql := range sqls { + for _, sql := range sqls[0] { q, err := NewQuery4Audit(sql) if err == nil { rule := q.RuleOrderByExpr() @@ -342,6 +345,17 @@ func TestRuleOrderByExpr(t *testing.T) { t.Error("sqlparser.Parse Error:", err) } } + for _, sql := range sqls[1] { + q, err := NewQuery4Audit(sql) + if err == nil { + rule := q.RuleOrderByExpr() + 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()) } -- GitLab