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 case rule (#6094)
rebelice71a686fa6fdb
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.goindex deb790de4b..ee30e9dc40 100644--- a/backend/plugin/advisor/advisor.go+++ b/backend/plugin/advisor/advisor.go@@ -385,6 +385,9 @@ const (// OracleIdentifierNamingNoKeyword is an advisor type for Oracle identifier naming convention without keyword.OracleIdentifierNamingNoKeyword Type = "bb.plugin.advisor.oracle.naming.identifier-no-keyword"++ // OracleIdentifierCase is an advisor type for Oracle identifier case.+ OracleIdentifierCase Type = "bb.plugin.advisor.oracle.naming.identifier-case")// Advice is the result of an advisor.diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.goindex cf7147460a..3ee9b6314b 100644--- a/backend/plugin/advisor/code.go+++ b/backend/plugin/advisor/code.go@@ -54,6 +54,8 @@ const (NamingAutoIncrementColumnConventionMismatch Code = 307// 308 name is keyword identifier advisor error code.NameIsKeywordIdentifier Code = 308+ // 309 naming case mismatch advisor error code.+ NamingCaseMismatch Code = 309// 401 ~ 499 column error code.NoRequiredColumn Code = 401diff --git a/backend/plugin/advisor/oracle/advisor_naming_identifier_case.go b/backend/plugin/advisor/oracle/advisor_naming_identifier_case.gonew file mode 100644index 0000000000..13833fc702--- /dev/null+++ b/backend/plugin/advisor/oracle/advisor_naming_identifier_case.go@@ -0,0 +1,103 @@+// Package oracle is the advisor for oracle database.+package oracle++import (+ "fmt"+ "strings"++ "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"+)++var (+ _ advisor.Advisor = (*NamingIdentifierCaseAdvisor)(nil)+)++func init() {+ advisor.Register(db.Oracle, advisor.OracleIdentifierCase, &NamingIdentifierCaseAdvisor{})+}++// NamingIdentifierCaseAdvisor is the advisor checking for identifier case.+type NamingIdentifierCaseAdvisor struct {+}++// Check checks for identifier case.+func (*NamingIdentifierCaseAdvisor) 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+ }+ payload, err := advisor.UnmarshalNamingCaseRulePayload(ctx.Rule.Payload)+ if err != nil {+ return nil, err+ }++ listener := &namingIdentifierCaseListener{+ level: level,+ title: string(ctx.Rule.Type),+ currentSchema: ctx.CurrentSchema,+ namingCase: payload.Case,+ }++ antlr.ParseTreeWalkerDefault.Walk(listener, tree)++ return listener.generateAdvice()+}++// namingIdentifierCaseListener is the listener for identifier case.+type namingIdentifierCaseListener struct {+ *parser.BasePlSqlParserListener++ level advisor.Status+ title string+ currentSchema string+ adviceList []advisor.Advice+ namingCase advisor.NamingCase+}++func (l *namingIdentifierCaseListener) 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 *namingIdentifierCaseListener) EnterId_expression(ctx *parser.Id_expressionContext) {+ identifier := normalizeIDExpression(ctx)+ switch l.namingCase {+ case advisor.NamingCaseLower:+ if identifier != strings.ToLower(identifier) {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: l.level,+ Code: advisor.NamingCaseMismatch,+ Title: l.title,+ Content: fmt.Sprintf("Identifier %q should be lower case", identifier),+ Line: ctx.GetStart().GetLine(),+ })+ }+ case advisor.NamingCaseUpper:+ if identifier != strings.ToUpper(identifier) {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: l.level,+ Code: advisor.NamingCaseMismatch,+ Title: l.title,+ Content: fmt.Sprintf("Identifier %q should be upper case", 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 c304c0d297..3e5671cfd4 100644--- a/backend/plugin/advisor/oracle/oracle_rules_test.go+++ b/backend/plugin/advisor/oracle/oracle_rules_test.go@@ -27,6 +27,7 @@ func TestOracleRules(t *testing.T) {advisor.SchemaRuleColumnMaximumVarcharLength,advisor.SchemaRuleTableNameNoKeyword,advisor.SchemaRuleIdentifierNoKeyword,+ advisor.SchemaRuleIdentifierCase,}for _, rule := range oracleRules {diff --git a/backend/plugin/advisor/oracle/test/naming_identifier_case.yaml b/backend/plugin/advisor/oracle/test/naming_identifier_case.yamlnew file mode 100644index 0000000000..7cd20f8ad8--- /dev/null+++ b/backend/plugin/advisor/oracle/test/naming_identifier_case.yaml@@ -0,0 +1,42 @@+- statement: CREATE TABLE "table"(id int, name varchar(255))+ want:+ - status: WARN+ code: 309+ title: naming.identifier.case+ content: Identifier "table" should be upper case+ line: 1+ details: ""+- statement: CREATE TABLE tt(id int, name varchar(255))+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+ details: ""+- statement: CREATE TABLE "Tt"(id int, name varchar(255))+ want:+ - status: WARN+ code: 309+ title: naming.identifier.case+ content: Identifier "Tt" should be upper case+ 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/sql_review.go b/backend/plugin/advisor/sql_review.goindex b079aabc8f..5fe6e1a14a 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -59,6 +59,8 @@ const (SchemaRuleTableNameNoKeyword SQLReviewRuleType = "naming.table.no-keyword"// SchemaRuleIdentifierNoKeyword enforce the identifier not to use keyword.SchemaRuleIdentifierNoKeyword SQLReviewRuleType = "naming.identifier.no-keyword"+ // SchemaRuleIdentifierCase enforce the identifier case.+ SchemaRuleIdentifierCase SQLReviewRuleType = "naming.identifier.case"// SchemaRuleStatementNoSelectAll disallow 'SELECT *'.SchemaRuleStatementNoSelectAll SQLReviewRuleType = "statement.select.no-select-all"@@ -279,6 +281,10 @@ func (rule *SQLReviewRule) Validate() error {if _, err := UnmarshalStringArrayTypeRulePayload(rule.Payload); err != nil {return err}+ case SchemaRuleIdentifierCase:+ if _, err := UnmarshalNamingCaseRulePayload(rule.Payload); err != nil {+ return err+ }}return nil}@@ -310,6 +316,22 @@ type NumberTypeRulePayload struct {Number int `json:"number"`}+// NamingCase is the naming case for naming rule.+type NamingCase string++const (+ // NamingCaseLower is the lower case for naming rule.+ NamingCaseLower NamingCase = "LOWER"+ // NamingCaseUpper is the upper case for naming rule.+ NamingCaseUpper NamingCase = "UPPER"+)++// NamingCaseRulePayload is the payload for naming case rule.+type NamingCaseRulePayload struct {+ // Case is the naming case for naming rule.+ Case NamingCase `json:"case"`+}+// UnamrshalNamingRulePayloadAsRegexp will unmarshal payload to NamingRulePayload and compile it as regular expression.func UnamrshalNamingRulePayloadAsRegexp(payload string) (*regexp.Regexp, int, error) {var nr NamingRulePayload@@ -438,6 +460,21 @@ func UnmarshalStringArrayTypeRulePayload(payload string) (*StringArrayTypeRulePareturn &trr, nil}+// UnmarshalNamingCaseRulePayload will unmarshal payload to NamingCaseRulePayload.+func UnmarshalNamingCaseRulePayload(payload string) (*NamingCaseRulePayload, error) {+ var ncr NamingCaseRulePayload+ if err := json.Unmarshal([]byte(payload), &ncr); err != nil {+ return nil, errors.Wrapf(err, "failed to unmarshal naming case rule payload %q", payload)+ }+ switch ncr.Case {+ case NamingCaseLower:+ case NamingCaseUpper:+ default:+ return nil, errors.Errorf("invalid naming case %s", ncr.Case)+ }+ return &ncr, nil+}+// SQLReviewCheckContext is the context for SQL review check.type SQLReviewCheckContext struct {Charset string@@ -858,6 +895,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errif engine == db.Oracle {return OracleIdentifierNamingNoKeyword, nil}+ case SchemaRuleIdentifierCase:+ if engine == db.Oracle {+ return OracleIdentifierCase, 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 d842902252..6e9e3aba99 100644--- a/backend/plugin/advisor/utils_for_tests.go+++ b/backend/plugin/advisor/utils_for_tests.go@@ -470,6 +470,10 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType) (string, error) {payload, err = json.Marshal(StringArrayTypeRulePayload{List: []string{"serial", "bigserial", "int", "bigint"},})+ case SchemaRuleIdentifierCase:+ payload, err = json.Marshal(NamingCaseRulePayload{+ Case: NamingCaseUpper,+ })default:return "", errors.Errorf("unknown SQL review type for default payload: %s", ruleTp)}