Skip to content

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

chore: migrate stmt_where_required (#8994)

zll600dd72e7e45370
diff --git a/backend/plugin/advisor/mysqlwip/advisor_stmt_where_required.go b/backend/plugin/advisor/mysqlwip/advisor_stmt_where_required.go
index 567edafada..ebc8a7db79 100644
--- a/backend/plugin/advisor/mysqlwip/advisor_stmt_where_required.go
+++ b/backend/plugin/advisor/mysqlwip/advisor_stmt_where_required.go
@@ -3,25 +3,31 @@ package mysqlwip
import (
"fmt"
+ "github.com/antlr4-go/antlr/v4"
+ mysql "github.com/bytebase/mysql-parser"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/plugin/advisor"
-
- "github.com/pingcap/tidb/parser/ast"
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
var (
_ advisor.Advisor = (*WhereRequirementAdvisor)(nil)
- _ ast.Visitor = (*whereRequirementChecker)(nil)
)
+func init() {
+ // only for mysqlwip test.
+ advisor.Register(storepb.Engine_ENGINE_UNSPECIFIED, advisor.MySQLWhereRequirement, &WhereRequirementAdvisor{})
+}
+
// WhereRequirementAdvisor is the advisor checking for the WHERE clause requirement.
type WhereRequirementAdvisor struct {
}
// Check checks for the WHERE clause requirement.
func (*WhereRequirementAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {
- root, ok := ctx.AST.([]ast.StmtNode)
+ root, ok := ctx.AST.([]*mysqlparser.ParseResult)
if !ok {
return nil, errors.Errorf("failed to convert to StmtNode")
}
@@ -35,9 +41,8 @@ func (*WhereRequirementAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.
title: string(ctx.Rule.Type),
}
for _, stmtNode := range root {
- checker.text = stmtNode.Text()
- checker.line = stmtNode.OriginTextPosition()
- (stmtNode).Accept(checker)
+ checker.baseLine = stmtNode.BaseLine
+ antlr.ParseTreeWalkerDefault.Walk(checker, stmtNode.Tree)
}
if len(checker.adviceList) == 0 {
@@ -52,47 +57,45 @@ func (*WhereRequirementAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.
}
type whereRequirementChecker struct {
+ *mysql.BaseMySQLParserListener
+
+ baseLine int
adviceList []advisor.Advice
level advisor.Status
title string
text string
- line int
}
-// Enter implements the ast.Visitor interface.
-func (v *whereRequirementChecker) Enter(in ast.Node) (ast.Node, bool) {
- code := advisor.Ok
- switch node := in.(type) {
- // DELETE
- case *ast.DeleteStmt:
- if node.Where == nil {
- code = advisor.StatementNoWhere
- }
- // UPDATE
- case *ast.UpdateStmt:
- if node.Where == nil {
- code = advisor.StatementNoWhere
- }
- // SELECT
- case *ast.SelectStmt:
- if node.Where == nil {
- code = advisor.StatementNoWhere
- }
+func (checker *whereRequirementChecker) EnterQuery(ctx *mysql.QueryContext) {
+ checker.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
+}
+
+// EnterDeleteStatement is called when production deleteStatement is entered.
+func (checker *whereRequirementChecker) EnterDeleteStatement(ctx *mysql.DeleteStatementContext) {
+ if ctx.WhereClause() == nil || ctx.WhereClause().WHERE_SYMBOL() == nil {
+ checker.handleWhereClause(ctx.GetStart().GetLine())
}
+}
- if code != advisor.Ok {
- v.adviceList = append(v.adviceList, advisor.Advice{
- Status: v.level,
- Code: code,
- Title: v.title,
- Content: fmt.Sprintf("\"%s\" requires WHERE clause", v.text),
- Line: v.line,
- })
+// EnterUpdateStatement is called when production updateStatement is entered.
+func (checker *whereRequirementChecker) EnterUpdateStatement(ctx *mysql.UpdateStatementContext) {
+ if ctx.WhereClause() == nil || ctx.WhereClause().WHERE_SYMBOL() == nil {
+ checker.handleWhereClause(ctx.GetStart().GetLine())
+ }
+}
+
+func (checker *whereRequirementChecker) EnterQuerySpecification(ctx *mysql.QuerySpecificationContext) {
+ if ctx.WhereClause() == nil || ctx.WhereClause().WHERE_SYMBOL() == nil {
+ checker.handleWhereClause(ctx.GetStart().GetLine())
}
- return in, false
}
-// Leave implements the ast.Visitor interface.
-func (*whereRequirementChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
+func (checker *whereRequirementChecker) handleWhereClause(lineNumber int) {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.StatementNoWhere,
+ Title: checker.title,
+ Content: fmt.Sprintf("\"%s\" requires WHERE clause", checker.text),
+ Line: checker.baseLine + lineNumber,
+ })
}
diff --git a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go
index cfd89f1c18..cb06a49bd3 100644
--- a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go
+++ b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go
@@ -27,6 +27,8 @@ func TestMySQLWIPRules(t *testing.T) {
// advisor.SchemaRuleStatementNoSelectAll disallow 'SELECT *'.
advisor.SchemaRuleStatementNoSelectAll,
+ // advisor.SchemaRuleStatementRequireWhere require 'WHERE' clause.
+ advisor.SchemaRuleStatementRequireWhere,
// advisor.SchemaRuleStatementInsertRowLimit enforce the insert row limit.
advisor.SchemaRuleStatementInsertRowLimit,
diff --git a/backend/plugin/advisor/mysqlwip/test/statement_where_require.yaml b/backend/plugin/advisor/mysqlwip/test/statement_where_require.yaml
index 71011e6780..e579a92a6d 100644
--- a/backend/plugin/advisor/mysqlwip/test/statement_where_require.yaml
+++ b/backend/plugin/advisor/mysqlwip/test/statement_where_require.yaml
@@ -1,4 +1,6 @@
-- statement: INSERT INTO tech_book(id) values (1)
+- statement: |
+ CREATE TABLE tech_book(id INT);
+ INSERT INTO tech_book(id) values (1);
want:
- status: SUCCESS
code: 0
@@ -6,27 +8,31 @@
content: ""
line: 0
details: ""
-- statement: DELETE FROM tech_book
+- statement: |
+ CREATE TABLE tech_book(id INT);
+ DELETE FROM tech_book;
want:
- status: WARN
code: 202
title: statement.where.require
content: |-
- "DELETE FROM tech_book
- ;" requires WHERE clause
+ "DELETE FROM tech_book;" requires WHERE clause
line: 2
details: ""
-- statement: UPDATE tech_book SET id = 1
+- statement: |
+ CREATE TABLE tech_book(id INT);
+ UPDATE tech_book SET id = 1;
want:
- status: WARN
code: 202
title: statement.where.require
content: |-
- "UPDATE tech_book SET id = 1
- ;" requires WHERE clause
+ "UPDATE tech_book SET id = 1;" requires WHERE clause
line: 2
details: ""
-- statement: DELETE FROM tech_book WHERE id > 0
+- statement: |
+ CREATE TABLE tech_book(id INT);
+ DELETE FROM tech_book WHERE id > 0;
want:
- status: SUCCESS
code: 0
@@ -34,7 +40,9 @@
content: ""
line: 0
details: ""
-- statement: UPDATE tech_book SET id = 1 WHERE id > 10
+- statement: |
+ CREATE TABLE tech_book(id INT);
+ UPDATE tech_book SET id = 1 WHERE id > 10;
want:
- status: SUCCESS
code: 0
@@ -42,17 +50,20 @@
content: ""
line: 0
details: ""
-- statement: SELECT id FROM tech_book
+- statement: |
+ CREATE TABLE tech_book(id INT);
+ SELECT id FROM tech_book;
want:
- status: WARN
code: 202
title: statement.where.require
content: |-
- "SELECT id FROM tech_book
- ;" requires WHERE clause
+ "SELECT id FROM tech_book;" requires WHERE clause
line: 2
details: ""
-- statement: SELECT id FROM tech_book WHERE id > 0
+- statement: |
+ CREATE TABLE tech_book(id INT);
+ SELECT id FROM tech_book WHERE id > 0;
want:
- status: SUCCESS
code: 0
@@ -60,13 +71,24 @@
content: ""
line: 0
details: ""
-- statement: SELECT id FROM tech_book WHERE id > (SELECT max(id) FROM tech_book)
+- statement: |
+ CREATE TABLE tech_book(id INT);
+ SELECT id
+ FROM tech_book
+ WHERE id > (
+ SELECT max(id)
+ FROM tech_book
+ );
want:
- status: WARN
code: 202
title: statement.where.require
content: |-
- "SELECT id FROM tech_book WHERE id > (SELECT max(id) FROM tech_book)
- ;" requires WHERE clause
- line: 2
+ "SELECT id
+ FROM tech_book
+ WHERE id > (
+ SELECT max(id)
+ FROM tech_book
+ );" requires WHERE clause
+ line: 5
details: ""
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 38e8ef58a2..759e966bdf 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -1018,6 +1018,9 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
switch ruleType {
case SchemaRuleStatementRequireWhere:
switch engine {
+ // only for mysqlwip test.
+ case storepb.Engine_ENGINE_UNSPECIFIED:
+ return MySQLWhereRequirement, nil
case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB, storepb.Engine_OCEANBASE:
return MySQLWhereRequirement, nil
case storepb.Engine_POSTGRES: