Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Xiaomi
soar
提交
6c844313
S
soar
项目概览
Xiaomi
/
soar
8 个月 前同步成功
通知
373
Star
8512
Fork
1328
代码
文件
提交
分支
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 搜索 >>
提交
6c844313
编写于
9月 23, 2019
作者:
martianzhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
new heuristic rule ARG.013 for full-width quote
上级
c08fc159
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
76 addition
and
1 deletion
+76
-1
advisor/heuristic.go
advisor/heuristic.go
+19
-1
advisor/heuristic_test.go
advisor/heuristic_test.go
+39
-0
advisor/rules.go
advisor/rules.go
+8
-0
doc/heuristic.md
doc/heuristic.md
+10
-0
未找到文件。
advisor/heuristic.go
浏览文件 @
6c844313
...
...
@@ -418,7 +418,7 @@ func (q *Query4Audit) RuleOffsetLimit() Rule {
switch
v
:=
n
.
Offset
.
(
type
)
{
case
*
sqlparser
.
SQLVal
:
offset
,
err
:=
strconv
.
Atoi
(
string
(
v
.
Val
))
// 检查一下Offset阈值,太小了给这个建议也没什么用,阈值写死了没加配置
//
TODO:
检查一下Offset阈值,太小了给这个建议也没什么用,阈值写死了没加配置
if
err
==
nil
&&
offset
>
1000
{
rule
=
HeuristicRules
[
"CLA.003"
]
return
false
,
nil
...
...
@@ -2230,6 +2230,24 @@ func (q *Query4Audit) RuleInsertValues() Rule {
return
rule
}
// RuleFullWidthQuote ARG.013
func
(
q
*
Query4Audit
)
RuleFullWidthQuote
()
Rule
{
var
rule
=
q
.
RuleOK
()
for
_
,
node
:=
range
q
.
TiStmt
{
switch
n
:=
node
.
(
type
)
{
case
*
tidb
.
CreateTableStmt
,
*
tidb
.
AlterTableStmt
:
var
sb
strings
.
Builder
ctx
:=
format
.
NewRestoreCtx
(
format
.
DefaultRestoreFlags
,
&
sb
)
if
err
:=
n
.
Restore
(
ctx
);
err
==
nil
{
if
strings
.
Contains
(
sb
.
String
(),
`“”`
)
||
strings
.
Contains
(
sb
.
String
(),
`‘’`
)
{
rule
=
HeuristicRules
[
"ARG.013"
]
}
}
}
}
return
rule
}
// RuleUNIONUsage SUB.002
func
(
q
*
Query4Audit
)
RuleUNIONUsage
()
Rule
{
var
rule
=
q
.
RuleOK
()
...
...
advisor/heuristic_test.go
浏览文件 @
6c844313
...
...
@@ -1886,6 +1886,7 @@ func TestRuleSpaceWithQuote(t *testing.T) {
`SELECT ' a';`
,
`SELECT "a ";`
,
`SELECT " a";`
,
`create table tb ( a varchar(10) default ' ');`
,
},
{
`select ''`
,
...
...
@@ -2036,6 +2037,44 @@ func TestRuleInsertValues(t *testing.T) {
common
.
Log
.
Debug
(
"Exiting function: %s"
,
common
.
GetFunctionName
())
}
// ARG.013
func
TestRuleFullWidthQuote
(
t
*
testing
.
T
)
{
common
.
Log
.
Debug
(
"Entering function: %s"
,
common
.
GetFunctionName
())
sqls
:=
[][]
string
{
{
`CREATE TABLE tb (a varchar(10) default '“”')`
,
`CREATE TABLE tb (a varchar(10) default '‘’')`
,
`ALTER TABLE tb ADD COLUMN a VARCHAR(10) DEFAULT "“”"`
,
},
{
`CREATE TABLE tb (a varchar(10) default '""')`
,
},
}
for
_
,
sql
:=
range
sqls
[
0
]
{
q
,
err
:=
NewQuery4Audit
(
sql
)
if
err
==
nil
{
rule
:=
q
.
RuleFullWidthQuote
()
if
rule
.
Item
!=
"ARG.013"
{
t
.
Error
(
"Rule not match:"
,
rule
.
Item
,
"Expect : ARG.013"
)
}
}
else
{
t
.
Error
(
"sqlparser.Parse Error:"
,
err
)
}
}
for
_
,
sql
:=
range
sqls
[
1
]
{
q
,
err
:=
NewQuery4Audit
(
sql
)
if
err
==
nil
{
rule
:=
q
.
RuleFullWidthQuote
()
if
rule
.
Item
!=
"OK"
{
t
.
Error
(
"Rule not match:"
,
rule
.
Item
,
"Expect : OK"
)
}
}
else
{
t
.
Error
(
"sqlparser.Parse Error:"
,
err
)
}
}
common
.
Log
.
Debug
(
"Exiting function: %s"
,
common
.
GetFunctionName
())
}
// SUB.002
func
TestRuleUNIONUsage
(
t
*
testing
.
T
)
{
common
.
Log
.
Debug
(
"Entering function: %s"
,
common
.
GetFunctionName
())
...
...
advisor/rules.go
浏览文件 @
6c844313
...
...
@@ -272,6 +272,14 @@ func init() {
Case
:
"INSERT INTO tb (a) VALUES (1), (2)"
,
Func
:
(
*
Query4Audit
)
.
RuleInsertValues
,
},
"ARG.013"
:
{
Item
:
"ARG.013"
,
Severity
:
"L0"
,
Summary
:
"DDL 语句中使用了中文全角引号"
,
Content
:
"DDL 语句中使用了中文全角引号“”或‘’,这可能是书写错误,请确认是否符合预期。"
,
Case
:
"CREATE TABLE tb (a varchar(10) default '“”'"
,
Func
:
(
*
Query4Audit
)
.
RuleFullWidthQuote
,
},
"CLA.001"
:
{
Item
:
"CLA.001"
,
Severity
:
"L4"
,
...
...
doc/heuristic.md
浏览文件 @
6c844313
...
...
@@ -192,6 +192,16 @@ select id from t where num not in(1,2,3);
```
sql
INSERT
INTO
tb
(
a
)
VALUES
(
1
),
(
2
)
```
## DDL 语句中使用了中文全角引号
*
**Item**
:ARG.013
*
**Severity**
:L0
*
**Content**
:DDL 语句中使用了中文全角引号“”或‘’,这可能是书写错误,请确认是否符合预期。
*
**Case**
:
```
sql
CREATE
TABLE
tb
(
a
varchar
(
10
)
default
'“”'
```
## 最外层 SELECT 未指定 WHERE 条件
*
**Item**
:CLA.001
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录