From b23bb1e0c4fe2abe7569b197e0882a74acf53e5f Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Sun, 24 Jan 2021 23:25:43 +0800 Subject: [PATCH] fix #261 --- database/mysql.go | 6 +++--- database/mysql_test.go | 2 ++ database/testdata/TestRemoveSQLComments.golden | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/database/mysql.go b/database/mysql.go index aebf304..3ec6c9d 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 cd9fa50..04ff775 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 6fb66a5..bdf9468 100644 --- a/database/testdata/TestRemoveSQLComments.golden +++ b/database/testdata/TestRemoveSQLComments.golden @@ -1,3 +1,5 @@ +select 'c#\'#not comment' +select "c#\"#not comment" -- GitLab