Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: implement Oracle table name no keyword rule (#6072)
rebelice3c5360035f3d
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.goindex bfa8e12261..6e43e618db 100644--- a/backend/plugin/advisor/advisor.go+++ b/backend/plugin/advisor/advisor.go@@ -379,6 +379,9 @@ const (// OracleAddNotNullColumnRequireDefault is an advisor type for Oracle adding not null column requires default.OracleAddNotNullColumnRequireDefault Type = "bb.plugin.advisor.oracle.column.add-not-null-column-require-default"++ // OracleTableNamingNoKeyword is an advisor type for Oracle table naming convention without keyword.+ OracleTableNamingNoKeyword Type = "bb.plugin.advisor.oracle.naming.table-no-keyword")// Advice is the result of an advisor.diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.goindex 616cad7d2c..cf7147460a 100644--- a/backend/plugin/advisor/code.go+++ b/backend/plugin/advisor/code.go@@ -52,6 +52,8 @@ const (NamingPKConventionMismatch Code = 306// 307 auto_increment column naming advisor error code.NamingAutoIncrementColumnConventionMismatch Code = 307+ // 308 name is keyword identifier advisor error code.+ NameIsKeywordIdentifier Code = 308// 401 ~ 499 column error code.NoRequiredColumn Code = 401diff --git a/backend/plugin/advisor/oracle/advisor_naming_table_no_keyword.go b/backend/plugin/advisor/oracle/advisor_naming_table_no_keyword.gonew file mode 100644index 0000000000..92a772e953--- /dev/null+++ b/backend/plugin/advisor/oracle/advisor_naming_table_no_keyword.go@@ -0,0 +1,101 @@+// Package oracle is the advisor for oracle database.+package oracle++import (+ "fmt"++ "github.com/antlr/antlr4/runtime/Go/antlr/v4"+ parser "github.com/bytebase/plsql-parser"++ "github.com/bytebase/bytebase/backend/plugin/advisor"+ "github.com/bytebase/bytebase/backend/plugin/advisor/db"+ bbparser "github.com/bytebase/bytebase/backend/plugin/parser/sql"+)++var (+ _ advisor.Advisor = (*NamingTableNoKeywordAdvisor)(nil)+)++func init() {+ advisor.Register(db.Oracle, advisor.OracleTableNamingNoKeyword, &NamingTableNoKeywordAdvisor{})+}++// NamingTableNoKeywordAdvisor is the advisor checking for table naming convention without keyword.+type NamingTableNoKeywordAdvisor struct {+}++// Check checks for table naming convention without keyword.+func (*NamingTableNoKeywordAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {+ tree, errAdvice := parseStatement(statement)+ if errAdvice != nil {+ return errAdvice, nil+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }++ listener := &namingTableNoKeywordListener{+ level: level,+ title: string(ctx.Rule.Type),+ currentSchema: ctx.CurrentSchema,+ }++ antlr.ParseTreeWalkerDefault.Walk(listener, tree)++ return listener.generateAdvice()+}++// namingTableNoKeywordListener is the listener for table naming convention without keyword.+type namingTableNoKeywordListener struct {+ *parser.BasePlSqlParserListener++ level advisor.Status+ title string+ currentSchema string+ adviceList []advisor.Advice+}++func (l *namingTableNoKeywordListener) generateAdvice() ([]advisor.Advice, error) {+ if len(l.adviceList) == 0 {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ })+ }+ return l.adviceList, nil+}++// EnterCreate_table is called when production create_table is entered.+func (l *namingTableNoKeywordListener) EnterCreate_table(ctx *parser.Create_tableContext) {+ tableName := normalizeIdentifier(ctx.Table_name(), l.currentSchema)+ if bbparser.IsOracleKeyword(tableName) {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: l.level,+ Code: advisor.NameIsKeywordIdentifier,+ Title: l.title,+ Content: fmt.Sprintf("Table name %q is a keyword identifier and should be avoided.", tableName),+ Line: ctx.GetStart().GetLine(),+ })+ }+}++// EnterAlter_table_properties is called when production alter_table_properties is entered.+func (l *namingTableNoKeywordListener) EnterAlter_table_properties(ctx *parser.Alter_table_propertiesContext) {+ if ctx.Tableview_name() == nil {+ return+ }+ tableName := lastIdentifier(normalizeIdentifier(ctx.Tableview_name(), l.currentSchema))+ if bbparser.IsOracleKeyword(tableName) {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: l.level,+ Code: advisor.NameIsKeywordIdentifier,+ Title: l.title,+ Content: fmt.Sprintf("Table name %q is a keyword identifier and should be avoided.", tableName),+ Line: ctx.GetStart().GetLine(),+ })+ }+}diff --git a/backend/plugin/advisor/oracle/oracle_rules_test.go b/backend/plugin/advisor/oracle/oracle_rules_test.goindex 084f627426..bcf8fcbbd0 100644--- a/backend/plugin/advisor/oracle/oracle_rules_test.go+++ b/backend/plugin/advisor/oracle/oracle_rules_test.go@@ -25,6 +25,7 @@ func TestOracleRules(t *testing.T) {advisor.SchemaRuleColumnRequireDefault,advisor.SchemaRuleAddNotNullColumnRequireDefault,advisor.SchemaRuleColumnMaximumVarcharLength,+ advisor.SchemaRuleTableNameNoKeyword,}for _, rule := range oracleRules {diff --git a/backend/plugin/advisor/oracle/test/naming_table_no_keyword.yaml b/backend/plugin/advisor/oracle/test/naming_table_no_keyword.yamlnew file mode 100644index 0000000000..5340c1ce48--- /dev/null+++ b/backend/plugin/advisor/oracle/test/naming_table_no_keyword.yaml@@ -0,0 +1,42 @@+- statement: CREATE TABLE "table"(id int, name varchar(255))+ want:+ - status: WARN+ code: 308+ title: naming.table.no-keyword+ content: Table name "table" is a keyword identifier and should be avoided.+ line: 1+ details: ""+- statement: CREATE TABLE "rlcmidzlevbivwvcntihenpoibtsdfasdfasdfiutqeb"(id int, name varchar(255))+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+ details: ""+- statement: CREATE TABLE analyze(id int, name varchar(255))+ want:+ - status: WARN+ code: 308+ title: naming.table.no-keyword+ content: Table name "ANALYZE" is a keyword identifier and should be avoided.+ line: 1+ details: ""+- statement: ALTER TABLE tech_book RENAME TO analyze+ want:+ - status: WARN+ code: 308+ title: naming.table.no-keyword+ content: Table name "ANALYZE" is a keyword identifier and should be avoided.+ line: 1+ details: ""+- statement: |-+ create table techBook(id int);+ ALTER TABLE techBook RENAME TO tech_book_tmp;+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+ details: ""diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex ab61e260d2..9170b1377a 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -55,6 +55,8 @@ const (SchemaRuleIDXNaming SQLReviewRuleType = "naming.index.idx"// SchemaRuleAutoIncrementColumnNaming enforce the auto_increment column name format.SchemaRuleAutoIncrementColumnNaming SQLReviewRuleType = "naming.column.auto-increment"+ // SchemaRuleTableNameNoKeyword enforce the table name not to use keyword.+ SchemaRuleTableNameNoKeyword SQLReviewRuleType = "naming.table.no-keyword"// SchemaRuleStatementNoSelectAll disallow 'SELECT *'.SchemaRuleStatementNoSelectAll SQLReviewRuleType = "statement.select.no-select-all"@@ -846,6 +848,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errcase db.MySQL, db.TiDB, db.MariaDB:return MySQLNamingAutoIncrementColumnConvention, nil}+ case SchemaRuleTableNameNoKeyword:+ if engine == db.Oracle {+ return OracleTableNamingNoKeyword, nil+ }case SchemaRuleRequiredColumn:switch engine {case db.MySQL, db.TiDB, db.MariaDB:diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.goindex c36dc8bd25..8803276901 100644--- a/backend/plugin/advisor/utils_for_tests.go+++ b/backend/plugin/advisor/utils_for_tests.go@@ -376,7 +376,8 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType) (string, error) {SchemaRuleCreateIndexConcurrently,SchemaRuleStatementAddCheckNotValid,SchemaRuleStatementDisallowAddNotNull,- SchemaRuleIndexTypeNoBlob:+ SchemaRuleIndexTypeNoBlob,+ SchemaRuleTableNameNoKeyword:case SchemaRuleTableDropNamingConvention:payload, err = json.Marshal(NamingRulePayload{Format: "_delete$",diff --git a/backend/plugin/parser/sql/plsql.go b/backend/plugin/parser/sql/plsql.goindex 796be7a8d0..01a344e714 100644--- a/backend/plugin/parser/sql/plsql.go+++ b/backend/plugin/parser/sql/plsql.go@@ -2,6 +2,7 @@ package parserimport ("fmt"+ "strings""github.com/antlr/antlr4/runtime/Go/antlr/v4"@@ -244,3 +245,18 @@ func equivalentType(lType parser.IDatatypeContext, rType parser.IDatatypeContextreturn lType.GetText() == rType.GetText()}++// IsOracleKeyword returns true if the given text is an Oracle keyword.+func IsOracleKeyword(text string) bool {+ if len(text) == 0 {+ return false+ }++ lexer := parser.NewPlSqlLexer(antlr.NewInputStream(text))+ for _, keyword := range lexer.GetLiteralNames() {+ if strings.EqualFold(strings.Trim(keyword, "'"), text) {+ return true+ }+ }+ return false+}