Skip to content

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

feat: support PG NEED WHERE CLAUSE rule (#2082)

rebeliceb3dfc168d07d
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 58b3eaf33a..c1a9675af7 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -122,6 +122,9 @@ const (
// PostgreSQLTableRequirePK is an advisor type for PostgreSQL table require primary key.
PostgreSQLTableRequirePK Type = "bb.plugin.advisor.postgresql.table.require-pk"
+
+ // PostgreSQLWhereRequirement is an advisor type for PostgreSQL WHERE clause requirement.
+ PostgreSQLWhereRequirement Type = "bb.plugin.advisor.postgresql.where.require"
)
// Advice is the result of an advisor.
diff --git a/plugin/advisor/pg/advisor_stmt_where_required.go b/plugin/advisor/pg/advisor_stmt_where_required.go
new file mode 100644
index 0000000000..feeff8f358
--- /dev/null
+++ b/plugin/advisor/pg/advisor_stmt_where_required.go
@@ -0,0 +1,92 @@
+package pg
+
+import (
+ "fmt"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/parser/ast"
+)
+
+var (
+ _ advisor.Advisor = (*WhereRequirementAdvisor)(nil)
+ _ ast.Visitor = (*whereRequirementChecker)(nil)
+)
+
+func init() {
+ advisor.Register(advisor.Postgres, advisor.PostgreSQLWhereRequirement, &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, statement string) ([]advisor.Advice, error) {
+ stmts, errAdvice := parseStatement(statement)
+ if errAdvice != nil {
+ return errAdvice, nil
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+ checker := &whereRequirementChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ }
+
+ for _, stmt := range stmts {
+ checker.text = stmt.Text()
+ ast.Walk(checker, stmt)
+ }
+
+ if len(checker.adviceList) == 0 {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return checker.adviceList, nil
+}
+
+type whereRequirementChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+}
+
+// Visit implements the ast.Visitor interface.
+func (checker *whereRequirementChecker) Visit(node ast.Node) ast.Visitor {
+ code := advisor.Ok
+ switch n := node.(type) {
+ // DELETE
+ case *ast.DeleteStmt:
+ if n.WhereClause == nil {
+ code = advisor.StatementNoWhere
+ }
+ // UPDATE
+ case *ast.UpdateStmt:
+ if n.WhereClause == nil {
+ code = advisor.StatementNoWhere
+ }
+ // SELECT
+ case *ast.SelectStmt:
+ if n.WhereClause == nil {
+ code = advisor.StatementNoWhere
+ }
+ }
+
+ if code != advisor.Ok {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: code,
+ Title: checker.title,
+ Content: fmt.Sprintf("\"%s\" requires WHERE clause", checker.text),
+ })
+ }
+ return checker
+}
diff --git a/plugin/advisor/pg/advisor_stmt_where_required_test.go b/plugin/advisor/pg/advisor_stmt_where_required_test.go
new file mode 100644
index 0000000000..8cab4dc9e9
--- /dev/null
+++ b/plugin/advisor/pg/advisor_stmt_where_required_test.go
@@ -0,0 +1,95 @@
+package pg
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestWhereRequirement(t *testing.T) {
+ tests := []advisor.TestCase{
+ {
+ Statement: "DELETE FROM t1",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.StatementNoWhere,
+ Title: "statement.where.require",
+ Content: "\"DELETE FROM t1\" requires WHERE clause",
+ },
+ },
+ },
+ {
+ Statement: "UPDATE t1 SET a = 1",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.StatementNoWhere,
+ Title: "statement.where.require",
+ Content: "\"UPDATE t1 SET a = 1\" requires WHERE clause",
+ },
+ },
+ },
+ {
+ Statement: "DELETE FROM t1 WHERE a > 0",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: "UPDATE t1 SET a = 1 WHERE a > 10",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: "SELECT a FROM t",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.StatementNoWhere,
+ Title: "statement.where.require",
+ Content: "\"SELECT a FROM t\" requires WHERE clause",
+ },
+ },
+ },
+ {
+ Statement: "SELECT a FROM t WHERE a > 0",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: "SELECT a FROM t WHERE a > (SELECT max(id) FROM user)",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.StatementNoWhere,
+ Title: "statement.where.require",
+ Content: "\"SELECT a FROM t WHERE a > (SELECT max(id) FROM user)\" requires WHERE clause",
+ },
+ },
+ },
+ }
+
+ advisor.RunSchemaReviewRuleTests(t, tests, &WhereRequirementAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleStatementRequireWhere,
+ Level: advisor.SchemaRuleLevelWarning,
+ Payload: "",
+ }, advisor.MockPostgreSQLDatabase)
+}