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 identifier is not keyword rule (#6088)
rebelice039751b59cf4
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.goindex 6e43e618db..deb790de4b 100644--- a/backend/plugin/advisor/advisor.go+++ b/backend/plugin/advisor/advisor.go@@ -382,6 +382,9 @@ const (// OracleTableNamingNoKeyword is an advisor type for Oracle table naming convention without keyword.OracleTableNamingNoKeyword Type = "bb.plugin.advisor.oracle.naming.table-no-keyword"++ // OracleIdentifierNamingNoKeyword is an advisor type for Oracle identifier naming convention without keyword.+ OracleIdentifierNamingNoKeyword Type = "bb.plugin.advisor.oracle.naming.identifier-no-keyword")// Advice is the result of an advisor.diff --git a/backend/plugin/advisor/oracle/advisor_naming_identifier_no_keyword.go b/backend/plugin/advisor/oracle/advisor_naming_identifier_no_keyword.gonew file mode 100644index 0000000000..348f92c144--- /dev/null+++ b/backend/plugin/advisor/oracle/advisor_naming_identifier_no_keyword.go@@ -0,0 +1,84 @@+// 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"+ sql "github.com/bytebase/bytebase/backend/plugin/parser/sql"+)++var (+ _ advisor.Advisor = (*NamingIdentifierNoKeywordAdvisor)(nil)+)++func init() {+ advisor.Register(db.Oracle, advisor.OracleIdentifierNamingNoKeyword, &NamingIdentifierNoKeywordAdvisor{})+}++// NamingIdentifierNoKeywordAdvisor is the advisor checking for identifier naming convention without keyword.+type NamingIdentifierNoKeywordAdvisor struct {+}++// Check checks for identifier naming convention without keyword.+func (*NamingIdentifierNoKeywordAdvisor) 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 := &namingIdentifierNoKeywordListener{+ level: level,+ title: string(ctx.Rule.Type),+ currentSchema: ctx.CurrentSchema,+ }++ antlr.ParseTreeWalkerDefault.Walk(listener, tree)++ return listener.generateAdvice()+}++// namingIdentifierNoKeywordListener is the listener for identifier naming convention without keyword.+type namingIdentifierNoKeywordListener struct {+ *parser.BasePlSqlParserListener++ level advisor.Status+ title string+ currentSchema string+ adviceList []advisor.Advice+}++func (l *namingIdentifierNoKeywordListener) 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+}++// EnterId_expression is called when production id_expression is entered.+func (l *namingIdentifierNoKeywordListener) EnterId_expression(ctx *parser.Id_expressionContext) {+ identifier := normalizeIDExpression(ctx)+ if sql.IsOracleKeyword(identifier) {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: l.level,+ Code: advisor.NameIsKeywordIdentifier,+ Title: l.title,+ Content: fmt.Sprintf("Identifier %q is a keyword and should be avoided", identifier),+ Line: ctx.GetStart().GetLine(),+ })+ }+}diff --git a/backend/plugin/advisor/oracle/oracle_rules_test.go b/backend/plugin/advisor/oracle/oracle_rules_test.goindex bcf8fcbbd0..c304c0d297 100644--- a/backend/plugin/advisor/oracle/oracle_rules_test.go+++ b/backend/plugin/advisor/oracle/oracle_rules_test.go@@ -26,6 +26,7 @@ func TestOracleRules(t *testing.T) {advisor.SchemaRuleAddNotNullColumnRequireDefault,advisor.SchemaRuleColumnMaximumVarcharLength,advisor.SchemaRuleTableNameNoKeyword,+ advisor.SchemaRuleIdentifierNoKeyword,}for _, rule := range oracleRules {diff --git a/backend/plugin/advisor/oracle/test/naming_identifier_no_keyword.yaml b/backend/plugin/advisor/oracle/test/naming_identifier_no_keyword.yamlnew file mode 100644index 0000000000..8d119ee2e5--- /dev/null+++ b/backend/plugin/advisor/oracle/test/naming_identifier_no_keyword.yaml@@ -0,0 +1,48 @@+- statement: CREATE TABLE "table"(id int, name varchar(255))+ want:+ - status: WARN+ code: 308+ title: naming.identifier.no-keyword+ content: Identifier "table" is a keyword and should be avoided+ line: 1+ details: ""+ - status: WARN+ code: 308+ title: naming.identifier.no-keyword+ content: Identifier "NAME" is a keyword and should be avoided+ line: 1+ details: ""+- statement: CREATE TABLE "rlcmidzlevbivwvcntihenpoibtsdfasdfasdfiutqeb"(id int, name varchar(255))+ want:+ - status: WARN+ code: 308+ title: naming.identifier.no-keyword+ content: Identifier "NAME" is a keyword and should be avoided+ line: 1+ details: ""+- statement: CREATE TABLE analyze(id int, name varchar(255))+ want:+ - status: WARN+ code: 308+ title: naming.identifier.no-keyword+ content: Identifier "NAME" is a keyword and should be avoided+ line: 1+ details: ""+- statement: ALTER TABLE tech_book RENAME TO analyze+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+ 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/oracle/test/naming_table_no_keyword.yaml b/backend/plugin/advisor/oracle/test/naming_table_no_keyword.yamlindex 5340c1ce48..5390f22500 100644--- a/backend/plugin/advisor/oracle/test/naming_table_no_keyword.yaml+++ b/backend/plugin/advisor/oracle/test/naming_table_no_keyword.yaml@@ -14,20 +14,20 @@content: ""line: 0details: ""-- statement: CREATE TABLE analyze(id int, name varchar(255))+- statement: CREATE TABLE avg(id int, name varchar(255))want:- status: WARNcode: 308title: naming.table.no-keyword- content: Table name "ANALYZE" is a keyword identifier and should be avoided.+ content: Table name "AVG" is a keyword identifier and should be avoided.line: 1details: ""-- statement: ALTER TABLE tech_book RENAME TO analyze+- statement: ALTER TABLE tech_book RENAME TO avgwant:- status: WARNcode: 308title: naming.table.no-keyword- content: Table name "ANALYZE" is a keyword identifier and should be avoided.+ content: Table name "AVG" is a keyword identifier and should be avoided.line: 1details: ""- statement: |-diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex 9170b1377a..b079aabc8f 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -57,6 +57,8 @@ const (SchemaRuleAutoIncrementColumnNaming SQLReviewRuleType = "naming.column.auto-increment"// SchemaRuleTableNameNoKeyword enforce the table name not to use keyword.SchemaRuleTableNameNoKeyword SQLReviewRuleType = "naming.table.no-keyword"+ // SchemaRuleIdentifierNoKeyword enforce the identifier not to use keyword.+ SchemaRuleIdentifierNoKeyword SQLReviewRuleType = "naming.identifier.no-keyword"// SchemaRuleStatementNoSelectAll disallow 'SELECT *'.SchemaRuleStatementNoSelectAll SQLReviewRuleType = "statement.select.no-select-all"@@ -852,6 +854,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errif engine == db.Oracle {return OracleTableNamingNoKeyword, nil}+ case SchemaRuleIdentifierNoKeyword:+ if engine == db.Oracle {+ return OracleIdentifierNamingNoKeyword, 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 8803276901..d842902252 100644--- a/backend/plugin/advisor/utils_for_tests.go+++ b/backend/plugin/advisor/utils_for_tests.go@@ -377,6 +377,7 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType) (string, error) {SchemaRuleStatementAddCheckNotValid,SchemaRuleStatementDisallowAddNotNull,SchemaRuleIndexTypeNoBlob,+ SchemaRuleIdentifierNoKeyword,SchemaRuleTableNameNoKeyword:case SchemaRuleTableDropNamingConvention:payload, err = json.Marshal(NamingRulePayload{diff --git a/backend/plugin/parser/sql/plsql.go b/backend/plugin/parser/sql/plsql.goindex 01a344e714..0b4dd71f54 100644--- a/backend/plugin/parser/sql/plsql.go+++ b/backend/plugin/parser/sql/plsql.go@@ -9,6 +9,350 @@ import (parser "github.com/bytebase/plsql-parser")+// https://docs.oracle.com/en/database/oracle/oracle-database/21/lnpls/plsql-reserved-words-keywords.html#GUID-9BAA3A99-41B1-45CB-A91E-1E482BC1F927+var oracleReservedWords = map[string]bool{+ "ALL": true,+ "ALTER": true,+ "AND": true,+ "ANY": true,+ "AS": true,+ "ASC": true,+ "AT": true,+ "BEGIN": true,+ "BETWEEN": true,+ "BY": true,+ "CASE": true,+ "CHECK": true,+ "CLUSTERS": true,+ "CLUSTER": true,+ "COLAUTH": true,+ "COLUMNS": true,+ "COMPRESS": true,+ "CONNECT": true,+ "CRASH": true,+ "CREATE": true,+ "CURSOR": true,+ "DECLARE": true,+ "DEFAULT": true,+ "DESC": true,+ "DISTINCT": true,+ "DROP": true,+ "ELSE": true,+ "END": true,+ "EXCEPTION": true,+ "EXCLUSIVE": true,+ "FETCH": true,+ "FOR": true,+ "FROM": true,+ "FUNCTION": true,+ "GOTO": true,+ "GRANT": true,+ "GROUP": true,+ "HAVING": true,+ "IDENTIFIED": true,+ "IF": true,+ "IN": true,+ "INDEX": true,+ "INDEXES": true,+ "INSERT": true,+ "INTERSECT": true,+ "INTO": true,+ "IS": true,+ "LIKE": true,+ "LOCK": true,+ "MINUS": true,+ "MODE": true,+ "NOCOMPRESS": true,+ "NOT": true,+ "NOWAIT": true,+ "NULL": true,+ "OF": true,+ "ON": true,+ "OPTION": true,+ "OR": true,+ "ORDER": true,+ "OVERLAPS": true,+ "PROCEDURE": true,+ "PUBLIC": true,+ "RESOURCE": true,+ "REVOKE": true,+ "SELECT": true,+ "SHARE": true,+ "SIZE": true,+ "SQL": true,+ "START": true,+ "SUBTYPE": true,+ "TABAUTH": true,+ "TABLE": true,+ "THEN": true,+ "TO": true,+ "TYPE": true,+ "UNION": true,+ "UNIQUE": true,+ "UPDATE": true,+ "VALUES": true,+ "VIEW": true,+ "VIEWS": true,+ "WHEN": true,+ "WHERE": true,+ "WITH": true,+}++var oracleKeywords = map[string]bool{+ "A": true,+ "ADD": true,+ "ACCESSIBLE": true,+ "AGENT": true,+ "AGGREGATE": true,+ "ARRAY": true,+ "ATTRIBUTE": true,+ "AUTHID": true,+ "AVG": true,+ "BFILE_BASE": true,+ "BINARY": true,+ "BLOB_BASE": true,+ "BLOCK": true,+ "BODY": true,+ "BOTH": true,+ "BOUND": true,+ "BULK": true,+ "BYTE": true,+ "C": true,+ "CALL": true,+ "CALLING": true,+ "CASCADE": true,+ "CHAR": true,+ "CHAR_BASE": true,+ "CHARACTER": true,+ "CHARSET": true,+ "CHARSETFORM": true,+ "CHARSETID": true,+ "CLOB_BASE": true,+ "CLONE": true,+ "CLOSE": true,+ "COLLECT": true,+ "COMMENT": true,+ "COMMIT": true,+ "COMMITTED": true,+ "COMPILED": true,+ "CONSTANT": true,+ "CONSTRUCTOR": true,+ "CONTEXT": true,+ "CONTINUE": true,+ "CONVERT": true,+ "COUNT": true,+ "CREDENTIAL": true,+ "CURRENT": true,+ "CUSTOMDATUM": true,+ "DANGLING": true,+ "DATA": true,+ "DATE": true,+ "DATE_BASE": true,+ "DAY": true,+ "DEFINE": true,+ "DELETE": true,+ "DETERMINISTIC": true,+ "DIRECTORY": true,+ "DOUBLE": true,+ "DURATION": true,+ "ELEMENT": true,+ "ELSIF": true,+ "EMPTY": true,+ "ESCAPE": true,+ "EXCEPT": true,+ "EXCEPTIONS": true,+ "EXECUTE": true,+ "EXISTS": true,+ "EXIT": true,+ "EXTERNAL": true,+ "FINAL": true,+ "FIRST": true,+ "FIXED": true,+ "FLOAT": true,+ "FORALL": true,+ "FORCE": true,+ "GENERAL": true,+ "HASH": true,+ "HEAP": true,+ "HIDDEN": true,+ "HOUR": true,+ "IMMEDIATE": true,+ "IMMUTABLE": true,+ "INCLUDING": true,+ "INDICATOR": true,+ "INDICES": true,+ "INFINITE": true,+ "INSTANTIABLE": true,+ "INT": true,+ "INTERFACE": true,+ "INTERVAL": true,+ "INVALIDATE": true,+ "ISOLATION": true,+ "JAVA": true,+ "LANGUAGE": true,+ "LARGE": true,+ "LEADING": true,+ "LENGTH": true,+ "LEVEL": true,+ "LIBRARY": true,+ "LIKE2": true,+ "LIKE4": true,+ "LIKEC": true,+ "LIMIT": true,+ "LIMITED": true,+ "LOCAL": true,+ "LONG": true,+ "LOOP": true,+ "MAP": true,+ "MAX": true,+ "MAXLEN": true,+ "MEMBER": true,+ "MERGE": true,+ "MIN": true,+ "MINUTE": true,+ "MOD": true,+ "MODIFY": true,+ "MONTH": true,+ "MULTISET": true,+ "MUTABLE": true,+ "NAME": true,+ "NAN": true,+ "NATIONAL": true,+ "NATIVE": true,+ "NCHAR": true,+ "NEW": true,+ "NOCOPY": true,+ "NUMBER_BASE": true,+ "OBJECT": true,+ "OCICOLL": true,+ "OCIDATE": true,+ "OCIDATETIME": true,+ "OCIDURATION": true,+ "OCIINTERVAL": true,+ "OCILOBLOCATOR": true,+ "OCINUMBER": true,+ "OCIRAW": true,+ "OCIREF": true,+ "OCIREFCURSOR": true,+ "OCIROWID": true,+ "OCISTRING": true,+ "OCITYPE": true,+ "OLD": true,+ "ONLY": true,+ "OPAQUE": true,+ "OPEN": true,+ "OPERATOR": true,+ "ORACLE": true,+ "ORADATA": true,+ "ORGANIZATION": true,+ "ORLANY": true,+ "ORLVARY": true,+ "OTHERS": true,+ "OUT": true,+ "OVERRIDING": true,+ "PACKAGE": true,+ "PARALLEL_ENABLE": true,+ "PARAMETER": true,+ "PARAMETERS": true,+ "PARENT": true,+ "PARTITION": true,+ "PASCAL": true,+ "PERSISTABLE": true,+ "PIPE": true,+ "PIPELINED": true,+ "PLUGGABLE": true,+ "POLYMORPHIC": true,+ "PRAGMA": true,+ "PRECISION": true,+ "PRIOR": true,+ "PRIVATE": true,+ "RAISE": true,+ "RANGE": true,+ "RAW": true,+ "READ": true,+ "RECORD": true,+ "REF": true,+ "REFERENCE": true,+ "RELIES_ON": true,+ "REM": true,+ "REMAINDER": true,+ "RENAME": true,+ "RESULT": true,+ "RESULT_CACHE": true,+ "RETURN": true,+ "RETURNING": true,+ "REVERSE": true,+ "ROLLBACK": true,+ "ROW": true,+ "SAMPLE": true,+ "SAVE": true,+ "SAVEPOINT": true,+ "SB1": true,+ "SB2": true,+ "SB4": true,+ "SECOND": true,+ "SEGMENT": true,+ "SELF": true,+ "SEPARATE": true,+ "SEQUENCE": true,+ "SERIALIZABLE": true,+ "SET": true,+ "SHORT": true,+ "SIZE_T": true,+ "SOME": true,+ "SPARSE": true,+ "SQLCODE": true,+ "SQLDATA": true,+ "SQLNAME": true,+ "SQLSTATE": true,+ "STANDARD": true,+ "STATIC": true,+ "STDDEV": true,+ "STORED": true,+ "STRING": true,+ "STRUCT": true,+ "STYLE": true,+ "SUBMULTISET": true,+ "SUBPARTITION": true,+ "SUBSTITUTABLE": true,+ "SUM": true,+ "SYNONYM": true,+ "TDO": true,+ "THE": true,+ "TIME": true,+ "TIMESTAMP": true,+ "TIMEZONE_ABBR": true,+ "TIMEZONE_HOUR": true,+ "TIMEZONE_MINUTE": true,+ "TIMEZONE_REGION": true,+ "TRAILING": true,+ "TRANSACTION": true,+ "TRANSACTIONAL": true,+ "TRUSTED": true,+ "UB1": true,+ "UB2": true,+ "UB4": true,+ "UNDER": true,+ "UNPLUG": true,+ "UNSIGNED": true,+ "UNTRUSTED": true,+ "USE": true,+ "USING": true,+ "VALIST": true,+ "VALUE": true,+ "VARIABLE": true,+ "VARIANCE": true,+ "VARRAY": true,+ "VARYING": true,+ "VOID": true,+ "WHILE": true,+ "WORK": true,+ "WRAPPED": true,+ "WRITE": true,+ "YEAR": true,+ "ZONE": true,+}+// SyntaxError is a syntax error.type SyntaxError struct {Line int@@ -252,11 +596,5 @@ func IsOracleKeyword(text string) bool {return false}- lexer := parser.NewPlSqlLexer(antlr.NewInputStream(text))- for _, keyword := range lexer.GetLiteralNames() {- if strings.EqualFold(strings.Trim(keyword, "'"), text) {- return true- }- }- return false+ return oracleKeywords[strings.ToUpper(text)] || oracleReservedWords[strings.ToUpper(text)]}