From cd17cceda8b324b6b70a5b4a510b4bd7bf555f9a Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Tue, 8 Oct 2019 13:56:23 +0800 Subject: [PATCH] fix #229 SELECT c1, c2, c3, FROM tb WHERE id < 1000 AND content="mytest* as test" --- advisor/heuristic.go | 8 +++++--- advisor/heuristic_test.go | 20 +++++++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/advisor/heuristic.go b/advisor/heuristic.go index f667bf6..8867dd1 100644 --- a/advisor/heuristic.go +++ b/advisor/heuristic.go @@ -64,9 +64,11 @@ func (q *Query4Audit) RuleImplicitAlias() Rule { // RuleStarAlias ALI.002 func (q *Query4Audit) RuleStarAlias() Rule { var rule = q.RuleOK() - re := regexp.MustCompile(`(?i)(\*\s+as\b)`) - if re.FindString(q.Query) != "" { - rule = HeuristicRules["ALI.002"] + tkns := ast.Tokenizer(q.Query) + for i, tkn := range tkns { + if tkn.Val == "*" && i+1 < len(tkns) && tkns[i+1].Val == "as" { + rule = HeuristicRules["ALI.002"] + } } return rule } diff --git a/advisor/heuristic_test.go b/advisor/heuristic_test.go index 115cfcf..0243243 100644 --- a/advisor/heuristic_test.go +++ b/advisor/heuristic_test.go @@ -60,16 +60,30 @@ func TestRuleImplicitAlias(t *testing.T) { // ALI.002 func TestRuleStarAlias(t *testing.T) { common.Log.Debug("Entering function: %s", common.GetFunctionName()) - sqls := []string{ - "select tbl.* as c1,c2,c3 from tbl where id < 1000", + sqls := [][]string{ + { + "select tbl.* AS c1,c2,c3 from tbl where id < 1000", + "SELECT * as", + }, + { + `SELECT c1, c2, c3, FROM tb WHERE id < 1000 AND content="mytest* as test"`, + `select *`, + }, } - for _, sql := range sqls { + for _, sql := range sqls[0] { q, _ := NewQuery4Audit(sql) rule := q.RuleStarAlias() if rule.Item != "ALI.002" { t.Error("Rule not match:", rule.Item, "Expect : ALI.002") } } + for _, sql := range sqls[1] { + q, _ := NewQuery4Audit(sql) + rule := q.RuleStarAlias() + if rule.Item != "OK" { + t.Error("Rule not match:", rule.Item, "Expect : OK") + } + } common.Log.Debug("Exiting function: %s", common.GetFunctionName()) } -- GitLab