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

add comment and test case for RemoveSQLComments

上级 1f50907c
......@@ -252,9 +252,14 @@ func (db *Connector) IsView(tbName string) bool {
// RemoveSQLComments 去除SQL中的注释
func RemoveSQLComments(sql string) string {
buf := []byte(sql)
cmtReg := regexp.MustCompile(`("(""|[^"])*")|('(''|[^'])*')|(--[^\n\r]*)|(#.*)|(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)`)
res := cmtReg.ReplaceAllFunc(buf, func(s []byte) []byte {
// ("(""|[^"])*") 双引号中的内容
// ('(''|[^'])*') 单引号中的内容
// (--[^\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] == '"') ||
(s[0] == '\'' && s[len(s)-1] == '\'') ||
(string(s[:3]) == "/*!") {
......
......@@ -146,17 +146,18 @@ func TestVersion(t *testing.T) {
}
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{
`-- comment`,
`--`,
`# comment`,
`#comment`,
`/* multi-line
comment*/`,
`--
-- comment`,
}
err := common.GoldenDiff(func() {
for _, sql := range SQLs {
fmt.Println(RemoveSQLComments(sql))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册