fix(jdbc): preserve discovered schemas in object trees

Closes #10870
This commit is contained in:
t8y2 committed 2026-10-02 04:03:57 +00:00
1 parent 14c152f1eb
commit cc9f8e3358
4 files changed
+206 -12

No files matched your search

@@ -431,6 +431,45 @@ describe("query execution schema", () => {
});
describe("object tree node schema", () => {
it.each(["APP", "OTHER", "app", "A_B", "AXB", "_SYS_EPM", "A%B"])("preserves discovered unknown JDBC schema %s without changing SQL dialect or execution context", (schema) => {
const connection = {
db_type: "jdbc" as const,
connection_string: "jdbc:sap://localhost:443/?currentschema=APP",
jdbc_driver_class: "com.sap.db.jdbc.Driver",
};
expect(inferJdbcDialect(connection)).toBeUndefined();
expect(effectiveDatabaseTypeForConnection(connection)).toBe("jdbc");
expect(connectionShouldDiscoverJdbcSchemas(connection)).toBe(true);
expect(connectionUsesDatabaseObjectTreeMode(connection)).toBe(true);
expect(connectionObjectTreeQuerySchema(connection, "catalog", schema)).toBe(schema);
expect(connectionObjectTreeNodeSchema(connection, "catalog", schema)).toBe(schema);
expect(connectionQueryExecutionSchema(connection, "catalog", schema, false)).toBeUndefined();
});
it.each([undefined, ""])("keeps unknown JDBC catalog fallback when schema is %s", (schema) => {
const connection = { db_type: "jdbc" as const, connection_string: "jdbc:example://localhost/catalog" };
expect(connectionObjectTreeQuerySchema(connection, "catalog", schema)).toBe("");
expect(connectionObjectTreeNodeSchema(connection, "catalog", schema)).toBeUndefined();
});
it.each([
{ db_type: "saphana", query: "APP", node: "APP", fallbackQuery: "", fallbackNode: undefined },
{ db_type: "mysql", query: "APP", node: undefined, fallbackQuery: "catalog", fallbackNode: undefined },
{ db_type: "oracle", query: "APP", node: "APP", fallbackQuery: "catalog", fallbackNode: "catalog" },
{ db_type: "jdbc", driver_profile: "mysql", query: "", node: undefined, fallbackQuery: "", fallbackNode: undefined },
{ db_type: "jdbc", driver_profile: "oracle", query: "APP", node: "APP", fallbackQuery: "catalog", fallbackNode: "catalog" },
{ db_type: "jdbc", driver_profile: "databend", connection_string: "jdbc:databend://localhost:8000/catalog", query: "APP", node: "APP", fallbackQuery: "catalog", fallbackNode: "catalog" },
{ db_type: "jdbc", driver_profile: "postgres", query: "APP", node: "APP", fallbackQuery: "", fallbackNode: undefined },
{ db_type: "jdbc", driver_profile: "phoenix", query: "APP", node: "APP", fallbackQuery: "", fallbackNode: undefined },
] as const)("keeps $db_type/$driver_profile schema rules", ({ db_type, query, node, fallbackQuery, fallbackNode, ...identity }) => {
const connection = { db_type, ...identity };
expect(connectionShouldDiscoverJdbcSchemas(connection)).toBe(false);
expect(connectionObjectTreeQuerySchema(connection, "catalog", "APP")).toBe(query);
expect(connectionObjectTreeNodeSchema(connection, "catalog", "APP")).toBe(node);
expect(connectionObjectTreeQuerySchema(connection, "catalog")).toBe(fallbackQuery);
expect(connectionObjectTreeNodeSchema(connection, "catalog")).toBe(fallbackNode);
});
it("ignores database-shaped schema metadata for MySQL tables", () => {
expect(connectionObjectTreeNodeSchema({ db_type: "mysql" }, "app", "app")).toBeUndefined();
});
@@ -278,6 +278,9 @@ function databaseNameIsNotASchema(type: DatabaseType | undefined): boolean {
}
export function connectionObjectTreeQuerySchema(connection: JdbcDialectConnection | undefined, database: string, schema?: string): string {
// Unknown JDBC drivers default to a flat tree, but can discover schemas.
// Keep that explicit scope: an empty JDBC metadata schema is unrestricted.
if (schema && connection?.db_type === "jdbc" && !inferJdbcDialect(connection)) return schema;
if (connection?.db_type === "jdbc" && inferJdbcDialect(connection) === "databend") return schema || database;
if (connectionUsesDatabaseObjectTreeMode(connection)) return "";
const type = effectiveDatabaseTypeForConnection(connection);
@@ -325,6 +328,8 @@ export function metadataSchemaForConnection(connection: JdbcDialectConnection |
}
export function connectionObjectTreeNodeSchema(connection: JdbcDialectConnection | undefined, database: string, schema?: string): string | undefined {
// Child nodes and cache identities must retain the metadata request's scope.
if (schema && connection?.db_type === "jdbc" && !inferJdbcDialect(connection)) return schema;
if (connection?.db_type === "jdbc" && inferJdbcDialect(connection) === "databend") return schema || database;
if (connectionUsesDatabaseObjectTreeMode(connection)) return undefined;
const type = effectiveDatabaseTypeForConnection(connection);