Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
rainie_yuyue
soar
提交
9d7ec0f3
S
soar
项目概览
rainie_yuyue
/
soar
与 Fork 源项目一致
Fork自
Xiaomi / soar
通知
1
Star
1
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 搜索 >>
提交
9d7ec0f3
编写于
11月 20, 2018
作者:
martianzhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix #110
remove bom before auditing
上级
a07240e6
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
99 addition
and
6 deletion
+99
-6
cmd/soar/soar.go
cmd/soar/soar.go
+10
-2
common/chardet.go
common/chardet.go
+61
-3
common/chardet_test.go
common/chardet_test.go
+24
-0
common/testdata/TestRemoveBOM.golden
common/testdata/TestRemoveBOM.golden
+2
-0
common/testdata/UTF-8.bom.sql
common/testdata/UTF-8.bom.sql
+1
-0
database/explain.go
database/explain.go
+1
-1
未找到文件。
cmd/soar/soar.go
浏览文件 @
9d7ec0f3
...
...
@@ -119,6 +119,10 @@ func main() {
}
}
// remove bom from file header
var
bom
[]
byte
sql
,
bom
=
common
.
RemoveBOM
([]
byte
(
sql
))
switch
common
.
Config
.
ReportType
{
case
"html"
:
// HTML 格式输入 CSS 加载
...
...
@@ -129,13 +133,17 @@ func main() {
fmt
.
Println
(
common
.
Markdown2HTML
(
sql
))
return
case
"explain-digest"
:
// 当用户输入为 EXPLAIN
信
息,只对 Explain 信息进行分析
// 当用户输入为 EXPLAIN
信
息,只对 Explain 信息进行分析
// 注意: 这里只能处理一条 SQL 的 EXPLAIN 信息,用户一次反馈多条 SQL 的 EXPLAIN 信息无法处理
advisor
.
DigestExplainText
(
sql
)
return
case
"chardet"
:
// Get charset of input
fmt
.
Println
(
common
.
Chardet
([]
byte
(
sql
)))
charset
:=
common
.
CheckCharsetByBOM
(
bom
)
if
charset
==
""
{
charset
=
common
.
Chardet
([]
byte
(
sql
))
}
fmt
.
Println
(
charset
)
return
case
"remove-comment"
:
fmt
.
Println
(
string
(
database
.
RemoveSQLComments
([]
byte
(
sql
))))
...
...
common/chardet.go
浏览文件 @
9d7ec0f3
...
...
@@ -22,11 +22,69 @@ import (
// Chardet get best match charset
func
Chardet
(
buf
[]
byte
)
string
{
// check character set by file BOM
charset
:=
CheckCharsetByBOM
(
buf
)
if
charset
!=
""
{
return
charset
}
// use chardet pkg check file charset
charset
=
"unknown"
var
confidence
int
detector
:=
chardet
.
NewTextDetector
()
result
,
err
:=
detector
.
DetectBest
(
buf
)
// detector.DetectBest is unstable
// when the confidence value are equally, the best detect charset will be random
result
,
err
:=
detector
.
DetectAll
(
buf
)
if
err
!=
nil
{
return
"unknown"
return
charset
}
// SOAR's main user speak Chinese, GB-18030, UTF-8 are higher suggested
for
_
,
r
:=
range
result
{
if
confidence
>=
r
.
Confidence
&&
r
.
Confidence
!=
0
{
return
charset
}
confidence
=
r
.
Confidence
if
r
.
Charset
==
"GB-18030"
||
r
.
Charset
==
"UTF-8"
{
return
r
.
Charset
}
charset
=
r
.
Charset
}
return
charset
}
// CheckCharsetByBOM ref: https://en.wikipedia.org/wiki/Byte_order_mark
func
CheckCharsetByBOM
(
buf
[]
byte
)
string
{
// TODO: There are many kind of BOM
// UTF-8 EF BB BF
if
len
(
buf
)
>=
3
{
if
buf
[
0
]
==
0xef
&&
buf
[
1
]
==
0xbb
&&
buf
[
2
]
==
0xbf
{
return
"UTF-8"
}
}
// GB-18030 84 31 95 33
if
len
(
buf
)
>=
4
{
if
buf
[
0
]
==
0x84
&&
buf
[
1
]
==
0x31
&&
buf
[
2
]
==
0x95
&&
buf
[
3
]
==
0x33
{
return
"GB-18030"
}
}
return
result
.
Charset
return
""
}
// RemoveBOM remove bom from file
func
RemoveBOM
(
buf
[]
byte
)
(
string
,
[]
byte
)
{
// ef bb bf, UTF-8 BOM
if
len
(
buf
)
>
3
{
if
buf
[
0
]
==
0xef
&&
buf
[
1
]
==
0xbb
&&
buf
[
2
]
==
0xbf
{
return
string
(
buf
[
3
:
]),
buf
[
:
3
]
}
}
// ff fe, UTF-16 (LE) BOM
if
len
(
buf
)
>
2
{
if
buf
[
0
]
==
0xff
&&
buf
[
1
]
==
0xfe
{
return
string
(
buf
[
2
:
]),
buf
[
:
2
]
}
}
return
string
(
buf
),
[]
byte
{}
}
common/chardet_test.go
浏览文件 @
9d7ec0f3
...
...
@@ -17,6 +17,7 @@
package
common
import
(
"fmt"
"io/ioutil"
"testing"
)
...
...
@@ -38,3 +39,26 @@ func TestChardet(t *testing.T) {
}
}
}
func
TestRemoveBOM
(
t
*
testing
.
T
)
{
fileName
:=
DevPath
+
"/common/testdata/UTF-8.bom.sql"
buf
,
err
:=
ioutil
.
ReadFile
(
fileName
)
if
err
!=
nil
{
t
.
Errorf
(
"ioutil.ReadFile %s, Error: %s"
,
fileName
,
err
.
Error
())
}
GoldenDiff
(
func
()
{
fmt
.
Println
(
RemoveBOM
(
buf
))
},
t
.
Name
(),
update
)
}
func
TestCheckCharsetByBOM
(
t
*
testing
.
T
)
{
fileName
:=
DevPath
+
"/common/testdata/UTF-8.bom.sql"
buf
,
err
:=
ioutil
.
ReadFile
(
fileName
)
if
err
!=
nil
{
t
.
Errorf
(
"ioutil.ReadFile %s, Error: %s"
,
fileName
,
err
.
Error
())
}
if
CheckCharsetByBOM
(
buf
)
!=
"UTF-8"
{
t
.
Errorf
(
"checkCharsetByBOM Want: UTF-8, Get: %s"
,
CheckCharsetByBOM
(
buf
))
}
}
common/testdata/TestRemoveBOM.golden
0 → 100644
浏览文件 @
9d7ec0f3
select col from tb c = 1;
[239 187 191]
common/testdata/UTF-8.bom.sql
0 → 100644
浏览文件 @
9d7ec0f3
select
col
from
tb
c
=
1
;
database/explain.go
浏览文件 @
9d7ec0f3
...
...
@@ -532,7 +532,7 @@ func (db *Connector) explainAbleSQL(sql string) (string, error) {
// 执行explain请求,返回mysql.Result执行结果
func
(
db
*
Connector
)
executeExplain
(
sql
string
,
explainType
int
,
formatType
int
)
(
*
QueryResult
,
error
)
{
var
err
error
sql
,
_
=
db
.
explainAbleSQL
(
sql
)
sql
,
err
=
db
.
explainAbleSQL
(
sql
)
if
sql
==
""
{
return
nil
,
err
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录