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

fix #121

  RemoveSQLComment trim space
上级 d23dfdb4
......@@ -1275,7 +1275,7 @@ func (q *Query4Audit) RuleMeaninglessWhere() Rule {
func (q *Query4Audit) RuleLoadFile() Rule {
var rule = q.RuleOK()
// 去除注释
sql := string(database.RemoveSQLComments([]byte(q.Query)))
sql := database.RemoveSQLComments(q.Query)
// 去除多余的空格和回车
sql = strings.Join(strings.Fields(sql), " ")
tks := ast.Tokenize(sql)
......
......@@ -120,8 +120,7 @@ func main() {
buf = string(bufBytes)
// 去除无用的备注和空格
sql = strings.TrimSpace(sql)
sql = string(database.RemoveSQLComments([]byte(sql)))
sql = database.RemoveSQLComments(sql)
if sql == "" {
common.Log.Debug("empty query or comment, buf: %s", buf)
continue
......
......@@ -175,7 +175,7 @@ func reportTool(sql string, bom []byte) (isContinue bool, exitCode int) {
fmt.Println(charset)
return false, 0
case "remove-comment":
fmt.Println(string(database.RemoveSQLComments([]byte(sql))))
fmt.Println(database.RemoveSQLComments(sql))
return false, 0
default:
return true, 0
......
......@@ -384,7 +384,7 @@ var ExplainExtra = map[string]string{
func findTablesInJSON(explainJSON string, depth int) {
common.Log.Debug("findTablesInJSON Enter: depth(%d), json(%s)", depth, explainJSON)
// 去除注释,语法检查
explainJSON = string(RemoveSQLComments([]byte(explainJSON)))
explainJSON = RemoveSQLComments(explainJSON)
if !gjson.Valid(explainJSON) {
return
}
......@@ -923,7 +923,7 @@ func parseVerticalExplainText(content string) (explainRows []*ExplainRow, err er
// 解析文本形式JSON Explain信息
func parseJSONExplainText(content string) (*ExplainJSON, error) {
explainJSON := new(ExplainJSON)
err := json.Unmarshal(RemoveSQLComments([]byte(content)), explainJSON)
err := json.Unmarshal([]byte(RemoveSQLComments(content)), explainJSON)
return explainJSON, err
}
......
......@@ -2366,7 +2366,7 @@ func TestExplain(t *testing.T) {
func TestParseExplainText(t *testing.T) {
for _, content := range exp {
pretty.Println(string(RemoveSQLComments([]byte(content))))
pretty.Println(RemoveSQLComments(content))
pretty.Println(ParseExplainText(content))
}
/*
......
......@@ -259,10 +259,11 @@ func (db *Connector) IsView(tbName string) bool {
}
// RemoveSQLComments 去除SQL中的注释
func RemoveSQLComments(sql []byte) []byte {
func RemoveSQLComments(sql string) string {
buf := []byte(sql)
cmtReg := regexp.MustCompile(`("(""|[^"])*")|('(''|[^'])*')|(--[^\n\r]*)|(#.*)|(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)`)
return cmtReg.ReplaceAllFunc(sql, func(s []byte) []byte {
res := cmtReg.ReplaceAllFunc(buf, func(s []byte) []byte {
if (s[0] == '"' && s[len(s)-1] == '"') ||
(s[0] == '\'' && s[len(s)-1] == '\'') ||
(string(s[:3]) == "/*!") {
......@@ -270,6 +271,7 @@ func RemoveSQLComments(sql []byte) []byte {
}
return []byte("")
})
return strings.TrimSpace(string(res))
}
// 为了防止在 Online 环境进行误操作,通过 dangerousQuery 来判断能否在 Online 执行
......
......@@ -88,3 +88,24 @@ func TestSource(t *testing.T) {
t.Error("Source result not match, expect 1, 1")
}
}
func TestRemoveSQLComments(t *testing.T) {
SQLs := []string{
`-- comment`,
`--`,
`# comment`,
`/* multi-line
comment*/`,
`--
-- comment`,
}
err := common.GoldenDiff(func() {
for _, sql := range SQLs {
fmt.Println(RemoveSQLComments(sql))
}
}, t.Name(), update)
if err != nil {
t.Error(err)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册