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

fix #173 with JSONFind

  WHERE col = col = '' and col1 = 'xx'
上级 26574fea
......@@ -31,6 +31,7 @@ import (
"github.com/percona/go-mysql/query"
tidb "github.com/pingcap/parser/ast"
"github.com/pingcap/parser/mysql"
"github.com/tidwall/gjson"
"vitess.io/vitess/go/vt/sqlparser"
)
......@@ -1312,37 +1313,16 @@ func (q *Query4Audit) RuleLoadFile() Rule {
func (q *Query4Audit) RuleMultiCompare() Rule {
var rule = q.RuleOK()
if q.TiStmt != nil {
for _, tiStmt := range q.TiStmt {
switch node := tiStmt.(type) {
case *tidb.SelectStmt:
switch where := node.Where.(type) {
case *tidb.BinaryOperationExpr:
switch where.L.(type) {
case *tidb.BinaryOperationExpr:
if where.Op.String() == "eq" {
rule = HeuristicRules["RES.009"]
}
}
}
case *tidb.UpdateStmt:
switch where := node.Where.(type) {
case *tidb.BinaryOperationExpr:
switch where.L.(type) {
case *tidb.BinaryOperationExpr:
if where.Op.String() == "eq" {
rule = HeuristicRules["RES.009"]
}
}
}
case *tidb.DeleteStmt:
switch where := node.Where.(type) {
case *tidb.BinaryOperationExpr:
switch where.L.(type) {
case *tidb.BinaryOperationExpr:
if where.Op.String() == "eq" {
rule = HeuristicRules["RES.009"]
}
}
json := ast.StmtNode2JSON(q.Query, "", "")
whereJSON := common.JSONFind(json, "Where")
for _, where := range whereJSON {
conds := []string{where}
conds = append(conds, common.JSONFind(where, "L")...)
conds = append(conds, common.JSONFind(where, "R")...)
for _, cond := range conds {
if gjson.Get(cond, "Op").Int() == 7 && gjson.Get(cond, "L.Op").Int() == 7 {
rule = HeuristicRules["RES.009"]
return rule
}
}
}
......
......@@ -946,6 +946,9 @@ func TestRuleMultiCompare(t *testing.T) {
sqls := [][]string{
{
"SELECT * FROM tbl WHERE col = col = 'abc'",
"SELECT * FROM tbl WHERE col = 'def' and col = col = 'abc'",
"SELECT * FROM tbl WHERE col = 'def' or col = col = 'abc'",
"SELECT * FROM tbl WHERE col = col = 'abc' and col = 'def'",
"UPDATE tbl set col = 1 WHERE col = col = 'abc'",
"DELETE FROM tbl WHERE col = col = 'abc'",
},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册