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

fix #261

上级 ea0e68f2
...@@ -252,12 +252,12 @@ func (db *Connector) IsView(tbName string) bool { ...@@ -252,12 +252,12 @@ func (db *Connector) IsView(tbName string) bool {
// RemoveSQLComments 去除SQL中的注释 // RemoveSQLComments 去除SQL中的注释
func RemoveSQLComments(sql string) string { func RemoveSQLComments(sql string) string {
buf := []byte(sql) buf := []byte(sql)
// ("(""|[^"])*") 双引号中的内容 // ("(""|[^"]|(\"))*") 双引号中的内容, "", "\""
// ('(''|[^'])*') 单引号中的内容 // ('(''|[^']|(\'))*') 单引号中的内容, '', '\''
// (--[^\n\r]*) 双减号注释 // (--[^\n\r]*) 双减号注释
// (#.*) 井号注释 // (#.*) 井号注释
// (/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/) 多行注释 // (/\*([^*]|[\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 { res := commentRegex.ReplaceAllFunc(buf, func(s []byte) []byte {
if (s[0] == '"' && s[len(s)-1] == '"') || if (s[0] == '"' && s[len(s)-1] == '"') ||
......
...@@ -149,6 +149,8 @@ func TestRemoveSQLComments(t *testing.T) { ...@@ -149,6 +149,8 @@ func TestRemoveSQLComments(t *testing.T) {
// Notice: double dash without space not comment, eg. `--not comment` // Notice: double dash without space not comment, eg. `--not comment`
common.Log.Debug("Entering function: %s", common.GetFunctionName()) common.Log.Debug("Entering function: %s", common.GetFunctionName())
SQLs := []string{ SQLs := []string{
`select 'c#\'#not comment'`,
`select "c#\"#not comment"`,
`-- comment`, `-- comment`,
`--`, `--`,
`# comment`, `# comment`,
......
select 'c#\'#not comment'
select "c#\"#not comment"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册