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

fix #104

  case insensitive regex @ CLA.009
上级 e5a73668
...@@ -629,8 +629,8 @@ func (q *Query4Audit) RuleOrderByExpr() Rule { ...@@ -629,8 +629,8 @@ func (q *Query4Audit) RuleOrderByExpr() Rule {
var rule = q.RuleOK() var rule = q.RuleOK()
var orderByCols []string var orderByCols []string
var selectCols []string var selectCols []string
funcExp := regexp.MustCompile(`[a-z0-9]\(`) funcExp := regexp.MustCompile(`(?i)[a-z0-9]\(`)
allowExp := regexp.MustCompile("[a-z0-9_,.` ()]") allowExp := regexp.MustCompile("(?i)[a-z0-9_,.` ()]")
err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) { err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
switch n := node.(type) { switch n := node.(type) {
case sqlparser.OrderBy: case sqlparser.OrderBy:
......
...@@ -319,19 +319,22 @@ func TestRuleExplicitOrderBy(t *testing.T) { ...@@ -319,19 +319,22 @@ func TestRuleExplicitOrderBy(t *testing.T) {
// CLA.009 // CLA.009
func TestRuleOrderByExpr(t *testing.T) { func TestRuleOrderByExpr(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName()) common.Log.Debug("Entering function: %s", common.GetFunctionName())
sqls := []string{ sqls := [][]string{
"SELECT col FROM tbl order by cola - colb;", // order by 列运算 {
"SELECT cola - colb col FROM tbl order by col;", // 别名为列运算 "SELECT col FROM tbl order by cola - colb;", // order by 列运算
"SELECT cola FROM tbl order by from_unixtime(col);", // order by 函数运算 "SELECT cola - colb col FROM tbl order by col;", // 别名为列运算
"SELECT from_unixtime(col) cola FROM tbl order by cola;", // 别名为函数运算 "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 tbl ORDER BY col`,
// "SELECT tbl.col FROM tb, tbl WHERE tbl.tag_id = tb.id ORDER BY tbl.col", "SELECT sum(col) AS col FROM tbl ORDER BY dt",
// "SELECT col FROM tbl order by `timestamp`;", // 列名为关键字 "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) q, err := NewQuery4Audit(sql)
if err == nil { if err == nil {
rule := q.RuleOrderByExpr() rule := q.RuleOrderByExpr()
...@@ -342,6 +345,17 @@ func TestRuleOrderByExpr(t *testing.T) { ...@@ -342,6 +345,17 @@ func TestRuleOrderByExpr(t *testing.T) {
t.Error("sqlparser.Parse Error:", err) 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()) common.Log.Debug("Exiting function: %s", common.GetFunctionName())
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册