Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore(schema system): fix that advisor cannot print the SQL text with sub-queries (#1402)

rebelice0389ed9a87ad
diff --git a/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like_test.go b/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like_test.go
index 617b4ddabe..a3cef77f1e 100644
--- a/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like_test.go
+++ b/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like_test.go
@@ -54,6 +54,17 @@ func TestNoLeadingWildcardLike(t *testing.T) {
},
},
},
+ {
+ statement: "SELECT * FROM (SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc') t1",
+ want: []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: common.StatementLeadingWildcardLike,
+ Title: "No leading wildcard LIKE",
+ Content: "\"SELECT * FROM (SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc') t1\" uses leading wildcard LIKE",
+ },
+ },
+ },
}
runSchemaReviewRuleTests(t, tests, &NoLeadingWildcardLikeAdvisor{}, &api.SchemaReviewRule{
diff --git a/plugin/advisor/mysql/advisor_stmt_no_select_all.go b/plugin/advisor/mysql/advisor_stmt_no_select_all.go
index 7d2a27cb5d..f3cd9f953c 100644
--- a/plugin/advisor/mysql/advisor_stmt_no_select_all.go
+++ b/plugin/advisor/mysql/advisor_stmt_no_select_all.go
@@ -35,6 +35,7 @@ func (adv *NoSelectAllAdvisor) Check(ctx advisor.Context, statement string) ([]a
}
checker := &noSelectAllChecker{level: level}
for _, stmtNode := range root {
+ checker.text = stmtNode.Text()
(stmtNode).Accept(checker)
}
@@ -52,6 +53,7 @@ func (adv *NoSelectAllAdvisor) Check(ctx advisor.Context, statement string) ([]a
type noSelectAllChecker struct {
adviceList []advisor.Advice
level advisor.Status
+ text string
}
// Enter implements the ast.Visitor interface
@@ -62,8 +64,8 @@ func (v *noSelectAllChecker) Enter(in ast.Node) (ast.Node, bool) {
v.adviceList = append(v.adviceList, advisor.Advice{
Status: v.level,
Code: common.StatementSelectAll,
- Title: "No SELECT all",
- Content: fmt.Sprintf("%q uses SELECT all", in.Text()),
+ Title: "Not SELECT all",
+ Content: fmt.Sprintf("%q uses SELECT all", v.text),
})
break
}
diff --git a/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go b/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go
index f93d42e3e8..7182ce8d10 100644
--- a/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go
+++ b/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go
@@ -16,7 +16,7 @@ func TestNoSelectAll(t *testing.T) {
{
Status: advisor.Error,
Code: common.StatementSelectAll,
- Title: "No SELECT all",
+ Title: "Not SELECT all",
Content: "\"SELECT * FROM t\" uses SELECT all",
},
},
@@ -32,6 +32,17 @@ func TestNoSelectAll(t *testing.T) {
},
},
},
+ {
+ statement: "SELECT a, b FROM (SELECT * from t1 JOIN t2) t",
+ want: []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: common.StatementSelectAll,
+ Title: "Not SELECT all",
+ Content: "\"SELECT a, b FROM (SELECT * from t1 JOIN t2) t\" uses SELECT all",
+ },
+ },
+ },
}
runSchemaReviewRuleTests(t, tests, &NoSelectAllAdvisor{}, &api.SchemaReviewRule{
diff --git a/plugin/advisor/mysql/advisor_stmt_where_required.go b/plugin/advisor/mysql/advisor_stmt_where_required.go
index b41e5ccf2e..dcf5ef0755 100644
--- a/plugin/advisor/mysql/advisor_stmt_where_required.go
+++ b/plugin/advisor/mysql/advisor_stmt_where_required.go
@@ -34,25 +34,27 @@ func (adv *WhereRequirementAdvisor) Check(ctx advisor.Context, statement string)
if err != nil {
return nil, err
}
- we := &whereRequirementChecker{level: level}
+ checker := &whereRequirementChecker{level: level}
for _, stmtNode := range root {
- (stmtNode).Accept(we)
+ checker.text = stmtNode.Text()
+ (stmtNode).Accept(checker)
}
- if len(we.adviceList) == 0 {
- we.adviceList = append(we.adviceList, advisor.Advice{
+ if len(checker.adviceList) == 0 {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: advisor.Success,
Code: common.Ok,
Title: "OK",
Content: "",
})
}
- return we.adviceList, nil
+ return checker.adviceList, nil
}
type whereRequirementChecker struct {
adviceList []advisor.Advice
level advisor.Status
+ text string
}
// Enter implements the ast.Visitor interface
@@ -81,7 +83,7 @@ func (v *whereRequirementChecker) Enter(in ast.Node) (ast.Node, bool) {
Status: v.level,
Code: code,
Title: "Require WHERE clause",
- Content: fmt.Sprintf("%q requires WHERE clause", in.Text()),
+ Content: fmt.Sprintf("%q requires WHERE clause", v.text),
})
}
return in, false
diff --git a/plugin/advisor/mysql/advisor_stmt_where_required_test.go b/plugin/advisor/mysql/advisor_stmt_where_required_test.go
index 00996dfe04..02cf2b9a46 100644
--- a/plugin/advisor/mysql/advisor_stmt_where_required_test.go
+++ b/plugin/advisor/mysql/advisor_stmt_where_required_test.go
@@ -76,6 +76,17 @@ func TestWhereRequirement(t *testing.T) {
},
},
},
+ {
+ statement: "SELECT a FROM t WHERE a > (SELECT max(id) FROM user)",
+ want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: common.StatementNoWhere,
+ Title: "Require WHERE clause",
+ Content: "\"SELECT a FROM t WHERE a > (SELECT max(id) FROM user)\" requires WHERE clause",
+ },
+ },
+ },
}
runSchemaReviewRuleTests(t, tests, &WhereRequirementAdvisor{}, &api.SchemaReviewRule{
diff --git a/tests/schema_system_test.go b/tests/schema_system_test.go
index ef76ac6a6b..0494bfdf49 100644
--- a/tests/schema_system_test.go
+++ b/tests/schema_system_test.go
@@ -176,20 +176,19 @@ func TestSchemaSystem(t *testing.T) {
},
},
{
- // TODO(rebelice): most advisors cannot get the SQL text with sub-queries, fix it
statement: "INSERT INTO t_copy SELECT * FROM t",
result: []api.TaskCheckResult{
{
Status: api.TaskCheckStatusError,
Code: common.StatementSelectAll,
- Title: "No SELECT all",
- Content: "\"\" uses SELECT all",
+ Title: "Not SELECT all",
+ Content: "\"INSERT INTO t_copy SELECT * FROM t\" uses SELECT all",
},
{
Status: api.TaskCheckStatusError,
Code: common.StatementNoWhere,
Title: "Require WHERE clause",
- Content: "\"\" requires WHERE clause",
+ Content: "\"INSERT INTO t_copy SELECT * FROM t\" requires WHERE clause",
},
},
},