diff --git a/database/mysql.go b/database/mysql.go index aebf304b6afec6f2b59f768ac9febded2b085e76..3ec6c9d6cb9f060da3ce65a49667c3bb88b51546 100644 --- a/database/mysql.go +++ b/database/mysql.go @@ -252,12 +252,12 @@ func (db *Connector) IsView(tbName string) bool { // RemoveSQLComments 去除SQL中的注释 func RemoveSQLComments(sql string) string { buf := []byte(sql) - // ("(""|[^"])*") 双引号中的内容 - // ('(''|[^'])*') 单引号中的内容 + // ("(""|[^"]|(\"))*") 双引号中的内容, "", "\"" + // ('(''|[^']|(\'))*') 单引号中的内容, '', '\'' // (--[^\n\r]*) 双减号注释 // (#.*) 井号注释 // (/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/) 多行注释 - commentRegex := regexp.MustCompile(`("(""|[^"])*")|('(''|[^'])*')|(--[^\n\r]*)|(#.*)|(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)`) + commentRegex := regexp.MustCompile(`("(""|[^"]|(\"))*")|('(''|[^']|(\'))*')|(--[^\n\r]*)|(#.*)|(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)`) res := commentRegex.ReplaceAllFunc(buf, func(s []byte) []byte { if (s[0] == '"' && s[len(s)-1] == '"') || diff --git a/database/mysql_test.go b/database/mysql_test.go index cd9fa50df7eec4f3085745a06a3e91b6dbbfcdd4..04ff7756e9b5d352ced2a4a2aba1c0d21e75318a 100644 --- a/database/mysql_test.go +++ b/database/mysql_test.go @@ -149,6 +149,8 @@ func TestRemoveSQLComments(t *testing.T) { // Notice: double dash without space not comment, eg. `--not comment` common.Log.Debug("Entering function: %s", common.GetFunctionName()) SQLs := []string{ + `select 'c#\'#not comment'`, + `select "c#\"#not comment"`, `-- comment`, `--`, `# comment`, diff --git a/database/testdata/TestRemoveSQLComments.golden b/database/testdata/TestRemoveSQLComments.golden index 6fb66a5e2f01e878ab235d7714bb18eb45225504..bdf9468b7f0801e6d1d5468bc564e508a51126d4 100644 --- a/database/testdata/TestRemoveSQLComments.golden +++ b/database/testdata/TestRemoveSQLComments.golden @@ -1,3 +1,5 @@ +select 'c#\'#not comment' +select "c#\"#not comment"