Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
m0_67397764
soar
提交
de99de7e
S
soar
项目概览
m0_67397764
/
soar
与 Fork 源项目一致
Fork自
Xiaomi / soar
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
soar
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
de99de7e
编写于
11月 23, 2018
作者:
martianzhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix #121
RemoveSQLComment trim space
上级
d23dfdb4
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
36 addition
and
9 deletion
+36
-9
advisor/heuristic.go
advisor/heuristic.go
+1
-1
cmd/soar/soar.go
cmd/soar/soar.go
+1
-2
cmd/soar/tool.go
cmd/soar/tool.go
+1
-1
database/explain.go
database/explain.go
+2
-2
database/explain_test.go
database/explain_test.go
+1
-1
database/mysql.go
database/mysql.go
+4
-2
database/mysql_test.go
database/mysql_test.go
+21
-0
database/testdata/TestRemoveSQLComments.golden
database/testdata/TestRemoveSQLComments.golden
+5
-0
未找到文件。
advisor/heuristic.go
浏览文件 @
de99de7e
...
...
@@ -1275,7 +1275,7 @@ func (q *Query4Audit) RuleMeaninglessWhere() Rule {
func
(
q
*
Query4Audit
)
RuleLoadFile
()
Rule
{
var
rule
=
q
.
RuleOK
()
// 去除注释
sql
:=
string
(
database
.
RemoveSQLComments
([]
byte
(
q
.
Query
))
)
sql
:=
database
.
RemoveSQLComments
(
q
.
Query
)
// 去除多余的空格和回车
sql
=
strings
.
Join
(
strings
.
Fields
(
sql
),
" "
)
tks
:=
ast
.
Tokenize
(
sql
)
...
...
cmd/soar/soar.go
浏览文件 @
de99de7e
...
...
@@ -120,8 +120,7 @@ func main() {
buf
=
string
(
bufBytes
)
// 去除无用的备注和空格
sql
=
strings
.
TrimSpace
(
sql
)
sql
=
string
(
database
.
RemoveSQLComments
([]
byte
(
sql
)))
sql
=
database
.
RemoveSQLComments
(
sql
)
if
sql
==
""
{
common
.
Log
.
Debug
(
"empty query or comment, buf: %s"
,
buf
)
continue
...
...
cmd/soar/tool.go
浏览文件 @
de99de7e
...
...
@@ -175,7 +175,7 @@ func reportTool(sql string, bom []byte) (isContinue bool, exitCode int) {
fmt
.
Println
(
charset
)
return
false
,
0
case
"remove-comment"
:
fmt
.
Println
(
string
(
database
.
RemoveSQLComments
([]
byte
(
sql
))
))
fmt
.
Println
(
database
.
RemoveSQLComments
(
sql
))
return
false
,
0
default
:
return
true
,
0
...
...
database/explain.go
浏览文件 @
de99de7e
...
...
@@ -384,7 +384,7 @@ var ExplainExtra = map[string]string{
func
findTablesInJSON
(
explainJSON
string
,
depth
int
)
{
common
.
Log
.
Debug
(
"findTablesInJSON Enter: depth(%d), json(%s)"
,
depth
,
explainJSON
)
// 去除注释,语法检查
explainJSON
=
string
(
RemoveSQLComments
([]
byte
(
explainJSON
))
)
explainJSON
=
RemoveSQLComments
(
explainJSON
)
if
!
gjson
.
Valid
(
explainJSON
)
{
return
}
...
...
@@ -923,7 +923,7 @@ func parseVerticalExplainText(content string) (explainRows []*ExplainRow, err er
// 解析文本形式JSON Explain信息
func
parseJSONExplainText
(
content
string
)
(
*
ExplainJSON
,
error
)
{
explainJSON
:=
new
(
ExplainJSON
)
err
:=
json
.
Unmarshal
(
RemoveSQLComments
([]
byte
(
content
)),
explainJSON
)
err
:=
json
.
Unmarshal
(
[]
byte
(
RemoveSQLComments
(
content
)),
explainJSON
)
return
explainJSON
,
err
}
...
...
database/explain_test.go
浏览文件 @
de99de7e
...
...
@@ -2366,7 +2366,7 @@ func TestExplain(t *testing.T) {
func
TestParseExplainText
(
t
*
testing
.
T
)
{
for
_
,
content
:=
range
exp
{
pretty
.
Println
(
string
(
RemoveSQLComments
([]
byte
(
content
))
))
pretty
.
Println
(
RemoveSQLComments
(
content
))
pretty
.
Println
(
ParseExplainText
(
content
))
}
/*
...
...
database/mysql.go
浏览文件 @
de99de7e
...
...
@@ -259,10 +259,11 @@ func (db *Connector) IsView(tbName string) bool {
}
// RemoveSQLComments 去除SQL中的注释
func
RemoveSQLComments
(
sql
[]
byte
)
[]
byte
{
func
RemoveSQLComments
(
sql
string
)
string
{
buf
:=
[]
byte
(
sql
)
cmtReg
:=
regexp
.
MustCompile
(
`("(""|[^"])*")|('(''|[^'])*')|(--[^\n\r]*)|(#.*)|(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)`
)
re
turn
cmtReg
.
ReplaceAllFunc
(
sql
,
func
(
s
[]
byte
)
[]
byte
{
re
s
:=
cmtReg
.
ReplaceAllFunc
(
buf
,
func
(
s
[]
byte
)
[]
byte
{
if
(
s
[
0
]
==
'"'
&&
s
[
len
(
s
)
-
1
]
==
'"'
)
||
(
s
[
0
]
==
'\'
'
&&
s
[
len
(
s
)
-
1
]
==
'\'
'
)
||
(
string
(
s
[
:
3
])
==
"/*!"
)
{
...
...
@@ -270,6 +271,7 @@ func RemoveSQLComments(sql []byte) []byte {
}
return
[]
byte
(
""
)
})
return
strings
.
TrimSpace
(
string
(
res
))
}
// 为了防止在 Online 环境进行误操作,通过 dangerousQuery 来判断能否在 Online 执行
...
...
database/mysql_test.go
浏览文件 @
de99de7e
...
...
@@ -88,3 +88,24 @@ func TestSource(t *testing.T) {
t
.
Error
(
"Source result not match, expect 1, 1"
)
}
}
func
TestRemoveSQLComments
(
t
*
testing
.
T
)
{
SQLs
:=
[]
string
{
`-- comment`
,
`--`
,
`# comment`
,
`/* multi-line
comment*/`
,
`--
-- comment`
,
}
err
:=
common
.
GoldenDiff
(
func
()
{
for
_
,
sql
:=
range
SQLs
{
fmt
.
Println
(
RemoveSQLComments
(
sql
))
}
},
t
.
Name
(),
update
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
}
database/testdata/TestRemoveSQLComments.golden
0 → 100644
浏览文件 @
de99de7e
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录