Skip to content

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

fix(advisor): check only defined names in Oracle identifier rules (#21639)

rebelice5f374e778273
diff --git a/backend/plugin/advisor/oracle/test/naming_identifier_case.yaml b/backend/plugin/advisor/oracle/test/naming_identifier_case.yaml
index 44cb06c98c..df2098c64b 100644
--- a/backend/plugin/advisor/oracle/test/naming_identifier_case.yaml
+++ b/backend/plugin/advisor/oracle/test/naming_identifier_case.yaml
@@ -28,3 +28,30 @@
create table techBook(id int);
ALTER TABLE techBook RENAME TO tech_book_tmp;
changeType: 1
+- statement: |-
+ DECLARE
+ "v_Count" NUMBER;
+ BEGIN
+ SELECT COUNT(1) INTO "v_Count" FROM "Tt";
+ END;
+ changeType: 1
+ want:
+ - status: 2
+ code: 309
+ title: NAMING_IDENTIFIER_CASE
+ content: Identifier "v_Count" should be upper case
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
+- statement: CREATE CLUSTER "MixedCase" (id NUMBER)
+ changeType: 1
+ want:
+ - status: 2
+ code: 309
+ title: NAMING_IDENTIFIER_CASE
+ content: Identifier "MixedCase" should be upper case
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/oracle/test/naming_identifier_no_keyword.yaml b/backend/plugin/advisor/oracle/test/naming_identifier_no_keyword.yaml
index e212c82f62..33b12ff939 100644
--- a/backend/plugin/advisor/oracle/test/naming_identifier_no_keyword.yaml
+++ b/backend/plugin/advisor/oracle/test/naming_identifier_no_keyword.yaml
@@ -53,3 +53,113 @@
changeType: 1
- statement: GRANT SELECT, INSERT, UPDATE, DELETE ON my_table TO admin;
changeType: 1
+- statement: |-
+ CREATE OR REPLACE PROCEDURE p_demo AS
+ v NUMBER;
+ x NUMBER;
+ n NUMBER;
+ TYPE t_nos IS TABLE OF NUMBER;
+ coll t_nos;
+ BEGIN
+ SELECT COUNT(1) INTO v FROM t;
+ x := coll.COUNT;
+ n := SQL%ROWCOUNT;
+ RAISE_APPLICATION_ERROR(-20001, SQLCODE || SQLERRM);
+ END;
+ changeType: 1
+- statement: |-
+ DECLARE
+ TYPE t_nos IS TABLE OF NUMBER;
+ l_record_nos t_nos;
+ CURSOR c_rows IS SELECT id FROM t;
+ BEGIN
+ SELECT id BULK COLLECT INTO l_record_nos FROM t;
+ FORALL i IN 1..l_record_nos.COUNT
+ DELETE FROM t WHERE id = l_record_nos(i);
+ IF SQL%ROWCOUNT = 0 AND l_record_nos.FIRST IS NOT NULL THEN
+ NULL;
+ END IF;
+ OPEN c_rows;
+ IF c_rows%NOTFOUND THEN
+ NULL;
+ END IF;
+ CLOSE c_rows;
+ EXCEPTION
+ WHEN OTHERS THEN
+ DBMS_OUTPUT.PUT_LINE(SQLCODE || ': ' || SQLERRM);
+ END;
+ changeType: 1
+- statement: |-
+ DECLARE
+ name VARCHAR2(10);
+ BEGIN
+ SELECT COUNT(1) INTO name FROM t;
+ END;
+ changeType: 1
+ want:
+ - status: 2
+ code: 308
+ title: NAMING_IDENTIFIER_NO_KEYWORD
+ content: Identifier "NAME" is a keyword and should be avoided
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
+- statement: CREATE TABLE t_log (id NUMBER, "PARTITION" NUMBER, "TIMESTAMP" DATE)
+ changeType: 1
+ want:
+ - status: 2
+ code: 308
+ title: NAMING_IDENTIFIER_NO_KEYWORD
+ content: Identifier "PARTITION" is a keyword and should be avoided
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+ - status: 2
+ code: 308
+ title: NAMING_IDENTIFIER_NO_KEYWORD
+ content: Identifier "TIMESTAMP" is a keyword and should be avoided
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: SELECT COUNT(*) AS "DATE" FROM t
+ changeType: 1
+ want:
+ - status: 2
+ code: 308
+ title: NAMING_IDENTIFIER_NO_KEYWORD
+ content: Identifier "DATE" is a keyword and should be avoided
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: UPDATE t SET name = 1 WHERE id = 1
+ changeType: 1
+- statement: ALTER TABLE t DROP CONSTRAINT "LEVEL"
+ changeType: 1
+- statement: CREATE CLUSTER "LEVEL" (id NUMBER)
+ changeType: 1
+ want:
+ - status: 2
+ code: 308
+ title: NAMING_IDENTIFIER_NO_KEYWORD
+ content: Identifier "LEVEL" is a keyword and should be avoided
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: CREATE DIRECTORY "LEVEL" AS '/tmp'
+ changeType: 1
+ want:
+ - status: 2
+ code: 308
+ title: NAMING_IDENTIFIER_NO_KEYWORD
+ content: Identifier "LEVEL" is a keyword and should be avoided
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: DROP DIRECTORY "LEVEL"
+ changeType: 1
diff --git a/backend/plugin/advisor/oracle/utils.go b/backend/plugin/advisor/oracle/utils.go
index 65888987ea..5165b2c314 100644
--- a/backend/plugin/advisor/oracle/utils.go
+++ b/backend/plugin/advisor/oracle/utils.go
@@ -2,6 +2,7 @@ package oracle
import (
"fmt"
+ "reflect"
"strconv"
"strings"
@@ -145,6 +146,11 @@ func omniColumnHasConstraint(col *ast.ColumnDef, typ ast.ConstraintType) bool {
return false
}
+// omniIdentifiers returns the names a statement defines: created objects and
+// their columns, constraints, and partitions; aliases and CTEs; rename targets;
+// and PL/SQL declarations. References are excluded because the author did not
+// choose those names, including built-ins such as COUNT, coll.COUNT,
+// SQL%ROWCOUNT, and SQLCODE.
func omniIdentifiers(node ast.Node) []omniIdentifier {
var result []omniIdentifier
seen := make(map[string]bool)
@@ -159,36 +165,127 @@ func omniIdentifiers(node ast.Node) []omniIdentifier {
seen[key] = true
result = append(result, omniIdentifier{name: name, loc: loc})
}
+ addObject := func(name *ast.ObjectName) {
+ if name != nil {
+ add(name.Name, name.Loc)
+ }
+ }
+ addStrings := func(list *ast.List, loc ast.Loc) {
+ for _, name := range omniListStrings(list) {
+ add(name, loc)
+ }
+ }
+ // A TableConstraint node also names the target of DROP CONSTRAINT, so
+ // constraint names are taken from the statements that define them.
+ addTableConstraints := func(list *ast.List) {
+ for _, c := range omniTableConstraints(list) {
+ add(c.Name, c.Loc)
+ }
+ }
omniWalk(node, func(n ast.Node) {
+ add(omniCreatedName(n))
switch x := n.(type) {
- case *ast.ObjectName:
- add(x.Schema, x.Loc)
- add(x.Name, x.Loc)
+ case *ast.CreateTableStmt:
+ addTableConstraints(x.Constraints)
+ case *ast.CreateViewStmt:
+ addStrings(x.Columns, x.Loc)
+ addTableConstraints(x.Constraints)
+ add(x.ConstraintName, x.Loc)
+ case *ast.AdminDDLStmt:
+ if x.Action == "CREATE" {
+ addObject(x.Name)
+ }
+ case *ast.RenameStmt:
+ addObject(x.NewName)
+ case *ast.AlterTableCmd:
+ switch x.Action {
+ case ast.AT_ADD_CONSTRAINT:
+ if x.Constraint != nil {
+ add(x.Constraint.Name, x.Constraint.Loc)
+ }
+ case ast.AT_RENAME, ast.AT_RENAME_COLUMN, ast.AT_RENAME_CONSTRAINT, ast.AT_RENAME_PARTITION:
+ add(x.NewName, x.Loc)
+ default:
+ }
+ case *ast.AlterIndexStmt:
+ add(x.NewName, x.Loc)
+ case *ast.AlterTriggerStmt:
+ add(x.NewName, x.Loc)
case *ast.ColumnDef:
add(x.Name, x.Loc)
- case *ast.ColumnRef:
- add(x.Schema, x.Loc)
- add(x.Table, x.Loc)
- add(x.Column, x.Loc)
+ case *ast.ColumnConstraint:
+ add(x.Name, x.Loc)
+ case *ast.PartitionDef:
+ add(x.Name, x.Loc)
+ case *ast.ResTarget:
+ add(x.Name, x.Loc)
case *ast.Alias:
add(x.Name, x.Loc)
- case *ast.ColumnConstraint:
+ addStrings(x.Cols, x.Loc)
+ case *ast.CTE:
add(x.Name, x.Loc)
- case *ast.TableConstraint:
+ addStrings(x.Columns, x.Loc)
+ case *ast.Parameter:
add(x.Name, x.Loc)
- for _, name := range omniListStrings(x.Columns) {
- add(name, x.Loc)
+ case *ast.PLSQLVarDecl:
+ add(x.Name, x.Loc)
+ case *ast.PLSQLCursorDecl:
+ add(x.Name, x.Loc)
+ case *ast.PLSQLTypeDecl:
+ add(x.Name, x.Loc)
+ case *ast.PLSQLSubtypeDecl:
+ add(x.Name, x.Loc)
+ case *ast.PLSQLLoop:
+ // Loops with an Iteration declare their variables as PLSQLIterand nodes.
+ if x.Iteration == nil {
+ add(x.Iterator, x.Loc)
}
- case *ast.CTE:
+ case *ast.PLSQLIterand:
add(x.Name, x.Loc)
- case *ast.CommentStmt:
- add(x.Column, x.Loc)
+ case *ast.PLSQLForall:
+ add(x.Index, x.Loc)
default:
}
})
return result
}
+// omniCreatedName returns the Name field of any omni Create*Stmt node, so
+// CREATE statements omni adds later are checked without being listed here.
+func omniCreatedName(node ast.Node) (string, ast.Loc) {
+ value := reflect.ValueOf(node)
+ if value.Kind() != reflect.Pointer || value.IsNil() {
+ return "", ast.Loc{}
+ }
+ elem := value.Elem()
+ if elem.Kind() != reflect.Struct {
+ return "", ast.Loc{}
+ }
+ typeName := elem.Type().Name()
+ if !strings.HasPrefix(typeName, "Create") || !strings.HasSuffix(typeName, "Stmt") {
+ return "", ast.Loc{}
+ }
+ nameField := elem.FieldByName("Name")
+ if !nameField.IsValid() {
+ return "", ast.Loc{}
+ }
+ switch name := nameField.Interface().(type) {
+ case *ast.ObjectName:
+ if name != nil {
+ return name.Name, name.Loc
+ }
+ case string:
+ if locField := elem.FieldByName("Loc"); locField.IsValid() {
+ if loc, ok := locField.Interface().(ast.Loc); ok {
+ return name, loc
+ }
+ }
+ return name, ast.Loc{}
+ default:
+ }
+ return "", ast.Loc{}
+}
+
func omniWalk(node ast.Node, visit func(ast.Node)) {
ast.Inspect(node, func(n ast.Node) bool {
visit(n)