Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(oracle): parse dblink suffix so alias projection rewrite applies

zipg79c69e509726
diff --git a/agents/drivers/oracle-go/main.go b/agents/drivers/oracle-go/main.go
index a8560f9268..d69bc2272b 100644
--- a/agents/drivers/oracle-go/main.go
+++ b/agents/drivers/oracle-go/main.go
@@ -4524,6 +4524,7 @@ func parseSingleOracleTableRef(fromSQL string) (oracleTableRef, bool) {
ref.Table = second.Name
pos = skipSQLWhitespace(fromSQL, afterSecond)
}
+ pos = parseOracleDatabaseLinkSuffix(fromSQL, pos)
if pos < len(fromSQL) {
if strings.HasPrefix(strings.TrimLeft(fromSQL[pos:], " \t\r\n"), ",") {
return oracleTableRef{}, false
@@ -4558,6 +4559,31 @@ func parseSingleOracleTableRef(fromSQL string) (oracleTableRef, bool) {
return ref, true
}
+// parseOracleDatabaseLinkSuffix consumes the `@link` (or `@!link`) suffix of a
+// remote table reference so that the alias written after the link is still
+// recognized (issues #9171 / #9344). Without it `schema.table@link t` parsed as
+// an unaliased reference, so a `t.*` / `t.column` projection no longer matched
+// the table and go-ora panicked on the user-defined type columns the value
+// rewrite is meant to substitute.
+//
+// A suffix that cannot be read as a link name is left in place, which keeps the
+// previous shape for connect-string links and other unparsable spellings.
+func parseOracleDatabaseLinkSuffix(fromSQL string, pos int) int {
+ if pos >= len(fromSQL) || fromSQL[pos] != '@' {
+ return pos
+ }
+ linkStart := pos + 1
+ if linkStart < len(fromSQL) && fromSQL[linkStart] == '!' {
+ // `table@!link` keeps the same link name; the marker only changes where
+ // the link itself is resolved.
+ linkStart++
+ }
+ if _, afterLink, ok := readOracleIdentifierToken(fromSQL, linkStart); ok {
+ return skipSQLWhitespace(fromSQL, afterLink)
+ }
+ return pos
+}
+
func splitOracleSelectListModifier(selectList string) (string, string) {
trimmedLeft := strings.TrimLeft(selectList, " \t\r\n")
prefixLen := len(selectList) - len(trimmedLeft)
diff --git a/agents/drivers/oracle-go/main_test.go b/agents/drivers/oracle-go/main_test.go
index 4f93f37fba..2e1fcd221a 100644
--- a/agents/drivers/oracle-go/main_test.go
+++ b/agents/drivers/oracle-go/main_test.go
@@ -4226,3 +4226,122 @@ func TestRewriteOracleOpaqueObjectInDeferredProjection(t *testing.T) {
t.Fatalf("deferred rewrite dropped the LOB column: %s", sqlText)
}
}
+
+func TestParseSingleOracleTableRefReadsDatabaseLinkAndAlias(t *testing.T) {
+ cases := []struct {
+ name string
+ from string
+ want oracleTableRef
+ ok bool
+ }{
+ {
+ name: "unqualified link with alias",
+ from: `T_UDT@DBX_LOOP t`,
+ want: oracleTableRef{Table: "T_UDT", Alias: "T", AliasText: "t"},
+ ok: true,
+ },
+ {
+ name: "schema qualified link with alias",
+ from: `DBX_TEST.T_UDT@DBX_LOOP t`,
+ want: oracleTableRef{Schema: "DBX_TEST", Table: "T_UDT", Alias: "T", AliasText: "t"},
+ ok: true,
+ },
+ {
+ name: "remote link marker",
+ from: `DBX_TEST.T_UDT@!DBX_LOOP x`,
+ want: oracleTableRef{Schema: "DBX_TEST", Table: "T_UDT", Alias: "X", AliasText: "x"},
+ ok: true,
+ },
+ {
+ name: "quoted link followed by a clause",
+ from: `DBX_TEST.T_UDT@"Loop Link" WHERE ID > 0`,
+ want: oracleTableRef{Schema: "DBX_TEST", Table: "T_UDT"},
+ ok: true,
+ },
+ {
+ name: "link without alias",
+ from: `DBX_TEST.T_UDT@DBX_LOOP`,
+ want: oracleTableRef{Schema: "DBX_TEST", Table: "T_UDT"},
+ ok: true,
+ },
+ {
+ name: "local table is unchanged",
+ from: `DBX_TEST.T_UDT t`,
+ want: oracleTableRef{Schema: "DBX_TEST", Table: "T_UDT", Alias: "T", AliasText: "t"},
+ ok: true,
+ },
+ {
+ name: "link followed by another table is not a single reference",
+ from: `DBX_TEST.T_UDT@DBX_LOOP, DBX_TEST.OTHER`,
+ ok: false,
+ },
+ {
+ name: "link followed by a join is not a single reference",
+ from: `DBX_TEST.T_UDT@DBX_LOOP JOIN DBX_TEST.OTHER ON 1 = 1`,
+ ok: false,
+ },
+ {
+ name: "connect string link keeps the previous shape",
+ from: `DBX_TEST.T_UDT@'XE'`,
+ want: oracleTableRef{Schema: "DBX_TEST", Table: "T_UDT"},
+ ok: true,
+ },
+ }
+ for _, testCase := range cases {
+ got, ok := parseSingleOracleTableRef(testCase.from)
+ if ok != testCase.ok {
+ t.Fatalf("%s: parseSingleOracleTableRef(%q) ok = %v, want %v", testCase.name, testCase.from, ok, testCase.ok)
+ }
+ if !testCase.ok {
+ continue
+ }
+ if got != testCase.want {
+ t.Fatalf("%s: parseSingleOracleTableRef(%q) = %+v, want %+v", testCase.name, testCase.from, got, testCase.want)
+ }
+ }
+}
+
+func TestRewriteOracleOpaqueObjectProjectionKeepsLinkedAlias(t *testing.T) {
+ var gotSchema, gotTable string
+ sqlText, err := rewriteOracleSelectSQL(
+ `SELECT t.*, ROWIDTOCHAR(t.ROWID) AS "__DBX_PK_0" FROM DBX_TEST.T_UDT@DBX_LOOP t`,
+ func(schema, table string) ([]oracleColumnMeta, error) {
+ gotSchema, gotTable = schema, table
+ return []oracleColumnMeta{
+ {Name: "ID", DataType: "NUMBER"},
+ {Name: "G", DataType: "GEOM_T", DataTypeOwner: "DBX_TEST"},
+ }, nil
+ },
+ false,
+ )
+ if err != nil {
+ t.Fatal(err)
+ }
+ if gotSchema != "DBX_TEST" || gotTable != "T_UDT" {
+ t.Fatalf("column metadata loaded for (%q, %q), want (DBX_TEST, T_UDT)", gotSchema, gotTable)
+ }
+ want := `SELECT t."ID", CASE WHEN t."G" IS NULL THEN NULL ELSE '<GEOM_T>' END AS "G", ROWIDTOCHAR(t.ROWID) AS "__DBX_PK_0" FROM DBX_TEST.T_UDT@DBX_LOOP t`
+ if sqlText != want {
+ t.Fatalf("rewriteOracleSelectSQL() = %s, want %s", sqlText, want)
+ }
+}
+
+func TestRewriteOracleOpaqueObjectExplicitColumnKeepsLinkedAlias(t *testing.T) {
+ sqlText, err := rewriteOracleSelectSQL(
+ `SELECT t.ID, t.G FROM T_UDT@DBX_LOOP t`,
+ func(schema, table string) ([]oracleColumnMeta, error) {
+ return []oracleColumnMeta{
+ {Name: "ID", DataType: "NUMBER"},
+ {Name: "G", DataType: "GEOM_T", DataTypeOwner: "DBX_TEST"},
+ }, nil
+ },
+ false,
+ )
+ if err != nil {
+ t.Fatal(err)
+ }
+ want := `SELECT t.ID, CASE WHEN t."G" IS NULL THEN NULL ELSE '<GEOM_T>' END AS "G" FROM T_UDT@DBX_LOOP t`
+ if sqlText != want {
+ t.Fatalf("rewriteOracleSelectSQL() = %s, want %s", sqlText, want)
+ }
+}