Skip to content

Commit f594360

Browse files
authored
Merge pull request #2950 from actiontech/fix_sql_line_comment
fix: scanner扫描带注释的xml文件报错
2 parents c2485e4 + 4244772 commit f594360

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

sqle/cmd/scannerd/scanners/common/common_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,26 @@ func TestClearComments(t *testing.T) {
111111
}
112112
}
113113
}
114+
115+
func TestClearLineComments(t *testing.T) {
116+
testCases := []struct {
117+
input string
118+
expected string
119+
}{
120+
{
121+
"SELECT -- id,\n -- name,\n id, name FROM table;",
122+
"SELECT id, name FROM table",
123+
},
124+
{
125+
"SELECT -- id,\n -- name,\n -- age,\n id, name, age FROM table;",
126+
"SELECT id, name, age FROM table",
127+
},
128+
}
129+
130+
for _, tc := range testCases {
131+
actual := clearComments(tc.input)
132+
if actual != tc.expected {
133+
t.Errorf("Expected %s, got %s", tc.expected, actual)
134+
}
135+
}
136+
}

sqle/cmd/scannerd/scanners/common/parse.go

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func Parse(_ context.Context, sqlText string) ([]driverV2.Node, error) {
4646
func clearComments(sqlText string) string {
4747
// 将注释替换为一个空格,防止语句粘连
4848
sqlText = regexp.MustCompile(`(?s)/\*.*?\*/`).ReplaceAllString(sqlText, " ")
49+
sqlText = regexp.MustCompile(`--.*`).ReplaceAllString(sqlText, " ")
4950
// 去除结尾分号后的内容
5051
idx := strings.Index(sqlText, ";")
5152
if idx >= 0 {

0 commit comments

Comments
 (0)