Skip to content

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

feat: support PG no-select-all rule (#2087)

rebelice6adeaf4f1abb
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 67698787ea..d57880a00a 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -128,6 +128,9 @@ const (
// PostgreSQLWhereRequirement is an advisor type for PostgreSQL WHERE clause requirement.
PostgreSQLWhereRequirement Type = "bb.plugin.advisor.postgresql.where.require"
+
+ // PostgreSQLNoSelectAll is an advisor type for PostgreSQL no select all.
+ PostgreSQLNoSelectAll Type = "bb.plugin.advisor.postgresql.select.no-select-all"
)
// Advice is the result of an advisor.
diff --git a/plugin/advisor/pg/advisor_stmt_no_select_all.go b/plugin/advisor/pg/advisor_stmt_no_select_all.go
new file mode 100644
index 0000000000..302c34b2f9
--- /dev/null
+++ b/plugin/advisor/pg/advisor_stmt_no_select_all.go
@@ -0,0 +1,78 @@
+package pg
+
+import (
+ "fmt"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/parser/ast"
+)
+
+var (
+ _ advisor.Advisor = (*NoSelectAllAdvisor)(nil)
+ _ ast.Visitor = (*noSelectAllChecker)(nil)
+)
+
+func init() {
+ advisor.Register(advisor.Postgres, advisor.PostgreSQLNoSelectAll, &NoSelectAllAdvisor{})
+}
+
+// NoSelectAllAdvisor is the advisor checking for no "select *".
+type NoSelectAllAdvisor struct {
+}
+
+// Check checks for no "select *".
+func (*NoSelectAllAdvisor) 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 := &noSelectAllChecker{
+ 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 noSelectAllChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+}
+
+// Visit implements the ast.Visitor interface.
+func (checker *noSelectAllChecker) Visit(node ast.Node) ast.Visitor {
+ if n, ok := node.(*ast.SelectStmt); ok {
+ for _, field := range n.FieldList {
+ if column, ok := field.(*ast.ColumnNameDef); ok && column.ColumnName == "*" {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.StatementSelectAll,
+ Title: checker.title,
+ Content: fmt.Sprintf("\"%s\" uses SELECT all", checker.text),
+ })
+ break
+ }
+ }
+ }
+ return checker
+}
diff --git a/plugin/advisor/pg/advisor_stmt_no_select_all_test.go b/plugin/advisor/pg/advisor_stmt_no_select_all_test.go
new file mode 100644
index 0000000000..c6ce40d22e
--- /dev/null
+++ b/plugin/advisor/pg/advisor_stmt_no_select_all_test.go
@@ -0,0 +1,51 @@
+package pg
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestNoSelectAll(t *testing.T) {
+ tests := []advisor.TestCase{
+ {
+ Statement: "SELECT * FROM t",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: advisor.StatementSelectAll,
+ Title: "statement.select.no-select-all",
+ Content: "\"SELECT * FROM t\" uses SELECT all",
+ },
+ },
+ },
+ {
+ Statement: "SELECT a, b FROM t",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: "SELECT a, b FROM (SELECT * from t1, t2) t",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: advisor.StatementSelectAll,
+ Title: "statement.select.no-select-all",
+ Content: "\"SELECT a, b FROM (SELECT * from t1, t2) t\" uses SELECT all",
+ },
+ },
+ },
+ }
+
+ advisor.RunSchemaReviewRuleTests(t, tests, &NoSelectAllAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleStatementNoSelectAll,
+ Level: advisor.SchemaRuleLevelError,
+ Payload: "",
+ }, advisor.MockPostgreSQLDatabase)
+}