Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  RemoveSQLComment trim space
  • Loading branch information
martianzhang committed Nov 23, 2018
1 parent d23dfdb commit de99de7
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion advisor/heuristic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions cmd/soar/soar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/soar/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions database/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion database/explain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
/*
Expand Down
6 changes: 4 additions & 2 deletions database/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,19 @@ 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]) == "/*!") {
return s
}
return []byte("")
})
return strings.TrimSpace(string(res))
}

// 为了防止在 Online 环境进行误操作,通过 dangerousQuery 来判断能否在 Online 执行
Expand Down
21 changes: 21 additions & 0 deletions database/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
5 changes: 5 additions & 0 deletions database/testdata/TestRemoveSQLComments.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@





0 comments on commit de99de7

Please sign in to comment.